-
Notifications
You must be signed in to change notification settings - Fork 197
/
get_clustercompliancereport.go
113 lines (104 loc) · 3.83 KB
/
get_clustercompliancereport.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"context"
"fmt"
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1"
"github.com/aquasecurity/starboard/pkg/compliance"
"github.com/aquasecurity/starboard/pkg/starboard"
"github.com/spf13/cobra"
"io"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func NewGetClusterComplianceReportsCmd(executable string, cf *genericclioptions.ConfigFlags, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "clustercompliancereports (NAME)",
Aliases: []string{"clustercompliance"},
Short: "Get cluster compliance reports",
Long: `Get cluster compliance report for pre-defined spec`,
Example: fmt.Sprintf(` # Get cluster compliance report for specifc spec in JSON output format
%[1]s get clustercompliancereports nsa -o json
# Get compliance detail report for control checks failure in JSON output format
%[1]s get clustercompliancereports nsa -o json --detail`, executable),
RunE: func(cmd *cobra.Command, args []string) error {
logger := ctrl.Log.WithName("reconciler").WithName("clustercompliancereport")
ctx := context.Background()
scheme := starboard.NewScheme()
kubeConfig, err := cf.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to create kubeConfig: %w", err)
}
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme})
if err != nil {
return fmt.Errorf("failed to create kubernetes client: %w", err)
}
namespaceName, err := ComplianceNameFromArgs(args)
if err != nil {
return err
}
var report v1alpha1.ClusterComplianceReport
err = GetComplianceReport(ctx, kubeClient, namespaceName, out, &report)
if err != nil {
return err
}
// generate compliance and compliance failure detail reports
complianceMgr := compliance.NewMgr(kubeClient, logger)
err = complianceMgr.GenerateComplianceReport(ctx, report.Spec)
if err != nil {
return fmt.Errorf("failed to generate report: %w", err)
}
format := cmd.Flag("output").Value.String()
printer, err := genericclioptions.NewPrintFlags("").
WithTypeSetter(scheme).
WithDefaultOutput(format).
ToPrinter()
if err != nil {
return fmt.Errorf("faild to create printer: %w", err)
}
detail, err := cmd.Flags().GetBool("detail")
if err != nil {
return fmt.Errorf("detail flag is not set correctly, check flag usage: %w", err)
}
if !detail {
var complianceReport v1alpha1.ClusterComplianceReport
err := GetComplianceReport(ctx, kubeClient, namespaceName, out, &complianceReport)
if err != nil {
return err
}
if err := printer.PrintObj(&complianceReport, out); err != nil {
return fmt.Errorf("print compliance reports: %w", err)
}
return nil
}
detailNamespaceName, err := ComplianceNameFromArgs(args, "details")
if err != nil {
return err
}
var complianceDetailReport v1alpha1.ClusterComplianceDetailReport
err = GetComplianceReport(ctx, kubeClient, detailNamespaceName, out, &complianceDetailReport)
if err != nil {
return err
}
if err := printer.PrintObj(&complianceDetailReport, out); err != nil {
return fmt.Errorf("print compliance reports: %w", err)
}
return nil
},
}
cmd.PersistentFlags().BoolP("detail", "d", false, "Get compliance detail report for control checks failure")
return cmd
}
func GetComplianceReport(ctx context.Context, client client.Client, namespaceName types.NamespacedName, out io.Writer, report client.Object) error {
err := client.Get(ctx, namespaceName, report)
if err != nil {
if errors.IsNotFound(err) {
fmt.Fprintf(out, "No complaince reports found with name: %s .\n", namespaceName.Name)
return err
}
return fmt.Errorf("failed getting report: %w", err)
}
return nil
}