Skip to content

Commit ac8f305

Browse files
Return note field in case of zero violations (#42)
<!-- Thank you for proposing a pull request! Please note that SOME TESTS WILL LIKELY FAIL due to how GitHub exposes secrets in Pull Requests from forks. Someone from the team will review your Pull Request and respond. Please describe your change and any implementation details below. --> --------- Signed-off-by: pankhurisaxena28 <[email protected]> Co-authored-by: Seth Vargo <[email protected]>
1 parent e89e82e commit ac8f305

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
if [ "${{ steps.violations-found.outputs.iac_scan_result }}" != "passed" ]; then
109109
exit 1
110110
fi
111-
111+
112112
- id: 'no-violations-found'
113113
name: 'No violations found in plan file'
114114
uses: './'
@@ -119,13 +119,15 @@ jobs:
119119
failure_criteria: 'CRITICAL:2, Operator:OR'
120120
- name: 'Check scan result and report not generated.'
121121
run: |
122-
if [ "${{ steps.no-violations-found.outputs.iac_scan_result_sarif_path }}" != "" ]; then
122+
report_expected="tests/resources/zero_violations_sarif.json"
123+
report_generated="${{ steps.no-violations-found.outputs.iac_scan_result_sarif_path }}"
124+
if cmp -s "$report_expected" "$report_generated"; then
123125
exit 1
124126
fi
125127
if [ "${{ steps.no-violations-found.outputs.iac_scan_result }}" != "passed" ]; then
126128
exit 1
127129
fi
128-
130+
129131
- id: 'failure-criteria-satisfied'
130132
name: 'Failure criteria satisfied'
131133
uses: './'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules/
22
runner/
3+
dist/
34

45
# Rest of the file pulled from https://github.com/github/gitignore/blob/main/Node.gitignore
56
# Logs

dist/main/index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/reports/iac_scan_report_processor.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ export abstract class IACScanReportProcessor {
4343
reportGenerator: ReportGenerator,
4444
reportName: string,
4545
) {
46-
if (report.violations?.length == 0) {
47-
// no violations, returning as no action to take.
48-
return;
49-
}
50-
5146
const generatedReport = reportGenerator.generate(report);
5247
logDebug(`IaC scan report generated`);
5348

@@ -85,10 +80,14 @@ export class SarifReportGenerator implements ReportGenerator {
8580
* @param violations non empty list of violation fetched from scan API response.
8681
*/
8782
generate(report: IACValidationReport): string {
83+
const note: string = <string>report.note;
84+
if (report.violations?.length === 0) {
85+
const sarifReport: SARIFTemplate = this.constructSARIFReport(<Rule[]>[], <Result[]>[], note);
86+
return JSON.stringify(sarifReport, null, 2);
87+
}
8888
const policyToViolationMap = this.getUniqueViolation(<Violation[]>report.violations);
8989
const rules: Rule[] = this.constructRules(policyToViolationMap);
9090
const results: Result[] = this.constructResults(<Violation[]>report.violations);
91-
const note: string = <string>report.note;
9291
const sarifReport: SARIFTemplate = this.constructSARIFReport(rules, results, note);
9392
return JSON.stringify(sarifReport, null, 2);
9493
}

tests/iac_scan_report_processor.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,21 @@ test(
157157
assert.deepStrictEqual(sarifJson.runs.at(0)?.results.length, 2);
158158
},
159159
);
160+
161+
await suite.test('zero violations, generates report with only the note', async () => {
162+
const reportGenerator = new SarifReportGenerator('version');
163+
164+
const report: IACValidationReport = {
165+
note: 'IaC validation is limited to certain asset types and policies. For information about supported asset types and policies for IaC validation, see https://cloud.google.com/security-command-center/docs/supported-iac-assets-policies.',
166+
violations: <Violation[]>[],
167+
};
168+
169+
await IACScanReportProcessor.processReport(report, reportGenerator, 'sarif.json');
170+
const sarif = await fs.readFile('./sarif.json', 'utf-8');
171+
const sarifJson: SARIFTemplate = JSON.parse(sarif);
172+
173+
assert.deepStrictEqual(sarifJson.runs.at(0)?.tool.driver.rules.length, 0);
174+
assert.deepStrictEqual(sarifJson.runs.at(0)?.results.length, 0);
175+
});
160176
},
161177
);

tests/resources/sarif.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
44
"runs": [
55
{
6+
"note": "IaC validation is limited to certain asset types and policies. For information about supported asset types and policies for IaC validation, see https://cloud.google.com/security-command-center/docs/supported-iac-assets-policies.",
67
"tool": {
78
"driver": {
89
"name": "analyze-code-security-scc",

0 commit comments

Comments
 (0)