-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathget_configaudit.go
88 lines (75 loc) · 2.53 KB
/
get_configaudit.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
package cmd
import (
"context"
"fmt"
"io"
"github.com/aquasecurity/starboard/pkg/configauditreport"
"github.com/aquasecurity/starboard/pkg/starboard"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func NewGetConfigAuditCmd(executable string, cf *genericclioptions.ConfigFlags, outWriter io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "configaudit (NAME | TYPE/NAME)",
Short: "Get configuration audit report",
Long: `Get configuration audit 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 configuration audit for a Deployment with the specified name
%[1]s get configauditreports.aquasecurity.github.io deploy/nginx
# Get configuration audit for a Deployment with the specified name in the specified namespace
%[1]s get configauditreports deploy/nginx -n staging
# Get configuration audit for a ReplicaSet with the specified name
%[1]s get configaudit replicaset/nginx
# Get vulnerabilities for a CronJob with the specified name in JSON output format
%[1]s get configaudit cj/my-job -o json`, executable),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
kubeConfig, err := cf.ToRESTConfig()
if err != nil {
return err
}
ns, _, err := cf.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
mapper, err := cf.ToRESTMapper()
if err != nil {
return err
}
workload, _, err := WorkloadFromArgs(mapper, ns, args)
if err != nil {
return err
}
scheme := starboard.NewScheme()
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme})
if err != nil {
return err
}
reader := configauditreport.NewReadWriter(kubeClient)
report, err := reader.FindByOwnerInHierarchy(ctx, workload)
if err != nil {
return nil
}
if report == nil {
fmt.Fprintf(outWriter, "No reports found in %s namespace.\n", workload.Namespace)
return nil
}
format := cmd.Flag("output").Value.String()
printer, err := genericclioptions.NewPrintFlags("").
WithTypeSetter(scheme).
WithDefaultOutput(format).
ToPrinter()
if err != nil {
return fmt.Errorf("create printer: %v", err)
}
if err := printer.PrintObj(report, outWriter); err != nil {
return fmt.Errorf("print vulnerability reports: %v", err)
}
return nil
},
}
return cmd
}