systemprompt_security/authz/
rule_based.rs1use std::sync::Arc;
27
28use async_trait::async_trait;
29use sqlx::PgPool;
30
31use super::audit::{AuthzAuditSink, AuthzSource};
32use super::error::{AuthzError, AuthzResult};
33use super::hook::AuthzDecisionHook;
34use super::parent_chain::{ChainIndexCache, ChainSources, ParentChainIndex, ResolveBase};
35use super::registry::AuthzHookContext;
36use super::repository::AccessControlRepository;
37use super::subject::{
38 SharedSubjectAttributeProvider, SubjectDimension, dimensions_of, discover_subject_providers,
39 gather_subject_attributes,
40};
41use super::types::{AuthzDecision, AuthzRequest, Decision, DenyReason};
42
43#[derive(Clone)]
44pub struct RuleBasedHook {
45 repo: AccessControlRepository,
46 sink: Arc<dyn AuthzAuditSink>,
47 providers: Vec<SharedSubjectAttributeProvider>,
48 dimensions: Vec<SubjectDimension>,
49 sources: Arc<ChainSources>,
50 cache: Arc<ChainIndexCache>,
51}
52
53impl std::fmt::Debug for RuleBasedHook {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 f.debug_struct("RuleBasedHook")
56 .field("repo", &self.repo)
57 .field("sink", &self.sink)
58 .field("providers", &self.providers)
59 .field("dimensions", &self.dimensions)
60 .finish_non_exhaustive()
61 }
62}
63
64impl RuleBasedHook {
65 #[must_use]
66 pub fn new(pool: Arc<PgPool>, sink: Arc<dyn AuthzAuditSink>, sources: ChainSources) -> Self {
67 let providers = discover_subject_providers(&AuthzHookContext {
68 pool: Arc::clone(&pool),
69 sink: Arc::clone(&sink),
70 });
71 Self {
72 repo: AccessControlRepository::from_pool(pool),
73 sink,
74 dimensions: dimensions_of(&providers),
75 providers,
76 sources: Arc::new(sources),
77 cache: Arc::new(ChainIndexCache::default()),
78 }
79 }
80
81 async fn chain_index(&self) -> AuthzResult<Arc<ParentChainIndex>> {
82 self.cache.get(&self.repo, Arc::clone(&self.sources)).await
83 }
84
85 async fn fault(&self, req: &AuthzRequest, error: &AuthzError) -> AuthzDecision {
86 let policy = AuthzSource::RuleBased.policy().to_owned();
87 let detail = error.to_string();
88 let decision = AuthzDecision::Deny {
89 reason: DenyReason::HookUnavailable {
90 policy: policy.clone(),
91 detail: detail.clone(),
92 },
93 policy,
94 };
95 tracing::warn!(
96 entity = %req.entity,
97 user_id = %req.user_id,
98 error = %detail,
99 "rule-based authz hook fault",
100 );
101 self.sink
102 .record(req, &decision, AuthzSource::RuleBased)
103 .await;
104 decision
105 }
106}
107
108#[async_trait]
109impl AuthzDecisionHook for RuleBasedHook {
110 async fn evaluate(&self, req: AuthzRequest) -> AuthzDecision {
111 let kind = req.entity.kind();
112 let id = req.entity.id_str();
113
114 let entity = match self.repo.get_entity(kind, id).await {
115 Ok(row) => row,
116 Err(err) => return self.fault(&req, &err).await,
117 };
118 let rules = match self.repo.list_rules_for_entity(kind, id).await {
119 Ok(rules) => rules,
120 Err(err) => return self.fault(&req, &err).await,
121 };
122
123 let index = match self.chain_index().await {
124 Ok(index) => index,
125 Err(err) => return self.fault(&req, &err).await,
126 };
127
128 let attributes = gather_subject_attributes(&self.providers, &req.user_id).await;
129 let decision = index.resolve(
130 kind,
131 id,
132 ResolveBase {
133 rules: &rules,
134 user_id: &req.user_id,
135 user_roles: &req.roles,
136 default_included: entity.map(|e| e.default_included),
137 attributes: &attributes,
138 dimensions: &self.dimensions,
139 },
140 );
141
142 let policy = AuthzSource::RuleBased.policy().to_owned();
143 let authz_decision = match decision {
144 Decision::Allow { .. } => AuthzDecision::Allow,
145 Decision::Warn { reason } => {
146 tracing::warn!(
147 entity = %req.entity,
148 user_id = %req.user_id,
149 %reason,
150 "access rule evaluated to warn; allowing the request"
151 );
152 AuthzDecision::Allow
153 },
154 Decision::Deny { reason } => AuthzDecision::Deny { reason, policy },
155 Decision::Pending { reason } => {
156 tracing::error!(
157 %reason,
158 "a governance hold reached the rule-based resolver, which cannot park a \
159 request; refusing it"
160 );
161 AuthzDecision::Deny {
162 reason: DenyReason::PolicyViolation {
163 policy: "require_approval".to_owned(),
164 detail: std::borrow::Cow::Borrowed(
165 "approval required, but this enforcement point cannot hold a request",
166 ),
167 },
168 policy,
169 }
170 },
171 };
172 self.sink
173 .record(&req, &authz_decision, AuthzSource::RuleBased)
174 .await;
175 authz_decision
176 }
177}