Skip to content

Commit 7b33523

Browse files
Add note field in response of the IaC Validation API (#32)
<!-- 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. --> A new field has been added to the response of IaC Validation API. This change attempts to include the additional field in the response generated by this plugin.
1 parent 07734f4 commit 7b33523

File tree

12 files changed

+139
-42
lines changed

12 files changed

+139
-42
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/analyze-code-security-scc.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/accessor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export type OperationMetadata = {
8787

8888
export type IACValidationReport = {
8989
violations?: Violation[];
90+
note?: string;
9091
};
9192

9293
export type Response = {
@@ -250,15 +251,19 @@ export class IACAccessor {
250251
}
251252
}
252253

253-
private processIACValidationResponse(operation: Operation): Violation[] {
254+
private processIACValidationResponse(operation: Operation): IACValidationReport {
254255
const violations: Violation[] = [];
255256
operation.response?.iacValidationReport?.violations?.forEach((violation) => {
256257
if (!violation.severity) {
257258
violation.severity = Severity.SeverityUnspecified;
258259
}
259260
violations.push(violation);
260261
});
261-
return violations;
262+
const report: IACValidationReport = {
263+
violations: violations,
264+
note: operation.response?.iacValidationReport?.note,
265+
};
266+
return report;
262267
}
263268

264269
/**
@@ -299,7 +304,7 @@ export class IACAccessor {
299304
*
300305
* @param iac IAC file to scan.
301306
*/
302-
async scan(iac: string): Promise<Violation[]> {
307+
async scan(iac: string): Promise<IACValidationReport> {
303308
logDebug(`IaC scanning invoked at: ${this.scanStartTime}`);
304309
const request: IACRequest = {
305310
parent: this.organizationId,

src/main.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { errorMessage, parseBoolean, parseDuration } from '@google-github-action
2929

3030
import { IACType } from './input_configuration';
3131
import { isFailureCriteriaSatisfied, validateAndParseFailureCriteria } from './utils';
32-
import { IACAccessor, Violation } from './accessor';
32+
import { IACAccessor, Violation, IACValidationReport } from './accessor';
3333
import { VALIDATE_ENDPOINT_DOMAIN } from './commons/http_config';
3434
import { SarifReportGenerator } from './reports/iac_scan_report_processor';
3535
import { IACScanReportProcessor } from './reports/iac_scan_report_processor';
@@ -93,20 +93,23 @@ async function run(): Promise<void> {
9393
scanStartTime,
9494
version,
9595
);
96-
logInfo(`Fetching violations for IaC file`);
97-
const violations: Violation[] = await accessor.scan(planFile);
96+
logInfo(`Fetching violations report for IaC file`);
97+
const report: IACValidationReport = await accessor.scan(planFile);
9898
logDebug(`Violations fetched from IaC scan APIs`);
9999

100100
const sarifReportGenerator: SarifReportGenerator = new SarifReportGenerator(version);
101101
logInfo('Processing report generation for violations fetched');
102102
await IACScanReportProcessor.processReport(
103-
violations,
103+
report,
104104
sarifReportGenerator,
105105
SARIF_REPORT_FILE_NAME,
106106
);
107107
logDebug(`IaC scan report processing completed`);
108108

109-
const failureCriteriaSatisfied = isFailureCriteriaSatisfied(failureCriteria, violations);
109+
const failureCriteriaSatisfied = isFailureCriteriaSatisfied(
110+
failureCriteria,
111+
<Violation[]>report.violations,
112+
);
110113
if (failureCriteriaSatisfied && !ignoreViolations) {
111114
setOutput(IAC_SCAN_RESULT_OUTPUT_KEY, IAC_SCAN_RESULT.FAILED);
112115
setFailed(ACTION_FAIL_ERROR(`${FAILURE_CRITERIA_CONFIG_KEY} was satisfied`));

src/reports/iac_scan_report_processor.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as fs from 'fs/promises';
1818

1919
import { debug as logDebug, setOutput } from '@actions/core';
2020

21-
import { Violation } from '../accessor';
21+
import { Violation, IACValidationReport } from '../accessor';
2222
import { Rule, Result, SARIFTemplate } from './sarif_template';
2323
import {
2424
SARIF_SCHEMA,
@@ -34,24 +34,24 @@ export abstract class IACScanReportProcessor {
3434
* Processes violations fetched from IaC scan API.
3535
*
3636
* If violations are found, this method generates the report and writes the report to the github workspace.
37-
* @param violations violations found in IaC file.
37+
* @param report IaC Validation report.
3838
* @param reportGenerator implementation of {@link ReportGenerator}
3939
* @param reportName name of the generated report.
4040
*/
4141
static async processReport(
42-
violations: Violation[],
42+
report: IACValidationReport,
4343
reportGenerator: ReportGenerator,
4444
reportName: string,
4545
) {
46-
if (violations.length == 0) {
46+
if (report.violations?.length == 0) {
4747
// no violations, returning as no action to take.
4848
return;
4949
}
5050

51-
const report = reportGenerator.generate(violations);
51+
const generatedReport = reportGenerator.generate(report);
5252
logDebug(`IaC scan report generated`);
5353

54-
await fs.writeFile(reportName, report);
54+
await fs.writeFile(reportName, generatedReport);
5555
setOutput(IAC_SCAN_RESULT_SARIF_PATH_OUTPUT_KEY, IAC_SCAN_RESULT_SARIF_PATH_OUTPUT_VALUE);
5656
logDebug(`IAC scan report written to github action workspace`);
5757
}
@@ -66,7 +66,7 @@ export interface ReportGenerator {
6666
*
6767
* @param violations non empty list of violation fetched from scan API response.
6868
*/
69-
generate(violations: Violation[]): string;
69+
generate(report: IACValidationReport): string;
7070
}
7171

7272
/**
@@ -84,11 +84,12 @@ export class SarifReportGenerator implements ReportGenerator {
8484
* fields undefined in scan API response are omitted from report.
8585
* @param violations non empty list of violation fetched from scan API response.
8686
*/
87-
generate(violations: Violation[]): string {
88-
const policyToViolationMap = this.getUniqueViolation(violations);
87+
generate(report: IACValidationReport): string {
88+
const policyToViolationMap = this.getUniqueViolation(<Violation[]>report.violations);
8989
const rules: Rule[] = this.constructRules(policyToViolationMap);
90-
const results: Result[] = this.constructResults(violations);
91-
const sarifReport: SARIFTemplate = this.constructSARIFReport(rules, results);
90+
const results: Result[] = this.constructResults(<Violation[]>report.violations);
91+
const note: string = <string>report.note;
92+
const sarifReport: SARIFTemplate = this.constructSARIFReport(rules, results, note);
9293
return JSON.stringify(sarifReport, null, 2);
9394
}
9495

@@ -160,12 +161,13 @@ export class SarifReportGenerator implements ReportGenerator {
160161
return results;
161162
}
162163

163-
private constructSARIFReport(rules: Rule[], results: Result[]) {
164+
private constructSARIFReport(rules: Rule[], results: Result[], note: string) {
164165
const sarifReport: SARIFTemplate = {
165166
version: SARIF_VERSION,
166167
$schema: SARIF_SCHEMA,
167168
runs: [
168169
{
170+
note: note,
169171
tool: {
170172
driver: {
171173
name: IAC_TOOL_NAME,

src/reports/sarif_template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type SARIFTemplate = {
6464
version: string;
6565
$schema: string;
6666
runs: {
67+
note: string;
6768
tool: {
6869
driver: {
6970
name: string;

0 commit comments

Comments
 (0)