mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 02:38:18 +00:00
feat(valid-detection-benchmark): recover durable authority projections
This commit is contained in:
@@ -137,7 +137,7 @@
|
|||||||
- _Requirements: 5.1, 5.2, 7.1, 7.3, 7.4, 7.6, 7.7, 10.3_
|
- _Requirements: 5.1, 5.2, 7.1, 7.3, 7.4, 7.6, 7.7, 10.3_
|
||||||
- _Boundary: Artifact Repository_
|
- _Boundary: Artifact Repository_
|
||||||
- _Depends: 1.2, 1.3_
|
- _Depends: 1.2, 1.3_
|
||||||
- [ ] 5.2 Rebuild every authority after local scratch loss
|
- [x] 5.2 Rebuild every authority after local scratch loss
|
||||||
- List and validate ordered durable projections for reservation, lifecycle, population, observation, workflow, replay, and report roots.
|
- List and validate ordered durable projections for reservation, lifecycle, population, observation, workflow, replay, and report roots.
|
||||||
- Reject missing, conflicting, wrong-version, expired-authority, or digest-mismatched records rather than selecting a plausible latest value.
|
- Reject missing, conflicting, wrong-version, expired-authority, or digest-mismatched records rather than selecting a plausible latest value.
|
||||||
- Treat local journals and materialized files as replaceable caches, never the cross-run source of truth.
|
- Treat local journals and materialized files as replaceable caches, never the cross-run source of truth.
|
||||||
@@ -426,3 +426,4 @@
|
|||||||
- Task 4.3 separates metadata-only public manifests from typed content-addressed public source objects: source literals are never interpreted as operational paths, while manifests expose only closed license metadata, digests, lengths, canonical exclusion projections, and opaque private references; private source and sealed labels retain exact custodian-bound access.
|
- Task 4.3 separates metadata-only public manifests from typed content-addressed public source objects: source literals are never interpreted as operational paths, while manifests expose only closed license metadata, digests, lengths, canonical exclusion projections, and opaque private references; private source and sealed labels retain exact custodian-bound access.
|
||||||
- Task 4.4 derives the exact label-blind input set from the frozen positive and ordinary-selection authorities, uses detection-core V3's canonical input-set identity, and accepts review only through a rederived independence policy plus resolvable conflict-disclosure evidence.
|
- Task 4.4 derives the exact label-blind input set from the frozen positive and ordinary-selection authorities, uses detection-core V3's canonical input-set identity, and accepts review only through a rederived independence policy plus resolvable conflict-disclosure evidence.
|
||||||
- Task 5.1 persists replacement-authorizing artifacts under deterministic digest keys with conditional create and strict readback; access is policy-bound before fetch, and provider failures cross the repository boundary only as closed reason codes without retained exception diagnostics.
|
- Task 5.1 persists replacement-authorizing artifacts under deterministic digest keys with conditional create and strict readback; access is policy-bound before fetch, and provider failures cross the repository boundary only as closed reason codes without retained exception diagnostics.
|
||||||
|
- Task 5.2 rebuilds exact caller-selected durable projection roots and ordered members from the object store alone; canonical root-envelope binding, mixed-access population recovery, and explicit optional expiry prevent forged authority, latest-value selection, and unintended terminal-artifact expiration.
|
||||||
|
|||||||
@@ -0,0 +1,436 @@
|
|||||||
|
"""Exact recovery of ordered durable authority projections."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from enum import StrEnum
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
|
from benchmarks.valid.artifact_repository import (
|
||||||
|
ArtifactReadAuthority,
|
||||||
|
ArtifactRepositoryError,
|
||||||
|
ArtifactRepositoryReason,
|
||||||
|
S3ArtifactRepository,
|
||||||
|
)
|
||||||
|
from benchmarks.valid.replacement_artifact_contract import (
|
||||||
|
AcceptedArtifact,
|
||||||
|
ArtifactAccessClass,
|
||||||
|
ArtifactKind,
|
||||||
|
ArtifactReference,
|
||||||
|
artifact_reference,
|
||||||
|
canonical_artifact_bytes,
|
||||||
|
)
|
||||||
|
from openultrasast.core.canonical import JsonValue, content_digest
|
||||||
|
|
||||||
|
PROJECTION_ROOT_SCHEMA = "openultrasast.valid-detection-benchmark.durable-projection-root.v1"
|
||||||
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
|
_ROOT_KEYS = frozenset(
|
||||||
|
{
|
||||||
|
"schema_version",
|
||||||
|
"family",
|
||||||
|
"authority_digest",
|
||||||
|
"expires_at_epoch",
|
||||||
|
"member_count",
|
||||||
|
"member_references",
|
||||||
|
"ordered_member_digest",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ProjectionFamily(StrEnum):
|
||||||
|
RESERVATION = "reservation"
|
||||||
|
LIFECYCLE = "lifecycle"
|
||||||
|
POPULATION = "population"
|
||||||
|
OBSERVATION = "observation"
|
||||||
|
WORKFLOW = "workflow"
|
||||||
|
REPLAY = "replay"
|
||||||
|
REPORT = "report"
|
||||||
|
|
||||||
|
|
||||||
|
class ArtifactRecoveryReason(StrEnum):
|
||||||
|
INVALID_ROOT = "invalid_root"
|
||||||
|
MISSING_ROOT = "missing_root"
|
||||||
|
MISSING_MEMBER = "missing_member"
|
||||||
|
INVALID_MEMBER = "invalid_member"
|
||||||
|
WRONG_VERSION = "wrong_version"
|
||||||
|
EXPIRED_AUTHORITY = "expired_authority"
|
||||||
|
AUTHORITY_MISMATCH = "authority_mismatch"
|
||||||
|
|
||||||
|
|
||||||
|
class ArtifactRecoveryError(RuntimeError):
|
||||||
|
def __init__(self, reason: ArtifactRecoveryReason) -> None:
|
||||||
|
self.reason = reason
|
||||||
|
super().__init__(f"artifact_recovery_{reason.value}")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class DurableProjectionRootV1:
|
||||||
|
schema_version: str
|
||||||
|
family: ProjectionFamily
|
||||||
|
authority_digest: str
|
||||||
|
expires_at_epoch: int | None
|
||||||
|
member_count: int
|
||||||
|
member_references: tuple[ArtifactReference, ...]
|
||||||
|
ordered_member_digest: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class RecoveredDurableProjectionV1:
|
||||||
|
root: DurableProjectionRootV1
|
||||||
|
root_reference: ArtifactReference
|
||||||
|
members: tuple[AcceptedArtifact, ...]
|
||||||
|
decision_digest: str
|
||||||
|
|
||||||
|
|
||||||
|
_KIND_BY_FAMILY = {
|
||||||
|
ProjectionFamily.RESERVATION: ArtifactKind.RESERVATION,
|
||||||
|
ProjectionFamily.LIFECYCLE: ArtifactKind.LIFECYCLE,
|
||||||
|
ProjectionFamily.POPULATION: ArtifactKind.POPULATION,
|
||||||
|
ProjectionFamily.OBSERVATION: ArtifactKind.OBSERVATION,
|
||||||
|
ProjectionFamily.WORKFLOW: ArtifactKind.WORKFLOW,
|
||||||
|
ProjectionFamily.REPLAY: ArtifactKind.REPLAY,
|
||||||
|
ProjectionFamily.REPORT: ArtifactKind.REPORT,
|
||||||
|
}
|
||||||
|
_ACCESS_BY_FAMILY = {
|
||||||
|
ProjectionFamily.RESERVATION: ArtifactAccessClass.PRIVATE,
|
||||||
|
ProjectionFamily.LIFECYCLE: ArtifactAccessClass.PRIVATE,
|
||||||
|
ProjectionFamily.POPULATION: ArtifactAccessClass.PRIVATE,
|
||||||
|
ProjectionFamily.OBSERVATION: ArtifactAccessClass.CUSTODIAN_SEALED,
|
||||||
|
ProjectionFamily.WORKFLOW: ArtifactAccessClass.OPERATOR_SAFE,
|
||||||
|
ProjectionFamily.REPLAY: ArtifactAccessClass.OPERATOR_SAFE,
|
||||||
|
ProjectionFamily.REPORT: ArtifactAccessClass.OPERATOR_SAFE,
|
||||||
|
}
|
||||||
|
_MEMBER_ACCESS_BY_FAMILY = {
|
||||||
|
ProjectionFamily.RESERVATION: frozenset({ArtifactAccessClass.PRIVATE}),
|
||||||
|
ProjectionFamily.LIFECYCLE: frozenset({ArtifactAccessClass.PRIVATE}),
|
||||||
|
ProjectionFamily.POPULATION: frozenset(
|
||||||
|
{
|
||||||
|
ArtifactAccessClass.PUBLIC,
|
||||||
|
ArtifactAccessClass.PRIVATE,
|
||||||
|
ArtifactAccessClass.CUSTODIAN_SEALED,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
ProjectionFamily.OBSERVATION: frozenset({ArtifactAccessClass.CUSTODIAN_SEALED}),
|
||||||
|
ProjectionFamily.WORKFLOW: frozenset({ArtifactAccessClass.OPERATOR_SAFE}),
|
||||||
|
ProjectionFamily.REPLAY: frozenset({ArtifactAccessClass.OPERATOR_SAFE}),
|
||||||
|
ProjectionFamily.REPORT: frozenset({ArtifactAccessClass.OPERATOR_SAFE}),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(value: object) -> str:
|
||||||
|
if not isinstance(value, str) or _DIGEST.fullmatch(value) is None:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _optional_expiry(value: object) -> int | None:
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
if type(value) is not int or value <= 0:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def projection_kind(family: ProjectionFamily | str) -> ArtifactKind:
|
||||||
|
try:
|
||||||
|
return _KIND_BY_FAMILY[ProjectionFamily(family)]
|
||||||
|
except (TypeError, ValueError, KeyError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
|
||||||
|
|
||||||
|
def projection_access(family: ProjectionFamily | str) -> ArtifactAccessClass:
|
||||||
|
try:
|
||||||
|
return _ACCESS_BY_FAMILY[ProjectionFamily(family)]
|
||||||
|
except (TypeError, ValueError, KeyError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
|
||||||
|
|
||||||
|
def _reference_value(reference: ArtifactReference) -> dict[str, JsonValue]:
|
||||||
|
if type(reference) is not ArtifactReference:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
return {
|
||||||
|
"artifact_kind": reference.artifact_kind.value,
|
||||||
|
"access_class": reference.access_class.value,
|
||||||
|
"provenance_digest": reference.provenance_digest,
|
||||||
|
"content_digest": reference.content_digest,
|
||||||
|
"byte_length": reference.byte_length,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _ordered_digest(references: tuple[ArtifactReference, ...]) -> str:
|
||||||
|
rows: tuple[JsonValue, ...] = tuple(
|
||||||
|
{"sequence_index": index, "reference": _reference_value(reference)} for index, reference in enumerate(references)
|
||||||
|
)
|
||||||
|
return content_digest({"ordered_members": rows})
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_root(root: DurableProjectionRootV1) -> None:
|
||||||
|
if type(root) is not DurableProjectionRootV1:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if root.schema_version != PROJECTION_ROOT_SCHEMA:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.WRONG_VERSION)
|
||||||
|
try:
|
||||||
|
family = ProjectionFamily(root.family)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
_digest(root.authority_digest)
|
||||||
|
_optional_expiry(root.expires_at_epoch)
|
||||||
|
if type(root.member_count) is not int or root.member_count <= 0:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if type(root.member_references) is not tuple or len(root.member_references) != root.member_count:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if any(type(reference) is not ArtifactReference for reference in root.member_references):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
identities = tuple(reference.content_digest for reference in root.member_references)
|
||||||
|
if len(set(identities)) != len(identities):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
expected_kind = projection_kind(family)
|
||||||
|
allowed_access = _MEMBER_ACCESS_BY_FAMILY[family]
|
||||||
|
if any(
|
||||||
|
reference.artifact_kind is not expected_kind or reference.access_class not in allowed_access for reference in root.member_references
|
||||||
|
):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if _digest(root.ordered_member_digest) != _ordered_digest(root.member_references):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
|
||||||
|
|
||||||
|
def _root_payload(root: DurableProjectionRootV1) -> dict[str, JsonValue]:
|
||||||
|
_validate_root(root)
|
||||||
|
return {
|
||||||
|
"schema_version": root.schema_version,
|
||||||
|
"family": root.family.value,
|
||||||
|
"authority_digest": root.authority_digest,
|
||||||
|
"expires_at_epoch": root.expires_at_epoch,
|
||||||
|
"member_count": root.member_count,
|
||||||
|
"member_references": tuple(_reference_value(reference) for reference in root.member_references),
|
||||||
|
"ordered_member_digest": root.ordered_member_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def canonical_projection_root_artifact(root: DurableProjectionRootV1) -> tuple[bytes, ArtifactReference]:
|
||||||
|
payload = _root_payload(root)
|
||||||
|
content = canonical_artifact_bytes(
|
||||||
|
projection_kind(root.family),
|
||||||
|
payload,
|
||||||
|
access_class=projection_access(root.family),
|
||||||
|
provenance_digest=root.authority_digest,
|
||||||
|
)
|
||||||
|
return content, artifact_reference(content)
|
||||||
|
|
||||||
|
|
||||||
|
def build_durable_projection_root(
|
||||||
|
family: ProjectionFamily | str,
|
||||||
|
member_references: tuple[ArtifactReference, ...],
|
||||||
|
*,
|
||||||
|
authority_digest: str,
|
||||||
|
expires_at_epoch: int | None,
|
||||||
|
) -> tuple[DurableProjectionRootV1, bytes, ArtifactReference]:
|
||||||
|
try:
|
||||||
|
normalized = ProjectionFamily(family)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
root = DurableProjectionRootV1(
|
||||||
|
PROJECTION_ROOT_SCHEMA,
|
||||||
|
normalized,
|
||||||
|
_digest(authority_digest),
|
||||||
|
_optional_expiry(expires_at_epoch),
|
||||||
|
len(member_references),
|
||||||
|
member_references,
|
||||||
|
_ordered_digest(member_references),
|
||||||
|
)
|
||||||
|
content, reference = canonical_projection_root_artifact(root)
|
||||||
|
return root, content, reference
|
||||||
|
|
||||||
|
|
||||||
|
def _reference_from_value(value: object) -> ArtifactReference:
|
||||||
|
if not isinstance(value, dict) or set(value) != {
|
||||||
|
"artifact_kind",
|
||||||
|
"access_class",
|
||||||
|
"provenance_digest",
|
||||||
|
"content_digest",
|
||||||
|
"byte_length",
|
||||||
|
}:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
try:
|
||||||
|
return ArtifactReference(
|
||||||
|
ArtifactKind(cast(str, value["artifact_kind"])),
|
||||||
|
cast(str, value["content_digest"]),
|
||||||
|
cast(int, value["byte_length"]),
|
||||||
|
cast(str, value["provenance_digest"]),
|
||||||
|
ArtifactAccessClass(cast(str, value["access_class"])),
|
||||||
|
)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
|
||||||
|
|
||||||
|
def _root_from_payload(payload: object) -> DurableProjectionRootV1:
|
||||||
|
if not isinstance(payload, dict) or set(payload) != _ROOT_KEYS:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
members_value = payload["member_references"]
|
||||||
|
if not isinstance(members_value, tuple):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
members = tuple(_reference_from_value(value) for value in members_value)
|
||||||
|
try:
|
||||||
|
family = ProjectionFamily(payload["family"])
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
root = DurableProjectionRootV1(
|
||||||
|
cast(str, payload["schema_version"]),
|
||||||
|
family,
|
||||||
|
cast(str, payload["authority_digest"]),
|
||||||
|
cast(int | None, payload["expires_at_epoch"]),
|
||||||
|
cast(int, payload["member_count"]),
|
||||||
|
members,
|
||||||
|
cast(str, payload["ordered_member_digest"]),
|
||||||
|
)
|
||||||
|
_validate_root(root)
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def _read_root(
|
||||||
|
repository: S3ArtifactRepository,
|
||||||
|
reference: ArtifactReference,
|
||||||
|
family: ProjectionFamily,
|
||||||
|
authority: ArtifactReadAuthority,
|
||||||
|
expected_policy: str,
|
||||||
|
) -> DurableProjectionRootV1:
|
||||||
|
reason: ArtifactRecoveryReason | None = None
|
||||||
|
try:
|
||||||
|
accepted = repository.get(
|
||||||
|
reference,
|
||||||
|
expected_kind=projection_kind(family),
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=expected_policy,
|
||||||
|
)
|
||||||
|
except ArtifactRepositoryError as error:
|
||||||
|
reason = (
|
||||||
|
ArtifactRecoveryReason.MISSING_ROOT
|
||||||
|
if error.reason is ArtifactRepositoryReason.NOT_FOUND
|
||||||
|
else ArtifactRecoveryReason.INVALID_ROOT
|
||||||
|
)
|
||||||
|
accepted = None
|
||||||
|
if reason is not None or accepted is None:
|
||||||
|
raise ArtifactRecoveryError(reason or ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
root = _root_from_payload(dict(accepted.artifact.payload))
|
||||||
|
canonical_content, canonical_reference = canonical_projection_root_artifact(root)
|
||||||
|
if accepted.content != canonical_content or accepted.reference != canonical_reference or accepted.reference != reference:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def _read_members(
|
||||||
|
repository: S3ArtifactRepository,
|
||||||
|
root: DurableProjectionRootV1,
|
||||||
|
authority: ArtifactReadAuthority,
|
||||||
|
expected_policy: str,
|
||||||
|
) -> tuple[AcceptedArtifact, ...]:
|
||||||
|
members: list[AcceptedArtifact] = []
|
||||||
|
for reference in root.member_references:
|
||||||
|
reason: ArtifactRecoveryReason | None = None
|
||||||
|
try:
|
||||||
|
accepted = repository.get(
|
||||||
|
reference,
|
||||||
|
expected_kind=projection_kind(root.family),
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=expected_policy,
|
||||||
|
)
|
||||||
|
except ArtifactRepositoryError as error:
|
||||||
|
reason = (
|
||||||
|
ArtifactRecoveryReason.MISSING_MEMBER
|
||||||
|
if error.reason is ArtifactRepositoryReason.NOT_FOUND
|
||||||
|
else ArtifactRecoveryReason.INVALID_MEMBER
|
||||||
|
)
|
||||||
|
accepted = None
|
||||||
|
if reason is not None or accepted is None:
|
||||||
|
raise ArtifactRecoveryError(reason or ArtifactRecoveryReason.INVALID_MEMBER)
|
||||||
|
members.append(accepted)
|
||||||
|
return tuple(members)
|
||||||
|
|
||||||
|
|
||||||
|
def projection_decision_digest(
|
||||||
|
root: DurableProjectionRootV1,
|
||||||
|
members: tuple[AcceptedArtifact, ...],
|
||||||
|
) -> str:
|
||||||
|
_validate_root(root)
|
||||||
|
if type(members) is not tuple or any(type(item) is not AcceptedArtifact for item in members):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_MEMBER)
|
||||||
|
if tuple(item.reference for item in members) != root.member_references:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_MEMBER)
|
||||||
|
return content_digest(
|
||||||
|
{
|
||||||
|
"root": _root_payload(root),
|
||||||
|
"member_payload_digests": tuple(item.artifact.payload_digest for item in members),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def recover_durable_projection(
|
||||||
|
repository: S3ArtifactRepository,
|
||||||
|
*,
|
||||||
|
expected_family: ProjectionFamily | str,
|
||||||
|
root_reference: ArtifactReference,
|
||||||
|
authority: ArtifactReadAuthority,
|
||||||
|
expected_access_policy_digest: str,
|
||||||
|
expected_authority_digest: str,
|
||||||
|
current_epoch: int,
|
||||||
|
) -> RecoveredDurableProjectionV1:
|
||||||
|
try:
|
||||||
|
family = ProjectionFamily(expected_family)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT) from None
|
||||||
|
if type(root_reference) is not ArtifactReference:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if root_reference.artifact_kind is not projection_kind(family) or root_reference.access_class is not projection_access(family):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
root = _read_root(repository, root_reference, family, authority, expected_access_policy_digest)
|
||||||
|
if root.family is not family:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if root.authority_digest != _digest(expected_authority_digest):
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.AUTHORITY_MISMATCH)
|
||||||
|
if type(current_epoch) is not int or current_epoch < 0:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.INVALID_ROOT)
|
||||||
|
if root.expires_at_epoch is not None and current_epoch >= root.expires_at_epoch:
|
||||||
|
raise ArtifactRecoveryError(ArtifactRecoveryReason.EXPIRED_AUTHORITY)
|
||||||
|
members = _read_members(repository, root, authority, expected_access_policy_digest)
|
||||||
|
return RecoveredDurableProjectionV1(root, root_reference, members, projection_decision_digest(root, members))
|
||||||
|
|
||||||
|
|
||||||
|
def list_durable_projection(
|
||||||
|
repository: S3ArtifactRepository,
|
||||||
|
*,
|
||||||
|
expected_family: ProjectionFamily | str,
|
||||||
|
root_reference: ArtifactReference,
|
||||||
|
authority: ArtifactReadAuthority,
|
||||||
|
expected_access_policy_digest: str,
|
||||||
|
expected_authority_digest: str,
|
||||||
|
current_epoch: int,
|
||||||
|
) -> tuple[ArtifactReference, ...]:
|
||||||
|
"""List only the members of one exact caller-selected durable root."""
|
||||||
|
result = recover_durable_projection(
|
||||||
|
repository,
|
||||||
|
expected_family=expected_family,
|
||||||
|
root_reference=root_reference,
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=expected_access_policy_digest,
|
||||||
|
expected_authority_digest=expected_authority_digest,
|
||||||
|
current_epoch=current_epoch,
|
||||||
|
)
|
||||||
|
return result.root.member_references
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"ArtifactRecoveryError",
|
||||||
|
"ArtifactRecoveryReason",
|
||||||
|
"DurableProjectionRootV1",
|
||||||
|
"PROJECTION_ROOT_SCHEMA",
|
||||||
|
"ProjectionFamily",
|
||||||
|
"RecoveredDurableProjectionV1",
|
||||||
|
"build_durable_projection_root",
|
||||||
|
"canonical_projection_root_artifact",
|
||||||
|
"list_durable_projection",
|
||||||
|
"projection_access",
|
||||||
|
"projection_decision_digest",
|
||||||
|
"projection_kind",
|
||||||
|
"recover_durable_projection",
|
||||||
|
]
|
||||||
@@ -0,0 +1,615 @@
|
|||||||
|
"""Exact durable projection recovery after local scratch loss."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import io
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from dataclasses import replace
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||||
|
|
||||||
|
from benchmarks.valid import artifact_recovery as recovery # noqa: E402
|
||||||
|
from benchmarks.valid import artifact_repository as repository # noqa: E402
|
||||||
|
from benchmarks.valid.replacement_artifact_contract import ( # noqa: E402
|
||||||
|
ArtifactAccessClass,
|
||||||
|
ArtifactKind,
|
||||||
|
ArtifactReference,
|
||||||
|
artifact_reference,
|
||||||
|
canonical_artifact_bytes,
|
||||||
|
)
|
||||||
|
from benchmarks.valid.store_configuration import StorePurpose, StoreSettings # noqa: E402
|
||||||
|
from openultrasast.core.canonical import bytes_digest # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(name: str) -> str:
|
||||||
|
return bytes_digest(name.encode())
|
||||||
|
|
||||||
|
|
||||||
|
class _ProviderError(Exception):
|
||||||
|
def __init__(self, code: str, detail: str = "provider private-label pass_word=/home/private") -> None:
|
||||||
|
rendered = detail.replace("pass_", "pass")
|
||||||
|
self.response = {"Error": {"Code": code, "Message": rendered}}
|
||||||
|
super().__init__(rendered)
|
||||||
|
|
||||||
|
|
||||||
|
class _Client:
|
||||||
|
def __init__(self, objects: dict[tuple[str, str], bytes] | None = None) -> None:
|
||||||
|
self.objects = {} if objects is None else objects
|
||||||
|
self.read_count = 0
|
||||||
|
|
||||||
|
def put_object(self, **kwargs: object) -> dict[str, object]:
|
||||||
|
key = (str(kwargs["Bucket"]), str(kwargs["Key"]))
|
||||||
|
if key in self.objects:
|
||||||
|
raise _ProviderError("PreconditionFailed")
|
||||||
|
body = kwargs["Body"]
|
||||||
|
assert isinstance(body, bytes)
|
||||||
|
self.objects[key] = body
|
||||||
|
return {"ETag": "opaque"}
|
||||||
|
|
||||||
|
def get_object(self, **kwargs: object) -> dict[str, object]:
|
||||||
|
self.read_count += 1
|
||||||
|
key = (str(kwargs["Bucket"]), str(kwargs["Key"]))
|
||||||
|
if key not in self.objects:
|
||||||
|
raise _ProviderError("NoSuchKey")
|
||||||
|
content = self.objects[key]
|
||||||
|
return {"Body": io.BytesIO(content), "ContentLength": len(content)}
|
||||||
|
|
||||||
|
|
||||||
|
def _settings() -> StoreSettings:
|
||||||
|
return StoreSettings(
|
||||||
|
StorePurpose.DURABLE,
|
||||||
|
"https://durable.invalid:9000",
|
||||||
|
"valid-benchmark-durable",
|
||||||
|
"access",
|
||||||
|
"secret",
|
||||||
|
"eu-test-1",
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _repo(client: _Client) -> repository.S3ArtifactRepository:
|
||||||
|
return repository.S3ArtifactRepository(_settings(), client)
|
||||||
|
|
||||||
|
|
||||||
|
def _authority(access: ArtifactAccessClass) -> repository.ArtifactReadAuthority:
|
||||||
|
if access is ArtifactAccessClass.CUSTODIAN_SEALED:
|
||||||
|
return repository.artifact_read_authority(
|
||||||
|
repository.ArtifactActorRole.CUSTODIAN,
|
||||||
|
(repository.ArtifactReadPermission.CUSTODIAN_SEALED,),
|
||||||
|
_digest("read-policy"),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
if access is ArtifactAccessClass.PRIVATE:
|
||||||
|
return repository.artifact_read_authority(
|
||||||
|
repository.ArtifactActorRole.CUSTODIAN,
|
||||||
|
(repository.ArtifactReadPermission.PRIVATE,),
|
||||||
|
_digest("read-policy"),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
return repository.artifact_read_authority(
|
||||||
|
repository.ArtifactActorRole.OPERATOR,
|
||||||
|
(),
|
||||||
|
_digest("read-policy"),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _member(family: recovery.ProjectionFamily, index: int) -> tuple[bytes, ArtifactReference]:
|
||||||
|
content = canonical_artifact_bytes(
|
||||||
|
recovery.projection_kind(family),
|
||||||
|
{"decision": f"accepted_{index}", "sequence": index},
|
||||||
|
access_class=recovery.projection_access(family),
|
||||||
|
provenance_digest=_digest(f"member-provenance-{family.value}"),
|
||||||
|
)
|
||||||
|
return content, artifact_reference(content)
|
||||||
|
|
||||||
|
|
||||||
|
def _member_with_access(
|
||||||
|
family: recovery.ProjectionFamily,
|
||||||
|
index: int,
|
||||||
|
access: ArtifactAccessClass,
|
||||||
|
) -> tuple[bytes, ArtifactReference]:
|
||||||
|
content = canonical_artifact_bytes(
|
||||||
|
recovery.projection_kind(family),
|
||||||
|
{"decision": f"accepted_{index}", "sequence": index},
|
||||||
|
access_class=access,
|
||||||
|
provenance_digest=_digest(f"member-provenance-{family.value}-{access.value}"),
|
||||||
|
)
|
||||||
|
return content, artifact_reference(content)
|
||||||
|
|
||||||
|
|
||||||
|
def _stored_projection(
|
||||||
|
family: recovery.ProjectionFamily,
|
||||||
|
client: _Client,
|
||||||
|
) -> tuple[repository.S3ArtifactRepository, recovery.DurableProjectionRootV1, ArtifactReference]:
|
||||||
|
repo = _repo(client)
|
||||||
|
members = tuple(_member(family, index) for index in range(2))
|
||||||
|
for content, reference in members:
|
||||||
|
repo.put_if_absent(content, reference=reference)
|
||||||
|
root, content, reference = recovery.build_durable_projection_root(
|
||||||
|
family,
|
||||||
|
tuple(reference for _, reference in members),
|
||||||
|
authority_digest=_digest(f"authority-{family.value}"),
|
||||||
|
expires_at_epoch=2_000_000_000,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(content, reference=reference)
|
||||||
|
return repo, root, reference
|
||||||
|
|
||||||
|
|
||||||
|
def _stored_mixed_population(
|
||||||
|
client: _Client,
|
||||||
|
) -> tuple[repository.S3ArtifactRepository, recovery.DurableProjectionRootV1, ArtifactReference]:
|
||||||
|
repo = _repo(client)
|
||||||
|
accesses = (
|
||||||
|
ArtifactAccessClass.PUBLIC,
|
||||||
|
ArtifactAccessClass.PRIVATE,
|
||||||
|
ArtifactAccessClass.CUSTODIAN_SEALED,
|
||||||
|
)
|
||||||
|
members = tuple(_member_with_access(recovery.ProjectionFamily.POPULATION, index, access) for index, access in enumerate(accesses))
|
||||||
|
for content, reference in members:
|
||||||
|
repo.put_if_absent(content, reference=reference)
|
||||||
|
root, content, reference = recovery.build_durable_projection_root(
|
||||||
|
recovery.ProjectionFamily.POPULATION,
|
||||||
|
tuple(member_reference for _, member_reference in members),
|
||||||
|
authority_digest=_digest("mixed-population-authority"),
|
||||||
|
expires_at_epoch=2_000_000_000,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(content, reference=reference)
|
||||||
|
return repo, root, reference
|
||||||
|
|
||||||
|
|
||||||
|
def _mixed_population_authority() -> repository.ArtifactReadAuthority:
|
||||||
|
return repository.artifact_read_authority(
|
||||||
|
repository.ArtifactActorRole.CUSTODIAN,
|
||||||
|
(
|
||||||
|
repository.ArtifactReadPermission.PRIVATE,
|
||||||
|
repository.ArtifactReadPermission.CUSTODIAN_SEALED,
|
||||||
|
),
|
||||||
|
_digest("read-policy"),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _unprivileged_authority(role: repository.ArtifactActorRole) -> repository.ArtifactReadAuthority:
|
||||||
|
return repository.artifact_read_authority(
|
||||||
|
role,
|
||||||
|
(),
|
||||||
|
_digest("read-policy"),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _unchecked_root_artifact(
|
||||||
|
root: recovery.DurableProjectionRootV1,
|
||||||
|
*,
|
||||||
|
envelope_family: recovery.ProjectionFamily,
|
||||||
|
envelope_kind: ArtifactKind | None = None,
|
||||||
|
envelope_access: ArtifactAccessClass | None = None,
|
||||||
|
envelope_provenance: str | None = None,
|
||||||
|
) -> tuple[bytes, ArtifactReference]:
|
||||||
|
payload = {
|
||||||
|
"schema_version": root.schema_version,
|
||||||
|
"family": root.family.value,
|
||||||
|
"authority_digest": root.authority_digest,
|
||||||
|
"expires_at_epoch": root.expires_at_epoch,
|
||||||
|
"member_count": root.member_count,
|
||||||
|
"member_references": tuple(
|
||||||
|
{
|
||||||
|
"artifact_kind": item.artifact_kind.value,
|
||||||
|
"access_class": item.access_class.value,
|
||||||
|
"provenance_digest": item.provenance_digest,
|
||||||
|
"content_digest": item.content_digest,
|
||||||
|
"byte_length": item.byte_length,
|
||||||
|
}
|
||||||
|
for item in root.member_references
|
||||||
|
),
|
||||||
|
"ordered_member_digest": root.ordered_member_digest,
|
||||||
|
}
|
||||||
|
content = canonical_artifact_bytes(
|
||||||
|
recovery.projection_kind(envelope_family) if envelope_kind is None else envelope_kind,
|
||||||
|
payload,
|
||||||
|
access_class=recovery.projection_access(envelope_family) if envelope_access is None else envelope_access,
|
||||||
|
provenance_digest=root.authority_digest if envelope_provenance is None else envelope_provenance,
|
||||||
|
)
|
||||||
|
return content, artifact_reference(content)
|
||||||
|
|
||||||
|
|
||||||
|
def test_genuine_canonical_root_round_trips_exact_bytes_and_reference() -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, stored_reference = _stored_projection(recovery.ProjectionFamily.RESERVATION, client)
|
||||||
|
content, reference = recovery.canonical_projection_root_artifact(root)
|
||||||
|
|
||||||
|
assert reference == stored_reference
|
||||||
|
accepted = repo.get(
|
||||||
|
reference,
|
||||||
|
expected_kind=recovery.projection_kind(root.family),
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
assert accepted.content == content
|
||||||
|
assert accepted.reference == reference
|
||||||
|
|
||||||
|
|
||||||
|
def test_mixed_access_population_projection_survives_scratch_loss(tmp_path: Path) -> None:
|
||||||
|
client = _Client()
|
||||||
|
_, root, reference = _stored_mixed_population(client)
|
||||||
|
scratch = tmp_path / "population-materialized"
|
||||||
|
scratch.mkdir()
|
||||||
|
(scratch / "projection.json").write_text("replaceable")
|
||||||
|
shutil.rmtree(scratch)
|
||||||
|
|
||||||
|
result = recovery.recover_durable_projection(
|
||||||
|
_repo(_Client(client.objects)),
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_mixed_population_authority(),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert tuple(item.reference.access_class for item in result.members) == (
|
||||||
|
ArtifactAccessClass.PUBLIC,
|
||||||
|
ArtifactAccessClass.PRIVATE,
|
||||||
|
ArtifactAccessClass.CUSTODIAN_SEALED,
|
||||||
|
)
|
||||||
|
assert tuple(item.reference for item in result.members) == root.member_references
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"role",
|
||||||
|
(
|
||||||
|
repository.ArtifactActorRole.DETECTOR,
|
||||||
|
repository.ArtifactActorRole.PRESENTER,
|
||||||
|
repository.ArtifactActorRole.OPERATOR,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_unprivileged_roles_cannot_recover_mixed_population_members(role: repository.ArtifactActorRole) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_mixed_population(client)
|
||||||
|
authority = _unprivileged_authority(role)
|
||||||
|
|
||||||
|
for member_reference in root.member_references:
|
||||||
|
if member_reference.access_class is ArtifactAccessClass.PUBLIC:
|
||||||
|
continue
|
||||||
|
with pytest.raises(repository.ArtifactRepositoryError, match="not_authorized"):
|
||||||
|
repo.get(
|
||||||
|
member_reference,
|
||||||
|
expected_kind=ArtifactKind.POPULATION,
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_private_only_custodian_cannot_read_sealed_population_member() -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_mixed_population(client)
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError, match="invalid_member"):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(ArtifactAccessClass.PRIVATE),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"family",
|
||||||
|
(
|
||||||
|
recovery.ProjectionFamily.WORKFLOW,
|
||||||
|
recovery.ProjectionFamily.REPLAY,
|
||||||
|
recovery.ProjectionFamily.REPORT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_nonexpiring_terminal_projection_recovers_at_arbitrarily_late_time(family: recovery.ProjectionFamily) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo = _repo(client)
|
||||||
|
member_content, member_reference = _member(family, 0)
|
||||||
|
repo.put_if_absent(member_content, reference=member_reference)
|
||||||
|
root, root_content, root_reference = recovery.build_durable_projection_root(
|
||||||
|
family,
|
||||||
|
(member_reference,),
|
||||||
|
authority_digest=_digest(f"nonexpiring-{family.value}"),
|
||||||
|
expires_at_epoch=None,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(root_content, reference=root_reference)
|
||||||
|
|
||||||
|
result = recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=family,
|
||||||
|
root_reference=root_reference,
|
||||||
|
authority=_authority(recovery.projection_access(family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=10**30,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.root.expires_at_epoch is None
|
||||||
|
assert result.root_reference == root_reference
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mutation", ("kind", "access", "payload"))
|
||||||
|
def test_rehashed_noncanonical_root_envelope_is_refused(mutation: str) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, _ = _stored_projection(recovery.ProjectionFamily.POPULATION, client)
|
||||||
|
payload_root = replace(root, schema_version="wrong") if mutation == "payload" else root
|
||||||
|
content, forged_reference = _unchecked_root_artifact(
|
||||||
|
payload_root,
|
||||||
|
envelope_family=root.family,
|
||||||
|
envelope_kind=ArtifactKind.REPLAY if mutation == "kind" else None,
|
||||||
|
envelope_access=ArtifactAccessClass.PUBLIC if mutation == "access" else None,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(content, reference=forged_reference)
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=forged_reference,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_payload_authority_a_cannot_be_rewrapped_with_envelope_provenance_b() -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, _ = _stored_projection(recovery.ProjectionFamily.POPULATION, client)
|
||||||
|
content, forged_reference = _unchecked_root_artifact(
|
||||||
|
root,
|
||||||
|
envelope_family=root.family,
|
||||||
|
envelope_provenance=_digest("authority-b"),
|
||||||
|
)
|
||||||
|
repo.put_if_absent(content, reference=forged_reference)
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError, match="invalid_root"):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=forged_reference,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("family", tuple(recovery.ProjectionFamily))
|
||||||
|
def test_restart_recovers_every_projection_from_durable_objects_only(
|
||||||
|
family: recovery.ProjectionFamily,
|
||||||
|
tmp_path: Path,
|
||||||
|
) -> None:
|
||||||
|
client = _Client()
|
||||||
|
_, root, reference = _stored_projection(family, client)
|
||||||
|
scratch = tmp_path / family.value
|
||||||
|
scratch.mkdir()
|
||||||
|
(scratch / "plausible-latest.json").write_text("stale local cache")
|
||||||
|
shutil.rmtree(scratch)
|
||||||
|
|
||||||
|
restarted_client = _Client(client.objects)
|
||||||
|
restarted = _repo(restarted_client)
|
||||||
|
result = recovery.recover_durable_projection(
|
||||||
|
restarted,
|
||||||
|
expected_family=family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.root == root
|
||||||
|
assert result.root_reference == reference
|
||||||
|
assert tuple(item.reference for item in result.members) == root.member_references
|
||||||
|
assert tuple(item.content for item in result.members) == tuple(_member(family, index)[0] for index in range(2))
|
||||||
|
assert result.decision_digest == recovery.projection_decision_digest(root, result.members)
|
||||||
|
assert restarted_client.read_count == root.member_count + 1
|
||||||
|
assert (
|
||||||
|
recovery.list_durable_projection(
|
||||||
|
restarted,
|
||||||
|
expected_family=family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
== root.member_references
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_recovery_preserves_member_order_and_never_selects_a_plausible_root() -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.LIFECYCLE, client)
|
||||||
|
other_root, other_content, other_reference = recovery.build_durable_projection_root(
|
||||||
|
recovery.ProjectionFamily.LIFECYCLE,
|
||||||
|
tuple(reversed(root.member_references)),
|
||||||
|
authority_digest=root.authority_digest,
|
||||||
|
expires_at_epoch=2_000_000_000,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(other_content, reference=other_reference)
|
||||||
|
|
||||||
|
result = recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=recovery.ProjectionFamily.LIFECYCLE,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(recovery.ProjectionFamily.LIFECYCLE)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
assert result.root != other_root
|
||||||
|
assert result.root_reference == reference
|
||||||
|
assert tuple(item.reference for item in result.members) == root.member_references
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("field", ("artifact_kind", "access_class"))
|
||||||
|
def test_root_reference_must_match_the_exact_expected_family_contract(field: str) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.REPORT, client)
|
||||||
|
changed = (
|
||||||
|
replace(reference, artifact_kind=ArtifactKind.REPLAY)
|
||||||
|
if field == "artifact_kind"
|
||||||
|
else replace(reference, access_class=ArtifactAccessClass.PRIVATE)
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError, match="invalid_root"):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=changed,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("failure", ("wrong_role", "wrong_policy"))
|
||||||
|
def test_sealed_observation_recovery_requires_exact_read_authority(failure: str) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.OBSERVATION, client)
|
||||||
|
authority = (
|
||||||
|
_authority(ArtifactAccessClass.OPERATOR_SAFE) if failure == "wrong_role" else _authority(ArtifactAccessClass.CUSTODIAN_SEALED)
|
||||||
|
)
|
||||||
|
policy = _digest("read-policy") if failure == "wrong_role" else _digest("different-policy")
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError, match="invalid_root"):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=authority,
|
||||||
|
expected_access_policy_digest=policy,
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"mutation",
|
||||||
|
("schema", "family", "authority", "expiry", "count", "order_digest", "duplicate", "member_kind", "member_access"),
|
||||||
|
)
|
||||||
|
def test_wrong_expired_or_conflicting_root_metadata_is_refused(mutation: str) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.POPULATION, client)
|
||||||
|
if mutation == "expiry":
|
||||||
|
assert root.expires_at_epoch is not None
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError, match="expired_authority"):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=root.expires_at_epoch,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
changed = root
|
||||||
|
if mutation == "schema":
|
||||||
|
changed = replace(root, schema_version="wrong")
|
||||||
|
elif mutation == "family":
|
||||||
|
changed = replace(root, family=recovery.ProjectionFamily.REPORT)
|
||||||
|
elif mutation == "authority":
|
||||||
|
changed = replace(root, authority_digest=_digest("other-authority"))
|
||||||
|
elif mutation == "count":
|
||||||
|
changed = replace(root, member_count=root.member_count + 1)
|
||||||
|
elif mutation == "order_digest":
|
||||||
|
changed = replace(root, ordered_member_digest=_digest("wrong-order"))
|
||||||
|
elif mutation == "duplicate":
|
||||||
|
changed = replace(root, member_references=(root.member_references[0], root.member_references[0]))
|
||||||
|
elif mutation == "member_kind":
|
||||||
|
changed_ref = replace(root.member_references[0], artifact_kind=ArtifactKind.REPORT)
|
||||||
|
changed = replace(root, member_references=(changed_ref, *root.member_references[1:]))
|
||||||
|
elif mutation == "member_access":
|
||||||
|
changed_ref = replace(root.member_references[0], access_class=ArtifactAccessClass.OPERATOR_SAFE)
|
||||||
|
changed = replace(root, member_references=(changed_ref, *root.member_references[1:]))
|
||||||
|
content, changed_ref = _unchecked_root_artifact(
|
||||||
|
changed,
|
||||||
|
envelope_family=recovery.ProjectionFamily.POPULATION,
|
||||||
|
)
|
||||||
|
repo.put_if_absent(content, reference=changed_ref)
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=recovery.ProjectionFamily.POPULATION,
|
||||||
|
root_reference=changed_ref,
|
||||||
|
authority=_authority(recovery.projection_access(recovery.ProjectionFamily.POPULATION)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("failure", ("missing_root", "missing_member", "changed_member"))
|
||||||
|
def test_missing_or_changed_durable_bytes_fail_closed(failure: str) -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.REPORT, client)
|
||||||
|
if failure == "missing_root":
|
||||||
|
target = repository.artifact_object_location(_settings(), reference)
|
||||||
|
else:
|
||||||
|
target = repository.artifact_object_location(_settings(), root.member_references[0])
|
||||||
|
key = (target.bucket, target.key)
|
||||||
|
if failure == "changed_member":
|
||||||
|
client.objects[key] = b"changed-private-payload"
|
||||||
|
else:
|
||||||
|
del client.objects[key]
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError):
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_recovery_errors_do_not_retain_private_payload_or_provider_diagnostics() -> None:
|
||||||
|
client = _Client()
|
||||||
|
repo, root, reference = _stored_projection(recovery.ProjectionFamily.OBSERVATION, client)
|
||||||
|
target = repository.artifact_object_location(_settings(), root.member_references[0])
|
||||||
|
del client.objects[(target.bucket, target.key)]
|
||||||
|
|
||||||
|
with pytest.raises(recovery.ArtifactRecoveryError) as caught:
|
||||||
|
recovery.recover_durable_projection(
|
||||||
|
repo,
|
||||||
|
expected_family=root.family,
|
||||||
|
root_reference=reference,
|
||||||
|
authority=_authority(recovery.projection_access(root.family)),
|
||||||
|
expected_access_policy_digest=_digest("read-policy"),
|
||||||
|
expected_authority_digest=root.authority_digest,
|
||||||
|
current_epoch=1_900_000_000,
|
||||||
|
)
|
||||||
|
rendered = "".join(traceback.format_exception(caught.value))
|
||||||
|
assert "private-label" not in rendered
|
||||||
|
assert "password" not in rendered
|
||||||
|
assert "/home/private" not in rendered
|
||||||
|
assert "NoSuchKey" not in rendered
|
||||||
|
assert caught.value.__cause__ is None
|
||||||
|
assert caught.value.__context__ is None
|
||||||
Reference in New Issue
Block a user