-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
375 lines (350 loc) · 11.1 KB
/
manager.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package audit
import (
"context"
"encoding/json"
"flag"
"strings"
"time"
opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
constraintTypes "github.com/open-policy-agent/frameworks/constraint/pkg/types"
"github.com/pkg/errors"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var log = logf.Log.WithName("controller").WithValues("metaKind", "audit")
const (
crdName = "constrainttemplates.templates.gatekeeper.sh"
constraintsGV = "constraints.gatekeeper.sh/v1alpha1"
msgSize = 256
)
var (
auditInterval = flag.Int("auditInterval", 60, "interval to run audit in seconds. defaulted to 60 secs if unspecified ")
constraintViolationsLimit = flag.Int("constraintViolationsLimit", 20, "limit of number of violations per constraint. defaulted to 20 violations if unspecified ")
emptyAuditResults []auditResult
)
// AuditManager allows us to audit resources periodically
type AuditManager struct {
client client.Client
opa opa.Client
stopper chan struct{}
stopped chan struct{}
cfg *rest.Config
ctx context.Context
ucloop *updateConstraintLoop
}
type auditResult struct {
cname string
cnamespace string
cgvk schema.GroupVersionKind
capiversion string
rkind string
rname string
rnamespace string
message string
}
// StatusViolation represents each violation under status
type StatusViolation struct {
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Message string `json:"message"`
}
// New creates a new manager for audit
func New(ctx context.Context, cfg *rest.Config, opa opa.Client) (*AuditManager, error) {
am := &AuditManager{
opa: opa,
stopper: make(chan struct{}),
stopped: make(chan struct{}),
cfg: cfg,
ctx: ctx,
}
return am, nil
}
// audit performs an audit then updates the status of all constraint resources with the results
func (am *AuditManager) audit(ctx context.Context) error {
timestamp := time.Now().UTC().Format(time.RFC3339)
// new client to get updated restmapper
c, err := client.New(am.cfg, client.Options{Scheme: nil, Mapper: nil})
if err != nil {
return err
}
am.client = c
// don't audit anything until the constraintTemplate crd is in the cluster
if err := am.ensureCRDExists(ctx); err != nil {
log.Info("Audit exits, required crd has not been deployed ", "CRD", crdName)
return nil
}
resp, err := am.opa.Audit(ctx)
if err != nil {
return err
}
log.Info("Audit opa.Audit() audit results", "violations", len(resp.Results()))
// get updatedLists
updateLists := make(map[string][]auditResult)
if len(resp.Results()) > 0 {
updateLists, err = getUpdateListsFromAuditResponses(resp)
if err != nil {
return err
}
}
// get all constraint kinds
rs, err := am.getAllConstraintKinds()
if err != nil {
// if no constraint is found with the constraint apiversion, then return
log.Info("no constraint is found with apiversion", "constraint apiversion", constraintsGV)
return nil
}
// update constraints for each kind
return am.writeAuditResults(ctx, rs, updateLists, timestamp)
}
func (am *AuditManager) auditManagerLoop(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Info("Audit Manager close")
close(am.stopper)
return
default:
time.Sleep(time.Duration(*auditInterval) * time.Second)
if err := am.audit(ctx); err != nil {
log.Error(err, "audit manager audit() failed")
}
}
}
}
// Start implements controller.Controller
func (am *AuditManager) Start(stop <-chan struct{}) error {
log.Info("Starting Audit Manager")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go am.auditManagerLoop(ctx)
<-stop
log.Info("Stopping audit manager workers")
return nil
}
func (am *AuditManager) ensureCRDExists(ctx context.Context) error {
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
return am.client.Get(ctx, types.NamespacedName{Name: crdName}, crd)
}
func (am *AuditManager) getAllConstraintKinds() (*metav1.APIResourceList, error) {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(am.cfg)
if err != nil {
return nil, err
}
return discoveryClient.ServerResourcesForGroupVersion(constraintsGV)
}
func getUpdateListsFromAuditResponses(resp *constraintTypes.Responses) (map[string][]auditResult, error) {
updateLists := make(map[string][]auditResult)
for _, r := range resp.Results() {
selfLink := r.Constraint.GetSelfLink()
// skip if this constraint has reached the constraintViolationsLimit
if len(updateLists[selfLink]) == *constraintViolationsLimit {
continue
}
name := r.Constraint.GetName()
namespace := r.Constraint.GetNamespace()
apiVersion := r.Constraint.GetAPIVersion()
gvk := r.Constraint.GroupVersionKind()
message := r.Msg
if len(message) > msgSize {
message = truncateString(message, msgSize)
}
resource, ok := r.Resource.(*unstructured.Unstructured)
if !ok {
return nil, errors.Errorf("could not cast resource as reviewResource: %v", r.Resource)
}
rname := resource.GetName()
rkind := resource.GetKind()
rnamespace := resource.GetNamespace()
updateLists[selfLink] = append(updateLists[selfLink], auditResult{
cgvk: gvk,
capiversion: apiVersion,
cname: name,
cnamespace: namespace,
rkind: rkind,
rname: rname,
rnamespace: rnamespace,
message: message,
})
}
return updateLists, nil
}
func (am *AuditManager) writeAuditResults(ctx context.Context, resourceList *metav1.APIResourceList, updateLists map[string][]auditResult, timestamp string) error {
resourceGV := strings.Split(resourceList.GroupVersion, "/")
group := resourceGV[0]
version := resourceGV[1]
// get constraints for each Kind
for _, r := range resourceList.APIResources {
log.Info("constraint", "resource kind", r.Kind)
constraintGvk := schema.GroupVersionKind{
Group: group,
Version: version,
Kind: r.Kind + "List",
}
instanceList := &unstructured.UnstructuredList{}
instanceList.SetGroupVersionKind(constraintGvk)
err := am.client.List(ctx, &client.ListOptions{}, instanceList)
if err != nil {
return err
}
log.Info("constraint", "count of constraints", len(instanceList.Items))
updateConstraints := make(map[string]unstructured.Unstructured, len(instanceList.Items))
// get each constraint
for _, item := range instanceList.Items {
updateConstraints[item.GetSelfLink()] = item
}
if len(updateConstraints) > 0 {
if am.ucloop != nil {
close(am.ucloop.stop)
select {
case <-am.ucloop.stopped:
case <-time.After(time.Duration(*auditInterval) * time.Second):
}
}
am.ucloop = &updateConstraintLoop{
uc: updateConstraints,
client: am.client,
stop: make(chan struct{}),
stopped: make(chan struct{}),
ul: updateLists,
ts: timestamp,
}
log.Info("starting update constraints loop", "updateConstraints", updateConstraints)
go am.ucloop.update()
}
}
return nil
}
func (ucloop *updateConstraintLoop) updateConstraintStatus(ctx context.Context, instance *unstructured.Unstructured, auditResults []auditResult, timestamp string) error {
constraintName := instance.GetName()
log.Info("updating constraint", "constraintName", constraintName)
// create constraint status violations
var statusViolations []interface{}
for _, ar := range auditResults {
statusViolations = append(statusViolations, StatusViolation{
Kind: ar.rkind,
Name: ar.rname,
Namespace: ar.rnamespace,
Message: ar.message,
})
}
raw, err := json.Marshal(statusViolations)
if err != nil {
return err
}
// need to convert to []interface{}
violations := make([]interface{}, 0)
err = json.Unmarshal(raw, &violations)
if err != nil {
return err
}
// update constraint status auditTimestamp
unstructured.SetNestedField(instance.Object, timestamp, "status", "auditTimestamp")
// update constraint status violations
if len(violations) == 0 {
_, found, err := unstructured.NestedSlice(instance.Object, "status", "violations")
if err != nil {
return err
}
if found {
unstructured.RemoveNestedField(instance.Object, "status", "violations")
log.Info("removed status violations", "constraintName", constraintName)
}
err = ucloop.client.Update(ctx, instance)
if err != nil {
return err
}
} else {
unstructured.SetNestedSlice(instance.Object, violations, "status", "violations")
log.Info("update constraint", "object", instance)
err = ucloop.client.Update(ctx, instance)
if err != nil {
return err
}
log.Info("updated constraint status violations", "constraintName", constraintName, "count", len(violations))
}
return nil
}
func truncateString(str string, size int) string {
shortenStr := str
if len(str) > size {
if size > 3 {
size -= 3
}
shortenStr = str[0:size] + "..."
}
return shortenStr
}
type updateConstraintLoop struct {
uc map[string]unstructured.Unstructured
client client.Client
stop chan struct{}
stopped chan struct{}
ul map[string][]auditResult
ts string
}
func (ucloop *updateConstraintLoop) update() {
defer close(ucloop.stopped)
updateLoop := func() (bool, error) {
for _, item := range ucloop.uc {
select {
case <-ucloop.stop:
return true, nil
default:
failure := false
ctx := context.Background()
var latestItem unstructured.Unstructured
item.DeepCopyInto(&latestItem)
name := latestItem.GetName()
namespace := latestItem.GetNamespace()
namespacedName := types.NamespacedName{
Name: name,
Namespace: namespace,
}
// get the latest constraint
err := ucloop.client.Get(ctx, namespacedName, &latestItem)
if err != nil {
failure = true
log.Error(err, "could not get latest constraint during update", "name", name, "namespace", namespace)
}
if constraintAuditResults, ok := ucloop.ul[latestItem.GetSelfLink()]; !ok {
err := ucloop.updateConstraintStatus(ctx, &latestItem, emptyAuditResults, ucloop.ts)
if err != nil {
failure = true
log.Error(err, "could not update constraint status", "name", name, "namespace", namespace)
}
} else {
// update the constraint
err := ucloop.updateConstraintStatus(ctx, &latestItem, constraintAuditResults, ucloop.ts)
if err != nil {
failure = true
log.Error(err, "could not update constraint status", "name", name, "namespace", namespace)
}
}
if !failure {
delete(ucloop.uc, latestItem.GetSelfLink())
}
}
}
if len(ucloop.uc) == 0 {
return true, nil
}
return false, nil
}
if err := wait.ExponentialBackoff(wait.Backoff{
Duration: 1 * time.Second,
Factor: 2,
Jitter: 1,
Steps: 5,
}, updateLoop); err != nil {
log.Error(err, "could not update constraint reached max retries", "remaining update constraints", ucloop.uc)
}
}