"""Atomic, secret-free release state and recovery journal helpers.""" from __future__ import annotations import json import hashlib import os import re import tempfile from pathlib import Path SERVICES = {"backend", "opensearch"} def unique_object(pairs: list[tuple[str, object]]) -> dict: result = {} for key, value in pairs: if key in result: raise ValueError(f"duplicate release state field: {key}") result[key] = value return result def validate_images(value: object, label: str) -> dict[str, dict[str, str]]: if not isinstance(value, dict) or set(value) != SERVICES: raise ValueError(f"{label} must contain backend and opensearch") result = {} for service in sorted(SERVICES): record = value[service] if not isinstance(record, dict) or set(record) != {"configured", "image_id"}: raise ValueError(f"{label} has an invalid {service} record") configured, image_id = record["configured"], record["image_id"] if ( not isinstance(configured, str) or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._:/-]*(?:@sha256:[0-9a-f]{64})?", configured) or not isinstance(image_id, str) or not re.fullmatch(r"sha256:[0-9a-f]{64}", image_id) ): raise ValueError(f"{label} has invalid {service} values") result[service] = {"configured": configured, "image_id": image_id} return result def write_json_atomic(path: Path, value: dict, mode: int = 0o600) -> None: path.parent.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile("w", encoding="utf-8", dir=path.parent, prefix=f".{path.name}.", delete=False) as stream: temporary = Path(stream.name) json.dump(value, stream, sort_keys=True, indent=2) stream.write("\n") stream.flush() os.fsync(stream.fileno()) try: os.chmod(temporary, mode) os.replace(temporary, path) directory_fd = os.open(path.parent, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0)) try: os.fsync(directory_fd) finally: os.close(directory_fd) finally: temporary.unlink(missing_ok=True) def read_json(path: Path) -> dict: if path.is_symlink() or not path.is_file(): raise ValueError(f"release state is not a regular file: {path}") try: value = json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=unique_object) except (OSError, UnicodeError, json.JSONDecodeError) as error: raise ValueError(f"release state is unreadable: {error}") from error if not isinstance(value, dict): raise ValueError("release state must be a JSON object") return value def validate_state(state: object) -> dict: old_fields = {"schema_version", "commit", "manifest_sha256", "current", "previous"} new_fields = old_fields | {"previous_backup"} if (not isinstance(state, dict) or type(state.get("schema_version")) is not int or state.get("schema_version") not in {1, 2} or set(state) != (old_fields if state.get("schema_version") == 1 else new_fields)): raise ValueError("release state has unsupported schema") if not isinstance(state["commit"], str) or not re.fullmatch(r"[0-9a-f]{40}|rollback", state["commit"]): raise ValueError("release state commit is invalid") if not isinstance(state["manifest_sha256"], str) or not re.fullmatch(r"[0-9a-f]{64}|rollback", state["manifest_sha256"]): raise ValueError("release state provenance is invalid") state["current"] = validate_images(state["current"], "current images") state["previous"] = validate_images(state["previous"], "previous images") if state["schema_version"] == 2: state["previous_backup"] = validate_backup_identity(state["previous_backup"]) return state def validate_backup_identity(value: object) -> dict | None: if value is None: return None required = {"backup_id", "size", "archive_sha256", "manifest_sha256", "manifest"} if not isinstance(value, dict) or set(value) != required: raise ValueError("external backup identity has an unsupported shape") if (not isinstance(value["backup_id"], str) or not re.fullmatch(r"akyldash-[0-9]{8}T[0-9]{6}Z-[0-9a-f]{8}", value["backup_id"]) or type(value["size"]) is not int or not 0 < value["size"] <= 100 * 1024**3 or not isinstance(value["archive_sha256"], str) or not re.fullmatch(r"[0-9a-f]{64}", value["archive_sha256"]) or not isinstance(value["manifest_sha256"], str) or not re.fullmatch(r"[0-9a-f]{64}", value["manifest_sha256"]) or not isinstance(value["manifest"], dict)): raise ValueError("external backup identity has invalid values") canonical = json.dumps(value["manifest"], sort_keys=True, separators=(",", ":")).encode("utf-8") if hashlib.sha256(canonical).hexdigest() != value["manifest_sha256"]: raise ValueError("external backup manifest identity is inconsistent") return value