rustmaninoff-engine 0.3.0

Checkov-compatible attribute policy engine
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
//! Checkov-compatible attribute policy engine.

mod eval;
mod policy;

pub use eval::{eval_cond, eval_graph_cond, Cond, Finding, Status};
pub use policy::{load_policy_dir, parse_policy, Policy, PolicyError, Severity};

use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use rayon::prelude::*;
use rustmaninoff_ir::{Edge, Framework, Resource};

/// Evaluate attribute policies against `resources`.
///
/// Graph (`connection`) policies are skipped here; use [`evaluate_graph`].
///
/// Results that share a check id and resource are collapsed to the worst status,
/// so a security group and its rule form can share one Checkov id.
pub fn evaluate(resources: &[Resource], policies: &[Policy]) -> Vec<Finding> {
    if resources.is_empty() || policies.is_empty() {
        return Vec::new();
    }
    let attribute_policies: Vec<&Policy> = policies
        .iter()
        .filter(|policy| !policy.definition.needs_graph())
        .collect();
    if attribute_policies.is_empty() {
        return Vec::new();
    }
    let index = PolicyIndex::build(&attribute_policies);
    let findings = if resources.len() < 32 {
        resources
            .iter()
            .flat_map(|resource| eval_resource(resource, &index))
            .collect()
    } else {
        resources
            .par_iter()
            .flat_map(|resource| eval_resource(resource, &index))
            .collect()
    };
    collapse(findings)
}

/// Evaluate graph (`connection`) policies using IR edges.
pub fn evaluate_graph(resources: &[Resource], edges: &[Edge], policies: &[Policy]) -> Vec<Finding> {
    if resources.is_empty() || policies.is_empty() {
        return Vec::new();
    }
    let graph_policies: Vec<&Policy> = policies
        .iter()
        .filter(|policy| policy.definition.needs_graph())
        .collect();
    if graph_policies.is_empty() {
        return Vec::new();
    }
    let index = PolicyIndex::build(&graph_policies);
    let findings = if resources.len() < 32 {
        resources
            .iter()
            .flat_map(|resource| eval_graph_resource(resource, resources, edges, &index))
            .collect()
    } else {
        resources
            .par_iter()
            .flat_map(|resource| eval_graph_resource(resource, resources, edges, &index))
            .collect()
    };
    collapse(findings)
}

struct PolicyIndex<'a> {
    exact: HashMap<Framework, HashMap<String, Vec<&'a Policy>>>,
    wild: HashMap<Framework, Vec<&'a Policy>>,
    unscoped_exact: HashMap<String, Vec<&'a Policy>>,
    unscoped_wild: Vec<&'a Policy>,
}

impl<'a> PolicyIndex<'a> {
    fn build(policies: &[&'a Policy]) -> Self {
        let mut index = Self {
            exact: HashMap::new(),
            wild: HashMap::new(),
            unscoped_exact: HashMap::new(),
            unscoped_wild: Vec::new(),
        };
        for policy in policies {
            let wild = policy
                .resource_types
                .iter()
                .any(|resource_type| resource_type == "*" || resource_type == "all");
            match policy.framework {
                Some(framework) => {
                    if wild {
                        index.wild.entry(framework).or_default().push(policy);
                    }
                    for resource_type in &policy.resource_types {
                        if resource_type == "*" || resource_type == "all" {
                            continue;
                        }
                        index
                            .exact
                            .entry(framework)
                            .or_default()
                            .entry(resource_type.clone())
                            .or_default()
                            .push(policy);
                    }
                }
                None => {
                    if wild {
                        index.unscoped_wild.push(policy);
                    }
                    for resource_type in &policy.resource_types {
                        if resource_type == "*" || resource_type == "all" {
                            continue;
                        }
                        index
                            .unscoped_exact
                            .entry(resource_type.clone())
                            .or_default()
                            .push(policy);
                    }
                }
            }
        }
        index
    }
}

fn eval_resource(resource: &Resource, index: &PolicyIndex<'_>) -> Vec<Finding> {
    visit_policies(resource, index, |policy| {
        eval_cond(&policy.definition, &resource.attributes)
    })
}

fn eval_graph_resource(
    resource: &Resource,
    resources: &[Resource],
    edges: &[Edge],
    index: &PolicyIndex<'_>,
) -> Vec<Finding> {
    visit_policies(resource, index, |policy| {
        eval_graph_cond(&policy.definition, resource, resources, edges)
    })
}

fn visit_policies(
    resource: &Resource,
    index: &PolicyIndex<'_>,
    mut eval: impl FnMut(&Policy) -> Status,
) -> Vec<Finding> {
    let shared = SharedIds {
        address: Arc::from(format!("{}.{}", resource.resource_type, resource.name)),
        file: Arc::from(resource.file.display().to_string()),
        resource_type: Arc::from(resource.resource_type.as_str()),
        resource_name: Arc::from(resource.name.as_str()),
        framework: Arc::from(resource.framework.as_str()),
    };
    let mut findings = Vec::new();
    let mut visit = |policy: &Policy| {
        if let Some(skip) = resource
            .skips
            .iter()
            .find(|skip| skip.check_id == policy.id.as_ref())
        {
            findings.push(make_finding(
                policy,
                Status::Skipped,
                &shared,
                resource,
                skip.reason.clone(),
            ));
            return;
        }
        let status = eval(policy);
        findings.push(make_finding(policy, status, &shared, resource, None));
    };
    if let Some(by_type) = index.exact.get(&resource.framework) {
        if let Some(list) = by_type.get(resource.resource_type.as_str()) {
            for policy in list {
                visit(policy);
            }
        }
    }
    if let Some(list) = index.wild.get(&resource.framework) {
        for policy in list {
            visit(policy);
        }
    }
    if let Some(list) = index.unscoped_exact.get(resource.resource_type.as_str()) {
        for policy in list {
            visit(policy);
        }
    }
    for policy in &index.unscoped_wild {
        visit(policy);
    }
    findings
}

struct SharedIds {
    address: Arc<str>,
    file: Arc<str>,
    resource_type: Arc<str>,
    resource_name: Arc<str>,
    framework: Arc<str>,
}

fn make_finding(
    policy: &Policy,
    status: Status,
    shared: &SharedIds,
    resource: &Resource,
    skip_reason: Option<String>,
) -> Finding {
    Finding {
        check_id: Arc::clone(&policy.id),
        check_name: Arc::clone(&policy.name),
        category: Arc::clone(&policy.category),
        severity: policy.severity,
        status,
        resource_address: Arc::clone(&shared.address),
        resource_type: Arc::clone(&shared.resource_type),
        resource_name: Arc::clone(&shared.resource_name),
        framework: Arc::clone(&shared.framework),
        file: Arc::clone(&shared.file),
        start_line: resource.start_line,
        end_line: resource.end_line,
        guidelines: policy.guidelines.clone(),
        skip_reason,
    }
}

type FindingKey = (Arc<str>, Arc<str>, Arc<str>);

fn collapse(findings: Vec<Finding>) -> Vec<Finding> {
    let mut grouped: BTreeMap<FindingKey, Finding> = BTreeMap::new();
    for finding in findings {
        let key = (
            Arc::clone(&finding.check_id),
            Arc::clone(&finding.file),
            Arc::clone(&finding.resource_address),
        );
        match grouped.get(&key) {
            Some(existing) if existing.status >= finding.status => {}
            _ => {
                grouped.insert(key, finding);
            }
        }
    }
    grouped.into_values().collect()
}

#[cfg(test)]
mod coverage;

#[cfg(test)]
mod tests {
    use super::*;
    use rustmaninoff_ir::{Framework, Value};
    use std::path::PathBuf;

    fn resource(resource_type: &str, name: &str, attributes: Value) -> Resource {
        Resource {
            framework: Framework::Terraform,
            kind: rustmaninoff_ir::ResourceKind::Managed,
            resource_type: resource_type.to_string(),
            name: name.to_string(),
            attributes,
            file: PathBuf::from("main.tf"),
            start_line: 1,
            end_line: 4,
            skips: Vec::new(),
        }
    }

    fn policy(yaml: &str) -> Policy {
        parse_policy(yaml).expect("policy")
    }

    #[test]
    fn equals_true_fails_when_false() {
        let policy = policy(
            r#"
metadata:
  id: CKV_AWS_3
  name: encrypted
  severity: HIGH
  framework: terraform
resource_types:
  - aws_ebs_volume
definition:
  cond_type: attribute
  attribute: encrypted
  operator: equals
  value: true
"#,
        );
        let bad = resource(
            "aws_ebs_volume",
            "bad",
            Value::Object(
                [("encrypted".into(), Value::Bool(false))]
                    .into_iter()
                    .collect(),
            ),
        );
        let findings = evaluate(&[bad], &[policy]);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].status, Status::Failed);
    }

    #[test]
    fn unknown_does_not_fail() {
        let policy = policy(
            r#"
metadata:
  id: CKV_AWS_3
  name: encrypted
  severity: HIGH
  framework: terraform
resource_types: [aws_ebs_volume]
definition:
  cond_type: attribute
  attribute: encrypted
  operator: equals
  value: true
"#,
        );
        let unknown = resource(
            "aws_ebs_volume",
            "x",
            Value::Object(
                [(
                    "encrypted".into(),
                    Value::Unknown {
                        expr: "var.x".into(),
                    },
                )]
                .into_iter()
                .collect(),
            ),
        );
        let findings = evaluate(&[unknown], &[policy]);
        assert_eq!(findings[0].status, Status::Unknown);
    }

    #[test]
    fn no_element_detects_open_ingress() {
        let policy = policy(
            r#"
metadata:
  id: CKV_AWS_24
  name: ssh
  severity: HIGH
  framework: terraform
resource_types: [aws_security_group]
definition:
  cond_type: no_element
  attribute: ingress
  where:
    and:
      - cond_type: attribute
        attribute: from_port
        operator: less_than_or_equal
        value: 22
      - cond_type: attribute
        attribute: to_port
        operator: greater_than_or_equal
        value: 22
      - cond_type: attribute
        attribute: cidr_blocks
        operator: contains
        value: "0.0.0.0/0"
"#,
        );
        let bad = resource(
            "aws_security_group",
            "bad",
            Value::from_json(serde_json::json!({
                "ingress": [{"from_port": 22, "to_port": 22, "cidr_blocks": ["0.0.0.0/0"]}]
            })),
        );
        let good = resource(
            "aws_security_group",
            "good",
            Value::from_json(serde_json::json!({
                "ingress": [{"from_port": 443, "to_port": 443, "cidr_blocks": ["0.0.0.0/0"]}]
            })),
        );
        assert_eq!(
            evaluate(&[bad], std::slice::from_ref(&policy))[0].status,
            Status::Failed
        );
        assert_eq!(evaluate(&[good], &[policy])[0].status, Status::Passed);
    }

    #[test]
    fn numeric_comparisons_agree_with_integers() {
        let policy = policy(
            r#"
metadata:
  id: NUM
  name: length
  framework: terraform
resource_types: [aws_iam_account_password_policy]
definition:
  cond_type: attribute
  attribute: minimum_password_length
  operator: greater_than_or_equal
  value: 14
"#,
        );
        for length in 0..40 {
            let resource = resource(
                "aws_iam_account_password_policy",
                "p",
                Value::from_json(serde_json::json!({"minimum_password_length": length})),
            );
            let status = evaluate(&[resource], std::slice::from_ref(&policy))[0].status;
            if length >= 14 {
                assert_eq!(status, Status::Passed, "{length}");
            } else {
                assert_eq!(status, Status::Failed, "{length}");
            }
        }
    }

    #[test]
    fn connection_requires_logging_neighbor() {
        use rustmaninoff_ir::{Edge, EdgeKind, ResourceRef};

        let policy = policy(
            r#"
metadata:
  id: CKV2_AWS_S3_LOGGING
  name: bucket has logging
  severity: MEDIUM
  framework: terraform
resource_types:
  - aws_s3_bucket
definition:
  cond_type: connection
  connected_resource_types:
    - aws_s3_bucket_logging
  operator: exists
"#,
        );
        let bucket = resource("aws_s3_bucket", "data", Value::Object(Default::default()));
        let logging = resource(
            "aws_s3_bucket_logging",
            "data",
            Value::Object(Default::default()),
        );
        let edge = Edge::new(
            ResourceRef::from_resource(&bucket),
            ResourceRef::from_resource(&logging),
            EdgeKind::References,
        )
        .via("bucket");
        assert!(evaluate(
            &[bucket.clone(), logging.clone()],
            std::slice::from_ref(&policy)
        )
        .is_empty());
        let failed = evaluate_graph(
            std::slice::from_ref(&bucket),
            &[],
            std::slice::from_ref(&policy),
        );
        assert_eq!(failed[0].status, Status::Failed);
        let passed = evaluate_graph(&[bucket, logging], &[edge], std::slice::from_ref(&policy));
        assert_eq!(passed[0].status, Status::Passed);
    }
}