-
Notifications
You must be signed in to change notification settings - Fork 42
/
advisor.go
136 lines (103 loc) · 3.1 KB
/
advisor.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package advisor
import (
"encoding/json"
"fmt"
"os"
"github.com/open-policy-agent/opa/ast"
"github.com/sysdiglabs/kube-psp-advisor/advisor/types"
"github.com/sysdiglabs/kube-psp-advisor/advisor/processor"
"github.com/sysdiglabs/kube-psp-advisor/advisor/report"
"k8s.io/api/policy/v1beta1"
k8sJSON "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
type Advisor struct {
podSecurityPolicy *v1beta1.PodSecurityPolicy
OPAModulePolicy *ast.Module
k8sClient *kubernetes.Clientset
processor *processor.Processor
report *report.Report
grants []types.PSPGrant
grantWarnings string
}
// Create an podSecurityPolicy advisor instance
func NewAdvisor(kubeconfig string) (*Advisor, error) {
p, err := processor.NewProcessor(kubeconfig)
if err != nil {
return nil, err
}
return &Advisor{
podSecurityPolicy: nil,
processor: p,
report: nil,
grants: []types.PSPGrant{},
}, nil
}
func (advisor *Advisor) Process(namespace string, excludeNamespaces []string, OPAformat string, OPAdefaultRule bool) error {
advisor.processor.SetNamespace(namespace)
advisor.processor.SetExcludeNamespaces(excludeNamespaces)
cssList, pssList, err := advisor.processor.GetSecuritySpec()
if err != nil {
return err
}
if OPAformat == "opa" {
advisor.OPAModulePolicy = advisor.processor.GenerateOPA(cssList, pssList, OPAdefaultRule)
} else if OPAformat == "psp" {
advisor.podSecurityPolicy = advisor.processor.GeneratePSP(cssList, pssList)
}
advisor.report = advisor.processor.GenerateReport(cssList, pssList)
advisor.grants, advisor.grantWarnings = advisor.processor.GeneratePSPGrant(cssList, pssList)
return nil
}
func (advisor *Advisor) PrintReport() {
jsonOutput, err := json.Marshal(advisor.report)
if err != nil {
panic(err)
}
fmt.Println(string(jsonOutput))
}
func (advisor *Advisor) PrintPodSecurityPolicy() error {
e := k8sJSON.NewYAMLSerializer(k8sJSON.DefaultMetaFactory, nil, nil)
err := e.Encode(advisor.podSecurityPolicy, os.Stdout)
return err
}
func (advisor *Advisor) PrintOPAPolicy() string {
if advisor.OPAModulePolicy != nil {
err := advisor.OPAModulePolicy.String()
fmt.Printf(err)
return err
} else {
return ""
}
}
func (advisor *Advisor) GetPodSecurityPolicy() *v1beta1.PodSecurityPolicy {
return advisor.podSecurityPolicy
}
func (advisor *Advisor) PrintPodSecurityPolicyWithGrants() error {
var err error
e := k8sJSON.NewYAMLSerializer(k8sJSON.DefaultMetaFactory, nil, nil)
if advisor.grantWarnings != "" {
fmt.Println(advisor.grantWarnings)
printYamlSeparator()
}
for _, pspGrant := range advisor.grants {
fmt.Println(pspGrant.Comment)
if err = e.Encode(pspGrant.PodSecurityPolicy, os.Stdout); err != nil {
return err
}
printYamlSeparator()
if err = e.Encode(pspGrant.Role, os.Stdout); err != nil {
return err
}
printYamlSeparator()
if err = e.Encode(pspGrant.RoleBinding, os.Stdout); err != nil {
return err
}
printYamlSeparator()
}
return nil
}
func printYamlSeparator() {
fmt.Println("---")
}