| 1 | #!/usr/bin/env python3 |
| 2 | """Vet a local native artifact through CARLOS' real local-store boot check. |
| 3 | |
| 4 | Never uploads or contacts CARLOS. The synthetic fixture grants no live access. |
| 5 | Run on Linux/arm64 with --target linux-arm64 to vet the deployment artifact. |
| 6 | """ |
| 7 | import argparse |
| 8 | import base64 |
| 9 | import hashlib |
| 10 | import http.client |
| 11 | import json |
| 12 | import os |
| 13 | import pathlib |
| 14 | import platform |
| 15 | import shutil |
| 16 | import socket |
| 17 | import time |
| 18 | import subprocess |
| 19 | import tempfile |
| 20 | |
| 21 | parser = argparse.ArgumentParser() |
| 22 | parser.add_argument("bundle", type=pathlib.Path) |
| 23 | parser.add_argument("--carlos", required=True) |
| 24 | parser.add_argument("--target", choices=("native", "linux-arm64"), default="native") |
| 25 | args = parser.parse_args() |
| 26 | args.bundle = args.bundle.resolve(strict=True) |
| 27 | carlos = pathlib.Path(args.carlos).resolve(strict=True) |
| 28 | if not carlos.is_file() or not os.access(carlos, os.X_OK): |
| 29 | parser.error("--carlos must name an executable file") |
| 30 | if args.target == "linux-arm64" and (platform.system() != "Linux" or platform.machine() not in ("aarch64", "arm64")): |
| 31 | parser.error("linux-arm64 boot vet requires Linux/arm64") |
| 32 | version = (args.bundle / "VERSION").read_text().strip() |
| 33 | if len(version) != 40 or any(c not in "0123456789abcdef" for c in version): |
| 34 | parser.error("VERSION must be a full git SHA") |
| 35 | expected_names = {"oficina-connect-linux-arm64", "oficina-linux-arm64", "oficina-connect-native", "oficina-native"} |
| 36 | recorded = {} |
| 37 | for line in (args.bundle / "SHA256SUMS").read_text().splitlines(): |
| 38 | digest, name = line.split(" ") |
| 39 | if name not in expected_names or name in recorded or len(digest) != 64: |
| 40 | parser.error("invalid SHA256SUMS") |
| 41 | recorded[name] = digest |
| 42 | if set(recorded) != expected_names: |
| 43 | parser.error("incomplete SHA256SUMS") |
| 44 | for name, digest in recorded.items(): |
| 45 | if hashlib.sha256((args.bundle / name).read_bytes()).hexdigest() != digest: |
| 46 | parser.error("artifact differs from recorded SHA256SUMS: " + name) |
| 47 | artifact = args.bundle / ("oficina-connect-" + args.target) |
| 48 | raw = artifact.read_bytes() |
| 49 | config = (args.bundle / "synthetic-config.json").read_bytes() |
| 50 | with tempfile.TemporaryDirectory(prefix="oficina-local-vet-") as scratch: |
| 51 | work = pathlib.Path(scratch) |
| 52 | release = work / "store" / "apps" / "bds" / "oficina-connect" / "releases" / version |
| 53 | release.mkdir(parents=True) |
| 54 | shutil.copyfile(artifact, release / "oficina-connect") |
| 55 | target = "linux-arm64" if args.target == "linux-arm64" else platform.system().lower() + "-" + {"arm64": "arm64", "aarch64": "arm64", "x86_64": "amd64"}[platform.machine()] |
| 56 | manifest = {"schema": 1, "app": "oficina-connect", "version": version, "kind": "binary", "artifacts": {target: {"file": "oficina-connect", "sha256": hashlib.sha256(raw).hexdigest(), "size": len(raw)}}} |
| 57 | (release / "manifest.json").write_text(json.dumps(manifest)) |
| 58 | state = work / "state" |
| 59 | state.mkdir() |
| 60 | # No production deployment variables, credentials, or inherited activation FDs. |
| 61 | env = {k: v for k, v in os.environ.items() if not k.startswith(("CARLOS_", "OFICINA_", "AWS_", "LISTEN_")) and k != "STATE_DIRECTORY"} |
| 62 | env.update(CARLOS_DEPLOYMENT_DIR=str(work / "store"), CARLOS_CREDENTIALS=str(work / "no-credentials"), |
| 63 | STATE_DIRECTORY=str(state), OFICINA_CONNECT_CONFIG_BASE64=base64.b64encode(config).decode(), |
| 64 | OFICINA_CONNECT_DEK=base64.b64encode(bytes([5]) * 32).decode(), |
| 65 | OFICINA_DIRECTORY_TOKEN="synthetic-directory-secret-must-not-appear") |
| 66 | subprocess.run([str(carlos), "vet", "--account", "bds", "--app", "oficina-connect", "--version", version, "--boot"], cwd=work, env=env, check=True) |
| 67 | if not (state / "oficina-connect.db").is_file(): |
| 68 | raise SystemExit("boot vet did not create persistent state; a skipped boot is not success") |
| 69 | |
| 70 | # CARLOS boot vet checks health only; independently prove the exact release |
| 71 | # stamp from these verified bytes, not just from the manifest we supplied. |
| 72 | sock = work / "version.sock" |
| 73 | class UnixHTTP(http.client.HTTPConnection): |
| 74 | def connect(self): |
| 75 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 76 | self.sock.settimeout(1) |
| 77 | self.sock.connect(str(sock)) |
| 78 | |
| 79 | with tempfile.TemporaryFile() as diagnostics: |
| 80 | process = subprocess.Popen([str(artifact), "serve", "-socket", str(sock)], cwd=work, env=env, |
| 81 | stdout=diagnostics, stderr=diagnostics) |
| 82 | try: |
| 83 | deadline = time.monotonic() + 10 |
| 84 | while True: |
| 85 | request = UnixHTTP("probe.invalid", timeout=1) |
| 86 | try: |
| 87 | request.request("GET", "/api/version") |
| 88 | response = request.getresponse() |
| 89 | body = response.read(256) |
| 90 | if response.status != 200 or body != version.encode(): |
| 91 | raise SystemExit("artifact /api/version does not exactly match VERSION") |
| 92 | break |
| 93 | except (OSError, http.client.HTTPException): |
| 94 | if process.poll() is not None or time.monotonic() >= deadline: |
| 95 | raise SystemExit("artifact version probe failed") |
| 96 | time.sleep(0.05) |
| 97 | finally: |
| 98 | request.close() |
| 99 | process.terminate() |
| 100 | if process.wait(timeout=15) != 0: |
| 101 | raise SystemExit("artifact failed graceful SIGTERM shutdown") |
| 102 | finally: |
| 103 | if process.poll() is None: |
| 104 | process.kill() |
| 105 | process.wait() |
| 106 | print("Artifact checksum, exact version and graceful shutdown verified: " + version) |
| 107 | |