Skip to content

Commit

Permalink
feat: add support for k8s intrusive flag
Browse files Browse the repository at this point in the history
Signed-off-by: chenk <[email protected]>
  • Loading branch information
chen-keinan committed Mar 13, 2024
1 parent 71da44f commit a8aafb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/flag/kubernetes_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ var (
Shorthand: "A",
Usage: "fetch resources from all cluster namespaces",
}
Intrusive = Flag[bool]{
Name: "intrusive",
ConfigName: "kubernetes.intrusive",
Usage: "When the flag is activated, the node-collector job will be executed, revealing misconfiguration findings detected within the Node.",
}
NodeCollectorNamespace = Flag[string]{
Name: "node-collector-namespace",
ConfigName: "node.collector.namespace",
Expand Down Expand Up @@ -101,6 +106,7 @@ type K8sFlagGroup struct {
Components *Flag[[]string]
K8sVersion *Flag[string]
Tolerations *Flag[[]string]
Intrusive *Flag[bool]
NodeCollectorImageRef *Flag[string]
AllNamespaces *Flag[bool]
NodeCollectorNamespace *Flag[string]
Expand All @@ -121,6 +127,7 @@ type K8sOptions struct {
AllNamespaces bool
NodeCollectorNamespace string
ExcludeOwned bool
Intrusive bool
ExcludeNodes map[string]string
QPS float32
Burst int
Expand All @@ -134,6 +141,7 @@ func NewK8sFlagGroup() *K8sFlagGroup {
Components: ComponentsFlag.Clone(),
K8sVersion: K8sVersionFlag.Clone(),
Tolerations: TolerationsFlag.Clone(),
Intrusive: Intrusive.Clone(),
AllNamespaces: AllNamespaces.Clone(),
NodeCollectorNamespace: NodeCollectorNamespace.Clone(),
ExcludeOwned: ExcludeOwned.Clone(),
Expand Down Expand Up @@ -193,6 +201,7 @@ func (f *K8sFlagGroup) ToOptions() (K8sOptions, error) {
Components: f.Components.Value(),
K8sVersion: f.K8sVersion.Value(),
Tolerations: tolerations,
Intrusive: f.Intrusive.Value(),
AllNamespaces: f.AllNamespaces.Value(),
NodeCollectorNamespace: f.NodeCollectorNamespace.Value(),
ExcludeOwned: f.ExcludeOwned.Value(),
Expand Down
3 changes: 1 addition & 2 deletions pkg/k8s/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"context"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"

k8sArtifacts "github.com/aquasecurity/trivy-kubernetes/pkg/artifacts"
Expand All @@ -28,7 +27,7 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err
return xerrors.Errorf("get k8s artifacts with node info error: %w", err)
}
case types.FormatJSON, types.FormatTable:
if opts.Scanners.AnyEnabled(types.MisconfigScanner) && slices.Contains(opts.Components, "infra") {
if opts.Scanners.AnyEnabled(types.MisconfigScanner) && opts.Intrusive {
artifacts, err = trivyk8s.New(cluster, log.Logger, trivyk8s.WithExcludeOwned(opts.ExcludeOwned)).ListArtifactAndNodeInfo(ctx,
trivyk8s.WithScanJobNamespace(opts.NodeCollectorNamespace),
trivyk8s.WithIgnoreLabels(opts.ExcludeNodes),
Expand Down

0 comments on commit a8aafb6

Please sign in to comment.