Skip to content

Commit 1a56295

Browse files
authored
feat: node-collector custom namespace support (#4407)
Signed-off-by: chenk <[email protected]>
1 parent 864ad10 commit 1a56295

File tree

5 files changed

+105
-93
lines changed

5 files changed

+105
-93
lines changed

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

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,64 @@ trivy kubernetes [flags] { cluster | all | specific resources like kubectl. eg:
2727
### Options
2828

2929
```
30-
-A, --all-namespaces fetch resources from all cluster namespaces
31-
--cache-backend string cache backend (e.g. redis://localhost:6379) (default "fs")
32-
--cache-ttl duration cache TTL when using redis as cache backend
33-
--clear-cache clear image caches without scanning
34-
--compliance string compliance report to generate (k8s-nsa,k8s-cis, k8s-pss-baseline, k8s-pss-restricted)
35-
--components strings specify which components to scan (default [workload,infra])
36-
--config-data strings specify paths from which data for the Rego policies will be recursively loaded
37-
--config-policy strings specify paths to the Rego policy files directory, applying config files
38-
--context string specify a context to scan
39-
--db-repository string OCI repository to retrieve trivy-db from (default "ghcr.io/aquasecurity/trivy-db")
40-
--dependency-tree [EXPERIMENTAL] show dependency origin tree of vulnerable packages
41-
--download-db-only download/update vulnerability database but don't run a scan
42-
--download-java-db-only download/update Java index database but don't run a scan
43-
--exit-code int specify exit code when any security issues are found
44-
--file-patterns strings specify config file patterns
45-
-f, --format string format (table, json, template, sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln) (default "table")
46-
--helm-set strings specify Helm values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
47-
--helm-set-file strings specify Helm values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
48-
--helm-set-string strings specify Helm string values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
49-
--helm-values strings specify paths to override the Helm values.yaml files
50-
-h, --help help for kubernetes
51-
--ignore-policy string specify the Rego file path to evaluate each vulnerability
52-
--ignore-unfixed display only fixed vulnerabilities
53-
--ignorefile string specify .trivyignore file (default ".trivyignore")
54-
--include-non-failures include successes and exceptions, available with '--scanners config'
55-
--java-db-repository string OCI repository to retrieve trivy-java-db from (default "ghcr.io/aquasecurity/trivy-java-db")
56-
--k8s-version string specify k8s version to validate outdated api by it (example: 1.21.0)
57-
--kubeconfig string specify the kubeconfig file path to use
58-
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
59-
-n, --namespace string specify a namespace to scan
60-
--no-progress suppress progress bar
61-
--offline-scan do not issue API requests to identify dependencies
62-
-o, --output string output file name
63-
--parallel int number (between 1-20) of goroutines enabled for parallel scanning (default 5)
64-
--policy-namespaces strings Rego namespaces
65-
--redis-ca string redis ca file location, if using redis as cache backend
66-
--redis-cert string redis certificate file location, if using redis as cache backend
67-
--redis-key string redis key file location, if using redis as cache backend
68-
--redis-tls enable redis TLS with public certificates, if using redis as cache backend
69-
--rekor-url string [EXPERIMENTAL] address of rekor STL server (default "https://rekor.sigstore.dev")
70-
--report string specify a report format for the output. (all,summary) (default "all")
71-
--reset remove all caches and database
72-
--sbom-sources strings [EXPERIMENTAL] try to retrieve SBOM from the specified sources (oci,rekor)
73-
--scanners string comma-separated list of what security issues to detect (vuln,config,secret,license) (default "vuln,config,secret,rbac")
74-
--secret-config string specify a path to config file for secret scanning (default "trivy-secret.yaml")
75-
-s, --severity string severities of security issues to be displayed (comma separated) (default "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL")
76-
--skip-db-update skip updating vulnerability database
77-
--skip-dirs strings specify the directories where the traversal is skipped
78-
--skip-files strings specify the file paths to skip traversal
79-
--skip-java-db-update skip updating Java index database
80-
--skip-policy-update skip fetching rego policy updates
81-
--slow scan over time with lower CPU and memory utilization
82-
-t, --template string output template
83-
--tf-vars strings specify paths to override the Terraform tfvars files
84-
--tolerations strings specify node-collector job tolerations (example: key1=value1:NoExecute,key2=value2:NoSchedule)
85-
--trace enable more verbose trace output for custom queries
86-
--vuln-type strings comma-separated list of vulnerability types (os,library) (default [os,library])
30+
-A, --all-namespaces fetch resources from all cluster namespaces
31+
--cache-backend string cache backend (e.g. redis://localhost:6379) (default "fs")
32+
--cache-ttl duration cache TTL when using redis as cache backend
33+
--clear-cache clear image caches without scanning
34+
--compliance string compliance report to generate (k8s-nsa,k8s-cis, k8s-pss-baseline, k8s-pss-restricted)
35+
--components strings specify which components to scan (default [workload,infra])
36+
--config-data strings specify paths from which data for the Rego policies will be recursively loaded
37+
--config-policy strings specify paths to the Rego policy files directory, applying config files
38+
--context string specify a context to scan
39+
--db-repository string OCI repository to retrieve trivy-db from (default "ghcr.io/aquasecurity/trivy-db")
40+
--dependency-tree [EXPERIMENTAL] show dependency origin tree of vulnerable packages
41+
--download-db-only download/update vulnerability database but don't run a scan
42+
--download-java-db-only download/update Java index database but don't run a scan
43+
--exit-code int specify exit code when any security issues are found
44+
--file-patterns strings specify config file patterns
45+
-f, --format string format (table, json, template, sarif, cyclonedx, spdx, spdx-json, github, cosign-vuln) (default "table")
46+
--helm-set strings specify Helm values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
47+
--helm-set-file strings specify Helm values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
48+
--helm-set-string strings specify Helm string values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
49+
--helm-values strings specify paths to override the Helm values.yaml files
50+
-h, --help help for kubernetes
51+
--ignore-policy string specify the Rego file path to evaluate each vulnerability
52+
--ignore-unfixed display only fixed vulnerabilities
53+
--ignorefile string specify .trivyignore file (default ".trivyignore")
54+
--include-non-failures include successes and exceptions, available with '--scanners config'
55+
--java-db-repository string OCI repository to retrieve trivy-java-db from (default "ghcr.io/aquasecurity/trivy-java-db")
56+
--k8s-version string specify k8s version to validate outdated api by it (example: 1.21.0)
57+
--kubeconfig string specify the kubeconfig file path to use
58+
--list-all-pkgs enabling the option will output all packages regardless of vulnerability
59+
-n, --namespace string specify a namespace to scan
60+
--no-progress suppress progress bar
61+
--node-collector-namespace string specify the namespace in which the node-collector job should be deployed (default "trivy-temp")
62+
--offline-scan do not issue API requests to identify dependencies
63+
-o, --output string output file name
64+
--parallel int number (between 1-20) of goroutines enabled for parallel scanning (default 5)
65+
--policy-namespaces strings Rego namespaces
66+
--redis-ca string redis ca file location, if using redis as cache backend
67+
--redis-cert string redis certificate file location, if using redis as cache backend
68+
--redis-key string redis key file location, if using redis as cache backend
69+
--redis-tls enable redis TLS with public certificates, if using redis as cache backend
70+
--rekor-url string [EXPERIMENTAL] address of rekor STL server (default "https://rekor.sigstore.dev")
71+
--report string specify a report format for the output. (all,summary) (default "all")
72+
--reset remove all caches and database
73+
--sbom-sources strings [EXPERIMENTAL] try to retrieve SBOM from the specified sources (oci,rekor)
74+
--scanners string comma-separated list of what security issues to detect (vuln,config,secret,license) (default "vuln,config,secret,rbac")
75+
--secret-config string specify a path to config file for secret scanning (default "trivy-secret.yaml")
76+
-s, --severity string severities of security issues to be displayed (comma separated) (default "UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL")
77+
--skip-db-update skip updating vulnerability database
78+
--skip-dirs strings specify the directories where the traversal is skipped
79+
--skip-files strings specify the file paths to skip traversal
80+
--skip-java-db-update skip updating Java index database
81+
--skip-policy-update skip fetching rego policy updates
82+
--slow scan over time with lower CPU and memory utilization
83+
-t, --template string output template
84+
--tf-vars strings specify paths to override the Terraform tfvars files
85+
--tolerations strings specify node-collector job tolerations (example: key1=value1:NoExecute,key2=value2:NoSchedule)
86+
--trace enable more verbose trace output for custom queries
87+
--vuln-type strings comma-separated list of vulnerability types (os,library) (default [os,library])
8788
```
8889

8990
### Options inherited from parent commands

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/aquasecurity/tml v0.6.1
2727
github.com/aquasecurity/trivy-db v0.0.0-20230515061101-378ab9ed302c
2828
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728
29-
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295
29+
github.com/aquasecurity/trivy-kubernetes v0.5.2
3030
github.com/aws/aws-sdk-go v1.44.245
3131
github.com/aws/aws-sdk-go-v2 v1.18.0
3232
github.com/aws/aws-sdk-go-v2/config v1.18.24

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ github.com/aquasecurity/trivy-db v0.0.0-20230515061101-378ab9ed302c h1:mFMfHmb5G
345345
github.com/aquasecurity/trivy-db v0.0.0-20230515061101-378ab9ed302c/go.mod h1:s7x7CTxYeiFf6gPOakSsg4mCD93au4dbYplG4h0FGrs=
346346
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728 h1:0eS+V7SXHgqoT99tV1mtMW6HL4HdoB9qGLMCb1fZp8A=
347347
github.com/aquasecurity/trivy-java-db v0.0.0-20230209231723-7cddb1406728/go.mod h1:Ldya37FLi0e/5Cjq2T5Bty7cFkzUDwTcPeQua+2M8i8=
348-
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295 h1:ZdQMyXrUTNhsjKMiGLNtwIpGkn0Aj7r6eRPzaJlDbYc=
349-
github.com/aquasecurity/trivy-kubernetes v0.4.1-0.20230420095211-019a895da295/go.mod h1:FPtS3hhfzykyaIiAIUg3vovniDP5loM9hHRa8W2+PuU=
348+
github.com/aquasecurity/trivy-kubernetes v0.5.2 h1:EOoEiNphSaypSrlyPSN1bCywPud0Sx+JYe6GZNfvIhU=
349+
github.com/aquasecurity/trivy-kubernetes v0.5.2/go.mod h1:uh1LZLfGnJLxnuYxlcrfCpcsKbwP6ETlmz+fW/HO148=
350350
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
351351
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
352352
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=

pkg/flag/kubernetes_flags.go

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,49 @@ var (
6969
Value: false,
7070
Usage: "fetch resources from all cluster namespaces",
7171
}
72+
NodeCollectorNamespace = Flag{
73+
Name: "node-collector-namespace",
74+
ConfigName: "node.collector.namespace",
75+
Value: "trivy-temp",
76+
Usage: "specify the namespace in which the node-collector job should be deployed",
77+
}
7278
)
7379

7480
type K8sFlagGroup struct {
75-
ClusterContext *Flag
76-
Namespace *Flag
77-
KubeConfig *Flag
78-
Components *Flag
79-
K8sVersion *Flag
80-
Parallel *Flag
81-
Tolerations *Flag
82-
AllNamespaces *Flag
81+
ClusterContext *Flag
82+
Namespace *Flag
83+
KubeConfig *Flag
84+
Components *Flag
85+
K8sVersion *Flag
86+
Parallel *Flag
87+
Tolerations *Flag
88+
AllNamespaces *Flag
89+
NodeCollectorNamespace *Flag
8390
}
8491

8592
type K8sOptions struct {
86-
ClusterContext string
87-
Namespace string
88-
KubeConfig string
89-
Components []string
90-
K8sVersion string
91-
Parallel int
92-
Tolerations []corev1.Toleration
93-
AllNamespaces bool
93+
ClusterContext string
94+
Namespace string
95+
KubeConfig string
96+
Components []string
97+
K8sVersion string
98+
Parallel int
99+
Tolerations []corev1.Toleration
100+
AllNamespaces bool
101+
NodeCollectorNamespace string
94102
}
95103

96104
func NewK8sFlagGroup() *K8sFlagGroup {
97105
return &K8sFlagGroup{
98-
ClusterContext: &ClusterContextFlag,
99-
Namespace: &K8sNamespaceFlag,
100-
KubeConfig: &KubeConfigFlag,
101-
Components: &ComponentsFlag,
102-
K8sVersion: &K8sVersionFlag,
103-
Parallel: &ParallelFlag,
104-
Tolerations: &TolerationsFlag,
105-
AllNamespaces: &AllNamespaces,
106+
ClusterContext: &ClusterContextFlag,
107+
Namespace: &K8sNamespaceFlag,
108+
KubeConfig: &KubeConfigFlag,
109+
Components: &ComponentsFlag,
110+
K8sVersion: &K8sVersionFlag,
111+
Parallel: &ParallelFlag,
112+
Tolerations: &TolerationsFlag,
113+
AllNamespaces: &AllNamespaces,
114+
NodeCollectorNamespace: &NodeCollectorNamespace,
106115
}
107116
}
108117

@@ -120,6 +129,7 @@ func (f *K8sFlagGroup) Flags() []*Flag {
120129
f.Parallel,
121130
f.Tolerations,
122131
f.AllNamespaces,
132+
f.NodeCollectorNamespace,
123133
}
124134
}
125135

@@ -137,14 +147,15 @@ func (f *K8sFlagGroup) ToOptions() (K8sOptions, error) {
137147
}
138148
}
139149
return K8sOptions{
140-
ClusterContext: getString(f.ClusterContext),
141-
Namespace: getString(f.Namespace),
142-
KubeConfig: getString(f.KubeConfig),
143-
Components: getStringSlice(f.Components),
144-
K8sVersion: getString(f.K8sVersion),
145-
Parallel: parallel,
146-
Tolerations: tolerations,
147-
AllNamespaces: getBool(f.AllNamespaces),
150+
ClusterContext: getString(f.ClusterContext),
151+
Namespace: getString(f.Namespace),
152+
KubeConfig: getString(f.KubeConfig),
153+
Components: getStringSlice(f.Components),
154+
K8sVersion: getString(f.K8sVersion),
155+
Parallel: parallel,
156+
Tolerations: tolerations,
157+
AllNamespaces: getBool(f.AllNamespaces),
158+
NodeCollectorNamespace: getString(f.NodeCollectorNamespace),
148159
}, nil
149160
}
150161

pkg/k8s/commands/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err
2222
var artifacts []*artifacts.Artifact
2323
var err error
2424
if opts.Scanners.AnyEnabled(types.MisconfigScanner) && slices.Contains(opts.Components, "infra") {
25-
artifacts, err = trivyk8s.New(cluster, log.Logger).ListArtifactAndNodeInfo(ctx, opts.Tolerations...)
25+
artifacts, err = trivyk8s.New(cluster, log.Logger).ListArtifactAndNodeInfo(ctx, opts.NodeCollectorNamespace, opts.Tolerations...)
2626
if err != nil {
2727
return xerrors.Errorf("get k8s artifacts with node info error: %w", err)
2828
}

0 commit comments

Comments
 (0)