-
Notifications
You must be signed in to change notification settings - Fork 114
/
configurator_impl.go
574 lines (521 loc) · 16.4 KB
/
configurator_impl.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
/*
* // Copyright (c) 2017 Cisco and/or its affiliates.
* //
* // 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 configurator
import (
"net"
"sort"
"github.com/ligato/cn-infra/logging"
"github.com/contiv/vpp/plugins/contiv"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
"github.com/contiv/vpp/plugins/policy/cache"
"github.com/contiv/vpp/plugins/policy/renderer"
"github.com/contiv/vpp/plugins/policy/utils"
)
// PolicyConfigurator translates a set of Contiv Policies into ingress and
// egress lists of Contiv Rules (n-tuples with the most basic policy rule
// definition) and applies them into the target vswitch via registered
// renderers. Allows to register multiple renderers for different network stacks.
// For the best performance, creates a shortest possible sequence of rules
// that implement a given policy. Furthermore, to allow renderers share a list
// of ingress or egress rules between interfaces, the same set of policies
// always results in the same list of rules.
type PolicyConfigurator struct {
Deps
renderers []renderer.PolicyRendererAPI
parallelRendering bool
podIPAddresses PodIPAddresses
}
// Deps lists dependencies of PolicyConfigurator.
type Deps struct {
Log logging.Logger
Cache cache.PolicyCacheAPI
Contiv contiv.API /* to get the NAT-loopback IP */
}
// PolicyConfiguratorTxn represents a single transaction of the policy configurator.
type PolicyConfiguratorTxn struct {
Log logging.Logger
configurator *PolicyConfigurator
resync bool
config map[podmodel.ID]ContivPolicies // config to render
podIPAddresses PodIPAddresses
}
// ContivPolicies is a list of policies that can be ordered by policy ID.
type ContivPolicies []*ContivPolicy
// ProcessedPolicySet stores configuration already generated for a given
// set of policies. It is used only temporarily for a duration of the commit
// for a performance optimization.
type ProcessedPolicySet struct {
policies ContivPolicies // ordered
ingress ContivRules
egress ContivRules
}
// ContivRules is a list of Contiv rules.
type ContivRules []*renderer.ContivRule
// PodIPAddresses is a map used to remember IP address for each configured pod.
type PodIPAddresses map[podmodel.ID]*net.IPNet
// Init initializes policy configurator.
func (pc *PolicyConfigurator) Init(parallelRendering bool) error {
pc.renderers = []renderer.PolicyRendererAPI{}
pc.parallelRendering = parallelRendering
pc.podIPAddresses = make(PodIPAddresses)
return nil
}
// RegisterRenderer registers a new renderer.
// The renderer will be receiving rules for all pods in this K8s node.
// It is up to the render to possibly filter out rules for pods without
// an inter-connection in the destination network stack.
func (pc *PolicyConfigurator) RegisterRenderer(renderer renderer.PolicyRendererAPI) error {
pc.renderers = append(pc.renderers, renderer)
return nil
}
// Close deallocates resource held by the configurator.
func (pc *PolicyConfigurator) Close() error {
return nil
}
// NewTxn starts a new transaction. The re-configuration executes only after
// Commit() is called. If <resync> is enabled, the supplied configuration will
// completely replace the existing one, otherwise pods not mentioned in the
// transaction are left unchanged.
func (pc *PolicyConfigurator) NewTxn(resync bool) Txn {
txn := &PolicyConfiguratorTxn{
Log: pc.Log,
configurator: pc,
resync: resync,
config: make(map[podmodel.ID]ContivPolicies),
podIPAddresses: pc.podIPAddresses.Copy(),
}
return txn
}
// Configure applies the set of policies for a given pod. The existing policies
// are replaced. The order of policies is not important (it is a set).
func (pct *PolicyConfiguratorTxn) Configure(pod podmodel.ID, policies []*ContivPolicy) Txn {
pct.Log.WithFields(logging.Fields{
"pod": pod,
"policies": policies,
}).Debug("PolicyConfigurator Configure()")
pct.config[pod] = policies
return pct
}
// Commit proceeds with the reconfiguration.
func (pct *PolicyConfiguratorTxn) Commit() error {
// Remember processed sets of policies between iterations so that the same
// set will not be processed more than once.
processed := []ProcessedPolicySet{}
// Transactions of all registered renderers.
rendererTxns := []renderer.Txn{}
for pod, unorderedPolicies := range pct.config {
var ingress ContivRules
var egress ContivRules
var delPodConfig bool
// Get target pod configuration.
podIPNet, hadIPAddr := pct.podIPAddresses[pod]
found, podData := pct.configurator.Cache.LookupPod(pod)
// Handle removed pod.
if !found || podData.IpAddress == "" {
if hadIPAddr {
pct.Log.WithField("pod", pod).Debug("Removing policies from the pod.")
delPodConfig = true
delete(pct.podIPAddresses, pod)
} else {
/* already un-configured */
continue
}
}
if !delPodConfig {
// Get pod IP address (expressed as one-host subnet).
podIPNet = utils.GetOneHostSubnet(podData.IpAddress)
if podIPNet == nil {
pct.Log.WithField("pod", pod).Warn("Pod has invalid IP address assigned")
continue
}
pct.podIPAddresses[pod] = podIPNet
// Sort policies to get the same outcome for the same set.
policies := unorderedPolicies.Copy()
sort.Sort(policies)
// Check if this set was already processed.
alreadyProcessed := false
for _, policySet := range processed {
if policySet.policies.Equals(policies) {
ingress = policySet.ingress
egress = policySet.egress
alreadyProcessed = true
}
}
// Generate rules for a set of policies not yet processed.
if !alreadyProcessed {
// Direction in policies is from the pod point of view, whereas rules
// are evaluated from the vswitch perspective.
egress = pct.generateRules(MatchIngress, policies)
ingress = pct.generateRules(MatchEgress, policies)
// Remember already processed set of policies.
processed = append(processed,
ProcessedPolicySet{
policies: policies,
ingress: ingress,
egress: egress,
})
}
}
// Start transaction on every renderer if they are not running already.
if len(rendererTxns) == 0 {
for _, renderer := range pct.configurator.renderers {
rendererTxns = append(rendererTxns, renderer.NewTxn(pct.resync))
}
}
// Add rules into the transactions.
for _, rTxn := range rendererTxns {
rTxn.Render(pod, podIPNet, ingress.Copy(), egress.Copy(), delPodConfig)
}
}
// Commit all renderer transactions.
var wasError error
rndrChan := make(chan error)
for _, rTxn := range rendererTxns {
if pct.configurator.parallelRendering {
go func(txn renderer.Txn) {
err := txn.Commit()
rndrChan <- err
}(rTxn)
} else {
err := rTxn.Commit()
if err != nil {
wasError = err
}
}
}
if pct.configurator.parallelRendering {
for i := 0; i < len(rendererTxns); i++ {
err := <-rndrChan
if err != nil {
wasError = err
}
}
}
// Save changes to the configurator.
pct.configurator.podIPAddresses = pct.podIPAddresses.Copy()
return wasError
}
// PeerPod represents the opposite pod in the policy rule.
type PeerPod struct {
ID podmodel.ID
IPNet *net.IPNet
}
// Generate a list of ingress or egress rules implementing a given list of policies.
func (pct *PolicyConfiguratorTxn) generateRules(direction MatchType, policies ContivPolicies) ContivRules {
rules := ContivRules{}
hasPolicy := false
allAllowed := false
for _, policy := range policies {
if (policy.Type == PolicyIngress && direction == MatchEgress) ||
(policy.Type == PolicyEgress && direction == MatchIngress) {
// Policy does not apply to this direction.
continue
}
hasPolicy = true
for _, match := range policy.Matches {
if match.Type != direction {
continue
}
// Collect IP addresses of all pod peers.
peers := []PeerPod{}
for _, peer := range match.Pods {
found, peerData := pct.configurator.Cache.LookupPod(peer)
if !found {
pct.Log.WithField("peer", peer).Warn("Peer pod data not found in the cache")
continue
}
if peerData.IpAddress == "" {
pct.Log.WithField("peer", peer).Warn("Peer pod has no IP address assigned")
continue
}
peerIPNet := utils.GetOneHostSubnet(peerData.IpAddress)
if peerIPNet == nil {
pct.Log.WithFields(logging.Fields{
"peer": peer,
"ip": peerData.IpAddress}).Warn("Peer pod has invalid IP address assigned")
continue
}
peers = append(peers, PeerPod{ID: peer, IPNet: peerIPNet})
}
// Collect all subnets from IPBlocks.
allSubnets := []*net.IPNet{}
for _, block := range match.IPBlocks {
subnets := []*net.IPNet{&block.Network}
for _, except := range block.Except {
subtracted := []*net.IPNet{}
for _, subnet := range subnets {
subtracted = append(subtracted, subtractSubnet(subnet, &except)...)
}
subnets = subtracted
}
allSubnets = append(allSubnets, subnets...)
}
// Handle undefined set of pods and IP blocks.
// = match anything on L3
if match.Pods == nil && match.IPBlocks == nil {
if len(match.Ports) == 0 {
// = match anything on L3 & L4
ruleAny := &renderer.ContivRule{
Action: renderer.ActionPermit,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
Protocol: renderer.ANY,
SrcPort: 0,
DestPort: 0,
}
rules = pct.appendRules(rules, ruleAny)
allAllowed = true
} else {
// = match by L4
for _, port := range match.Ports {
rule := &renderer.ContivRule{
Action: renderer.ActionPermit,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: port.Number,
}
if port.Protocol == TCP {
rule.Protocol = renderer.TCP
} else {
rule.Protocol = renderer.UDP
}
rules = pct.appendRules(rules, rule)
}
}
}
// Combine pod peers with ports.
for _, peer := range peers {
if len(match.Ports) == 0 {
// Match all ports.
// = match by L3
ruleAny := &renderer.ContivRule{
Action: renderer.ActionPermit,
Protocol: renderer.ANY,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: 0,
}
if direction == MatchIngress {
ruleAny.SrcNetwork = peer.IPNet
} else {
ruleAny.DestNetwork = peer.IPNet
}
rules = pct.appendRules(rules, ruleAny)
} else {
// Combine each port with the peer.
// = match by L3 & L4
for _, port := range match.Ports {
rule := &renderer.ContivRule{
Action: renderer.ActionPermit,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: port.Number,
}
if direction == MatchIngress {
rule.SrcNetwork = peer.IPNet
} else {
rule.DestNetwork = peer.IPNet
}
if port.Protocol == TCP {
rule.Protocol = renderer.TCP
} else {
rule.Protocol = renderer.UDP
}
rules = pct.appendRules(rules, rule)
}
}
}
// Combine IPBlocks with ports.
for _, subnet := range allSubnets {
if len(match.Ports) == 0 {
// Handle IPBlock with no ports.
// = match by L3
ruleAny := &renderer.ContivRule{
Action: renderer.ActionPermit,
Protocol: renderer.ANY,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: 0,
}
if direction == MatchIngress {
ruleAny.SrcNetwork = subnet
} else {
ruleAny.DestNetwork = subnet
}
rules = pct.appendRules(rules, ruleAny)
} else {
// Combine each port with the block.
// = match by L3 & L4
for _, port := range match.Ports {
rule := &renderer.ContivRule{
Action: renderer.ActionPermit,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: port.Number,
}
if direction == MatchIngress {
rule.SrcNetwork = subnet
} else {
rule.DestNetwork = subnet
}
if port.Protocol == TCP {
rule.Protocol = renderer.TCP
} else {
rule.Protocol = renderer.UDP
}
rules = pct.appendRules(rules, rule)
}
}
}
}
}
if hasPolicy && !allAllowed {
if direction == MatchIngress {
// Allow connections from the virtual NAT-loopback (access to service from itself).
natLoopIP := pct.configurator.Contiv.GetNatLoopbackIP()
ruleAny := &renderer.ContivRule{
Action: renderer.ActionPermit,
Protocol: renderer.ANY,
SrcNetwork: utils.GetOneHostSubnetFromIP(natLoopIP),
DestNetwork: &net.IPNet{},
SrcPort: 0,
DestPort: 0,
}
rules = pct.appendRules(rules, ruleAny)
}
// Deny the rest.
ruleNone := &renderer.ContivRule{
Action: renderer.ActionDeny,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
Protocol: renderer.ANY,
SrcPort: 0,
DestPort: 0,
}
rules = pct.appendRules(rules, ruleNone)
}
return rules
}
// Append rule into the list if it is not there already.
func (pct *PolicyConfiguratorTxn) appendRule(rules []*renderer.ContivRule, newRule *renderer.ContivRule) []*renderer.ContivRule {
for _, rule := range rules {
if rule.Compare(newRule) == 0 {
pct.Log.WithField("rule", newRule).Debug("Skipping duplicate rule")
return rules
}
}
return append(rules, newRule)
}
// Append rules into the list. Skip those which are already there.
func (pct *PolicyConfiguratorTxn) appendRules(rules []*renderer.ContivRule, newRules ...*renderer.ContivRule) []*renderer.ContivRule {
for _, newRule := range newRules {
rules = pct.appendRule(rules, newRule)
}
return rules
}
// Copy creates a shallow copy of ContivPolicies.
func (cp ContivPolicies) Copy() ContivPolicies {
cpCopy := make(ContivPolicies, len(cp))
copy(cpCopy, cp)
return cpCopy
}
// Equals returns true for equal lists of policies.
func (cp ContivPolicies) Equals(cp2 ContivPolicies) bool {
if len(cp) != len(cp2) {
return false
}
for idx, policy := range cp {
if policy.ID != cp2[idx].ID {
return false
}
}
return true
}
// Len return the number of policies in the list.
func (cp ContivPolicies) Len() int {
return len(cp)
}
// Swap replaces order of two policies in the list.
func (cp ContivPolicies) Swap(i, j int) {
cp[i], cp[j] = cp[j], cp[i]
}
// Less compares two policies by their IDs.
func (cp ContivPolicies) Less(i, j int) bool {
if cp[i].ID.Namespace < cp[j].ID.Namespace {
return true
}
if cp[i].ID.Namespace == cp[j].ID.Namespace {
if cp[i].ID.Name < cp[j].ID.Name {
return true
}
}
return false
}
// Copy creates a deep copy of ContivRules.
func (cr ContivRules) Copy() ContivRules {
crCopy := make(ContivRules, len(cr))
for idx, rule := range cr {
crCopy[idx] = &renderer.ContivRule{}
*(crCopy[idx]) = *rule
}
return crCopy
}
// Copy creates a deep copy of PodIPAddresses.
func (pa PodIPAddresses) Copy() PodIPAddresses {
paCopy := make(PodIPAddresses, len(pa))
for pod, addr := range pa {
paCopy[pod] = addr
}
return paCopy
}
// Function returns a list of subnets with all IPs included in net1 and not included in net2.
func subtractSubnet(net1, net2 *net.IPNet) []*net.IPNet {
result := []*net.IPNet{}
net1MaskSize, _ := net1.Mask.Size()
net2MaskSize, _ := net2.Mask.Size()
if net1MaskSize > net2MaskSize {
// net2 higher than net1 in the tree
if !net2.Contains(net1.IP) {
result = append(result, net1)
}
} else if net1MaskSize == net2MaskSize {
// same level in the tree
if !net1.IP.Equal(net2.IP) {
result = append(result, net1)
}
} else {
// net2 lower then net1 in the tree
if !net1.Contains(net2.IP) {
result = append(result, net1)
} else {
// net2 under net1
for bit := net1MaskSize; bit < net2MaskSize; bit++ {
subnet := &net.IPNet{}
subnet.Mask = net.CIDRMask(bit+1, len(net2.Mask)*8)
subnet.IP = net2.IP.Mask(subnet.Mask)
// flip the last bit of the IP
subnet.IP[bit/8] ^= byte(1 << uint(7-(bit%8)))
result = append(result, subnet)
}
}
}
return result
}