"""Atomic, secret-free release state and recovery journal helpers.""" from __future__ import annotations import json import os import re import tempfile from pathlib import Path SERVICES = {"backend", "opensearch"} 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")) 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: if not isinstance(state, dict) or set(state) != { "schema_version", "commit", "manifest_sha256", "current", "previous" } or state.get("schema_version") != 1: 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") return state