mockforge-http 0.3.116

HTTP/REST protocol support for MockForge
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
//! HTTP handlers for compliance dashboard
//!
//! This module provides REST API endpoints for accessing compliance
//! dashboard data, gaps, alerts, and control effectiveness metrics.

use axum::{
    extract::{Path, Query, State},
    http::StatusCode,
    response::Json,
};
use mockforge_core::security::compliance_dashboard::{
    AlertType, ComplianceDashboardEngine, ComplianceStandard, GapSeverity,
};
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{error, info};

/// State for compliance dashboard handlers
#[derive(Clone)]
pub struct ComplianceDashboardState {
    /// Compliance dashboard engine
    pub engine: Arc<RwLock<ComplianceDashboardEngine>>,
}

/// Request to add a compliance gap
#[derive(Debug, Deserialize)]
pub struct AddGapRequest {
    /// Gap description
    pub description: String,
    /// Severity
    pub severity: GapSeverity,
    /// Standard
    pub standard: ComplianceStandard,
    /// Control ID (optional)
    pub control_id: Option<String>,
    /// Target remediation date (optional)
    pub target_remediation_date: Option<chrono::DateTime<chrono::Utc>>,
}

/// Request to add a compliance alert
#[derive(Debug, Deserialize)]
pub struct AddAlertRequest {
    /// Alert type
    pub alert_type: AlertType,
    /// Severity
    pub severity: GapSeverity,
    /// Message
    pub message: String,
    /// Standard (optional)
    pub standard: Option<ComplianceStandard>,
    /// Control ID (optional)
    pub control_id: Option<String>,
}

/// Request to update gap status
#[derive(Debug, Deserialize)]
pub struct UpdateGapStatusRequest {
    /// New status
    pub status: mockforge_core::security::compliance_dashboard::GapStatus,
}

/// Get dashboard data
///
/// GET /api/v1/compliance/dashboard
pub async fn get_dashboard(
    State(state): State<ComplianceDashboardState>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.read().await;
    let dashboard = engine.get_dashboard_data().await.map_err(|e| {
        error!("Failed to get dashboard data: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    serde_json::to_value(&dashboard).map(Json).map_err(|e| {
        error!("Failed to serialize dashboard: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })
}

/// Get all compliance gaps
///
/// GET /api/v1/compliance/gaps
pub async fn get_gaps(
    State(state): State<ComplianceDashboardState>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.read().await;

    let gaps = if let Some(severity_str) = params.get("severity") {
        let severity = match severity_str.as_str() {
            "critical" => GapSeverity::Critical,
            "high" => GapSeverity::High,
            "medium" => GapSeverity::Medium,
            "low" => GapSeverity::Low,
            _ => return Err(StatusCode::BAD_REQUEST),
        };
        engine.get_gaps_by_severity(severity).await.map_err(|e| {
            error!("Failed to get gaps by severity: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?
    } else {
        engine.get_all_gaps().await.map_err(|e| {
            error!("Failed to get all gaps: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?
    };

    serde_json::to_value(&gaps).map(Json).map_err(|e| {
        error!("Failed to serialize gaps: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })
}

/// Add a compliance gap
///
/// POST /api/v1/compliance/gaps
pub async fn add_gap(
    State(state): State<ComplianceDashboardState>,
    Json(request): Json<AddGapRequest>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let gap_id = format!(
        "GAP-{}",
        uuid::Uuid::new_v4().simple().to_string().get(..8).unwrap_or("00000000")
    );

    let engine = state.engine.write().await;
    engine
        .add_gap(
            gap_id.clone(),
            request.description,
            request.severity,
            request.standard,
            request.control_id,
            request.target_remediation_date,
        )
        .await
        .map_err(|e| {
            error!("Failed to add gap: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    info!("Compliance gap added: {}", gap_id);

    Ok(Json(serde_json::json!({
        "gap_id": gap_id,
        "status": "created"
    })))
}

/// Update gap status
///
/// PATCH /api/v1/compliance/gaps/{gap_id}/status
pub async fn update_gap_status(
    State(state): State<ComplianceDashboardState>,
    Path(gap_id): Path<String>,
    Json(request): Json<UpdateGapStatusRequest>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.write().await;
    engine.update_gap_status(&gap_id, request.status).await.map_err(|e| {
        error!("Failed to update gap status: {}", e);
        StatusCode::BAD_REQUEST
    })?;

    info!("Gap status updated: {}", gap_id);

    Ok(Json(serde_json::json!({
        "gap_id": gap_id,
        "status": "updated"
    })))
}

/// Get all compliance alerts
///
/// GET /api/v1/compliance/alerts
pub async fn get_alerts(
    State(state): State<ComplianceDashboardState>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.read().await;

    let alerts = if let Some(severity_str) = params.get("severity") {
        let severity = match severity_str.as_str() {
            "critical" => GapSeverity::Critical,
            "high" => GapSeverity::High,
            "medium" => GapSeverity::Medium,
            "low" => GapSeverity::Low,
            _ => return Err(StatusCode::BAD_REQUEST),
        };
        engine.get_alerts_by_severity(severity).await.map_err(|e| {
            error!("Failed to get alerts by severity: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?
    } else {
        engine.get_all_alerts().await.map_err(|e| {
            error!("Failed to get all alerts: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?
    };

    serde_json::to_value(&alerts).map(Json).map_err(|e| {
        error!("Failed to serialize alerts: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })
}

/// Add a compliance alert
///
/// POST /api/v1/compliance/alerts
pub async fn add_alert(
    State(state): State<ComplianceDashboardState>,
    Json(request): Json<AddAlertRequest>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let alert_id = format!(
        "ALERT-{}",
        uuid::Uuid::new_v4().simple().to_string().get(..8).unwrap_or("00000000")
    );

    let engine = state.engine.write().await;
    engine
        .add_alert(
            alert_id.clone(),
            request.alert_type,
            request.severity,
            request.message,
            request.standard,
            request.control_id,
        )
        .await
        .map_err(|e| {
            error!("Failed to add alert: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

    info!("Compliance alert added: {}", alert_id);

    Ok(Json(serde_json::json!({
        "alert_id": alert_id,
        "status": "created"
    })))
}

/// Get compliance status
///
/// GET /api/v1/compliance/status
pub async fn get_compliance_status(
    State(state): State<ComplianceDashboardState>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.read().await;
    let dashboard = engine.get_dashboard_data().await.map_err(|e| {
        error!("Failed to get dashboard data: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Extract control effectiveness by area
    let mut by_area = serde_json::Map::new();
    for (category, effectiveness) in &dashboard.control_effectiveness {
        let category_name = match category {
            mockforge_core::security::compliance_dashboard::ControlCategory::AccessControl => {
                "access_control"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::Encryption => {
                "encryption"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::Monitoring => {
                "monitoring"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::ChangeManagement => {
                "change_management"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::IncidentResponse => {
                "incident_response"
            }
        };
        by_area.insert(
            category_name.to_string(),
            serde_json::Value::Number(effectiveness.effectiveness.into()),
        );
    }

    Ok(Json(serde_json::json!({
        "overall_compliance": dashboard.overall_compliance,
        "soc2_compliance": dashboard.soc2_compliance,
        "iso27001_compliance": dashboard.iso27001_compliance,
        "by_area": by_area,
        "gaps": dashboard.gaps.total,
        "remediation_in_progress": dashboard.remediation.in_progress
    })))
}

/// Get compliance report
///
/// GET /api/v1/compliance/reports/{period}
pub async fn get_compliance_report(
    State(state): State<ComplianceDashboardState>,
    Path(_period): Path<String>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let engine = state.engine.read().await;
    let dashboard = engine.get_dashboard_data().await.map_err(|e| {
        error!("Failed to get dashboard data: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Extract report period from query or use provided period
    let report_period = params
        .get("month")
        .or_else(|| params.get("period"))
        .cloned()
        .unwrap_or_else(|| {
            // Default to current month
            let now = chrono::Utc::now();
            now.format("%Y-%m").to_string()
        });

    // Get all gaps for recommendations
    let all_gaps = engine.get_all_gaps().await.map_err(|e| {
        error!("Failed to get gaps: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Generate recommendations based on gaps
    let mut recommendations = Vec::new();
    for gap in &all_gaps {
        match gap.severity {
            GapSeverity::Critical => {
                recommendations.push(format!("Urgent: {}", gap.description));
            }
            GapSeverity::High => {
                recommendations.push(format!("High priority: {}", gap.description));
            }
            _ => {}
        }
    }

    // Add generic recommendations if no gaps
    if recommendations.is_empty() {
        if dashboard
            .control_effectiveness
            .get(&mockforge_core::security::compliance_dashboard::ControlCategory::ChangeManagement)
            .map(|e| e.effectiveness < 95)
            .unwrap_or(false)
        {
            recommendations.push("Enhance change management procedures".to_string());
        }
        if dashboard
            .control_effectiveness
            .get(&mockforge_core::security::compliance_dashboard::ControlCategory::IncidentResponse)
            .map(|e| e.effectiveness < 95)
            .unwrap_or(false)
        {
            recommendations.push("Improve incident response time".to_string());
        }
    }

    // Format gaps for report
    let gaps_summary: Vec<serde_json::Value> = all_gaps
        .iter()
        .take(10)
        .map(|gap| {
            serde_json::json!({
                "id": gap.gap_id,
                "severity": format!("{:?}", gap.severity).to_lowercase(),
                "remediation_status": format!("{:?}", gap.status).to_lowercase()
            })
        })
        .collect();

    // Format control effectiveness
    let mut control_effectiveness = serde_json::Map::new();
    for (category, effectiveness) in &dashboard.control_effectiveness {
        let category_name = match category {
            mockforge_core::security::compliance_dashboard::ControlCategory::AccessControl => {
                "access_control"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::Encryption => {
                "encryption"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::Monitoring => {
                "monitoring"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::ChangeManagement => {
                "change_management"
            }
            mockforge_core::security::compliance_dashboard::ControlCategory::IncidentResponse => {
                "incident_response"
            }
        };
        control_effectiveness.insert(
            category_name.to_string(),
            serde_json::Value::Number(effectiveness.effectiveness.into()),
        );
    }

    Ok(Json(serde_json::json!({
        "report_period": report_period,
        "overall_compliance": dashboard.overall_compliance,
        "control_effectiveness": control_effectiveness,
        "gaps": gaps_summary,
        "recommendations": recommendations
    })))
}

/// Create compliance dashboard router
pub fn compliance_dashboard_router(state: ComplianceDashboardState) -> axum::Router {
    use axum::routing::{get, patch, post};

    axum::Router::new()
        .route("/dashboard", get(get_dashboard))
        .route("/status", get(get_compliance_status))
        .route("/reports/:period", get(get_compliance_report))
        .route("/gaps", get(get_gaps))
        .route("/gaps", post(add_gap))
        .route("/gaps/{gap_id}/status", patch(update_gap_status))
        .route("/alerts", get(get_alerts))
        .route("/alerts", post(add_alert))
        .with_state(state)
}