#!/usr/bin/env python3 """Fail when the SeaweedFS identities and the API environment disagree. The stand keeps synthetic credentials in two places on purpose: the identities file is mounted into SeaweedFS, while the API receives the same values through the environment. This check keeps the copies from drifting. """ from __future__ import annotations import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent IDENTITIES = ROOT / "infra/s3/s3.json" def parse_env(path: Path) -> dict[str, str]: values: dict[str, str] = {} for raw in path.read_text().splitlines(): line = raw.strip() if not line or line.startswith("#") or "=" not in line: continue key, value = line.split("=", 1) values[key.strip()] = value.strip().strip('"').strip("'") return values def main() -> int: identities = json.loads(IDENTITIES.read_text()) by_name = {item["name"]: item for item in identities.get("identities", [])} source = ROOT / ".env" if not source.is_file(): source = ROOT / ".env.example" env = parse_env(source) expected = { "app": ("S3_ACCESS_KEY_ID", "S3_SECRET_ACCESS_KEY"), } failures: list[str] = [] for name, (key_var, secret_var) in expected.items(): identity = by_name.get(name) if identity is None: failures.append(f"{IDENTITIES}: identity {name!r} is missing") continue credentials = identity.get("credentials") or [] if not credentials: failures.append(f"{IDENTITIES}: identity {name!r} has no credentials") continue access_key = credentials[0].get("accessKey") secret_key = credentials[0].get("secretKey") if env.get(key_var) != access_key: failures.append(f"{source.name}: {key_var} does not match identity {name!r}") if env.get(secret_var) != secret_key: failures.append(f"{source.name}: {secret_var} does not match identity {name!r}") if failures: print("FAIL: S3 credentials are inconsistent") for failure in failures: print(" -", failure) return 1 if "admin" not in by_name: print("FAIL: infra/s3/s3.json has no admin identity for s3-init") return 1 print(f"PASS: S3 app identity matches {source.name}; admin stays in s3.json/s3-init") return 0 if __name__ == "__main__": sys.exit(main())