Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update iac_scan_report_processor.ts #41

Closed
11 changes: 5 additions & 6 deletions src/reports/iac_scan_report_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ export abstract class IACScanReportProcessor {
reportGenerator: ReportGenerator,
reportName: string,
) {
if (report.violations?.length == 0) {
// no violations, returning as no action to take.
return;
}

const generatedReport = reportGenerator.generate(report);
logDebug(`IaC scan report generated`);

Expand Down Expand Up @@ -85,10 +80,14 @@ export class SarifReportGenerator implements ReportGenerator {
* @param violations non empty list of violation fetched from scan API response.
*/
generate(report: IACValidationReport): string {
const note: string = <string>report.note;
if (report.violations?.length == 0) {
const sarifReport: SARIFTemplate = this.constructSARIFReport(Rule[], Result[], note);
return JSON.stringify(sarifReport, null, 2);
}
const policyToViolationMap = this.getUniqueViolation(<Violation[]>report.violations);
const rules: Rule[] = this.constructRules(policyToViolationMap);
const results: Result[] = this.constructResults(<Violation[]>report.violations);
const note: string = <string>report.note;
const sarifReport: SARIFTemplate = this.constructSARIFReport(rules, results, note);
return JSON.stringify(sarifReport, null, 2);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/iac_scan_report_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,20 @@ test(
assert.deepStrictEqual(sarifJson.runs.at(0)?.results.length, 2);
},
);

await suite.test('zero violations, generates report with only the note field', async () => {
const reportGenerator = new SarifReportGenerator('version');

const report: IACValidationReport = {
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.',
};

await IACScanReportProcessor.processReport(report, reportGenerator, 'sarif.json');
const sarif = await fs.readFile('./sarif.json', 'utf-8');
const sarifJson: SARIFTemplate = JSON.parse(sarif);

assert.deepStrictEqual(sarifJson.runs.at(0)?.tool.driver.rules.length, 0);
assert.deepStrictEqual(sarifJson.runs.at(0)?.results.length, 0);
});
},
);
Loading