feat: define policy impact subject contract
This commit is contained in:
@@ -7,6 +7,12 @@ from urllib.parse import quote, unquote
|
||||
from govoplan_core.core.access import PrincipalRef
|
||||
|
||||
PolicyScopeType = Literal["system", "tenant", "user", "group", "campaign"]
|
||||
PolicyImpactPopulationState = Literal[
|
||||
"complete",
|
||||
"sampled",
|
||||
"truncated",
|
||||
"unavailable",
|
||||
]
|
||||
CampaignArchiveEncryptionMethod = Literal["aes", "zip_standard"]
|
||||
CampaignArchivePasswordDeliveryChannel = Literal[
|
||||
"separate_mail",
|
||||
@@ -54,6 +60,7 @@ CAPABILITY_POLICY_DEFINITION_GOVERNANCE = "policy.definitionGovernance"
|
||||
CAPABILITY_POLICY_VIEW_GOVERNANCE = "policy.viewGovernance"
|
||||
CAPABILITY_POLICY_FUNCTION_ASSIGNMENT_GOVERNANCE = "policy.functionAssignmentGovernance"
|
||||
CAPABILITY_POLICY_CAMPAIGN_ARCHIVE_ENCRYPTION = "policy.campaignArchiveEncryption"
|
||||
CAPABILITY_POLICY_IMPACT_SUBJECT_PREFIX = "policy.impactSubjects."
|
||||
|
||||
POLICY_SCOPE_TYPES: tuple[PolicyScopeType, ...] = (
|
||||
"system",
|
||||
@@ -191,6 +198,146 @@ class PolicyDecision:
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PolicyImpactPopulationRequest:
|
||||
"""One explicit, bounded request to an optional impact-subject provider."""
|
||||
|
||||
tenant_id: str
|
||||
policy_family: str
|
||||
selector: Mapping[str, Any] = field(default_factory=dict)
|
||||
limit: int = 200
|
||||
actor_scopes: tuple[str, ...] = ()
|
||||
allow_sensitive_details: bool = False
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.tenant_id.strip():
|
||||
raise ValueError("Policy impact population requires a tenant ID")
|
||||
if not self.policy_family.strip() or len(self.policy_family) > 120:
|
||||
raise ValueError(
|
||||
"Policy impact population family must contain 1 to 120 characters"
|
||||
)
|
||||
if self.limit < 1 or self.limit > 500:
|
||||
raise ValueError("Policy impact population limit must be between 1 and 500")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PolicyImpactSubject:
|
||||
"""Provider-owned reference safe for Policy to compare without domain imports."""
|
||||
|
||||
module_id: str
|
||||
resource_type: str
|
||||
resource_id: str
|
||||
action: str
|
||||
label: str | None = None
|
||||
scope_type: PolicyScopeType | None = None
|
||||
scope_id: str | None = None
|
||||
attributes: Mapping[str, Any] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
for label, value, maximum in (
|
||||
("module ID", self.module_id, 80),
|
||||
("resource type", self.resource_type, 80),
|
||||
("resource ID", self.resource_id, 240),
|
||||
("action", self.action, 120),
|
||||
):
|
||||
if not value.strip() or len(value) > maximum:
|
||||
raise ValueError(
|
||||
f"Policy impact subject {label} must contain 1 to {maximum} characters"
|
||||
)
|
||||
|
||||
@property
|
||||
def key(self) -> tuple[str, str, str, str]:
|
||||
return (
|
||||
self.module_id,
|
||||
self.resource_type,
|
||||
self.resource_id,
|
||||
self.action,
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"module_id": self.module_id,
|
||||
"resource_type": self.resource_type,
|
||||
"resource_id": self.resource_id,
|
||||
"action": self.action,
|
||||
"label": self.label,
|
||||
"scope_type": self.scope_type,
|
||||
"scope_id": self.scope_id,
|
||||
"attributes": dict(self.attributes),
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PolicyImpactSubjectBatch:
|
||||
provider_id: str
|
||||
subjects: tuple[PolicyImpactSubject, ...] = ()
|
||||
state: PolicyImpactPopulationState = "complete"
|
||||
total_available: int | None = None
|
||||
explanation: str | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.provider_id.strip() or len(self.provider_id) > 120:
|
||||
raise ValueError(
|
||||
"Policy impact provider ID must contain 1 to 120 characters"
|
||||
)
|
||||
if len(self.subjects) > 500:
|
||||
raise ValueError("Policy impact providers may return at most 500 subjects")
|
||||
if len({subject.key for subject in self.subjects}) != len(self.subjects):
|
||||
raise ValueError("Policy impact provider returned duplicate subjects")
|
||||
if self.total_available is not None and self.total_available < len(self.subjects):
|
||||
raise ValueError(
|
||||
"Policy impact population total cannot be smaller than its subjects"
|
||||
)
|
||||
if self.state == "unavailable" and not self.explanation:
|
||||
raise ValueError("Unavailable policy impact populations need an explanation")
|
||||
|
||||
def to_dict(self, *, include_subjects: bool = True) -> dict[str, Any]:
|
||||
return {
|
||||
"provider_id": self.provider_id,
|
||||
"state": self.state,
|
||||
"returned": len(self.subjects),
|
||||
"total_available": self.total_available,
|
||||
"explanation": self.explanation,
|
||||
"subjects": (
|
||||
[subject.to_dict() for subject in self.subjects]
|
||||
if include_subjects
|
||||
else []
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class PolicyImpactSubjectProvider(Protocol):
|
||||
provider_id: str
|
||||
supported_policy_families: tuple[str, ...]
|
||||
|
||||
def collect_policy_impact_subjects(
|
||||
self,
|
||||
session: object | None = None,
|
||||
*,
|
||||
request: PolicyImpactPopulationRequest,
|
||||
) -> PolicyImpactSubjectBatch: ...
|
||||
|
||||
|
||||
def policy_impact_subject_provider(
|
||||
registry: object | None,
|
||||
provider_id: str,
|
||||
) -> PolicyImpactSubjectProvider | None:
|
||||
clean_provider_id = provider_id.strip()
|
||||
if not clean_provider_id or registry is None:
|
||||
return None
|
||||
capability_name = f"{CAPABILITY_POLICY_IMPACT_SUBJECT_PREFIX}{clean_provider_id}"
|
||||
if (
|
||||
not hasattr(registry, "has_capability")
|
||||
or not registry.has_capability(capability_name)
|
||||
):
|
||||
return None
|
||||
capability = registry.capability(capability_name)
|
||||
if not isinstance(capability, PolicyImpactSubjectProvider):
|
||||
return None
|
||||
return capability
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CampaignArchiveEncryptionRequest:
|
||||
"""Context required to resolve one Campaign archive-encryption ceiling.
|
||||
|
||||
Reference in New Issue
Block a user