DNS-Token-Fallback für Butler-Proxyroute #8
2 changed files with 8 additions and 7 deletions
7
app.py
7
app.py
|
|
@ -920,6 +920,7 @@ def _validate_proxy_route(domain: str, upstream: str) -> tuple[str, str, str, st
|
||||||
class ProxyRouteRequest(BaseModel):
|
class ProxyRouteRequest(BaseModel):
|
||||||
domain: str
|
domain: str
|
||||||
upstream: str
|
upstream: str
|
||||||
|
dns_token: str | None = None
|
||||||
|
|
||||||
|
|
||||||
def _remote_python(script: str, timeout: int = 30) -> tuple[int, str, str]:
|
def _remote_python(script: str, timeout: int = 30) -> tuple[int, str, str]:
|
||||||
|
|
@ -995,8 +996,8 @@ def _get_hetzner_dns_token() -> str:
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
||||||
async def _upsert_dns_records(zone: str, name: str) -> dict:
|
async def _upsert_dns_records(zone: str, name: str, token_override: str | None = None) -> dict:
|
||||||
token = await asyncio.to_thread(_get_hetzner_dns_token)
|
token = token_override or await asyncio.to_thread(_get_hetzner_dns_token)
|
||||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||||
api = "https://api.hetzner.cloud/v1"
|
api = "https://api.hetzner.cloud/v1"
|
||||||
async with httpx.AsyncClient(timeout=30) as client:
|
async with httpx.AsyncClient(timeout=30) as client:
|
||||||
|
|
@ -1027,7 +1028,7 @@ async def vps_proxy_route(req: ProxyRouteRequest, _=Depends(_verify)):
|
||||||
raise HTTPException(400, str(exc)) from exc
|
raise HTTPException(400, str(exc)) from exc
|
||||||
caddy = await asyncio.to_thread(_configure_caddy_route, domain, upstream)
|
caddy = await asyncio.to_thread(_configure_caddy_route, domain, upstream)
|
||||||
try:
|
try:
|
||||||
dns = await _upsert_dns_records(zone, name)
|
dns = await _upsert_dns_records(zone, name, req.dns_token)
|
||||||
except Exception:
|
except Exception:
|
||||||
await asyncio.to_thread(_restore_caddy_backup, caddy["backup"])
|
await asyncio.to_thread(_restore_caddy_backup, caddy["backup"])
|
||||||
raise
|
raise
|
||||||
|
|
|
||||||
|
|
@ -290,8 +290,8 @@ def test_proxy_route_endpoint_configures_caddy_and_dns(monkeypatch):
|
||||||
calls.append(("caddy", domain, upstream))
|
calls.append(("caddy", domain, upstream))
|
||||||
return {"status": "reloaded", "backup": "/app-config/caddy/Caddyfile.bak-test"}
|
return {"status": "reloaded", "backup": "/app-config/caddy/Caddyfile.bak-test"}
|
||||||
|
|
||||||
async def fake_dns(zone, name):
|
async def fake_dns(zone, name, token_override=None):
|
||||||
calls.append(("dns", zone, name))
|
calls.append(("dns", zone, name, token_override))
|
||||||
return {"zone_id": 123, "records": ["A", "AAAA"]}
|
return {"zone_id": 123, "records": ["A", "AAAA"]}
|
||||||
|
|
||||||
monkeypatch.setattr(app, "_configure_caddy_route", fake_caddy)
|
monkeypatch.setattr(app, "_configure_caddy_route", fake_caddy)
|
||||||
|
|
@ -300,14 +300,14 @@ def test_proxy_route_endpoint_configures_caddy_and_dns(monkeypatch):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/vps/proxy-route",
|
"/vps/proxy-route",
|
||||||
headers={"Authorization": "Bearer test-token"},
|
headers={"Authorization": "Bearer test-token"},
|
||||||
json={"domain": "speed.guck.tv", "upstream": "127.0.0.1:8080"},
|
json={"domain": "speed.guck.tv", "upstream": "127.0.0.1:8080", "dns_token": "test-dns-token"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json()["status"] == "configured"
|
assert response.json()["status"] == "configured"
|
||||||
assert calls == [
|
assert calls == [
|
||||||
("caddy", "speed.guck.tv", "127.0.0.1:8080"),
|
("caddy", "speed.guck.tv", "127.0.0.1:8080"),
|
||||||
("dns", "guck.tv", "speed"),
|
("dns", "guck.tv", "speed", "test-dns-token"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue