mirror of
https://github.com/Such-Software/adaptive-multi-protocol-gateway.git
synced 2026-12-02 04:43:53 +01:00
feat: reconcile preserve-unmanaged DNS manifests
This commit is contained in:
@@ -0,0 +1,362 @@
|
||||
"""Generic, preserve-unmanaged DNS manifest reconciliation.
|
||||
|
||||
The existing AMPG DNS provider already performs safe Namecheap getHosts and
|
||||
setHosts calls. This module adds a provider-neutral manifest projection for
|
||||
application onboarding without coupling it to a gateway.toml transport plan.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import Any, Iterable
|
||||
|
||||
from .dns import (
|
||||
DEFAULT_TTL,
|
||||
NAMECHEAP_SUPPORTED_RECORD_TYPES,
|
||||
NamecheapDNSProvider,
|
||||
ProviderDNSRecord,
|
||||
)
|
||||
|
||||
|
||||
_HOSTNAME = re.compile(
|
||||
r"(?=^.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+"
|
||||
r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$"
|
||||
)
|
||||
_RELATIVE_NAME = re.compile(
|
||||
r"@|(?:[a-z0-9_*](?:[a-z0-9_*-]{0,61}[a-z0-9_*])?\.)*"
|
||||
r"[a-z0-9_*](?:[a-z0-9_*-]{0,61}[a-z0-9_*])?"
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagedDNSRecord:
|
||||
record: ProviderDNSRecord
|
||||
replace: str
|
||||
match_value_prefix: str | None
|
||||
conflict_types: tuple[str, ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DNSManifest:
|
||||
contract_version: int
|
||||
app_id: str
|
||||
zone: str
|
||||
provider: str
|
||||
preserve_unmanaged: bool
|
||||
protected_names: tuple[str, ...]
|
||||
records: tuple[ManagedDNSRecord, ...]
|
||||
|
||||
|
||||
class DNSManifestError(RuntimeError):
|
||||
"""Safe-to-display manifest or reconciliation failure."""
|
||||
|
||||
|
||||
def _required_string(document: dict[str, Any], name: str) -> str:
|
||||
value = document.get(name)
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise ValueError(f"{name} must be a non-empty string")
|
||||
return value.strip()
|
||||
|
||||
|
||||
def _record_name(value: str) -> str:
|
||||
normalized = value.strip().rstrip(".").lower() or "@"
|
||||
if len(normalized) > 253 or not _RELATIVE_NAME.fullmatch(normalized):
|
||||
raise ValueError(f"invalid relative DNS name: {value}")
|
||||
return normalized
|
||||
|
||||
|
||||
def _record_identity(record: ProviderDNSRecord) -> tuple[Any, ...]:
|
||||
return (
|
||||
_record_name(record.name),
|
||||
record.type.upper(),
|
||||
record.value.strip(),
|
||||
int(record.ttl),
|
||||
record.mx_pref,
|
||||
)
|
||||
|
||||
|
||||
def _record_sort_key(record: ProviderDNSRecord) -> tuple[str, str, str, int]:
|
||||
return (
|
||||
_record_name(record.name),
|
||||
record.type.upper(),
|
||||
record.value.strip(),
|
||||
int(record.mx_pref or 0),
|
||||
)
|
||||
|
||||
|
||||
def _load_record(raw: Any) -> ManagedDNSRecord:
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("managed DNS records must be objects")
|
||||
name = _record_name(_required_string(raw, "name"))
|
||||
record_type = _required_string(raw, "type").upper()
|
||||
if record_type not in NAMECHEAP_SUPPORTED_RECORD_TYPES:
|
||||
raise ValueError(f"unsupported Namecheap record type: {record_type}")
|
||||
value = _required_string(raw, "value")
|
||||
if (
|
||||
"\n" in value
|
||||
or "\r" in value
|
||||
or "{{" in value
|
||||
or "${" in value
|
||||
or (value.startswith("<") and value.endswith(">"))
|
||||
):
|
||||
raise ValueError(f"record {name}/{record_type} has an unresolved value")
|
||||
ttl = raw.get("ttl", DEFAULT_TTL)
|
||||
if not isinstance(ttl, int) or ttl < 60 or ttl > 86400:
|
||||
raise ValueError(f"record {name}/{record_type} has an invalid TTL")
|
||||
mx_pref = raw.get("mx_pref")
|
||||
if record_type == "MX":
|
||||
if not isinstance(mx_pref, int) or not 0 <= mx_pref <= 65535:
|
||||
raise ValueError(f"record {name}/MX requires mx_pref")
|
||||
elif mx_pref is not None:
|
||||
raise ValueError(f"record {name}/{record_type} cannot set mx_pref")
|
||||
|
||||
replace = raw.get("replace", "all")
|
||||
if replace not in {"all", "value_prefix"}:
|
||||
raise ValueError(f"record {name}/{record_type} has invalid replace mode")
|
||||
match_value_prefix = raw.get("match_value_prefix")
|
||||
if replace == "value_prefix":
|
||||
if not isinstance(match_value_prefix, str) or not match_value_prefix:
|
||||
raise ValueError(
|
||||
f"record {name}/{record_type} requires match_value_prefix"
|
||||
)
|
||||
elif match_value_prefix is not None:
|
||||
raise ValueError(
|
||||
f"record {name}/{record_type} cannot set match_value_prefix"
|
||||
)
|
||||
|
||||
raw_conflicts = raw.get("conflict_types", [])
|
||||
if (
|
||||
not isinstance(raw_conflicts, list)
|
||||
or not all(isinstance(item, str) for item in raw_conflicts)
|
||||
):
|
||||
raise ValueError(f"record {name}/{record_type} has invalid conflicts")
|
||||
conflict_types = tuple(item.upper() for item in raw_conflicts)
|
||||
if any(item not in NAMECHEAP_SUPPORTED_RECORD_TYPES for item in conflict_types):
|
||||
raise ValueError(f"record {name}/{record_type} has unsupported conflicts")
|
||||
|
||||
return ManagedDNSRecord(
|
||||
record=ProviderDNSRecord(name, record_type, value, ttl, mx_pref),
|
||||
replace=replace,
|
||||
match_value_prefix=match_value_prefix,
|
||||
conflict_types=conflict_types,
|
||||
)
|
||||
|
||||
|
||||
def load_dns_manifest(path: Path) -> DNSManifest:
|
||||
if path.is_symlink() or not path.is_file():
|
||||
raise ValueError("DNS manifest must be a regular, non-symlink file")
|
||||
try:
|
||||
document = json.loads(path.read_text(encoding="utf-8"))
|
||||
except json.JSONDecodeError as error:
|
||||
raise ValueError("DNS manifest is not valid JSON") from error
|
||||
if not isinstance(document, dict) or document.get("contract_version") != 1:
|
||||
raise ValueError("unsupported DNS manifest contract")
|
||||
app_id = _required_string(document, "app_id")
|
||||
if not re.fullmatch(r"[a-z][a-z0-9_]{1,63}", app_id):
|
||||
raise ValueError("app_id is invalid")
|
||||
zone = _required_string(document, "zone").lower().rstrip(".")
|
||||
if not _HOSTNAME.fullmatch(zone):
|
||||
raise ValueError("zone must be a DNS hostname")
|
||||
provider = _required_string(document, "provider")
|
||||
if provider != "namecheap":
|
||||
raise ValueError("contract version 1 supports only Namecheap")
|
||||
if document.get("preserve_unmanaged") is not True:
|
||||
raise ValueError("preserve_unmanaged must be true")
|
||||
raw_protected = document.get("protected_names", [])
|
||||
if (
|
||||
not isinstance(raw_protected, list)
|
||||
or not all(isinstance(item, str) for item in raw_protected)
|
||||
):
|
||||
raise ValueError("protected_names must be a list")
|
||||
protected_names = tuple(_record_name(item) for item in raw_protected)
|
||||
raw_records = document.get("records")
|
||||
if not isinstance(raw_records, list) or not raw_records:
|
||||
raise ValueError("records must be a non-empty list")
|
||||
if len(raw_records) > 100:
|
||||
raise ValueError("DNS manifest has too many managed records")
|
||||
records = tuple(_load_record(raw) for raw in raw_records)
|
||||
for managed in records:
|
||||
if _record_name(managed.record.name) in protected_names:
|
||||
raise ValueError(
|
||||
f"managed record targets protected name {managed.record.name}"
|
||||
)
|
||||
identities = [_record_identity(managed.record) for managed in records]
|
||||
if len(identities) != len(set(identities)):
|
||||
raise ValueError("DNS manifest contains a duplicate record")
|
||||
return DNSManifest(
|
||||
contract_version=1,
|
||||
app_id=app_id,
|
||||
zone=zone,
|
||||
provider=provider,
|
||||
preserve_unmanaged=True,
|
||||
protected_names=protected_names,
|
||||
records=records,
|
||||
)
|
||||
|
||||
|
||||
def _is_replaced(
|
||||
existing: ProviderDNSRecord, managed: ManagedDNSRecord
|
||||
) -> bool:
|
||||
existing_name = _record_name(existing.name)
|
||||
desired_name = _record_name(managed.record.name)
|
||||
existing_type = existing.type.upper()
|
||||
desired_type = managed.record.type.upper()
|
||||
if existing_name != desired_name:
|
||||
return False
|
||||
if existing_type in managed.conflict_types:
|
||||
return True
|
||||
if existing_type != desired_type:
|
||||
return False
|
||||
if managed.replace == "all":
|
||||
return True
|
||||
assert managed.match_value_prefix is not None
|
||||
return existing.value.lower().startswith(
|
||||
managed.match_value_prefix.lower()
|
||||
)
|
||||
|
||||
|
||||
def merge_dns_manifest_records(
|
||||
existing: Iterable[ProviderDNSRecord],
|
||||
managed_records: Iterable[ManagedDNSRecord],
|
||||
) -> tuple[ProviderDNSRecord, ...]:
|
||||
managed = tuple(managed_records)
|
||||
retained = [
|
||||
record
|
||||
for record in existing
|
||||
if not any(_is_replaced(record, desired) for desired in managed)
|
||||
]
|
||||
desired = [entry.record for entry in managed]
|
||||
return tuple(sorted(retained + desired, key=_record_sort_key))
|
||||
|
||||
|
||||
def _changes(
|
||||
before: Iterable[ProviderDNSRecord],
|
||||
after: Iterable[ProviderDNSRecord],
|
||||
) -> list[dict[str, Any]]:
|
||||
before_set = {_record_identity(record) for record in before}
|
||||
after_set = {_record_identity(record) for record in after}
|
||||
changes: list[dict[str, Any]] = []
|
||||
for identity in sorted(before_set - after_set):
|
||||
name, record_type, value, ttl, mx_pref = identity
|
||||
changes.append(
|
||||
{
|
||||
"action": "remove",
|
||||
"name": name,
|
||||
"type": record_type,
|
||||
"value": value,
|
||||
"ttl": ttl,
|
||||
"mx_pref": mx_pref,
|
||||
}
|
||||
)
|
||||
for identity in sorted(after_set - before_set):
|
||||
name, record_type, value, ttl, mx_pref = identity
|
||||
changes.append(
|
||||
{
|
||||
"action": "add",
|
||||
"name": name,
|
||||
"type": record_type,
|
||||
"value": value,
|
||||
"ttl": ttl,
|
||||
"mx_pref": mx_pref,
|
||||
}
|
||||
)
|
||||
return changes
|
||||
|
||||
|
||||
def offline_plan(manifest: DNSManifest) -> dict[str, Any]:
|
||||
return {
|
||||
"contract_version": 1,
|
||||
"app_id": manifest.app_id,
|
||||
"zone": manifest.zone,
|
||||
"mode": "offline-plan",
|
||||
"preserve_unmanaged": True,
|
||||
"protected_names": list(manifest.protected_names),
|
||||
"managed_records": [
|
||||
{
|
||||
"name": entry.record.name,
|
||||
"type": entry.record.type,
|
||||
"value": entry.record.value,
|
||||
"ttl": entry.record.ttl,
|
||||
"mx_pref": entry.record.mx_pref,
|
||||
"replace": entry.replace,
|
||||
}
|
||||
for entry in manifest.records
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _write_backup(zone: str, raw: str, backup_dir: Path) -> Path:
|
||||
stamp = datetime.datetime.now(datetime.UTC).strftime("%Y%m%d_%H%M%S_%f")
|
||||
path = backup_dir / "namecheap" / f"{zone}.{stamp}.xml"
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
with path.open("x", encoding="utf-8") as backup:
|
||||
backup.write(raw)
|
||||
os.chmod(path, 0o600)
|
||||
return path
|
||||
|
||||
|
||||
def reconcile_dns_manifest(
|
||||
manifest: DNSManifest,
|
||||
provider: Any,
|
||||
*,
|
||||
apply: bool = False,
|
||||
backup_dir: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
snapshot = provider.get_hosts(manifest.zone)
|
||||
if snapshot.status != "ok":
|
||||
raise DNSManifestError(
|
||||
f"provider fetch failed for {manifest.zone}: {snapshot.message}"
|
||||
)
|
||||
next_records = merge_dns_manifest_records(
|
||||
snapshot.records, manifest.records
|
||||
)
|
||||
changes = _changes(snapshot.records, next_records)
|
||||
backup_path = None
|
||||
if apply and changes:
|
||||
if backup_dir is None or not backup_dir.is_absolute():
|
||||
raise DNSManifestError("live apply requires an absolute backup_dir")
|
||||
backup_path = _write_backup(manifest.zone, snapshot.raw, backup_dir)
|
||||
provider.set_hosts(
|
||||
manifest.zone, next_records, mail_policy="preserve"
|
||||
)
|
||||
verified = provider.get_hosts(manifest.zone)
|
||||
if verified.status != "ok":
|
||||
raise DNSManifestError("post-apply provider verification failed")
|
||||
projected = merge_dns_manifest_records(
|
||||
verified.records, manifest.records
|
||||
)
|
||||
if {_record_identity(record) for record in projected} != {
|
||||
_record_identity(record) for record in verified.records
|
||||
}:
|
||||
raise DNSManifestError(
|
||||
"post-apply zone does not match the managed projection"
|
||||
)
|
||||
return {
|
||||
"contract_version": 1,
|
||||
"app_id": manifest.app_id,
|
||||
"zone": manifest.zone,
|
||||
"mode": "apply" if apply else "connected-plan",
|
||||
"status": (
|
||||
"applied"
|
||||
if apply and changes
|
||||
else "planned"
|
||||
if changes
|
||||
else "verified"
|
||||
),
|
||||
"preserve_unmanaged": True,
|
||||
"changes": changes,
|
||||
"backup_path": str(backup_path) if backup_path else None,
|
||||
}
|
||||
|
||||
|
||||
def namecheap_provider(
|
||||
credentials: Path, client_ip: str | None
|
||||
) -> NamecheapDNSProvider:
|
||||
return NamecheapDNSProvider(
|
||||
credentials=credentials, client_ip=client_ip
|
||||
)
|
||||
@@ -0,0 +1,81 @@
|
||||
"""CLI for generic preserve-unmanaged DNS manifest reconciliation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from .dns_manifest import (
|
||||
DNSManifestError,
|
||||
load_dns_manifest,
|
||||
namecheap_provider,
|
||||
offline_plan,
|
||||
reconcile_dns_manifest,
|
||||
)
|
||||
|
||||
|
||||
def _parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(prog="ampg-dns-manifest")
|
||||
parser.add_argument("--manifest", required=True, type=Path)
|
||||
parser.add_argument(
|
||||
"--offline",
|
||||
action="store_true",
|
||||
help="validate and render the manifest without provider access",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--credentials",
|
||||
type=Path,
|
||||
help="Namecheap key-value credential file; required for provider access",
|
||||
)
|
||||
parser.add_argument("--client-ip")
|
||||
parser.add_argument(
|
||||
"--backup-dir",
|
||||
type=Path,
|
||||
help="absolute backup root; required for live apply",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--apply-zone",
|
||||
help="exact zone sentinel required for writes; omit for read-only plan",
|
||||
)
|
||||
return parser
|
||||
|
||||
|
||||
def main(arguments: list[str] | None = None) -> int:
|
||||
options = _parser().parse_args(arguments)
|
||||
try:
|
||||
manifest = load_dns_manifest(options.manifest)
|
||||
if options.offline:
|
||||
if options.apply_zone or options.credentials:
|
||||
raise ValueError(
|
||||
"--offline cannot be combined with credentials or apply"
|
||||
)
|
||||
result = offline_plan(manifest)
|
||||
else:
|
||||
if options.credentials is None:
|
||||
raise ValueError("--credentials is required")
|
||||
apply = options.apply_zone is not None
|
||||
if apply and options.apply_zone != manifest.zone:
|
||||
raise ValueError("--apply-zone must exactly match manifest zone")
|
||||
if apply and options.backup_dir is None:
|
||||
raise ValueError("--backup-dir is required for live apply")
|
||||
provider = namecheap_provider(
|
||||
options.credentials, options.client_ip
|
||||
)
|
||||
result = reconcile_dns_manifest(
|
||||
manifest,
|
||||
provider,
|
||||
apply=apply,
|
||||
backup_dir=options.backup_dir,
|
||||
)
|
||||
json.dump(result, sys.stdout, indent=2, sort_keys=True)
|
||||
sys.stdout.write("\n")
|
||||
return 0
|
||||
except (DNSManifestError, OSError, ValueError) as error:
|
||||
print(f"ampg-dns-manifest: {error}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user