"""Queue-bound private manifests. Never accept a manifest path from a request.""" import hashlib import hmac import json from .protocol import sha256, token, verify_binding def principal(settings, scheduling): from ..store import open_confined_file with open_confined_file(settings.key_file, settings.workspace_root) as handle: credential = hashlib.sha256(handle.read().strip()).hexdigest() return credential + ":" + (scheduling or {}).get("principal_id", "legacy") def analysis_manifest(settings, store, inputs, parameters, identity): from ..store import confined_path, open_confined_file analysis = store.get(inputs["analysis_id"]) if ( not analysis or analysis["state"] != "succeeded" or analysis["task_type"] != "video.people.analyze" ): raise ValueError("Analysis is missing, unsuccessful or deleted") if principal(settings, analysis.get("scheduling")) != identity: raise ValueError("Analysis belongs to another principal") if analysis["parameters"]["max_pixels"] != parameters["max_pixels"]: raise ValueError("Analysis pixel profile does not match replacement") base = settings.outputs_dir / analysis["id"] if analysis["attempt_count"] >= 2: base = base / f"attempt-{analysis['attempt_count']:04d}" manifest = confined_path( base / ".private" / "manifest.json", settings.workspace_root, must_exist=True, file_only=True, ) binding_path = confined_path( base / ".private" / "binding.json", settings.workspace_root, must_exist=True, file_only=True, ) with open_confined_file(binding_path, settings.workspace_root) as stream: binding = json.load(stream) verify_binding( binding, principal=identity, source_sha256=sha256(inputs["video_path"]), manifest_sha256=sha256(manifest), ) if not hmac.compare_digest(token(binding), inputs["analysis_token"]): raise ValueError("Analysis token mismatch") with open_confined_file(manifest, settings.workspace_root) as stream: data = json.load(stream) if any(item["person_id"] not in data["people"] for item in inputs["replacements"]): raise ValueError("Selected person does not exist in analysis") return str(manifest)