Add castle secret CLI and fail-loud unresolved-secret apply gate
Writing a secret by hand meant knowing the active backend and its layout;
getting it wrong (a file write on an OpenBao fleet) left the value where the
resolver never reads it, so ${secret:NAME} silently degraded to the literal
<MISSING_SECRET:NAME> placeholder that a service then used as its credential.
- `castle secret {list|set|get|rm}` reads/writes the ACTIVE backend, so there's
no wrong store to pick. `set NAME` with no value reads a hidden prompt / stdin.
- `castle apply` (and --plan) now refuses to converge any deployment whose
${secret:...} refs don't resolve in the active backend: exits non-zero, writes
nothing, and names each deployment + secret + the `castle secret set` fix.
- New helpers in core/config.py: active_secret_backend(), active_backend_name(),
secret_refs(). Gate impl: deploy._unresolved_secrets() + ApplyResult.blocked.
- Tests: test_deploy_secret_gate.py, TestSecretRefs, test_secret.py. Docs: AGENTS.md.
This commit is contained in:
65
core/tests/test_deploy_secret_gate.py
Normal file
65
core/tests/test_deploy_secret_gate.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Tests for the apply-time unresolved-secret gate.
|
||||
|
||||
`castle apply` must refuse to converge a deployment whose ``${secret:NAME}`` env
|
||||
references the active backend can't resolve — otherwise the value silently
|
||||
degrades to a ``<MISSING_SECRET:NAME>`` placeholder and the service starts with a
|
||||
bogus credential (the immich-DB-password-to-the-wrong-backend failure).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from castle_core.deploy import _unresolved_secrets
|
||||
|
||||
|
||||
def _spec(env: dict[str, str], enabled: bool = True) -> SimpleNamespace:
|
||||
return SimpleNamespace(enabled=enabled, defaults=SimpleNamespace(env=env))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def file_backend(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
||||
"""Force the active backend to a temp file store with a known secret."""
|
||||
secrets = tmp_path / "secrets"
|
||||
secrets.mkdir()
|
||||
(secrets / "PRESENT").write_text("value\n")
|
||||
monkeypatch.setenv("CASTLE_SECRET_BACKEND", "file")
|
||||
monkeypatch.setattr("castle_core.config.SECRETS_DIR", secrets)
|
||||
return secrets
|
||||
|
||||
|
||||
def test_all_resolved_returns_empty(file_backend: Path) -> None:
|
||||
items = [("service", "svc", _spec({"TOKEN": "${secret:PRESENT}"}))]
|
||||
assert _unresolved_secrets(items) == []
|
||||
|
||||
|
||||
def test_missing_secret_is_reported(file_backend: Path) -> None:
|
||||
items = [("service", "svc", _spec({"TOKEN": "${secret:ABSENT}"}))]
|
||||
assert _unresolved_secrets(items) == [("svc", ["ABSENT"])]
|
||||
|
||||
|
||||
def test_disabled_deployment_skipped(file_backend: Path) -> None:
|
||||
items = [("service", "svc", _spec({"TOKEN": "${secret:ABSENT}"}, enabled=False))]
|
||||
assert _unresolved_secrets(items) == []
|
||||
|
||||
|
||||
def test_non_secret_placeholders_ignored(file_backend: Path) -> None:
|
||||
items = [("service", "svc", _spec({"PORT": "${port}", "DIR": "${data_dir}"}))]
|
||||
assert _unresolved_secrets(items) == []
|
||||
|
||||
|
||||
def test_composite_value_partial_missing(file_backend: Path) -> None:
|
||||
# A URL mixing a present and an absent secret still flags the absent one.
|
||||
env = {"URL": "postgres://u:${secret:PRESENT}@h/db?k=${secret:ABSENT}"}
|
||||
assert _unresolved_secrets([("service", "svc", _spec(env))]) == [("svc", ["ABSENT"])]
|
||||
|
||||
|
||||
def test_multiple_deployments_only_broken_flagged(file_backend: Path) -> None:
|
||||
items = [
|
||||
("service", "ok", _spec({"T": "${secret:PRESENT}"})),
|
||||
("service", "bad", _spec({"T": "${secret:ABSENT}"})),
|
||||
]
|
||||
assert _unresolved_secrets(items) == [("bad", ["ABSENT"])]
|
||||
Reference in New Issue
Block a user