|
| 1 | +"""Validate that Railway database credentials stay runtime-configured.""" |
| 2 | + |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +ROOT = Path(__file__).resolve().parents[1] |
| 8 | + |
| 9 | +DEV_CONFIG = ROOT / "environments/dev-server-railway/common/config/main-local.php" |
| 10 | +DEPLOYMENT_SCRIPTS = [ |
| 11 | + ROOT / "environments/dev-server-railway/deployments/july_2025/3_july_2025_deployment.sh", |
| 12 | + ROOT / "environments/dev-server-railway/deployments/july_2025/29_july_2025_deployment.sh", |
| 13 | + ROOT / "environments/prod-railway/deployments/july_2025/3_july_2025_deployment.sh", |
| 14 | + ROOT / "environments/prod-railway/deployments/july_2025/29_july_2025_deployment.sh", |
| 15 | +] |
| 16 | +DOC = ROOT / "docs/railway-db-runtime-secrets.md" |
| 17 | + |
| 18 | +FORBIDDEN_SECRET_FRAGMENTS = [ |
| 19 | + "TpijAlObvfdvZxzPgrnMTHMxyekEqTtt", |
| 20 | + "hOCpxbVoSIbPUnuuBmaQGILPshVyRRuj", |
| 21 | + "nySjmLVspFXlYOzKrOFQcRwuUprjyDli", |
| 22 | + "JImnisvcRDpKLdWpoMECoHHoCbutPhQC", |
| 23 | +] |
| 24 | + |
| 25 | +DEV_CONFIG_ENV_VARS = [ |
| 26 | + "DB_DSN", |
| 27 | + "DB_USERNAME", |
| 28 | + "DB_PASSWORD", |
| 29 | + "WALLET_DB_DSN", |
| 30 | + "WALLET_DB_USERNAME", |
| 31 | + "WALLET_DB_PASSWORD", |
| 32 | + "REDIS_HOSTNAME", |
| 33 | + "REDIS_USERNAME", |
| 34 | + "REDIS_PASSWORD", |
| 35 | + "REDIS_PORT", |
| 36 | + "REDIS_DATABASE", |
| 37 | +] |
| 38 | + |
| 39 | +SCRIPT_TOKENS = [ |
| 40 | + 'MYSQL_HOST="${MYSQL_HOST:-mysql.railway.internal}"', |
| 41 | + 'MYSQL_PORT="${MYSQL_PORT:-3306}"', |
| 42 | + 'MYSQL_USER="${MYSQL_USER:-root}"', |
| 43 | + 'MYSQL_DATABASE="${MYSQL_DATABASE:-railway}"', |
| 44 | + ': "${MYSQL_PASSWORD:?MYSQL_PASSWORD must be set in Railway environment variables.}"', |
| 45 | + 'MYSQL_PWD="$MYSQL_PASSWORD" mysql', |
| 46 | +] |
| 47 | + |
| 48 | + |
| 49 | +def read(path): |
| 50 | + """Read a repository file as UTF-8 text.""" |
| 51 | + return path.read_text(encoding="utf-8") |
| 52 | + |
| 53 | + |
| 54 | +def require(condition, message): |
| 55 | + """Fail the check with a clear message when a condition is false.""" |
| 56 | + if not condition: |
| 57 | + raise SystemExit(message) |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + """Run the Railway runtime secret regression checks.""" |
| 62 | + config = read(DEV_CONFIG) |
| 63 | + doc = read(DOC) |
| 64 | + script_contents = [read(path) for path in DEPLOYMENT_SCRIPTS] |
| 65 | + checked_text = "\n".join([config, doc, *script_contents]) |
| 66 | + |
| 67 | + for fragment in FORBIDDEN_SECRET_FRAGMENTS: |
| 68 | + require(fragment not in checked_text, f"Committed Railway secret fragment remains: {fragment}") |
| 69 | + |
| 70 | + for name in DEV_CONFIG_ENV_VARS: |
| 71 | + require(f"getenv('{name}')" in config, f"Dev Railway config must use getenv('{name}').") |
| 72 | + require(f"`{name}`" in doc, f"Docs must mention `{name}`.") |
| 73 | + |
| 74 | + for path, content in zip(DEPLOYMENT_SCRIPTS, script_contents): |
| 75 | + for token in SCRIPT_TOKENS: |
| 76 | + require(token in content, f"{path} must contain {token!r}.") |
| 77 | + require('-p"$MYSQL_PASSWORD"' not in content, f"{path} must not pass MYSQL_PASSWORD on the command line.") |
| 78 | + require('MYSQL_PASSWORD="' not in content, f"{path} must not assign a committed MYSQL_PASSWORD.") |
| 79 | + |
| 80 | + print("Railway DB runtime secret check passed.") |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments