feat(gitops): default deployments.yaml to ~/Workloads/deployments.yaml
- scripts/appctl_auth.py: default DEFAULT_DEPLOYMENTS_FILE to WORKLOADS_DIR/SITES_DIR or ~/Workloads/deployments.yaml and honor WORKLOADS_DIR in load_admitted_workloads() - scripts/appctl_daemon.py: default DEPLOYMENTS_FILE to WORKLOADS_DIR/deployments.yaml (~/Workloads/deployments.yaml) instead of CORE_DIR/config/deployments.yaml - scripts/gitops_dispatcher.py: default DEPLOYMENTS_FILE to WORKLOADS_DIR/deployments.yaml and remove CONFIG_DIR/deployments.yaml fallback in get_trusted_repository_mapping() so missing registry fails closed - tests: add regression tests for default ~/Workloads/deployments.yaml resolution and fail-closed behavior; isolate test_operational_router_workload_update_propagates_non_zero_exit_and_stderr from host registry
This commit is contained in:
1 parent
f0db802f72
commit
33fda26dc1
7 files changed
+174
-10
No files matched your search
+12
-3
@@ -34,7 +34,12 @@ except ImportError:
|
|||||||
|
|
||||||
DEFAULT_TOKENS_FILE = Path("/run/secrets/appctl/tokens.json")
|
DEFAULT_TOKENS_FILE = Path("/run/secrets/appctl/tokens.json")
|
||||||
DEFAULT_DEPLOYMENTS_FILE = (
|
DEFAULT_DEPLOYMENTS_FILE = (
|
||||||
Path(__file__).resolve().parent.parent / "config" / "deployments.yaml"
|
Path(
|
||||||
|
os.environ.get("WORKLOADS_DIR")
|
||||||
|
or os.environ.get("SITES_DIR")
|
||||||
|
or os.path.expanduser("~/Workloads")
|
||||||
|
)
|
||||||
|
/ "deployments.yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
CORE_TARGETS: frozenset[str] = frozenset(
|
CORE_TARGETS: frozenset[str] = frozenset(
|
||||||
@@ -227,8 +232,12 @@ def load_admitted_workloads(deployments_path: Path | str | None = None) -> set[s
|
|||||||
"""
|
"""
|
||||||
if deployments_path is not None:
|
if deployments_path is not None:
|
||||||
reg_file = Path(deployments_path)
|
reg_file = Path(deployments_path)
|
||||||
elif "HOMELAB_DEPLOYMENTS_FILE" in os.environ:
|
elif env_deploy := os.environ.get("HOMELAB_DEPLOYMENTS_FILE"):
|
||||||
reg_file = Path(os.environ["HOMELAB_DEPLOYMENTS_FILE"])
|
reg_file = Path(env_deploy)
|
||||||
|
elif env_workloads := (
|
||||||
|
os.environ.get("WORKLOADS_DIR") or os.environ.get("SITES_DIR")
|
||||||
|
):
|
||||||
|
reg_file = Path(env_workloads) / "deployments.yaml"
|
||||||
else:
|
else:
|
||||||
reg_file = DEFAULT_DEPLOYMENTS_FILE
|
reg_file = DEFAULT_DEPLOYMENTS_FILE
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ WORKLOADS_DIR = Path(
|
|||||||
)
|
)
|
||||||
SITES_DIR = WORKLOADS_DIR
|
SITES_DIR = WORKLOADS_DIR
|
||||||
DEPLOYMENTS_FILE = Path(
|
DEPLOYMENTS_FILE = Path(
|
||||||
os.environ.get("HOMELAB_DEPLOYMENTS_FILE", CORE_DIR / "config" / "deployments.yaml")
|
os.environ.get("HOMELAB_DEPLOYMENTS_FILE") or (WORKLOADS_DIR / "deployments.yaml")
|
||||||
)
|
)
|
||||||
|
|
||||||
DEFAULT_SOCKET_PATH = Path(
|
DEFAULT_SOCKET_PATH = Path(
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ CONFIG_DIR = Path(
|
|||||||
os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab"))
|
os.environ.get("HOMELAB_CONFIG_DIR", os.path.expanduser("~/.config/homelab"))
|
||||||
)
|
)
|
||||||
DEPLOYMENTS_FILE = Path(
|
DEPLOYMENTS_FILE = Path(
|
||||||
os.environ.get("HOMELAB_DEPLOYMENTS_FILE", CORE_DIR / "config" / "deployments.yaml")
|
os.environ.get("HOMELAB_DEPLOYMENTS_FILE") or (WORKLOADS_DIR / "deployments.yaml")
|
||||||
)
|
)
|
||||||
DEFAULT_SECRET_PATH = Path(
|
DEFAULT_SECRET_PATH = Path(
|
||||||
os.environ.get("GITOPS_SECRET_FILE", "/run/secrets/gitops/webhook_secret")
|
os.environ.get("GITOPS_SECRET_FILE", "/run/secrets/gitops/webhook_secret")
|
||||||
@@ -265,11 +265,7 @@ def get_trusted_repository_mapping() -> dict[str, Path]:
|
|||||||
"""
|
"""
|
||||||
mapping: dict[str, Path] = {}
|
mapping: dict[str, Path] = {}
|
||||||
|
|
||||||
registry_file = (
|
registry_file = DEPLOYMENTS_FILE
|
||||||
DEPLOYMENTS_FILE
|
|
||||||
if DEPLOYMENTS_FILE.is_file()
|
|
||||||
else (CONFIG_DIR / "deployments.yaml")
|
|
||||||
)
|
|
||||||
if not registry_file.is_file():
|
if not registry_file.is_file():
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Authoritative registry {registry_file} not found. Failing closed with zero admitted repositories."
|
f"Authoritative registry {registry_file} not found. Failing closed with zero admitted repositories."
|
||||||
|
|||||||
@@ -269,6 +269,56 @@ def test_registry_resolves_workloads_and_sites_tilde_prefixes(
|
|||||||
assert gitops_dispatcher.resolve_repository("valid") is None
|
assert gitops_dispatcher.resolve_repository("valid") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_deployments_file_defaults_to_workloads_and_does_not_fallback(
|
||||||
|
tmp_path: Path, monkeypatch
|
||||||
|
):
|
||||||
|
"""Ensure DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed without CONFIG_DIR/CORE_DIR fallback."""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||||
|
base_env = {
|
||||||
|
k: v
|
||||||
|
for k, v in os.environ.items()
|
||||||
|
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||||
|
}
|
||||||
|
base_env["HOME"] = str(tmp_path)
|
||||||
|
base_env["PYTHONPATH"] = scripts_dir
|
||||||
|
|
||||||
|
resolved_default = subprocess.check_output(
|
||||||
|
[sys.executable, "-c", "import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"],
|
||||||
|
env=base_env,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
expected_default = tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
assert Path(resolved_default) == expected_default
|
||||||
|
|
||||||
|
# Even if CONFIG_DIR/deployments.yaml exists, missing DEPLOYMENTS_FILE must fail closed
|
||||||
|
config_dir = tmp_path / "config-fallback"
|
||||||
|
config_dir.mkdir()
|
||||||
|
(config_dir / "deployments.yaml").write_text(
|
||||||
|
'schemaVersion: "1.0"\n'
|
||||||
|
"repositories:\n"
|
||||||
|
" docs:\n"
|
||||||
|
' path: "~/Workloads/docs"\n',
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(gitops_dispatcher, "DEPLOYMENTS_FILE", expected_default)
|
||||||
|
monkeypatch.setattr(gitops_dispatcher, "CONFIG_DIR", config_dir)
|
||||||
|
assert gitops_dispatcher.get_trusted_repository_mapping() == {}
|
||||||
|
|
||||||
|
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
||||||
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
|
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
||||||
|
resolved_override = subprocess.check_output(
|
||||||
|
[sys.executable, "-c", "import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"],
|
||||||
|
env=env_with_workloads,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||||
|
|
||||||
|
|
||||||
def test_webhook_verification_valid_signature():
|
def test_webhook_verification_valid_signature():
|
||||||
"""Ensure valid HMAC-SHA256 signature passes verification."""
|
"""Ensure valid HMAC-SHA256 signature passes verification."""
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|||||||
@@ -492,3 +492,48 @@ def test_rbac_viewer_denied_on_sensitive_reads():
|
|||||||
enforce_rbac(viewer, sens_op, "snappymail")
|
enforce_rbac(viewer, sens_op, "snappymail")
|
||||||
assert "not permitted to access sensitive operation" in str(exc_info.value)
|
assert "not permitted to access sensitive operation" in str(exc_info.value)
|
||||||
assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED
|
assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_deployments_file_resolves_to_workloads_dir(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
):
|
||||||
|
"""DEFAULT_DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed when absent."""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import appctl_auth
|
||||||
|
|
||||||
|
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||||
|
env = {
|
||||||
|
k: v
|
||||||
|
for k, v in os.environ.items()
|
||||||
|
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||||
|
}
|
||||||
|
env["HOME"] = str(tmp_path)
|
||||||
|
env["PYTHONPATH"] = scripts_dir
|
||||||
|
|
||||||
|
resolved = subprocess.check_output(
|
||||||
|
[sys.executable, "-c", "import appctl_auth; print(appctl_auth.DEFAULT_DEPLOYMENTS_FILE)"],
|
||||||
|
env=env,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
expected = tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
assert Path(resolved) == expected
|
||||||
|
|
||||||
|
# Absent ~/Workloads/deployments.yaml must fail closed (no CORE_DIR fallback)
|
||||||
|
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||||
|
monkeypatch.delenv("WORKLOADS_DIR", raising=False)
|
||||||
|
monkeypatch.delenv("SITES_DIR", raising=False)
|
||||||
|
monkeypatch.setattr(appctl_auth, "DEFAULT_DEPLOYMENTS_FILE", expected)
|
||||||
|
assert appctl_auth.load_admitted_workloads() == set()
|
||||||
|
|
||||||
|
# WORKLOADS_DIR takes precedence when HOMELAB_DEPLOYMENTS_FILE is unset
|
||||||
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
|
custom_workloads.mkdir()
|
||||||
|
(custom_workloads / "deployments.yaml").write_text(
|
||||||
|
"repositories:\n custom_app:\n path: ~/Workloads/custom_app\n",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
monkeypatch.setenv("WORKLOADS_DIR", str(custom_workloads))
|
||||||
|
assert appctl_auth.load_admitted_workloads() == {"custom_app"}
|
||||||
@@ -591,3 +591,66 @@ def test_gitops_dispatcher_delegates_to_daemon(daemon_env, monkeypatch):
|
|||||||
)
|
)
|
||||||
assert success is True
|
assert success is True
|
||||||
assert mock_sub.call_count >= 1
|
assert mock_sub.call_count >= 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_daemon_deployments_file_defaults_to_workloads_and_fails_closed(
|
||||||
|
daemon_env, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml (or WORKLOADS_DIR/deployments.yaml)
|
||||||
|
and homelab-operator fails closed (403) when ~/Workloads/deployments.yaml is absent.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import appctl_auth
|
||||||
|
|
||||||
|
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||||
|
base_env = {
|
||||||
|
k: v
|
||||||
|
for k, v in os.environ.items()
|
||||||
|
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||||
|
}
|
||||||
|
base_env["HOME"] = str(tmp_path)
|
||||||
|
base_env["PYTHONPATH"] = scripts_dir
|
||||||
|
|
||||||
|
resolved_default = subprocess.check_output(
|
||||||
|
[sys.executable, "-c", "import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"],
|
||||||
|
env=base_env,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
assert Path(resolved_default) == tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
|
||||||
|
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
||||||
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
|
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
||||||
|
resolved_override = subprocess.check_output(
|
||||||
|
[sys.executable, "-c", "import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"],
|
||||||
|
env=env_with_workloads,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||||
|
|
||||||
|
# When HOMELAB_DEPLOYMENTS_FILE is unset and ~/Workloads/deployments.yaml is absent,
|
||||||
|
# homelab-operator must be denied (403) without falling back to CORE_DIR/config/deployments.yaml.
|
||||||
|
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||||
|
monkeypatch.delenv("WORKLOADS_DIR", raising=False)
|
||||||
|
monkeypatch.delenv("SITES_DIR", raising=False)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
appctl_auth,
|
||||||
|
"DEFAULT_DEPLOYMENTS_FILE",
|
||||||
|
tmp_path / "empty-home" / "Workloads" / "deployments.yaml",
|
||||||
|
)
|
||||||
|
deploy_req = OperationRequest(
|
||||||
|
operation=OperationType.WORKLOAD_DEPLOY, target="docs"
|
||||||
|
)
|
||||||
|
resp = send_daemon_request(
|
||||||
|
endpoint=daemon_env.socket_path,
|
||||||
|
operation_request=deploy_req,
|
||||||
|
token=OPERATOR_SECRET,
|
||||||
|
)
|
||||||
|
assert resp.success is False
|
||||||
|
assert resp.error is not None
|
||||||
|
assert resp.error.code == ErrorCode.ERR_PERMISSION_DENIED
|
||||||
|
assert resp.error.code.http_status == 403
|
||||||
@@ -1416,6 +1416,7 @@ def test_operational_router_workload_update_propagates_non_zero_exit_and_stderr(
|
|||||||
return (0, "", "")
|
return (0, "", "")
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
patch("appctl_router.load_admitted_workloads", return_value={"snappymail"}),
|
||||||
patch("appctl_router.resolve_app", return_value=mock_app),
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
||||||
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
||||||
patch("appctl_router.cmd_sync_homepage"),
|
patch("appctl_router.cmd_sync_homepage"),
|
||||||
|
|||||||
Reference in new issue
Block a user