mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +00:00
feat(valid-detection-benchmark): persist content addressed artifacts
This commit is contained in:
@@ -129,7 +129,7 @@
|
||||
- _Depends: 1.1, 4.2, 4.3_
|
||||
|
||||
- [ ] 5. Persist authoritative artifacts and custody durably
|
||||
- [ ] 5.1 Implement create-only content-addressed artifact persistence
|
||||
- [x] 5.1 Implement create-only content-addressed artifact persistence
|
||||
- Persist canonical reservation, review, lifecycle, population, sealed-label, observation, workflow, replay, and report artifacts under purpose-specific keys.
|
||||
- Require conditional creation or immutable digest keys, strict expected-kind/digest reads, nonzero bytes before decode, and equal-byte idempotency.
|
||||
- Enforce public, private, custodian-sealed, and operator-safe access classes before returning artifact bytes.
|
||||
@@ -425,3 +425,4 @@
|
||||
- Task 4.2 classifies and freezes only exact reviewed authorities against the Reservation Authority's current census snapshot; admission requires at least forty positives across Python and JavaScript and two families, while stale censuses, split networks, and insufficient scope remain non-authorizing.
|
||||
- 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 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.
|
||||
|
||||
@@ -0,0 +1,372 @@
|
||||
"""Create-only S3 persistence for replacement benchmark artifacts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Iterable, Mapping
|
||||
from dataclasses import dataclass, replace
|
||||
from enum import StrEnum
|
||||
from typing import Protocol, cast
|
||||
|
||||
from benchmarks.valid.replacement_artifact_contract import (
|
||||
AcceptedArtifact,
|
||||
ArtifactAccessClass,
|
||||
ArtifactContractError,
|
||||
ArtifactKind,
|
||||
ArtifactReference,
|
||||
accept_artifact,
|
||||
)
|
||||
from benchmarks.valid.store_configuration import StorePurpose, StoreSettings
|
||||
from openultrasast.core.canonical import JsonValue, content_digest
|
||||
|
||||
|
||||
class ArtifactRepositoryReason(StrEnum):
|
||||
INVALID_REQUEST = "invalid_request"
|
||||
NOT_AUTHORIZED = "not_authorized"
|
||||
POLICY_MISMATCH = "policy_mismatch"
|
||||
NOT_FOUND = "not_found"
|
||||
EMPTY_OBJECT = "empty_object"
|
||||
INVALID_OBJECT = "invalid_object"
|
||||
IDENTITY_CONFLICT = "identity_conflict"
|
||||
STORE_FAILURE = "store_failure"
|
||||
DEPENDENCY_MISSING = "dependency_missing"
|
||||
|
||||
|
||||
class ArtifactRepositoryError(RuntimeError):
|
||||
"""Closed repository failure that never includes provider or artifact bytes."""
|
||||
|
||||
def __init__(self, reason: ArtifactRepositoryReason) -> None:
|
||||
self.reason = reason
|
||||
super().__init__(f"artifact_repository_{reason.value}")
|
||||
|
||||
|
||||
class ArtifactRepositoryConflict(ArtifactRepositoryError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(ArtifactRepositoryReason.IDENTITY_CONFLICT)
|
||||
|
||||
|
||||
class ArtifactActorRole(StrEnum):
|
||||
CUSTODIAN = "custodian"
|
||||
DETECTOR = "detector"
|
||||
PRESENTER = "presenter"
|
||||
OPERATOR = "operator"
|
||||
|
||||
|
||||
class ArtifactReadPermission(StrEnum):
|
||||
PRIVATE = "private"
|
||||
CUSTODIAN_SEALED = "custodian_sealed"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArtifactReadAuthority:
|
||||
actor_role: ArtifactActorRole
|
||||
permissions: tuple[ArtifactReadPermission, ...]
|
||||
policy_digest: str
|
||||
authority_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArtifactObjectLocation:
|
||||
purpose: StorePurpose
|
||||
bucket: str
|
||||
key: str
|
||||
|
||||
|
||||
class S3ObjectClient(Protocol):
|
||||
def put_object(self, **kwargs: object) -> Mapping[str, object]: ...
|
||||
|
||||
def get_object(self, **kwargs: object) -> Mapping[str, object]: ...
|
||||
|
||||
|
||||
_PREFIX_BY_KIND = {
|
||||
ArtifactKind.RESERVATION: "reservations",
|
||||
ArtifactKind.REVIEW: "reviews",
|
||||
ArtifactKind.LIFECYCLE: "lifecycles",
|
||||
ArtifactKind.POPULATION: "populations",
|
||||
ArtifactKind.OBSERVATION: "observations",
|
||||
ArtifactKind.WORKFLOW: "workflows",
|
||||
ArtifactKind.REPORT: "reports",
|
||||
ArtifactKind.REPLAY: "replays",
|
||||
}
|
||||
_PRECONDITION_CODES = frozenset({"409", "412", "ConditionalRequestConflict", "PreconditionFailed"})
|
||||
_NOT_FOUND_CODES = frozenset({"404", "NoSuchKey", "NotFound"})
|
||||
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||
|
||||
|
||||
def _digest(value: object) -> str:
|
||||
if not isinstance(value, str) or _DIGEST.fullmatch(value) is None:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
return value
|
||||
|
||||
|
||||
def _authority_value(authority: ArtifactReadAuthority) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"actor_role": authority.actor_role.value,
|
||||
"permissions": tuple(item.value for item in authority.permissions),
|
||||
"policy_digest": authority.policy_digest,
|
||||
}
|
||||
|
||||
|
||||
def artifact_read_authority(
|
||||
actor_role: ArtifactActorRole | str,
|
||||
permissions: Iterable[ArtifactReadPermission | str],
|
||||
policy_digest: str,
|
||||
*,
|
||||
expected_access_policy_digest: str,
|
||||
) -> ArtifactReadAuthority:
|
||||
invalid = False
|
||||
try:
|
||||
role = ArtifactActorRole(actor_role)
|
||||
granted = tuple(sorted({ArtifactReadPermission(item) for item in permissions}, key=lambda item: item.value))
|
||||
except (TypeError, ValueError):
|
||||
invalid = True
|
||||
if invalid:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
policy = _digest(policy_digest)
|
||||
expected = _digest(expected_access_policy_digest)
|
||||
if policy != expected:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.POLICY_MISMATCH)
|
||||
if role is not ArtifactActorRole.CUSTODIAN and granted:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
draft = ArtifactReadAuthority(role, granted, policy, "")
|
||||
return replace(draft, authority_digest=content_digest(_authority_value(draft)))
|
||||
|
||||
|
||||
def _validate_authority(authority: ArtifactReadAuthority, expected_policy: str) -> None:
|
||||
if type(authority) is not ArtifactReadAuthority:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
if type(authority.actor_role) is not ArtifactActorRole or any(
|
||||
type(item) is not ArtifactReadPermission for item in authority.permissions
|
||||
):
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
expected_permissions = tuple(sorted(set(authority.permissions), key=lambda item: item.value))
|
||||
if authority.permissions != expected_permissions:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
expected = _digest(expected_policy)
|
||||
if authority.actor_role is not ArtifactActorRole.CUSTODIAN and authority.permissions:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
if authority.policy_digest != expected:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.POLICY_MISMATCH)
|
||||
if authority.authority_digest != content_digest(_authority_value(authority)):
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
|
||||
|
||||
def _require_access(access: ArtifactAccessClass, authority: ArtifactReadAuthority) -> None:
|
||||
role = authority.actor_role
|
||||
permissions = authority.permissions
|
||||
is_allowed = access is ArtifactAccessClass.PUBLIC
|
||||
if access is ArtifactAccessClass.PRIVATE:
|
||||
is_allowed = role is ArtifactActorRole.CUSTODIAN and ArtifactReadPermission.PRIVATE in permissions
|
||||
elif access is ArtifactAccessClass.CUSTODIAN_SEALED:
|
||||
is_allowed = role is ArtifactActorRole.CUSTODIAN and ArtifactReadPermission.CUSTODIAN_SEALED in permissions
|
||||
elif access is ArtifactAccessClass.OPERATOR_SAFE:
|
||||
is_allowed = role in {ArtifactActorRole.OPERATOR, ArtifactActorRole.PRESENTER}
|
||||
if not is_allowed:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.NOT_AUTHORIZED)
|
||||
|
||||
|
||||
def artifact_object_location(settings: StoreSettings, reference: ArtifactReference) -> ArtifactObjectLocation:
|
||||
if type(settings) is not StoreSettings or settings.purpose is not StorePurpose.DURABLE:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
if type(reference) is not ArtifactReference:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
prefix = _PREFIX_BY_KIND[reference.artifact_kind]
|
||||
key = f"{prefix}/{reference.access_class.value}/{reference.content_digest}.json"
|
||||
return ArtifactObjectLocation(StorePurpose.DURABLE, settings.bucket, key)
|
||||
|
||||
|
||||
def _provider_code(error: Exception) -> str:
|
||||
try:
|
||||
response = getattr(error, "response", None)
|
||||
if isinstance(response, Mapping):
|
||||
details = response.get("Error")
|
||||
code = details.get("Code", "") if isinstance(details, Mapping) else ""
|
||||
else:
|
||||
code = getattr(error, "code", "")
|
||||
except Exception:
|
||||
return ""
|
||||
if isinstance(code, str):
|
||||
return code
|
||||
return str(code) if type(code) is int else ""
|
||||
|
||||
|
||||
def _body_bytes(response: Mapping[str, object]) -> bytes:
|
||||
body = response.get("Body")
|
||||
read_failed = False
|
||||
try:
|
||||
value = body.read() if hasattr(body, "read") else body
|
||||
except Exception:
|
||||
read_failed = True
|
||||
value = None
|
||||
if read_failed:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
if not isinstance(value, bytes):
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
if not value:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.EMPTY_OBJECT)
|
||||
length = response.get("ContentLength")
|
||||
if type(length) is not int or length != len(value):
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
return value
|
||||
|
||||
|
||||
class S3ArtifactRepository:
|
||||
"""Strict content-object repository over an injected S3-compatible client."""
|
||||
|
||||
def __init__(self, settings: StoreSettings, client: S3ObjectClient) -> None:
|
||||
if type(settings) is not StoreSettings or settings.purpose is not StorePurpose.DURABLE:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
self._settings = settings
|
||||
self._client = client
|
||||
|
||||
def _read_raw(self, reference: ArtifactReference) -> bytes:
|
||||
location = artifact_object_location(self._settings, reference)
|
||||
failure: ArtifactRepositoryReason | None = None
|
||||
try:
|
||||
response = self._client.get_object(Bucket=location.bucket, Key=location.key)
|
||||
except Exception as error:
|
||||
failure = (
|
||||
ArtifactRepositoryReason.NOT_FOUND if _provider_code(error) in _NOT_FOUND_CODES else ArtifactRepositoryReason.STORE_FAILURE
|
||||
)
|
||||
response = None
|
||||
if failure is not None:
|
||||
raise ArtifactRepositoryError(failure)
|
||||
if not isinstance(response, Mapping):
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
return _body_bytes(response)
|
||||
|
||||
def _read_accepted(self, reference: ArtifactReference) -> AcceptedArtifact:
|
||||
content = self._read_raw(reference)
|
||||
invalid = False
|
||||
try:
|
||||
accepted = accept_artifact(content, reference=reference)
|
||||
except ArtifactContractError:
|
||||
invalid = True
|
||||
accepted = None
|
||||
if invalid or accepted is None:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_OBJECT)
|
||||
return accepted
|
||||
|
||||
def _existing_after_precondition(self, content: bytes, reference: ArtifactReference) -> ArtifactReference:
|
||||
read_failed = False
|
||||
try:
|
||||
existing = self._read_raw(reference)
|
||||
except ArtifactRepositoryError:
|
||||
read_failed = True
|
||||
existing = b""
|
||||
if read_failed:
|
||||
raise ArtifactRepositoryConflict()
|
||||
if existing != content:
|
||||
raise ArtifactRepositoryConflict()
|
||||
invalid = False
|
||||
try:
|
||||
accept_artifact(existing, reference=reference)
|
||||
except ArtifactContractError:
|
||||
invalid = True
|
||||
if invalid:
|
||||
raise ArtifactRepositoryConflict()
|
||||
return reference
|
||||
|
||||
def put_if_absent(self, content: bytes, *, reference: ArtifactReference) -> ArtifactReference:
|
||||
invalid = False
|
||||
try:
|
||||
accepted = accept_artifact(content, reference=reference)
|
||||
except (ArtifactContractError, TypeError):
|
||||
invalid = True
|
||||
accepted = None
|
||||
if invalid or accepted is None:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_OBJECT)
|
||||
location = artifact_object_location(self._settings, accepted.reference)
|
||||
precondition = False
|
||||
put_failed = False
|
||||
try:
|
||||
response = self._client.put_object(
|
||||
Bucket=location.bucket,
|
||||
Key=location.key,
|
||||
Body=content,
|
||||
ContentType="application/json",
|
||||
IfNoneMatch="*",
|
||||
)
|
||||
except Exception as error:
|
||||
precondition = _provider_code(error) in _PRECONDITION_CODES
|
||||
put_failed = not precondition
|
||||
response = None
|
||||
if precondition:
|
||||
return self._existing_after_precondition(content, reference)
|
||||
if put_failed:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
if not isinstance(response, Mapping) or not isinstance(response.get("ETag"), str) or not response["ETag"]:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
self._read_accepted(reference)
|
||||
return reference
|
||||
|
||||
def get(
|
||||
self,
|
||||
reference: ArtifactReference,
|
||||
*,
|
||||
expected_kind: ArtifactKind | str,
|
||||
authority: ArtifactReadAuthority,
|
||||
expected_access_policy_digest: str,
|
||||
) -> AcceptedArtifact:
|
||||
if type(reference) is not ArtifactReference:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
invalid = False
|
||||
try:
|
||||
kind = ArtifactKind(expected_kind)
|
||||
except (TypeError, ValueError):
|
||||
invalid = True
|
||||
kind = None
|
||||
if invalid or kind is None:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
if reference.artifact_kind is not kind:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
_validate_authority(authority, expected_access_policy_digest)
|
||||
_require_access(reference.access_class, authority)
|
||||
return self._read_accepted(reference)
|
||||
|
||||
|
||||
def open_s3_artifact_repository(settings: StoreSettings) -> S3ArtifactRepository:
|
||||
"""Build the application client without any bucket-administration authority."""
|
||||
if type(settings) is not StoreSettings or settings.purpose is not StorePurpose.DURABLE:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.INVALID_REQUEST)
|
||||
dependency_missing = False
|
||||
try:
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
except ImportError:
|
||||
dependency_missing = True
|
||||
if dependency_missing:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.DEPENDENCY_MISSING)
|
||||
store_failed = False
|
||||
try:
|
||||
config = Config(signature_version="s3v4", s3={"addressing_style": "path"})
|
||||
client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=settings.endpoint,
|
||||
aws_access_key_id=settings.access_key_id,
|
||||
aws_secret_access_key=settings.secret_access_key,
|
||||
aws_session_token=settings.session_token,
|
||||
region_name=settings.region,
|
||||
config=config,
|
||||
)
|
||||
except Exception:
|
||||
store_failed = True
|
||||
client = None
|
||||
if store_failed or client is None:
|
||||
raise ArtifactRepositoryError(ArtifactRepositoryReason.STORE_FAILURE)
|
||||
return S3ArtifactRepository(settings, cast(S3ObjectClient, client))
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ArtifactActorRole",
|
||||
"ArtifactObjectLocation",
|
||||
"ArtifactReadAuthority",
|
||||
"ArtifactReadPermission",
|
||||
"ArtifactRepositoryConflict",
|
||||
"ArtifactRepositoryError",
|
||||
"ArtifactRepositoryReason",
|
||||
"S3ArtifactRepository",
|
||||
"artifact_object_location",
|
||||
"artifact_read_authority",
|
||||
"open_s3_artifact_repository",
|
||||
]
|
||||
@@ -0,0 +1,480 @@
|
||||
"""Create-only S3 artifact repository and access-boundary tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import sys
|
||||
import traceback
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import cast
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
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, secret: str = "provider-secret") -> None:
|
||||
self.response = {"Error": {"Code": code, "Message": secret}}
|
||||
super().__init__(secret)
|
||||
|
||||
|
||||
class _ExplodingBody:
|
||||
def read(self) -> bytes:
|
||||
raise _ProviderError("InternalError", "body endpoint password=/home/body-private")
|
||||
|
||||
|
||||
class _Client:
|
||||
def __init__(self) -> None:
|
||||
self.objects: dict[tuple[str, str], bytes] = {}
|
||||
self.put_calls: list[dict[str, object]] = []
|
||||
self.get_calls: list[dict[str, object]] = []
|
||||
self.put_error: Exception | None = None
|
||||
self.get_error: Exception | None = None
|
||||
self.read_override: bytes | None = None
|
||||
self.length_override: int | None = None
|
||||
self.put_response: dict[str, object] = {"ETag": "opaque-storage-token"}
|
||||
self.body_error = False
|
||||
|
||||
def put_object(self, **kwargs: object) -> dict[str, object]:
|
||||
self.put_calls.append(kwargs)
|
||||
if self.put_error is not None:
|
||||
raise self.put_error
|
||||
key = (str(kwargs["Bucket"]), str(kwargs["Key"]))
|
||||
if kwargs.get("IfNoneMatch") != "*":
|
||||
raise AssertionError("conditional create was not requested")
|
||||
if key in self.objects:
|
||||
raise _ProviderError("PreconditionFailed")
|
||||
body = kwargs["Body"]
|
||||
assert isinstance(body, bytes)
|
||||
self.objects[key] = body
|
||||
return self.put_response
|
||||
|
||||
def get_object(self, **kwargs: object) -> dict[str, object]:
|
||||
self.get_calls.append(kwargs)
|
||||
if self.get_error is not None:
|
||||
raise self.get_error
|
||||
key = (str(kwargs["Bucket"]), str(kwargs["Key"]))
|
||||
if key not in self.objects:
|
||||
raise _ProviderError("NoSuchKey")
|
||||
content = self.objects[key] if self.read_override is None else self.read_override
|
||||
length = len(content) if self.length_override is None else self.length_override
|
||||
body: object = _ExplodingBody() if self.body_error else io.BytesIO(content)
|
||||
return {"Body": body, "ContentLength": length}
|
||||
|
||||
|
||||
def _settings() -> StoreSettings:
|
||||
return StoreSettings(
|
||||
StorePurpose.DURABLE,
|
||||
"https://store.internal.invalid:9000",
|
||||
"valid-benchmark-durable",
|
||||
"ACCESS-SECRET",
|
||||
"SECRET-SECRET",
|
||||
"eu-test-1",
|
||||
"SESSION-SECRET",
|
||||
)
|
||||
|
||||
|
||||
def _artifact(
|
||||
kind: ArtifactKind = ArtifactKind.POPULATION,
|
||||
access: ArtifactAccessClass = ArtifactAccessClass.PUBLIC,
|
||||
) -> tuple[bytes, ArtifactReference]:
|
||||
content = canonical_artifact_bytes(
|
||||
kind,
|
||||
{"count": 1, "authority_digest": _digest("authority")},
|
||||
access_class=access,
|
||||
provenance_digest=_digest("provenance"),
|
||||
)
|
||||
return content, artifact_reference(content)
|
||||
|
||||
|
||||
def _authority(
|
||||
role: repository.ArtifactActorRole,
|
||||
permissions: tuple[repository.ArtifactReadPermission, ...] = (),
|
||||
) -> repository.ArtifactReadAuthority:
|
||||
return repository.artifact_read_authority(
|
||||
role,
|
||||
permissions,
|
||||
_digest("read-policy"),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
|
||||
|
||||
def _repo(client: _Client) -> repository.S3ArtifactRepository:
|
||||
return repository.S3ArtifactRepository(_settings(), client)
|
||||
|
||||
|
||||
def _assert_closed_exception(error: BaseException) -> None:
|
||||
rendered = "".join(traceback.format_exception(error))
|
||||
for forbidden in (
|
||||
"provider-secret",
|
||||
"endpoint password",
|
||||
"/home/get-private",
|
||||
"/home/put-private",
|
||||
"/home/body-private",
|
||||
"raw-private-payload",
|
||||
):
|
||||
assert forbidden not in rendered
|
||||
pending: list[BaseException] = [error]
|
||||
seen: set[int] = set()
|
||||
while pending:
|
||||
current = pending.pop()
|
||||
if id(current) in seen:
|
||||
continue
|
||||
seen.add(id(current))
|
||||
for linked in (current.__cause__, current.__context__):
|
||||
if linked is not None:
|
||||
pending.append(linked)
|
||||
assert len(seen) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", tuple(ArtifactKind))
|
||||
def test_all_closed_artifact_kinds_round_trip_byte_identically(kind: ArtifactKind) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact(kind)
|
||||
|
||||
assert content
|
||||
assert service.put_if_absent(content, reference=reference) == reference
|
||||
accepted = service.get(
|
||||
reference,
|
||||
expected_kind=kind,
|
||||
authority=_authority(repository.ArtifactActorRole.DETECTOR),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
assert accepted.content == content
|
||||
assert accepted.reference == reference
|
||||
assert client.put_calls[0]["IfNoneMatch"] == "*"
|
||||
|
||||
|
||||
def test_purpose_key_is_closed_deterministic_and_contains_only_digest_metadata() -> None:
|
||||
content, reference = _artifact()
|
||||
first = repository.artifact_object_location(_settings(), reference)
|
||||
second = repository.artifact_object_location(_settings(), reference)
|
||||
|
||||
assert content
|
||||
assert first == second
|
||||
assert first.purpose is StorePurpose.DURABLE
|
||||
assert first.bucket == "valid-benchmark-durable"
|
||||
assert first.key == f"populations/public/{reference.content_digest}.json"
|
||||
for forbidden in ("store.internal", "ACCESS-SECRET", "SECRET-SECRET", "/home/", "password"):
|
||||
assert forbidden not in first.key
|
||||
|
||||
|
||||
def test_equal_existing_bytes_are_idempotent_and_different_bytes_conflict_without_overwrite() -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
location = repository.artifact_object_location(_settings(), reference)
|
||||
client.objects[(location.bucket, location.key)] = content
|
||||
|
||||
assert service.put_if_absent(content, reference=reference) == reference
|
||||
assert client.objects[(location.bucket, location.key)] == content
|
||||
|
||||
client.objects[(location.bucket, location.key)] = b"different-existing-bytes"
|
||||
with pytest.raises(repository.ArtifactRepositoryConflict) as caught:
|
||||
service.put_if_absent(content, reference=reference)
|
||||
assert caught.value.reason is repository.ArtifactRepositoryReason.IDENTITY_CONFLICT
|
||||
assert client.objects[(location.bucket, location.key)] == b"different-existing-bytes"
|
||||
assert "different" not in str(caught.value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("access", "allowed_role", "permission"),
|
||||
(
|
||||
(ArtifactAccessClass.PUBLIC, repository.ArtifactActorRole.DETECTOR, None),
|
||||
(ArtifactAccessClass.PRIVATE, repository.ArtifactActorRole.CUSTODIAN, repository.ArtifactReadPermission.PRIVATE),
|
||||
(
|
||||
ArtifactAccessClass.CUSTODIAN_SEALED,
|
||||
repository.ArtifactActorRole.CUSTODIAN,
|
||||
repository.ArtifactReadPermission.CUSTODIAN_SEALED,
|
||||
),
|
||||
(ArtifactAccessClass.OPERATOR_SAFE, repository.ArtifactActorRole.OPERATOR, None),
|
||||
(ArtifactAccessClass.OPERATOR_SAFE, repository.ArtifactActorRole.PRESENTER, None),
|
||||
),
|
||||
)
|
||||
def test_access_classes_require_exact_roles_and_permissions(
|
||||
access: ArtifactAccessClass,
|
||||
allowed_role: repository.ArtifactActorRole,
|
||||
permission: repository.ArtifactReadPermission | None,
|
||||
) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact(access=access)
|
||||
service.put_if_absent(content, reference=reference)
|
||||
permissions = () if permission is None else (permission,)
|
||||
|
||||
accepted = service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=_authority(allowed_role, permissions),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
assert accepted.content == content
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("access", "role", "permissions"),
|
||||
(
|
||||
(ArtifactAccessClass.PRIVATE, repository.ArtifactActorRole.DETECTOR, ()),
|
||||
(ArtifactAccessClass.PRIVATE, repository.ArtifactActorRole.CUSTODIAN, ()),
|
||||
(ArtifactAccessClass.CUSTODIAN_SEALED, repository.ArtifactActorRole.OPERATOR, ()),
|
||||
(ArtifactAccessClass.CUSTODIAN_SEALED, repository.ArtifactActorRole.CUSTODIAN, ()),
|
||||
(ArtifactAccessClass.OPERATOR_SAFE, repository.ArtifactActorRole.DETECTOR, ()),
|
||||
),
|
||||
)
|
||||
def test_unauthorized_reads_fail_before_fetch_without_raw_content(
|
||||
access: ArtifactAccessClass,
|
||||
role: repository.ArtifactActorRole,
|
||||
permissions: tuple[repository.ArtifactReadPermission, ...],
|
||||
) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact(access=access)
|
||||
service.put_if_absent(content, reference=reference)
|
||||
client.get_calls.clear()
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as caught:
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=_authority(role, permissions),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
assert caught.value.reason is repository.ArtifactRepositoryReason.NOT_AUTHORIZED
|
||||
assert client.get_calls == []
|
||||
assert content.decode() not in str(caught.value)
|
||||
|
||||
|
||||
def test_policy_mismatch_and_tampered_authority_fail_before_fetch() -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
service.put_if_absent(content, reference=reference)
|
||||
authority = _authority(repository.ArtifactActorRole.DETECTOR)
|
||||
client.get_calls.clear()
|
||||
|
||||
for invalid in (authority, replace(authority, actor_role=repository.ArtifactActorRole.OPERATOR)):
|
||||
with pytest.raises(repository.ArtifactRepositoryError):
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=invalid,
|
||||
expected_access_policy_digest=_digest("wrong-policy") if invalid is authority else _digest("read-policy"),
|
||||
)
|
||||
assert client.get_calls == []
|
||||
|
||||
malformed_permissions = replace(
|
||||
authority,
|
||||
permissions=cast(tuple[repository.ArtifactReadPermission, ...], ("private",)),
|
||||
)
|
||||
with pytest.raises(repository.ArtifactRepositoryError):
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=malformed_permissions,
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError):
|
||||
repository.artifact_read_authority(
|
||||
repository.ArtifactActorRole.DETECTOR,
|
||||
(repository.ArtifactReadPermission.PRIVATE,),
|
||||
_digest("read-policy"),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
with pytest.raises(repository.ArtifactRepositoryError):
|
||||
repository.artifact_read_authority(
|
||||
repository.ArtifactActorRole.DETECTOR,
|
||||
(),
|
||||
"not-a-digest",
|
||||
expected_access_policy_digest="not-a-digest",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mutation", ("kind", "digest", "length", "provenance", "access"))
|
||||
def test_strict_reads_reject_every_reference_or_kind_mismatch(mutation: str) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
service.put_if_absent(content, reference=reference)
|
||||
expected_kind = reference.artifact_kind
|
||||
changed = reference
|
||||
if mutation == "kind":
|
||||
expected_kind = ArtifactKind.REPORT
|
||||
elif mutation == "digest":
|
||||
changed = replace(reference, content_digest=_digest("wrong"))
|
||||
elif mutation == "length":
|
||||
changed = replace(reference, byte_length=reference.byte_length + 1)
|
||||
elif mutation == "provenance":
|
||||
changed = replace(reference, provenance_digest=_digest("wrong"))
|
||||
else:
|
||||
changed = replace(reference, access_class=ArtifactAccessClass.PRIVATE)
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError):
|
||||
service.get(
|
||||
changed,
|
||||
expected_kind=expected_kind,
|
||||
authority=_authority(repository.ArtifactActorRole.DETECTOR),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mode", "reason"),
|
||||
(
|
||||
("missing", repository.ArtifactRepositoryReason.NOT_FOUND),
|
||||
("empty", repository.ArtifactRepositoryReason.EMPTY_OBJECT),
|
||||
("malformed", repository.ArtifactRepositoryReason.INVALID_OBJECT),
|
||||
("length", repository.ArtifactRepositoryReason.STORE_FAILURE),
|
||||
("transport", repository.ArtifactRepositoryReason.STORE_FAILURE),
|
||||
),
|
||||
)
|
||||
def test_missing_empty_malformed_and_transport_reads_are_closed_failures(
|
||||
mode: str,
|
||||
reason: repository.ArtifactRepositoryReason,
|
||||
) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
location = repository.artifact_object_location(_settings(), reference)
|
||||
if mode != "missing":
|
||||
client.objects[(location.bucket, location.key)] = content
|
||||
if mode == "empty":
|
||||
client.read_override = b""
|
||||
elif mode == "malformed":
|
||||
client.read_override = b"not-json"
|
||||
elif mode == "length":
|
||||
client.length_override = len(content) + 1
|
||||
elif mode == "transport":
|
||||
client.get_error = _ProviderError("InternalError", "endpoint password=/home/private")
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as caught:
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=_authority(repository.ArtifactActorRole.DETECTOR),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
assert caught.value.reason is reason
|
||||
assert "password" not in str(caught.value)
|
||||
assert "/home/" not in str(caught.value)
|
||||
|
||||
|
||||
def test_successful_create_requires_exact_nonempty_readback() -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
client.read_override = b""
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as caught:
|
||||
service.put_if_absent(content, reference=reference)
|
||||
assert caught.value.reason is repository.ArtifactRepositoryReason.EMPTY_OBJECT
|
||||
|
||||
wrong_reference = replace(reference, byte_length=reference.byte_length + 1)
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as invalid:
|
||||
_repo(_Client()).put_if_absent(content, reference=wrong_reference)
|
||||
assert invalid.value.reason is repository.ArtifactRepositoryReason.INVALID_OBJECT
|
||||
|
||||
ambiguous_client = _Client()
|
||||
ambiguous_client.put_response = {}
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as ambiguous:
|
||||
_repo(ambiguous_client).put_if_absent(content, reference=reference)
|
||||
assert ambiguous.value.reason is repository.ArtifactRepositoryReason.STORE_FAILURE
|
||||
|
||||
|
||||
def test_configured_client_is_lazy_path_style_sigv4_and_does_not_administer_store(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
def client_factory(*args: object, **kwargs: object) -> _Client:
|
||||
captured["args"] = args
|
||||
captured["kwargs"] = kwargs
|
||||
return _Client()
|
||||
|
||||
monkeypatch.setitem(sys.modules, "boto3", SimpleNamespace(client=client_factory))
|
||||
monkeypatch.setitem(sys.modules, "botocore.config", SimpleNamespace(Config=lambda **kwargs: kwargs))
|
||||
service = repository.open_s3_artifact_repository(_settings())
|
||||
|
||||
assert isinstance(service, repository.S3ArtifactRepository)
|
||||
assert captured["args"] == ("s3",)
|
||||
kwargs = captured["kwargs"]
|
||||
assert isinstance(kwargs, dict)
|
||||
assert kwargs["endpoint_url"] == _settings().endpoint
|
||||
assert kwargs["aws_access_key_id"] == _settings().access_key_id
|
||||
assert kwargs["aws_secret_access_key"] == _settings().secret_access_key
|
||||
config = kwargs["config"]
|
||||
assert isinstance(config, dict)
|
||||
assert config["signature_version"] == "s3v4"
|
||||
assert config["s3"] == {"addressing_style": "path"}
|
||||
assert not any(name in captured for name in ("create_bucket", "put_bucket_lifecycle", "put_bucket_versioning"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"failure",
|
||||
("get", "put", "body", "precondition_missing", "precondition_transport", "precondition_malformed"),
|
||||
)
|
||||
def test_provider_failures_discard_exception_chains_and_sensitive_messages(failure: str) -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
location = repository.artifact_object_location(_settings(), reference)
|
||||
client.objects[(location.bucket, location.key)] = content
|
||||
if failure == "get":
|
||||
client.get_error = _ProviderError("InternalError", "endpoint password=/home/get-private")
|
||||
elif failure == "put":
|
||||
client.objects.clear()
|
||||
client.put_error = _ProviderError("InternalError", "endpoint password=/home/put-private")
|
||||
elif failure == "body":
|
||||
client.body_error = True
|
||||
elif failure == "precondition_missing":
|
||||
client.get_error = _ProviderError("NoSuchKey", "endpoint password=/home/missing-private")
|
||||
elif failure == "precondition_transport":
|
||||
client.get_error = _ProviderError("InternalError", "endpoint password=/home/reread-private")
|
||||
else:
|
||||
client.read_override = b"raw-private-payload"
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as caught:
|
||||
if failure in {"get", "body"}:
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=_authority(repository.ArtifactActorRole.DETECTOR),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
else:
|
||||
service.put_if_absent(content, reference=reference)
|
||||
_assert_closed_exception(caught.value)
|
||||
|
||||
|
||||
def test_invalid_raw_artifact_failure_has_no_payload_bearing_exception_chain() -> None:
|
||||
client = _Client()
|
||||
service = _repo(client)
|
||||
content, reference = _artifact()
|
||||
location = repository.artifact_object_location(_settings(), reference)
|
||||
client.objects[(location.bucket, location.key)] = b"raw-private-payload"
|
||||
|
||||
with pytest.raises(repository.ArtifactRepositoryError) as caught:
|
||||
service.get(
|
||||
reference,
|
||||
expected_kind=reference.artifact_kind,
|
||||
authority=_authority(repository.ArtifactActorRole.DETECTOR),
|
||||
expected_access_policy_digest=_digest("read-policy"),
|
||||
)
|
||||
_assert_closed_exception(caught.value)
|
||||
Reference in New Issue
Block a user