pub mod detector;
pub mod listener;
#[cfg(feature = "session-hijack-detection")]
pub use detector::SessionHijackDetector;
pub use detector::{IpChangeDetector, RapidSuccessiveDetector};
pub use listener::{AuditAlertListener, TracingAlertListener};
use crate::error::GarrisonResult;
use async_trait::async_trait;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SecurityAlertEvent {
AnomalyLogin {
login_id: String,
anomaly_type: AnomalyType,
detail: String,
trace_id: String,
},
NewDeviceLogin {
login_id: String,
device_id: String,
ip: Option<String>,
},
DisableTriggered {
login_id: String,
service: String,
level: u32,
},
PrivilegeEscalation {
login_id: String,
old_roles: Vec<String>,
new_roles: Vec<String>,
},
SensitiveOperation {
login_id: String,
operation: String,
resource: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalyType {
IpChanged,
DeviceChanged,
GeoJump,
RapidSuccessiveLogin,
SessionHijackSuspected,
}
#[async_trait]
pub trait AlertListener: Send + Sync {
async fn on_alert(&self, _event: &SecurityAlertEvent) -> GarrisonResult<()> {
Ok(())
}
}
#[async_trait]
pub trait AnomalyDetector: Send + Sync {
async fn check_on_login(
&self,
login_id: &str,
device_id: &str,
ip: Option<&str>,
) -> GarrisonResult<Vec<SecurityAlertEvent>>;
async fn check_on_check_login(
&self,
login_id: &str,
token: &str,
) -> GarrisonResult<Vec<SecurityAlertEvent>>;
}
pub struct AlertListenerManager {
listeners: Arc<RwLock<Vec<Arc<dyn AlertListener>>>>,
}
mod manager_impl;
#[cfg(test)]
mod tests;