forked from cilium/cilium
/
node.go
513 lines (422 loc) · 11.7 KB
/
node.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
// Copyright 2016-2017 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 policy
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/cilium/cilium/pkg/labels"
)
// Node to define hierarchy of rules
type Node struct {
path string
Name string `json:"name"`
Parent *Node `json:"-"`
Rules []PolicyRule `json:"rules,omitempty"`
Children map[string]*Node `json:"children,omitempty"`
mergeable bool
resolved bool
}
func NewNode(name string, parent *Node) *Node {
return &Node{
Name: name,
Parent: parent,
Rules: nil,
Children: map[string]*Node{},
mergeable: false,
}
}
func (n *Node) Path() string {
if n.path == "" {
n.buildPath()
}
return n.path
}
// ResolveName translates a possibly relative name to an absolute path relative to the node
func (n *Node) ResolveName(name string) string {
// If name is an absolute path already, return it
if strings.HasPrefix(name, RootPrefix) {
return name
}
for strings.HasPrefix(name, "../") {
name = name[3:]
n = n.Parent
if n == nil {
log.Warningf("Could not resolve label %+v, reached root\n", name)
return name
}
}
return JoinPath(n.Path(), name)
}
// NormalizeNames walks all policy nodes and normalizes the policy node name
// according to to the path specified. Takes a node with a list of optional
// children and the path to where the node is/will be located in the tree.
//
// 1. If the name of a node is omitted, the node name will be derived from
// the path. The element after the last node path delimiter is assumed to
// be the node name, e.g. rootNode.parentNode.name
//
// 2. If the node name is an absolute path, it must match the path but will
// be translated to a relative node name.
func (n *Node) NormalizeNames(path string) (string, error) {
if n == nil {
return path, nil
}
// Path must always be absolute. If root delimiter has not been added,
// add it now.
if !strings.HasPrefix(path, RootNodeName) {
path = JoinPath(RootNodeName, path)
}
// If no name is provided, the last node name in the path is assumed
// to be the node's name
if n.Name == "" {
if path == RootNodeName {
path, n.Name = RootNodeName, RootNodeName
} else {
path, n.Name = SplitNodePath(path)
}
}
if n.Name != RootNodeName {
// If name starts with the root prefix, it is an absolute path,
// check if it matches the provided path
if strings.HasPrefix(n.Name, RootPrefix) {
// If path is "root.foo", we need to subtract "root.foo."
toTrim := JoinPath(path, "")
if !strings.HasPrefix(n.Name, toTrim) {
return "", fmt.Errorf("absolute node name '%s' must match path '%s'",
n.Name, toTrim)
}
n.Name = strings.TrimPrefix(n.Name, toTrim)
}
// Any additional segments separated by a node path delimiter
// are moved into the path
for strings.Contains(n.Name, NodePathDelimiter) {
remainingPath, isolatedName := SplitNodePath(n.Name)
path = JoinPath(path, remainingPath)
n.Name = isolatedName
}
}
// Validate all child nodes as well
for keyName, child := range n.Children {
if child.Name != "" {
if keyName != child.Name {
return "", fmt.Errorf("map key name '%s' must match child name '%s'",
keyName, child.Name)
}
} else {
child.Name = keyName
}
if _, err := child.NormalizeNames(JoinPath(path, n.Name)); err != nil {
return "", err
}
}
return path, nil
}
func coversLabel(l *labels.Label, path string) bool {
key := l.AbsoluteKey()
// root matches everything, e.g. "." matches ".foo"
if path == RootNodeName {
return true
}
if strings.HasPrefix(key, path) {
// key and path are identical, e.g. ".foo" matches ".foo"
if len(key) == len(path) {
return true
}
// if path < prefix, then match must be up to delimiter, e.g. ".foo" matches ".foo.bar"
if len(path) < len(key) && key[len(path)] == '.' {
return true
}
}
return false
}
func (n *Node) Covers(ctx *SearchContext) bool {
for k := range ctx.To {
if coversLabel(&ctx.To[k], n.Path()) {
return true
}
}
return false
}
func (n *Node) Allows(ctx *SearchContext) ConsumableDecision {
decision := UNDECIDED
policyTraceVerbose(ctx, "Evaluating node %+v\n", n)
for k := range n.Rules {
rule := n.Rules[k]
subDecision := UNDECIDED
switch rule.(type) {
case *RuleConsumers:
r := rule.(*RuleConsumers)
subDecision = r.Allows(ctx)
case *RuleRequires:
r := rule.(*RuleRequires)
subDecision = r.Allows(ctx)
}
switch subDecision {
case ALWAYS_ACCEPT:
return ALWAYS_ACCEPT
case DENY:
return DENY
case ACCEPT:
decision = ACCEPT
}
}
return decision
}
func (n *Node) buildPath() (string, error) {
if n.Parent != nil {
// Optimization: if parent has calculated path already (likely),
// we don't have to walk to the entire root again
s := n.Parent.path
if s == "" {
var err error
if s, err = n.Parent.buildPath(); err != nil {
return "", err
}
}
n.path = JoinPath(s, n.Name)
return n.path, nil
}
if n.Name != RootNodeName {
return "", fmt.Errorf("encountered non-root node '%s' without a parent while building path", n.Name)
}
n.path = RootNodeName
return n.path, nil
}
func (n *Node) resolveRules() error {
log.Debugf("Resolving rules of node %+v\n", n)
for k := range n.Rules {
if err := n.Rules[k].Resolve(n); err != nil {
return err
}
if !n.Rules[k].IsMergeable() {
n.mergeable = false
break
}
}
return nil
}
func (n *Node) HasPolicyRule(pr PolicyRule) bool {
pr256Sum, _ := pr.SHA256Sum()
for _, r := range n.Rules {
if r256Sum, _ := r.SHA256Sum(); r256Sum == pr256Sum {
return true
}
}
return false
}
func (n *Node) ResolveTree() error {
log.Debugf("Resolving policy node %+v\n", n)
if _, err := n.buildPath(); err != nil {
return err
}
if err := n.resolveRules(); err != nil {
return err
}
for k, val := range n.Children {
n.Children[k].Parent = n
val.Parent = n
val.Name = k
if err := val.ResolveTree(); err != nil {
return err
}
}
n.isMergeable()
n.resolved = true
return nil
}
func (n *Node) isMergeable() bool {
for k := range n.Rules {
if !n.Rules[k].IsMergeable() {
n.mergeable = false
return n.mergeable
}
}
n.mergeable = true
return n.mergeable
}
// IsMergeable returns true if the node is eligible to be merged with another node
func (n *Node) IsMergeable() bool {
if n.resolved {
return n.mergeable
}
return n.isMergeable()
}
func (n *Node) UnmarshalJSON(data []byte) error {
var policyNode struct {
Name string `json:"name,omitempty"`
Rules []*json.RawMessage `json:"rules,omitempty"`
Children map[string]*Node `json:"children,omitempty"`
}
decoder := json.NewDecoder(bytes.NewReader(data))
if err := decoder.Decode(&policyNode); err != nil {
return fmt.Errorf("decode of Node failed: %s", err)
}
n.Name = policyNode.Name
n.Children = policyNode.Children
for _, rawMsg := range policyNode.Rules {
var om map[string]*json.RawMessage
if err := json.Unmarshal(*rawMsg, &om); err != nil {
return err
}
if _, ok := om[privEnc[ALLOW]]; ok {
var prC RuleConsumers
if err := json.Unmarshal(*rawMsg, &prC); err != nil {
return err
}
if n.HasPolicyRule(&prC) {
log.Infof("Ignoring rule %+v since it's already present in the list of rules", prC)
} else {
n.Rules = append(n.Rules, &prC)
}
} else if _, ok := om[privEnc[ALWAYS_ALLOW]]; ok {
var prC RuleConsumers
if err := json.Unmarshal(*rawMsg, &prC); err != nil {
return err
}
for _, r := range prC.Allow {
// DENY rules are always deny anyway
if r.Action == ACCEPT {
r.Action = ALWAYS_ACCEPT
}
}
if n.HasPolicyRule(&prC) {
log.Infof("Ignoring rule %+v since it's already present in the list of rules", prC)
} else {
n.Rules = append(n.Rules, &prC)
}
} else if _, ok := om[privEnc[REQUIRES]]; ok {
var prR RuleRequires
if err := json.Unmarshal(*rawMsg, &prR); err != nil {
return err
}
if n.HasPolicyRule(&prR) {
log.Infof("Ignoring rule %+v since it's already present in the list of rules", prR)
} else {
n.Rules = append(n.Rules, &prR)
}
} else if _, ok := om[privEnc[L4]]; ok {
var prL4 RuleL4
if err := json.Unmarshal(*rawMsg, &prL4); err != nil {
return err
}
if n.HasPolicyRule(&prL4) {
log.Infof("Ignoring rule %+v since it's already present in the list of rules", prL4)
} else {
n.Rules = append(n.Rules, &prL4)
}
} else {
return fmt.Errorf("unknown policy rule object: %+v", om)
}
}
// We have now parsed all children in a recursive manner and are back
// to the root node. Walk the tree again to resolve the path and the
// labels of all nodes and rules.
if n.Name == RootNodeName {
log.Debugf("Resolving tree: %+v\n", n)
if err := n.ResolveTree(); err != nil {
return err
}
log.Debugf("Resolved tree: %+v\n", n)
}
return nil
}
// CanMerge returns an error if obj cannot be safely merged into an existing node
func (n *Node) CanMerge(obj *Node) error {
if n.Name != obj.Name {
return fmt.Errorf("node name mismatch %q != %q", n.Name, obj.Name)
}
if obj.path != "" && n.path != obj.path {
return fmt.Errorf("node path mismatch %q != %q", n.path, obj.path)
}
if !n.IsMergeable() || !obj.IsMergeable() {
return fmt.Errorf("node %s is not mergeable", obj.Name)
}
for k := range obj.Children {
if childNode, ok := n.Children[k]; ok {
if err := childNode.CanMerge(obj.Children[k]); err != nil {
return err
}
}
}
return nil
}
// Merge incorporates the rules and children of obj into an existing node
func (n *Node) Merge(obj *Node) (bool, error) {
if err := n.CanMerge(obj); err != nil {
return false, fmt.Errorf("cannot merge node: %s", err)
}
policyModified := false
for _, objRule := range obj.Rules {
if !n.HasPolicyRule(objRule) {
n.Rules = append(n.Rules, objRule)
policyModified = true
}
}
for k := range obj.Children {
childPolicyModified, err := n.AddChild(k, obj.Children[k])
if err != nil {
log.Warningf("unexpected error while merging nodes: %s", err)
}
policyModified = policyModified || childPolicyModified
}
n.resolved = false
return policyModified, nil
}
func (n *Node) AddChild(name string, child *Node) (bool, error) {
if _, ok := n.Children[name]; ok {
child.Parent = n
child.Path()
return n.Children[name].Merge(child)
}
n.Children[name] = child
child.Parent = n
child.Path()
return true, nil
}
func (n *Node) DebugString(level int) string {
str := fmt.Sprintf("%+v\n", n)
for _, child := range n.Children {
f := fmt.Sprintf("%%%ds%%s", level*4)
str += fmt.Sprintf(f, " ", child.DebugString(level+1))
}
return str
}
func (n *Node) JSONMarshal() string {
b, err := json.MarshalIndent(n, "", " ")
if err != nil {
return err.Error()
}
return string(b)
}
func (n *Node) ResolveL4Policy(ctx *SearchContext, result *L4Policy) *L4Policy {
for k := range n.Rules {
switch n.Rules[k].(type) {
case *RuleL4:
l4 := n.Rules[k].(*RuleL4)
l4.GetL4Policy(ctx, result)
}
}
for _, child := range n.Children {
if child.Covers(ctx) {
child.ResolveL4Policy(ctx, result)
}
}
return result
}
// HasRules returns true if a node has any rules attached or at least one child.
func (n *Node) HasRules() bool {
return (n.Children != nil && len(n.Children) > 0) || len(n.Rules) > 0
}