forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
//! FJ-3206: Built-in compliance pack generators (NIST, SOC2, HIPAA).
//!
//! CIS Ubuntu pack lives in `cis_ubuntu_pack.rs`. This module provides
//! the remaining built-in packs and the dispatch logic.

use super::compliance_pack::{ComplianceCheck, CompliancePack, ComplianceRule};

/// Supported built-in pack names.
pub fn builtin_pack_names() -> &'static [&'static str] {
    &["cis-ubuntu-22", "nist-800-53", "soc2", "hipaa"]
}

/// Generate YAML for a built-in compliance pack by name.
///
/// Returns `Err` if the pack name is not recognized.
pub fn generate_builtin_pack_yaml(name: &str) -> Result<String, String> {
    let pack = generate_builtin_pack(name)?;
    serde_yaml_ng::to_string(&pack).map_err(|e| format!("serialize pack: {e}"))
}

/// Generate a built-in compliance pack by name.
pub fn generate_builtin_pack(name: &str) -> Result<CompliancePack, String> {
    match name {
        "cis-ubuntu-22" => Ok(super::cis_ubuntu_pack::cis_ubuntu_2204_pack()),
        "nist-800-53" => Ok(nist_800_53_pack()),
        "soc2" => Ok(soc2_pack()),
        "hipaa" => Ok(hipaa_pack()),
        _ => Err(format!(
            "unknown pack '{name}'. Available: {}",
            builtin_pack_names().join(", ")
        )),
    }
}

/// NIST 800-53 compliance pack.
fn nist_800_53_pack() -> CompliancePack {
    CompliancePack {
        name: "nist-800-53".into(),
        version: "1.0.0".into(),
        framework: "NIST".into(),
        description: Some("NIST SP 800-53 Rev.5 Security Controls".into()),
        rules: nist_rules(),
    }
}

fn nist_rules() -> Vec<ComplianceRule> {
    vec![
        pack_rule(
            "NIST-AC-3",
            "Access enforcement",
            "error",
            &["NIST AC-3"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "owner".into(),
            },
        ),
        pack_rule(
            "NIST-AC-6",
            "Least privilege",
            "error",
            &["NIST AC-6"],
            ComplianceCheck::Deny {
                resource_type: "service".into(),
                field: "owner".into(),
                pattern: "root".into(),
            },
        ),
        pack_rule(
            "NIST-CM-6",
            "Configuration settings",
            "warning",
            &["NIST CM-6"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "mode".into(),
            },
        ),
        pack_rule(
            "NIST-SC-28",
            "Protection at rest",
            "error",
            &["NIST SC-28"],
            ComplianceCheck::Deny {
                resource_type: "file".into(),
                field: "mode".into(),
                pattern: "777".into(),
            },
        ),
        pack_rule(
            "NIST-SI-7",
            "Integrity verification",
            "warning",
            &["NIST SI-7"],
            ComplianceCheck::Require {
                resource_type: "package".into(),
                field: "version".into(),
            },
        ),
        pack_rule(
            "NIST-AU-2",
            "Audit events",
            "warning",
            &["NIST AU-2"],
            ComplianceCheck::Require {
                resource_type: "service".into(),
                field: "enabled".into(),
            },
        ),
        pack_rule(
            "NIST-IA-5",
            "Authenticator management",
            "error",
            &["NIST IA-5"],
            ComplianceCheck::Deny {
                resource_type: "file".into(),
                field: "content".into(),
                pattern: "PasswordAuthentication yes".into(),
            },
        ),
        pack_rule(
            "NIST-SC-7",
            "Boundary protection",
            "warning",
            &["NIST SC-7"],
            ComplianceCheck::Require {
                resource_type: "service".into(),
                field: "firewall".into(),
            },
        ),
    ]
}

/// SOC2 compliance pack.
fn soc2_pack() -> CompliancePack {
    CompliancePack {
        name: "soc2".into(),
        version: "1.0.0".into(),
        framework: "SOC2".into(),
        description: Some("SOC 2 Type II Trust Services Criteria".into()),
        rules: soc2_rules(),
    }
}

fn soc2_rules() -> Vec<ComplianceRule> {
    vec![
        pack_rule(
            "SOC2-CC6.1",
            "Logical access security",
            "error",
            &["SOC2 CC6.1"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "owner".into(),
            },
        ),
        pack_rule(
            "SOC2-CC6.3",
            "Role-based access",
            "warning",
            &["SOC2 CC6.3"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "group".into(),
            },
        ),
        pack_rule(
            "SOC2-CC7.2",
            "System monitoring",
            "warning",
            &["SOC2 CC7.2"],
            ComplianceCheck::Require {
                resource_type: "service".into(),
                field: "enabled".into(),
            },
        ),
        pack_rule(
            "SOC2-CC8.1",
            "Change management",
            "warning",
            &["SOC2 CC8.1"],
            ComplianceCheck::Require {
                resource_type: "package".into(),
                field: "version".into(),
            },
        ),
        pack_rule(
            "SOC2-CC6.6",
            "Boundary protection",
            "error",
            &["SOC2 CC6.6"],
            ComplianceCheck::Deny {
                resource_type: "file".into(),
                field: "mode".into(),
                pattern: "777".into(),
            },
        ),
        pack_rule(
            "SOC2-CC6.7",
            "Data integrity",
            "warning",
            &["SOC2 CC6.7"],
            ComplianceCheck::RequireTag {
                tag: "environment".into(),
            },
        ),
    ]
}

/// HIPAA compliance pack.
fn hipaa_pack() -> CompliancePack {
    CompliancePack {
        name: "hipaa".into(),
        version: "1.0.0".into(),
        framework: "HIPAA".into(),
        description: Some("HIPAA Security Rule (45 CFR 164.312)".into()),
        rules: hipaa_rules(),
    }
}

fn hipaa_rules() -> Vec<ComplianceRule> {
    vec![
        pack_rule(
            "HIPAA-164.312a",
            "Access control",
            "error",
            &["HIPAA 164.312(a)"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "owner".into(),
            },
        ),
        pack_rule(
            "HIPAA-164.312b",
            "Audit controls",
            "error",
            &["HIPAA 164.312(b)"],
            ComplianceCheck::Require {
                resource_type: "service".into(),
                field: "enabled".into(),
            },
        ),
        pack_rule(
            "HIPAA-164.312c",
            "Integrity",
            "error",
            &["HIPAA 164.312(c)"],
            ComplianceCheck::Require {
                resource_type: "file".into(),
                field: "mode".into(),
            },
        ),
        pack_rule(
            "HIPAA-164.312d",
            "Authentication",
            "error",
            &["HIPAA 164.312(d)"],
            ComplianceCheck::Deny {
                resource_type: "file".into(),
                field: "content".into(),
                pattern: "PermitRootLogin yes".into(),
            },
        ),
        pack_rule(
            "HIPAA-164.312e",
            "Transmission security",
            "error",
            &["HIPAA 164.312(e)"],
            ComplianceCheck::Deny {
                resource_type: "file".into(),
                field: "mode".into(),
                pattern: "777".into(),
            },
        ),
        pack_rule(
            "HIPAA-164.308a",
            "Security management",
            "warning",
            &["HIPAA 164.308(a)"],
            ComplianceCheck::RequireTag {
                tag: "environment".into(),
            },
        ),
    ]
}

/// Helper to build a `ComplianceRule` for built-in packs.
fn pack_rule(
    id: &str,
    title: &str,
    severity: &str,
    controls: &[&str],
    check: ComplianceCheck,
) -> ComplianceRule {
    ComplianceRule {
        id: id.into(),
        title: title.into(),
        description: None,
        severity: severity.into(),
        controls: controls.iter().map(|s| s.to_string()).collect(),
        check,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::compliance_pack::evaluate_pack;
    use std::collections::HashMap;

    #[test]
    fn builtin_names_has_four() {
        assert_eq!(builtin_pack_names().len(), 4);
    }

    #[test]
    fn generate_cis_ubuntu() {
        let pack = generate_builtin_pack("cis-ubuntu-22").unwrap();
        assert_eq!(pack.name, "cis-ubuntu-22.04");
        assert!(!pack.rules.is_empty());
    }

    #[test]
    fn generate_nist() {
        let pack = generate_builtin_pack("nist-800-53").unwrap();
        assert_eq!(pack.name, "nist-800-53");
        assert_eq!(pack.framework, "NIST");
        assert_eq!(pack.rules.len(), 8);
    }

    #[test]
    fn generate_soc2() {
        let pack = generate_builtin_pack("soc2").unwrap();
        assert_eq!(pack.name, "soc2");
        assert_eq!(pack.framework, "SOC2");
        assert_eq!(pack.rules.len(), 6);
    }

    #[test]
    fn generate_hipaa() {
        let pack = generate_builtin_pack("hipaa").unwrap();
        assert_eq!(pack.name, "hipaa");
        assert_eq!(pack.framework, "HIPAA");
        assert_eq!(pack.rules.len(), 6);
    }

    #[test]
    fn generate_unknown_fails() {
        let result = generate_builtin_pack("unknown");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("unknown pack"));
    }

    #[test]
    fn yaml_roundtrip_nist() {
        let yaml = generate_builtin_pack_yaml("nist-800-53").unwrap();
        let pack: CompliancePack = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(pack.name, "nist-800-53");
        assert_eq!(pack.rules.len(), 8);
    }

    #[test]
    fn yaml_roundtrip_soc2() {
        let yaml = generate_builtin_pack_yaml("soc2").unwrap();
        let pack: CompliancePack = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(pack.name, "soc2");
    }

    #[test]
    fn yaml_roundtrip_hipaa() {
        let yaml = generate_builtin_pack_yaml("hipaa").unwrap();
        let pack: CompliancePack = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(pack.name, "hipaa");
    }

    #[test]
    fn nist_rule_ids_unique() {
        let pack = nist_800_53_pack();
        let mut seen = std::collections::HashSet::new();
        for rule in &pack.rules {
            assert!(seen.insert(&rule.id), "duplicate: {}", rule.id);
        }
    }

    #[test]
    fn soc2_rule_ids_unique() {
        let pack = soc2_pack();
        let mut seen = std::collections::HashSet::new();
        for rule in &pack.rules {
            assert!(seen.insert(&rule.id), "duplicate: {}", rule.id);
        }
    }

    #[test]
    fn hipaa_rule_ids_unique() {
        let pack = hipaa_pack();
        let mut seen = std::collections::HashSet::new();
        for rule in &pack.rules {
            assert!(seen.insert(&rule.id), "duplicate: {}", rule.id);
        }
    }

    #[test]
    fn all_packs_have_descriptions() {
        for name in builtin_pack_names() {
            let pack = generate_builtin_pack(name).unwrap();
            assert!(pack.description.is_some(), "pack {name} has no description");
        }
    }

    #[test]
    fn all_rules_have_controls() {
        for name in builtin_pack_names() {
            let pack = generate_builtin_pack(name).unwrap();
            for rule in &pack.rules {
                assert!(
                    !rule.controls.is_empty(),
                    "pack {name} rule {} has no controls",
                    rule.id
                );
            }
        }
    }

    #[test]
    fn nist_evaluate_passing() {
        let pack = nist_800_53_pack();
        let mut resources = HashMap::new();
        let mut fields = HashMap::new();
        fields.insert("type".into(), "file".into());
        fields.insert("owner".into(), "root".into());
        fields.insert("mode".into(), "0644".into());
        resources.insert("cfg".into(), fields);

        let result = evaluate_pack(&pack, &resources);
        assert!(result.passed_count() > 0);
    }

    #[test]
    fn soc2_evaluate_passing() {
        let pack = soc2_pack();
        let mut resources = HashMap::new();
        let mut fields = HashMap::new();
        fields.insert("type".into(), "file".into());
        fields.insert("owner".into(), "app".into());
        fields.insert("group".into(), "app".into());
        fields.insert("tags".into(), "environment".into());
        fields.insert("mode".into(), "0644".into());
        resources.insert("cfg".into(), fields);

        let result = evaluate_pack(&pack, &resources);
        assert!(result.passed_count() > 0);
    }

    #[test]
    fn hipaa_evaluate_deny_triggered() {
        let pack = hipaa_pack();
        let mut resources = HashMap::new();
        let mut fields = HashMap::new();
        fields.insert("type".into(), "file".into());
        fields.insert("mode".into(), "777".into());
        resources.insert("bad-file".into(), fields);

        let result = evaluate_pack(&pack, &resources);
        let failed_ids: Vec<_> = result
            .results
            .iter()
            .filter(|r| !r.passed)
            .map(|r| r.rule_id.as_str())
            .collect();
        assert!(
            failed_ids.contains(&"HIPAA-164.312e"),
            "expected HIPAA-164.312e to fail for mode 777"
        );
    }
}