auth_framework/monitoring/
alerts.rs1use super::{SecurityEvent, SecurityEventSeverity, SecurityEventType};
4use crate::errors::Result;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct AlertConfig {
11 pub enabled: bool,
13 pub thresholds: AlertThresholds,
15 pub channels: Vec<NotificationChannel>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct AlertThresholds {
22 pub failed_logins_per_minute: u64,
24 pub max_response_time_ms: u64,
26 pub error_rate_threshold: f64,
28 pub alert_cooldown_seconds: u64,
30}
31
32impl Default for AlertThresholds {
33 fn default() -> Self {
34 Self {
35 failed_logins_per_minute: 10,
36 max_response_time_ms: 5000,
37 error_rate_threshold: 0.1, alert_cooldown_seconds: 300, }
40 }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
45pub enum NotificationChannel {
46 Email { recipients: Vec<String> },
48 Slack { webhook_url: String },
50 Teams { webhook_url: String },
52 Webhook {
54 url: String,
55 headers: HashMap<String, String>,
56 },
57 Log { level: String },
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
63pub enum AlertSeverity {
64 Info,
65 Warning,
66 Critical,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct Alert {
72 pub id: String,
74 pub title: String,
76 pub message: String,
78 pub severity: AlertSeverity,
80 pub source: String,
82 pub metrics: HashMap<String, f64>,
84 pub timestamp: u64,
86}
87
88pub struct AlertManager {
90 config: AlertConfig,
92 recent_alerts: HashMap<String, u64>,
94}
95
96impl AlertManager {
97 pub fn new(config: AlertConfig) -> Self {
99 Self {
100 config,
101 recent_alerts: HashMap::new(),
102 }
103 }
104
105 pub async fn process_security_event(&mut self, event: &SecurityEvent) -> Result<()> {
107 if !self.config.enabled {
108 return Ok(());
109 }
110
111 let alert = match event.event_type {
112 SecurityEventType::FailedLogin => {
113 if event.severity >= SecurityEventSeverity::High {
114 Some(Alert {
115 id: format!("failed_login_{}", event.timestamp),
116 title: "High volume of failed login attempts detected".to_string(),
117 message: format!(
118 "Multiple failed login attempts detected for user {:?} from IP {:?}",
119 event.user_id, event.ip_address
120 ),
121 severity: AlertSeverity::Warning,
122 source: "authentication".to_string(),
123 metrics: HashMap::new(),
124 timestamp: event.timestamp,
125 })
126 } else {
127 None
128 }
129 }
130 SecurityEventType::AccountLockout => Some(Alert {
131 id: format!("account_lockout_{}", event.timestamp),
132 title: "Account lockout triggered".to_string(),
133 message: format!(
134 "Account {:?} has been locked due to security policy",
135 event.user_id
136 ),
137 severity: AlertSeverity::Warning,
138 source: "security".to_string(),
139 metrics: HashMap::new(),
140 timestamp: event.timestamp,
141 }),
142 SecurityEventType::PrivilegeEscalation => Some(Alert {
143 id: format!("privilege_escalation_{}", event.timestamp),
144 title: "Privilege escalation attempt detected".to_string(),
145 message: format!("Privilege escalation attempt by user {:?}", event.user_id),
146 severity: AlertSeverity::Critical,
147 source: "authorization".to_string(),
148 metrics: HashMap::new(),
149 timestamp: event.timestamp,
150 }),
151 _ => None,
152 };
153
154 if let Some(alert) = alert {
155 self.send_alert(alert).await?;
156 }
157
158 Ok(())
159 }
160
161 pub async fn process_performance_metrics(
163 &mut self,
164 metrics: &HashMap<String, u64>,
165 ) -> Result<()> {
166 if !self.config.enabled {
167 return Ok(());
168 }
169
170 if let Some(&response_time) = metrics.get("avg_response_time_us") {
172 let response_time_ms = response_time / 1000; if response_time_ms > self.config.thresholds.max_response_time_ms {
175 let alert = Alert {
176 id: format!(
177 "high_response_time_{}",
178 crate::monitoring::current_timestamp()
179 ),
180 title: "High response time detected".to_string(),
181 message: format!(
182 "Average response time is {}ms, which exceeds the threshold of {}ms",
183 response_time_ms, self.config.thresholds.max_response_time_ms
184 ),
185 severity: AlertSeverity::Warning,
186 source: "performance".to_string(),
187 metrics: {
188 let mut m = HashMap::new();
189 m.insert("response_time_ms".to_string(), response_time_ms as f64);
190 m.insert(
191 "threshold_ms".to_string(),
192 self.config.thresholds.max_response_time_ms as f64,
193 );
194 m
195 },
196 timestamp: crate::monitoring::current_timestamp(),
197 };
198
199 self.send_alert(alert).await?;
200 }
201 }
202
203 if let (Some(&auth_requests), Some(&auth_failures)) =
205 (metrics.get("auth_requests"), metrics.get("auth_failures"))
206 && auth_requests > 0
207 {
208 let error_rate = auth_failures as f64 / auth_requests as f64;
209
210 if error_rate > self.config.thresholds.error_rate_threshold {
211 let alert = Alert {
212 id: format!("high_error_rate_{}", crate::monitoring::current_timestamp()),
213 title: "High authentication error rate".to_string(),
214 message: format!(
215 "Authentication error rate is {:.1}%, which exceeds the threshold of {:.1}%",
216 error_rate * 100.0,
217 self.config.thresholds.error_rate_threshold * 100.0
218 ),
219 severity: AlertSeverity::Critical,
220 source: "authentication".to_string(),
221 metrics: {
222 let mut m = HashMap::new();
223 m.insert("error_rate".to_string(), error_rate);
224 m.insert(
225 "threshold".to_string(),
226 self.config.thresholds.error_rate_threshold,
227 );
228 m.insert("total_requests".to_string(), auth_requests as f64);
229 m.insert("failed_requests".to_string(), auth_failures as f64);
230 m
231 },
232 timestamp: crate::monitoring::current_timestamp(),
233 };
234
235 self.send_alert(alert).await?;
236 }
237 }
238
239 Ok(())
240 }
241
242 async fn send_alert(&mut self, alert: Alert) -> Result<()> {
244 if let Some(&last_alert_time) = self.recent_alerts.get(&alert.id) {
246 let current_time = crate::monitoring::current_timestamp();
247 if current_time - last_alert_time < self.config.thresholds.alert_cooldown_seconds {
248 tracing::debug!("Alert {} is in cooldown period, skipping", alert.id);
249 return Ok(());
250 }
251 }
252
253 self.recent_alerts.insert(alert.id.clone(), alert.timestamp);
255
256 for channel in &self.config.channels {
258 self.send_to_channel(&alert, channel).await?;
259 }
260
261 tracing::info!(
262 "Alert sent: {} - {} (Severity: {:?})",
263 alert.title,
264 alert.message,
265 alert.severity
266 );
267
268 Ok(())
269 }
270
271 async fn send_to_channel(&self, alert: &Alert, channel: &NotificationChannel) -> Result<()> {
273 match channel {
274 NotificationChannel::Email { recipients } => {
275 tracing::info!(
277 "EMAIL ALERT to {:?}: {} - {}",
278 recipients,
279 alert.title,
280 alert.message
281 );
282 }
283 NotificationChannel::Slack { webhook_url } => {
284 tracing::info!(
286 "SLACK ALERT to {}: {} - {}",
287 webhook_url,
288 alert.title,
289 alert.message
290 );
291 }
292 NotificationChannel::Teams { webhook_url } => {
293 tracing::info!(
295 "TEAMS ALERT to {}: {} - {}",
296 webhook_url,
297 alert.title,
298 alert.message
299 );
300 }
301 NotificationChannel::Webhook { url, headers: _ } => {
302 tracing::info!(
304 "WEBHOOK ALERT to {}: {} - {}",
305 url,
306 alert.title,
307 alert.message
308 );
309 }
310 NotificationChannel::Log { level } => match level.as_str() {
311 "error" => tracing::error!("ALERT: {} - {}", alert.title, alert.message),
312 "warn" => tracing::warn!("ALERT: {} - {}", alert.title, alert.message),
313 _ => tracing::info!("ALERT: {} - {}", alert.title, alert.message),
314 },
315 }
316
317 Ok(())
318 }
319
320 pub fn cleanup_alert_history(&mut self, max_age_seconds: u64) {
322 let current_time = crate::monitoring::current_timestamp();
323 self.recent_alerts
324 .retain(|_, &mut timestamp| current_time - timestamp < max_age_seconds);
325 }
326}
327
328impl Default for AlertConfig {
329 fn default() -> Self {
330 Self {
331 enabled: true,
332 thresholds: AlertThresholds::default(),
333 channels: vec![NotificationChannel::Log {
334 level: "warn".to_string(),
335 }],
336 }
337 }
338}