cfn-guard 3.2.0

AWS CloudFormation Guard is an open-source general-purpose policy-as-code evaluation tool. It provides developers with a simple-to-use, yet powerful and expressive domain-specific language (DSL) to define policies and enables developers to validate JSON- or YAML- formatted structured data with those policies.
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
pub mod test;
pub mod validate;

use std::{fmt::Display, rc::Rc, time::Instant};

use quick_xml::{
    events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event},
    Writer,
};
use serde::{Deserialize, Serialize};

use crate::{
    commands::{
        reporters::test::structured::TestResult, validate::DataFile, ERROR_STATUS_CODE,
        FAILURE_STATUS_CODE,
    },
    rules::{
        self,
        eval::eval_rules_file,
        eval_context::{root_scope, simplified_json_from_root, Messages},
        exprs::RulesFile,
        Status,
    },
};

pub struct JunitReport<'report> {
    pub name: &'report str,
    pub test_suites: Vec<TestSuite<'report>>,
    pub failures: usize,
    pub errors: usize,
    pub tests: usize,
    pub duration: u128,
}

impl<'report> From<&'report Vec<TestResult>> for JunitReport<'report> {
    fn from(value: &'report Vec<TestResult>) -> Self {
        let mut errors = 0;
        let mut failures = 0;
        let mut tests = 0;
        let mut time = 0;

        let test_suites = value.iter().fold(vec![], |mut acc, result| {
            let suite = result.build_test_suite();

            time += suite.time;
            errors += suite.errors;
            failures += suite.failures;
            tests += suite.test_cases.len();

            acc.push(suite);
            acc
        });

        JunitReport {
            name: "cfn-guard test report",
            test_suites,
            failures,
            errors,
            tests,
            duration: time,
        }
    }
}

impl<'report> JunitReport<'report> {
    pub fn serialize(
        &self,
        writer: &'report mut crate::utils::writer::Writer,
    ) -> crate::rules::Result<()> {
        let mut writer = quick_xml::Writer::new_with_indent(writer, b' ', 4);
        let decl = BytesDecl::new("1.0", Some("UTF-8"), None);

        writer.write_event(Event::Decl(decl))?;
        let suites = EventType::TestSuites(TestSuites {
            name: self.name,
            tests: self.tests,
            failures: self.failures,
            errors: self.errors,
            time: self.duration,
            test_suites: &self.test_suites,
        });

        suites.serialize(&mut writer)?;
        Ok(writer.write_indent()?)
    }
}

struct JunitReporter<'reporter> {
    rules: Vec<(RulesFile<'reporter>, &'reporter str)>,
    data: Vec<DataFile>,
    writer: &'reporter mut crate::utils::writer::Writer,
    exit_code: i32,
}

impl<'reporter> JunitReporter<'reporter> {
    /// Update exit code only if code takes more precedence than current exit code
    fn update_exit_code(&mut self, code: i32) {
        if code == ERROR_STATUS_CODE
            || code == FAILURE_STATUS_CODE && self.exit_code != ERROR_STATUS_CODE
        {
            self.exit_code = code;
        }
    }
}

fn get_test_case<'rule>(
    data: &DataFile,
    rule: &RulesFile<'_>,
    name: &'rule str,
) -> crate::rules::Result<TestCase<'rule>> {
    let now = Instant::now();
    let mut root_scope = root_scope(rule, Rc::new(data.path_value.clone()));
    let status = eval_rules_file(rule, &mut root_scope, Some(&data.name))?;
    let root_record = root_scope.reset_recorder().extract();
    let time = now.elapsed().as_millis();

    let tc = match simplified_json_from_root(&root_record) {
        Ok(report) => match status {
            Status::FAIL => {
                let status = report.not_compliant.iter().fold(
                    FailingTestCase {
                        name: None,
                        messages: vec![],
                    },
                    |mut test_case, failure| {
                        failure.get_message().into_iter().for_each(|e| {
                            if let rules::eval_context::ClauseReport::Rule(rule) = failure {
                                let name = match rule.name.contains(".guard/") {
                                    true => rule.name.split(".guard/").collect::<Vec<&str>>()[1],
                                    false => rule.name,
                                };
                                test_case.name = Some(String::from(name));
                            };
                            test_case.messages.push(e);
                        });
                        test_case
                    },
                );

                TestCase {
                    id: None,
                    name,
                    time,
                    status: TestCaseStatus::Fail(status),
                }
            }
            _ => TestCase {
                id: None,
                name,
                time,
                status: match status {
                    Status::PASS => TestCaseStatus::Pass,
                    Status::SKIP => TestCaseStatus::Skip,
                    _ => unreachable!(),
                },
            },
        },

        Err(error) => TestCase {
            id: None,
            name,
            time,
            status: TestCaseStatus::Error {
                error: error.to_string(),
            },
        },
    };

    Ok(tc)
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct TestCase<'test> {
    pub id: Option<&'test str>,
    pub name: &'test str,
    pub time: u128,
    pub(crate) status: TestCaseStatus,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub(crate) enum TestCaseStatus {
    Pass,
    Skip,
    Fail(FailingTestCase),
    Error { error: String },
}

#[derive(Debug, Clone)]
pub struct TestSuite<'suite> {
    pub name: String,
    pub test_cases: Vec<TestCase<'suite>>,
    pub time: u128,
    pub errors: usize,
    pub failures: usize,
}

impl<'suite> TestSuite<'suite> {
    pub fn new(
        name: String,
        test_cases: Vec<TestCase<'suite>>,
        time: u128,
        errors: usize,
        failures: usize,
    ) -> Self {
        Self {
            name,
            test_cases,
            time,
            errors,
            failures,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub(crate) struct FailingTestCase {
    pub(crate) name: Option<String>,
    pub(crate) messages: Vec<Messages>,
}

#[derive(Default, Debug)]
struct Failure<'report> {
    name: Option<&'report String>,
    messages: Vec<&'report String>,
}

#[derive(Default, Debug)]
pub struct TestSuites<'report, 'se: 'report> {
    pub name: &'report str,
    pub tests: usize,
    pub failures: usize,
    pub errors: usize,
    pub time: u128,
    pub test_suites: &'se [TestSuite<'report>],
}

#[derive(Debug)]
enum EventType<'report, 'se: 'report> {
    Failure(Failure<'report>),
    Error(&'report str),
    TestCase(&'se TestCase<'report>),
    TestSuite(&'se TestSuite<'report>),
    TestSuites(TestSuites<'report, 'se>),
}

impl<'report, 'se: 'report> EventType<'report, 'se> {
    fn serialize_start_event(
        &self,
        writer: &mut Writer<impl std::io::Write>,
        tag: BytesStart<'_>,
    ) -> crate::rules::Result<()> {
        Ok(writer.write_event(Event::Start(tag))?)
    }
    fn start_tag(&self) -> BytesStart<'_> {
        BytesStart::new(self.to_string())
    }
    fn serialize_end_event(
        &self,
        writer: &mut Writer<impl std::io::Write>,
    ) -> crate::rules::Result<()> {
        Ok(writer.write_event(Event::End(BytesEnd::new(self.to_string())))?)
    }
    fn extend_attributes(&self, tag: &mut BytesStart<'_>) {
        match self {
            EventType::Failure(failure) => {
                if let Some(name) = &failure.name {
                    tag.push_attribute(("message", name.as_str()));
                }
            }
            EventType::TestCase(test_case) => {
                if let Some(id) = test_case.id {
                    tag.push_attribute(("id", id));
                }
                tag.extend_attributes([
                    ("name", test_case.name),
                    ("time", format!("{:.3}", test_case.time).as_str()),
                ]);
                match &test_case.status {
                    TestCaseStatus::Fail(..) => {}
                    status => {
                        let status = match status {
                            TestCaseStatus::Skip => "skip",
                            TestCaseStatus::Pass => "pass",
                            TestCaseStatus::Error { .. } => "error",
                            _ => unreachable!(),
                        };
                        tag.extend_attributes([("status", status)]);
                    }
                }
            }
            EventType::TestSuite(test_suite) => {
                tag.extend_attributes([
                    ("name", test_suite.name.as_str()),
                    ("errors", test_suite.errors.to_string().as_str()),
                    ("failures", test_suite.failures.to_string().as_str()),
                    ("time", format!("{:.3}", test_suite.time).as_str()),
                ]);
            }
            EventType::Error(..) => {}
            EventType::TestSuites(suites) => {
                tag.extend_attributes([
                    ("name", suites.name),
                    ("tests", suites.tests.to_string().as_str()),
                    ("failures", suites.failures.to_string().as_str()),
                    ("errors", suites.errors.to_string().as_str()),
                    ("time", format!("{:.3}", suites.time).as_str()),
                ]);
            }
        }
    }

    fn serialize(&self, writer: &mut Writer<impl std::io::Write>) -> crate::rules::Result<()> {
        let mut tag = self.start_tag();
        self.extend_attributes(&mut tag);
        match self {
            EventType::Failure(failure) => {
                if !failure.messages.is_empty() {
                    self.serialize_start_event(writer, tag)?;
                    self.serialize_text_events(writer)?;
                    self.serialize_end_event(writer)?;
                } else {
                    writer.write_event(Event::Empty(tag))?;
                }
            }
            EventType::TestCase(test_case) => match &test_case.status {
                TestCaseStatus::Fail(failure) => {
                    self.serialize_start_event(writer, tag)?;
                    let name = failure.name.as_ref();
                    let event = match failure.messages.is_empty() {
                        false => {
                            let messages = failure.messages.iter().fold(vec![], |mut acc, msg| {
                                if let Some(custom_message) = &msg.custom_message {
                                    acc.push(custom_message);
                                }
                                if let Some(error_message) = &msg.error_message {
                                    acc.push(error_message);
                                }
                                acc
                            });
                            EventType::Failure(Failure { name, messages })
                        }
                        true => EventType::Failure(Failure {
                            name,
                            messages: vec![],
                        }),
                    };
                    event.serialize(writer)?;
                    self.serialize_end_event(writer)?;
                }
                TestCaseStatus::Error { ref error } => {
                    self.serialize_start_event(writer, tag)?;
                    EventType::Error(error).serialize(writer)?;
                    self.serialize_end_event(writer)?;
                }
                _ => {
                    writer.write_event(Event::Empty(tag))?;
                }
            },
            EventType::Error(..) => {
                self.serialize_start_event(writer, tag)?;
                self.serialize_text_events(writer)?;
                self.serialize_end_event(writer)?;
            }
            EventType::TestSuite(test_suite) => {
                self.serialize_start_event(writer, tag)?;

                for test_case in &test_suite.test_cases {
                    EventType::TestCase(test_case).serialize(writer)?;
                }

                self.serialize_end_event(writer)?;
            }
            EventType::TestSuites(suites) => {
                self.serialize_start_event(writer, tag)?;
                for test_suite in suites.test_suites {
                    EventType::TestSuite(test_suite).serialize(writer)?;
                }

                self.serialize_end_event(writer)?;
                writer.write_event(Event::Eof)?;
            }
        }

        Ok(())
    }

    fn serialize_text_events(
        &self,
        writer: &mut Writer<impl std::io::Write>,
    ) -> crate::rules::Result<()> {
        match self {
            EventType::Failure(Failure { messages, .. }) => {
                for message in messages {
                    writer.write_event(Event::Text(BytesText::new(message)))?;
                }
            }
            EventType::Error(err) => {
                writer.write_event(Event::Text(BytesText::new(err)))?;
            }
            _ => unreachable!(),
        }

        Ok(())
    }
}

impl<'report, 'se: 'report> Display for EventType<'report, 'se> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let text = match self {
            EventType::Failure(..) => "failure",
            EventType::Error(..) => "error",
            EventType::TestCase(..) => "testcase",
            EventType::TestSuite(..) => "testsuite",
            EventType::TestSuites(..) => "testsuites",
        };

        f.write_str(text)
    }
}