src/pqc_enclave_sdk/policy.py
1.2 KB · 43 lines · python Raw
1 """Access policies - which app bundles / artifact kinds may be read by whom."""
2
3 from __future__ import annotations
4
5 from dataclasses import dataclass, field
6
7 from pqc_enclave_sdk.artifact import ArtifactKind, ArtifactMetadata
8 from pqc_enclave_sdk.errors import PolicyViolationError
9
10
11 @dataclass
12 class ArtifactPolicy:
13 """Policy rule for a single artifact kind."""
14
15 kind: ArtifactKind
16 allowed_bundle_ids: frozenset[str]
17 require_biometric: bool = False
18 max_uses_per_hour: int = 0
19
20
21 @dataclass
22 class AccessPolicy:
23 """Collection of per-kind policies."""
24
25 rules: dict[ArtifactKind, ArtifactPolicy] = field(default_factory=dict)
26
27 def add(self, rule: ArtifactPolicy) -> AccessPolicy:
28 self.rules[rule.kind] = rule
29 return self
30
31 def check(self, artifact_meta: ArtifactMetadata, caller_bundle_id: str) -> None:
32 rule = self.rules.get(artifact_meta.kind)
33 if rule is None:
34 return
35 if (
36 rule.allowed_bundle_ids
37 and caller_bundle_id not in rule.allowed_bundle_ids
38 ):
39 raise PolicyViolationError(
40 f"caller {caller_bundle_id} not allowed to read "
41 f"{artifact_meta.kind.value}"
42 )
43