fortress-api-server 1.0.1

REST API server for Fortress secure database system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Health check system for the Fortress server
//!
//! This module provides comprehensive health monitoring for all server components,
//! including database connections, external services, and internal metrics.

use crate::config::FeatureFlags;
use crate::models::{HealthResponse, HealthStatus, ComponentHealth};
use crate::error::{ServerError, ServerResult};
use chrono::{DateTime, Utc};
use fortress_core::encryption::PerformanceProfile;
use fortress_core::audit::{AuditConfig, AuditEntry, AuditEventType, SecurityLevel, EventOutcome, AuditStatistics, AuditLogger, AuditQuery, IntegrityReport};
use fortress_core::encryption::{EncryptionAlgorithm, Aegis256};
use fortress_core::key::{SecureKey, InMemoryKeyManager, KeyManager, KeyMetadata};
use fortress_core::error::FortressError;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{info, error};
use uuid::Uuid;

/// Simple in-memory audit logger for health checks
struct InMemoryAuditLogger;

impl InMemoryAuditLogger {
    fn new(_config: AuditConfig) -> Self {
        Self
    }
}

#[async_trait::async_trait]
impl AuditLogger for InMemoryAuditLogger {
    fn log(&mut self, _entry: AuditEntry) -> Result<(), FortressError> {
        // Just return success for health check
        Ok(())
    }
    
    fn query(&self, _query: AuditQuery) -> Result<Vec<AuditEntry>, FortressError> {
        // Return empty results for health check
        Ok(vec![])
    }
    
    fn verify_integrity(&self) -> Result<IntegrityReport, FortressError> {
        // Return a simple integrity report
        Ok(IntegrityReport {
            total_entries: 0,
            valid_entries: 0,
            violations: 0,
            violation_details: vec![],
        })
    }
    
    fn get_statistics(&self) -> Result<AuditStatistics, FortressError> {
        // Return empty statistics for health check
        Ok(AuditStatistics {
            total_entries: 0,
            entries_by_event_type: HashMap::new(),
            entries_by_security_level: HashMap::new(),
            entries_by_outcome: HashMap::new(),
            date_range: (None, None),
            log_size: 0,
        })
    }
    
    fn rotate_logs(&self) -> Result<(), FortressError> {
        // Just return success for health check
        Ok(())
    }
}

/// Health checker for monitoring server components
#[derive(Clone)]
pub struct HealthChecker {
    /// Component health registry
    components: Arc<RwLock<HashMap<String, ComponentStatus>>>,
    /// Server start time
    start_time: Instant,
    /// Feature flags
    features: FeatureFlags,
}

/// Internal component status
#[derive(Clone, Debug)]
struct ComponentStatus {
    /// Current health status
    status: HealthStatus,
    /// Status message
    message: Option<String>,
    /// Last check timestamp
    last_check: DateTime<Utc>,
    /// Response time in milliseconds
    response_time_ms: Option<u64>,
    /// Consecutive failures
    consecutive_failures: u32,
}

impl HealthChecker {
    /// Create a new health checker
    pub fn new(features: FeatureFlags) -> Self {
        Self {
            components: Arc::new(RwLock::new(HashMap::new())),
            start_time: Instant::now(),
            features,
        }
    }

    /// Get overall health status
    pub async fn get_health(&self) -> HealthResponse {
        let components = self.components.read().await;
        let mut component_health = HashMap::new();

        let mut overall_status = HealthStatus::Healthy;

        for (name, status) in components.iter() {
            let health = ComponentHealth {
                status: status.status.clone(),
                message: status.message.clone(),
                response_time_ms: status.response_time_ms,
                last_check: status.last_check,
            };

            component_health.insert(name.clone(), health);

            // Determine overall status
            match status.status {
                HealthStatus::Unhealthy => {
                    overall_status = HealthStatus::Unhealthy;
                }
                HealthStatus::Degraded if overall_status == HealthStatus::Healthy => {
                    overall_status = HealthStatus::Degraded;
                }
                _ => {}
            }
        }

        HealthResponse {
            status: overall_status,
            version: crate::VERSION.to_string(),
            uptime: self.start_time.elapsed().as_secs(),
            components: component_health,
            timestamp: Utc::now(),
        }
    }

    /// Check a component's health
    pub async fn check_component<F, Fut>(&self, name: &str, checker: F) -> ServerResult<()>
    where
        F: FnOnce() -> Fut + Send,
        Fut: std::future::Future<Output = ServerResult<()>> + Send,
    {
        let start = Instant::now();
        let result = checker().await;
        let response_time = start.elapsed().as_millis() as u64;

        let mut components = self.components.write().await;
        let status = components.entry(name.to_string()).or_insert_with(|| ComponentStatus {
            status: HealthStatus::Healthy,
            message: None,
            last_check: Utc::now(),
            response_time_ms: None,
            consecutive_failures: 0,
        });

        status.last_check = Utc::now();
        status.response_time_ms = Some(response_time);

        match result {
            Ok(()) => {
                status.status = HealthStatus::Healthy;
                status.message = None;
                status.consecutive_failures = 0;
                
                if response_time > 1000 {
                    status.status = HealthStatus::Degraded;
                    status.message = Some("High response time".to_string());
                }
                
                info!(
                    component = %name,
                    response_time_ms = response_time,
                    status = ?status.status,
                    "Health check completed"
                );
            }
            Err(e) => {
                status.consecutive_failures += 1;
                status.message = Some(e.to_string());
                
                if status.consecutive_failures >= 3 {
                    status.status = HealthStatus::Unhealthy;
                } else {
                    status.status = HealthStatus::Degraded;
                }
                
                error!(
                    component = %name,
                    error = %e,
                    consecutive_failures = status.consecutive_failures,
                    "Health check failed"
                );
            }
        }

        Ok(())
    }

    /// Set component health manually
    pub async fn set_component_health(
        &self,
        name: &str,
        status: HealthStatus,
        message: Option<String>,
    ) {
        let mut components = self.components.write().await;
        
        let component_status = components.entry(name.to_string()).or_insert_with(|| ComponentStatus {
            status: HealthStatus::Healthy,
            message: None,
            last_check: Utc::now(),
            response_time_ms: None,
            consecutive_failures: 0,
        });

        component_status.status = status;
        component_status.message = message;
        component_status.last_check = Utc::now();
    }

    /// Run comprehensive health checks
    pub async fn run_all_checks(&self) {
        info!("Starting comprehensive health checks");

        // Check core Fortress components
        if self.features.auth_enabled {
            let _ = self.check_component("auth", || async {
                // Check authentication system
                self.check_auth_system().await
            }).await;
        }

        if self.features.field_encryption {
            let _ = self.check_component("encryption", || async {
                // Check encryption system
                self.check_encryption_system().await
            }).await;
        }

        // Check storage backend
        let _ = self.check_component("storage", || async {
            self.check_storage_backend().await
        }).await;

        // Check key management
        let _ = self.check_component("key_management", || async {
            self.check_key_management().await
        }).await;

        // Check audit logging
        if self.features.audit_enabled {
            let _ = self.check_component("audit_logging", || async {
                self.check_audit_logging().await
            }).await;
        }

        // Check metrics collection
        if self.features.metrics_enabled {
            let _ = self.check_component("metrics", || async {
                self.check_metrics_collection().await
            }).await;
        }

        info!("Comprehensive health checks completed");
    }

    /// Check authentication system
    async fn check_auth_system(&self) -> ServerResult<()> {
        // In a real implementation, this would check JWT validation,
        // user database connectivity, etc.
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(())
    }

    /// Check encryption system
    async fn check_encryption_system(&self) -> ServerResult<()> {
        // Test encryption/decryption with default algorithm
        let algorithm = Aegis256::new();
        let key = SecureKey::generate(algorithm.key_size())
            .expect("Failed to generate secure key");
        
        let plaintext = b"health_check_test";
        let ciphertext = algorithm.encrypt(plaintext, key.as_bytes())?;
        let decrypted = algorithm.decrypt(&ciphertext, key.as_bytes())?;
        
        if plaintext != &decrypted[..] {
            return Err(ServerError::internal("Encryption test failed"));
        }
        
        Ok(())
    }

    /// Check storage backend
    async fn check_storage_backend(&self) -> ServerResult<()> {
        // In a real implementation, this would check database connectivity
        // For now, we'll simulate a storage check
        tokio::time::sleep(Duration::from_millis(50)).await;
        Ok(())
    }

    /// Check key management
    async fn check_key_management(&self) -> ServerResult<()> {
        // Test key generation
        let key_manager = InMemoryKeyManager::new();
        let algorithm = Aegis256::new();
        let key = SecureKey::generate(algorithm.key_size())
            .expect("Failed to generate secure key");
        
        // Store the key
        let key_id = "test_key".to_string();
        let now = Utc::now();
        let metadata = KeyMetadata::new(
            key_id.clone(),
            "aegis256".to_string(),
            1,
            now,
            now + chrono::Duration::days(365),
            "test".to_string(),
            PerformanceProfile::Balanced,
        );
        key_manager.store_key(&key_id, &key, &metadata).await?;
        
        // Retrieve the key
        let (retrieved_key, _retrieved_metadata) = key_manager.retrieve_key(&key_id).await?;
        
        if retrieved_key.as_bytes() != key.as_bytes() {
            return Err(ServerError::internal("Key retrieval failed"));
        }
        
        Ok(())
    }

    /// Check audit logging
    async fn check_audit_logging(&self) -> ServerResult<()> {
        // Test audit log creation
        let audit_config = AuditConfig::default();
        let mut audit_logger = InMemoryAuditLogger::new(audit_config);
        
        let entry = AuditEntry {
            id: Uuid::new_v4().to_string(),
            timestamp: Utc::now().timestamp_millis() as u64,
            event_type: AuditEventType::DataAccess,
            security_level: SecurityLevel::Medium,
            principal: Some("health_check".to_string()),
            resource: Some("test".to_string()),
            action: "health_check".to_string(),
            outcome: EventOutcome::Success,
            metadata: HashMap::new(),
            previous_hash: None,
            current_hash: "test_hash".to_string(),
            signature: "test_signature".to_string(),
        };
        
        audit_logger.log(entry)?;
        
        Ok(())
    }

    /// Check metrics collection
    async fn check_metrics_collection(&self) -> ServerResult<()> {
        // Test metrics collection
        metrics::counter!("test_counter", 1);
        metrics::gauge!("test_gauge", 42.0);
        metrics::histogram!("test_histogram", 100.0);
        
        Ok(())
    }

    /// Get component-specific health
    pub async fn get_component_health(&self, component_name: &str) -> Option<ComponentHealth> {
        let components = self.components.read().await;
        components.get(component_name).map(|status| ComponentHealth {
            status: status.status.clone(),
            message: status.message.clone(),
            response_time_ms: status.response_time_ms,
            last_check: status.last_check,
        })
    }

    /// Reset component health
    pub async fn reset_component_health(&self, component_name: &str) {
        let mut components = self.components.write().await;
        components.remove(component_name);
    }

    /// Get server uptime
    pub fn uptime(&self) -> Duration {
        self.start_time.elapsed()
    }
}

/// Health check trait for custom components
#[async_trait::async_trait]
pub trait HealthCheck: Send + Sync {
    /// Check the health of the component
    async fn check_health(&self) -> ServerResult<()>;
    
    /// Get component name
    fn name(&self) -> &str;
}

/// Registry for custom health checks
pub struct HealthCheckRegistry {
    checks: Arc<RwLock<HashMap<String, Arc<dyn HealthCheck>>>>,
}

impl HealthCheckRegistry {
    /// Create a new health check registry
    pub fn new() -> Self {
        Self {
            checks: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Register a health check
    pub async fn register(&self, check: Arc<dyn HealthCheck>) {
        let mut checks = self.checks.write().await;
        checks.insert(check.name().to_string(), check);
    }

    /// Unregister a health check
    pub async fn unregister(&self, name: &str) {
        let mut checks = self.checks.write().await;
        checks.remove(name);
    }

    /// Run all registered health checks
    pub async fn run_all_checks(&self, health_checker: &HealthChecker) {
        let checks = self.checks.read().await;
        
        for (name, check) in checks.iter() {
            let _ = health_checker.check_component(name, || async {
                check.check_health().await
            }).await;
        }
    }
}

impl Default for HealthCheckRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::FeatureFlags;

    #[tokio::test]
    async fn test_health_checker_creation() {
        let features = FeatureFlags::default();
        let health_checker = HealthChecker::new(features);
        
        let health = health_checker.get_health().await;
        assert_eq!(health.status, HealthStatus::Healthy);
        assert!(!health.components.is_empty());
    }

    #[tokio::test]
    async fn test_component_health_check() {
        let features = FeatureFlags::default();
        let health_checker = HealthChecker::new(features);
        
        // Test successful check
        health_checker.check_component("test_component", || async {
            Ok(())
        }).await.unwrap();
        
        let health = health_checker.get_health().await;
        assert!(health.components.contains_key("test_component"));
        assert_eq!(health.components["test_component"].status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_component_health_check_failure() {
        let features = FeatureFlags::default();
        let health_checker = HealthChecker::new(features);
        
        // Test failed check
        health_checker.check_component("failing_component", || async {
            Err(ServerError::internal("Test failure"))
        }).await;
        
        let health = health_checker.get_health().await;
        assert!(health.components.contains_key("failing_component"));
        assert_eq!(health.components["failing_component"].status, HealthStatus::Degraded);
    }

    #[tokio::test]
    async fn test_manual_component_health() {
        let features = FeatureFlags::default();
        let health_checker = HealthChecker::new(features);
        
        health_checker.set_component_health(
            "manual_component",
            HealthStatus::Degraded,
            Some("Manual test".to_string()),
        ).await;
        
        let health = health_checker.get_health().await;
        assert!(health.components.contains_key("manual_component"));
        assert_eq!(health.components["manual_component"].status, HealthStatus::Degraded);
        assert_eq!(health.components["manual_component"].message, Some("Manual test".to_string()));
    }

    #[tokio::test]
    async fn test_health_check_registry() {
        let registry = HealthCheckRegistry::new();
        
        struct TestHealthCheck;
        
        #[async_trait::async_trait]
        impl HealthCheck for TestHealthCheck {
            async fn check_health(&self) -> ServerResult<()> {
                Ok(())
            }
            
            fn name(&self) -> &str {
                "test_check"
            }
        }
        
        let check = Arc::new(TestHealthCheck);
        registry.register(check.clone()).await;
        
        let features = FeatureFlags::default();
        let health_checker = HealthChecker::new(features);
        registry.run_all_checks(&health_checker).await;
        
        let health = health_checker.get_health().await;
        assert!(health.components.contains_key("test_check"));
    }
}