/
policy.go
733 lines (622 loc) · 24.7 KB
/
policy.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright 2016-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/cilium/cilium/api/v1/models"
. "github.com/cilium/cilium/api/v1/server/restapi/policy"
"github.com/cilium/cilium/pkg/api"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/eventqueue"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/logging/logfields"
bpfIPCache "github.com/cilium/cilium/pkg/maps/ipcache"
"github.com/cilium/cilium/pkg/metrics"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
policyAPI "github.com/cilium/cilium/pkg/policy/api"
"github.com/cilium/cilium/pkg/safetime"
"github.com/cilium/cilium/pkg/uuid"
"github.com/go-openapi/runtime/middleware"
"github.com/op/go-logging"
)
type policyTriggerMetrics struct{}
func (p *policyTriggerMetrics) QueueEvent(reason string) {
if option.Config.MetricsConfig.TriggerPolicyUpdateTotal {
metrics.TriggerPolicyUpdateTotal.WithLabelValues(reason).Inc()
}
}
func (p *policyTriggerMetrics) PostRun(duration, latency time.Duration, folds int) {
if option.Config.MetricsConfig.TriggerPolicyUpdateCallDuration {
metrics.TriggerPolicyUpdateCallDuration.WithLabelValues("duration").Observe(duration.Seconds())
metrics.TriggerPolicyUpdateCallDuration.WithLabelValues("latency").Observe(latency.Seconds())
}
if option.Config.MetricsConfig.TriggerPolicyUpdateFolds {
metrics.TriggerPolicyUpdateFolds.Set(float64(folds))
}
}
func (d *Daemon) policyUpdateTrigger(reasons []string) {
log.Debugf("Regenerating all endpoints")
reason := strings.Join(reasons, ", ")
regenerationMetadata := ®eneration.ExternalRegenerationMetadata{Reason: reason}
endpointmanager.RegenerateAllEndpoints(regenerationMetadata)
}
// TriggerPolicyUpdates triggers policy updates for every daemon's endpoint.
// This may be called in a variety of situations: after policy changes, changes
// in agent configuration, changes in endpoint labels, and change of security
// identities.
func (d *Daemon) TriggerPolicyUpdates(force bool, reason string) {
if force {
log.Debugf("Artificially increasing policy revision to enforce policy recalculation")
d.policy.BumpRevision()
}
d.policyTrigger.TriggerWithReason(reason)
}
// UpdateIdentities informs the policy package of all identity changes
// and also triggers policy updates.
//
// The caller is responsible for making sure the same identity is not
// present in both 'added' and 'deleted'.
func (d *Daemon) UpdateIdentities(added, deleted cache.IdentityCache) {
d.policy.GetSelectorCache().UpdateIdentities(added, deleted)
d.TriggerPolicyUpdates(false, "one or more identities created or deleted")
}
type getPolicyResolve struct {
daemon *Daemon
}
func NewGetPolicyResolveHandler(d *Daemon) GetPolicyResolveHandler {
return &getPolicyResolve{daemon: d}
}
func (h *getPolicyResolve) Handle(params GetPolicyResolveParams) middleware.Responder {
log.WithField(logfields.Params, logfields.Repr(params)).Debug("GET /policy/resolve request")
d := h.daemon
var policyEnforcementMsg string
isPolicyEnforcementEnabled := true
d.policy.Mutex.RLock()
// If policy enforcement isn't enabled, then traffic is allowed.
if policy.GetPolicyEnabled() == option.NeverEnforce {
policyEnforcementMsg = "Policy enforcement is disabled for the daemon."
isPolicyEnforcementEnabled = false
} else if policy.GetPolicyEnabled() == option.DefaultEnforcement {
// If there are no rules matching the set of from / to labels provided in
// the API request, that means that policy enforcement is not enabled
// for the endpoints corresponding to said sets of labels; thus, we allow
// traffic between these sets of labels, and do not enforce policy between them.
fromIngress, fromEgress := d.policy.GetRulesMatching(labels.NewSelectLabelArrayFromModel(params.TraceSelector.From.Labels))
toIngress, toEgress := d.policy.GetRulesMatching(labels.NewSelectLabelArrayFromModel(params.TraceSelector.To.Labels))
if !fromIngress && !fromEgress && !toIngress && !toEgress {
policyEnforcementMsg = "Policy enforcement is disabled because " +
"no rules in the policy repository match any endpoint selector " +
"from the provided destination sets of labels."
isPolicyEnforcementEnabled = false
}
}
d.policy.Mutex.RUnlock()
// Return allowed verdict if policy enforcement isn't enabled between the two sets of labels.
if !isPolicyEnforcementEnabled {
buffer := new(bytes.Buffer)
ctx := params.TraceSelector
searchCtx := policy.SearchContext{
From: labels.NewSelectLabelArrayFromModel(ctx.From.Labels),
Trace: policy.TRACE_ENABLED,
To: labels.NewSelectLabelArrayFromModel(ctx.To.Labels),
DPorts: ctx.To.Dports,
Logging: logging.NewLogBackend(buffer, "", 0),
}
if ctx.Verbose {
searchCtx.Trace = policy.TRACE_VERBOSE
}
verdict := policyAPI.Allowed.String()
searchCtx.PolicyTrace("Label verdict: %s\n", verdict)
msg := fmt.Sprintf("%s\n %s\n%s", searchCtx.String(), policyEnforcementMsg, buffer.String())
return NewGetPolicyResolveOK().WithPayload(&models.PolicyTraceResult{
Log: msg,
Verdict: verdict,
})
}
// If we hit the following code, policy enforcement is enabled for at least
// one of the endpoints corresponding to the provided sets of labels, or for
// the daemon.
ingressBuffer := new(bytes.Buffer)
ctx := params.TraceSelector
ingressSearchCtx := policy.SearchContext{
Trace: policy.TRACE_ENABLED,
Logging: logging.NewLogBackend(ingressBuffer, "", 0),
From: labels.NewSelectLabelArrayFromModel(ctx.From.Labels),
To: labels.NewSelectLabelArrayFromModel(ctx.To.Labels),
DPorts: ctx.To.Dports,
}
if ctx.Verbose {
ingressSearchCtx.Trace = policy.TRACE_VERBOSE
}
// TODO: GH-3394 (add egress trace to API for policy trace).
egressBuffer := new(bytes.Buffer)
egressSearchCtx := ingressSearchCtx
egressSearchCtx.Logging = logging.NewLogBackend(egressBuffer, "", 0)
d.policy.Mutex.RLock()
ingressVerdict := d.policy.AllowsIngressRLocked(&ingressSearchCtx)
d.policy.Mutex.RUnlock()
result := models.PolicyTraceResult{
Verdict: ingressVerdict.String(),
Log: ingressBuffer.String(),
}
return NewGetPolicyResolveOK().WithPayload(&result)
}
// AddOptions are options which can be passed to PolicyAdd
type AddOptions struct {
// Replace if true indicates that existing rules with identical labels should be replaced
Replace bool
// ReplaceWithLabels if present indicates that existing rules with the
// given LabelArray should be deleted.
ReplaceWithLabels labels.LabelArray
// Generated should be set as true to signalize a the policy being inserted
// was generated by cilium-agent, e.g. dns poller.
Generated bool
// The source of this policy, one of api, fqdn or k8s
Source string
}
// PolicyAddEvent is a wrapper around the parameters for policyAdd.
type PolicyAddEvent struct {
rules policyAPI.Rules
opts *AddOptions
d *Daemon
}
// Handle implements pkg/eventqueue/EventHandler interface.
func (p *PolicyAddEvent) Handle(res chan interface{}) {
p.d.policyAdd(p.rules, p.opts, res)
}
// PolicyAddResult is a wrapper around the values returned by policyAdd. It
// contains the new revision of a policy repository after adding a list of rules
// to it, and any error associated with adding rules to said repository.
type PolicyAddResult struct {
newRev uint64
err error
}
// PolicyAdd adds a slice of rules to the policy repository owned by the
// daemon. Eventual changes in policy rules are propagated to all locally
// managed endpoints. Returns the policy revision number of the repository after
// adding the rules into the repository, or an error if the updated policy
// was not able to be imported.
func (d *Daemon) PolicyAdd(rules policyAPI.Rules, opts *AddOptions) (newRev uint64, err error) {
p := &PolicyAddEvent{
rules: rules,
opts: opts,
d: d,
}
polAddEvent := eventqueue.NewEvent(p)
resChan, err := d.policy.RepositoryChangeQueue.Enqueue(polAddEvent)
if err != nil {
return 0, fmt.Errorf("enqueue of PolicyAddEvent failed: %s", err)
}
res, ok := <-resChan
if ok {
pRes := res.(*PolicyAddResult)
return pRes.newRev, pRes.err
}
return 0, fmt.Errorf("policy addition event was cancelled")
}
// policyAdd adds a slice of rules to the policy repository owned by the
// daemon. Eventual changes in policy rules are propagated to all locally
// managed endpoints. Returns the policy revision number of the repository after
// adding the rules into the repository, or an error if the updated policy
// was not able to be imported.
func (d *Daemon) policyAdd(sourceRules policyAPI.Rules, opts *AddOptions, resChan chan interface{}) {
policyAddStartTime := time.Now()
logger := log.WithField("policyAddRequest", uuid.NewUUID().String())
if opts != nil && opts.Generated {
logger.WithField(logfields.CiliumNetworkPolicy, sourceRules.String()).Debug("Policy Add Request")
} else {
logger.WithField(logfields.CiliumNetworkPolicy, sourceRules.String()).Info("Policy Add Request")
}
prefixes := policy.GetCIDRPrefixes(sourceRules)
logger.WithField("prefixes", prefixes).Debug("Policy imported via API, found CIDR prefixes...")
newPrefixLengths, err := d.prefixLengths.Add(prefixes)
if err != nil {
metrics.PolicyImportErrors.Inc()
logger.WithError(err).WithField("prefixes", prefixes).Warn(
"Failed to reference-count prefix lengths in CIDR policy")
resChan <- &PolicyAddResult{
newRev: 0,
err: api.Error(PutPolicyFailureCode, err),
}
return
}
if newPrefixLengths && !bpfIPCache.BackedByLPM() {
// Only recompile if configuration has changed.
logger.Debug("CIDR policy has changed; recompiling base programs")
if err := d.compileBase(); err != nil {
_ = d.prefixLengths.Delete(prefixes)
metrics.PolicyImportErrors.Inc()
err2 := fmt.Errorf("Unable to recompile base programs: %s", err)
logger.WithError(err2).WithField("prefixes", prefixes).Warn(
"Failed to recompile base programs due to prefix length count change")
resChan <- &PolicyAddResult{
newRev: 0,
err: api.Error(PutPolicyFailureCode, err),
}
return
}
}
if _, err := ipcache.AllocateCIDRs(bpfIPCache.IPCache, prefixes); err != nil {
_ = d.prefixLengths.Delete(prefixes)
metrics.PolicyImportErrors.Inc()
logger.WithError(err).WithField("prefixes", prefixes).Warn(
"Failed to allocate identities for CIDRs during policy add")
resChan <- &PolicyAddResult{
newRev: 0,
err: err,
}
return
}
// No errors past this point!
d.policy.Mutex.Lock()
// removedPrefixes tracks prefixes that we replace in the rules. It is used
// after we release the policy repository lock.
var removedPrefixes []*net.IPNet
// policySelectionWG is used to signal when the updating of all of the
// caches of endpoints in the rules which were added / updated have been
// updated.
var policySelectionWG sync.WaitGroup
// Get all endpoints at the time rules were added / updated so we can figure
// out which endpoints to regenerate / bump policy revision.
allEndpoints := endpointmanager.GetPolicyEndpoints()
// Start with all endpoints to be in set for which we need to bump their
// revision.
endpointsToBumpRevision := policy.NewEndpointSet(allEndpoints)
endpointsToRegen := policy.NewEndpointSet(nil)
if opts != nil {
if opts.Replace {
for _, r := range sourceRules {
oldRules := d.policy.SearchRLocked(r.Labels)
removedPrefixes = append(removedPrefixes, policy.GetCIDRPrefixes(oldRules)...)
if len(oldRules) > 0 {
deletedRules, _, _ := d.policy.DeleteByLabelsLocked(r.Labels)
deletedRules.UpdateRulesEndpointsCaches(endpointsToBumpRevision, endpointsToRegen, &policySelectionWG)
}
}
}
if len(opts.ReplaceWithLabels) > 0 {
oldRules := d.policy.SearchRLocked(opts.ReplaceWithLabels)
removedPrefixes = append(removedPrefixes, policy.GetCIDRPrefixes(oldRules)...)
if len(oldRules) > 0 {
deletedRules, _, _ := d.policy.DeleteByLabelsLocked(opts.ReplaceWithLabels)
deletedRules.UpdateRulesEndpointsCaches(endpointsToBumpRevision, endpointsToRegen, &policySelectionWG)
}
}
}
addedRules, newRev := d.policy.AddListLocked(sourceRules)
// The information needed by the caller is available at this point, signal
// accordingly.
resChan <- &PolicyAddResult{
newRev: newRev,
err: nil,
}
addedRules.UpdateRulesEndpointsCaches(endpointsToBumpRevision, endpointsToRegen, &policySelectionWG)
d.policy.Mutex.Unlock()
// Begin tracking the time taken to deploy newRev to the datapath. The start
// time is from before the locking above, and thus includes all waits and
// processing in this function.
source := ""
if opts != nil {
source = opts.Source
}
endpointmanager.CallbackForEndpointsAtPolicyRev(context.Background(), newRev, func(now time.Time) {
duration, _ := safetime.TimeSinceSafe(policyAddStartTime, logger)
metrics.PolicyImplementationDelay.WithLabelValues(source).Observe(duration.Seconds())
})
// remove prefixes of replaced rules above. This potentially blocks on the
// kvstore and should happen without holding the policy lock. Refcounts have
// been incremented above, so any decrements here will be no-ops for CIDRs
// that are re-added, and will trigger deletions for those that are no longer
// used.
if len(removedPrefixes) > 0 {
logger.WithField("prefixes", removedPrefixes).Debug("Decrementing replaced CIDR refcounts when adding rules")
ipcache.ReleaseCIDRs(removedPrefixes)
d.prefixLengths.Delete(removedPrefixes)
}
logger.WithField(logfields.PolicyRevision, newRev).Info("Policy imported via API, recalculating...")
labels := make([]string, 0, len(sourceRules))
for _, r := range sourceRules {
labels = append(labels, r.Labels.GetModel()...)
}
repr, err := monitorAPI.PolicyUpdateRepr(len(sourceRules), labels, newRev)
if err != nil {
logger.WithField(logfields.PolicyRevision, newRev).Warn("Failed to represent policy update as monitor notification")
} else {
d.SendNotification(monitorAPI.AgentNotifyPolicyUpdated, repr)
}
if option.Config.SelectiveRegeneration {
// Only regenerate endpoints which are needed to be regenerated as a
// result of the rule update. The rules which were imported most likely
// do not select all endpoints in the policy repository (and may not
// select any at all). The "reacting" to rule updates enqueues events
// for all endpoints. Once all endpoints have events queued up, this
// function will return.
r := &PolicyReactionEvent{
d: d,
wg: &policySelectionWG,
epsToBumpRevision: endpointsToBumpRevision,
endpointsToRegen: endpointsToRegen,
newRev: newRev,
}
ev := eventqueue.NewEvent(r)
// This event may block if the RuleReactionQueue is full. We don't care
// about when it finishes, just that the work it does is done in a serial
// order.
_, err := d.policy.RuleReactionQueue.Enqueue(ev)
if err != nil {
log.WithField(logfields.PolicyRevision, newRev).Errorf("enqueue of RuleReactionEvent failed: %s", err)
}
} else {
// Regenerate all endpoints unconditionally.
d.TriggerPolicyUpdates(false, "policy rules added")
}
return
}
// PolicyReactionEvent is an event which needs to be serialized after changes
// to a policy repository for a daemon. This currently consists of endpoint
// regenerations / policy revision incrementing for a given endpoint.
type PolicyReactionEvent struct {
d *Daemon
wg *sync.WaitGroup
epsToBumpRevision *policy.EndpointSet
endpointsToRegen *policy.EndpointSet
newRev uint64
}
// Handle implements pkg/eventqueue/EventHandler interface.
func (r *PolicyReactionEvent) Handle(res chan interface{}) {
// Wait until we have calculated which endpoints need to be selected
// across multiple goroutines.
r.wg.Wait()
r.d.ReactToRuleUpdates(r.epsToBumpRevision, r.endpointsToRegen, r.newRev)
}
// ReactToRuleUpdates does the following:
// * regenerate all endpoints in epsToRegen
// * bump the policy revision of all endpoints not in epsToRegen, but which are
// in allEps, to revision rev.
func (d *Daemon) ReactToRuleUpdates(epsToBumpRevision, epsToRegen *policy.EndpointSet, rev uint64) {
var enqueueWaitGroup sync.WaitGroup
// Bump revision of endpoints which don't need to be regenerated.
epsToBumpRevision.ForEachGo(&enqueueWaitGroup, func(epp policy.Endpoint) {
if epp == nil {
return
}
epp.PolicyRevisionBumpEvent(rev)
})
// Regenerate all other endpoints.
regenMetadata := ®eneration.ExternalRegenerationMetadata{Reason: "policy rules added"}
epsToRegen.ForEachGo(&enqueueWaitGroup, func(ep policy.Endpoint) {
if ep != nil {
switch e := ep.(type) {
case *endpoint.Endpoint:
// Do not wait for the returned channel as we want this to be
// ASync
e.RegenerateIfAlive(regenMetadata)
default:
log.Errorf("BUG: endpoint not type of *endpoint.Endpoint, received '%s' instead", e)
}
}
})
enqueueWaitGroup.Wait()
}
// PolicyDeleteEvent is a wrapper around deletion of policy rules with a given
// set of labels from the policy repository in the daemon.
type PolicyDeleteEvent struct {
labels labels.LabelArray
d *Daemon
}
// Handle implements pkg/eventqueue/EventHandler interface.
func (p *PolicyDeleteEvent) Handle(res chan interface{}) {
p.d.policyDelete(p.labels, res)
}
// PolicyDeleteResult is a wrapper around the values returned by policyDelete.
// It contains the new revision of a policy repository after deleting a list of
// rules to it, and any error associated with adding rules to said repository.
type PolicyDeleteResult struct {
newRev uint64
err error
}
// PolicyDelete deletes the policy rules with the provided set of labels from
// the policy repository of the daemon.
// Returns the revision number and an error in case it was not possible to
// delete the policy.
func (d *Daemon) PolicyDelete(labels labels.LabelArray) (newRev uint64, err error) {
p := &PolicyDeleteEvent{
labels: labels,
d: d,
}
policyDeleteEvent := eventqueue.NewEvent(p)
resChan, err := d.policy.RepositoryChangeQueue.Enqueue(policyDeleteEvent)
if err != nil {
return 0, fmt.Errorf("enqueue of PolicyDeleteEvent failed: %s", err)
}
res, ok := <-resChan
if ok {
ress := res.(*PolicyDeleteResult)
return ress.newRev, ress.err
}
return 0, fmt.Errorf("policy deletion event cancelled")
}
func (d *Daemon) policyDelete(labels labels.LabelArray, res chan interface{}) {
log.WithField(logfields.IdentityLabels, logfields.Repr(labels)).Debug("Policy Delete Request")
d.policy.Mutex.Lock()
// First, find rules by the label. We'll use this set of rules to
// determine which CIDR identities that we need to release.
rules := d.policy.SearchRLocked(labels)
// Return an error if a label filter was provided and there are no
// rules matching it. A deletion request for all policy entries should
// not fail if no policies are loaded.
if len(rules) == 0 && len(labels) != 0 {
rev := d.policy.GetRevision()
d.policy.Mutex.Unlock()
err := api.New(DeletePolicyNotFoundCode, "policy not found")
res <- &PolicyDeleteResult{
newRev: rev,
err: err,
}
return
}
// policySelectionWG is used to signal when the updating of all of the
// caches of allEndpoints in the rules which were added / updated have been
// updated.
var policySelectionWG sync.WaitGroup
// Get all endpoints at the time rules were added / updated so we can figure
// out which endpoints to regenerate / bump policy revision.
allEndpoints := endpointmanager.GetPolicyEndpoints()
// Initially keep all endpoints in set of endpoints which need to have
// revision bumped.
epsToBumpRevision := policy.NewEndpointSet(allEndpoints)
endpointsToRegen := policy.NewEndpointSet(nil)
deletedRules, rev, deleted := d.policy.DeleteByLabelsLocked(labels)
deletedRules.UpdateRulesEndpointsCaches(epsToBumpRevision, endpointsToRegen, &policySelectionWG)
res <- &PolicyDeleteResult{
newRev: rev,
err: nil,
}
d.policy.Mutex.Unlock()
// Now that the policies are deleted, we can also attempt to remove
// all CIDR identities referenced by the deleted rules.
//
// We don't treat failures to clean up identities as API failures,
// because the policy can still successfully be updated. We're just
// not appropriately performing garbage collection.
prefixes := policy.GetCIDRPrefixes(rules)
log.WithField("prefixes", prefixes).Debug("Policy deleted via API, found prefixes...")
ipcache.ReleaseCIDRs(prefixes)
prefixesChanged := d.prefixLengths.Delete(prefixes)
if !bpfIPCache.BackedByLPM() && prefixesChanged {
// Only recompile if configuration has changed.
log.Debug("CIDR policy has changed; recompiling base programs")
if err := d.compileBase(); err != nil {
log.WithError(err).Error("Unable to recompile base programs")
}
}
if option.Config.SelectiveRegeneration {
r := &PolicyReactionEvent{
d: d,
wg: &policySelectionWG,
epsToBumpRevision: epsToBumpRevision,
endpointsToRegen: endpointsToRegen,
newRev: rev,
}
ev := eventqueue.NewEvent(r)
// This event may block if the RuleReactionQueue is full. We don't care
// about when it finishes, just that the work it does is done in a serial
// order.
_, err := d.policy.RuleReactionQueue.Enqueue(ev)
if err != nil {
log.WithField(logfields.PolicyRevision, rev).Errorf("enqueue of RuleReactionEvent failed: %s", err)
}
} else {
d.TriggerPolicyUpdates(true, "policy rules deleted")
}
repr, err := monitorAPI.PolicyDeleteRepr(deleted, labels.GetModel(), rev)
if err != nil {
log.WithField(logfields.PolicyRevision, rev).Warn("Failed to represent policy update as monitor notification")
} else {
d.SendNotification(monitorAPI.AgentNotifyPolicyDeleted, repr)
}
return
}
type deletePolicy struct {
daemon *Daemon
}
func newDeletePolicyHandler(d *Daemon) DeletePolicyHandler {
return &deletePolicy{daemon: d}
}
func (h *deletePolicy) Handle(params DeletePolicyParams) middleware.Responder {
d := h.daemon
lbls := labels.ParseSelectLabelArrayFromArray(params.Labels)
rev, err := d.PolicyDelete(lbls)
if err != nil {
return api.Error(DeletePolicyFailureCode, err)
}
ruleList := d.policy.SearchRLocked(labels.LabelArray{})
policy := &models.Policy{
Revision: int64(rev),
Policy: policy.JSONMarshalRules(ruleList),
}
return NewDeletePolicyOK().WithPayload(policy)
}
type putPolicy struct {
daemon *Daemon
}
func newPutPolicyHandler(d *Daemon) PutPolicyHandler {
return &putPolicy{daemon: d}
}
func (h *putPolicy) Handle(params PutPolicyParams) middleware.Responder {
d := h.daemon
var rules policyAPI.Rules
if err := json.Unmarshal([]byte(params.Policy), &rules); err != nil {
return NewPutPolicyInvalidPolicy()
}
for _, r := range rules {
if err := r.Sanitize(); err != nil {
return api.Error(PutPolicyFailureCode, err)
}
}
rev, err := d.PolicyAdd(rules, &AddOptions{Source: metrics.LabelEventSourceAPI})
if err != nil {
return api.Error(PutPolicyFailureCode, err)
}
policy := &models.Policy{
Revision: int64(rev),
Policy: policy.JSONMarshalRules(rules),
}
return NewPutPolicyOK().WithPayload(policy)
}
type getPolicy struct {
daemon *Daemon
}
func newGetPolicyHandler(d *Daemon) GetPolicyHandler {
return &getPolicy{daemon: d}
}
func (h *getPolicy) Handle(params GetPolicyParams) middleware.Responder {
d := h.daemon
d.policy.Mutex.RLock()
defer d.policy.Mutex.RUnlock()
lbls := labels.ParseSelectLabelArrayFromArray(params.Labels)
ruleList := d.policy.SearchRLocked(lbls)
// Error if labels have been specified but no entries found, otherwise,
// return empty list
if len(ruleList) == 0 && len(lbls) != 0 {
return NewGetPolicyNotFound()
}
policy := &models.Policy{
Revision: int64(d.policy.GetRevision()),
Policy: policy.JSONMarshalRules(ruleList),
}
return NewGetPolicyOK().WithPayload(policy)
}
type getPolicySelectors struct {
daemon *Daemon
}
func newGetPolicyCacheHandler(d *Daemon) GetPolicySelectorsHandler {
return &getPolicySelectors{daemon: d}
}
func (h *getPolicySelectors) Handle(params GetPolicySelectorsParams) middleware.Responder {
return NewGetPolicySelectorsOK().WithPayload(h.daemon.policy.GetSelectorCache().GetModel())
}