forked from projectcalico/felix
/
active_rules_calculator.go
416 lines (375 loc) · 14.3 KB
/
active_rules_calculator.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
// Copyright (c) 2016-2018 Tigera, Inc. All rights reserved.
//
// 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 calc
import (
"reflect"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/dispatcher"
"github.com/projectcalico/felix/labelindex"
"github.com/projectcalico/felix/multidict"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/selector"
"github.com/projectcalico/libcalico-go/lib/set"
)
type ruleScanner interface {
OnPolicyActive(model.PolicyKey, *model.Policy)
OnPolicyInactive(model.PolicyKey)
OnProfileActive(model.ProfileRulesKey, *model.ProfileRules)
OnProfileInactive(model.ProfileRulesKey)
}
type FelixSender interface {
SendUpdateToFelix(update model.KVPair)
}
type PolicyMatchListener interface {
OnPolicyMatch(policyKey model.PolicyKey, endpointKey interface{})
OnPolicyMatchStopped(policyKey model.PolicyKey, endpointKey interface{})
}
// ActiveRulesCalculator calculates the set of policies and profiles (i.e. the rules) that
// are active for the particular endpoints that it's been told about. It emits events
// when the set of active rules changes.
//
// For example, if the ActiveRulesCalculator is fed *all* the policies/profiles along with
// the endpoints that are on the local host then its output (via the callback objects) will
// indicate exactly which policies/profiles are active on the local host.
//
// When looking at policies, the ActiveRules calculator is only interested in the selector
// attached to the policy itself (which determines the set of endpoints that it applies to).
// The rules in a policy may also contain selectors; those are are ignored here; they are
// mapped to IP sets by the RuleScanner.
type ActiveRulesCalculator struct {
// Caches of all known policies/profiles.
allPolicies map[model.PolicyKey]*model.Policy
allProfileRules map[string]*model.ProfileRules
// Caches for ALP policies for stat collector.
allALPPolicies set.Set
// Policy/profile ID to matching endpoint sets.
policyIDToEndpointKeys multidict.IfaceToIface
profileIDToEndpointKeys multidict.IfaceToIface
// Label index, matching policy selectors against local endpoints.
labelIndex *labelindex.InheritIndex
// Cache of profile IDs by local endpoint.
endpointKeyToProfileIDs *EndpointKeyToProfileIDMap
// True if we've got the in-sync message from the datastore.
datastoreInSync bool
// Set containing the names of any profiles that were missing during the resync. Used to
// log out those profiles at the end of the resync.
missingProfiles set.Set
// Callback objects.
RuleScanner ruleScanner
PolicyMatchListener PolicyMatchListener
OnPolicyCountsChanged func(numPolicies, numProfiles, numALPPolicies int)
}
func NewActiveRulesCalculator() *ActiveRulesCalculator {
arc := &ActiveRulesCalculator{
// Caches of all known policies/profiles.
allPolicies: make(map[model.PolicyKey]*model.Policy),
allProfileRules: make(map[string]*model.ProfileRules),
allALPPolicies: set.New(),
// Policy/profile ID to matching endpoint sets.
policyIDToEndpointKeys: multidict.NewIfaceToIface(),
profileIDToEndpointKeys: multidict.NewIfaceToIface(),
missingProfiles: set.New(),
// Cache of profile IDs by local endpoint.
endpointKeyToProfileIDs: NewEndpointKeyToProfileIDMap(),
}
arc.labelIndex = labelindex.NewInheritIndex(arc.onMatchStarted, arc.onMatchStopped)
return arc
}
func (arc *ActiveRulesCalculator) RegisterWith(localEndpointDispatcher, allUpdDispatcher *dispatcher.Dispatcher) {
// It needs the filtered endpoints...
localEndpointDispatcher.Register(model.WorkloadEndpointKey{}, arc.OnUpdate)
localEndpointDispatcher.Register(model.HostEndpointKey{}, arc.OnUpdate)
// ...as well as all the policies and profiles.
allUpdDispatcher.Register(model.PolicyKey{}, arc.OnUpdate)
allUpdDispatcher.Register(model.ProfileRulesKey{}, arc.OnUpdate)
allUpdDispatcher.Register(model.ProfileLabelsKey{}, arc.OnUpdate)
allUpdDispatcher.Register(model.ProfileTagsKey{}, arc.OnUpdate)
allUpdDispatcher.RegisterStatusHandler(arc.OnStatusUpdate)
}
func (arc *ActiveRulesCalculator) OnUpdate(update api.Update) (_ bool) {
switch key := update.Key.(type) {
case model.WorkloadEndpointKey:
if update.Value != nil {
log.Debugf("Updating ARC with endpoint %v", key)
endpoint := update.Value.(*model.WorkloadEndpoint)
profileIDs := endpoint.ProfileIDs
arc.updateEndpointProfileIDs(key, profileIDs)
} else {
log.Debugf("Deleting endpoint %v from ARC", key)
arc.updateEndpointProfileIDs(key, []string{})
}
arc.labelIndex.OnUpdate(update)
case model.HostEndpointKey:
if update.Value != nil {
// Figure out what's changed and update the cache.
log.Debugf("Updating ARC for host endpoint %v", key)
endpoint := update.Value.(*model.HostEndpoint)
profileIDs := endpoint.ProfileIDs
arc.updateEndpointProfileIDs(key, profileIDs)
} else {
log.Debugf("Deleting host endpoint %v from ARC", key)
arc.updateEndpointProfileIDs(key, []string{})
}
arc.labelIndex.OnUpdate(update)
case model.ProfileLabelsKey:
arc.labelIndex.OnUpdate(update)
case model.ProfileTagsKey:
arc.labelIndex.OnUpdate(update)
case model.ProfileRulesKey:
if update.Value != nil {
rules := update.Value.(*model.ProfileRules)
if reflect.DeepEqual(arc.allProfileRules[key.Name], rules) {
log.WithField("key", update.Key).Debug("No-op profile change; ignoring.")
return
}
arc.allProfileRules[key.Name] = rules
if arc.profileIDToEndpointKeys.ContainsKey(key.Name) {
log.Debugf("Profile rules updated while active: %v", key.Name)
arc.sendProfileUpdate(key.Name)
} else {
log.Debugf("Profile rules updated while inactive: %v", key.Name)
}
} else {
delete(arc.allProfileRules, key.Name)
if arc.profileIDToEndpointKeys.ContainsKey(key.Name) {
log.Debug("Profile rules deleted while active, telling listener/felix")
arc.sendProfileUpdate(key.Name)
} else {
log.Debugf("Profile rules deleted while inactive: %v", key.Name)
}
}
// Update the policy/profile counts.
arc.updateStats()
case model.PolicyKey:
if update.Value != nil {
log.Debugf("Updating ARC for policy %v", key)
policy := update.Value.(*model.Policy)
if reflect.DeepEqual(arc.allPolicies[key], policy) {
log.WithField("key", update.Key).Debug("No-op policy change; ignoring.")
return
}
arc.allPolicies[key] = policy
// Update the index, which will call us back if the selector no
// longer matches.
sel, err := selector.Parse(policy.Selector)
if err != nil {
log.WithError(err).Panic("Failed to parse selector")
}
arc.labelIndex.UpdateSelector(key, sel)
if arc.policyIDToEndpointKeys.ContainsKey(key) {
// If we get here, the selector still matches something,
// update the rules.
log.Debug("Policy updated while active, telling listener")
arc.sendPolicyUpdate(key)
}
// update ALP policies set.
if arc.isALPPolicy(policy) {
arc.allALPPolicies.Add(key)
} else if arc.allALPPolicies.Contains(key) {
arc.allALPPolicies.Discard(key)
}
} else {
log.Debugf("Removing policy %v from ARC", key)
delete(arc.allPolicies, key)
arc.labelIndex.DeleteSelector(key)
// No need to call updatePolicy() because we'll have got a matchStopped
// callback.
// update ALP policies set.
if arc.allALPPolicies.Contains(key) {
arc.allALPPolicies.Discard(key)
}
}
// Update the policy/profile counts.
arc.updateStats()
default:
log.Infof("Ignoring unexpected update: %v %#v",
reflect.TypeOf(update.Key), update)
}
return
}
func (arc *ActiveRulesCalculator) updateStats() {
if arc.OnPolicyCountsChanged == nil {
return
}
arc.OnPolicyCountsChanged(len(arc.allPolicies), len(arc.allProfileRules), arc.allALPPolicies.Len())
}
func (arc *ActiveRulesCalculator) OnStatusUpdate(status api.SyncStatus) {
if status == api.InSync && !arc.datastoreInSync {
arc.datastoreInSync = true
if arc.missingProfiles.Len() > 0 {
// Log out any profiles that were missing during the resync. We defer
// this until now because we may hear about profiles or endpoints first.
arc.missingProfiles.Iter(func(item interface{}) error {
log.WithField("profileID", item).Warning(
"End of resync: local endpoints refer to missing " +
"or invalid profile, profile's rules replaced " +
"with drop rules.")
return set.RemoveItem
})
}
}
}
func (arc *ActiveRulesCalculator) updateEndpointProfileIDs(key model.Key, profileIDs []string) {
// Figure out which profiles have been added/removed.
log.Debugf("Endpoint %#v now has profile IDs: %v", key, profileIDs)
removedIDs, addedIDs := arc.endpointKeyToProfileIDs.Update(key, profileIDs)
// Update the index of required profile IDs for added profiles,
// triggering events for profiles that just became active.
for id := range addedIDs {
wasActive := arc.profileIDToEndpointKeys.ContainsKey(id)
arc.profileIDToEndpointKeys.Put(id, key)
if !wasActive {
// This profile is now active.
arc.sendProfileUpdate(id)
}
}
// Update the index for no-longer required profile IDs, triggering
// events for profiles that just became inactive.
for id := range removedIDs {
arc.profileIDToEndpointKeys.Discard(id, key)
if !arc.profileIDToEndpointKeys.ContainsKey(id) {
// No endpoint refers to this ID any more. Clean it
// up.
arc.sendProfileUpdate(id)
}
}
}
func (arc *ActiveRulesCalculator) onMatchStarted(selID, labelId interface{}) {
polKey := selID.(model.PolicyKey)
policyWasActive := arc.policyIDToEndpointKeys.ContainsKey(polKey)
arc.policyIDToEndpointKeys.Put(selID, labelId)
if !policyWasActive {
// Policy wasn't active before, tell the listener. The policy
// must be in allPolicies because we can only match on a policy
// that we've seen.
log.Debugf("Policy %v now matches a local endpoint", polKey)
arc.sendPolicyUpdate(polKey)
}
arc.PolicyMatchListener.OnPolicyMatch(polKey, labelId)
}
func (arc *ActiveRulesCalculator) onMatchStopped(selID, labelId interface{}) {
polKey := selID.(model.PolicyKey)
arc.policyIDToEndpointKeys.Discard(selID, labelId)
if !arc.policyIDToEndpointKeys.ContainsKey(selID) {
// Policy no longer active.
polKey := selID.(model.PolicyKey)
log.Debugf("Policy %v no longer matches a local endpoint", polKey)
arc.sendPolicyUpdate(polKey)
}
arc.PolicyMatchListener.OnPolicyMatchStopped(polKey, labelId)
}
var (
DummyDropRules = model.ProfileRules{
InboundRules: []model.Rule{{Action: "deny"}},
OutboundRules: []model.Rule{{Action: "deny"}},
}
)
func (arc *ActiveRulesCalculator) sendProfileUpdate(profileID string) {
active := arc.profileIDToEndpointKeys.ContainsKey(profileID)
rules, known := arc.allProfileRules[profileID]
log.Debugf("Sending profile update for profile %v (known: %v, active: %v)",
profileID, known, active)
key := model.ProfileRulesKey{ProfileKey: model.ProfileKey{Name: profileID}}
// We'll re-add the profile to the set if it's still missing below.
arc.missingProfiles.Discard(key.Name)
if active {
if !known {
if arc.datastoreInSync {
// We're in sync so we know the profile is missing from the
// datastore or it failed validation
log.WithField("profileID", profileID).Warn(
"Profile not known or invalid, generating dummy profile " +
"that drops all traffic.")
} else {
// Not in sync, the profile may still show up. Keep a record of its
// name so we can log it out at the end of the resync.
arc.missingProfiles.Add(profileID)
log.WithField("profileID", profileID).Debug(
"Profile unknown during resync.")
}
rules = &DummyDropRules
}
arc.RuleScanner.OnProfileActive(key, rules)
} else {
arc.RuleScanner.OnProfileInactive(key)
}
}
func (arc *ActiveRulesCalculator) sendPolicyUpdate(policyKey model.PolicyKey) {
policy, known := arc.allPolicies[policyKey]
active := arc.policyIDToEndpointKeys.ContainsKey(policyKey)
log.Debugf("Sending policy update for policy %v (known: %v, active: %v)",
policyKey, known, active)
if active {
if !known {
// This shouldn't happen because a policy can only become active if
// we know its selector, which is inside the policy struct.
log.WithField("policyKey", policyKey).Panic("Unknown policy became active!")
}
arc.RuleScanner.OnPolicyActive(policyKey, policy)
} else {
arc.RuleScanner.OnPolicyInactive(policyKey)
}
}
func (arc *ActiveRulesCalculator) isALPPolicy(policy *model.Policy) bool {
// Policy is a ALP policy if HTTPMatch rule or service account selector exists.
checkRules := func(rules []model.Rule) bool {
for _, rule := range rules {
if rule.HTTPMatch != nil || rule.OriginalSrcServiceAccountSelector != "" || rule.OriginalDstServiceAccountSelector != "" {
return true
}
}
return false
}
return checkRules(policy.InboundRules) || checkRules(policy.OutboundRules)
}
// EndpointKeyToProfileIDMap is a specialised map that calculates the deltas to the profile IDs
// when making an update.
type EndpointKeyToProfileIDMap struct {
endpointKeyToProfileIDs map[model.Key][]string
}
func NewEndpointKeyToProfileIDMap() *EndpointKeyToProfileIDMap {
return &EndpointKeyToProfileIDMap{
endpointKeyToProfileIDs: make(map[model.Key][]string),
}
}
func (idx EndpointKeyToProfileIDMap) Update(
key model.Key,
profileIDs []string,
) (
removedIDs, addedIDs map[string]bool,
) {
oldIDs := idx.endpointKeyToProfileIDs[key]
removedIDs = make(map[string]bool)
for _, id := range oldIDs {
removedIDs[id] = true
}
addedIDs = make(map[string]bool)
for _, id := range profileIDs {
if removedIDs[id] {
delete(removedIDs, id)
} else {
addedIDs[id] = true
}
}
// Store off the update in our cache.
if len(profileIDs) > 0 {
idx.endpointKeyToProfileIDs[key] = profileIDs
} else {
// No profiles is equivalent to deletion so we may as well
// clean up completely.
delete(idx.endpointKeyToProfileIDs, key)
}
return removedIDs, addedIDs
}