use serde::{Deserialize, Serialize};
pub const MAX_QUERY_LIMIT: u64 = 1_000;
pub const MAX_TEXT_SEARCH_LEN: usize = 512;
pub const MAX_FILTER_STRING_LEN: usize = 256;
pub const MAX_QUERY_OFFSET: u64 = 1_000_000;
pub const MAX_TENANT_ID_LEN: usize = 64;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum AuditStoreBackend {
#[default]
File,
Postgres,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VerdictFilter {
Allow,
Deny,
RequireApproval,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditQueryParams {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub since: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub until: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub verdict: Option<VerdictFilter>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text_search: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_sequence: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub to_sequence: Option<u64>,
#[serde(default = "default_limit")]
pub limit: u64,
#[serde(default)]
pub offset: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tenant_id: Option<String>,
}
fn default_limit() -> u64 {
100
}
impl Default for AuditQueryParams {
fn default() -> Self {
Self {
since: None,
until: None,
agent_id: None,
tool: None,
verdict: None,
text_search: None,
from_sequence: None,
to_sequence: None,
limit: default_limit(),
offset: 0,
tenant_id: None,
}
}
}
impl AuditQueryParams {
pub fn validate(&self) -> Result<(), String> {
if self.limit > MAX_QUERY_LIMIT {
return Err(format!(
"limit {} exceeds maximum {}",
self.limit, MAX_QUERY_LIMIT
));
}
if self.limit == 0 {
return Err("limit must be > 0".to_string());
}
if self.offset > MAX_QUERY_OFFSET {
return Err(format!(
"offset {} exceeds maximum {}",
self.offset, MAX_QUERY_OFFSET
));
}
if let Some(ref agent_id) = self.agent_id {
if agent_id.is_empty() {
return Err("agent_id filter must not be empty".to_string());
}
if agent_id.len() > MAX_FILTER_STRING_LEN {
return Err(format!(
"agent_id length {} exceeds maximum {}",
agent_id.len(),
MAX_FILTER_STRING_LEN
));
}
if crate::has_dangerous_chars(agent_id) {
return Err("agent_id contains control or format characters".to_string());
}
}
if let Some(ref tool) = self.tool {
if tool.is_empty() {
return Err("tool filter must not be empty".to_string());
}
if tool.len() > MAX_FILTER_STRING_LEN {
return Err(format!(
"tool length {} exceeds maximum {}",
tool.len(),
MAX_FILTER_STRING_LEN
));
}
if crate::has_dangerous_chars(tool) {
return Err("tool contains control or format characters".to_string());
}
}
if let Some(ref text_search) = self.text_search {
if text_search.is_empty() {
return Err("text_search must not be empty".to_string());
}
if text_search.len() > MAX_TEXT_SEARCH_LEN {
return Err(format!(
"text_search length {} exceeds maximum {}",
text_search.len(),
MAX_TEXT_SEARCH_LEN
));
}
if crate::has_dangerous_chars(text_search) {
return Err("text_search contains control or format characters".to_string());
}
if text_search.chars().all(|c| c == '%' || c == '_') {
return Err(
"text_search must not consist solely of SQL wildcard characters".to_string(),
);
}
}
if let Some(ref tenant_id) = self.tenant_id {
if tenant_id.is_empty() {
return Err("tenant_id filter must not be empty".to_string());
}
if tenant_id.len() > MAX_TENANT_ID_LEN {
return Err(format!(
"tenant_id length {} exceeds maximum {}",
tenant_id.len(),
MAX_TENANT_ID_LEN
));
}
if crate::has_dangerous_chars(tenant_id) {
return Err("tenant_id contains control or format characters".to_string());
}
}
if let (Some(from), Some(to)) = (self.from_sequence, self.to_sequence) {
if from > to {
return Err(format!(
"from_sequence ({from}) must be <= to_sequence ({to})"
));
}
}
if let Some(ref since) = self.since {
if since.len() > 64 {
return Err("since timestamp too long".to_string());
}
if crate::has_dangerous_chars(since) {
return Err("since contains control or format characters".to_string());
}
crate::time_util::parse_iso8601_secs(since)
.map_err(|e| format!("since is not valid ISO 8601: {e}"))?;
}
if let Some(ref until) = self.until {
if until.len() > 64 {
return Err("until timestamp too long".to_string());
}
if crate::has_dangerous_chars(until) {
return Err("until contains control or format characters".to_string());
}
crate::time_util::parse_iso8601_secs(until)
.map_err(|e| format!("until is not valid ISO 8601: {e}"))?;
}
if let (Some(ref since), Some(ref until)) = (&self.since, &self.until) {
let since_epoch = crate::time_util::parse_iso8601_secs(since)
.map_err(|e| format!("since is not valid ISO 8601: {e}"))?;
let until_epoch = crate::time_util::parse_iso8601_secs(until)
.map_err(|e| format!("until is not valid ISO 8601: {e}"))?;
if since_epoch >= until_epoch {
return Err(format!("since ({since}) must be before until ({until})"));
}
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditQueryResult {
pub entries: Vec<serde_json::Value>,
pub total: u64,
pub offset: u64,
pub limit: u64,
}
impl AuditQueryResult {
pub fn validate(&self) -> Result<(), String> {
if u64::try_from(self.entries.len()).unwrap_or(u64::MAX) > MAX_QUERY_LIMIT {
return Err(format!(
"AuditQueryResult entries count {} exceeds MAX_QUERY_LIMIT {}",
self.entries.len(),
MAX_QUERY_LIMIT
));
}
if (u64::try_from(self.entries.len()).unwrap_or(u64::MAX)) > self.total {
return Err(format!(
"AuditQueryResult entries count ({}) exceeds total ({})",
self.entries.len(),
self.total
));
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditStoreStatus {
pub backend: AuditStoreBackend,
pub enabled: bool,
pub sink_healthy: bool,
pub pending_count: usize,
}