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

feat: IaC SARIF output improvements [CFG-1313] [CFG-1314] #2524

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions src/lib/formatters/iac-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as Debug from 'debug';
import * as pathLib from 'path';
import { pathToFileURL } from 'url';
import upperFirst = require('lodash.upperfirst');
import camelCase = require('lodash.camelcase');
import { execSync } from 'child_process';

import {
IacTestResponse,
Expand Down Expand Up @@ -136,11 +138,16 @@ export function createSarifOutputForIac(
const basePath = isLocalFolder(iacTestResponses[0].path)
? pathLib.resolve('.', iacTestResponses[0].path)
: pathLib.resolve('.');
const repoRoot = getRepoRoot();
const issues = iacTestResponses.reduce((collect: ResponseIssues, res) => {
if (res.result) {
// targetFile is the computed relative path of the scanned file
// so needs to be cleaned up before assigning to the URI
const targetPath = res.targetFile.replace(/\\/g, '/');
const targetPath = getPathRelativeToRepoRoot(
repoRoot,
basePath,
res.targetFile,
);
const mapped = res.result.cloudConfigResults.map((issue) => ({
issue,
targetPath,
Expand All @@ -152,25 +159,33 @@ export function createSarifOutputForIac(

const tool: sarif.Tool = {
driver: {
name: 'Snyk Infrastructure as Code',
name: 'Snyk IaC',
fullName: 'Snyk Infrastructure as Code',
informationUri:
'https://docs.snyk.io/products/snyk-infrastructure-as-code',
rules: extractReportingDescriptor(issues),
},
};
return {
$schema:
'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json',
version: '2.1.0',
runs: [
{
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/os/sarif-v2.1.0-os.html#_Toc34317498
originalUriBaseIds: {
[PROJECT_ROOT_KEY]: {
uri: pathToFileURL(pathLib.join(basePath, '/')).href,
uri: pathToFileURL(repoRoot).href,
description: {
text: 'The root directory for all project files.',
},
},
},

tool,
automationDetails: {
id: 'snyk-iac',
},
results: mapIacTestResponseToSarifResults(issues),
},
],
Expand Down Expand Up @@ -214,6 +229,7 @@ export function extractReportingDescriptor(
}
tool[issue.id] = {
id: issue.id,
name: upperFirst(camelCase(issue.title)).replace(/ /g, ''),
shortDescription: {
text: `${upperFirst(issue.severity)} severity - ${issue.title}`,
},
Expand All @@ -232,8 +248,11 @@ export function extractReportingDescriptor(
},
properties: {
tags,
documentation: issue.documentation,
problem: {
severity: issue.severity,
},
},
helpUri: issue.documentation,
};
});

Expand Down Expand Up @@ -274,3 +293,21 @@ export function mapIacTestResponseToSarifResults(
};
});
}

function getRepoRoot() {
const cwd = process.cwd();
const stdout = execSync('git rev-parse --show-toplevel', {
encoding: 'utf8',
cwd,
});
return stdout.trim() + '/';
}

function getPathRelativeToRepoRoot(
repoRoot: string,
basePath: string,
filePath: string,
) {
const fullPath = pathLib.resolve(basePath, filePath).replace(/\\/g, '/');
return fullPath.replace(repoRoot, '');
}
6 changes: 4 additions & 2 deletions test/jest/acceptance/iac/file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ describe('iac test --sarif-file-output', () => {
.artifactLocation.uri;
const actualProjectRoot =
jsonObj?.runs?.[0].originalUriBaseIds.PROJECTROOT.uri;
expect(actualPhysicalLocation).toEqual('./iac/file-output/sg_open_ssh.tf');
expect(actualPhysicalLocation).toEqual(
'test/fixtures/iac/file-output/sg_open_ssh.tf',
);
expect(actualProjectRoot).toEqual(
pathToFileURL(path.join(path.resolve('./test/fixtures/'), '/')).href,
pathToFileURL(path.join(path.resolve(''), '/')).href,
);
});
});