File size: 5,822 Bytes
4620c5a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #!/usr/bin/env python3
"""Atomically preserve owner-only private evidence for CTX A/B failures."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import stat
import tempfile
import traceback
from typing import Any
FAILURE_GUARD = "ctx-ab-private-failure-v1"
FAILURE_MANIFEST = "artifact-manifest.json"
class FailureEvidenceError(RuntimeError):
"""Private failure evidence could not be published safely."""
def _canonical_bytes(value: Any) -> bytes:
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()
def _sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def _private_write(path: Path, data: bytes) -> None:
descriptor = os.open(
path,
os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0),
0o600,
)
with os.fdopen(descriptor, "wb") as handle:
handle.write(data)
def _exception_chain(exc: BaseException) -> list[dict[str, str]]:
chain: list[dict[str, str]] = []
current: BaseException | None = exc
seen: set[int] = set()
while current is not None and id(current) not in seen:
seen.add(id(current))
chain.append({"message": str(current), "type": type(current).__name__})
current = current.__cause__ or (
None if current.__suppress_context__ else current.__context__
)
return chain
def _harden_private_tree(root: Path) -> None:
os.chmod(root, 0o700)
for path in root.rglob("*"):
metadata = path.lstat()
if stat.S_ISLNK(metadata.st_mode):
continue
if stat.S_ISDIR(metadata.st_mode):
os.chmod(path, 0o700)
elif stat.S_ISREG(metadata.st_mode):
os.chmod(path, 0o700 if metadata.st_mode & 0o111 else 0o600)
else:
raise FailureEvidenceError("private failure evidence contains a special file")
def _artifact_manifest(root: Path) -> dict[str, Any]:
entries: list[dict[str, Any]] = []
for path in sorted(root.rglob("*"), key=lambda item: item.relative_to(root).as_posix()):
relative = path.relative_to(root).as_posix()
if relative == FAILURE_MANIFEST:
continue
metadata = path.lstat()
record: dict[str, Any] = {
"mode": stat.S_IMODE(metadata.st_mode),
"path": relative,
}
if stat.S_ISDIR(metadata.st_mode):
record["type"] = "directory"
elif stat.S_ISREG(metadata.st_mode):
data = path.read_bytes()
record.update(
{
"bytes": len(data),
"sha256": _sha256(data),
"type": "file",
}
)
elif stat.S_ISLNK(metadata.st_mode):
record.update(
{
"sha256": _sha256(os.fsencode(os.readlink(path))),
"type": "symlink",
}
)
else:
raise FailureEvidenceError("private failure evidence contains a special file")
entries.append(record)
return {
"entries": entries,
"entry_count": len(entries),
"guard": FAILURE_GUARD,
"manifest_sha256": _sha256(_canonical_bytes(entries)),
"schema_version": 1,
}
def validate_destination(destination: Path, *, repository_root: Path) -> None:
if not destination.is_absolute():
raise FailureEvidenceError("failure evidence destination must be absolute")
resolved = destination.resolve(strict=False)
private_root = (repository_root / ".gate" / "ctx-ab-private").resolve()
if repository_root.resolve() in resolved.parents and private_root not in resolved.parents:
raise FailureEvidenceError(
"failure evidence inside the repository must use the private root"
)
destination.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
if destination.exists() or destination.is_symlink():
raise FailureEvidenceError("failure evidence destination already exists")
def publish_failure(
*,
destination: Path,
operation: str,
exc: BaseException,
repository_root: Path,
staging: Path | None = None,
) -> dict[str, Any]:
"""Publish a private immutable exception bundle, optionally with staged raw evidence."""
validate_destination(destination, repository_root=repository_root)
owns_staging = staging is None
if staging is None:
staging = Path(tempfile.mkdtemp(prefix=".ctx-ab-failure-", dir=destination.parent))
os.chmod(staging, 0o700)
elif staging.is_symlink() or not staging.is_dir():
raise FailureEvidenceError("failure evidence staging root is unsafe")
failure = {
"exception_chain": _exception_chain(exc),
"guard": FAILURE_GUARD,
"operation": operation,
"schema_version": 1,
"traceback": "".join(traceback.format_exception(exc)),
}
try:
_harden_private_tree(staging)
_private_write(staging / "failure.json", _canonical_bytes(failure))
manifest = _artifact_manifest(staging)
_private_write(staging / FAILURE_MANIFEST, _canonical_bytes(manifest))
staging.rename(destination)
except BaseException:
# Never delete a partially captured failure. A hidden owner-only staging
# directory is safer than destroying the only root-cause evidence.
if owns_staging and staging.exists():
os.chmod(staging, 0o700)
raise
return manifest
def already_preserved(destination: Path) -> bool:
return (
destination.is_dir()
and not destination.is_symlink()
and (destination / "failure.json").is_file()
and (destination / FAILURE_MANIFEST).is_file()
)
|