51 lines
1.6 KiB
Python
51 lines
1.6 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:
|
|
value = os.getenv(name, default).strip()
|
|
if not value:
|
|
raise SystemExit(f"missing env: {name}")
|
|
return value.rstrip("/")
|
|
|
|
|
|
TA = env("DJANGO_TRUST_ANCHOR")
|
|
PROVIDER_ISSUER = env("DJANGO_PROVIDER_ISSUER")
|
|
RP_BASE = env("DJANGO_RP_BASE_URL")
|
|
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")
|