-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add assertIaCOptionsFlag()
- Loading branch information
Showing
5 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/cli/commands/test/iac-local-execution/assert-iac-options-flag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { CustomError } from '../../../../lib/errors'; | ||
import { IaCTestOptions } from '../../../../lib/types'; | ||
import { args } from '../../../args'; | ||
|
||
const keys: (keyof IaCTestOptions)[] = [ | ||
'debug', | ||
'insecure', | ||
'debug', | ||
'experimental', | ||
'detectionDepth', | ||
'severityThreshold', | ||
'json', | ||
'sarif', | ||
'json-file-output', | ||
'sarif-file-output', | ||
'v', | ||
'version', | ||
'h', | ||
'help', | ||
'q', | ||
'quiet', | ||
]; | ||
const allowed = new Set<string>(keys); | ||
|
||
function camelcaseToDash(key: string) { | ||
return key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase()); | ||
} | ||
|
||
class FlagError extends CustomError { | ||
constructor(key) { | ||
const dashes = key.length === 1 ? '-' : '--'; | ||
const flag = camelcaseToDash(key); | ||
const msg = `Unsupported flag "${dashes}${flag}" provided. Run snyk iac test --help for supported flags.`; | ||
super(msg); | ||
this.userMessage = msg; | ||
} | ||
} | ||
|
||
/** | ||
* Validates the command line flags passed to the snyk iac test | ||
* command. The current argument parsing is very permissive and | ||
* allows unknown flags to be provided without valdiation. | ||
* | ||
* For snyk iac we need to explictly validate the flags to avoid | ||
* misconfigurations and typos. For example, if the --experimental | ||
* flag were to be mis-spelled we would end up sending the client | ||
* data to our backend rather than running it locally as intended. | ||
* @param argv command line args passed to the process | ||
*/ | ||
export function assertIaCOptionsFlags(argv: string[]) { | ||
// We process the process.argv so we don't get default values. | ||
const parsed = args(argv); | ||
for (const key of Object.keys(parsed.options)) { | ||
// The _ property is a special case that contains non | ||
// flag strings passed to the command line (usually files) | ||
// and `iac` is the command provided. | ||
if (key !== '_' && key !== 'iac' && !allowed.has(key)) { | ||
throw new FlagError(key); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { IaCTestOptions } from '../../src/lib/types'; | ||
import { assertIaCOptionsFlags } from '../../src/cli/commands/test/iac-local-execution/assert-iac-options-flag'; | ||
|
||
describe('assertIaCOptionsFlags()', () => { | ||
const command = ['node', 'cli', 'iac', 'test']; | ||
const files = ['input.tf']; | ||
|
||
it('accepts all command line flags accepted by the iac command', () => { | ||
const options = [ | ||
'--debug', | ||
'--insecure', | ||
'--experimental', | ||
'--detection-depth', | ||
'--severity-threshold', | ||
'--json', | ||
'--sarif', | ||
'--json-file-output', | ||
'--sarif-file-output', | ||
'-v', | ||
'--version', | ||
'-h', | ||
'--help', | ||
'-q', | ||
'--quiet', | ||
]; | ||
expect(() => | ||
assertIaCOptionsFlags([...command, ...options, ...files]), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('accepts an empty options object', () => { | ||
expect(() => assertIaCOptionsFlags([...command, ...files])).not.toThrow(); | ||
}); | ||
|
||
it('throws an error if an unexpected flag is present', () => { | ||
const options = ['--project-name']; | ||
expect(() => | ||
assertIaCOptionsFlags([...command, ...options, ...files]), | ||
).toThrow(); | ||
}); | ||
|
||
it('throws an error if a flag contains a typo', () => { | ||
const options = ['--experimenta']; | ||
expect(() => | ||
assertIaCOptionsFlags([...command, ...options, ...files]), | ||
).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters