Files
computer-playbook/utils/cleanup/tokens.py
T
kevinveenbirkenbachandClaude Opus 4.7 5325fe3cf0 refactor(purge): app-keyed orchestrator + wipe stale tokens between rounds
The token store at /var/lib/infinito/secrets/tokens.yml survived
compose+volumes purges, so matrix-deploy variant transitions left a
stale matomo token in place. On the next round the token-empty guard
in sys-front-inj-matomo never fired and the public-URL probe hit an
empty openresty upstream, returning 502.

- utils/cleanup/tokens.py: wipes users.<user>.tokens.<app_id> entries
  via utils.cache.yaml (load_yaml_any / dump_yaml).
- scripts/container/purge/apps.sh: app-keyed orchestrator that maps
  $APPS to compose entities, drives entity/{db,compose,dir}.sh, and
  invokes utils.cleanup.tokens for the same app set.
- scripts/tests/deploy/local/purge/entity.sh becomes a thin
  docker-exec wrapper invoking apps.sh inside the runner.
- Drop scripts/container/purge/entity/all.sh; its trivial loop and
  unused --wipe-data-only / --db-only flags fold into apps.sh.
- tests/unit/utils/cleanup/test_tokens.py covers wipe paths (single
  app, multi-user, multi-app, no-match no-rewrite, missing file,
  user-without-tokens, non-dict user) plus the __main__ shim.
- READMEs updated for the new layout.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 02:25:04 +02:00

76 lines
2.2 KiB
Python

"""Token-store wipe helper.
The token store at ``/var/lib/infinito/secrets/tokens.yml`` survives
compose+volumes purges. Matrix-deploy variant transitions wipe an app's
persistent state but the stale token would otherwise be reused on the next
round (for example matomo's API probe in ``sys-front-inj-matomo``, whose
token-empty guard never fires while the prior round's token is still
present). The purge orchestrator invokes this module to drop the matching
``users.<user>.tokens.<app_id>`` entries alongside the entity-level wipe.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
from utils.cache.yaml import dump_yaml, load_yaml_any
DEFAULT_TOKENS_FILE = Path("/var/lib/infinito/secrets/tokens.yml")
def _resolve_tokens_file() -> Path:
env_path = os.environ.get("FILE_TOKENS")
return Path(env_path) if env_path else DEFAULT_TOKENS_FILE
def wipe_tokens(app_ids: list[str], tokens_file: Path | None = None) -> list[str]:
"""Remove ``users.<user>.tokens.<app_id>`` entries for *app_ids*.
Returns the list of removed ``<user>.<app_id>`` keys (empty when the
file is missing or no entries matched). The file is rewritten only
when at least one entry was removed.
"""
path = tokens_file or _resolve_tokens_file()
if not path.exists():
return []
data = load_yaml_any(str(path), default_if_missing={}) or {}
users = data.get("users") or {}
removed: list[str] = []
for user_key, user_data in users.items():
if not isinstance(user_data, dict):
continue
tokens = user_data.get("tokens") or {}
for app_id in app_ids:
if app_id in tokens:
del tokens[app_id]
removed.append(f"{user_key}.{app_id}")
if removed:
dump_yaml(path, data)
return removed
def main(argv: list[str]) -> int:
if not argv:
print(
"usage: python -m utils.cleanup.tokens <APP_ID> [APP_ID ...]",
file=sys.stderr,
)
return 2
removed = wipe_tokens(argv)
if removed:
print(f">>> Wiped token entries: {', '.join(removed)}")
else:
print(">>> No token entries to wipe")
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))