systemprompt_security/policy/
types.rs1use std::fmt;
14use std::str::FromStr;
15
16use serde::{Deserialize, Serialize};
17use systemprompt_identifiers::{CallId, PolicyId, SessionId, UserId};
18
19use super::governed::{GovernedInput, GovernedTarget};
20use crate::authz::error::AuthzError;
21use crate::authz::types::Decision;
22
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29pub struct SecretLocation {
30 pub kind: String,
31 pub path: String,
32 pub redacted: String,
33}
34
35impl SecretLocation {
36 pub fn new(
37 kind: impl Into<String>,
38 path: impl Into<String>,
39 redacted: impl Into<String>,
40 ) -> Self {
41 Self {
42 kind: kind.into(),
43 path: path.into(),
44 redacted: redacted.into(),
45 }
46 }
47}
48
49impl fmt::Display for SecretLocation {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 if self.path.is_empty() {
52 write!(f, "{} ({})", self.kind, self.redacted)
53 } else {
54 write!(f, "{}.{} ({})", self.kind, self.path, self.redacted)
55 }
56 }
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60pub struct RateLimitWindow {
61 pub name: String,
62 pub seconds: u64,
63 pub limit: u64,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(tag = "kind", rename_all = "snake_case")]
71pub enum AgentScope {
72 User { user_id: UserId },
73 System,
74}
75
76impl AgentScope {
77 #[must_use]
78 pub const fn user_id(&self) -> Option<&UserId> {
79 match self {
80 Self::User { user_id } => Some(user_id),
81 Self::System => None,
82 }
83 }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, sqlx::Type)]
95#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
96#[serde(rename_all = "lowercase")]
97pub enum AccessScope {
98 Admin,
99 User,
100 Unknown,
101}
102
103impl AccessScope {
104 #[must_use]
105 pub const fn as_str(self) -> &'static str {
106 match self {
107 Self::Admin => "admin",
108 Self::User => "user",
109 Self::Unknown => "unknown",
110 }
111 }
112}
113
114impl fmt::Display for AccessScope {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 f.write_str(self.as_str())
117 }
118}
119
120impl FromStr for AccessScope {
121 type Err = AuthzError;
122
123 fn from_str(s: &str) -> Result<Self, Self::Err> {
124 match s {
125 "admin" => Ok(Self::Admin),
126 "user" => Ok(Self::User),
127 "unknown" | "" => Ok(Self::Unknown),
128 other => Err(AuthzError::Validation(format!(
129 "unknown access scope: {other}"
130 ))),
131 }
132 }
133}
134
135#[derive(Debug)]
136pub struct PolicyContext<'a> {
137 pub target: GovernedTarget,
138 pub agent_scope: AgentScope,
139 pub access_scope: AccessScope,
140 pub session_id: &'a SessionId,
141 pub user_id: &'a UserId,
142 pub input: &'a GovernedInput,
143 pub call_id: &'a CallId,
144}
145
146pub trait GovernancePolicy: Send + Sync + fmt::Debug {
158 fn id(&self) -> PolicyId;
159 fn name(&self) -> &'static str;
160 fn description(&self) -> &'static str;
161 fn evaluate(&self, ctx: &PolicyContext<'_>) -> Decision;
162}