Skip to content

Commit d2fd088

Browse files
authored
Merge pull request #3792 from snyk/feat/add-var-file-support
feat: add --var-file support
2 parents 544b0f1 + 537372d commit d2fd088

File tree

6 files changed

+55
-11
lines changed

6 files changed

+55
-11
lines changed

src/cli/commands/test/iac/local-execution/assert-iac-options-flag.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ export class FeatureFlagError extends CustomError {
7878
}
7979

8080
export class FlagValueError extends CustomError {
81-
constructor(key: string, value: string) {
81+
constructor(key: string, value: string, supportedValues: string) {
8282
const flag = getFlagName(key);
83-
const msg = `Unsupported value "${value}" provided to flag "${flag}".\nSupported values are: ${SUPPORTED_TF_PLAN_SCAN_MODES.join(
84-
', ',
85-
)}`;
83+
const msg = `Unsupported value "${value}" provided to flag "${flag}".\nSupported values are: ${supportedValues}`;
8684
super(msg);
8785
this.code = IaCErrorCodes.FlagValueError;
8886
this.strCode = getErrorStringCode(this.code);
@@ -152,7 +150,11 @@ export function assertTerraformPlanModes(scanModeArgValue: string) {
152150
scanModeArgValue as TerraformPlanScanMode,
153151
)
154152
) {
155-
throw new FlagValueError('scan', scanModeArgValue);
153+
throw new FlagValueError(
154+
'scan',
155+
scanModeArgValue,
156+
SUPPORTED_TF_PLAN_SCAN_MODES.join(', '),
157+
);
156158
}
157159
}
158160

src/cli/commands/test/iac/v2/assert-iac-options.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { existsSync } from 'fs';
2+
import { extname } from 'path';
3+
import { SEVERITIES, SEVERITY } from '../../../../../lib/snyk-test/common';
4+
5+
import { InvalidVarFilePath } from '../local-execution';
16
import {
27
assertTerraformPlanModes,
38
FlagError,
9+
FlagValueError,
410
} from '../local-execution/assert-iac-options-flag';
511
import { IaCTestFlags } from '../local-execution/types';
612

@@ -40,7 +46,36 @@ export function assertIacV2Options(options: IaCTestFlags): void {
4046
}
4147
}
4248

49+
if (options.severityThreshold) {
50+
assertSeverityOptions(options.severityThreshold);
51+
}
52+
53+
if (options['var-file']) {
54+
assertVarFileOptions(options['var-file']);
55+
}
56+
4357
if (options.scan) {
4458
assertTerraformPlanModes(options.scan as string);
4559
}
4660
}
61+
62+
function assertSeverityOptions(severity: SEVERITY) {
63+
const validSeverityOptions = SEVERITIES.map((s) => s.verboseName);
64+
65+
if (!validSeverityOptions.includes(severity)) {
66+
throw new FlagValueError(
67+
'severityThreshold',
68+
severity,
69+
validSeverityOptions.join(', '),
70+
);
71+
}
72+
}
73+
74+
function assertVarFileOptions(filePath: string) {
75+
if (!existsSync(filePath)) {
76+
throw new InvalidVarFilePath(filePath);
77+
}
78+
if (extname(filePath) !== '.tfvars') {
79+
throw new FlagValueError('var-file', filePath, '.tfvars file');
80+
}
81+
}

src/cli/commands/test/iac/v2/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ async function prepareTestConfig(
5959
const attributes = parseAttributes(options);
6060
const policy = await findAndLoadPolicy(process.cwd(), 'iac', options);
6161
const scan = options.scan ?? 'resource-changes';
62+
const varFile = options['var-file'];
6263

6364
return {
6465
paths,
@@ -75,6 +76,7 @@ async function prepareTestConfig(
7576
remoteRepoUrl,
7677
policy: policy?.toString(),
7778
scan,
79+
varFile,
7880
depthDetection,
7981
};
8082
}

src/lib/iac/test/v2/local-cache/policy-engine/constants/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as os from 'os';
22

3-
const policyEngineChecksums = `104f3a8d8d1835f9621007fb7976a837ee8946510f41f7fc50323f728cebb21c snyk-iac-test_0.26.0_Darwin_arm64
4-
61bfc743d4392952eb7de3f3c4cdb6e0dfb4a491d0ca24d67c929fc3656d6c5f snyk-iac-test_0.26.0_Linux_x86_64
5-
73847b5bcc0f42cc8acd918f0dff97ee917a64ce84991785a8e6c46a6c4bc6f2 snyk-iac-test_0.26.0_Linux_arm64
6-
ac9100c8a1314a22fe7db7df8faa7d6be0aa6ba986f2db172f727fe004a0853d snyk-iac-test_0.26.0_Windows_x86_64.exe
7-
ad2983ff583989608e259441de12b6871d9e9dcb994eb81214e9dbb14d3b3dd4 snyk-iac-test_0.26.0_Darwin_x86_64
8-
c7de20ee54fd66c885e2bbe37b8c1d533464a525a5abdbc1d86a6a5c8a76b2b8 snyk-iac-test_0.26.0_Windows_arm64.exe
3+
const policyEngineChecksums = `283cb07a894f8252733e6634bef84fbc4fe98eac338239493753e20477150abb snyk-iac-test_0.27.0_Darwin_arm64
4+
55c6cae0b4805047d0f0d8f3eea74f12a4233211499cc2f006cee633f1f2e7b8 snyk-iac-test_0.27.0_Windows_x86_64.exe
5+
7a845e2108c309a7bde435342b69d3ed172a36971779dbc2e1a9a96582f1c4fb snyk-iac-test_0.27.0_Windows_arm64.exe
6+
a06de762874686612d9d42b2eb165979f334413f6460a675f0559e8e56a264dc snyk-iac-test_0.27.0_Linux_x86_64
7+
ac3ece2e1d59927330c996d968dc5bf84faaa766f85402b56b3ae15fe2fae313 snyk-iac-test_0.27.0_Linux_arm64
8+
d96eda3334548db4dc17ea9892b94f48a3a4187af13090118e04cdbd23c821b7 snyk-iac-test_0.27.0_Darwin_x86_64
99
`;
1010

1111
export const policyEngineVersion = getPolicyEngineVersion();

src/lib/iac/test/v2/scan/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ function processFlags(
146146
flags.push('-remote-repo-url', options.remoteRepoUrl);
147147
}
148148

149+
if (options.varFile) {
150+
flags.push('-var-file', options.varFile);
151+
}
152+
149153
return flags;
150154
}
151155

src/lib/iac/test/v2/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export interface TestConfig {
1717
remoteRepoUrl?: string;
1818
policy?: string;
1919
scan: string;
20+
varFile?: string;
2021
depthDetection?: number;
2122
}

0 commit comments

Comments
 (0)