-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3046 from snyk/feat/drift-exclude
Change `iac gen-driftignore` to `iac update-exclude-policy`
- Loading branch information
Showing
15 changed files
with
429 additions
and
259 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 was deleted.
Oops, something went wrong.
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,46 @@ | ||
# snyk iac update-exclude-policy -- generate ignore rules based on scan result | ||
|
||
## Usage | ||
|
||
`snyk iac update-exclude-policy [<OPTIONS>]` | ||
|
||
## Description | ||
|
||
The `snyk iac update-exclude-policy` can generate exclude policy rules to be used by `snyk iac scan`. | ||
|
||
## Exit codes | ||
|
||
Possible exit codes and their meaning: | ||
|
||
**0**: success, exclude rules generated successfully | ||
**1**: error, something wrong happened during exclude rules generation | ||
|
||
## Configure the Snyk CLI | ||
|
||
You can use environment variables to configure the Snyk CLI and also set variables to configure the Snyk CLI to connect with the Snyk API. | ||
See [Configure the Snyk CLI](https://docs.snyk.io/features/snyk-cli/configure-the-snyk-cli). | ||
|
||
## Debug | ||
|
||
Use the `-d` option to output the debug logs. | ||
|
||
## Options | ||
|
||
### `--exclude-changed` | ||
|
||
Exclude resources that changed on cloud provider | ||
|
||
### `--exclude-missing` | ||
|
||
Exclude missing resources | ||
|
||
### `--exclude-unmanaged` | ||
|
||
Exclude resources not managed by IaC | ||
|
||
## Usage | ||
|
||
``` | ||
$ snyk iac scan --output=json://output.json | ||
$ snyk iac describe --json --all | snyk iac update-exclude-policy | ||
``` |
This file was deleted.
Oops, something went wrong.
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,57 @@ | ||
import { MethodArgs } from '../args'; | ||
import { processCommandArgs } from './process-command-args'; | ||
import * as legacyError from '../../lib/errors/legacy-errors'; | ||
import * as fs from 'fs'; | ||
import * as snykPolicyLib from 'snyk-policy'; | ||
import { getIacOrgSettings } from './test/iac-local-execution/org-settings/get-iac-org-settings'; | ||
import { UnsupportedEntitlementCommandError } from './test/iac-local-execution/assert-iac-options-flag'; | ||
import config from '../../lib/config'; | ||
import { | ||
parseDriftAnalysisResults, | ||
updateExcludeInPolicy, | ||
} from '../../lib/iac/drift'; | ||
import { Policy } from '../../lib/policy/find-and-load-policy'; | ||
|
||
export default async (...args: MethodArgs): Promise<any> => { | ||
const { options } = processCommandArgs(...args); | ||
|
||
// Ensure that this update-exclude-policy command can only be runned when using `snyk iac update-exclude-policy` | ||
// Avoid `snyk update-exclude-policy` direct usage | ||
if (options.iac != true) { | ||
return legacyError('update-exclude-policy'); | ||
} | ||
|
||
// Ensure that we are allowed to run that command | ||
// by checking the entitlement | ||
const orgPublicId = options.org ?? config.org; | ||
const iacOrgSettings = await getIacOrgSettings(orgPublicId); | ||
if (!iacOrgSettings.entitlements?.iacDrift) { | ||
throw new UnsupportedEntitlementCommandError( | ||
'update-exclude-policy', | ||
'iacDrift', | ||
); | ||
} | ||
|
||
try { | ||
// There's an open bug for this in Windows in the current version of node when called with no stdinput. | ||
// See https://github.com/nodejs/node/issues/19831 | ||
// The actual error handling behavior is enough for now but may be improved if needed | ||
const analysis = parseDriftAnalysisResults(fs.readFileSync(0).toString()); | ||
let policy: Policy; | ||
try { | ||
policy = await snykPolicyLib.load(); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
// policy file does not exist - create it | ||
policy = await snykPolicyLib.create(); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
await updateExcludeInPolicy(policy, analysis, options); | ||
await snykPolicyLib.save(policy); | ||
} catch (e) { | ||
const err = new Error('Error running `iac update-exclude-policy` ' + e); | ||
return Promise.reject(err); | ||
} | ||
}; |
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
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
Oops, something went wrong.