systemprompt_security/authz/types/
decision.rs1use std::borrow::Cow;
7use std::fmt;
8
9use serde::{Deserialize, Serialize};
10use systemprompt_identifiers::{McpToolName, PolicyId, SecretPatternId, UserId};
11use thiserror::Error;
12
13use super::entity_ref::EntityRef;
14use super::kinds::RuleType;
15use crate::policy::types::{AccessScope, RateLimitWindow, SecretLocation};
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21#[serde(tag = "kind", rename_all = "snake_case")]
22pub enum MatchedBy {
23 UserAllow,
24 RoleAllow {
25 role: String,
26 },
27 AttributeAllow {
28 rule_type: RuleType,
29 value: String,
30 },
31 DefaultIncluded,
32 PolicyAllow {
33 policy_id: PolicyId,
34 detail: Cow<'static, str>,
35 },
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
47#[serde(tag = "kind", rename_all = "snake_case")]
48pub enum DenyReason {
49 #[error("user {user_id} explicitly denied for {entity}")]
50 UserDeny {
51 entity: EntityRef,
52 user_id: UserId,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
54 justification: Option<String>,
55 },
56 #[error("role {role} denied for {entity}")]
57 RoleDeny {
58 entity: EntityRef,
59 role: String,
60 #[serde(default, skip_serializing_if = "Option::is_none")]
61 justification: Option<String>,
62 },
63 #[error("{rule_type} {value} denied for {entity}")]
64 AttributeDeny {
65 entity: EntityRef,
66 rule_type: RuleType,
67 value: String,
68 #[serde(default, skip_serializing_if = "Option::is_none")]
69 justification: Option<String>,
70 },
71 #[error(
72 "{entity}: not assigned to user {user_id} with roles {roles:?} (no allow rule; \
73 default_included = false). Add an allow rule in services/access-control/roles.yaml."
74 )]
75 NotAssigned {
76 entity: EntityRef,
77 user_id: UserId,
78 roles: Vec<String>,
79 },
80 #[error(
81 "{entity}: unknown to access control. Add an entity row via the publish pipeline or \
82 roles.yaml."
83 )]
84 UnknownEntity { entity: EntityRef },
85 #[error("authz hook unavailable for policy {policy}")]
86 HookUnavailable { policy: String },
87 #[error("{detail}")]
88 PolicyViolation {
89 policy: String,
90 detail: Cow<'static, str>,
91 },
92 #[error("secret detected: {pattern_name} at {location}")]
93 SecretLeak {
94 pattern_id: SecretPatternId,
95 pattern_name: Cow<'static, str>,
96 location: SecretLocation,
97 },
98 #[error("tool {tool} requires {required} scope")]
99 ScopeViolation {
100 tool: McpToolName,
101 required: AccessScope,
102 },
103 #[error("tool {tool} blocked by list {list_id}")]
104 ToolBlocked { tool: McpToolName, list_id: String },
105 #[error("rate limit {window:?} exceeded; retry after {retry_after_ms}ms")]
106 RateLimitExceeded {
107 window: RateLimitWindow,
108 retry_after_ms: u64,
109 },
110}
111
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
120#[serde(tag = "kind", rename_all = "snake_case")]
121pub enum PendingReason {
122 #[error("tool {tool} requires human approval (matched {rule})")]
123 ApprovalRequired { tool: McpToolName, rule: String },
124}
125
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127#[serde(tag = "decision", rename_all = "lowercase")]
128pub enum Decision {
129 Allow { matched_by: MatchedBy },
130 Deny { reason: DenyReason },
131 Pending { reason: PendingReason },
132}
133
134impl Decision {
135 #[must_use]
136 pub const fn tag(&self) -> DecisionTag {
137 match self {
138 Self::Allow { .. } => DecisionTag::Allow,
139 Self::Deny { .. } => DecisionTag::Deny,
140 Self::Pending { .. } => DecisionTag::Pending,
141 }
142 }
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, sqlx::Type)]
152#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
153#[serde(rename_all = "lowercase")]
154pub enum DecisionTag {
155 Allow,
156 Deny,
157 Pending,
158}
159
160impl DecisionTag {
161 #[must_use]
162 pub const fn as_str(self) -> &'static str {
163 match self {
164 Self::Allow => "allow",
165 Self::Deny => "deny",
166 Self::Pending => "pending",
167 }
168 }
169}
170
171impl fmt::Display for DecisionTag {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 f.write_str(self.as_str())
174 }
175}
176
177impl From<&super::request::AuthzDecision> for DecisionTag {
178 fn from(d: &super::request::AuthzDecision) -> Self {
179 match d {
180 super::request::AuthzDecision::Allow => Self::Allow,
181 super::request::AuthzDecision::Deny { .. } => Self::Deny,
182 }
183 }
184}