secrets-engine/tests/test_catalog.py
tegwick 784be978bf
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 2s
feat: admit existing OpenBao catalog lanes
2026-08-21 08:20:33 +02:00

188 lines
6.5 KiB
Python

import copy
import uuid
import pytest
from secrets_engine.catalog import validate_entry, load_catalog
from secrets_engine.config import repo_root
from secrets_engine.errors import CatalogError
VALID = {
"id": "test-lane",
"org": "coulomb",
"repo": "team-repo",
"stage": "test",
"mount": "secret",
"path": "test/team/thing",
"fields": ["api_token"],
"consumers": [{"name": "c", "auth": "approle", "claim": "role:c"}],
"delivery_modes": ["exec-env"],
"approval": {"model": "bootstrap-only"},
"verification": {"positive": "x", "negative": "y"},
"rotation": {"ttl": "1h"},
"deactivation": {"expectation": "delete"},
"audit": {"evidence": "non-secret"},
}
def test_valid_entry_parses():
e = validate_entry(VALID)
assert e.id == "test-lane"
assert e.policy_name == "se-test-test-lane"
assert not e.approval_required()
# owner is the explicit org/repo slug, not a bare name
assert e.owner == "coulomb/team-repo"
@pytest.mark.parametrize("field", ["org", "repo"])
def test_missing_org_or_repo_rejected(field):
data = copy.deepcopy(VALID)
data.pop(field)
with pytest.raises(CatalogError):
validate_entry(data)
def test_npm_config_requires_registry_and_scope():
data = copy.deepcopy(VALID)
data["delivery_modes"] = ["npm-config"]
with pytest.raises(CatalogError):
validate_entry(data) # no delivery_config.npm
data["delivery_config"] = {"npm": {"registry": "https://x/", "scope": "@s"}}
e = validate_entry(data)
assert e.npm["scope"] == "@s"
@pytest.mark.parametrize("field", ["stage", "mount", "path", "fields", "approval", "delivery_modes"])
def test_missing_required_field_rejected(field):
data = copy.deepcopy(VALID)
data.pop(field)
with pytest.raises(CatalogError):
validate_entry(data)
def test_bad_stage_rejected():
data = copy.deepcopy(VALID)
data["stage"] = "staging"
with pytest.raises(CatalogError):
validate_entry(data)
def test_unknown_delivery_mode_rejected():
data = copy.deepcopy(VALID)
data["delivery_modes"] = ["telepathy"]
with pytest.raises(CatalogError):
validate_entry(data)
def test_existing_mount_and_workload_delivery_parse():
data = copy.deepcopy(VALID)
data["mount"] = "platform"
data["mount_management"] = "existing"
data["workload_delivery"] = [
{"mode": "external-secrets", "owner": "rapp-example"}
]
entry = validate_entry(data)
assert entry.manages_mount is False
assert entry.manages_delivery_auth is True
assert entry.workload_delivery[0]["mode"] == "external-secrets"
def test_existing_delivery_auth_requires_explicit_role():
data = copy.deepcopy(VALID)
data["delivery_auth"] = {"method": "approle", "management": "existing"}
with pytest.raises(CatalogError):
validate_entry(data)
data["delivery_auth"]["role_name"] = "existing-exact-role"
entry = validate_entry(data)
assert entry.manages_delivery_auth is False
assert entry.role_name == "existing-exact-role"
def test_native_delivery_rejects_missing_auth():
data = copy.deepcopy(VALID)
data["delivery_auth"] = {"method": "none", "management": "none"}
with pytest.raises(CatalogError):
validate_entry(data)
def test_workload_delivery_requires_mode_and_owner():
data = copy.deepcopy(VALID)
data["workload_delivery"] = [{"mode": "external-secrets"}]
with pytest.raises(CatalogError):
validate_entry(data)
def test_high_risk_lane_requires_owners_and_non_bootstrap_approval():
data = copy.deepcopy(VALID)
data["risk"] = {"classification": "high"}
data["approval"] = {"model": "decision", "decision_ref": "d1"}
with pytest.raises(CatalogError):
validate_entry(data)
data["rotation"]["owner"] = "platform-owner"
data["deactivation"]["owner"] = "platform-owner"
assert validate_entry(data).risk["classification"] == "high"
data["approval"] = {"model": "bootstrap-only"}
with pytest.raises(CatalogError):
validate_entry(data)
def test_admitted_existing_lanes_have_exact_safe_metadata():
entries = load_catalog(repo_root() / "catalog")
expected = {
"issue-core-ingestion-api-key": "workloads/issue-core/issue-core/issue-core-runtime",
"reuse-surface-hub-write-token": "workloads/reuse/reuse-surface/runtime-secrets",
"openrouter-llm-connect": "workloads/activity-core/llm-connect/llm-connect-provider-secrets",
"forgejo-admin-api-token": "workloads/forgejo/forgejo-admin",
"email-connect-transactional": "workloads/email-connect/transactional",
}
for lane_id, path in expected.items():
entry = entries[lane_id]
assert entry.mount == "platform"
assert entry.path == path
assert entry.manages_mount is False
assert entry.manages_delivery_auth is True
assert entry.risk["classification"] == "high"
assert entry.rotation["owner"]
assert entry.deactivation["owner"]
assert entry.workload_delivery
assert entry.approval["decision_ref"].startswith("CCR-2026-")
def test_generic_and_identity_routes_are_not_catalog_lanes():
entries = load_catalog(repo_root() / "catalog")
assert "openbao-api-key" not in entries
assert "key-cape-oidc-login" not in entries
def test_wildcard_path_rejected():
data = copy.deepcopy(VALID)
data["path"] = "test/*"
with pytest.raises(CatalogError):
validate_entry(data)
def test_inline_secret_value_rejected():
data = copy.deepcopy(VALID)
data["token"] = "npm_realvalue"
with pytest.raises(CatalogError):
validate_entry(data)
def test_repo_catalog_loads_and_has_pilot():
entries = load_catalog(repo_root() / "catalog")
assert "whynot-design-npm-publish" in entries
pilot = entries["whynot-design-npm-publish"]
assert pilot.stage == "prod"
assert pilot.approval_required()
# org=coulomb, repo=whynot-design (not conflated); npm targets Gitea registry
assert pilot.org == "coulomb"
assert pilot.repo == "whynot-design"
assert pilot.npm["registry"].startswith("https://forgejo.coulomb.social/")
assert pilot.npm["scope"] == "@whynot"
# SECRETS-WP-0003 closeout uses the canonical State Hub decision UUID;
# the slug-named fixture is retired to offline fallback only.
uuid.UUID(pilot.approval["decision_ref"])
assert pilot.approval["decision_ref"] == "e6381a56-6b04-4fd5-b2de-f3ef59cde888"
# build/test/prod stage separation is representable
stages = {e.stage for e in entries.values()}
assert {"build", "prod"} <= stages