test_intel_deployment_gate.py
python
sha256:4a18e5cebf34af33a6c511683aec240fe33af4f9254a7e1b8251d0bfb296aca0
feat(intel): allow production intel execution for explicitl…
Sonnet 5.5
patch
3 days ago
| 1 | """Explicit, fail-closed deployment identity for intel execution — musehub#8 release gate (environment safety). |
| 2 | |
| 3 | The gate called a database "local" by host NAME. The deploy scripts (deploy/secrets.sh, deploy/setup-ec2*.sh) write ``DATABASE_URL=...@postgres:5432/...`` for the |
| 4 | self-hosted staging and production hosts too, so the name proved nothing and the repository allowlist was the only real guard. Identity is now explicit: |
| 5 | ``MUSEHUB_DEPLOYMENT`` must be ``local``, ``staging`` or ``production`` (matching ``MUSE_ENV``), and the repository must still be listed by exact id. |
| 6 | |
| 7 | Run (no database):: python -P -m pytest tests/test_intel_deployment_gate.py |
| 8 | |
| 9 | D1 Unset or unknown deployment identity refuses, even with the allowlist and a "local-looking" database. |
| 10 | D2 ``production`` runs only an exactly listed repository: empty/missing allowlist, ``*``, a prefix, another repo and an unknown identity all refuse; an inherited ``local`` allowlist does not enable it. |
| 11 | D3 ``local`` needs a local database host as well; ``staging`` needs only the allowlisted repository. |
| 12 | D4 An empty allowlist refuses everywhere; the check is re-read on every call. |
| 13 | D5 The experiment runner refuses unless the deployment is ``local``. |
| 14 | D6 Identity must be consistent with ``MUSE_ENV``: ``local`` + staging/production/malformed ``MUSE_ENV`` refuses (also with a non-empty allowlist); staging and |
| 15 | production need their own name; valid local and staging configurations still run; the experiment runner applies the same rule. |
| 16 | """ |
| 17 | from __future__ import annotations |
| 18 | |
| 19 | import pytest |
| 20 | |
| 21 | from musehub.services import musehub_intel_gate as gate |
| 22 | |
| 23 | REPO = "sha256:" + "a" * 64 |
| 24 | OTHER = "sha256:" + "b" * 64 |
| 25 | LOCAL_DB = "postgresql+asyncpg://musehub:pw@postgres:5432/musehub" |
| 26 | REMOTE_DB = "postgresql+asyncpg://musehub:[email protected]:5432/musehub" |
| 27 | |
| 28 | |
| 29 | @pytest.fixture |
| 30 | def env(monkeypatch): |
| 31 | from musehub.config import settings |
| 32 | |
| 33 | def setup(*, deployment: str | None, allow: str | None = REPO, database_url: str = LOCAL_DB, muse_env: str | None = "UNSET") -> None: |
| 34 | """``muse_env``: ``"UNSET"`` (default) = what a local worker has (no MUSE_ENV); ``None`` is also unset; any other string is the value.""" |
| 35 | for name, value in ((gate.DEPLOYMENT_ENV, deployment), (gate.ALLOWLIST_ENV, allow), (gate.MUSE_ENV_ENV, None if muse_env == "UNSET" else muse_env)): |
| 36 | if value is None: |
| 37 | monkeypatch.delenv(name, raising=False) |
| 38 | else: |
| 39 | monkeypatch.setenv(name, value) |
| 40 | monkeypatch.setattr(settings, "database_url", database_url) |
| 41 | return setup |
| 42 | |
| 43 | |
| 44 | @pytest.mark.parametrize("deployment", [None, "", "prod", "Local", " local", "dev", "development", "test", "LOCAL"]) |
| 45 | def test_d1_an_unset_or_unknown_identity_refuses(env, deployment) -> None: |
| 46 | env(deployment=deployment) |
| 47 | assert gate.intel_execution_allowed(REPO) is False |
| 48 | |
| 49 | |
| 50 | @pytest.mark.parametrize("database_url", [LOCAL_DB, REMOTE_DB]) # the self-hosted production DB host IS called 'postgres': identity decides, not the name |
| 51 | def test_d2_production_runs_only_an_exactly_listed_repository(env, database_url) -> None: |
| 52 | env(deployment="production", muse_env="production", allow=REPO, database_url=database_url) |
| 53 | assert gate.intel_execution_allowed(REPO) is True |
| 54 | assert gate.intel_execution_allowed(OTHER) is False |
| 55 | assert gate.intel_execution_allowed("") is False |
| 56 | assert gate.intel_execution_allowed(REPO[:20]) is False # prefix of a listed id is not listed |
| 57 | assert gate.intel_execution_allowed(REPO + "0") is False |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize("allow", [None, "", " , ", "*", "sha256:*", "sha256:", "all"]) |
| 61 | def test_d2_production_without_an_exact_listing_refuses(env, allow) -> None: |
| 62 | env(deployment="production", muse_env="production", allow=allow) |
| 63 | assert gate.intel_execution_allowed(REPO) is False |
| 64 | assert gate.intel_execution_allowed(OTHER) is False |
| 65 | |
| 66 | |
| 67 | def test_d2_production_listing_is_per_repository_and_reread(env, monkeypatch) -> None: |
| 68 | env(deployment="production", muse_env="production", allow=f"{REPO}, {OTHER}") |
| 69 | assert gate.intel_execution_allowed(REPO) is True and gate.intel_execution_allowed(OTHER) is True |
| 70 | assert gate.intel_execution_allowed("sha256:" + "c" * 64) is False |
| 71 | monkeypatch.setenv(gate.ALLOWLIST_ENV, OTHER) |
| 72 | assert gate.intel_execution_allowed(REPO) is False # nothing captured at import time |
| 73 | |
| 74 | |
| 75 | def test_d2_an_inherited_local_or_staging_allowlist_does_not_enable_production(env) -> None: |
| 76 | """Identity, not the allowlist, selects the deployment: a production process must carry its own name.""" |
| 77 | for deployment, muse_env in (("local", "production"), ("staging", "production"), ("production", "staging"), ("production", "UNSET"), ("production", "local")): |
| 78 | env(deployment=deployment, muse_env=muse_env, allow=REPO) |
| 79 | assert gate.intel_execution_allowed(REPO) is False, (deployment, muse_env) |
| 80 | |
| 81 | |
| 82 | def test_d3_local_needs_a_local_database_and_staging_needs_only_the_listed_repository(env) -> None: |
| 83 | env(deployment="local") |
| 84 | assert gate.intel_execution_allowed(REPO) is True |
| 85 | assert gate.intel_execution_allowed(OTHER) is False |
| 86 | env(deployment="local", database_url=REMOTE_DB) |
| 87 | assert gate.intel_execution_allowed(REPO) is False |
| 88 | env(deployment="staging", muse_env="staging", database_url=LOCAL_DB) # staging's database host is also 'postgres': identity, not the name, decides |
| 89 | assert gate.intel_execution_allowed(REPO) is True |
| 90 | assert gate.intel_execution_allowed(OTHER) is False |
| 91 | env(deployment="staging", muse_env="staging", database_url=REMOTE_DB) |
| 92 | assert gate.intel_execution_allowed(REPO) is True |
| 93 | |
| 94 | |
| 95 | def test_d4_an_empty_allowlist_refuses_everywhere_and_the_check_is_reread(env, monkeypatch) -> None: |
| 96 | for deployment, muse_env in (("local", "UNSET"), ("staging", "staging"), ("production", "production")): |
| 97 | env(deployment=deployment, muse_env=muse_env, allow=None) |
| 98 | assert gate.intel_execution_allowed(REPO) is False |
| 99 | env(deployment=deployment, muse_env=muse_env, allow=" , ") |
| 100 | assert gate.intel_execution_allowed(REPO) is False |
| 101 | env(deployment="staging", muse_env="staging") |
| 102 | assert gate.intel_execution_allowed(REPO) is True |
| 103 | env(deployment="production", muse_env="production") |
| 104 | assert gate.intel_execution_allowed(REPO) is True |
| 105 | monkeypatch.delenv(gate.ALLOWLIST_ENV) |
| 106 | assert gate.intel_execution_allowed(REPO) is False # nothing captured at import time |
| 107 | |
| 108 | |
| 109 | def test_d5_the_experiment_runner_refuses_unless_the_deployment_is_local() -> None: |
| 110 | from musehub import intel_experiment as exp |
| 111 | |
| 112 | base = {exp.ENABLE_ENV: "1"} |
| 113 | for deployment in (None, "staging", "production", "Local"): |
| 114 | environ = dict(base) if deployment is None else {**base, gate.DEPLOYMENT_ENV: deployment} |
| 115 | with pytest.raises(exp.ExperimentRefused, match="deployment"): |
| 116 | exp.check_enabled(environ, LOCAL_DB) |
| 117 | exp.check_enabled({**base, gate.DEPLOYMENT_ENV: "local"}, LOCAL_DB) # must not raise |
| 118 | |
| 119 | |
| 120 | @pytest.mark.parametrize("muse_env", ["UNSET", "", "local", "development", "test"]) |
| 121 | def test_d6_a_valid_local_configuration_runs(env, muse_env) -> None: |
| 122 | env(deployment="local", muse_env=muse_env) |
| 123 | assert gate.intel_execution_allowed(REPO) is True |
| 124 | |
| 125 | |
| 126 | def test_d6_a_valid_staging_configuration_runs(env) -> None: |
| 127 | env(deployment="staging", muse_env="staging") |
| 128 | assert gate.intel_execution_allowed(REPO) is True |
| 129 | assert gate.deployment({gate.DEPLOYMENT_ENV: "staging", gate.MUSE_ENV_ENV: "staging"}) == "staging" |
| 130 | |
| 131 | |
| 132 | @pytest.mark.parametrize("muse_env", ["staging", "production", "Production", " staging", "prod", "weird"]) |
| 133 | def test_d6_local_identity_with_a_hosted_or_malformed_muse_env_refuses_even_with_an_allowlist(env, muse_env) -> None: |
| 134 | """What a hosted host looks like if Compose loaded a file that declares ``local``: the allowlist and a ``postgres`` database host must not rescue it.""" |
| 135 | env(deployment="local", allow=REPO, database_url=LOCAL_DB, muse_env=muse_env) |
| 136 | assert gate.intel_execution_allowed(REPO) is False |
| 137 | assert gate.deployment() is None |
| 138 | |
| 139 | |
| 140 | @pytest.mark.parametrize("muse_env", ["UNSET", "", "local", "production", "development", "Staging"]) |
| 141 | def test_d6_staging_identity_needs_a_matching_muse_env(env, muse_env) -> None: |
| 142 | env(deployment="staging", muse_env=muse_env) |
| 143 | assert gate.intel_execution_allowed(REPO) is False |
| 144 | |
| 145 | |
| 146 | @pytest.mark.parametrize("muse_env", ["UNSET", "production", "staging", "local", "Production", " production", ""]) |
| 147 | def test_d6_production_identity_needs_a_matching_muse_env(env, muse_env) -> None: |
| 148 | env(deployment="production", muse_env=muse_env) |
| 149 | assert gate.intel_execution_allowed(REPO) is (muse_env == "production") |
| 150 | |
| 151 | |
| 152 | @pytest.mark.parametrize("deployment", [None, "", "prod", "Local", " local", "LOCAL"]) |
| 153 | @pytest.mark.parametrize("muse_env", ["UNSET", "local", "staging", "production"]) |
| 154 | def test_d6_a_missing_or_malformed_identity_refuses_under_any_muse_env(env, deployment, muse_env) -> None: |
| 155 | env(deployment=deployment, muse_env=muse_env) |
| 156 | assert gate.intel_execution_allowed(REPO) is False |
| 157 | |
| 158 | |
| 159 | def test_d6_the_experiment_runner_applies_the_same_conflict_rule() -> None: |
| 160 | from musehub import intel_experiment as exp |
| 161 | |
| 162 | ok = {exp.ENABLE_ENV: "1", gate.DEPLOYMENT_ENV: "local"} |
| 163 | exp.check_enabled(ok, LOCAL_DB) |
| 164 | exp.check_enabled({**ok, gate.MUSE_ENV_ENV: "local"}, LOCAL_DB) |
| 165 | for conflicting in ("staging", "production", "Local"): |
| 166 | with pytest.raises(exp.ExperimentRefused, match="deployment"): |
| 167 | exp.check_enabled({**ok, gate.MUSE_ENV_ENV: conflicting}, LOCAL_DB) |
| 168 | |
| 169 | |
| 170 | def test_d6_the_committed_compose_override_does_not_declare_a_deployment_identity() -> None: |
| 171 | """Compose loads docker-compose.override.yml on any host that has the file; it must not be able to make a hosted deployment 'local'. Identity comes from .env.""" |
| 172 | from pathlib import Path |
| 173 | |
| 174 | root = Path(__file__).resolve().parent.parent |
| 175 | assert gate.DEPLOYMENT_ENV not in "".join(l for l in (root / "docker-compose.override.yml").read_text().splitlines() if not l.lstrip().startswith("#")) |
| 176 | assert gate.DEPLOYMENT_ENV not in (root / "docker-compose.yml").read_text() |
| 177 | assert f"{gate.DEPLOYMENT_ENV}=local" in (root / ".env.example").read_text() # the developer's explicit opt-in |
| 178 | assert f"{gate.DEPLOYMENT_ENV}=${{MUSEHUB_ENV}}" in (root / "deploy" / "secrets.sh").read_text() # hosted: written by secrets.sh beside MUSE_ENV |
| 179 | assert "MUSE_ENV=${MUSEHUB_ENV}" in (root / "deploy" / "secrets.sh").read_text() |
File History
3 commits
sha256:4a18e5cebf34af33a6c511683aec240fe33af4f9254a7e1b8251d0bfb296aca0
feat(intel): allow production intel execution for explicitl…
Sonnet 5.5
patch
3 days ago
sha256:fae7474413778dd8c57733b9e0e8804fed9ebaba62533b69583fab2ca619dd9d
fix(intel): a deployment identity that contradicts MUSE_ENV…
Sonnet 5.5
minor
⚠
3 days ago
sha256:64d53876b5299082b6b4bfbe92721976ec7bb5f1d9a88fdf6eafc1490148d27c
fix(intel): explicit, fail-closed deployment identity repla…
Sonnet 5.5
minor
⚠
4 days ago