Skip to content

Commit

Permalink
Merge pull request #3520 from snyk/feat/add-project-attributes
Browse files Browse the repository at this point in the history
feat: add project attributes support in --experimental
  • Loading branch information
YairZ101 authored Aug 4, 2022
2 parents 4eee185 + 08791f8 commit 857b74f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/cli/commands/test/iac/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { spinnerMessage } from '../../../../../lib/formatters/iac-output';
import { buildOutput } from '../../../../../lib/iac/test/v2/output';
import { getIacOrgSettings } from '../local-execution/org-settings/get-iac-org-settings';
import { Options, TestOptions } from '../../../../../lib/types';
import { generateProjectAttributes } from '../../../monitor';

export async function test(
paths: string[],
Expand Down Expand Up @@ -42,7 +43,7 @@ export async function test(

async function prepareTestConfig(
paths: string[],
options: any,
options: Options & TestOptions,
): Promise<TestConfig> {
const systemCachePath = config.CACHE_PATH ?? envPaths('snyk').cache;
const iacCachePath = pathLib.join(systemCachePath, 'iac');
Expand All @@ -51,6 +52,8 @@ async function prepareTestConfig(
const org = (options.org as string) || config.org;
const orgSettings = await getIacOrgSettings(org);

const attributes = parseAttributes(options);

return {
paths,
iacCachePath,
Expand All @@ -59,5 +62,12 @@ async function prepareTestConfig(
userRulesBundlePath: config.IAC_BUNDLE_PATH,
userPolicyEnginePath: config.IAC_POLICY_ENGINE_PATH,
severityThreshold: options.severityThreshold,
attributes,
};
}

function parseAttributes(options: Options & TestOptions) {
if (options.report) {
return generateProjectAttributes(options);
}
}
38 changes: 33 additions & 5 deletions src/lib/iac/test/v2/scan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ function scanWithConfig(
rulesBundlePath: string,
configPath: string,
): SnykIacTestOutput {
const args = ['-bundle', rulesBundlePath, '-config', configPath];

if (options.severityThreshold) {
args.push('-severity-threshold', options.severityThreshold);
}
const args = processFlags(options, rulesBundlePath, configPath);

args.push(...options.paths);

Expand Down Expand Up @@ -73,6 +69,38 @@ function scanWithConfig(
return output;
}

function processFlags(
options: TestConfig,
rulesBundlePath: string,
configPath: string,
) {
const flags = ['-bundle', rulesBundlePath, '-config', configPath];

if (options.severityThreshold) {
flags.push('-severity-threshold', options.severityThreshold);
}

if (options.attributes?.criticality) {
flags.push(
'-project-business-criticality',
options.attributes.criticality.join(','),
);
}

if (options.attributes?.environment) {
flags.push(
'-project-environment',
options.attributes.environment.join(','),
);
}

if (options.attributes?.lifecycle) {
flags.push('-project-lifecycle', options.attributes.lifecycle.join(','));
}

return flags;
}

function createConfig(options: TestConfig): string {
try {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snyk-'));
Expand Down
2 changes: 2 additions & 0 deletions src/lib/iac/test/v2/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IacOrgSettings } from '../../../../cli/commands/test/iac/local-execution/types';
import { SEVERITY } from '../../../snyk-test/legacy';
import { ProjectAttributes } from '../../../types';

export interface TestConfig {
paths: string[];
Expand All @@ -9,4 +10,5 @@ export interface TestConfig {
projectName: string;
orgSettings: IacOrgSettings;
severityThreshold?: SEVERITY;
attributes?: ProjectAttributes;
}

0 comments on commit 857b74f

Please sign in to comment.