Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: snyk/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.957.0
Choose a base ref
...
head repository: snyk/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.958.0
Choose a head ref
  • 6 commits
  • 5 files changed
  • 6 contributors

Commits on Jun 23, 2022

  1. Verified

    This commit was signed with the committer’s verified signature.
    francescomari Francesco Mari
    Copy the full SHA
    796fbf1 View commit details
  2. fix: bump driftctl

    eliecharra committed Jun 23, 2022

    Unverified

    The email in this signature doesn’t match the committer email.
    Copy the full SHA
    dae3c8e View commit details
  3. Merge pull request #3374 from snyk/bump_driftctl

    Bump driftctl
    eliecharra authored Jun 23, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f530478 View commit details
  4. Verified

    This commit was signed with the committer’s verified signature.
    Jdunsby Johnny Dunsby
    Copy the full SHA
    6e26bdc View commit details
  5. Merge pull request #3373 from snyk/refactor/extract-output-logic-test

    refactor: extract the output logic to its own module
    francescomari authored Jun 23, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e02cd6b View commit details
  6. Merge pull request #3375 from snyk/fix/reduce-default-gradle-logging

    fix: reduce default snyk-gradle-plugin logging
    Jdunsby authored Jun 23, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    6cb1afa View commit details
Showing with 379 additions and 294 deletions.
  1. +28 −9 package-lock.json
  2. +1 −1 package.json
  3. +19 −273 src/cli/commands/test/iac/index.ts
  4. +320 −0 src/cli/commands/test/iac/output.ts
  5. +11 −11 src/lib/iac/drift/driftctl.ts
37 changes: 28 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@
"snyk-cpp-plugin": "2.20.0",
"snyk-docker-plugin": "^4.38.0",
"snyk-go-plugin": "1.19.0",
"snyk-gradle-plugin": "3.20.0",
"snyk-gradle-plugin": "3.20.2",
"snyk-module": "3.1.0",
"snyk-mvn-plugin": "2.29.7",
"snyk-nodejs-lockfile-parser": "1.38.0",
292 changes: 19 additions & 273 deletions src/cli/commands/test/iac/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,18 @@
import * as Debug from 'debug';
import { EOL } from 'os';
import chalk from 'chalk';

import { MethodArgs } from '../../../args';
import { TestCommandResult } from '../../types';
import { LegacyVulnApiResult } from '../../../../lib/snyk-test/legacy';
import { mapIacTestResult } from '../../../../lib/snyk-test/iac-test-result';

import {
summariseErrorResults,
summariseVulnerableResults,
} from '../../../../lib/formatters';
import {
failuresTipOutput,
formatIacTestFailures,
formatFailuresList,
formatIacTestSummary,
formatShareResultsOutput,
getIacDisplayedIssues,
getIacDisplayErrorFileOutput,
iacTestTitle,
shouldLogUserMessages,
spinnerSuccessMessage,
IaCTestFailure,
} from '../../../../lib/formatters/iac-output';
import { extractDataToSendFromResults } from '../../../../lib/formatters/test/format-test-results';

import { validateCredentials } from '../validate-credentials';
import { validateTestOptions } from '../validate-test-options';
import { setDefaultTestOptions } from '../set-default-test-options';
import { processCommandArgs } from '../../process-command-args';
import { displayResult } from '../../../../lib/formatters/test/display-result';

import { isIacShareResultsOptions } from './local-execution/assert-iac-options-flag';
import { hasFeatureFlag } from '../../../../lib/feature-flags';
import { buildDefaultOciRegistry } from './local-execution/rules/rules';
import { getIacOrgSettings } from './local-execution/measurable-methods';
import config from '../../../../lib/config';
import { UnsupportedEntitlementError } from '../../../../lib/errors/unsupported-entitlement-error';
import * as ora from 'ora';
import { CustomError, FormattedCustomError } from '../../../../lib/errors';
import { scan } from './scan';
import * as path from 'path';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';
import { buildOutput, buildSpinner, printHeader } from './output';

export default async function(...args: MethodArgs): Promise<TestCommandResult> {
const { options: originalOptions, paths } = processCommandArgs(...args);
@@ -62,25 +30,20 @@ export default async function(...args: MethodArgs): Promise<TestCommandResult> {

const buildOciRegistry = () => buildDefaultOciRegistry(iacOrgSettings);

let testSpinner: ora.Ora | undefined;

const isNewIacOutputSupported =
config.IAC_OUTPUT_V2 ||
(await hasFeatureFlag('iacCliOutputRelease', options));

if (shouldLogUserMessages(options, isNewIacOutputSupported)) {
console.log(EOL + iacTestTitle + EOL);

if (paths.some(isOutsideCurrentWorkingDirectory)) {
printCurrentWorkingDirectoryTraversalWarning();
}

testSpinner = ora({ isSilent: options.quiet, stream: process.stdout });
}
const testSpinner = buildSpinner({
options,
isNewIacOutputSupported,
});

if (!iacOrgSettings.entitlements?.infrastructureAsCode) {
throw new UnsupportedEntitlementError('infrastructureAsCode');
}
printHeader({
paths,
options,
isNewIacOutputSupported,
});

const {
iacOutputMeta,
@@ -97,231 +60,14 @@ export default async function(...args: MethodArgs): Promise<TestCommandResult> {
buildOciRegistry,
);

// this is any[] to follow the resArray type above
const successResults: any[] = [],
errorResults: any[] = [];
results.forEach((result) => {
if (!(result instanceof Error)) {
successResults.push(result);
} else {
errorResults.push(result);
}
return buildOutput({
results,
options,
isNewIacOutputSupported,
iacOutputMeta,
resultOptions,
iacScanFailures,
iacIgnoredIssuesCount,
testSpinner,
});

const vulnerableResults = successResults.filter(
(res) =>
(res.vulnerabilities && res.vulnerabilities.length) ||
(res.result &&
res.result.cloudConfigResults &&
res.result.cloudConfigResults.length),
);
const hasErrors = errorResults.length;
const isPartialSuccess = !hasErrors || successResults.length;
const foundVulnerabilities = vulnerableResults.length;

if (isPartialSuccess) {
testSpinner?.succeed(spinnerSuccessMessage);
} else {
testSpinner?.stop();
}

// resultOptions is now an array of 1 or more options used for
// the tests results is now an array of 1 or more test results
// values depend on `options.json` value - string or object
const mappedResults = results.map(mapIacTestResult);

const {
stdout: dataToSend,
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
} = extractDataToSendFromResults(results, mappedResults, options);

if (options.json || options.sarif) {
// if all results are ok (.ok == true)
if (mappedResults.every((res) => res.ok)) {
return TestCommandResult.createJsonTestCommandResult(
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
);
}

const err = new Error(stringifiedData) as any;

if (foundVulnerabilities) {
err.code = 'VULNS';
const dataToSendNoVulns = dataToSend;
delete dataToSendNoVulns.vulnerabilities;
err.jsonNoVulns = dataToSendNoVulns;
}

if (hasErrors) {
// Take the code of the first problem to go through error
// translation.
// Note: this is done based on the logic done below
// for non-json/sarif outputs, where we take the code of
// the first error.
err.code = errorResults[0].code;
}
err.json = stringifiedData;
err.jsonStringifiedResults = stringifiedJsonData;
err.sarifStringifiedResults = stringifiedSarifData;
throw err;
}

let response = '';

if (isNewIacOutputSupported) {
if (isPartialSuccess) {
response += EOL + getIacDisplayedIssues(successResults, iacOutputMeta!);
}
} else {
response += results
.map((result, i) => {
return displayResult(
results[i] as LegacyVulnApiResult,
{
...resultOptions[i],
},
result.foundProjectCount,
);
})
.join(`\n${SEPARATOR}`);
}

if (!isNewIacOutputSupported && hasErrors) {
debug(`Failed to test ${errorResults.length} projects, errors:`);
errorResults.forEach((err) => {
const errString = err.stack ? err.stack.toString() : err.toString();
debug('error: %s', errString);
});
}

let summaryMessage = '';
let errorResultsLength = errorResults.length;

if (iacScanFailures.length || hasErrors) {
errorResultsLength = iacScanFailures.length || errorResults.length;

const thrownErrors: IaCTestFailure[] = errorResults.map((err) => ({
filePath: err.path,
failureReason: err.message,
}));

const allTestFailures: IaCTestFailure[] = iacScanFailures
.map((f) => ({
filePath: f.filePath,
failureReason: f.failureReason,
}))
.concat(thrownErrors);

if (hasErrors && !isPartialSuccess) {
response += chalk.bold.red(summaryMessage);

// take the code of the first problem to go through error
// translation
// HACK as there can be different errors, and we pass only the
// first one
const error: CustomError =
isNewIacOutputSupported && allTestFailures
? new FormattedCustomError(
errorResults[0].message,
formatFailuresList(allTestFailures),
)
: new CustomError(response);
error.code = errorResults[0].code;
error.userMessage = errorResults[0].userMessage;
error.strCode = errorResults[0].strCode;

throw error;
}

response += isNewIacOutputSupported
? EOL.repeat(2) + formatIacTestFailures(allTestFailures)
: iacScanFailures
.map((reason) => chalk.bold.red(getIacDisplayErrorFileOutput(reason)))
.join('');
}

if (isPartialSuccess && iacOutputMeta && isNewIacOutputSupported) {
response += `${EOL}${SEPARATOR}${EOL}`;

const iacTestSummary = `${formatIacTestSummary(
{
results: successResults,
failures: iacScanFailures,
ignoreCount: iacIgnoredIssuesCount,
},
iacOutputMeta,
)}`;

response += iacTestSummary;
}

if (results.length > 1) {
if (isNewIacOutputSupported) {
response += errorResultsLength ? EOL.repeat(2) + failuresTipOutput : '';
} else {
const projects = results.length === 1 ? 'project' : 'projects';
summaryMessage +=
`\n\n\nTested ${results.length} ${projects}` +
summariseVulnerableResults(vulnerableResults, options) +
summariseErrorResults(errorResultsLength) +
'\n';
}
}

if (foundVulnerabilities) {
response += chalk.bold.red(summaryMessage);
response += EOL + EOL;

if (isIacShareResultsOptions(options)) {
response += formatShareResultsOutput(iacOutputMeta!) + EOL.repeat(2);
}

const error = new Error(response) as any;
// take the code of the first problem to go through error
// translation
// HACK as there can be different errors, and we pass only the
// first one
error.code = vulnerableResults[0].code || 'VULNS';
error.userMessage = vulnerableResults[0].userMessage;
error.jsonStringifiedResults = stringifiedJsonData;
error.sarifStringifiedResults = stringifiedSarifData;
throw error;
}

response += chalk.bold.green(summaryMessage);

if (isIacShareResultsOptions(options)) {
response += formatShareResultsOutput(iacOutputMeta!) + EOL.repeat(2);
}

return TestCommandResult.createHumanReadableTestCommandResult(
response,
stringifiedJsonData,
stringifiedSarifData,
);
}

function isOutsideCurrentWorkingDirectory(p: string): boolean {
return path.relative(process.cwd(), p).includes('..');
}

function printCurrentWorkingDirectoryTraversalWarning() {
let msg = '';

msg +=
'Warning: Scanning paths outside the current working directory is deprecated and' +
EOL;
msg +=
'will be removed in the future. Please see the documentation for further details:' +
EOL +
EOL;
msg +=
' https://docs.snyk.io/products/snyk-infrastructure-as-code/snyk-cli-for-infrastructure-as-code/test-your-configuration-files' +
EOL;

console.log(chalk.yellow(msg));
}
320 changes: 320 additions & 0 deletions src/cli/commands/test/iac/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
import * as Debug from 'debug';
import { EOL } from 'os';
import chalk from 'chalk';

import { TestCommandResult } from '../../types';
import { LegacyVulnApiResult } from '../../../../lib/snyk-test/legacy';
import { mapIacTestResult } from '../../../../lib/snyk-test/iac-test-result';

import {
summariseErrorResults,
summariseVulnerableResults,
} from '../../../../lib/formatters';
import {
failuresTipOutput,
formatIacTestFailures,
formatFailuresList,
formatIacTestSummary,
formatShareResultsOutput,
getIacDisplayedIssues,
getIacDisplayErrorFileOutput,
spinnerSuccessMessage,
IaCTestFailure,
shouldLogUserMessages,
iacTestTitle,
} from '../../../../lib/formatters/iac-output';
import { extractDataToSendFromResults } from '../../../../lib/formatters/test/format-test-results';

import { displayResult } from '../../../../lib/formatters/test/display-result';

import { isIacShareResultsOptions } from './local-execution/assert-iac-options-flag';
import * as ora from 'ora';
import { CustomError, FormattedCustomError } from '../../../../lib/errors';
import {
IacFileInDirectory,
IacOutputMeta,
Options,
TestOptions,
} from '../../../../lib/types';
import * as path from 'path';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';

export function buildSpinner({
options,
isNewIacOutputSupported,
}: {
options: Options & TestOptions;
isNewIacOutputSupported?: boolean;
}) {
if (shouldLogUserMessages(options, isNewIacOutputSupported)) {
return ora({ isSilent: options.quiet, stream: process.stdout });
}
}

export function printHeader({
paths,
options,
isNewIacOutputSupported,
}: {
paths: string[];
options: Options & TestOptions;
isNewIacOutputSupported?: boolean;
}) {
if (shouldLogUserMessages(options, isNewIacOutputSupported)) {
console.log(EOL + iacTestTitle + EOL);

if (paths.some(isOutsideCurrentWorkingDirectory)) {
printCurrentWorkingDirectoryTraversalWarning();
}
}
}

export function buildOutput({
results,
options,
isNewIacOutputSupported,
iacOutputMeta,
resultOptions,
iacScanFailures,
iacIgnoredIssuesCount,
testSpinner,
}: {
results: any[];
options: Options & TestOptions;
isNewIacOutputSupported?: boolean;
iacOutputMeta?: IacOutputMeta;
resultOptions: (Options & TestOptions)[];
iacScanFailures: IacFileInDirectory[];
iacIgnoredIssuesCount: number;
testSpinner?: ora.Ora;
}): TestCommandResult {
// this is any[] to follow the resArray type above
const successResults: any[] = [],
errorResults: any[] = [];
results.forEach((result) => {
if (!(result instanceof Error)) {
successResults.push(result);
} else {
errorResults.push(result);
}
});

const vulnerableResults = successResults.filter(
(res) =>
(res.vulnerabilities && res.vulnerabilities.length) ||
(res.result &&
res.result.cloudConfigResults &&
res.result.cloudConfigResults.length),
);
const hasErrors = errorResults.length;
const isPartialSuccess = !hasErrors || successResults.length;
const foundVulnerabilities = vulnerableResults.length;

if (isPartialSuccess) {
testSpinner?.succeed(spinnerSuccessMessage);
} else {
testSpinner?.stop();
}

// resultOptions is now an array of 1 or more options used for
// the tests results is now an array of 1 or more test results
// values depend on `options.json` value - string or object
const mappedResults = results.map(mapIacTestResult);

const {
stdout: dataToSend,
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
} = extractDataToSendFromResults(results, mappedResults, options);

if (options.json || options.sarif) {
// if all results are ok (.ok == true)
if (mappedResults.every((res) => res.ok)) {
return TestCommandResult.createJsonTestCommandResult(
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
);
}

const err = new Error(stringifiedData) as any;

if (foundVulnerabilities) {
err.code = 'VULNS';
const dataToSendNoVulns = dataToSend;
delete dataToSendNoVulns.vulnerabilities;
err.jsonNoVulns = dataToSendNoVulns;
}

if (hasErrors) {
// Take the code of the first problem to go through error
// translation.
// Note: this is done based on the logic done below
// for non-json/sarif outputs, where we take the code of
// the first error.
err.code = errorResults[0].code;
}
err.json = stringifiedData;
err.jsonStringifiedResults = stringifiedJsonData;
err.sarifStringifiedResults = stringifiedSarifData;
throw err;
}

let response = '';

if (isNewIacOutputSupported) {
if (isPartialSuccess) {
response += EOL + getIacDisplayedIssues(successResults, iacOutputMeta!);
}
} else {
response += results
.map((result, i) => {
return displayResult(
results[i] as LegacyVulnApiResult,
{
...resultOptions[i],
},
result.foundProjectCount,
);
})
.join(`\n${SEPARATOR}`);
}

if (!isNewIacOutputSupported && hasErrors) {
debug(`Failed to test ${errorResults.length} projects, errors:`);
errorResults.forEach((err) => {
const errString = err.stack ? err.stack.toString() : err.toString();
debug('error: %s', errString);
});
}

let summaryMessage = '';
let errorResultsLength = errorResults.length;

if (iacScanFailures.length || hasErrors) {
errorResultsLength = iacScanFailures.length || errorResults.length;

const thrownErrors: IaCTestFailure[] = errorResults.map((err) => ({
filePath: err.path,
failureReason: err.message,
}));

const allTestFailures: IaCTestFailure[] = iacScanFailures
.map((f) => ({
filePath: f.filePath,
failureReason: f.failureReason,
}))
.concat(thrownErrors);

if (hasErrors && !isPartialSuccess) {
response += chalk.bold.red(summaryMessage);

// take the code of the first problem to go through error
// translation
// HACK as there can be different errors, and we pass only the
// first one
const error: CustomError =
isNewIacOutputSupported && allTestFailures
? new FormattedCustomError(
errorResults[0].message,
formatFailuresList(allTestFailures),
)
: new CustomError(response);
error.code = errorResults[0].code;
error.userMessage = errorResults[0].userMessage;
error.strCode = errorResults[0].strCode;

throw error;
}

response += isNewIacOutputSupported
? EOL.repeat(2) + formatIacTestFailures(allTestFailures)
: iacScanFailures
.map((reason) => chalk.bold.red(getIacDisplayErrorFileOutput(reason)))
.join('');
}

if (isPartialSuccess && iacOutputMeta && isNewIacOutputSupported) {
response += `${EOL}${SEPARATOR}${EOL}`;

const iacTestSummary = `${formatIacTestSummary(
{
results: successResults,
failures: iacScanFailures,
ignoreCount: iacIgnoredIssuesCount,
},
iacOutputMeta,
)}`;

response += iacTestSummary;
}

if (results.length > 1) {
if (isNewIacOutputSupported) {
response += errorResultsLength ? EOL.repeat(2) + failuresTipOutput : '';
} else {
const projects = results.length === 1 ? 'project' : 'projects';
summaryMessage +=
`\n\n\nTested ${results.length} ${projects}` +
summariseVulnerableResults(vulnerableResults, options) +
summariseErrorResults(errorResultsLength) +
'\n';
}
}

if (foundVulnerabilities) {
response += chalk.bold.red(summaryMessage);
response += EOL + EOL;

if (isIacShareResultsOptions(options)) {
response += formatShareResultsOutput(iacOutputMeta!) + EOL.repeat(2);
}

const error = new Error(response) as any;
// take the code of the first problem to go through error
// translation
// HACK as there can be different errors, and we pass only the
// first one
error.code = vulnerableResults[0].code || 'VULNS';
error.userMessage = vulnerableResults[0].userMessage;
error.jsonStringifiedResults = stringifiedJsonData;
error.sarifStringifiedResults = stringifiedSarifData;
throw error;
}

response += chalk.bold.green(summaryMessage);

if (isIacShareResultsOptions(options)) {
response += formatShareResultsOutput(iacOutputMeta!) + EOL.repeat(2);
}

return TestCommandResult.createHumanReadableTestCommandResult(
response,
stringifiedJsonData,
stringifiedSarifData,
);
}

function isOutsideCurrentWorkingDirectory(p: string): boolean {
return path.relative(process.cwd(), p).includes('..');
}

function printCurrentWorkingDirectoryTraversalWarning() {
let msg = '';

msg +=
'Warning: Scanning paths outside the current working directory is deprecated and' +
EOL;
msg +=
'will be removed in the future. Please see the documentation for further details:' +
EOL +
EOL;
msg +=
' https://docs.snyk.io/products/snyk-infrastructure-as-code/snyk-cli-for-infrastructure-as-code/test-your-configuration-files' +
EOL;

console.log(chalk.yellow(msg));
}
22 changes: 11 additions & 11 deletions src/lib/iac/drift/driftctl.ts
Original file line number Diff line number Diff line change
@@ -35,29 +35,29 @@ export const DCTL_EXIT_CODES = {
EXIT_ERROR: 2,
};

export const driftctlVersion = 'v0.32.1';
export const driftctlVersion = 'v0.34.1';

const driftctlChecksums = {
'driftctl_windows_386.exe':
'225de60d9179dda00780df18f168473bfc30d23222bbf87aa9ee856d65e995cf',
'345229e23b69ba02f241eabb693243f721a67627691aa6c589f13bf8467fe4ee',
driftctl_darwin_amd64:
'd8665a9d9eab5254447053e5a35be93ef6d5f42069e1b5b4083d4503cd79f572',
'f764b70a4de1a86dc350c017885c7213e9914dac2831fa1b627aeb9b8b04313a',
driftctl_linux_386:
'2469606111d599f5d72970acfccfa75ef43635c31bb4547a071514e91318ad4b',
'd50e33199500f49e27dad3cc0c44c0ce9f6f0e221e5c0842fbd4f2ee7a5ac295',
driftctl_linux_amd64:
'd262961eb6ee005dd65d8ad481801d3b4d30184a8b8aa03c370669729cab07a9',
'1a52aed21697e385a12b0ad90dab2a6906e3f0f66a3566a8c9a97db90ee3506c',
driftctl_linux_arm64:
'1b04652ed21c137ec69ebf2cde2d771da3b59a6d2ddad0de31efd4375174a2b2',
'9794d32100515062763af7d9e7d38494fc4e370918bacfdf9264080b9b14566e',
'driftctl_windows_arm64.exe':
'03772784258c08a8ab6634a9801dcf7671f6648ab0f6b47a939e84364b468cb9',
'a27384720cddbd4a6971e02f9c3bb4c7a45a1f67795df1ae5ce5465021df72b2',
driftctl_darwin_arm64:
'c04e520a9720abddb9e35b556497bd866abe7d2e0d34180ff5275cc45e0e383e',
'e9d2554f98fa7e674ec56e35bc0b5197f5c4cc35302d99bb7ef2789a6320b1be',
'driftctl_windows_arm.exe':
'65c58c0495e4a0741216cb453e37774b17a615f02e532881e2b2473a2ac83c7c',
'04e60682b98ee5b205ba3bdf79f7be903500989591089788b02f402d8733e7b7',
driftctl_linux_arm:
'd32125770fefe789695e939a9be39c371ec4e48a4743efe24edf90d4b1ee0aca',
'05d555436c98d09c501cdef5500e6137b0505008c30e08166f977c3492cf98b2',
'driftctl_windows_amd64.exe':
'46ab4c26231e01e336d35466136da72718ae51af35956875293c7f1e4e680307',
'7fde45b7e04d8b7495b13fb11fe162e9137bd63e6fde6b8bcc932d0ffe9bf80f',
};

const dctlBaseUrl = 'https://static.snyk.io/cli/driftctl/';