fix(tests): keep the host .env and the nix token out of bound act runs

make swarm-zombie runs act with ACT_BIND=true (Makefile:898), and workflow.sh also binds when roles/web-app-cst-* exist. A bound job works in the host checkout, so the deploy step's make setup -> make dotenv rewrote the host .env: INFINITO_DEPLOY_MODE=swarm, the swarm distro, INFINITO_SWARM_STEP_TIMEOUT_MINUTES=690 and NIX_CONFIG. call-test-deploy.yml puts the act GITHUB_TOKEN into NIX_CONFIG, and nix_config.py copied its access-tokens line into .env, so the token sat in plaintext in the host worktree and the next local gate failed test_distros and test_build.

Two layers:
- workflow.sh passes INFINITO_DOTENV_HOST_OWNED=true whenever it binds, so the job keeps the host .env (the guard compose.yml already uses).
- nix_config.py writes the access-tokens line only on a real GitHub runner (detect_gha_act: GITHUB_ACTIONS without ACT), never under act or on a developer machine.

Not covered: .env files that already hold a token need make dotenv-force and the token revoked; self-hosted GitHub runners still write the short-lived job token; INFINITO_API_*_KEY values still pass through.

Tests: test_nix_config.py (3, new) and test_workflow.py (3 new of 11); negative control: with the fix lines reverted, 2 of 3 fail in each file. Both files pass (3 and 11). ruff and shellcheck clean. MEDUSA 100 %: three files under env paths scanned under renamed paths, because the scanner's excludes skip any path containing env.

Reported by Stage 2.0.

🎯 Fix: 90 %
💥 Break: 5 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 86
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-11-19 10:21:16 +00:00
1 parent 78e389b025
commit 23e2fcba8f
6 files changed
+103 -9

No files matched your search

@@ -7,11 +7,13 @@ import tempfile
import unittest
from pathlib import Path
from cli.meta.env.__main__ import HOST_OWNED_KEY
from utils.cache.files import PROJECT_ROOT
SCRIPT = PROJECT_ROOT / "scripts" / "tests" / "deploy" / "act" / "workflow.sh"
ACT_STUB = '#!/usr/bin/env bash\nprintf \'%s\\0\' "$@" >"${STUB_LOG}"\n'
DB_ENV = "INFINITO_VULNERABILITY_DB_HOST_PATH"
HOST_OWNED = f"{HOST_OWNED_KEY}=true"
EXTRA_VARS = json.dumps(
{
"applications": {
@@ -23,7 +25,11 @@ EXTRA_VARS = json.dumps(
def run_act(
db_host_path: str | None, act_env: str = ""
db_host_path: str | None,
act_env: str = "",
*,
act_bind: str | None = None,
custom_role: bool = False,
) -> tuple[subprocess.CompletedProcess, list[str], Path, bool]:
"""Run the wrapper against an ``act`` stub that records its argv.
@@ -31,6 +37,9 @@ def run_act(
db_host_path: value of ``INFINITO_VULNERABILITY_DB_HOST_PATH`` below a
scratch directory, or None to leave it unset.
act_env: value of ``ACT_ENV``.
act_bind: value of ``ACT_BIND``, or None to leave it unset.
custom_role: run the wrapper from a scratch checkout that holds a
pulled ``roles/web-app-cst-*`` role instead of this repository.
Returns:
The finished process, the argv act received (one list item per
@@ -38,6 +47,14 @@ def run_act(
whether it existed afterwards.
"""
with tempfile.TemporaryDirectory() as tmp:
script = SCRIPT
if custom_role:
root = Path(tmp) / "repo"
(root / "roles" / "web-app-cst-demo").mkdir(parents=True)
(root / ".github" / "workflows").mkdir(parents=True)
script = root / "scripts" / "tests" / "deploy" / "act" / "workflow.sh"
script.parent.mkdir(parents=True)
script.symlink_to(SCRIPT)
stub = Path(tmp) / "act"
stub.write_text(ACT_STUB)
stub.chmod(0o755)
@@ -48,8 +65,10 @@ def run_act(
env = {
k: v
for k, v in os.environ.items()
if k not in ("BASH_ENV", DB_ENV, "ACT_CONTAINER_OPTIONS")
if k not in ("BASH_ENV", DB_ENV, "ACT_CONTAINER_OPTIONS", "ACT_BIND")
}
if act_bind is not None:
env["ACT_BIND"] = act_bind
env.update(
PATH=f"{tmp}{os.pathsep}{os.environ['PATH']}",
STUB_LOG=str(log),
@@ -63,7 +82,7 @@ def run_act(
if db_host_path is not None:
env[DB_ENV] = str(shared)
done = subprocess.run(
["bash", str(SCRIPT)],
["bash", str(script)],
env=env,
capture_output=True,
text=True,
@@ -99,10 +118,12 @@ def env_items(argv: list[str]) -> list[str]:
Returns:
Every ``--env`` value in order, without the wrapper's own
``INFINITO_VULNERABILITY_DB_HOST_PATH`` pin.
``INFINITO_VULNERABILITY_DB_HOST_PATH`` and host-owned ``.env`` pins.
"""
return [
value for value in option(argv, "--env") if not value.startswith(f"{DB_ENV}=")
value
for value in option(argv, "--env")
if not value.startswith(f"{DB_ENV}=") and value != HOST_OWNED
]
@@ -165,5 +186,24 @@ class TestActWorkflowEnv(unittest.TestCase):
self.assertEqual(items, ["INFINITO_INVENTORY_EXTRA_VARS=", "disable=css"])
class TestBoundCheckoutKeepsTheHostDotenv(unittest.TestCase):
def test_an_explicit_bind_keeps_the_host_dotenv(self) -> None:
done, argv, _shared, _created = run_act("vulnerability-db", act_bind="true")
self.assertEqual(done.returncode, 0, done.stderr)
self.assertIn("--bind", argv)
self.assertIn(HOST_OWNED, option(argv, "--env"))
def test_a_bind_for_pulled_custom_roles_keeps_the_host_dotenv(self) -> None:
done, argv, _shared, _created = run_act("vulnerability-db", custom_role=True)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertIn("--bind", argv)
self.assertIn(HOST_OWNED, option(argv, "--env"))
def test_the_marker_travels_only_with_the_bind(self) -> None:
done, argv, _shared, _created = run_act("vulnerability-db", act_bind="false")
self.assertEqual(done.returncode, 0, done.stderr)
self.assertEqual("--bind" in argv, HOST_OWNED in option(argv, "--env"))
if __name__ == "__main__":
unittest.main()