use serde::{Deserialize, Serialize};
pub mod scope;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ApiKeyScope {
pub read: bool,
pub write: bool,
pub admin: bool,
pub(crate) search_limit: u32,
pub(crate) scrape_limit: u32,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ScopePermission {
Read,
Write,
Admin,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogEntry {
pub id: crate::domain::id::Id,
pub api_key_id: Option<crate::domain::id::Id>,
pub team_id: Option<crate::domain::id::Id>,
pub requested_action: String,
pub decision: AuditDecision,
pub denial_reason: Option<String>,
pub scope_used: Option<ApiKeyScope>,
pub(crate) ip_address: Option<std::net::IpAddr>,
pub(crate) trace_id: Option<crate::domain::id::Id>,
pub(crate) user_agent: Option<String>,
pub(crate) request_path: Option<String>,
pub(crate) request_method: Option<String>,
pub metadata: serde_json::Value,
pub created_at: chrono::DateTime<chrono::Utc>,
}
impl AuditLogEntry {
pub fn ip_address(&self) -> Option<std::net::IpAddr> {
self.ip_address
}
pub fn trace_id(&self) -> Option<crate::domain::id::Id> {
self.trace_id
}
pub fn user_agent(&self) -> Option<&str> {
self.user_agent.as_deref()
}
pub fn request_path(&self) -> Option<&str> {
self.request_path.as_deref()
}
pub fn request_method(&self) -> Option<&str> {
self.request_method.as_deref()
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum AuditDecision {
Allow,
Deny,
}
impl std::fmt::Display for AuditDecision {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AuditDecision::Allow => write!(f, "ALLOW"),
AuditDecision::Deny => write!(f, "DENY"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_api_key_scope_default() {
let scope = ApiKeyScope::default();
assert!(scope.read);
assert!(!scope.write);
assert!(!scope.admin);
assert_eq!(scope.search_limit, 100);
assert_eq!(scope.scrape_limit, 50);
}
#[test]
fn test_api_key_scope_denied() {
let scope = ApiKeyScope::denied();
assert!(!scope.read);
assert!(!scope.write);
assert!(!scope.admin);
assert_eq!(scope.search_limit, 0);
assert_eq!(scope.scrape_limit, 0);
}
#[test]
fn test_api_key_scope_read_only() {
let scope = ApiKeyScope::read_only();
assert!(scope.read);
assert!(!scope.write);
assert!(!scope.admin);
assert_eq!(scope.search_limit, 100);
assert_eq!(scope.scrape_limit, 50);
}
#[test]
fn test_api_key_scope_full_access() {
let scope = ApiKeyScope::full_access();
assert!(scope.read);
assert!(scope.write);
assert!(scope.admin);
assert_eq!(scope.search_limit, u32::MAX);
assert_eq!(scope.scrape_limit, u32::MAX);
}
#[test]
fn test_api_key_scope_has_permission() {
let scope = ApiKeyScope::full_access();
assert!(scope.has_permission(ScopePermission::Read));
assert!(scope.has_permission(ScopePermission::Write));
assert!(scope.has_permission(ScopePermission::Admin));
let scope = ApiKeyScope::read_only();
assert!(scope.has_permission(ScopePermission::Read));
assert!(!scope.has_permission(ScopePermission::Write));
assert!(!scope.has_permission(ScopePermission::Admin));
}
#[test]
fn test_api_key_scope_allows_search_count() {
let scope = ApiKeyScope::default();
assert!(scope.allows_search_count(50));
assert!(scope.allows_search_count(100));
assert!(!scope.allows_search_count(101));
let scope = ApiKeyScope::full_access();
assert!(scope.allows_search_count(u32::MAX));
}
#[test]
fn test_api_key_scope_allows_scrape_count() {
let scope = ApiKeyScope::default();
assert!(scope.allows_scrape_count(25));
assert!(scope.allows_scrape_count(50));
assert!(!scope.allows_scrape_count(51));
let scope = ApiKeyScope::full_access();
assert!(scope.allows_scrape_count(u32::MAX));
}
#[test]
fn test_audit_decision_display() {
assert_eq!(AuditDecision::Allow.to_string(), "ALLOW");
assert_eq!(AuditDecision::Deny.to_string(), "DENY");
}
#[test]
fn test_scope_permission_display() {
assert_eq!(ScopePermission::Read.to_string(), "read");
assert_eq!(ScopePermission::Write.to_string(), "write");
assert_eq!(ScopePermission::Admin.to_string(), "admin");
}
#[test]
fn test_api_key_scope_display() {
let scope = ApiKeyScope::default();
let display = scope.to_string();
assert!(display.contains("read=true"));
assert!(display.contains("write=false"));
assert!(display.contains("admin=false"));
}
#[test]
fn test_scope_permission_from_read_conversion() {
let scope = ApiKeyScope::from(ScopePermission::Read);
assert!(scope.read);
assert!(!scope.write);
assert!(!scope.admin);
assert_eq!(scope.search_limit, 100);
assert_eq!(scope.scrape_limit, 50);
}
#[test]
fn test_scope_permission_from_write_conversion() {
let scope = ApiKeyScope::from(ScopePermission::Write);
assert!(scope.read);
assert!(scope.write);
assert!(!scope.admin);
assert_eq!(scope.search_limit, 100);
assert_eq!(scope.scrape_limit, 50);
}
#[test]
fn test_scope_permission_from_admin_conversion() {
let scope = ApiKeyScope::from(ScopePermission::Admin);
assert!(scope.read);
assert!(scope.write);
assert!(scope.admin);
assert_eq!(scope.search_limit, 100);
assert_eq!(scope.scrape_limit, 50);
}
}