forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policysets.go
585 lines (500 loc) · 18.2 KB
/
policysets.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
//+build windows
// Copyright (c) 2017 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 policysets
import (
"strings"
hns "github.com/Microsoft/hcsshim"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/dataplane/windows/ipsets"
"github.com/projectcalico/felix/proto"
"github.com/projectcalico/libcalico-go/lib/set"
)
// PolicySets manages a whole plane of policies/profiles
type PolicySets struct {
policySetIdToPolicySet map[string]*policySet
IpSets []*ipsets.IPSets
supportedFeatures hns.HNSSupportedFeatures
resyncRequired bool
}
func NewPolicySets(ipsets []*ipsets.IPSets) *PolicySets {
supportedFeatures := hns.GetHNSSupportedFeatures()
return &PolicySets{
policySetIdToPolicySet: map[string]*policySet{},
IpSets: ipsets,
supportedFeatures: supportedFeatures,
}
}
// AddOrReplacePolicySet is responsible for the creation (or replacement) of a Policy set
// and it is capable of processing either Profiles or Policies from the datastore.
func (s *PolicySets) AddOrReplacePolicySet(setId string, policy interface{}) {
log.WithField("setID", setId).Info("Processing add/replace of Policy set")
// Process the policy/profile from the datastore and convert it into
// equivalent rules which can be communicated to hns for enforcement in the
// dataplane. We compute these rules up front and cache them to avoid needing
// to recompute them each time the policy is applied to an endpoint. We also
// keep track of any IP sets which were referenced by the policy/profile so that
// we can easily tell which Policy sets are impacted when a IP set is modified.
var rules []*hns.ACLPolicy
var policyIpSetIds set.Set
setMetadata := PolicySetMetadata{
SetId: setId,
}
switch p := policy.(type) {
case *proto.Policy:
// Incoming datastore object is a Policy
rules = s.convertPolicyToRules(setId, p.InboundRules, p.OutboundRules)
policyIpSetIds = getReferencedIpSetIds(p.InboundRules, p.OutboundRules)
setMetadata.Type = PolicySetTypePolicy
case *proto.Profile:
// Incoming datastore object is a Profile
rules = s.convertPolicyToRules(setId, p.InboundRules, p.OutboundRules)
policyIpSetIds = getReferencedIpSetIds(p.InboundRules, p.OutboundRules)
setMetadata.Type = PolicySetTypeProfile
}
// Create the struct and store it off
policySet := &policySet{
PolicySetMetadata: setMetadata,
Policy: policy,
Members: set.FromArray(rules),
IpSetIds: policyIpSetIds,
}
s.policySetIdToPolicySet[setMetadata.SetId] = policySet
}
// RemovePolicySet is responsible for the removal of a Policy set
func (s *PolicySets) RemovePolicySet(setId string) {
log.WithField("setId", setId).Info("Processing removal of Policy set")
delete(s.policySetIdToPolicySet, setId)
}
// GetPolicySetRules receives a list of Policy set ids and it computes the complete
// set of resultant hns rules which are needed to enforce all of the Policy sets for the
// specified direction. As the Policy sets are processed, we increment a priority number
// and assign it to each rule from the current set. By incremening the rule priority for
// each set, we ensure that all of the sets will be enforced and considered by the dataplane
// in the order intended by felix. Once all rules are gathered, we add a final pair of rules
// to default deny any traffic which has not matched any rules from any Policy sets.
func (s *PolicySets) GetPolicySetRules(setIds []string, isInbound bool) (rules []*hns.ACLPolicy) {
// Rules from the first set will receive the default rule priority
currentPriority := rulePriority
direction := hns.Out
if isInbound {
direction = hns.In
}
for _, setId := range setIds {
log.WithFields(log.Fields{"setId": setId, "isInbound": isInbound}).Debug("Gathering per-direction rules for policy set")
policySet := s.policySetIdToPolicySet[setId]
if policySet == nil {
log.WithField("setId", setId).Error("Unable to find Policy set, this set will be skipped")
continue
}
policySet.Members.Iter(func(item interface{}) error {
member := item.(*hns.ACLPolicy)
if member.Direction == direction {
member.Priority = currentPriority
rules = append(rules, member)
}
return nil
})
// Increment the priority so that rules from the next set will be 'weaker' priority
// and therefore considered only after this current set of rules has failed to match.
currentPriority += 1
}
// Apply a default block rule for this direction at the end of the policy
rules = append(rules, s.NewRule(isInbound, currentPriority))
// Finally, for RS3 only, add default allow rule with a host-scope to allow traffic through
// the host windows firewall
rules = append(rules, s.NewHostRule(isInbound))
return
}
// ProcessIpSetUpdate locates any Policy set(s) which reference the provided IP set, and causes
// those Policy sets to be recomputed (to ensure any rule address conditions are using the latest
// addres values from the IP set). A list of the Policy sets which were found and recomputed are
// is returned to the caller.
func (s *PolicySets) ProcessIpSetUpdate(ipSetId string) []string {
log.WithField("IPSetId", ipSetId).Info("IP set has changed, looking for associated policies")
stalePolicies := s.getPoliciesByIpSetId(ipSetId)
if len(stalePolicies) == 0 {
return nil
}
log.WithFields(log.Fields{"IPSetId": ipSetId, "Policies": stalePolicies}).Info("Associated policies need to be refreshed")
for _, policyId := range stalePolicies {
policySet := s.policySetIdToPolicySet[policyId]
if policySet == nil {
log.WithFields(log.Fields{"IPSetId": ipSetId, "Policy": policyId}).Error("Unable to find Policy set, this set will be skipped")
continue
}
s.AddOrReplacePolicySet(policySet.PolicySetMetadata.SetId, policySet.Policy)
}
// Return the policies which were recalculated as a result of this update
return stalePolicies
}
// getPoliciesByIpSetId locates any Policy set(s) which reference the provided IP set
func (s *PolicySets) getPoliciesByIpSetId(ipSetId string) (policies []string) {
for policySetId, policySet := range s.policySetIdToPolicySet {
policySet.IpSetIds.Iter(func(item interface{}) error {
id := item.(string)
if id == ipSetId {
policies = append(policies, policySetId)
}
return nil
})
}
return
}
// getReferencedIpSetIds returns all of the IP sets which are referenced by the provided
// inbound and outbound proto Rules.
func getReferencedIpSetIds(inboundRules []*proto.Rule, outboundRules []*proto.Rule) set.Set {
var rules []*proto.Rule
rules = append(rules, inboundRules...)
rules = append(rules, outboundRules...)
ipSetIds := set.New()
for _, rule := range rules {
ipSetIds.AddAll(rule.SrcIpSetIds)
ipSetIds.AddAll(rule.DstIpSetIds)
}
return ipSetIds
}
// convertPolicyToRules converts the provided inbound and outbound proto rules into hns rules.
func (s *PolicySets) convertPolicyToRules(policyId string, inboundRules []*proto.Rule, outboundRules []*proto.Rule) (hnsRules []*hns.ACLPolicy) {
log.WithField("policyId", policyId).Debug("ConvertPolicyToRules")
inbound := s.protoRulesToHnsRules(policyId, inboundRules, true)
hnsRules = append(hnsRules, inbound...)
outbound := s.protoRulesToHnsRules(policyId, outboundRules, false)
hnsRules = append(hnsRules, outbound...)
for _, rule := range hnsRules {
log.WithFields(log.Fields{"policyId": policyId, "rule": rule}).Debug("ConvertPolicyToRules final rule output")
}
return
}
// protoRulesToHnsRules converts a set of proto rules into hns rules.
func (s *PolicySets) protoRulesToHnsRules(policyId string, protoRules []*proto.Rule, isInbound bool) (rules []*hns.ACLPolicy) {
log.WithField("policyId", policyId).Debug("protoRulesToHnsRules")
for _, protoRule := range protoRules {
hnsRules, err := s.protoRuleToHnsRules(policyId, protoRule, isInbound)
if err != nil {
switch err {
case SkipRule:
log.WithField("rule", protoRule).Info("Rule was skipped")
default:
log.WithField("rule", protoRule).Infof("Rule could not be converted, error: %v", err)
}
continue
}
rules = append(rules, hnsRules...)
}
return
}
// protoRuleToHnsRules converts a proto rule into equivalent hns rules (one or more resultant rules). For Windows RS3,
// there are a few limitations to be aware of:
//
// The following types of rules are not supported in this release and will be logged+skipped:
// Rules with: Negative match criteria, Actions other than 'allow' or 'deny', Port ranges, and ICMP type/codes.
//
func (s *PolicySets) protoRuleToHnsRules(policyId string, pRule *proto.Rule, isInbound bool) ([]*hns.ACLPolicy, error) {
log.WithField("policyId", policyId).Debug("protoRuleToHnsRules")
// Check IpVersion
if pRule.IpVersion != 0 && pRule.IpVersion != proto.IPVersion(ipVersion) {
log.WithField("rule", pRule).Info("Skipping rule because it is for an unsupported IP version.")
return nil, SkipRule
}
// Skip rules with negative match criteria, these are not supported in this version
if ruleHasNegativeMatches(pRule) {
log.WithField("rule", pRule).Info("Skipping rule because it contains negative matches (currently unsupported).")
return nil, SkipRule
}
// Skip rules with port ranges, only a single port is supported in this version
if portsContainRanges(pRule.SrcPorts) || portsContainRanges(pRule.DstPorts) {
log.WithField("rule", pRule).Info("Skipping rule because it contains port ranges (currently unsupported).")
return nil, SkipRule
}
// Skip rules with ICMP type/codes, these are not supported
if pRule.Icmp != nil {
log.WithField("rule", pRule).Info("Skipping rule because it contains ICMP type or code (currently unsupported).")
return nil, SkipRule
}
// Skip rules with name port ipsets
if len(pRule.SrcNamedPortIpSetIds) > 0 || len(pRule.DstNamedPortIpSetIds) > 0 {
log.WithField("rule", pRule).Info("Skipping rule because it contains named port ipsets (currently unsupported).")
return nil, SkipRule
}
// Filter the Src and Dst CIDRs to only the IP version that we're rendering
var filteredAll bool
ruleCopy := *pRule
ruleCopy.SrcNet, filteredAll = filterNets(pRule.SrcNet, ipVersion)
if filteredAll {
return nil, SkipRule
}
ruleCopy.NotSrcNet, filteredAll = filterNets(pRule.NotSrcNet, ipVersion)
if filteredAll {
return nil, SkipRule
}
ruleCopy.DstNet, filteredAll = filterNets(pRule.DstNet, ipVersion)
if filteredAll {
return nil, SkipRule
}
ruleCopy.NotDstNet, filteredAll = filterNets(pRule.NotDstNet, ipVersion)
if filteredAll {
return nil, SkipRule
}
// Log with the rule details for context
logCxt := log.WithField("rule", ruleCopy)
// Start with a new empty hns aclPolicy (rule)
var aclPolicies []*hns.ACLPolicy
aclPolicy := s.NewRule(isInbound, rulePriority)
//
// Id
//
if s.supportedFeatures.Acl.AclRuleId {
aclPolicy.Id = (policyId + "-" + ruleCopy.RuleId)
}
//
// Action
//
switch strings.ToLower(ruleCopy.Action) {
case "", "allow":
aclPolicy.Action = hns.Allow
case "deny":
aclPolicy.Action = hns.Block
case "next-tier", "pass", "log":
logCxt.WithField("action", ruleCopy.Action).Info("This rule action is not supported, rule will be skipped")
return nil, SkipRule
default:
logCxt.WithField("action", ruleCopy.Action).Panic("Unknown rule action")
}
//
// Source ports
//
if len(ruleCopy.SrcPorts) > 0 {
// Windows RS3 limitation, single port
ports := uint16(ruleCopy.SrcPorts[0].First)
if isInbound {
aclPolicy.RemotePort = ports
logCxt.WithField("RemotePort", aclPolicy.RemotePort).Debug("Adding Source Ports as RemotePort condition")
} else {
aclPolicy.LocalPort = ports
logCxt.WithField("LocalPort", aclPolicy.LocalPort).Debug("Adding Source Ports as LocalPort condition")
}
}
//
// Destination Ports
//
if len(ruleCopy.DstPorts) > 0 {
// Windows RS3 limitation, single port (start port)
ports := uint16(ruleCopy.DstPorts[0].First)
if isInbound {
aclPolicy.LocalPort = ports
logCxt.WithField("LocalPort", aclPolicy.LocalPort).Debug("Adding Destination Ports as LocalPort condition")
} else {
aclPolicy.RemotePort = ports
logCxt.WithField("RemotePort", aclPolicy.RemotePort).Debug("Adding Destination Ports as RemotePort condition")
}
}
//
// Protocol
//
if ruleCopy.Protocol != nil {
switch p := ruleCopy.Protocol.NumberOrName.(type) {
case *proto.Protocol_Name:
logCxt.WithField("protoName", p.Name).Debug("Adding Protocol Name condition")
aclPolicy.Protocol = protocolNameToNumber(p.Name)
case *proto.Protocol_Number:
logCxt.WithField("protoNum", p.Number).Debug("Adding Protocol number condition")
aclPolicy.Protocol = uint16(p.Number)
}
}
//
// Source Neworks and IPSets
//
localAddresses := []string{""} // ensures slice always has at least one value
remoteAddresses := []string{""}
srcAddresses := ruleCopy.SrcNet
if len(ruleCopy.SrcIpSetIds) > 0 {
ipsetAddresses, err := s.getIPSetAddresses(ruleCopy.SrcIpSetIds)
if err != nil {
logCxt.Info("SrcIpSetIds could not be resolved, rule will be skipped")
return nil, SkipRule
}
srcAddresses = append(srcAddresses, ipsetAddresses...)
}
if len(srcAddresses) > 0 {
if isInbound {
remoteAddresses = srcAddresses
logCxt.WithField("RemoteAddress", remoteAddresses).Debug("Adding Source Networks/IPsets as RemoteAddress conditions")
} else {
localAddresses = srcAddresses
logCxt.WithField("LocalAddress", localAddresses).Debug("Adding Source Networks/IPsets as LocalAddress conditions")
}
}
//
// Destination Networks and IPSets
//
dstAddresses := ruleCopy.DstNet
if len(ruleCopy.DstIpSetIds) > 0 {
ipsetAddresses, err := s.getIPSetAddresses(ruleCopy.DstIpSetIds)
if err != nil {
logCxt.Info("DstIpSetIds could not be resolved, rule will be skipped")
return nil, SkipRule
}
dstAddresses = append(dstAddresses, ipsetAddresses...)
}
if len(dstAddresses) > 0 {
if isInbound {
localAddresses = dstAddresses
logCxt.WithField("LocalAddress", localAddresses).Debug("Adding Destination Networks/IPsets as LocalAddress condition")
} else {
remoteAddresses = dstAddresses
logCxt.WithField("RemoteAddress", remoteAddresses).Debug("Adding Destination Networks/IPsets as RemoteAddress condition")
}
}
// For Windows RS3 only, there is a dataplane restriction of a single address/cidr per
// source or destination condition. The behavior below will be removed in
// the next iteration, but for now we have to break up the source and destination
// ip address combinations and represent them using multiple rules
for _, localAddr := range localAddresses {
for _, remoteAddr := range remoteAddresses {
newPolicy := *aclPolicy
newPolicy.LocalAddresses = localAddr
newPolicy.RemoteAddresses = remoteAddr
// Add this rule to the rules being returned
aclPolicies = append(aclPolicies, &newPolicy)
}
}
return aclPolicies, nil
}
func ruleHasNegativeMatches(pRule *proto.Rule) bool {
if len(pRule.NotSrcNet) > 0 || len(pRule.NotDstNet) > 0 {
return true
}
if len(pRule.NotSrcPorts) > 0 || len(pRule.NotDstPorts) > 0 {
return true
}
if len(pRule.NotSrcIpSetIds) > 0 || len(pRule.NotDstIpSetIds) > 0 {
return true
}
if len(pRule.NotSrcNamedPortIpSetIds) > 0 || len(pRule.NotDstNamedPortIpSetIds) > 0 {
return true
}
if pRule.NotProtocol != nil {
return true
}
if pRule.NotIcmp != nil {
return true
}
return false
}
func portsContainRanges(ports []*proto.PortRange) bool {
if len(ports) > 1 {
return true
}
for _, portRange := range ports {
if portRange.First != portRange.Last {
return true
}
}
return false
}
// getIPSetAddresses retrieves all of the ip addresses (members) referenced by the provided
// IP sets.
func (s *PolicySets) getIPSetAddresses(setIds []string) ([]string, error) {
var addresses []string
var found bool
for _, ipsetId := range setIds {
found = false
for _, ipSets := range s.IpSets {
ipSet := ipSets.GetIPSetMembers(ipsetId)
if ipSet == nil {
continue
}
addresses = append(addresses, ipSet...)
found = true
break
}
if !found {
log.WithField("ipsetId", ipsetId).Info("IPSet could not be found")
return nil, MissingSet
}
}
return addresses, nil
}
// protocolNameToNumber converts a protocol name to its numeric representation (returned as string)
func protocolNameToNumber(protocolName string) uint16 {
switch strings.ToLower(protocolName) {
case "tcp":
return 6
case "udp":
return 17
case "icmp":
return 1
case "icmpv6":
return 58
case "sctp":
return 132
case "udplite":
return 136
default:
return 256 // any (as understood by hns)
}
}
// NewRule returns a new hns switch rule object instantiated with default values.
func (s *PolicySets) NewRule(isInbound bool, priority uint16) *hns.ACLPolicy {
direction := hns.Out
if isInbound {
direction = hns.In
}
return &hns.ACLPolicy{
Type: hns.ACL,
RuleType: hns.Switch,
Action: hns.Block,
Direction: direction,
Protocol: 256, // Any, only required for RS3
Priority: priority,
}
}
// NewHostRule returns a new hns rule object scoped to the host.
func (s *PolicySets) NewHostRule(isInbound bool) *hns.ACLPolicy {
direction := hns.Out
if isInbound {
direction = hns.In
}
var priority uint16 = 0
if !s.supportedFeatures.Acl.AclNoHostRulePriority {
log.Debugf("This HNS version requires host rule priority to be specified. Adding priority=100 to Host rules.")
priority = 100
}
return &hns.ACLPolicy{
Type: hns.ACL,
RuleType: hns.Host,
Action: hns.Allow,
Direction: direction,
Protocol: 256, // Any
Priority: priority,
}
}
// filterNets filters out any addresses which are not of the requested ipVersion.
func filterNets(mixedCIDRs []string, ipVersion uint8) (filtered []string, filteredAll bool) {
if len(mixedCIDRs) == 0 {
return nil, false
}
wantV6 := ipVersion == 6
filteredAll = true
for _, net := range mixedCIDRs {
isV6 := strings.Contains(net, ":")
if isV6 != wantV6 {
continue
}
filtered = append(filtered, net)
filteredAll = false
}
return
}