Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENGPROD-6654] Ignore policies from a folder #6

Merged
merged 3 commits into from
Feb 15, 2024

Conversation

dstrelbytskyi
Copy link

Description

Adjusted the --ignore-policy option to also support reading the *.rego policies files from a folder.

> trivy image -h
...
Report Flags
  ...
  -f, --format string              format (table,json,template,sarif,cyclonedx,spdx,spdx-json,github,cosign-vuln) (default "table")
      --ignore-policy string       specify the Rego file path (or dir path with Rego files) to evaluate each vulnerability
  ...

The change is backward compatible. As usual, the --ignore-policy ignore.rego option can be used.
If the option value is a filesystem directory it will recursively search for *.rego files in it and apply each found one to the scan results.

> ./trivy image --ignore-policy /path/to/folder/with/regos image_to_scan

Related issues

  • Close #XXX

Related PRs

  • #XXX
  • #YYY

Remove this section if you don't have related PRs.

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@@ -69,11 +72,29 @@ func FilterResult(ctx context.Context, result *types.Result, ignoreConf IgnoreCo
if opt.PolicyFile != "" {
var err error
var ignored int
filteredVulns, filteredMisconfs, ignored, filteredSecrets, filteredLicenses, err = applyPolicy(ctx, filteredVulns, filteredMisconfs, filteredSecrets, filteredLicenses, opt.PolicyFile)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just subsequently exec this function for each target ignore rego file (either specified or found in the folder if the input value is the dir path) in the loop below.

Copy link
Member

@carsongee carsongee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I just had some output recommendations, but the approach looks good to me!

pkg/result/filter.go Outdated Show resolved Hide resolved
return xerrors.Errorf("failed to find policy files: %w", err)
}
} else {
policyFiles = append(policyFiles, opt.PolicyFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like debug output to show which policy files it found when --debug is used so I can verify it is seeing the policy files I think it should load (or not)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. And I was using exactly that debug logging during development 😄
The only inconvenience is that the ignore policies are loaded and applied for every result filtration.
So, I've added the logging of what result is being filtered and then the applied policies.

...
2024-02-15T13:51:33.946+0200	INFO	Detecting pip vulnerabilities...
2024-02-15T13:51:33.946+0200	DEBUG	Detecting library vulnerabilities, type: pip, path: requirements.txt
2024-02-15T13:51:33.949+0200	DEBUG	Detecting library vulnerabilities, type: pip, path: api_client/requirements.txt
2024-02-15T13:51:33.949+0200	DEBUG	Detecting library vulnerabilities, type: pip, path: dbt/requirements.txt
2024-02-15T13:51:33.949+0200	DEBUG	Detecting library vulnerabilities, type: npm, path: engprod/package-lock.json
2024-02-15T13:51:33.951+0200	DEBUG	Detecting library vulnerabilities, type: pip, path: misc/monitoring/searchblox/requirements.txt
--- NEW ---
2024-02-15T13:51:33.968+0200	DEBUG	Filtering result with ignore policies, type: pom, path: node_modules/serverless/docs/providers/openwhisk/examples/hello-world/java/pom.xml
2024-02-15T13:51:33.968+0200	DEBUG	Applying ignore policy: engprod/only_package.rego
2024-02-15T13:51:33.970+0200	DEBUG	Applying ignore policy: engprod/subdir/flask.rego
2024-02-15T13:51:33.972+0200	DEBUG	Filtering result with ignore policies, type: pom, path: node_modules/serverless/lib/plugins/aws/invoke-local/runtime-wrappers/java/pom.xml
2024-02-15T13:51:33.972+0200	DEBUG	Applying ignore policy: engprod/only_package.rego
2024-02-15T13:51:33.973+0200	DEBUG	Applying ignore policy: engprod/subdir/flask.rego
2024-02-15T13:51:33.974+0200	DEBUG	Filtering result with ignore policies, type: npm, path: package-lock.json
2024-02-15T13:51:33.974+0200	DEBUG	Applying ignore policy: engprod/only_package.rego
2024-02-15T13:51:33.975+0200	DEBUG	Applying ignore policy: engprod/subdir/flask.rego
2024-02-15T13:51:33.976+0200	DEBUG	Filtering result with ignore policies, type: pip, path: requirements.txt
2024-02-15T13:51:33.976+0200	DEBUG	Applying ignore policy: engprod/only_package.rego
2024-02-15T13:51:33.977+0200	DEBUG	Applying ignore policy: engprod/subdir/flask.rego

@dstrelbytskyi dstrelbytskyi merged commit 657e496 into main_datarobot Feb 15, 2024
1 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants