/
handler.go
167 lines (138 loc) · 4.68 KB
/
handler.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package api
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"path"
"github.com/kyverno/policy-reporter-kyverno-plugin/pkg/kyverno"
"github.com/kyverno/policy-reporter-kyverno-plugin/pkg/reporting"
"go.uber.org/zap"
)
var funcMap = template.FuncMap{
"add": func(i, j int) int {
return i + j
},
}
// PolicyHandler for the PolicyReport REST API
func PolicyReportingHandler(s reporting.PolicyReportGenerator, basePath string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := s.PerPolicyData(req.Context(), reporting.Filter{
Namespaces: req.URL.Query()["namespaces"],
Policies: req.URL.Query()["policies"],
ClusterScope: req.URL.Query().Get("clusterScope") != "0",
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.New("policy-report-details.html").Funcs(funcMap).ParseFiles(path.Join(basePath, "policy-report-details.html"), path.Join(basePath, "mui.css"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// NamespaceReportingHandler for the NamespaceReport REST API
func NamespaceReportingHandler(s reporting.PolicyReportGenerator, basePath string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := s.PerNamespaceData(req.Context(), reporting.Filter{
Namespaces: req.URL.Query()["namespaces"],
Policies: req.URL.Query()["policies"],
ClusterScope: req.URL.Query().Get("clusterScope") != "0",
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.New("namespace-report-details.html").Funcs(funcMap).ParseFiles(path.Join(basePath, "namespace-report-details.html"), path.Join(basePath, "mui.css"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// PolicyHandler for the Policy REST API
func PolicyHandler(s *kyverno.PolicyStore) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
policies := s.List()
if len(policies) == 0 {
fmt.Fprint(w, "[]")
return
}
if err := json.NewEncoder(w).Encode(policies); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{ "message": "%s" }`, err.Error())
}
}
}
// VerifyImageRulesHandler for the ImageVerify Policy REST API
func VerifyImageRulesHandler(s *kyverno.PolicyStore) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
policies := s.List()
if len(policies) == 0 {
fmt.Fprint(w, "[]")
return
}
verifyRules := make([]*VerifyImage, 0)
images := map[string]bool{}
for _, policy := range policies {
for _, rule := range policy.Rules {
if rule.VerifyImages == nil || len(rule.VerifyImages) == 0 {
continue
}
for _, verify := range rule.VerifyImages {
if _, ok := images[verify.Image]; ok {
continue
}
verifyRules = append(verifyRules, &VerifyImage{
Policy: &Policy{Name: policy.Name, Namespace: policy.Namespace, UID: policy.UID},
Rule: rule.Name,
Repository: verify.Repository,
Image: verify.Image,
Key: verify.Key,
Attestations: verify.Attestations,
})
images[verify.Image] = true
}
}
}
if err := json.NewEncoder(w).Encode(verifyRules); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{ "message": "%s" }`, err.Error())
}
}
}
// HealthzHandler for the Liveness REST API
func HealthzHandler(synced func() bool) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if !synced() {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusServiceUnavailable)
zap.L().Warn("Healthz Check: No kyverno policy crds are found")
fmt.Fprint(w, `{ "error": "No policy CRDs found" }`)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
}
}
// ReadyHandler for the Readiness REST API
func ReadyHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
}
}