legacy_upgrade_fixture_admission.py (9840B)
1 """Admit closed historical owner output and derive explicit refusal inputs.""" 2 3 from __future__ import annotations 4 5 from contextlib import closing, contextmanager 6 import hashlib 7 import json 8 import re 9 import shutil 10 import sqlite3 11 from pathlib import Path 12 13 14 def sha256(raw: bytes) -> str: 15 return hashlib.sha256(raw).hexdigest() 16 17 18 def load_object(path: Path) -> dict: 19 value = json.loads(path.read_bytes()) 20 if not isinstance(value, dict): 21 raise ValueError("Historical owner metadata is not an object") 22 return value 23 24 25 @contextmanager 26 def closed_owner_reader(path: Path): 27 if not path.is_file() or path.is_symlink(): 28 raise ValueError("Historical owner database is not a regular closed file") 29 wal = path.with_name(path.name + "-wal") 30 if wal.exists() and wal.stat().st_size: 31 raise ValueError("Historical owner database has an uncheckpointed WAL") 32 with closing( 33 sqlite3.connect(path.as_uri() + "?mode=ro&immutable=1", uri=True) 34 ) as connection: 35 yield connection 36 37 38 def read_database(path: Path) -> dict: 39 with closed_owner_reader(path) as connection: 40 if connection.execute("PRAGMA integrity_check").fetchall() != [("ok",)]: 41 raise ValueError("Historical owner database integrity differs") 42 version = connection.execute("PRAGMA user_version").fetchone()[0] 43 identity = connection.execute("PRAGMA application_id").fetchone()[0] 44 catalog = connection.execute( 45 "SELECT type,name,tbl_name,sql FROM sqlite_master " 46 "WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name" 47 ).fetchall() 48 return { 49 "user_version": version, 50 "application_id": identity, 51 "catalog_sha256": sha256(json.dumps(catalog).encode()), 52 } 53 54 55 def actual_revisions(path: Path) -> tuple[list[dict], list[str]]: 56 with closed_owner_reader(path) as connection: 57 rows = connection.execute( 58 "SELECT hex(draft_id),revision,hex(author),hex(operation_id)," 59 "hex(payload_sha256),snapshot FROM radroots_runtime_authored_draft_revisions " 60 "ORDER BY draft_id,revision" 61 ).fetchall() 62 signed = connection.execute( 63 "SELECT signed_raw_json FROM radroots_runtime_authored_artifacts " 64 "WHERE signed_raw_json IS NOT NULL ORDER BY artifact_id" 65 ).fetchall() 66 revisions = [] 67 for draft, revision, author, operation, payload_sha, snapshot in rows: 68 value = json.loads(bytes(snapshot)) 69 payload = bytes(value["payload"]) 70 if sha256(payload) != payload_sha.lower(): 71 raise ValueError("Historical authored payload hash differs") 72 if ( 73 value["revision"] != revision 74 or bytes(value["draft_id"]).hex() != draft.lower() 75 ): 76 raise ValueError("Historical authored revision identity differs") 77 revisions.append( 78 { 79 "draft_id": draft.lower(), 80 "revision": revision, 81 "author_public_key": author.lower(), 82 "operation_id": operation.lower() or None, 83 "payload_schema": value["payload_schema"], 84 "payload_sha256": payload_sha.lower(), 85 "snapshot_sha256": sha256(bytes(snapshot)), 86 } 87 ) 88 return revisions, [bytes(row[0]).decode() for row in signed] 89 90 91 def admit_case_logs(work: Path) -> dict: 92 expected = { 93 "historical-host": "testProduceHistoricalOwnerBytes", 94 "historical-rust": "produce_pre_refactor_owner_state", 95 "historical-transfer": "testProduceHistoricalOwnerBytes", 96 } 97 receipts = {} 98 for name, method in expected.items(): 99 path = work / (name + ".txt") 100 raw = path.read_text() 101 pattern = ( 102 r"^test " + method + r" \.\.\. ok$" 103 if name == "historical-rust" 104 else r"^Test Case .*" + method + r".* passed \(" 105 ) 106 summary = ( 107 "test result: ok. 1 passed; 0 failed; 0 ignored;" 108 if name == "historical-rust" 109 else "Executed 1 test, with 0 failures" 110 ) 111 if len(re.findall(pattern, raw, re.M)) != 1 or summary not in raw: 112 raise ValueError( 113 "Historical producer did not execute the required actual case: " + name 114 ) 115 receipts[name] = {"method": method, "log_sha256": sha256(path.read_bytes())} 116 return receipts 117 118 119 def admit_identities(host: dict, rust: dict, native: dict) -> str: 120 public_key = rust["public_key"] 121 if len(public_key) != 64 or bytes.fromhex(public_key).hex() != public_key: 122 raise ValueError("Historical public identity is not canonical") 123 if ( 124 native["public_key"] != public_key 125 or native["source_generation"] != host["source_generation"] 126 ): 127 raise ValueError("Historical native and Rust owner identities differ") 128 if ( 129 host["bundle_identifier"] != "dev.local.radroots" 130 or host["keychain_service_prefix"] != "org.radroots.field_ios.local" 131 ): 132 raise ValueError("Historical installed identity differs") 133 return public_key 134 135 136 def admit_media(output: Path, host: dict, native: dict) -> None: 137 media = output / host["staged_relative_path"] 138 if not media.resolve().is_relative_to(output.resolve()) or media.is_symlink(): 139 raise ValueError("Historical staged media escaped its fixture root") 140 if ( 141 sha256(media.read_bytes()) != host["media_sha256"] 142 or media.stat().st_size != host["media_bytes"] 143 ): 144 raise ValueError("Historical actual prepared media differs") 145 if ( 146 native["media_sha256"] != host["media_sha256"] 147 or native["state"] != "awaitingVerification" 148 ): 149 raise ValueError("Historical typed native receipt differs") 150 151 152 def admit_databases( 153 output: Path, public_key: str 154 ) -> tuple[dict, list[dict], list[str]]: 155 owner = output / "data" / "radroots" / "users" / public_key 156 runtime = owner / "runtime.sqlite" 157 databases = { 158 "runtime": read_database(runtime), 159 "private": read_database(owner / "private.sqlite"), 160 } 161 if ( 162 databases["runtime"]["user_version"] != 13 163 or databases["runtime"]["application_id"] != 1380209236 164 ): 165 raise ValueError( 166 "Historical runtime schema is not the expected pre-refactor owner" 167 ) 168 if databases["private"]["application_id"] != 1380208722: 169 raise ValueError("Historical private store identity differs") 170 revisions, signed = actual_revisions(runtime) 171 if len(signed) != 1 or json.loads(signed[0])["kind"] != 1: 172 raise ValueError( 173 "Historical output must contain one signed synthetic Update and no authorization event" 174 ) 175 return databases, revisions, signed 176 177 178 def admit_statuses(rust: dict, revisions: list[dict], public_key: str) -> None: 179 heads = {row["draft_id"]: row for row in revisions} 180 statuses = rust["drafts"] 181 if ( 182 len(statuses) != 106 183 or rust["actual_first_page_count"] != 100 184 or not rust["pending_media_outside_first_page"] 185 ): 186 raise ValueError("Historical hidden pending media inventory was not exercised") 187 for status in statuses: 188 head = heads[status["draft_id"]] 189 if (head["revision"], head["author_public_key"], head["operation_id"]) != ( 190 status["revision"], 191 public_key, 192 status["operation_id"], 193 ): 194 raise ValueError( 195 "Historical returned status differs from closed owner bytes" 196 ) 197 if not rust.get("secret_scan") or not rust.get("authorization_scan"): 198 raise ValueError("Historical writer did not complete credential scans") 199 200 201 def admit_output(output: Path, work: Path) -> dict: 202 host = load_object(output / "host-metadata.json") 203 rust = load_object(output / "rust-metadata.json") 204 native = load_object(output / "native-metadata.json") 205 public_key = admit_identities(host, rust, native) 206 admit_media(output, host, native) 207 databases, revisions, signed = admit_databases(output, public_key) 208 admit_statuses(rust, revisions, public_key) 209 return { 210 "state": "HISTORICAL_WRITER_ADMITTED_CURRENT_READERS_PENDING", 211 "actual_case_receipts": admit_case_logs(work), 212 "databases": databases, 213 "revisions": revisions, 214 "draft_count": 106, 215 "signed_event": { 216 "event_id": json.loads(signed[0])["id"], 217 "raw_json": signed[0], 218 "raw_sha256": sha256(signed[0].encode()), 219 }, 220 "limits": "Actual historical synthetic owner bytes; no real account, credential, OS enqueue, device or external delivery.", 221 } 222 223 224 def derive_refusal_inputs(output: Path, public_key: str) -> dict: 225 """Only negative copies change; record both actual parent and derived hashes.""" 226 source = output / "data" / "radroots" / "users" / public_key / "runtime.sqlite" 227 statements = { 228 "unsupported_version": "PRAGMA user_version = 18", 229 "damaged_catalog": "DROP TRIGGER radroots_runtime_authored_draft_revisions_update_guard", 230 } 231 derivatives = {} 232 for name, statement in statements.items(): 233 destination = output / "refusal_inputs" / name / "runtime.sqlite" 234 destination.parent.mkdir(parents=True, exist_ok=False) 235 shutil.copyfile(source, destination) 236 with closing(sqlite3.connect(destination)) as connection: 237 with connection: 238 connection.execute(statement) 239 derivatives[name] = { 240 "source_sha256": sha256(source.read_bytes()), 241 "derived_sha256": sha256(destination.read_bytes()), 242 "only_mutation": statement, 243 "purpose": "Explicit negative startup-refusal input, never a positive historical write.", 244 } 245 return derivatives