Files
cie-auth/scripts/rewrite_dumps.py
T

63 lines
2.2 KiB
Python

#!/usr/bin/env python3
import json
import os
import pathlib
import sys
ROOT = pathlib.Path(__file__).resolve().parents[1]
def env(name: str, default: str = "") -> str:
return os.getenv(name, default).strip().rstrip("/")
def infer_base_url(kind: str) -> str:
hosts = env("DJANGO_ALLOWED_HOSTS") or env("CIE_AUTH_ALLOWED_HOSTS")
items = [host.strip() for host in hosts.split(",") if host.strip() and host.strip() != "*"]
for host in items:
if kind in host:
return f"https://{host}"
defaults = {
"fa": "http://federation-authority:8000",
"provider": "http://provider:8002",
"rp": "http://relying-party:8001",
}
if kind in defaults:
return defaults[kind]
raise SystemExit(f"missing env for {kind}: set explicit base URL or include hostname in allowed hosts")
TA = env("DJANGO_TRUST_ANCHOR") or infer_base_url("fa")
PROVIDER_ISSUER = env("DJANGO_PROVIDER_ISSUER") or f"{infer_base_url('provider')}/oidc/op"
RP_BASE = env("DJANGO_RP_BASE_URL") or infer_base_url("rp")
RP_ISSUER = f"{RP_BASE}/oidc/rp"
PROVIDER_BASE = PROVIDER_ISSUER.removesuffix("/oidc/op")
def deep_replace(value):
if isinstance(value, dict):
return {k: deep_replace(v) for k, v in value.items()}
if isinstance(value, list):
return [deep_replace(v) for v in value]
if isinstance(value, str):
return (
value.replace("http://127.0.0.1:8000/oidc/op", TA + "/oidc/op")
.replace("http://127.0.0.1:8001/oidc/rp", RP_ISSUER)
.replace("http://127.0.0.1:8002/oidc/op", PROVIDER_ISSUER)
.replace("http://127.0.0.1:8000", TA)
.replace("http://127.0.0.1:8001", RP_BASE)
.replace("http://127.0.0.1:8002", PROVIDER_BASE)
.replace("http://provider:8002/oidc/op", PROVIDER_ISSUER)
.replace("http://provider:8002", PROVIDER_BASE)
)
return value
if len(sys.argv) != 3:
raise SystemExit("usage: rewrite_dumps.py <input_dump> <output_dump>")
src = pathlib.Path(sys.argv[1])
dst = pathlib.Path(sys.argv[2])
data = json.loads(src.read_text())
dst.write_text(json.dumps(deep_replace(data), indent=2, ensure_ascii=False) + "\n")