2026-08-21 08:20:33 +02:00
|
|
|
import copy
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from secrets_engine.catalog import validate_entry
|
|
|
|
|
from secrets_engine.errors import DeliveryError
|
|
|
|
|
from secrets_engine.exec_delivery import (
|
2026-08-23 12:05:58 +02:00
|
|
|
_fetch_value,
|
2026-08-21 08:20:33 +02:00
|
|
|
_npm_userconfig,
|
|
|
|
|
_registry_authkey,
|
|
|
|
|
exec_with_secret,
|
|
|
|
|
)
|
|
|
|
|
from tests.test_catalog import VALID
|
2026-06-28 12:44:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_registry_authkey_strips_scheme_and_trails_slash():
|
|
|
|
|
assert (
|
2026-07-09 11:38:15 +02:00
|
|
|
_registry_authkey("https://forgejo.coulomb.social/api/packages/coulomb/npm/")
|
|
|
|
|
== "//forgejo.coulomb.social/api/packages/coulomb/npm/"
|
2026-06-28 12:44:55 +02:00
|
|
|
)
|
|
|
|
|
# missing trailing slash is added
|
|
|
|
|
assert _registry_authkey("https://host/api/npm") == "//host/api/npm/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_npm_userconfig_writes_registry_and_token_ref_not_value():
|
2026-07-09 11:38:15 +02:00
|
|
|
registry = "https://forgejo.coulomb.social/api/packages/coulomb/npm/"
|
feat(policy): netkingdom maturity-gated publication-scope policy
Token scope is now bound to package maturity, gated on netkingdom's own maturity:
- maturity-build -> gitea-wide, maturity-test -> org-wide, maturity-prod -> repo-scoped
(scope narrows as stakes rise; broad tokens only for low-stakes build artifacts)
- the graduated table is DORMANT until netkingdom reaches production grade; until
then every lane clamps to repo-scope, injected as NPM_AUTH_TOKEN (fail-safe)
- token env-var name signals blast radius: NPM_AUTH_TOKEN (repo default),
NPM_AUTH_COULOMB_TOKEN (org), NPM_AUTH_GITEA_TOKEN (gitea), NPM_AUTH_WHYNOT_TOKEN
(npm scope, defined but unused), NPM_AUTH_WHYNOTDESIGN (explicit repo)
netkingdom is at maturity-build today, so whynot-design resolves to repo-scope /
NPM_AUTH_TOKEN. Flip netkingdom_maturity to maturity-prod to activate graduation.
- policies/netkingdom-publication-scope.yaml: the policy data + gate
- publication_policy.py: load + resolve (clamp/active, env naming, override)
- exec delivery injects under the resolved env-var name (was fixed SE_NPM_TOKEN)
- catalog lane carries delivery_config.npm.maturity
- new CLI: `secrets-engine policy publication <lane>`
- docs/publication-scope-policy.md; tests for clamp, graduation, naming, override
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 13:14:46 +02:00
|
|
|
with _npm_userconfig(registry, "@whynot", "NPM_AUTH_TOKEN") as path:
|
2026-06-28 12:44:55 +02:00
|
|
|
body = path.read_text()
|
|
|
|
|
assert f"@whynot:registry={registry}" in body
|
|
|
|
|
# token is referenced via env expansion, never written literally
|
feat(policy): netkingdom maturity-gated publication-scope policy
Token scope is now bound to package maturity, gated on netkingdom's own maturity:
- maturity-build -> gitea-wide, maturity-test -> org-wide, maturity-prod -> repo-scoped
(scope narrows as stakes rise; broad tokens only for low-stakes build artifacts)
- the graduated table is DORMANT until netkingdom reaches production grade; until
then every lane clamps to repo-scope, injected as NPM_AUTH_TOKEN (fail-safe)
- token env-var name signals blast radius: NPM_AUTH_TOKEN (repo default),
NPM_AUTH_COULOMB_TOKEN (org), NPM_AUTH_GITEA_TOKEN (gitea), NPM_AUTH_WHYNOT_TOKEN
(npm scope, defined but unused), NPM_AUTH_WHYNOTDESIGN (explicit repo)
netkingdom is at maturity-build today, so whynot-design resolves to repo-scope /
NPM_AUTH_TOKEN. Flip netkingdom_maturity to maturity-prod to activate graduation.
- policies/netkingdom-publication-scope.yaml: the policy data + gate
- publication_policy.py: load + resolve (clamp/active, env naming, override)
- exec delivery injects under the resolved env-var name (was fixed SE_NPM_TOKEN)
- catalog lane carries delivery_config.npm.maturity
- new CLI: `secrets-engine policy publication <lane>`
- docs/publication-scope-policy.md; tests for clamp, graduation, naming, override
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 13:14:46 +02:00
|
|
|
assert "${NPM_AUTH_TOKEN}" in body
|
2026-07-09 11:38:15 +02:00
|
|
|
assert "//forgejo.coulomb.social/api/packages/coulomb/npm/:_authToken" in body
|
2026-06-28 12:44:55 +02:00
|
|
|
# file is mode 0600
|
|
|
|
|
assert (path.stat().st_mode & 0o077) == 0
|
|
|
|
|
# cleaned up on context exit
|
|
|
|
|
assert not path.exists()
|
2026-08-21 08:20:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_exec_env_injects_only_selected_declared_field(monkeypatch):
|
|
|
|
|
data = copy.deepcopy(VALID)
|
|
|
|
|
data["fields"] = ["primary", "selected_value"]
|
|
|
|
|
entry = validate_entry(data)
|
|
|
|
|
|
|
|
|
|
def fake_fetch(_client, got_entry, field):
|
|
|
|
|
assert got_entry == entry
|
|
|
|
|
assert field == "selected_value"
|
|
|
|
|
return "test-secret-value"
|
|
|
|
|
|
|
|
|
|
def fake_spawn(command, env, secret):
|
|
|
|
|
assert command == ["probe"]
|
|
|
|
|
assert secret == "test-secret-value"
|
|
|
|
|
assert env["SELECTED_VALUE"] == "test-secret-value"
|
|
|
|
|
assert "PRIMARY" not in env
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("secrets_engine.exec_delivery._fetch_value", fake_fetch)
|
|
|
|
|
monkeypatch.setattr("secrets_engine.exec_delivery._spawn", fake_spawn)
|
|
|
|
|
assert (
|
|
|
|
|
exec_with_secret(
|
|
|
|
|
object(), entry, "selected_value", ["probe"], mode="exec-env"
|
|
|
|
|
)
|
|
|
|
|
== 0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_exec_rejects_undeclared_field_before_fetch(monkeypatch):
|
|
|
|
|
entry = validate_entry(VALID)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"secrets_engine.exec_delivery._fetch_value",
|
|
|
|
|
lambda *_args, **_kwargs: pytest.fail("must not fetch undeclared field"),
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(DeliveryError):
|
|
|
|
|
exec_with_secret(object(), entry, "other_field", ["probe"], mode="exec-env")
|
2026-08-23 12:05:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_records_non_secret_session_cleanup_before_child(monkeypatch):
|
|
|
|
|
entry = validate_entry(VALID)
|
|
|
|
|
|
|
|
|
|
class Session:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.client = self
|
|
|
|
|
self.closed = False
|
|
|
|
|
|
|
|
|
|
def _run(self, _args):
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
return SimpleNamespace(
|
|
|
|
|
returncode=0,
|
|
|
|
|
stdout=json.dumps({"data": {"data": {"api_token": "test-value"}}}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def evidence(self):
|
|
|
|
|
return {
|
|
|
|
|
"session_handle": "safe-handle",
|
|
|
|
|
"established": True,
|
|
|
|
|
"revocation_attempted": self.closed,
|
|
|
|
|
"revocation_succeeded": self.closed,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session = Session()
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def approle_session(self, _role):
|
|
|
|
|
try:
|
|
|
|
|
yield session
|
|
|
|
|
finally:
|
|
|
|
|
session.closed = True
|
|
|
|
|
|
|
|
|
|
evidence = {}
|
|
|
|
|
value = _fetch_value(Client(), entry, "api_token", session_evidence=evidence)
|
|
|
|
|
|
|
|
|
|
assert value == "test-value"
|
|
|
|
|
assert evidence == {
|
|
|
|
|
"session_handle": "safe-handle",
|
|
|
|
|
"established": True,
|
|
|
|
|
"revocation_attempted": True,
|
|
|
|
|
"revocation_succeeded": True,
|
|
|
|
|
}
|
|
|
|
|
assert "test-value" not in repr(evidence)
|