oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
Documentation
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
//! Security Audit and Hardening
//!
//! Provides comprehensive security scanning, audit logging, and vulnerability detection.
//! Implements OWASP Top 10 checks and security best practices.

use crate::error::{FusekiError, FusekiResult};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// Security audit manager
pub struct SecurityAuditManager {
    /// Audit configuration
    config: SecurityAuditConfig,
    /// Audit log
    audit_log: Arc<RwLock<Vec<AuditLogEntry>>>,
    /// Vulnerability scan results
    vulnerabilities: Arc<RwLock<Vec<Vulnerability>>>,
}

/// Security audit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityAuditConfig {
    /// Enable audit logging
    pub enabled: bool,
    /// Enable vulnerability scanning
    pub vulnerability_scanning: bool,
    /// Scan interval (hours)
    pub scan_interval_hours: u64,
    /// Enable OWASP Top 10 checks
    pub owasp_checks: bool,
    /// Enable compliance checks (GDPR, HIPAA, etc.)
    pub compliance_checks: bool,
    /// Maximum audit log entries
    pub max_log_entries: usize,
}

impl Default for SecurityAuditConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            vulnerability_scanning: true,
            scan_interval_hours: 24,
            owasp_checks: true,
            compliance_checks: false,
            max_log_entries: 10000,
        }
    }
}

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogEntry {
    pub timestamp: DateTime<Utc>,
    pub event_type: AuditEventType,
    pub severity: Severity,
    pub user: Option<String>,
    pub ip_address: Option<String>,
    pub resource: String,
    pub action: String,
    pub result: AuditResult,
    pub details: Option<String>,
}

/// Audit event types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditEventType {
    Authentication,
    Authorization,
    DataAccess,
    DataModification,
    ConfigurationChange,
    SecurityEvent,
    ComplianceEvent,
}

/// Audit result
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditResult {
    Success,
    Failure,
    Denied,
    Error,
}

/// Severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Severity {
    Info,
    Low,
    Medium,
    High,
    Critical,
}

/// Vulnerability information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vulnerability {
    pub id: String,
    pub severity: Severity,
    pub category: VulnerabilityCategory,
    pub title: String,
    pub description: String,
    pub affected_component: String,
    pub cve_id: Option<String>,
    pub remediation: String,
    pub discovered_at: DateTime<Utc>,
}

/// Vulnerability categories (OWASP Top 10)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VulnerabilityCategory {
    InjectionFlaws,
    BrokenAuthentication,
    SensitiveDataExposure,
    XmlExternalEntities,
    BrokenAccessControl,
    SecurityMisconfiguration,
    CrossSiteScripting,
    InsecureDeserialization,
    UsingComponentsWithKnownVulnerabilities,
    InsufficientLogging,
}

impl SecurityAuditManager {
    /// Create a new security audit manager
    pub fn new(config: SecurityAuditConfig) -> Self {
        Self {
            config,
            audit_log: Arc::new(RwLock::new(Vec::new())),
            vulnerabilities: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Log an audit event
    pub async fn log_event(&self, entry: AuditLogEntry) -> FusekiResult<()> {
        if !self.config.enabled {
            return Ok(());
        }

        let mut log = self.audit_log.write().await;

        // Enforce size limit
        if log.len() >= self.config.max_log_entries {
            log.remove(0); // Remove oldest entry
        }

        log.push(entry.clone());

        // Log to tracing based on severity
        match entry.severity {
            Severity::Critical | Severity::High => {
                warn!(
                    event_type = ?entry.event_type,
                    severity = ?entry.severity,
                    user = ?entry.user,
                    resource = %entry.resource,
                    action = %entry.action,
                    result = ?entry.result,
                    "Security audit event"
                );
            }
            _ => {
                debug!(
                    event_type = ?entry.event_type,
                    severity = ?entry.severity,
                    resource = %entry.resource,
                    "Security audit event"
                );
            }
        }

        Ok(())
    }

    /// Log authentication event
    pub async fn log_authentication(
        &self,
        user: &str,
        ip: &str,
        success: bool,
    ) -> FusekiResult<()> {
        let entry = AuditLogEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::Authentication,
            severity: if success {
                Severity::Info
            } else {
                Severity::Medium
            },
            user: Some(user.to_string()),
            ip_address: Some(ip.to_string()),
            resource: "authentication".to_string(),
            action: "login".to_string(),
            result: if success {
                AuditResult::Success
            } else {
                AuditResult::Failure
            },
            details: None,
        };

        self.log_event(entry).await
    }

    /// Log authorization event
    pub async fn log_authorization(
        &self,
        user: &str,
        resource: &str,
        action: &str,
        granted: bool,
    ) -> FusekiResult<()> {
        let entry = AuditLogEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::Authorization,
            severity: if granted {
                Severity::Info
            } else {
                Severity::Medium
            },
            user: Some(user.to_string()),
            ip_address: None,
            resource: resource.to_string(),
            action: action.to_string(),
            result: if granted {
                AuditResult::Success
            } else {
                AuditResult::Denied
            },
            details: None,
        };

        self.log_event(entry).await
    }

    /// Log data access event
    pub async fn log_data_access(
        &self,
        user: &str,
        dataset: &str,
        query: &str,
    ) -> FusekiResult<()> {
        let entry = AuditLogEntry {
            timestamp: Utc::now(),
            event_type: AuditEventType::DataAccess,
            severity: Severity::Info,
            user: Some(user.to_string()),
            ip_address: None,
            resource: dataset.to_string(),
            action: "query".to_string(),
            result: AuditResult::Success,
            details: Some(query.to_string()),
        };

        self.log_event(entry).await
    }

    /// Perform security scan
    pub async fn perform_security_scan(&self) -> FusekiResult<SecurityScanReport> {
        info!("Starting security vulnerability scan");

        let mut vulnerabilities = Vec::new();

        // OWASP Top 10 checks
        if self.config.owasp_checks {
            vulnerabilities.extend(self.check_owasp_top_10().await?);
        }

        // Configuration security checks
        vulnerabilities.extend(self.check_configuration_security().await?);

        // TLS/SSL checks
        vulnerabilities.extend(self.check_tls_security().await?);

        // Store vulnerabilities
        let mut vuln_store = self.vulnerabilities.write().await;
        vuln_store.clear();
        vuln_store.extend(vulnerabilities.clone());

        let report = SecurityScanReport {
            scan_time: Utc::now(),
            total_vulnerabilities: vulnerabilities.len(),
            critical_count: vulnerabilities
                .iter()
                .filter(|v| v.severity == Severity::Critical)
                .count(),
            high_count: vulnerabilities
                .iter()
                .filter(|v| v.severity == Severity::High)
                .count(),
            medium_count: vulnerabilities
                .iter()
                .filter(|v| v.severity == Severity::Medium)
                .count(),
            low_count: vulnerabilities
                .iter()
                .filter(|v| v.severity == Severity::Low)
                .count(),
            vulnerabilities,
        };

        info!(
            "Security scan complete: {} vulnerabilities found ({} critical, {} high)",
            report.total_vulnerabilities, report.critical_count, report.high_count
        );

        Ok(report)
    }

    /// Check OWASP Top 10 vulnerabilities
    async fn check_owasp_top_10(&self) -> FusekiResult<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();

        // A1: Injection - Check SPARQL injection protection
        // (In real implementation, scan query parsing for injection vulnerabilities)

        // A2: Broken Authentication - Check auth configuration
        // (Check password policies, session management, etc.)

        // A3: Sensitive Data Exposure - Check for exposed secrets
        // (Scan configuration for hardcoded credentials)

        // A5: Broken Access Control - Check RBAC implementation
        // (Verify permission checks are in place)

        // A6: Security Misconfiguration - Check default configs
        // (Check for default passwords, unnecessary services, etc.)

        // A10: Insufficient Logging - Check audit configuration
        if !self.config.enabled {
            vulnerabilities.push(Vulnerability {
                id: "AUDIT-001".to_string(),
                severity: Severity::Medium,
                category: VulnerabilityCategory::InsufficientLogging,
                title: "Audit logging disabled".to_string(),
                description: "Security audit logging is disabled".to_string(),
                affected_component: "SecurityAuditManager".to_string(),
                cve_id: None,
                remediation: "Enable audit logging in configuration".to_string(),
                discovered_at: Utc::now(),
            });
        }

        Ok(vulnerabilities)
    }

    /// Check configuration security
    async fn check_configuration_security(&self) -> FusekiResult<Vec<Vulnerability>> {
        let vulnerabilities = Vec::new();

        // Check for insecure defaults
        // Check for exposed sensitive data
        // Check for weak encryption settings

        Ok(vulnerabilities)
    }

    /// Check TLS/SSL security
    async fn check_tls_security(&self) -> FusekiResult<Vec<Vulnerability>> {
        let vulnerabilities = Vec::new();

        // Check TLS version (TLS 1.2+ required)
        // Check cipher suites (no weak ciphers)
        // Check certificate validity

        Ok(vulnerabilities)
    }

    /// Get audit log entries
    pub async fn get_audit_log(&self, filter: Option<AuditLogFilter>) -> Vec<AuditLogEntry> {
        let log = self.audit_log.read().await;

        if let Some(f) = filter {
            log.iter()
                .filter(|entry| {
                    if let Some(ref event_type) = f.event_type {
                        if entry.event_type != *event_type {
                            return false;
                        }
                    }
                    if let Some(ref severity) = f.min_severity {
                        if entry.severity < *severity {
                            return false;
                        }
                    }
                    if let Some(ref user) = f.user {
                        if entry.user.as_ref() != Some(user) {
                            return false;
                        }
                    }
                    true
                })
                .cloned()
                .collect()
        } else {
            log.clone()
        }
    }

    /// Get vulnerabilities
    pub async fn get_vulnerabilities(&self) -> Vec<Vulnerability> {
        self.vulnerabilities.read().await.clone()
    }
}

/// Audit log filter
#[derive(Debug, Clone)]
pub struct AuditLogFilter {
    pub event_type: Option<AuditEventType>,
    pub min_severity: Option<Severity>,
    pub user: Option<String>,
}

/// Security scan report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityScanReport {
    pub scan_time: DateTime<Utc>,
    pub total_vulnerabilities: usize,
    pub critical_count: usize,
    pub high_count: usize,
    pub medium_count: usize,
    pub low_count: usize,
    pub vulnerabilities: Vec<Vulnerability>,
}

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

    #[tokio::test]
    async fn test_audit_logging() {
        let config = SecurityAuditConfig::default();
        let manager = SecurityAuditManager::new(config);

        manager
            .log_authentication("testuser", "127.0.0.1", true)
            .await
            .unwrap();

        let log = manager.get_audit_log(None).await;
        assert_eq!(log.len(), 1);
        assert_eq!(log[0].event_type, AuditEventType::Authentication);
    }

    #[tokio::test]
    async fn test_audit_log_filter() {
        let config = SecurityAuditConfig::default();
        let manager = SecurityAuditManager::new(config);

        manager
            .log_authentication("user1", "127.0.0.1", true)
            .await
            .unwrap();
        manager
            .log_authorization("user2", "/dataset", "read", true)
            .await
            .unwrap();

        let filter = AuditLogFilter {
            event_type: Some(AuditEventType::Authentication),
            min_severity: None,
            user: None,
        };

        let filtered = manager.get_audit_log(Some(filter)).await;
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].event_type, AuditEventType::Authentication);
    }

    #[tokio::test]
    async fn test_security_scan() {
        let config = SecurityAuditConfig::default();
        let manager = SecurityAuditManager::new(config);

        let report = manager.perform_security_scan().await.unwrap();
        // Verify report structure is valid
        assert_eq!(
            report.total_vulnerabilities,
            report.critical_count + report.high_count + report.medium_count + report.low_count
        );
    }
}