/
get_vulnerabilities.go
94 lines (80 loc) · 2.67 KB
/
get_vulnerabilities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cmd
import (
"context"
"fmt"
"io"
clientset "github.com/aquasecurity/starboard/pkg/generated/clientset/versioned"
"github.com/aquasecurity/starboard/pkg/kube"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
func NewGetVulnerabilitiesCmd(executable string, cf *genericclioptions.ConfigFlags, outWriter io.Writer) *cobra.Command {
cmd := &cobra.Command{
Aliases: []string{"vulns", "vuln"},
Use: "vulnerabilities (NAME | TYPE/NAME)",
Short: "Get vulnerabilities report",
Long: `Get vulnerabilities report for the specified workload
TYPE is a Kubernetes workload. Shortcuts and API groups will be resolved, e.g. 'po' or 'deployments.apps'.
NAME is the name of a particular Kubernetes workload.
`,
Example: fmt.Sprintf(` # Get vulnerabilities for a Deployment with the specified name
%[1]s get vulnerabilities.aquasecurity.github.io deploy/nginx
# Get vulnerabilities for a Deployment with the specified name in the specified namespace
%[1]s get vulnerabilities deploy/nginx -n staging
# Get vulnerabilities for a ReplicaSet with the specified name
%[1]s get vulns replicaset/nginx
# Get vulnerabilities for a CronJob with the specified name in JSON output format
%[1]s get vuln cj/my-job -o json`, executable),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx := context.Background()
config, err := cf.ToRESTConfig()
if err != nil {
return
}
client, err := clientset.NewForConfig(config)
if err != nil {
return
}
ns, _, err := cf.ToRawKubeConfigLoader().Namespace()
if err != nil {
return
}
mapper, err := cf.ToRESTMapper()
if err != nil {
return
}
workload, _, err := WorkloadFromArgs(mapper, ns, args)
if err != nil {
return
}
list, err := client.AquasecurityV1alpha1().
VulnerabilityReports(workload.Namespace).
List(ctx, metav1.ListOptions{
LabelSelector: labels.Set{
kube.LabelResourceKind: string(workload.Kind),
kube.LabelResourceName: workload.Name,
kube.LabelResourceNamespace: workload.Namespace,
}.
String(),
})
if err != nil {
return fmt.Errorf("list vulnerability reports: %v", err)
}
format := cmd.Flag("output").Value.String()
printer, err := genericclioptions.NewPrintFlags("").
WithTypeSetter(GetScheme()).
WithDefaultOutput(format).
ToPrinter()
if err != nil {
return fmt.Errorf("create printer: %v", err)
}
if err := printer.PrintObj(list, outWriter); err != nil {
return fmt.Errorf("print vulnerability reports: %v", err)
}
return nil
},
}
return cmd
}