/
acl_renderer.go
427 lines (384 loc) · 14.2 KB
/
acl_renderer.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
/*
* // 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 acl
import (
"net"
"github.com/gogo/protobuf/proto"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/vpp-agent/api/models/vpp/acl"
"github.com/contiv/vpp/plugins/contivconf"
controller "github.com/contiv/vpp/plugins/controller/api"
"github.com/contiv/vpp/plugins/ipnet"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
"github.com/contiv/vpp/plugins/policy/renderer"
"github.com/contiv/vpp/plugins/policy/renderer/cache"
"strings"
)
const (
// ACLNamePrefix is used to tag ACLs created for the implementation of K8s policies.
ACLNamePrefix = "contiv-policy-"
// ReflectiveACLName is the name of the *reflective* ACL (full name prefixed with
// ACLNamePrefix). Reflective ACL is used to allow responses of accepted sessions
// regardless of installed policies on the way back.
ReflectiveACLName = "REFLECTION"
ipv4AddrAny = "0.0.0.0/0"
ipv6AddrAny = "::/0"
)
// Renderer renders Contiv Rules into VPP ACLs.
// ACLs are installed into VPP by the aclplugin from vpp-agent.
// The configuration changes are transported into aclplugin via localclient.
type Renderer struct {
Deps
cache *cache.RendererCache
podInterfaces PodInterfaces
}
// Deps lists dependencies of Renderer.
type Deps struct {
Log logging.Logger
LogFactory logging.LoggerFactory /* optional */
IPNet ipnet.API /* for GetIfName() */
ContivConf ContivConf
UpdateTxnFactory func() (txn controller.UpdateOperations)
ResyncTxnFactory func() (txn controller.ResyncOperations)
}
// ContivConf interface lists methods from ContivConf plugin which are needed
// by ACL Renderer.
type ContivConf interface {
// GetMainInterfaceName returns the logical name of the VPP physical interface
// to use for connecting the node with the cluster.
// If empty, a loopback interface should be configured instead.
GetMainInterfaceName() string
// GetOtherVPPInterfaces returns configuration to apply for non-main physical
// VPP interfaces.
GetOtherVPPInterfaces() contivconf.OtherInterfaces
}
// RendererTxn represents a single transaction of Renderer.
type RendererTxn struct {
Log logging.Logger
cacheTxn cache.Txn
renderer *Renderer
resync bool
}
// PodInterfaces is a map used to remember interface of each (configured) pod.
type PodInterfaces map[podmodel.ID]string
// Init initializes the ACL Renderer.
func (r *Renderer) Init() error {
r.cache = &cache.RendererCache{}
if r.LogFactory != nil {
r.cache.Log = r.LogFactory.NewLogger("-aclCache")
} else {
r.cache.Log = r.Log
}
r.cache.Init(cache.EgressOrientation)
r.podInterfaces = make(PodInterfaces)
return nil
}
// NewTxn starts a new transaction. The rendering executes only after Commit()
// is called. Rollback is not yet supported however.
// If <resync> is enabled, the supplied configuration will completely
// replace the existing one. Otherwise, the change is performed incrementally,
// i.e. interfaces not mentioned in the transaction are left unaffected.
func (r *Renderer) NewTxn(resync bool) renderer.Txn {
txn := &RendererTxn{
Log: r.Log,
cacheTxn: r.cache.NewTxn(),
renderer: r,
resync: resync,
}
return txn
}
// Render applies the set of ingress & egress rules for a given pod.
// The existing rules are replaced.
// Te actual change is performed only after the commit.
func (art *RendererTxn) Render(pod podmodel.ID, podIP *net.IPNet, ingress []*renderer.ContivRule, egress []*renderer.ContivRule, removed bool) renderer.Txn {
art.renderer.Log.WithFields(logging.Fields{
"pod": pod,
"ingress-count": len(ingress),
"egress-count": len(egress),
"removed": removed,
}).Debug("ACL RendererTxn Render()")
art.cacheTxn.Update(pod, &cache.PodConfig{PodIP: podIP, Ingress: ingress, Egress: egress, Removed: removed})
return art
}
// Commit proceeds with the rendering. A minimalistic set of changes is
// calculated using RendererCache and applied as one transaction via the
// localclient.
func (art *RendererTxn) Commit() error {
if art.resync {
return art.commitResync()
}
var (
globalTable *cache.ContivRuleTable
hasReflectiveACL bool
)
if art.renderer.cache.GetGlobalTable().NumOfRules != 0 ||
len(art.renderer.cache.GetIsolatedPods()) > 0 {
hasReflectiveACL = true
}
// Get the minimalistic diff to be rendered.
changes := art.cacheTxn.GetChanges()
if len(changes) == 0 {
// Still need to commit the configuration updates from the transaction.
return art.cacheTxn.Commit()
}
txn := art.renderer.UpdateTxnFactory()
// First render local tables.
for _, change := range changes {
if change.Table.Type == cache.Global {
// Reconfigure global table after the local ones.
globalTable = change.Table
continue
}
if len(change.PreviousPods) == 0 {
// New ACL
acl := art.renderACL(change.Table, false)
txn.Put(vpp_acl.Key(acl.Name), acl)
} else if len(change.Table.Pods) != 0 {
// Changed interfaces
aclPrivCopy := proto.Clone(change.Table.Private.(*vpp_acl.ACL))
acl := aclPrivCopy.(*vpp_acl.ACL)
acl.Interfaces = art.renderInterfaces(change.Table.Pods, false)
txn.Put(vpp_acl.Key(acl.Name), acl)
} else {
// Removed ACL
acl := change.Table.Private.(*vpp_acl.ACL)
txn.Delete(vpp_acl.Key(acl.Name))
}
}
// Render the global table.
var gtAddedOrDeleted bool // will be true if global table is being added / removed (not updated)
if globalTable != nil {
globalACL := art.renderACL(globalTable, false)
if globalTable.NumOfRules == 0 {
// Remove empty global table.
txn.Delete(vpp_acl.Key(globalACL.Name))
gtAddedOrDeleted = true
} else {
// Update content of the global table.
globalACL.Interfaces.Egress = art.getNodeOutputInterfaces()
txn.Put(vpp_acl.Key(globalACL.Name), globalACL)
if art.renderer.cache.GetGlobalTable().NumOfRules == 0 {
gtAddedOrDeleted = true
}
}
}
// Render the reflective ACL
if gtAddedOrDeleted || !art.cacheTxn.GetIsolatedPods().Equals(art.renderer.cache.GetIsolatedPods()) {
reflectiveACL := art.reflectiveACL()
if len(reflectiveACL.Interfaces.Ingress) == 0 {
if hasReflectiveACL {
txn.Delete(vpp_acl.Key(reflectiveACL.Name))
}
} else {
txn.Put(vpp_acl.Key(reflectiveACL.Name), reflectiveACL)
}
}
// Save changes into the cache.
return art.cacheTxn.Commit()
}
// commitResync commits full ACL configuration for re-synchronization.
func (art *RendererTxn) commitResync() error {
txn := art.renderer.ResyncTxnFactory()
// reset the cache and the renderer internal state first
art.renderer.cache.Flush()
art.renderer.podInterfaces = make(PodInterfaces)
// after the flush, changes == all newly created
changes := art.cacheTxn.GetChanges()
for _, change := range changes {
if change.Table.Type == cache.Global {
// global table
globalACL := art.renderACL(change.Table, false)
globalACL.Interfaces.Egress = art.getNodeOutputInterfaces()
txn.Put(vpp_acl.Key(globalACL.Name), globalACL)
} else {
// local table
localACL := art.renderACL(change.Table, false)
txn.Put(vpp_acl.Key(localACL.Name), localACL)
}
}
// reflective ACL at last
reflectiveACL := art.reflectiveACL()
if len(reflectiveACL.Interfaces.Ingress) != 0 {
txn.Put(vpp_acl.Key(reflectiveACL.Name), reflectiveACL)
}
// save changes into the cache.
return art.cacheTxn.Commit()
}
// reflectiveACL returns the configuration of the reflective ACL.
func (art *RendererTxn) reflectiveACL() *vpp_acl.ACL {
// Prepare table to render the ACL from.
ruleAny := &renderer.ContivRule{
Action: renderer.ActionPermit,
SrcNetwork: &net.IPNet{},
DestNetwork: &net.IPNet{},
Protocol: renderer.ANY,
SrcPort: 0,
DestPort: 0,
}
table := cache.NewContivRuleTable(cache.Local)
table.Rules = []*renderer.ContivRule{ruleAny}
table.NumOfRules = 1
table.Pods = art.cacheTxn.GetIsolatedPods()
// Render the ACL.
acl := art.renderACL(table, true)
if art.cacheTxn.GetGlobalTable().NumOfRules > 0 {
acl.Interfaces.Ingress = append(acl.Interfaces.Ingress, art.getNodeOutputInterfaces()...)
}
return acl
}
// getNodeOutputInterfaces returns the list of interfaces that connect this K8s node
// with the outside world.
func (art *RendererTxn) getNodeOutputInterfaces() []string {
interfaces := []string{}
interfaces = append(interfaces, art.renderer.IPNet.GetHostInterconnectIfName())
mainIface := art.renderer.ContivConf.GetMainInterfaceName()
if mainIface != "" {
interfaces = append(interfaces, mainIface)
}
for _, otherIface := range art.renderer.ContivConf.GetOtherVPPInterfaces() {
interfaces = append(interfaces, otherIface.InterfaceName)
}
vxlanBVI := art.renderer.IPNet.GetVxlanBVIIfName()
if vxlanBVI != "" {
interfaces = append(interfaces, vxlanBVI)
}
return interfaces
}
// anyAddrForIPversion returns any addr for the IP version defined by given argument
func anyAddrForIPversion(ip string) string {
if strings.Contains(ip, ":") {
return ipv6AddrAny
}
return ipv4AddrAny
}
// expandAnyAddr replaces empty string representing any networking in rules by explicit values
func expandAnyAddr(rule *vpp_acl.ACL_Rule) []*vpp_acl.ACL_Rule {
// both networks are defined, no modification needed
if rule.IpRule.Ip.SourceNetwork != "" && rule.IpRule.Ip.DestinationNetwork != "" {
return []*vpp_acl.ACL_Rule{rule}
}
// match any version based on the filled network
if rule.IpRule.Ip.SourceNetwork != "" {
rule.IpRule.Ip.DestinationNetwork = anyAddrForIPversion(rule.IpRule.Ip.SourceNetwork)
return []*vpp_acl.ACL_Rule{rule}
}
if rule.IpRule.Ip.DestinationNetwork != "" {
rule.IpRule.Ip.SourceNetwork = anyAddrForIPversion(rule.IpRule.Ip.DestinationNetwork)
return []*vpp_acl.ACL_Rule{rule}
}
// create rules for IPv4 and IPv6 as well
rule6 := proto.Clone(rule).(*vpp_acl.ACL_Rule)
rule4 := proto.Clone(rule).(*vpp_acl.ACL_Rule)
rule4.IpRule.Ip.DestinationNetwork = ipv4AddrAny
rule4.IpRule.Ip.SourceNetwork = ipv4AddrAny
rule6.IpRule.Ip.DestinationNetwork = ipv6AddrAny
rule6.IpRule.Ip.SourceNetwork = ipv6AddrAny
return []*vpp_acl.ACL_Rule{rule4, rule6}
}
// renderACL renders ContivRuleTable into the equivalent ACL configuration.
func (art *RendererTxn) renderACL(table *cache.ContivRuleTable, isReflectiveACL bool) *vpp_acl.ACL {
const maxPortNum = ^uint16(0)
acl := &vpp_acl.ACL{}
if isReflectiveACL {
acl.Name = ACLNamePrefix + ReflectiveACLName
} else {
acl.Name = ACLNamePrefix + table.GetID()
}
acl.Interfaces = art.renderInterfaces(table.Pods, isReflectiveACL)
for i := 0; i < table.NumOfRules; i++ {
rule := table.Rules[i]
aclRule := &vpp_acl.ACL_Rule{}
if rule.Action == renderer.ActionDeny {
aclRule.Action = vpp_acl.ACL_Rule_DENY
} else if isReflectiveACL {
aclRule.Action = vpp_acl.ACL_Rule_REFLECT
} else {
aclRule.Action = vpp_acl.ACL_Rule_PERMIT
}
aclRule.IpRule = &vpp_acl.ACL_Rule_IpRule{}
aclRule.IpRule.Ip = &vpp_acl.ACL_Rule_IpRule_Ip{}
if len(rule.SrcNetwork.IP) > 0 {
aclRule.IpRule.Ip.SourceNetwork = rule.SrcNetwork.String()
}
if len(rule.DestNetwork.IP) > 0 {
aclRule.IpRule.Ip.DestinationNetwork = rule.DestNetwork.String()
}
if rule.Protocol == renderer.TCP {
aclRule.IpRule.Tcp = &vpp_acl.ACL_Rule_IpRule_Tcp{}
aclRule.IpRule.Tcp.SourcePortRange = &vpp_acl.ACL_Rule_IpRule_PortRange{}
aclRule.IpRule.Tcp.SourcePortRange.LowerPort = uint32(rule.SrcPort)
if rule.SrcPort == 0 {
aclRule.IpRule.Tcp.SourcePortRange.UpperPort = uint32(maxPortNum)
} else {
aclRule.IpRule.Tcp.SourcePortRange.UpperPort = uint32(rule.SrcPort)
}
aclRule.IpRule.Tcp.DestinationPortRange = &vpp_acl.ACL_Rule_IpRule_PortRange{}
aclRule.IpRule.Tcp.DestinationPortRange.LowerPort = uint32(rule.DestPort)
if rule.DestPort == 0 {
aclRule.IpRule.Tcp.DestinationPortRange.UpperPort = uint32(maxPortNum)
} else {
aclRule.IpRule.Tcp.DestinationPortRange.UpperPort = uint32(rule.DestPort)
}
}
if rule.Protocol == renderer.UDP {
aclRule.IpRule.Udp = &vpp_acl.ACL_Rule_IpRule_Udp{}
aclRule.IpRule.Udp.SourcePortRange = &vpp_acl.ACL_Rule_IpRule_PortRange{}
aclRule.IpRule.Udp.SourcePortRange.LowerPort = uint32(rule.SrcPort)
if rule.SrcPort == 0 {
aclRule.IpRule.Udp.SourcePortRange.UpperPort = uint32(maxPortNum)
} else {
aclRule.IpRule.Udp.SourcePortRange.UpperPort = uint32(rule.SrcPort)
}
aclRule.IpRule.Udp.DestinationPortRange = &vpp_acl.ACL_Rule_IpRule_PortRange{}
aclRule.IpRule.Udp.DestinationPortRange.LowerPort = uint32(rule.DestPort)
if rule.DestPort == 0 {
aclRule.IpRule.Udp.DestinationPortRange.UpperPort = uint32(maxPortNum)
} else {
aclRule.IpRule.Udp.DestinationPortRange.UpperPort = uint32(rule.DestPort)
}
}
acl.Rules = append(acl.Rules, expandAnyAddr(aclRule)...)
}
table.Private = acl
return acl
}
// renderInterfaces renders a set of Interface names into the corresponding
// instance of AccessLists_Acl_Interfaces.
func (art *RendererTxn) renderInterfaces(pods cache.PodSet, ingress bool) *vpp_acl.ACL_Interfaces {
aclIfs := &vpp_acl.ACL_Interfaces{}
for podID := range pods {
// Get the interface associated with the pod.
ifName, found := art.renderer.podInterfaces[podID] // first query local cache
if !found {
ifName, _, _, found = art.renderer.IPNet.GetPodIfNames(podID.Namespace, podID.Name) // next query IPNet plugin
if !found {
// pod has been deleted in the meantime but notification from K8s
// has not arrived yet
art.renderer.Log.WithField("pod", podID).Debug(
"Unable to get the interface assigned to the Pod " +
"(expecting notification about its deletion)")
continue
}
}
art.renderer.podInterfaces[podID] = ifName
if ingress {
aclIfs.Ingress = append(aclIfs.Ingress, ifName)
} else {
aclIfs.Egress = append(aclIfs.Egress, ifName)
}
}
return aclIfs
}