loft-cli 0.6.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loft_cli/__init__.py +3 -0
- loft_cli/_builtins.py +269 -0
- loft_cli/addons/__init__.py +12 -0
- loft_cli/addons/goss/__init__.py +1 -0
- loft_cli/addons/goss/generator.py +202 -0
- loft_cli/addons/goss/renderer.py +207 -0
- loft_cli/addons/goss/shipper.py +174 -0
- loft_cli/agent_installer.py +65 -0
- loft_cli/checks/__init__.py +0 -0
- loft_cli/checks/compose.py +147 -0
- loft_cli/checks/container.py +24 -0
- loft_cli/checks/http.py +30 -0
- loft_cli/checks/nginx.py +21 -0
- loft_cli/checks/ports.py +24 -0
- loft_cli/checks/postgres.py +21 -0
- loft_cli/checks/ssh.py +66 -0
- loft_cli/checks/wireguard.py +21 -0
- loft_cli/cli.py +1161 -0
- loft_cli/compiler/__init__.py +0 -0
- loft_cli/compiler/normalizer.py +420 -0
- loft_cli/compiler/parser.py +42 -0
- loft_cli/compiler/planner.py +2543 -0
- loft_cli/local/__init__.py +0 -0
- loft_cli/local/ddl/__init__.py +0 -0
- loft_cli/local/ddl/bootstrap_tables.py +75 -0
- loft_cli/local/ddl/versionize_system.py +374 -0
- loft_cli/local/inventory.py +449 -0
- loft_cli/local/inventory_db.py +255 -0
- loft_cli/local/keys.py +45 -0
- loft_cli/local/remove.py +135 -0
- loft_cli/local/ssh_config.py +118 -0
- loft_cli/local/tunnel.py +228 -0
- loft_cli/local/wireguard_store.py +138 -0
- loft_cli/logs/__init__.py +0 -0
- loft_cli/logs/reader.py +55 -0
- loft_cli/logs/writer.py +58 -0
- loft_cli/runtime/__init__.py +0 -0
- loft_cli/runtime/agent_transport.py +115 -0
- loft_cli/runtime/executor.py +798 -0
- loft_cli/runtime/fabric_transport.py +68 -0
- loft_cli/runtime/secret_rotation.py +99 -0
- loft_cli/runtime/ssh.py +147 -0
- loft_cli/runtime/steps/__init__.py +0 -0
- loft_cli/runtime/steps/backup.py +72 -0
- loft_cli/runtime/steps/bootstrap.py +227 -0
- loft_cli/runtime/steps/compose.py +43 -0
- loft_cli/runtime/steps/container.py +40 -0
- loft_cli/runtime/steps/docker.py +15 -0
- loft_cli/runtime/steps/file_template.py +25 -0
- loft_cli/runtime/steps/nginx.py +126 -0
- loft_cli/runtime/steps/postgres.py +101 -0
- loft_cli/runtime/steps/postgres_ensure.py +141 -0
- loft_cli/runtime/steps/systemd.py +131 -0
- loft_cli/runtime/steps/wireguard.py +76 -0
- loft_cli/runtime/transport.py +43 -0
- loft_cli/updater.py +208 -0
- loft_cli-0.6.3.dist-info/METADATA +19 -0
- loft_cli-0.6.3.dist-info/RECORD +61 -0
- loft_cli-0.6.3.dist-info/WHEEL +5 -0
- loft_cli-0.6.3.dist-info/entry_points.txt +5 -0
- loft_cli-0.6.3.dist-info/top_level.txt +1 -0
loft_cli/__init__.py
ADDED
loft_cli/_builtins.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""Register the built-in core spec kinds: bootstrap and service.
|
|
2
|
+
|
|
3
|
+
All imports are lazy (inside function bodies) to avoid circular import
|
|
4
|
+
issues between the compiler, specs, runtime, and registry modules.
|
|
5
|
+
This module is called exactly once by load_addons() at CLI startup.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _register_builtins() -> None:
|
|
12
|
+
"""Register bootstrap and service kinds across all registries."""
|
|
13
|
+
_register_resolvers()
|
|
14
|
+
_register_specs()
|
|
15
|
+
_register_normalizers()
|
|
16
|
+
_register_validators()
|
|
17
|
+
_register_planners()
|
|
18
|
+
_register_step_handlers()
|
|
19
|
+
_register_hooks()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _register_specs() -> None:
|
|
23
|
+
from loft_cli_core.registry.specs import register_spec_kind
|
|
24
|
+
from loft_cli_core.specs.backup_job_schema import BackupJobSpec
|
|
25
|
+
from loft_cli_core.specs.bootstrap_schema import BootstrapSpec
|
|
26
|
+
from loft_cli_core.specs.compose_project_schema import ComposeProjectSpec
|
|
27
|
+
from loft_cli_core.specs.file_template_schema import FileTemplateSpec
|
|
28
|
+
from loft_cli_core.specs.http_check_schema import HttpCheckSpec
|
|
29
|
+
from loft_cli_core.specs.postgres_ensure_schema import PostgresEnsureSpec
|
|
30
|
+
from loft_cli_core.specs.service_schema import ServiceSpec
|
|
31
|
+
from loft_cli_core.specs.stack_schema import StackSpec
|
|
32
|
+
from loft_cli_core.specs.systemd_timer_schema import SystemdTimerSpec
|
|
33
|
+
from loft_cli_core.specs.systemd_unit_schema import SystemdUnitSpec
|
|
34
|
+
|
|
35
|
+
register_spec_kind("bootstrap", BootstrapSpec)
|
|
36
|
+
register_spec_kind("service", ServiceSpec)
|
|
37
|
+
register_spec_kind("file_template", FileTemplateSpec)
|
|
38
|
+
register_spec_kind("compose_project", ComposeProjectSpec)
|
|
39
|
+
register_spec_kind("stack", StackSpec)
|
|
40
|
+
register_spec_kind("http_check", HttpCheckSpec)
|
|
41
|
+
register_spec_kind("backup_job", BackupJobSpec)
|
|
42
|
+
register_spec_kind("systemd_unit", SystemdUnitSpec)
|
|
43
|
+
register_spec_kind("systemd_timer", SystemdTimerSpec)
|
|
44
|
+
register_spec_kind("postgres_ensure", PostgresEnsureSpec)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _register_normalizers() -> None:
|
|
48
|
+
from loft_cli.compiler.normalizer import (
|
|
49
|
+
_normalize_backup_job,
|
|
50
|
+
_normalize_bootstrap,
|
|
51
|
+
_normalize_compose_project,
|
|
52
|
+
_normalize_file_template,
|
|
53
|
+
_normalize_http_check,
|
|
54
|
+
_normalize_postgres_ensure,
|
|
55
|
+
_normalize_service,
|
|
56
|
+
_normalize_stack,
|
|
57
|
+
_normalize_systemd_timer,
|
|
58
|
+
_normalize_systemd_unit,
|
|
59
|
+
)
|
|
60
|
+
from loft_cli_core.registry.normalizers import register_normalizer
|
|
61
|
+
|
|
62
|
+
register_normalizer("bootstrap", _normalize_bootstrap)
|
|
63
|
+
register_normalizer("service", _normalize_service)
|
|
64
|
+
register_normalizer("file_template", _normalize_file_template)
|
|
65
|
+
register_normalizer("compose_project", _normalize_compose_project)
|
|
66
|
+
register_normalizer("stack", _normalize_stack)
|
|
67
|
+
register_normalizer("http_check", _normalize_http_check)
|
|
68
|
+
register_normalizer("backup_job", _normalize_backup_job)
|
|
69
|
+
register_normalizer("postgres_ensure", _normalize_postgres_ensure)
|
|
70
|
+
register_normalizer("systemd_unit", _normalize_systemd_unit)
|
|
71
|
+
register_normalizer("systemd_timer", _normalize_systemd_timer)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _register_validators() -> None:
|
|
75
|
+
from loft_cli_core.registry.validators import register_validator
|
|
76
|
+
from loft_cli_core.specs.validators import (
|
|
77
|
+
validate_backup_job,
|
|
78
|
+
validate_bootstrap,
|
|
79
|
+
validate_compose_project,
|
|
80
|
+
validate_file_template,
|
|
81
|
+
validate_http_check,
|
|
82
|
+
validate_postgres_ensure,
|
|
83
|
+
validate_service,
|
|
84
|
+
validate_stack,
|
|
85
|
+
validate_systemd_timer,
|
|
86
|
+
validate_systemd_unit,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
register_validator("bootstrap", validate_bootstrap)
|
|
90
|
+
register_validator("service", validate_service)
|
|
91
|
+
register_validator("file_template", validate_file_template)
|
|
92
|
+
register_validator("compose_project", validate_compose_project)
|
|
93
|
+
register_validator("stack", validate_stack)
|
|
94
|
+
register_validator("http_check", validate_http_check)
|
|
95
|
+
register_validator("backup_job", validate_backup_job)
|
|
96
|
+
register_validator("postgres_ensure", validate_postgres_ensure)
|
|
97
|
+
register_validator("systemd_unit", validate_systemd_unit)
|
|
98
|
+
register_validator("systemd_timer", validate_systemd_timer)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _register_planners() -> None:
|
|
102
|
+
from loft_cli.compiler.planner import (
|
|
103
|
+
_plan_backup_job,
|
|
104
|
+
_plan_bootstrap,
|
|
105
|
+
_plan_compose_project,
|
|
106
|
+
_plan_file_template,
|
|
107
|
+
_plan_http_check,
|
|
108
|
+
_plan_postgres_ensure,
|
|
109
|
+
_plan_service,
|
|
110
|
+
_plan_stack,
|
|
111
|
+
_plan_systemd_timer,
|
|
112
|
+
_plan_systemd_unit,
|
|
113
|
+
)
|
|
114
|
+
from loft_cli_core.registry.planners import register_planner
|
|
115
|
+
|
|
116
|
+
register_planner("bootstrap", _plan_bootstrap)
|
|
117
|
+
register_planner("service", _plan_service)
|
|
118
|
+
register_planner("file_template", _plan_file_template)
|
|
119
|
+
register_planner("compose_project", _plan_compose_project)
|
|
120
|
+
register_planner("stack", _plan_stack)
|
|
121
|
+
register_planner("http_check", _plan_http_check)
|
|
122
|
+
register_planner("backup_job", _plan_backup_job)
|
|
123
|
+
register_planner("postgres_ensure", _plan_postgres_ensure)
|
|
124
|
+
register_planner("systemd_unit", _plan_systemd_unit)
|
|
125
|
+
register_planner("systemd_timer", _plan_systemd_timer)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _register_step_handlers() -> None:
|
|
129
|
+
from loft_cli_core.plan.models import StepKind
|
|
130
|
+
from loft_cli_core.registry.executors import register_step_handler
|
|
131
|
+
|
|
132
|
+
# Wrap Executor instance methods: handler(executor, step) -> StepResult.
|
|
133
|
+
# The executor's private _execute_* methods are left entirely unchanged.
|
|
134
|
+
register_step_handler(StepKind.GATE, lambda ex, step: ex._execute_gate(step))
|
|
135
|
+
register_step_handler(StepKind.SSH_COMMAND, lambda ex, step: ex._execute_ssh_command(step))
|
|
136
|
+
register_step_handler(StepKind.SSH_UPLOAD, lambda ex, step: ex._execute_ssh_upload(step))
|
|
137
|
+
register_step_handler(
|
|
138
|
+
StepKind.LOCAL_FILE_WRITE, lambda ex, step: ex._execute_local_file_write(step)
|
|
139
|
+
)
|
|
140
|
+
register_step_handler(
|
|
141
|
+
StepKind.LOCAL_DB_WRITE, lambda ex, step: ex._execute_local_db_write(step)
|
|
142
|
+
)
|
|
143
|
+
register_step_handler(StepKind.LOCAL_COMMAND, lambda ex, step: ex._execute_local_command(step))
|
|
144
|
+
register_step_handler(StepKind.VERIFY, lambda ex, step: ex._execute_verify(step))
|
|
145
|
+
register_step_handler(
|
|
146
|
+
StepKind.COMPOSE_HEALTH_CHECK,
|
|
147
|
+
lambda ex, step: ex._execute_compose_health_check(step),
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _register_hooks() -> None:
|
|
152
|
+
from loft_cli.local.inventory import (
|
|
153
|
+
record_backup_job_apply,
|
|
154
|
+
record_bootstrap,
|
|
155
|
+
record_compose_project_apply,
|
|
156
|
+
record_file_template_apply,
|
|
157
|
+
record_http_check_apply,
|
|
158
|
+
record_postgres_ensure_apply,
|
|
159
|
+
record_service_apply,
|
|
160
|
+
record_stack_apply,
|
|
161
|
+
record_systemd_timer_apply,
|
|
162
|
+
record_systemd_unit_apply,
|
|
163
|
+
)
|
|
164
|
+
from loft_cli_core.registry.hooks import KindHooks, register_kind_hooks
|
|
165
|
+
|
|
166
|
+
register_kind_hooks(
|
|
167
|
+
"bootstrap",
|
|
168
|
+
KindHooks(
|
|
169
|
+
needs_key_generation=True,
|
|
170
|
+
ssh_port_fallback=True,
|
|
171
|
+
on_inventory_record=record_bootstrap,
|
|
172
|
+
),
|
|
173
|
+
)
|
|
174
|
+
register_kind_hooks(
|
|
175
|
+
"service",
|
|
176
|
+
KindHooks(
|
|
177
|
+
needs_key_generation=False,
|
|
178
|
+
ssh_port_fallback=False,
|
|
179
|
+
on_inventory_record=record_service_apply,
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
register_kind_hooks(
|
|
183
|
+
"file_template",
|
|
184
|
+
KindHooks(
|
|
185
|
+
needs_key_generation=False,
|
|
186
|
+
ssh_port_fallback=False,
|
|
187
|
+
on_inventory_record=record_file_template_apply,
|
|
188
|
+
),
|
|
189
|
+
)
|
|
190
|
+
register_kind_hooks(
|
|
191
|
+
"compose_project",
|
|
192
|
+
KindHooks(
|
|
193
|
+
needs_key_generation=False,
|
|
194
|
+
ssh_port_fallback=False,
|
|
195
|
+
on_inventory_record=record_compose_project_apply,
|
|
196
|
+
),
|
|
197
|
+
)
|
|
198
|
+
register_kind_hooks(
|
|
199
|
+
"stack",
|
|
200
|
+
KindHooks(
|
|
201
|
+
needs_key_generation=False,
|
|
202
|
+
ssh_port_fallback=False,
|
|
203
|
+
on_inventory_record=record_stack_apply,
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
register_kind_hooks(
|
|
207
|
+
"http_check",
|
|
208
|
+
KindHooks(
|
|
209
|
+
needs_key_generation=False,
|
|
210
|
+
ssh_port_fallback=False,
|
|
211
|
+
on_inventory_record=record_http_check_apply,
|
|
212
|
+
),
|
|
213
|
+
)
|
|
214
|
+
register_kind_hooks(
|
|
215
|
+
"backup_job",
|
|
216
|
+
KindHooks(
|
|
217
|
+
needs_key_generation=False,
|
|
218
|
+
ssh_port_fallback=False,
|
|
219
|
+
on_inventory_record=record_backup_job_apply,
|
|
220
|
+
),
|
|
221
|
+
)
|
|
222
|
+
register_kind_hooks(
|
|
223
|
+
"postgres_ensure",
|
|
224
|
+
KindHooks(
|
|
225
|
+
needs_key_generation=False,
|
|
226
|
+
ssh_port_fallback=False,
|
|
227
|
+
on_inventory_record=record_postgres_ensure_apply,
|
|
228
|
+
),
|
|
229
|
+
)
|
|
230
|
+
register_kind_hooks(
|
|
231
|
+
"systemd_unit",
|
|
232
|
+
KindHooks(
|
|
233
|
+
needs_key_generation=False,
|
|
234
|
+
ssh_port_fallback=False,
|
|
235
|
+
on_inventory_record=record_systemd_unit_apply,
|
|
236
|
+
),
|
|
237
|
+
)
|
|
238
|
+
register_kind_hooks(
|
|
239
|
+
"systemd_timer",
|
|
240
|
+
KindHooks(
|
|
241
|
+
needs_key_generation=False,
|
|
242
|
+
ssh_port_fallback=False,
|
|
243
|
+
on_inventory_record=record_systemd_timer_apply,
|
|
244
|
+
),
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _register_resolvers() -> None:
|
|
249
|
+
"""Register the built-in value resolvers: 'env' and 'file'."""
|
|
250
|
+
import os
|
|
251
|
+
from pathlib import Path
|
|
252
|
+
|
|
253
|
+
from loft_cli_core.registry.resolvers import register_resolver
|
|
254
|
+
|
|
255
|
+
def _resolve_env(key: str) -> str | None:
|
|
256
|
+
return os.environ.get(key)
|
|
257
|
+
|
|
258
|
+
def _resolve_file(key: str) -> str | None:
|
|
259
|
+
path = Path(key).expanduser()
|
|
260
|
+
if not path.is_absolute():
|
|
261
|
+
path = Path.cwd() / path
|
|
262
|
+
if not path.exists():
|
|
263
|
+
return None
|
|
264
|
+
content = path.read_text(encoding="utf-8")
|
|
265
|
+
# Strip a single trailing newline — common in key files, config files, etc.
|
|
266
|
+
return content.rstrip("\n")
|
|
267
|
+
|
|
268
|
+
register_resolver("env", _resolve_env)
|
|
269
|
+
register_resolver("file", _resolve_file)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""loft-cli built-in addons.
|
|
2
|
+
|
|
3
|
+
This package contains optional built-in components that ship with loft-cli
|
|
4
|
+
but are architecturally separate from the core compiler/runtime pipeline.
|
|
5
|
+
Each addon is a self-contained subpackage.
|
|
6
|
+
|
|
7
|
+
Current built-in addons:
|
|
8
|
+
goss/ -- Goss server-state verification (post-bootstrap)
|
|
9
|
+
|
|
10
|
+
External addons are distributed as separate Python packages and discovered
|
|
11
|
+
at runtime via the 'loft_cli.addons' entry_points group.
|
|
12
|
+
"""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Goss integration: generate, ship, run, and render server-spec test results."""
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Generate a goss YAML spec from a BootstrapSpec.
|
|
2
|
+
|
|
3
|
+
The generated spec is driven entirely by the live spec values — admin user name,
|
|
4
|
+
SSH port, firewall settings, WireGuard interface, etc. — so it always matches
|
|
5
|
+
exactly what loft-cli configured on the server.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
import yaml
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from loft_cli_core.specs.bootstrap_schema import BootstrapSpec
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def generate_goss_yaml(spec: BootstrapSpec) -> str:
|
|
19
|
+
"""Return a goss-compatible YAML string for the given BootstrapSpec.
|
|
20
|
+
|
|
21
|
+
The spec drives every check: no hardcoded user names, ports, or interfaces.
|
|
22
|
+
"""
|
|
23
|
+
doc: dict = {}
|
|
24
|
+
|
|
25
|
+
# ------------------------------------------------------------------ #
|
|
26
|
+
# user: admin account must exist in the sudo group
|
|
27
|
+
# ------------------------------------------------------------------ #
|
|
28
|
+
username = spec.admin_user.name
|
|
29
|
+
doc["user"] = {
|
|
30
|
+
username: {
|
|
31
|
+
"exists": True,
|
|
32
|
+
"groups": list(spec.admin_user.groups),
|
|
33
|
+
"home": f"/home/{username}",
|
|
34
|
+
"shell": "/bin/bash",
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
# ------------------------------------------------------------------ #
|
|
39
|
+
# file: sshd_config checks — cumulative based on what is configured
|
|
40
|
+
# ------------------------------------------------------------------ #
|
|
41
|
+
sshd_contains: list[str] = []
|
|
42
|
+
|
|
43
|
+
if spec.ssh.port != 22:
|
|
44
|
+
sshd_contains.append(f"/^Port\\s+{spec.ssh.port}/")
|
|
45
|
+
|
|
46
|
+
if spec.ssh.disable_root_login:
|
|
47
|
+
sshd_contains.append("/^PermitRootLogin\\s+no/")
|
|
48
|
+
|
|
49
|
+
if spec.ssh.disable_password_auth and spec.admin_user.pubkeys:
|
|
50
|
+
sshd_contains.append("/^PasswordAuthentication\\s+no/")
|
|
51
|
+
sshd_contains.append("/^PubkeyAuthentication\\s+yes/")
|
|
52
|
+
|
|
53
|
+
sshd_entry: dict = {"exists": True}
|
|
54
|
+
if sshd_contains:
|
|
55
|
+
sshd_entry["contains"] = sshd_contains
|
|
56
|
+
|
|
57
|
+
doc["file"] = {"/etc/ssh/sshd_config": sshd_entry}
|
|
58
|
+
|
|
59
|
+
# authorized_keys must exist and be non-empty when pubkeys were deployed
|
|
60
|
+
if spec.admin_user.pubkeys:
|
|
61
|
+
doc["file"][f"/home/{username}/.ssh/authorized_keys"] = {
|
|
62
|
+
"exists": True,
|
|
63
|
+
"mode": "0600",
|
|
64
|
+
"contains": ["/^(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp256)\\s+/"],
|
|
65
|
+
}
|
|
66
|
+
doc["file"][f"/home/{username}/.ssh"] = {
|
|
67
|
+
"exists": True,
|
|
68
|
+
"mode": "0700",
|
|
69
|
+
"filetype": "directory",
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
# WireGuard config file must exist, be restricted, and contain expected address
|
|
73
|
+
if spec.wireguard.enabled:
|
|
74
|
+
iface = spec.wireguard.interface
|
|
75
|
+
wg_addr = spec.wireguard.address.split("/")[0] # strip CIDR for content match
|
|
76
|
+
doc["file"][f"/etc/wireguard/{iface}.conf"] = {
|
|
77
|
+
"exists": True,
|
|
78
|
+
"mode": "0600",
|
|
79
|
+
"contains": [
|
|
80
|
+
"/\\[Interface\\]/",
|
|
81
|
+
f"/Address\\s*=\\s*{wg_addr}/",
|
|
82
|
+
],
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
# ------------------------------------------------------------------ #
|
|
86
|
+
# service: ssh must be running; ufw and wg-quick if active.
|
|
87
|
+
#
|
|
88
|
+
# We intentionally do NOT check ``enabled`` on the ssh service here.
|
|
89
|
+
# Ubuntu 24.04+ uses systemd socket activation (``ssh.socket``) where
|
|
90
|
+
# ``ssh.service`` is *not* enabled — ``ssh.socket`` is. Traditional
|
|
91
|
+
# distros (Ubuntu 22.04, Debian) have ``ssh.service`` enabled instead.
|
|
92
|
+
# Fedora/RHEL use ``sshd.service``. The ``enabled`` assertion is
|
|
93
|
+
# handled via a cross-distro command check below.
|
|
94
|
+
# ------------------------------------------------------------------ #
|
|
95
|
+
doc["service"] = {
|
|
96
|
+
"ssh": {"running": True},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if spec.firewall.provider == "ufw":
|
|
100
|
+
doc["service"]["ufw"] = {"enabled": True, "running": True}
|
|
101
|
+
|
|
102
|
+
if spec.wireguard.enabled:
|
|
103
|
+
iface = spec.wireguard.interface
|
|
104
|
+
doc["service"][f"wg-quick@{iface}"] = {"enabled": True}
|
|
105
|
+
|
|
106
|
+
# ------------------------------------------------------------------ #
|
|
107
|
+
# port: SSH port must be listening; port 22 must NOT be if port changed
|
|
108
|
+
# ------------------------------------------------------------------ #
|
|
109
|
+
doc["port"] = {
|
|
110
|
+
f"tcp:{spec.ssh.port}": {
|
|
111
|
+
"listening": True,
|
|
112
|
+
"ip": ["0.0.0.0"],
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if spec.ssh.port != 22:
|
|
117
|
+
doc["port"]["tcp:22"] = {"listening": False}
|
|
118
|
+
|
|
119
|
+
# Note: WireGuard UDP sockets are managed by the kernel module and do not
|
|
120
|
+
# appear as "listening" in ss/netstat output, so we skip the port check here.
|
|
121
|
+
# Interface state is verified via the "wg show" command check instead.
|
|
122
|
+
|
|
123
|
+
# ------------------------------------------------------------------ #
|
|
124
|
+
# package: wireguard-tools must be installed when WireGuard is enabled
|
|
125
|
+
# ------------------------------------------------------------------ #
|
|
126
|
+
if spec.wireguard.enabled:
|
|
127
|
+
doc["package"] = {
|
|
128
|
+
"wireguard-tools": {"installed": True},
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
# ------------------------------------------------------------------ #
|
|
132
|
+
# command: SSH enabled check, UFW status, and WireGuard interface checks
|
|
133
|
+
# ------------------------------------------------------------------ #
|
|
134
|
+
commands: dict = {}
|
|
135
|
+
|
|
136
|
+
# Cross-distro SSH enabled check: covers socket-activated sshd
|
|
137
|
+
# (Ubuntu 24.04+ ssh.socket), traditional Debian/Ubuntu (ssh.service),
|
|
138
|
+
# and Fedora/RHEL (sshd.service). The first ``is-enabled`` that
|
|
139
|
+
# succeeds makes the command exit 0.
|
|
140
|
+
commands[
|
|
141
|
+
"systemctl is-enabled ssh.socket 2>/dev/null || systemctl is-enabled ssh 2>/dev/null || systemctl is-enabled sshd"
|
|
142
|
+
] = {
|
|
143
|
+
"exit-status": 0,
|
|
144
|
+
"timeout": 5000,
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if spec.firewall.provider == "ufw":
|
|
148
|
+
ufw_stdout = [
|
|
149
|
+
"/Status:\\s+active/",
|
|
150
|
+
"/Default:\\s+deny\\s+\\(incoming\\)/",
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
if spec.wireguard.enabled:
|
|
154
|
+
iface = spec.wireguard.interface
|
|
155
|
+
wg_port = (
|
|
156
|
+
int(spec.wireguard.endpoint.split(":")[-1])
|
|
157
|
+
if ":" in spec.wireguard.endpoint
|
|
158
|
+
else 51820
|
|
159
|
+
)
|
|
160
|
+
# WireGuard UDP port is open to all (needed to establish the tunnel)
|
|
161
|
+
ufw_stdout.append(f"/{wg_port}\\/udp\\s+ALLOW\\s+IN/")
|
|
162
|
+
|
|
163
|
+
# NOTE: The SSH-on-WireGuard-interface restriction rule is applied by
|
|
164
|
+
# restrict_ssh_to_wireguard AFTER goss runs, so we cannot check for it
|
|
165
|
+
# here. At goss validation time the open SSH rule is still in effect.
|
|
166
|
+
ufw_stdout.append(f"/{spec.ssh.port}\\/tcp\\s+ALLOW\\s+IN/")
|
|
167
|
+
else:
|
|
168
|
+
# No WireGuard — SSH is open to all
|
|
169
|
+
ufw_stdout.append(f"/{spec.ssh.port}\\/tcp\\s+ALLOW\\s+IN/")
|
|
170
|
+
|
|
171
|
+
commands["ufw status verbose"] = {
|
|
172
|
+
"exit-status": 0,
|
|
173
|
+
"stdout": ufw_stdout,
|
|
174
|
+
"timeout": 10000,
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if spec.wireguard.enabled:
|
|
178
|
+
iface = spec.wireguard.interface
|
|
179
|
+
wg_addr = spec.wireguard.address.split("/")[0]
|
|
180
|
+
commands[f"ip address show {iface}"] = {
|
|
181
|
+
"exit-status": 0,
|
|
182
|
+
"stdout": [f"/{iface}/", f"/{wg_addr}/"],
|
|
183
|
+
"timeout": 5000,
|
|
184
|
+
}
|
|
185
|
+
commands[f"wg show {iface}"] = {
|
|
186
|
+
"exit-status": 0,
|
|
187
|
+
"stdout": [f"/interface: {iface}/"],
|
|
188
|
+
"timeout": 5000,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
doc["command"] = commands
|
|
192
|
+
|
|
193
|
+
# ------------------------------------------------------------------ #
|
|
194
|
+
# Serialise — use a header comment so the file is self-documenting
|
|
195
|
+
# ------------------------------------------------------------------ #
|
|
196
|
+
header = (
|
|
197
|
+
f"# loft-cli goss spec — auto-generated for '{spec.meta.name}'\n"
|
|
198
|
+
f"# Target: {spec.host.address} Admin: {spec.admin_user.name} SSH port: {spec.ssh.port}\n"
|
|
199
|
+
f"# Run on the server: goss -g ~/.goss/goss.yaml validate\n"
|
|
200
|
+
f"#\n"
|
|
201
|
+
)
|
|
202
|
+
return header + yaml.dump(doc, default_flow_style=False, sort_keys=True, allow_unicode=True)
|