Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d4cfe1c04 | ||
|
|
9306dae56b | ||
|
|
c337914fb3 |
No files matched your search
@@ -6,7 +6,7 @@ Implements the zero-trust authorization pipeline for homelab-appctl.service:
|
|||||||
- Unix domain socket SO_PEERCRED fallback to homelab-viewer for zero-friction local reads
|
- Unix domain socket SO_PEERCRED fallback to homelab-viewer for zero-friction local reads
|
||||||
- Mandatory Bearer tokens for all network (HTTP) operations and all mutations / sensitive reads
|
- Mandatory Bearer tokens for all network (HTTP) operations and all mutations / sensitive reads
|
||||||
- 3-role RBAC enforcement (homelab-admin, homelab-operator, homelab-viewer)
|
- 3-role RBAC enforcement (homelab-admin, homelab-operator, homelab-viewer)
|
||||||
- Workload scope gating against config/deployments.yaml for homelab-operator
|
- Workload scope gating against ~/Workloads/deployments.yaml (or HOMELAB_DEPLOYMENTS_FILE / WORKLOADS_DIR override) for homelab-operator
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -227,7 +227,8 @@ class TokenStore:
|
|||||||
|
|
||||||
def load_admitted_workloads(deployments_path: Path | str | None = None) -> set[str]:
|
def load_admitted_workloads(deployments_path: Path | str | None = None) -> set[str]:
|
||||||
"""
|
"""
|
||||||
Load admitted repository names and aliases from config/deployments.yaml.
|
Load admitted repository names and aliases from ~/Workloads/deployments.yaml
|
||||||
|
(or HOMELAB_DEPLOYMENTS_FILE / WORKLOADS_DIR override).
|
||||||
Fails closed (empty set) if registry is missing or malformed.
|
Fails closed (empty set) if registry is missing or malformed.
|
||||||
"""
|
"""
|
||||||
if deployments_path is not None:
|
if deployments_path is not None:
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ def get_trusted_repository_mapping() -> dict[str, Path]:
|
|||||||
"""
|
"""
|
||||||
Build authoritative mapping of logical repository identities to local paths.
|
Build authoritative mapping of logical repository identities to local paths.
|
||||||
Webhook payloads can only select repositories defined in this mapping.
|
Webhook payloads can only select repositories defined in this mapping.
|
||||||
Core Authoritative Registry (deployments.yaml) is the sole admission authority.
|
Workloads admission registry (~/Workloads/deployments.yaml, templated by config/deployments.yaml) is the sole admission authority.
|
||||||
Fails closed if the registry is missing, unreadable, or malformed.
|
Fails closed if the registry is missing, unreadable, or malformed.
|
||||||
"""
|
"""
|
||||||
mapping: dict[str, Path] = {}
|
mapping: dict[str, Path] = {}
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""Shared pytest fixtures for Canopy NixOS Core test suites."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
_STRIPPED_WORKLOAD_ENV_VARS = frozenset(
|
||||||
|
{"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||||
|
)
|
||||||
|
_SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts"
|
||||||
|
|
||||||
|
|
||||||
|
class IsolatedScriptRunner(Protocol):
|
||||||
|
"""Callable protocol for executing a Python expression in an isolated env."""
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self, expr: str, extra_env: dict[str, str] | None = None
|
||||||
|
) -> str: ...
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def run_isolated_script_expr(tmp_path: Path) -> IsolatedScriptRunner:
|
||||||
|
"""
|
||||||
|
Execute a Python expression in a clean subprocess with workload env vars stripped.
|
||||||
|
|
||||||
|
Strips ``HOMELAB_DEPLOYMENTS_FILE``, ``WORKLOADS_DIR``, and ``SITES_DIR``,
|
||||||
|
sets ``HOME`` to ``tmp_path`` and ``PYTHONPATH`` to ``scripts/``, and
|
||||||
|
merges any optional ``extra_env`` overrides.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _run(expr: str, extra_env: dict[str, str] | None = None) -> str:
|
||||||
|
env = {
|
||||||
|
k: v
|
||||||
|
for k, v in os.environ.items()
|
||||||
|
if k not in _STRIPPED_WORKLOAD_ENV_VARS
|
||||||
|
}
|
||||||
|
env["HOME"] = str(tmp_path)
|
||||||
|
env["PYTHONPATH"] = str(_SCRIPTS_DIR)
|
||||||
|
if extra_env:
|
||||||
|
env.update(extra_env)
|
||||||
|
return subprocess.check_output(
|
||||||
|
[sys.executable, "-c", expr],
|
||||||
|
env=env,
|
||||||
|
text=True,
|
||||||
|
).strip()
|
||||||
|
|
||||||
|
return _run
|
||||||
@@ -269,32 +269,20 @@ 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(
|
def test_deployments_file_defaults_to_workloads(
|
||||||
|
tmp_path: Path, run_isolated_script_expr
|
||||||
|
):
|
||||||
|
"""Ensure DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml."""
|
||||||
|
resolved_default = run_isolated_script_expr(
|
||||||
|
"import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"
|
||||||
|
)
|
||||||
|
assert Path(resolved_default) == tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
|
||||||
|
|
||||||
|
def test_deployments_file_fails_closed_without_config_dir_fallback(
|
||||||
tmp_path: Path, monkeypatch
|
tmp_path: Path, monkeypatch
|
||||||
):
|
):
|
||||||
"""Ensure DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed without CONFIG_DIR/CORE_DIR fallback."""
|
"""Ensure missing DEPLOYMENTS_FILE fails closed even when CONFIG_DIR/deployments.yaml exists."""
|
||||||
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 = tmp_path / "config-fallback"
|
||||||
config_dir.mkdir()
|
config_dir.mkdir()
|
||||||
(config_dir / "deployments.yaml").write_text(
|
(config_dir / "deployments.yaml").write_text(
|
||||||
@@ -304,18 +292,22 @@ def test_deployments_file_defaults_to_workloads_and_does_not_fallback(
|
|||||||
' path: "~/Workloads/docs"\n',
|
' path: "~/Workloads/docs"\n',
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
)
|
)
|
||||||
monkeypatch.setattr(gitops_dispatcher, "DEPLOYMENTS_FILE", expected_default)
|
monkeypatch.setattr(
|
||||||
|
gitops_dispatcher, "DEPLOYMENTS_FILE", tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
)
|
||||||
monkeypatch.setattr(gitops_dispatcher, "CONFIG_DIR", config_dir)
|
monkeypatch.setattr(gitops_dispatcher, "CONFIG_DIR", config_dir)
|
||||||
assert gitops_dispatcher.get_trusted_repository_mapping() == {}
|
assert gitops_dispatcher.get_trusted_repository_mapping() == {}
|
||||||
|
|
||||||
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
|
||||||
|
def test_deployments_file_honors_workloads_dir_override(
|
||||||
|
tmp_path: Path, run_isolated_script_expr
|
||||||
|
):
|
||||||
|
"""Ensure DEPLOYMENTS_FILE honors WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset."""
|
||||||
custom_workloads = tmp_path / "CustomWorkloads"
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
resolved_override = run_isolated_script_expr(
|
||||||
resolved_override = subprocess.check_output(
|
"import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)",
|
||||||
[sys.executable, "-c", "import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"],
|
extra_env={"WORKLOADS_DIR": str(custom_workloads)},
|
||||||
env=env_with_workloads,
|
)
|
||||||
text=True,
|
|
||||||
).strip()
|
|
||||||
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -495,45 +495,42 @@ def test_rbac_viewer_denied_on_sensitive_reads():
|
|||||||
|
|
||||||
|
|
||||||
def test_default_deployments_file_resolves_to_workloads_dir(
|
def test_default_deployments_file_resolves_to_workloads_dir(
|
||||||
|
tmp_path: Path, run_isolated_script_expr
|
||||||
|
):
|
||||||
|
"""DEFAULT_DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml."""
|
||||||
|
resolved = run_isolated_script_expr(
|
||||||
|
"import appctl_auth; print(appctl_auth.DEFAULT_DEPLOYMENTS_FILE)"
|
||||||
|
)
|
||||||
|
assert Path(resolved) == tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_admitted_workloads_fails_closed_when_absent(
|
||||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
):
|
):
|
||||||
"""DEFAULT_DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed when absent."""
|
"""Absent ~/Workloads/deployments.yaml fails closed (empty set, no CORE_DIR fallback)."""
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import appctl_auth
|
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("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||||
monkeypatch.delenv("WORKLOADS_DIR", raising=False)
|
monkeypatch.delenv("WORKLOADS_DIR", raising=False)
|
||||||
monkeypatch.delenv("SITES_DIR", raising=False)
|
monkeypatch.delenv("SITES_DIR", raising=False)
|
||||||
monkeypatch.setattr(appctl_auth, "DEFAULT_DEPLOYMENTS_FILE", expected)
|
monkeypatch.setattr(
|
||||||
|
appctl_auth, "DEFAULT_DEPLOYMENTS_FILE", tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
)
|
||||||
assert appctl_auth.load_admitted_workloads() == set()
|
assert appctl_auth.load_admitted_workloads() == set()
|
||||||
|
|
||||||
# WORKLOADS_DIR takes precedence when HOMELAB_DEPLOYMENTS_FILE is unset
|
|
||||||
|
def test_load_admitted_workloads_honors_workloads_dir_override(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
):
|
||||||
|
"""WORKLOADS_DIR takes precedence when HOMELAB_DEPLOYMENTS_FILE is unset."""
|
||||||
|
import appctl_auth
|
||||||
|
|
||||||
custom_workloads = tmp_path / "CustomWorkloads"
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
custom_workloads.mkdir()
|
custom_workloads.mkdir()
|
||||||
(custom_workloads / "deployments.yaml").write_text(
|
(custom_workloads / "deployments.yaml").write_text(
|
||||||
"repositories:\n custom_app:\n path: ~/Workloads/custom_app\n",
|
"repositories:\n custom_app:\n path: ~/Workloads/custom_app\n",
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
)
|
)
|
||||||
|
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||||
monkeypatch.setenv("WORKLOADS_DIR", str(custom_workloads))
|
monkeypatch.setenv("WORKLOADS_DIR", str(custom_workloads))
|
||||||
assert appctl_auth.load_admitted_workloads() == {"custom_app"}
|
assert appctl_auth.load_admitted_workloads() == {"custom_app"}
|
||||||
@@ -593,45 +593,34 @@ def test_gitops_dispatcher_delegates_to_daemon(daemon_env, monkeypatch):
|
|||||||
assert mock_sub.call_count >= 1
|
assert mock_sub.call_count >= 1
|
||||||
|
|
||||||
|
|
||||||
def test_daemon_deployments_file_defaults_to_workloads_and_fails_closed(
|
def test_daemon_deployments_file_defaults_to_workloads(
|
||||||
daemon_env, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
tmp_path: Path, run_isolated_script_expr
|
||||||
):
|
):
|
||||||
"""
|
"""DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml."""
|
||||||
DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml (or WORKLOADS_DIR/deployments.yaml)
|
resolved_default = run_isolated_script_expr(
|
||||||
and homelab-operator fails closed (403) when ~/Workloads/deployments.yaml is absent.
|
"import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"
|
||||||
"""
|
)
|
||||||
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"
|
assert Path(resolved_default) == tmp_path / "Workloads" / "deployments.yaml"
|
||||||
|
|
||||||
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
|
||||||
|
def test_daemon_deployments_file_honors_workloads_dir_override(
|
||||||
|
tmp_path: Path, run_isolated_script_expr
|
||||||
|
):
|
||||||
|
"""DEPLOYMENTS_FILE honors WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset."""
|
||||||
custom_workloads = tmp_path / "CustomWorkloads"
|
custom_workloads = tmp_path / "CustomWorkloads"
|
||||||
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
resolved_override = run_isolated_script_expr(
|
||||||
resolved_override = subprocess.check_output(
|
"import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)",
|
||||||
[sys.executable, "-c", "import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"],
|
extra_env={"WORKLOADS_DIR": str(custom_workloads)},
|
||||||
env=env_with_workloads,
|
)
|
||||||
text=True,
|
|
||||||
).strip()
|
|
||||||
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||||
|
|
||||||
|
|
||||||
|
def test_daemon_operator_fails_closed_when_deployments_file_absent(
|
||||||
|
daemon_env, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
):
|
||||||
|
"""homelab-operator fails closed (403 ERR_PERMISSION_DENIED) when ~/Workloads/deployments.yaml is absent."""
|
||||||
|
import appctl_auth
|
||||||
|
|
||||||
# When HOMELAB_DEPLOYMENTS_FILE is unset and ~/Workloads/deployments.yaml is absent,
|
# 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.
|
# homelab-operator must be denied (403) without falling back to CORE_DIR/config/deployments.yaml.
|
||||||
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||||
|
|||||||
Reference in new issue
Block a user