Skip to content

Commit b5874e3

Browse files
authored
feat(misconf): Add --misconfig-scanners option (#5670)
1 parent 075d8f6 commit b5874e3

File tree

12 files changed

+55
-0
lines changed

12 files changed

+55
-0
lines changed

docs/docs/references/configuration/cli/trivy_aws.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ trivy aws [flags]
8686
--include-non-failures include successes and exceptions, available with '--scanners misconfig'
8787
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
8888
--max-cache-age duration The maximum age of the cloud cache. Cached data will be requeried from the cloud provider if it is older than this. (default 24h0m0s)
89+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
8990
-o, --output string output file name
9091
--policy-bundle-repository string OCI registry URL to retrieve policy bundle from (default "ghcr.io/aquasecurity/trivy-policies:0")
9192
--policy-namespaces strings Rego namespaces

docs/docs/references/configuration/cli/trivy_config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ trivy config [flags] DIR
2929
--ignorefile string specify .trivyignore file (default ".trivyignore")
3030
--include-non-failures include successes and exceptions, available with '--scanners misconfig'
3131
--k8s-version string specify k8s version to validate outdated api by it (example: 1.21.0)
32+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
3233
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
3334
-o, --output string output file name
3435
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.

docs/docs/references/configuration/cli/trivy_filesystem.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ trivy filesystem [flags] PATH
5151
--license-confidence-level float specify license classifier's confidence level (default 0.9)
5252
--license-full eagerly look for licenses in source code headers and license files
5353
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
54+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
5455
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
5556
--no-progress suppress progress bar
5657
--offline-scan do not issue API requests to identify dependencies

docs/docs/references/configuration/cli/trivy_image.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ trivy image [flags] IMAGE_NAME
6969
--license-confidence-level float specify license classifier's confidence level (default 0.9)
7070
--license-full eagerly look for licenses in source code headers and license files
7171
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
72+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
7273
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
7374
--no-progress suppress progress bar
7475
--offline-scan do not issue API requests to identify dependencies

docs/docs/references/configuration/cli/trivy_kubernetes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ trivy kubernetes [flags] { cluster | all | specific resources like kubectl. eg:
6060
--k8s-version string specify k8s version to validate outdated api by it (example: 1.21.0)
6161
--kubeconfig string specify the kubeconfig file path to use
6262
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
63+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
6364
-n, --namespace string specify a namespace to scan
6465
--no-progress suppress progress bar
6566
--node-collector-namespace string specify the namespace in which the node-collector job should be deployed (default "trivy-temp")

docs/docs/references/configuration/cli/trivy_repository.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ trivy repository [flags] (REPO_PATH | REPO_URL)
5151
--license-confidence-level float specify license classifier's confidence level (default 0.9)
5252
--license-full eagerly look for licenses in source code headers and license files
5353
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
54+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
5455
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
5556
--no-progress suppress progress bar
5657
--offline-scan do not issue API requests to identify dependencies

docs/docs/references/configuration/cli/trivy_rootfs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ trivy rootfs [flags] ROOTDIR
5353
--license-confidence-level float specify license classifier's confidence level (default 0.9)
5454
--license-full eagerly look for licenses in source code headers and license files
5555
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
56+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
5657
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
5758
--no-progress suppress progress bar
5859
--offline-scan do not issue API requests to identify dependencies

docs/docs/references/configuration/cli/trivy_vm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ trivy vm [flags] VM_IMAGE
4747
--include-non-failures include successes and exceptions, available with '--scanners misconfig'
4848
--java-db-repository string OCI repository to retrieve trivy-java-db from (default "ghcr.io/aquasecurity/trivy-java-db")
4949
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
50+
--misconfig-scanners strings comma-separated list of misconfig scanners to use for misconfiguration scanning (default [azure-arm,cloudformation,dockerfile,helm,kubernetes,terraform,terraformplan])
5051
--module-dir string specify directory to the wasm modules that will be loaded (default "$HOME/.trivy/modules")
5152
--no-progress suppress progress bar
5253
--offline-scan do not issue API requests to identify dependencies

docs/docs/references/configuration/config-file.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ misconfiguration:
266266
# Same as '--include-non-failures'
267267
# Default is false
268268
include-non-failures: false
269+
270+
# Same as '--miconfig-scanners'
271+
# Default is all scanners
272+
scanners:
273+
- dockerfile
274+
- terraform
269275

270276
# helm value override configurations
271277
# set individual values

docs/docs/scanner/misconfiguration/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ Failures: 2 (MEDIUM: 2, HIGH: 0, CRITICAL: 0)
315315
This section describes misconfiguration-specific configuration.
316316
Other common options are documented [here](../../configuration/index.md).
317317

318+
### Enabling a subset of misconfiguration scanners
319+
It's possible to only enable certain misconfiguration scanners if you prefer. You can do so by passing the `--misconfig-scanners` option.
320+
This flag takes a comma-separated list of configuration scanner types.
321+
```bash
322+
trivy config --misconfig-scanners=terraform,dockerfile .
323+
```
324+
325+
Will only scan for misconfigurations that pertain to Terraform and Dockerfiles.
326+
318327
### Pass custom policies
319328
You can pass policy files or directories including your custom policies through `--policy` option.
320329
This can be repeated for specifying multiple files or directories.

pkg/commands/artifact/run.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/hashicorp/go-multierror"
9+
"github.com/samber/lo"
910
"github.com/spf13/viper"
1011
"golang.org/x/exp/slices"
1112
"golang.org/x/xerrors"
@@ -480,6 +481,14 @@ func disabledAnalyzers(opts flag.Options) []analyzer.Type {
480481
analyzers = append(analyzers, analyzer.TypeSecret)
481482
}
482483

484+
// Filter only enabled misconfiguration scanners
485+
ma, err := filterMisconfigAnalyzers(opts.MisconfigScanners, analyzer.TypeConfigFiles)
486+
if err != nil {
487+
log.Logger.Errorf("Invalid misconfig scanners specified: %s defaulting to use all misconfig scanners", opts.MisconfigScanners)
488+
} else {
489+
analyzers = append(analyzers, ma...)
490+
}
491+
483492
// Do not perform misconfiguration scanning when it is not specified.
484493
if !opts.Scanners.AnyEnabled(types.MisconfigScanner, types.RBACScanner) {
485494
analyzers = append(analyzers, analyzer.TypeConfigFiles...)
@@ -512,6 +521,16 @@ func disabledAnalyzers(opts flag.Options) []analyzer.Type {
512521
return analyzers
513522
}
514523

524+
func filterMisconfigAnalyzers(included, all []analyzer.Type) ([]analyzer.Type, error) {
525+
_, missing := lo.Difference(all, included)
526+
if len(missing) > 0 {
527+
return nil, xerrors.Errorf("invalid misconfiguration scanner specified %s valid scanners: %s", missing, all)
528+
}
529+
530+
log.Logger.Debugf("Enabling misconfiguration scanners: %s", included)
531+
return lo.Without(all, included...), nil
532+
}
533+
515534
func initScannerConfig(opts flag.Options, cacheClient cache.Cache) (ScannerConfig, types.ScanOptions, error) {
516535
target := opts.Target
517536
if opts.Input != "" {

pkg/flag/misconf_flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package flag
33
import (
44
"fmt"
55

6+
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
67
"github.com/aquasecurity/trivy/pkg/policy"
8+
xstrings "github.com/aquasecurity/trivy/pkg/x/strings"
79
)
810

911
// e.g. config yaml:
@@ -73,6 +75,12 @@ var (
7375
Default: fmt.Sprintf("%s:%d", policy.BundleRepository, policy.BundleVersion),
7476
Usage: "OCI registry URL to retrieve policy bundle from",
7577
}
78+
MisconfigScannersFlag = Flag{
79+
Name: "misconfig-scanners",
80+
ConfigName: "misconfiguration.scanners",
81+
Default: xstrings.ToStringSlice(analyzer.TypeConfigFiles),
82+
Usage: "comma-separated list of misconfig scanners to use for misconfiguration scanning",
83+
}
7684
)
7785

7886
// MisconfFlagGroup composes common printer flag structs used for commands providing misconfiguration scanning.
@@ -89,6 +97,7 @@ type MisconfFlagGroup struct {
8997
TerraformTFVars *Flag
9098
CloudformationParamVars *Flag
9199
TerraformExcludeDownloaded *Flag
100+
MisconfigScanners *Flag
92101
}
93102

94103
type MisconfOptions struct {
@@ -104,6 +113,7 @@ type MisconfOptions struct {
104113
TerraformTFVars []string
105114
CloudFormationParamVars []string
106115
TfExcludeDownloaded bool
116+
MisconfigScanners []analyzer.Type
107117
}
108118

109119
func NewMisconfFlagGroup() *MisconfFlagGroup {
@@ -119,6 +129,7 @@ func NewMisconfFlagGroup() *MisconfFlagGroup {
119129
TerraformTFVars: &TfVarsFlag,
120130
CloudformationParamVars: &CfParamsFlag,
121131
TerraformExcludeDownloaded: &TerraformExcludeDownloaded,
132+
MisconfigScanners: &MisconfigScannersFlag,
122133
}
123134
}
124135

@@ -138,6 +149,7 @@ func (f *MisconfFlagGroup) Flags() []*Flag {
138149
f.TerraformTFVars,
139150
f.TerraformExcludeDownloaded,
140151
f.CloudformationParamVars,
152+
f.MisconfigScanners,
141153
}
142154
}
143155

@@ -153,5 +165,6 @@ func (f *MisconfFlagGroup) ToOptions() (MisconfOptions, error) {
153165
TerraformTFVars: getStringSlice(f.TerraformTFVars),
154166
CloudFormationParamVars: getStringSlice(f.CloudformationParamVars),
155167
TfExcludeDownloaded: getBool(f.TerraformExcludeDownloaded),
168+
MisconfigScanners: getUnderlyingStringSlice[analyzer.Type](f.MisconfigScanners),
156169
}, nil
157170
}

0 commit comments

Comments
 (0)