Skip to content

Commit 5c37361

Browse files
authoredJul 29, 2024··
feat(vuln): add --pkg-relationships (#7237)
Signed-off-by: knqyf263 <[email protected]>
1 parent f35f4a5 commit 5c37361

27 files changed

+576
-274
lines changed
 

Diff for: ‎docs/docs/references/configuration/cli/trivy_filesystem.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ trivy filesystem [flags] PATH
6363
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
6464
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
6565
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.
66-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
66+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
67+
--pkg-types strings list of package types (os,library) (default [os,library])
6768
--redis-ca string redis ca file location, if using redis as cache backend
6869
--redis-cert string redis certificate file location, if using redis as cache backend
6970
--redis-key string redis key file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_image.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ trivy image [flags] IMAGE_NAME
8181
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
8282
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
8383
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.
84-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
84+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
85+
--pkg-types strings list of package types (os,library) (default [os,library])
8586
--platform string set platform in the form os/arch if image is multi-platform capable
8687
--podman-host string unix podman socket path to use for podman scanning
8788
--redis-ca string redis ca file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_kubernetes.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ trivy kubernetes [flags] [CONTEXT]
7878
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
7979
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
8080
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.
81-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
81+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
82+
--pkg-types strings list of package types (os,library) (default [os,library])
8283
--qps float specify the maximum QPS to the master from this client (default 5)
8384
--redis-ca string redis ca file location, if using redis as cache backend
8485
--redis-cert string redis certificate file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_repository.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ trivy repository [flags] (REPO_PATH | REPO_URL)
6363
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
6464
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
6565
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.
66-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
66+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
67+
--pkg-types strings list of package types (os,library) (default [os,library])
6768
--redis-ca string redis ca file location, if using redis as cache backend
6869
--redis-cert string redis certificate file location, if using redis as cache backend
6970
--redis-key string redis key file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_rootfs.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ trivy rootfs [flags] ROOTDIR
6565
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
6666
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
6767
--password strings password. Comma-separated passwords allowed. TRIVY_PASSWORD should be used for security reasons.
68-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
68+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
69+
--pkg-types strings list of package types (os,library) (default [os,library])
6970
--redis-ca string redis ca file location, if using redis as cache backend
7071
--redis-cert string redis certificate file location, if using redis as cache backend
7172
--redis-key string redis key file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_sbom.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ trivy sbom [flags] SBOM_PATH
4343
--offline-scan do not issue API requests to identify dependencies
4444
-o, --output string output file name
4545
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
46-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
46+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
47+
--pkg-types strings list of package types (os,library) (default [os,library])
4748
--redis-ca string redis ca file location, if using redis as cache backend
4849
--redis-cert string redis certificate file location, if using redis as cache backend
4950
--redis-key string redis key file location, if using redis as cache backend

Diff for: ‎docs/docs/references/configuration/cli/trivy_vm.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ trivy vm [flags] VM_IMAGE
5656
-o, --output string output file name
5757
--output-plugin-arg string [EXPERIMENTAL] output plugin arguments
5858
--parallel int number of goroutines enabled for parallel scanning, set 0 to auto-detect parallelism (default 5)
59-
--pkg-types strings comma-separated list of package types (os,library) (default [os,library])
59+
--pkg-relationships strings list of package relationships (unknown,root,direct,indirect) (default [unknown,root,direct,indirect])
60+
--pkg-types strings list of package types (os,library) (default [os,library])
6061
--redis-ca string redis ca file location, if using redis as cache backend
6162
--redis-cert string redis certificate file location, if using redis as cache backend
6263
--redis-key string redis key file location, if using redis as cache backend

Diff for: ‎docs/docs/scanner/vulnerability.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ Currently, specifying a username and password is not supported.
202202
This section describes vulnerability-specific configuration.
203203
Other common options are documented [here](../configuration/index.md).
204204

205-
### Enabling a subset of package types
205+
### Enabling a Subset of Package Types
206+
206207
It's possible to only enable certain package types if you prefer.
207208
You can do so by passing the `--pkg-types` option.
208209
This flag takes a comma-separated list of package types.
@@ -268,6 +269,45 @@ Total: 7 (UNKNOWN: 0, LOW: 1, MEDIUM: 1, HIGH: 3, CRITICAL: 2)
268269
269270
</details>
270271
272+
!!! info
273+
This flag filters the packages themselves, so it also affects the `--list-all-pkgs` option and SBOM generation.
274+
275+
### Filtering by Package Relationships
276+
277+
278+
Trivy supports filtering vulnerabilities based on the relationship of packages within a project.
279+
This is achieved through the `--pkg-relationships` flag.
280+
This feature allows you to focus on vulnerabilities in specific types of dependencies, such as only those in direct dependencies.
281+
282+
In Trivy, there are four types of package relationships:
283+
284+
1. `root`: The root package being scanned
285+
2. `direct`: Direct dependencies of the root package
286+
3. `indirect`: Transitive dependencies
287+
4. `unknown`: Packages whose relationship cannot be determined
288+
289+
The available relationships may vary depending on the ecosystem.
290+
To see which relationships are supported for a particular project, you can use the JSON output format and check the `Relationship` field:
291+
292+
```
293+
$ trivy repo -f json --list-all-pkgs /path/to/project
294+
```
295+
296+
To scan only the root package and its direct dependencies, you can use the flag as follows:
297+
298+
```
299+
$ trivy repo --pkg-relationships root,direct /path/to/project
300+
```
301+
302+
By default, all relationships are included in the scan.
303+
304+
!!! info
305+
This flag filters the packages themselves, so it also affects the `--list-all-pkgs` option and SBOM generation.
306+
307+
!!! warning
308+
As it may not provide a complete package list, `--pkg-relationships` cannot be used with `--dependency-tree`, `--vex` or SBOM generation.
309+
310+
271311
[^1]: https://github.com/GoogleContainerTools/distroless
272312
273313
[nvd-CVE-2023-0464]: https://nvd.nist.gov/vuln/detail/CVE-2023-0464

Diff for: ‎pkg/commands/app.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ func NewRootCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
243243
}
244244

245245
func NewImageCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
246-
scanFlagGroup := flag.NewScanFlagGroup()
247-
scanFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
248-
249246
reportFlagGroup := flag.NewReportFlagGroup()
250247
report := flag.ReportFormatFlag.Clone()
251248
report.Default = "summary" // override the default value as the summary is preferred for the compliance report
@@ -256,27 +253,28 @@ func NewImageCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
256253
compliance.Values = []string{types.ComplianceDockerCIS160}
257254
reportFlagGroup.Compliance = compliance // override usage as the accepted values differ for each subcommand.
258255

259-
misconfFlagGroup := flag.NewMisconfFlagGroup()
260-
misconfFlagGroup.CloudformationParamVars = nil // disable '--cf-params'
261-
misconfFlagGroup.TerraformTFVars = nil // disable '--tf-vars'
262-
263256
imageFlags := &flag.Flags{
264257
GlobalFlagGroup: globalFlags,
265258
CacheFlagGroup: flag.NewCacheFlagGroup(),
266259
DBFlagGroup: flag.NewDBFlagGroup(),
267260
ImageFlagGroup: flag.NewImageFlagGroup(), // container image specific
268261
LicenseFlagGroup: flag.NewLicenseFlagGroup(),
269-
MisconfFlagGroup: misconfFlagGroup,
262+
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
270263
ModuleFlagGroup: flag.NewModuleFlagGroup(),
264+
PackageFlagGroup: flag.NewPackageFlagGroup(),
271265
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
272266
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
273267
RegoFlagGroup: flag.NewRegoFlagGroup(),
274268
ReportFlagGroup: reportFlagGroup,
275-
ScanFlagGroup: scanFlagGroup,
269+
ScanFlagGroup: flag.NewScanFlagGroup(),
276270
SecretFlagGroup: flag.NewSecretFlagGroup(),
277271
VulnerabilityFlagGroup: flag.NewVulnerabilityFlagGroup(),
278272
}
279273

274+
imageFlags.PackageFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
275+
imageFlags.MisconfFlagGroup.CloudformationParamVars = nil // disable '--cf-params'
276+
imageFlags.MisconfFlagGroup.TerraformTFVars = nil // disable '--tf-vars'
277+
280278
cmd := &cobra.Command{
281279
Use: "image [flags] IMAGE_NAME",
282280
Aliases: []string{"i"},
@@ -342,6 +340,7 @@ func NewFilesystemCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
342340
LicenseFlagGroup: flag.NewLicenseFlagGroup(),
343341
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
344342
ModuleFlagGroup: flag.NewModuleFlagGroup(),
343+
PackageFlagGroup: flag.NewPackageFlagGroup(),
345344
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
346345
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
347346
RegoFlagGroup: flag.NewRegoFlagGroup(),
@@ -400,6 +399,7 @@ func NewRootfsCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
400399
LicenseFlagGroup: flag.NewLicenseFlagGroup(),
401400
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
402401
ModuleFlagGroup: flag.NewModuleFlagGroup(),
402+
PackageFlagGroup: flag.NewPackageFlagGroup(),
403403
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
404404
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
405405
RegoFlagGroup: flag.NewRegoFlagGroup(),
@@ -411,7 +411,7 @@ func NewRootfsCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
411411
rootfsFlags.ReportFlagGroup.ReportFormat = nil // TODO: support --report summary
412412
rootfsFlags.ReportFlagGroup.Compliance = nil // disable '--compliance'
413413
rootfsFlags.ReportFlagGroup.ReportFormat = nil // disable '--report'
414-
rootfsFlags.ScanFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
414+
rootfsFlags.PackageFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
415415
rootfsFlags.CacheFlagGroup.CacheBackend.Default = string(cache.TypeMemory) // Use memory cache by default
416416

417417
cmd := &cobra.Command{
@@ -460,6 +460,7 @@ func NewRepositoryCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
460460
LicenseFlagGroup: flag.NewLicenseFlagGroup(),
461461
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
462462
ModuleFlagGroup: flag.NewModuleFlagGroup(),
463+
PackageFlagGroup: flag.NewPackageFlagGroup(),
463464
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
464465
RegoFlagGroup: flag.NewRegoFlagGroup(),
465466
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
@@ -516,7 +517,6 @@ func NewConvertCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
516517
ScanFlagGroup: &flag.ScanFlagGroup{},
517518
ReportFlagGroup: flag.NewReportFlagGroup(),
518519
}
519-
convertFlags.ReportFlagGroup.PkgTypes = nil // disable '--pkg-types'
520520

521521
cmd := &cobra.Command{
522522
Use: "convert [flags] RESULT_JSON",
@@ -685,7 +685,6 @@ func NewConfigCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
685685
configFlags.ReportFlagGroup.ListAllPkgs = nil // disable '--list-all-pkgs'
686686
configFlags.ReportFlagGroup.ExitOnEOL = nil // disable '--exit-on-eol'
687687
configFlags.ReportFlagGroup.ShowSuppressed = nil // disable '--show-suppressed'
688-
configFlags.ReportFlagGroup.PkgTypes = nil // disable '--pkg-types'
689688
configFlags.ReportFlagGroup.ReportFormat.Usage = "specify a compliance report format for the output" // @TODO: support --report summary for non compliance reports
690689
configFlags.CacheFlagGroup.CacheBackend.Default = string(cache.TypeMemory)
691690

@@ -960,7 +959,6 @@ func NewKubernetesCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
960959
})
961960
scanners.Default = scanners.Values
962961
scanFlags.Scanners = scanners
963-
scanFlags.IncludeDevDeps = nil // disable '--include-dev-deps'
964962

965963
// required only SourceFlag
966964
imageFlags := &flag.ImageFlagGroup{ImageSources: flag.SourceFlag.Clone()}
@@ -997,13 +995,16 @@ func NewKubernetesCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
997995
ImageFlagGroup: imageFlags,
998996
K8sFlagGroup: flag.NewK8sFlagGroup(), // kubernetes-specific flags
999997
MisconfFlagGroup: misconfFlagGroup,
998+
PackageFlagGroup: flag.NewPackageFlagGroup(),
1000999
RegoFlagGroup: flag.NewRegoFlagGroup(),
10011000
ReportFlagGroup: reportFlagGroup,
10021001
ScanFlagGroup: scanFlags,
10031002
SecretFlagGroup: flag.NewSecretFlagGroup(),
10041003
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
10051004
VulnerabilityFlagGroup: flag.NewVulnerabilityFlagGroup(),
10061005
}
1006+
k8sFlags.PackageFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
1007+
10071008
cmd := &cobra.Command{
10081009
Use: "kubernetes [flags] [CONTEXT]",
10091010
Aliases: []string{"k8s"},
@@ -1055,6 +1056,7 @@ func NewVMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
10551056
DBFlagGroup: flag.NewDBFlagGroup(),
10561057
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
10571058
ModuleFlagGroup: flag.NewModuleFlagGroup(),
1059+
PackageFlagGroup: flag.NewPackageFlagGroup(),
10581060
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
10591061
ReportFlagGroup: flag.NewReportFlagGroup(),
10601062
ScanFlagGroup: flag.NewScanFlagGroup(),
@@ -1069,7 +1071,7 @@ func NewVMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
10691071
},
10701072
}
10711073
vmFlags.ReportFlagGroup.ReportFormat = nil // disable '--report'
1072-
vmFlags.ScanFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
1074+
vmFlags.PackageFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
10731075
vmFlags.MisconfFlagGroup.CloudformationParamVars = nil // disable '--cf-params'
10741076
vmFlags.MisconfFlagGroup.TerraformTFVars = nil // disable '--tf-vars'
10751077

@@ -1128,9 +1130,8 @@ func NewSBOMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
11281130
types.VulnerabilityScanner,
11291131
})
11301132
scanFlagGroup := flag.NewScanFlagGroup()
1131-
scanFlagGroup.Scanners = scanners // allow only 'vuln' and 'license' options for '--scanners'
1132-
scanFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
1133-
scanFlagGroup.Parallel = nil // disable '--parallel'
1133+
scanFlagGroup.Scanners = scanners // allow only 'vuln' and 'license' options for '--scanners'
1134+
scanFlagGroup.Parallel = nil // disable '--parallel'
11341135

11351136
licenseFlagGroup := flag.NewLicenseFlagGroup()
11361137
// License full-scan and confidence-level are for file content only
@@ -1141,6 +1142,7 @@ func NewSBOMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
11411142
GlobalFlagGroup: globalFlags,
11421143
CacheFlagGroup: flag.NewCacheFlagGroup(),
11431144
DBFlagGroup: flag.NewDBFlagGroup(),
1145+
PackageFlagGroup: flag.NewPackageFlagGroup(),
11441146
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
11451147
ReportFlagGroup: reportFlagGroup,
11461148
ScanFlagGroup: scanFlagGroup,
@@ -1150,6 +1152,7 @@ func NewSBOMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
11501152
}
11511153

11521154
sbomFlags.CacheFlagGroup.CacheBackend.Default = string(cache.TypeMemory) // Use memory cache by default
1155+
sbomFlags.PackageFlagGroup.IncludeDevDeps = nil // disable '--include-dev-deps'
11531156

11541157
cmd := &cobra.Command{
11551158
Use: "sbom [flags] SBOM_PATH",

0 commit comments

Comments
 (0)
Please sign in to comment.