Skip to content

Commit 3379a79

Browse files
authored
Merge pull request #3069 from snyk/chore/add-warning-for-report-with-custom-rules-cfg-1737
chore: Added warning when custom rules with the `report` command [CFG-1737]
2 parents 2db6329 + d23f67e commit 3379a79

2 files changed

Lines changed: 154 additions & 28 deletions

File tree

src/cli/commands/test/iac-local-execution/rules.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ export async function initRules(
4646
(isOCIRegistryURLProvided || customRulesPath) &&
4747
!(options.sarif || options.json)
4848
) {
49-
console.log(
50-
chalk.hex('#ff9b00')('Using custom rules to generate misconfigurations.'),
51-
);
49+
let userMessage = 'Using custom rules to generate misconfigurations.';
50+
51+
if (options.report) {
52+
userMessage +=
53+
"\nPlease note that your custom rules will not be sent to the Snyk platform, and will not be available on the project's page.";
54+
}
55+
56+
console.log(chalk.hex('#ff9b00')(userMessage));
5257
}
5358

5459
if (isOCIRegistryURLProvided && customRulesPath) {

test/jest/acceptance/iac/custom-rules.spec.ts

Lines changed: 146 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,55 @@ describe('iac test --rules', () => {
6767
`Flag "--rules" is currently not supported for this org. To enable it, please contact snyk support.`,
6868
);
6969
});
70+
71+
describe.each([
72+
['--report flag', 'test --report'],
73+
['report command', 'report'],
74+
])('when used with the %s', (_, testedCommand) => {
75+
it('should resolve successfully', async () => {
76+
const { stderr, exitCode } = await run(
77+
`snyk iac ${testedCommand} --rules=./iac/custom-rules/custom.tar.gz ./iac/terraform/sg_open_ssh.tf`,
78+
);
79+
80+
expect(exitCode).toEqual(1);
81+
expect(stderr).toEqual('');
82+
});
83+
84+
it('should display a message informing of the application of custom rules', async () => {
85+
const { stdout } = await run(
86+
`snyk iac ${testedCommand} --rules=./iac/custom-rules/custom.tar.gz ./iac/terraform/sg_open_ssh.tf`,
87+
);
88+
89+
expect(stdout).toContain(
90+
'Using custom rules to generate misconfigurations.',
91+
);
92+
});
93+
94+
it('should display a warning message for custom rules not being available on the platform', async () => {
95+
const { stdout } = await run(
96+
`snyk iac ${testedCommand} --rules=./iac/custom-rules/custom.tar.gz ./iac/terraform/sg_open_ssh.tf`,
97+
);
98+
99+
expect(stdout).toContain(
100+
"Please note that your custom rules will not be sent to the Snyk platform, and will not be available on the project's page.",
101+
);
102+
});
103+
104+
describe.each(['--json', '--sarif'])(
105+
'when the %s flag is provided',
106+
(testedFormatFlag) => {
107+
it('should not display the warning message for the custom rules not being available on the platform', async () => {
108+
const { stdout } = await run(
109+
`snyk iac ${testedCommand} --rules=./iac/custom-rules/custom.tar.gz ./iac/terraform/sg_open_ssh.tf ${testedFormatFlag}`,
110+
);
111+
112+
expect(stdout).not.toContain(
113+
"Please note that your custom rules will not be sent to the Snyk platform, and will not be available on the project's page.",
114+
);
115+
});
116+
},
117+
);
118+
});
70119
});
71120

72121
describe('custom rules pull from a remote OCI registry', () => {
@@ -149,37 +198,109 @@ describe('custom rules pull from a remote OCI registry', () => {
149198
// process.env.OCI_GCR_REGISTRY_PASSWORD,
150199
// ],
151200
];
152-
test.each(cases)(
153-
'given %p as a registry and correct credentials, it returns a success exit code',
154-
async (
201+
202+
describe.each(cases)(
203+
'given %p as a registry and correct credentials',
204+
(
155205
SNYK_CFG_OCI_REGISTRY_NAME,
156206
SNYK_CFG_OCI_REGISTRY_URL,
157207
SNYK_CFG_OCI_REGISTRY_USERNAME,
158208
SNYK_CFG_OCI_REGISTRY_PASSWORD,
159209
) => {
160-
const { stdout, exitCode } = await run(
161-
`snyk iac test ./iac/terraform/sg_open_ssh.tf`,
162-
{
163-
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
164-
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
165-
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
166-
},
167-
);
168-
expect(SNYK_CFG_OCI_REGISTRY_URL).toBeDefined();
169-
expect(SNYK_CFG_OCI_REGISTRY_USERNAME).toBeDefined();
170-
expect(SNYK_CFG_OCI_REGISTRY_PASSWORD).toBeDefined();
171-
expect(exitCode).toBe(1);
210+
it('should return a success exit code', async () => {
211+
const { stdout, exitCode } = await run(
212+
`snyk iac test ./iac/terraform/sg_open_ssh.tf`,
213+
{
214+
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
215+
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
216+
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
217+
},
218+
);
219+
expect(SNYK_CFG_OCI_REGISTRY_URL).toBeDefined();
220+
expect(SNYK_CFG_OCI_REGISTRY_USERNAME).toBeDefined();
221+
expect(SNYK_CFG_OCI_REGISTRY_PASSWORD).toBeDefined();
222+
expect(exitCode).toBe(1);
172223

173-
expect(stdout).toContain(
174-
'Using custom rules to generate misconfigurations.',
175-
);
176-
expect(stdout).toContain('Testing ./iac/terraform/sg_open_ssh.tf');
177-
expect(stdout).toContain('Infrastructure as code issues:');
178-
expect(stdout).toContain('Missing tags');
179-
expect(stdout).toContain('CUSTOM-1');
180-
expect(stdout).toContain(
181-
'introduced by input > resource > aws_security_group[allow_ssh] > tags',
182-
);
224+
expect(stdout).toContain(
225+
'Using custom rules to generate misconfigurations.',
226+
);
227+
expect(stdout).toContain('Testing ./iac/terraform/sg_open_ssh.tf');
228+
expect(stdout).toContain('Infrastructure as code issues:');
229+
expect(stdout).toContain('Missing tags');
230+
expect(stdout).toContain('CUSTOM-1');
231+
expect(stdout).toContain(
232+
'introduced by input > resource > aws_security_group[allow_ssh] > tags',
233+
);
234+
});
235+
236+
describe.each([
237+
['--report flag', 'test --report'],
238+
['report command', 'report'],
239+
])('when used with the %s', (_, testedCommand) => {
240+
it('should resolve successfully', async () => {
241+
const { exitCode, stderr } = await run(
242+
`snyk iac ${testedCommand} ./iac/terraform/sg_open_ssh.tf`,
243+
{
244+
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
245+
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
246+
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
247+
},
248+
);
249+
250+
expect(exitCode).toEqual(1);
251+
expect(stderr).toContain('');
252+
});
253+
254+
it('should display a message informing of the application of custom rules', async () => {
255+
const { stdout } = await run(
256+
`snyk iac ${testedCommand} ./iac/terraform/sg_open_ssh.tf`,
257+
{
258+
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
259+
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
260+
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
261+
},
262+
);
263+
264+
expect(stdout).toContain(
265+
'Using custom rules to generate misconfigurations.',
266+
);
267+
});
268+
269+
it('should display a warning message for custom rules not being available on the platform', async () => {
270+
const { stdout } = await run(
271+
`snyk iac ${testedCommand} ./iac/terraform/sg_open_ssh.tf`,
272+
{
273+
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
274+
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
275+
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
276+
},
277+
);
278+
279+
expect(stdout).toContain(
280+
"Please note that your custom rules will not be sent to the Snyk platform, and will not be available on the project's page.",
281+
);
282+
});
283+
284+
describe.each(['--json', '--sarif'])(
285+
'when the %s flag is provided',
286+
(testedFormatFlag) => {
287+
it('should not display the warning message for the custom rules not being available on the platform', async () => {
288+
const { stdout } = await run(
289+
`snyk iac ${testedCommand} ./iac/terraform/sg_open_ssh.tf ${testedFormatFlag}`,
290+
{
291+
SNYK_CFG_OCI_REGISTRY_URL: SNYK_CFG_OCI_REGISTRY_URL as string,
292+
SNYK_CFG_OCI_REGISTRY_USERNAME: SNYK_CFG_OCI_REGISTRY_USERNAME as string,
293+
SNYK_CFG_OCI_REGISTRY_PASSWORD: SNYK_CFG_OCI_REGISTRY_PASSWORD as string,
294+
},
295+
);
296+
297+
expect(stdout).not.toContain(
298+
"Please note that your custom rules will not be sent to the Snyk platform, and will not be available on the project's page.",
299+
);
300+
});
301+
},
302+
);
303+
});
183304
},
184305
);
185306

0 commit comments

Comments
 (0)