sh-layer4 1.0.2

Continuum Layer 4: Integration
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
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
//! # Compliance Report
//!
//! 合规检查报告生成。

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::rules::{ComplianceRule, ComplianceStandard, RuleCategory, RuleSeverity};

/// 合规状态
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ComplianceStatus {
    /// 合规
    Compliant,
    /// 不合规
    NonCompliant,
    /// 部分合规
    PartiallyCompliant,
    /// 未检查
    #[default]
    NotChecked,
    /// 不适用
    NotApplicable,
}

/// 报告格式
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportFormat {
    /// JSON 格式
    Json,
    /// CSV 格式
    Csv,
    /// HTML 格式
    Html,
    /// Markdown 格式
    Markdown,
    /// PDF 格式
    Pdf,
}

/// 违规项
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Violation {
    /// 违规 ID
    pub id: String,
    /// 规则 ID
    pub rule_id: String,
    /// 规则名称
    pub rule_name: String,
    /// 严重性
    pub severity: RuleSeverity,
    /// 类别
    pub category: RuleCategory,
    /// 违规描述
    pub description: String,
    /// 发现时间
    pub detected_at: DateTime<Utc>,
    /// 资源类型
    pub resource_type: Option<String>,
    /// 资源 ID
    pub resource_id: Option<String>,
    /// 建议修复措施
    pub remediation: Option<String>,
    /// 相关证据
    pub evidence: Option<serde_json::Value>,
}

impl Violation {
    pub fn new(rule: &ComplianceRule, description: impl Into<String>) -> Self {
        Self {
            id: format!("VIO-{}", uuid::Uuid::new_v4()),
            rule_id: rule.id.clone(),
            rule_name: rule.name.clone(),
            severity: rule.severity,
            category: rule.category,
            description: description.into(),
            detected_at: Utc::now(),
            resource_type: None,
            resource_id: None,
            remediation: rule.description.clone(),
            evidence: None,
        }
    }

    pub fn with_resource(
        mut self,
        resource_type: impl Into<String>,
        resource_id: impl Into<String>,
    ) -> Self {
        self.resource_type = Some(resource_type.into());
        self.resource_id = Some(resource_id.into());
        self
    }

    pub fn with_remediation(mut self, remediation: impl Into<String>) -> Self {
        self.remediation = Some(remediation.into());
        self
    }

    pub fn with_evidence(mut self, evidence: serde_json::Value) -> Self {
        self.evidence = Some(evidence);
        self
    }
}

/// 检查结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckResult {
    /// 规则 ID
    pub rule_id: String,
    /// 规则名称
    pub rule_name: String,
    /// 检查状态
    pub status: ComplianceStatus,
    /// 检查时间
    pub checked_at: DateTime<Utc>,
    /// 违规项(如果有)
    pub violations: Vec<Violation>,
    /// 检查详情
    pub details: Option<String>,
}

impl CheckResult {
    pub fn compliant(rule: &ComplianceRule) -> Self {
        Self {
            rule_id: rule.id.clone(),
            rule_name: rule.name.clone(),
            status: ComplianceStatus::Compliant,
            checked_at: Utc::now(),
            violations: Vec::new(),
            details: None,
        }
    }

    pub fn non_compliant(rule: &ComplianceRule, violations: Vec<Violation>) -> Self {
        Self {
            rule_id: rule.id.clone(),
            rule_name: rule.name.clone(),
            status: if violations.is_empty() {
                ComplianceStatus::Compliant
            } else {
                ComplianceStatus::NonCompliant
            },
            checked_at: Utc::now(),
            violations,
            details: None,
        }
    }

    pub fn not_applicable(rule: &ComplianceRule) -> Self {
        Self {
            rule_id: rule.id.clone(),
            rule_name: rule.name.clone(),
            status: ComplianceStatus::NotApplicable,
            checked_at: Utc::now(),
            violations: Vec::new(),
            details: Some("Rule not applicable to this system".to_string()),
        }
    }

    pub fn with_details(mut self, details: impl Into<String>) -> Self {
        self.details = Some(details.into());
        self
    }
}

/// 合规报告
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplianceReport {
    /// 报告 ID
    pub id: String,
    /// 报告名称
    pub name: String,
    /// 生成时间
    pub generated_at: DateTime<Utc>,
    /// 检查的标准
    pub standards: Vec<ComplianceStandard>,
    /// 总体状态
    pub overall_status: ComplianceStatus,
    /// 合规分数 (0-100)
    pub compliance_score: f32,
    /// 检查结果
    pub results: Vec<CheckResult>,
    /// 所有违规
    pub violations: Vec<Violation>,
    /// 摘要统计
    pub summary: ReportSummary,
    /// 元数据
    pub metadata: HashMap<String, serde_json::Value>,
}

/// 报告摘要
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ReportSummary {
    /// 检查的规则总数
    pub total_rules: usize,
    /// 合规规则数
    pub compliant_rules: usize,
    /// 不合规规则数
    pub non_compliant_rules: usize,
    /// 不适用规则数
    pub not_applicable_rules: usize,
    /// 总违规数
    pub total_violations: usize,
    /// 严重违规数
    pub critical_violations: usize,
    /// 高危违规数
    pub high_violations: usize,
    /// 中危违规数
    pub medium_violations: usize,
    /// 低危违规数
    pub low_violations: usize,
}

impl ComplianceReport {
    /// 创建新的合规报告
    pub fn new(name: impl Into<String>, standards: Vec<ComplianceStandard>) -> Self {
        Self {
            id: format!("RPT-{}", uuid::Uuid::new_v4()),
            name: name.into(),
            generated_at: Utc::now(),
            standards,
            overall_status: ComplianceStatus::NotChecked,
            compliance_score: 0.0,
            results: Vec::new(),
            violations: Vec::new(),
            summary: ReportSummary::default(),
            metadata: HashMap::new(),
        }
    }

    /// 添加检查结果
    pub fn add_result(&mut self, result: CheckResult) {
        // 更新摘要统计
        self.summary.total_rules += 1;

        match result.status {
            ComplianceStatus::Compliant => self.summary.compliant_rules += 1,
            ComplianceStatus::NonCompliant => self.summary.non_compliant_rules += 1,
            ComplianceStatus::PartiallyCompliant => self.summary.non_compliant_rules += 1,
            ComplianceStatus::NotApplicable => self.summary.not_applicable_rules += 1,
            ComplianceStatus::NotChecked => {}
        }

        // 统计违规
        for violation in &result.violations {
            self.summary.total_violations += 1;
            match violation.severity {
                RuleSeverity::Critical => self.summary.critical_violations += 1,
                RuleSeverity::High => self.summary.high_violations += 1,
                RuleSeverity::Medium => self.summary.medium_violations += 1,
                RuleSeverity::Low => self.summary.low_violations += 1,
            }
        }

        self.violations.extend(result.violations.clone());
        self.results.push(result);
    }

    /// 计算合规分数
    pub fn calculate_score(&mut self) {
        if self.summary.total_rules == 0 {
            self.compliance_score = 100.0;
            self.overall_status = ComplianceStatus::NotChecked;
            return;
        }

        let applicable_rules = self.summary.total_rules - self.summary.not_applicable_rules;
        if applicable_rules == 0 {
            self.compliance_score = 100.0;
            self.overall_status = ComplianceStatus::NotApplicable;
            return;
        }

        // 基础分数 = 合规规则数 / 适用规则数 * 100
        let base_score = (self.summary.compliant_rules as f32 / applicable_rules as f32) * 100.0;

        // 根据违规严重性扣分
        let penalty = (self.summary.critical_violations as f32 * 10.0)
            + (self.summary.high_violations as f32 * 5.0)
            + (self.summary.medium_violations as f32 * 2.0)
            + (self.summary.low_violations as f32 * 0.5);

        self.compliance_score = (base_score - penalty).clamp(0.0, 100.0);

        // 确定总体状态
        self.overall_status = if self.compliance_score >= 90.0 {
            ComplianceStatus::Compliant
        } else if self.compliance_score >= 60.0 {
            ComplianceStatus::PartiallyCompliant
        } else {
            ComplianceStatus::NonCompliant
        };
    }

    /// 导出报告
    pub fn export(&self, format: ReportFormat) -> Vec<u8> {
        match format {
            ReportFormat::Json => serde_json::to_string_pretty(self)
                .unwrap_or_default()
                .into_bytes(),
            ReportFormat::Csv => self.export_csv(),
            ReportFormat::Html => self.export_html().into_bytes(),
            ReportFormat::Markdown => self.export_markdown().into_bytes(),
            ReportFormat::Pdf => {
                // PDF 需要额外的依赖库,这里返回 HTML 作为占位
                self.export_html().into_bytes()
            }
        }
    }

    fn export_csv(&self) -> Vec<u8> {
        let mut csv = String::from("Rule ID,Rule Name,Status,Violations Count,Severity\n");
        for result in &self.results {
            let status = format!("{:?}", result.status);
            let violation_count = result.violations.len();
            let severity = result
                .violations
                .first()
                .map(|v| format!("{:?}", v.severity))
                .unwrap_or_else(|| "N/A".to_string());
            csv.push_str(&format!(
                "{},{},{},{},{}\n",
                result.rule_id, result.rule_name, status, violation_count, severity
            ));
        }
        csv.into_bytes()
    }

    fn export_html(&self) -> String {
        format!(
            r#"<!DOCTYPE html>
<html>
<head>
    <title>Compliance Report - {}</title>
    <style>
        body {{ font-family: Arial, sans-serif; margin: 20px; }}
        h1 {{ color: #333; }}
        .score {{ font-size: 24px; font-weight: bold; margin: 20px 0; }}
        .compliant {{ color: green; }}
        .non-compliant {{ color: red; }}
        .partial {{ color: orange; }}
        table {{ border-collapse: collapse; width: 100%; }}
        th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
        th {{ background-color: #f2f2f2; }}
    </style>
</head>
<body>
    <h1>Compliance Report</h1>
    <p>Generated: {}</p>
    <p>Standards: {}</p>
    <div class="score">Compliance Score: {:.1}%</div>
    <h2>Summary</h2>
    <ul>
        <li>Total Rules: {}</li>
        <li>Compliant: {}</li>
        <li>Non-Compliant: {}</li>
        <li>Total Violations: {}</li>
    </ul>
    <h2>Results</h2>
    <table>
        <tr><th>Rule ID</th><th>Rule Name</th><th>Status</th><th>Violations</th></tr>
        {}
    </table>
</body>
</html>"#,
            self.name,
            self.generated_at.format("%Y-%m-%d %H:%M:%S UTC"),
            self.standards
                .iter()
                .map(|s| s.name())
                .collect::<Vec<_>>()
                .join(", "),
            self.compliance_score,
            self.summary.total_rules,
            self.summary.compliant_rules,
            self.summary.non_compliant_rules,
            self.summary.total_violations,
            self.results
                .iter()
                .map(|r| format!(
                    "<tr><td>{}</td><td>{}</td><td>{:?}</td><td>{}</td></tr>",
                    r.rule_id,
                    r.rule_name,
                    r.status,
                    r.violations.len()
                ))
                .collect::<Vec<_>>()
                .join("\n        ")
        )
    }

    fn export_markdown(&self) -> String {
        format!(
            r#"# Compliance Report: {}

**Generated:** {}
**Standards:** {}
**Score:** {:.1}%

## Summary

| Metric | Count |
|--------|-------|
| Total Rules | {} |
| Compliant | {} |
| Non-Compliant | {} |
| Total Violations | {} |
| Critical Violations | {} |

## Results

| Rule ID | Rule Name | Status | Violations |
|---------|-----------|--------|------------|
{}

## Violations

{}
"#,
            self.name,
            self.generated_at.format("%Y-%m-%d %H:%M:%S UTC"),
            self.standards
                .iter()
                .map(|s| s.name())
                .collect::<Vec<_>>()
                .join(", "),
            self.compliance_score,
            self.summary.total_rules,
            self.summary.compliant_rules,
            self.summary.non_compliant_rules,
            self.summary.total_violations,
            self.summary.critical_violations,
            self.results
                .iter()
                .map(|r| format!(
                    "| {} | {} | {:?} | {} |",
                    r.rule_id,
                    r.rule_name,
                    r.status,
                    r.violations.len()
                ))
                .collect::<Vec<_>>()
                .join("\n"),
            self.violations
                .iter()
                .map(|v| format!(
                    "- **{}**: {} (Severity: {:?})",
                    v.rule_id, v.description, v.severity
                ))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}

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

    #[test]
    fn test_violation_creation() {
        let rule = ComplianceRule::new("TEST-1", "Test", RuleCategory::Security);
        let violation = Violation::new(&rule, "Test violation");

        assert_eq!(violation.rule_id, "TEST-1");
        assert_eq!(violation.description, "Test violation");
    }

    #[test]
    fn test_check_result_compliant() {
        let rule = ComplianceRule::new("TEST-1", "Test", RuleCategory::Security);
        let result = CheckResult::compliant(&rule);

        assert_eq!(result.status, ComplianceStatus::Compliant);
        assert!(result.violations.is_empty());
    }

    #[test]
    fn test_compliance_report() {
        let mut report = ComplianceReport::new("Test Report", vec![ComplianceStandard::SOC2]);

        let rule = ComplianceRule::new("TEST-1", "Test", RuleCategory::Security);
        let result = CheckResult::compliant(&rule);

        report.add_result(result);
        report.calculate_score();

        assert_eq!(report.summary.total_rules, 1);
        assert_eq!(report.summary.compliant_rules, 1);
        assert!(report.compliance_score > 0.0);
    }

    #[test]
    fn test_export_json() {
        let report = ComplianceReport::new("Test", vec![ComplianceStandard::SOC2]);
        let json = report.export(ReportFormat::Json);
        assert!(!json.is_empty());
    }

    #[test]
    fn test_export_markdown() {
        let mut report = ComplianceReport::new("Test Report", vec![ComplianceStandard::SOC2]);
        let rule = ComplianceRule::new("TEST-1", "Test Rule", RuleCategory::Security);
        report.add_result(CheckResult::compliant(&rule));
        report.calculate_score();

        let md = report.export(ReportFormat::Markdown);
        let md_str = String::from_utf8(md).unwrap();
        assert!(md_str.contains("# Compliance Report"));
        assert!(md_str.contains("TEST-1"));
    }
}