Coverage for src/copytrading_engine/backup/restore/candidates.py: 72%
369 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""Restore staging and candidate generations: intents, manifests, and checks before activation."""
3from __future__ import annotations
5import hashlib
6import json
7import os
8import shutil
9import stat
10import uuid
11from pathlib import Path, PurePosixPath
13from copytrading_engine.backup.archive import open_archive, parse_manifest
14from copytrading_engine.backup.configuration import (
15 read_configuration_identities,
16 read_installation_id,
17 validate_activation_json,
18)
19from copytrading_engine.backup.databases import (
20 account_environment,
21 require_manual_disabled_account,
22 validate_sqlite,
23)
24from copytrading_engine.backup.files import (
25 copy_private_bytes,
26 fsync_directory,
27 require_directory,
28 require_regular_file,
29 sha256_file,
30 tree_paths,
31)
32from copytrading_engine.backup.manifest import (
33 ACCOUNT_DATABASE,
34 CANDIDATE_MANIFEST_NAME,
35 DIGEST,
36 MAX_MEMBER_BYTES,
37 RESTORE_STAGE,
38 BackupManifest,
39 BackupManifestError,
40 is_sqlite_member,
41 json_object,
42 manifest_dict,
43 manifest_digest,
44)
45from copytrading_engine.backup.ports import AccountSnapshotSchemaProvider, SchemaCatalogProvider
46from copytrading_engine.backup.restore.gate import (
47 restore_manual_disabled,
48 restore_manual_disabled_path,
49)
51_MAX_CANDIDATE_MANIFEST_BYTES = 16 * 1024 * 1024
54_CANDIDATE_INTENT_NAME = ".restore-candidate-intent"
57def stage_restore(
58 archive_path: Path,
59 staging_root: Path,
60 manifest: BackupManifest,
61 schema_catalog: SchemaCatalogProvider,
62 snapshot_schema: AccountSnapshotSchemaProvider,
63) -> Path:
64 require_directory(staging_root, "restore staging directory is unavailable")
65 destination = staging_root / f"restore-{uuid.uuid4().hex}"
66 destination.mkdir(mode=0o700)
67 try:
68 with open_archive(archive_path) as archive:
69 configured_environments: dict[str, str] = {}
70 ledger_environments: dict[str, str] = {}
71 for member in manifest.members:
72 target = destination.joinpath(*PurePosixPath(member.path).parts)
73 target.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
74 entry = archive.getinfo(member.path)
75 digest = hashlib.sha256()
76 size = 0
77 descriptor = os.open(
78 target,
79 os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0),
80 0o600,
81 )
82 with archive.open(entry, "r") as source, os.fdopen(descriptor, "wb") as output:
83 while chunk := source.read(1024 * 1024):
84 size += len(chunk)
85 if size > member.size: 85 ↛ 86line 85 didn't jump to line 86 because the condition on line 85 was never true
86 raise BackupManifestError("restore member exceeds its declared size")
87 digest.update(chunk)
88 output.write(chunk)
89 output.flush()
90 os.fsync(output.fileno())
91 if size != member.size or digest.hexdigest() != member.sha256: 91 ↛ 92line 91 didn't jump to line 92 because the condition on line 91 was never true
92 raise BackupManifestError("restore member changed after validation")
93 os.chmod(target, 0o600)
94 if is_sqlite_member(member.path):
95 validate_sqlite(
96 target,
97 member.path,
98 manifest.installation_id,
99 schema_catalog,
100 snapshot_schema,
101 )
102 if account_match := ACCOUNT_DATABASE.fullmatch(member.path):
103 ledger_environments[account_match.group(1)] = account_environment(target)
104 elif member.path == "trading-configuration.json":
105 identities = read_configuration_identities(target.read_bytes())
106 configured_environments = {
107 identity.split(":", 1)[1]: identity.split(":", 1)[0]
108 for identity in identities
109 }
110 elif member.path.startswith("attachments/"): 110 ↛ 71line 110 didn't jump to line 71 because the condition on line 110 was always true
111 expected_digest = PurePosixPath(member.path).name
112 if sha256_file(target) != expected_digest: 112 ↛ 113line 112 didn't jump to line 113 because the condition on line 112 was never true
113 raise BackupManifestError("restore attachment hash does not match its path")
114 if any( 114 ↛ 119line 114 didn't jump to line 119 because the condition on line 114 was never true
115 account_id in configured_environments
116 and configured_environments[account_id] != environment
117 for account_id, environment in ledger_environments.items()
118 ):
119 raise BackupManifestError(
120 "restored account environment conflicts with configuration"
121 )
122 environment_ids = {
123 *(
124 f"{environment}:{account_id}"
125 for account_id, environment in ledger_environments.items()
126 ),
127 *(
128 f"{environment}:{account_id}"
129 for account_id, environment in configured_environments.items()
130 ),
131 }
132 if tuple(sorted(environment_ids)) != manifest.environment_ids: 132 ↛ 133line 132 didn't jump to line 133 because the condition on line 132 was never true
133 raise BackupManifestError("restore account identities do not match the manifest")
134 return destination
135 except BaseException:
136 shutil.rmtree(destination, ignore_errors=True)
137 raise
140def clear_restore_staging(staging_root: Path) -> None:
141 require_directory(staging_root, "restore staging directory is unavailable")
142 for entry in staging_root.iterdir():
143 if not RESTORE_STAGE.fullmatch(entry.name): 143 ↛ 144line 143 didn't jump to line 144 because the condition on line 143 was never true
144 raise BackupManifestError("restore staging contains an unexpected entry")
145 try:
146 info = entry.lstat()
147 except OSError as exc:
148 raise BackupManifestError("restore staging entry is unavailable") from exc
149 if not stat.S_ISDIR(info.st_mode) or stat.S_ISLNK(info.st_mode): 149 ↛ 150line 149 didn't jump to line 150 because the condition on line 149 was never true
150 raise BackupManifestError("restore staging entry is unsafe")
151 shutil.rmtree(entry)
154def candidate_intent_payload(
155 manifest: BackupManifest,
156 previous_generation: str,
157 candidate_generation: str,
158 candidate_manifest_sha256: str,
159) -> bytes:
160 return json.dumps(
161 {
162 "version": 1,
163 "installation_id": manifest.installation_id,
164 "previous_generation": previous_generation,
165 "candidate_generation": candidate_generation,
166 "manifest_sha256": manifest_digest(manifest),
167 "candidate_manifest_sha256": candidate_manifest_sha256,
168 },
169 sort_keys=True,
170 separators=(",", ":"),
171 ).encode("utf-8")
174def persist_candidate_intent(owner: Path, payload: bytes) -> None:
175 path = owner / _CANDIDATE_INTENT_NAME
176 try:
177 path.lstat()
178 except FileNotFoundError:
179 pass
180 else:
181 raise BackupManifestError("another restore candidate intent is already present")
182 temporary = owner / f".{_CANDIDATE_INTENT_NAME}.{uuid.uuid4().hex}.tmp"
183 directory_flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
184 directory_descriptor = os.open(owner, directory_flags)
185 file_descriptor = -1
186 try:
187 file_descriptor = os.open(
188 temporary,
189 os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0),
190 0o600,
191 )
192 remaining = memoryview(payload)
193 while remaining:
194 written = os.write(file_descriptor, remaining)
195 if written <= 0: 195 ↛ 196line 195 didn't jump to line 196 because the condition on line 195 was never true
196 raise OSError("restore candidate intent could not be written")
197 remaining = remaining[written:]
198 os.fsync(file_descriptor)
199 os.close(file_descriptor)
200 file_descriptor = -1
201 os.replace(temporary, path)
202 os.fsync(directory_descriptor)
203 finally:
204 if file_descriptor >= 0: 204 ↛ 205line 204 didn't jump to line 205 because the condition on line 204 was never true
205 os.close(file_descriptor)
206 try:
207 temporary.unlink()
208 except FileNotFoundError:
209 pass
210 os.close(directory_descriptor)
213def read_candidate_intent(owner: Path) -> dict[str, str | int] | None:
214 path = owner / _CANDIDATE_INTENT_NAME
215 try:
216 path.lstat()
217 except FileNotFoundError:
218 return None
219 require_regular_file(path, "restore candidate intent is unsafe")
220 if path.stat().st_size > 4096: 220 ↛ 221line 220 didn't jump to line 221 because the condition on line 220 was never true
221 raise BackupManifestError("restore candidate intent is oversized")
222 intent = json_object(path.read_bytes())
223 expected_fields = {
224 "version",
225 "installation_id",
226 "previous_generation",
227 "candidate_generation",
228 "manifest_sha256",
229 "candidate_manifest_sha256",
230 }
231 if ( 231 ↛ 236line 231 didn't jump to line 236 because the condition on line 231 was never true
232 set(intent) != expected_fields
233 or type(intent.get("version")) is not int
234 or intent["version"] != 1
235 ):
236 raise BackupManifestError("restore candidate intent is invalid")
237 for key in ("installation_id", "previous_generation", "candidate_generation"):
238 value = intent[key]
239 try:
240 canonical = str(uuid.UUID(value))
241 except (ValueError, TypeError, AttributeError) as exc:
242 raise BackupManifestError("restore candidate intent identity is invalid") from exc
243 if value != canonical: 243 ↛ 244line 243 didn't jump to line 244 because the condition on line 243 was never true
244 raise BackupManifestError("restore candidate intent identity is invalid")
245 if intent["previous_generation"] == intent["candidate_generation"]: 245 ↛ 246line 245 didn't jump to line 246 because the condition on line 245 was never true
246 raise BackupManifestError("restore candidate intent generations are invalid")
247 for key in ("manifest_sha256", "candidate_manifest_sha256"):
248 if not isinstance(intent[key], str) or not DIGEST.fullmatch(intent[key]): 248 ↛ 249line 248 didn't jump to line 249 because the condition on line 248 was never true
249 raise BackupManifestError("restore candidate intent digest is invalid")
250 return intent
253def remove_candidate_intent(owner: Path, candidate_generation: str | None = None) -> None:
254 path = owner / _CANDIDATE_INTENT_NAME
255 intent = read_candidate_intent(owner)
256 if intent is None:
257 return
258 if candidate_generation is not None and intent["candidate_generation"] != candidate_generation: 258 ↛ 259line 258 didn't jump to line 259 because the condition on line 258 was never true
259 raise BackupManifestError("restore candidate intent changed during cleanup")
260 path.unlink()
261 fsync_directory(owner)
264def remove_generation_entry(path: Path) -> bool:
265 try:
266 info = path.lstat()
267 except FileNotFoundError:
268 return False
269 except OSError as exc:
270 raise BackupManifestError("orphan restore generation is unavailable") from exc
271 try:
272 if stat.S_ISLNK(info.st_mode) or stat.S_ISREG(info.st_mode):
273 path.unlink()
274 elif stat.S_ISDIR(info.st_mode): 274 ↛ 277line 274 didn't jump to line 277 because the condition on line 274 was always true
275 shutil.rmtree(path)
276 else:
277 raise BackupManifestError("orphan restore generation has an unsafe type")
278 except FileNotFoundError:
279 return False
280 except OSError as exc:
281 raise BackupManifestError("orphan restore generation could not be removed") from exc
282 return True
285def reap_interrupted_restore_candidates(
286 owner: Path,
287 generations: Path,
288 active_generation: str,
289) -> None:
290 """Reap only intent-bound unpublished candidates and private temporary entries."""
291 intent = read_candidate_intent(owner)
292 if intent is not None:
293 if intent["installation_id"] != read_installation_id(owner): 293 ↛ 294line 293 didn't jump to line 294 because the condition on line 293 was never true
294 raise BackupManifestError("restore candidate intent belongs to another installation")
295 candidate_generation = str(intent["candidate_generation"])
296 previous_generation = str(intent["previous_generation"])
297 marker_path = restore_manual_disabled_path(owner)
298 if restore_manual_disabled(marker_path): 298 ↛ 299line 298 didn't jump to line 299 because the condition on line 298 was never true
299 require_regular_file(marker_path, "restore activation gate is unsafe")
300 marker = json_object(marker_path.read_bytes())
301 if (
302 marker.get("candidate_generation") != candidate_generation
303 or marker.get("previous_generation") != previous_generation
304 or marker.get("installation_id") != intent["installation_id"]
305 or marker.get("manifest_sha256") != intent["manifest_sha256"]
306 or marker.get("candidate_manifest_sha256") != intent["candidate_manifest_sha256"]
307 ):
308 raise BackupManifestError("restore gate does not match candidate intent")
309 elif active_generation == candidate_generation: 309 ↛ 310line 309 didn't jump to line 310 because the condition on line 309 was never true
310 remove_candidate_intent(owner, candidate_generation)
311 elif active_generation == previous_generation: 311 ↛ 317line 311 didn't jump to line 317 because the condition on line 311 was always true
312 candidate = generations / candidate_generation
313 if remove_generation_entry(candidate): 313 ↛ 315line 313 didn't jump to line 315 because the condition on line 313 was always true
314 fsync_directory(generations)
315 remove_candidate_intent(owner, candidate_generation)
316 else:
317 raise BackupManifestError("active generation does not match candidate intent")
319 changed = False
320 for entry in generations.iterdir():
321 if not entry.name.startswith(".restore-candidate-"):
322 continue
323 if remove_generation_entry(entry): 323 ↛ 320line 323 didn't jump to line 320 because the condition on line 323 was always true
324 changed = True
325 if changed:
326 fsync_directory(generations)
329def read_active_generation(owner: Path) -> str:
330 pointer = owner / "active-generation"
331 require_regular_file(pointer, "active operational generation is unavailable")
332 value = pointer.read_text(encoding="ascii").strip()
333 try:
334 canonical = str(uuid.UUID(value))
335 except (ValueError, TypeError, AttributeError) as exc:
336 raise BackupManifestError("active operational generation identity is invalid") from exc
337 if value != canonical: 337 ↛ 338line 337 didn't jump to line 338 because the condition on line 337 was never true
338 raise BackupManifestError("active operational generation identity is invalid")
339 generation = owner / "generations" / canonical
340 require_directory(generation, "active operational generation is unavailable")
341 return canonical
344def write_candidate_manifest(candidate: Path, manifest: BackupManifest) -> str:
345 candidate_members = []
346 for member in manifest.members:
347 path = candidate.joinpath(*PurePosixPath(member.path).parts)
348 require_regular_file(path, "restore candidate member is unavailable")
349 candidate_members.append(
350 {
351 "path": member.path,
352 "size": path.stat().st_size,
353 "sha256": sha256_file(path),
354 }
355 )
356 record = {
357 "version": 1,
358 "manifest": manifest_dict(manifest),
359 "manifest_sha256": manifest_digest(manifest),
360 "candidate_members": candidate_members,
361 }
362 payload = json.dumps(record, sort_keys=True, separators=(",", ":")).encode("utf-8")
363 if len(payload) > _MAX_CANDIDATE_MANIFEST_BYTES: 363 ↛ 364line 363 didn't jump to line 364 because the condition on line 363 was never true
364 raise BackupManifestError("restore candidate manifest exceeds the supported size")
365 copy_private_bytes(candidate / CANDIDATE_MANIFEST_NAME, payload)
366 return hashlib.sha256(payload).hexdigest()
369def _read_candidate_manifest(
370 candidate: Path,
371) -> tuple[BackupManifest, dict[str, tuple[int, str]], bytes]:
372 manifest_path = candidate / CANDIDATE_MANIFEST_NAME
373 require_regular_file(manifest_path, "restore candidate manifest is unavailable")
374 info = manifest_path.stat()
375 if info.st_size > _MAX_CANDIDATE_MANIFEST_BYTES: 375 ↛ 376line 375 didn't jump to line 376 because the condition on line 375 was never true
376 raise BackupManifestError("restore candidate manifest exceeds the supported size")
377 payload = manifest_path.read_bytes()
378 raw = json_object(payload)
379 if set(raw) != {"version", "manifest", "manifest_sha256", "candidate_members"}: 379 ↛ 380line 379 didn't jump to line 380 because the condition on line 379 was never true
380 raise BackupManifestError("restore candidate manifest is invalid")
381 if type(raw["version"]) is not int or raw["version"] != 1: 381 ↛ 382line 381 didn't jump to line 382 because the condition on line 381 was never true
382 raise BackupManifestError("restore candidate manifest version is unsupported")
383 manifest_value = raw["manifest"]
384 if not isinstance(manifest_value, dict): 384 ↛ 385line 384 didn't jump to line 385 because the condition on line 384 was never true
385 raise BackupManifestError("restore candidate manifest is invalid")
386 try:
387 manifest_content = json.dumps(manifest_value, sort_keys=True, separators=(",", ":")).encode(
388 "utf-8"
389 )
390 manifest = parse_manifest(manifest_content)
391 except (TypeError, UnicodeEncodeError) as exc:
392 raise BackupManifestError("restore candidate manifest is invalid") from exc
393 if manifest_dict(manifest) != manifest_value: 393 ↛ 394line 393 didn't jump to line 394 because the condition on line 393 was never true
394 raise BackupManifestError("restore candidate manifest is not canonical")
395 manifest_sha256 = raw["manifest_sha256"]
396 if not isinstance(manifest_sha256, str) or not DIGEST.fullmatch(manifest_sha256): 396 ↛ 397line 396 didn't jump to line 397 because the condition on line 396 was never true
397 raise BackupManifestError("restore candidate manifest digest is invalid")
398 if manifest_sha256 != manifest_digest(manifest): 398 ↛ 399line 398 didn't jump to line 399 because the condition on line 398 was never true
399 raise BackupManifestError("restore candidate manifest digest does not match")
401 candidate_members = raw["candidate_members"]
402 if not isinstance(candidate_members, list) or len(candidate_members) != len(manifest.members): 402 ↛ 403line 402 didn't jump to line 403 because the condition on line 402 was never true
403 raise BackupManifestError("restore candidate member hashes are invalid")
404 candidate_hashes: dict[str, tuple[int, str]] = {}
405 for entry, member in zip(candidate_members, manifest.members, strict=True):
406 if not isinstance(entry, dict) or set(entry) != {"path", "size", "sha256"}: 406 ↛ 407line 406 didn't jump to line 407 because the condition on line 406 was never true
407 raise BackupManifestError("restore candidate member hash is invalid")
408 member_path = entry["path"]
409 size = entry["size"]
410 digest = entry["sha256"]
411 if ( 411 ↛ 419line 411 didn't jump to line 419 because the condition on line 411 was never true
412 member_path != member.path
413 or type(size) is not int
414 or size < 0
415 or size > MAX_MEMBER_BYTES
416 or not isinstance(digest, str)
417 or not DIGEST.fullmatch(digest)
418 ):
419 raise BackupManifestError("restore candidate member hash is invalid")
420 candidate_hashes[member.path] = (size, digest)
421 return manifest, candidate_hashes, payload
424def validate_published_restore_candidate(
425 owner: Path,
426 candidate_identifier: str,
427 schema_catalog: SchemaCatalogProvider,
428 snapshot_schema: AccountSnapshotSchemaProvider,
429) -> BackupManifest:
430 try:
431 canonical_candidate = str(uuid.UUID(candidate_identifier))
432 except (ValueError, TypeError, AttributeError) as exc:
433 raise BackupManifestError("restore candidate identifier is invalid") from exc
434 if candidate_identifier != canonical_candidate: 434 ↛ 435line 434 didn't jump to line 435 because the condition on line 434 was never true
435 raise BackupManifestError("restore candidate identifier is invalid")
436 require_directory(owner, "restore owner directory is unavailable")
437 marker_path = restore_manual_disabled_path(owner)
438 require_regular_file(marker_path, "restore activation gate is unavailable")
439 marker = json_object(marker_path.read_bytes())
440 marker_fields = {
441 "version",
442 "installation_id",
443 "previous_generation",
444 "candidate_generation",
445 "backup_created_at",
446 "manifest_sha256",
447 "candidate_manifest_sha256",
448 }
449 if ( 449 ↛ 454line 449 didn't jump to line 454 because the condition on line 449 was never true
450 set(marker) != marker_fields
451 or type(marker.get("version")) is not int
452 or marker["version"] != 2
453 ):
454 raise BackupManifestError("restore activation gate is invalid")
455 for key in ("installation_id", "previous_generation", "candidate_generation"):
456 value = marker[key]
457 try:
458 canonical = str(uuid.UUID(value))
459 except (ValueError, TypeError, AttributeError) as exc:
460 raise BackupManifestError("restore activation identity is invalid") from exc
461 if value != canonical: 461 ↛ 462line 461 didn't jump to line 462 because the condition on line 461 was never true
462 raise BackupManifestError("restore activation identity is invalid")
463 if marker["candidate_generation"] != canonical_candidate: 463 ↛ 464line 463 didn't jump to line 464 because the condition on line 463 was never true
464 raise BackupManifestError("restore activation candidate does not match")
465 if marker["previous_generation"] == canonical_candidate: 465 ↛ 466line 465 didn't jump to line 466 because the condition on line 465 was never true
466 raise BackupManifestError("restore activation generations are invalid")
467 for key in ("manifest_sha256", "candidate_manifest_sha256"):
468 if not isinstance(marker[key], str) or not DIGEST.fullmatch(marker[key]): 468 ↛ 469line 468 didn't jump to line 469 because the condition on line 468 was never true
469 raise BackupManifestError("restore activation digest is invalid")
470 if not isinstance(marker["backup_created_at"], str): 470 ↛ 471line 470 didn't jump to line 471 because the condition on line 470 was never true
471 raise BackupManifestError("restore activation timestamp is invalid")
472 if read_installation_id(owner) != marker["installation_id"]: 472 ↛ 473line 472 didn't jump to line 473 because the condition on line 472 was never true
473 raise BackupManifestError("restore activation belongs to a different installation")
474 active_generation = read_active_generation(owner)
475 if active_generation not in {marker["previous_generation"], canonical_candidate}: 475 ↛ 476line 475 didn't jump to line 476 because the condition on line 475 was never true
476 raise BackupManifestError("active generation does not match restore activation")
478 candidate = owner / "generations" / canonical_candidate
479 require_directory(candidate, "restore candidate generation is unavailable")
480 manifest, _, payload = _read_candidate_manifest(candidate)
481 if hashlib.sha256(payload).hexdigest() != marker["candidate_manifest_sha256"]: 481 ↛ 482line 481 didn't jump to line 482 because the condition on line 481 was never true
482 raise BackupManifestError(
483 "restore candidate manifest digest does not match activation gate"
484 )
485 if manifest_digest(manifest) != marker["manifest_sha256"]: 485 ↛ 486line 485 didn't jump to line 486 because the condition on line 485 was never true
486 raise BackupManifestError("restore archive manifest digest does not match activation gate")
487 if manifest.installation_id != marker["installation_id"]: 487 ↛ 488line 487 didn't jump to line 488 because the condition on line 487 was never true
488 raise BackupManifestError("restore candidate installation identity does not match")
489 if manifest.created_at.isoformat() != marker["backup_created_at"]: 489 ↛ 490line 489 didn't jump to line 490 because the condition on line 489 was never true
490 raise BackupManifestError("restore candidate timestamp does not match activation gate")
491 validate_candidate_generation(candidate, manifest, schema_catalog, snapshot_schema)
492 return manifest
495def verify_staged_tree(
496 staged: Path,
497 manifest: BackupManifest,
498 schema_catalog: SchemaCatalogProvider,
499 snapshot_schema: AccountSnapshotSchemaProvider,
500) -> None:
501 tree_paths(staged, manifest)
502 account_environments: dict[str, str] = {}
503 configured_environments: dict[str, str] = {}
504 for member in manifest.members:
505 path = staged.joinpath(*PurePosixPath(member.path).parts)
506 if path.stat().st_size != member.size or sha256_file(path) != member.sha256:
507 raise BackupManifestError("restore member changed after validation")
508 if is_sqlite_member(member.path):
509 actual_schema = validate_sqlite(
510 path,
511 member.path,
512 manifest.installation_id,
513 schema_catalog,
514 snapshot_schema,
515 )
516 if actual_schema != member.schema_version: 516 ↛ 517line 516 didn't jump to line 517 because the condition on line 516 was never true
517 raise BackupManifestError("restore member schema changed after validation")
518 if account_match := ACCOUNT_DATABASE.fullmatch(member.path):
519 account_environments[account_match.group(1)] = account_environment(path)
520 elif member.path == "trading-configuration.json": 520 ↛ 525line 520 didn't jump to line 525 because the condition on line 520 was always true
521 identities = read_configuration_identities(path.read_bytes())
522 configured_environments = {
523 identity.split(":", 1)[1]: identity.split(":", 1)[0] for identity in identities
524 }
525 elif member.path == "trading-activation.json":
526 validate_activation_json(path.read_bytes())
527 elif member.path.startswith("attachments/"):
528 if sha256_file(path) != PurePosixPath(member.path).name:
529 raise BackupManifestError("restore attachment hash does not match its path")
530 _validate_restore_environments(manifest, account_environments, configured_environments)
533def validate_candidate_generation(
534 candidate: Path,
535 manifest: BackupManifest,
536 schema_catalog: SchemaCatalogProvider,
537 snapshot_schema: AccountSnapshotSchemaProvider,
538) -> None:
539 stored_manifest, candidate_hashes, _ = _read_candidate_manifest(candidate)
540 if stored_manifest != manifest: 540 ↛ 541line 540 didn't jump to line 541 because the condition on line 540 was never true
541 raise BackupManifestError("restore candidate archive manifest changed")
542 tree_paths(candidate, manifest, allow_candidate_manifest=True)
543 account_environments: dict[str, str] = {}
544 configured_environments: dict[str, str] = {}
545 for member in manifest.members:
546 path = candidate.joinpath(*PurePosixPath(member.path).parts)
547 recorded_size, recorded_digest = candidate_hashes[member.path]
548 if path.stat().st_size != recorded_size or sha256_file(path) != recorded_digest:
549 raise BackupManifestError("restore candidate member changed after preparation")
550 if account_match := ACCOUNT_DATABASE.fullmatch(member.path):
551 actual_schema = validate_sqlite(
552 path,
553 member.path,
554 manifest.installation_id,
555 schema_catalog,
556 snapshot_schema,
557 )
558 if actual_schema != member.schema_version: 558 ↛ 559line 558 didn't jump to line 559 because the condition on line 558 was never true
559 raise BackupManifestError("restore account schema changed during preparation")
560 require_manual_disabled_account(path)
561 account_environments[account_match.group(1)] = account_environment(path)
562 else:
563 if recorded_size != member.size or recorded_digest != member.sha256: 563 ↛ 564line 563 didn't jump to line 564 because the condition on line 563 was never true
564 raise BackupManifestError("restore member changed during candidate preparation")
565 if is_sqlite_member(member.path):
566 actual_schema = validate_sqlite(
567 path,
568 member.path,
569 manifest.installation_id,
570 schema_catalog,
571 snapshot_schema,
572 )
573 if actual_schema != member.schema_version: 573 ↛ 574line 573 didn't jump to line 574 because the condition on line 573 was never true
574 raise BackupManifestError("restore database schema changed during preparation")
575 elif member.path == "trading-configuration.json": 575 ↛ 580line 575 didn't jump to line 580 because the condition on line 575 was always true
576 identities = read_configuration_identities(path.read_bytes())
577 configured_environments = {
578 identity.split(":", 1)[1]: identity.split(":", 1)[0] for identity in identities
579 }
580 elif member.path == "trading-activation.json":
581 validate_activation_json(path.read_bytes())
582 elif member.path.startswith("attachments/"):
583 if sha256_file(path) != PurePosixPath(member.path).name:
584 raise BackupManifestError("restore attachment hash does not match its path")
585 _validate_restore_environments(manifest, account_environments, configured_environments)
588def _validate_restore_environments(
589 manifest: BackupManifest,
590 account_environments: dict[str, str],
591 configured_environments: dict[str, str],
592) -> None:
593 if any( 593 ↛ 597line 593 didn't jump to line 597 because the condition on line 593 was never true
594 account_id in configured_environments and configured_environments[account_id] != environment
595 for account_id, environment in account_environments.items()
596 ):
597 raise BackupManifestError("restored account environment conflicts with configuration")
598 identities = {
599 *(
600 f"{environment}:{account_id}"
601 for account_id, environment in account_environments.items()
602 ),
603 *(
604 f"{environment}:{account_id}"
605 for account_id, environment in configured_environments.items()
606 ),
607 }
608 if tuple(sorted(identities)) != manifest.environment_ids: 608 ↛ 609line 608 didn't jump to line 609 because the condition on line 608 was never true
609 raise BackupManifestError("restore account identities do not match the manifest")