-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
rules.go
165 lines (136 loc) · 5.21 KB
/
rules.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2016-2020 Authors of Cilium
package policy
import (
"fmt"
slim_metav1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
)
// ruleSlice is a wrapper around a slice of *rule, which allows for functions
// to be written with []*rule as a receiver.
type ruleSlice []*rule
func (rules ruleSlice) resolveL4IngressPolicy(policyCtx PolicyContext, ctx *SearchContext) (L4PolicyMap, error) {
result := L4PolicyMap{}
ctx.PolicyTrace("\n")
ctx.PolicyTrace("Resolving ingress policy for %+v\n", ctx.To)
state := traceState{}
var matchedRules ruleSlice
var requirements, requirementsDeny []slim_metav1.LabelSelectorRequirement
// Iterate over all FromRequires which select ctx.To. These requirements
// will be appended to each EndpointSelector's MatchExpressions in
// each FromEndpoints for all ingress rules. This ensures that FromRequires
// is taken into account when evaluating policy at L4.
for _, r := range rules {
if ctx.rulesSelect || r.getSelector().Matches(ctx.To) {
matchedRules = append(matchedRules, r)
for _, ingressRule := range r.Ingress {
for _, requirement := range ingressRule.FromRequires {
requirements = append(requirements, requirement.ConvertToLabelSelectorRequirementSlice()...)
}
}
for _, ingressRule := range r.IngressDeny {
for _, requirement := range ingressRule.FromRequires {
requirementsDeny = append(requirementsDeny, requirement.ConvertToLabelSelectorRequirementSlice()...)
}
}
}
}
// Only dealing with matching rules from now on. Mark it in the ctx
oldRulesSelect := ctx.rulesSelect
ctx.rulesSelect = true
for _, r := range matchedRules {
_, err := r.resolveIngressPolicy(policyCtx, ctx, &state, result, requirements, requirementsDeny)
if err != nil {
return nil, err
}
state.ruleID++
}
state.trace(len(rules), ctx)
// Restore ctx in case caller uses it again.
ctx.rulesSelect = oldRulesSelect
return result, nil
}
func (rules ruleSlice) resolveL4EgressPolicy(policyCtx PolicyContext, ctx *SearchContext) (L4PolicyMap, error) {
result := L4PolicyMap{}
ctx.PolicyTrace("\n")
ctx.PolicyTrace("Resolving egress policy for %+v\n", ctx.From)
state := traceState{}
var matchedRules ruleSlice
var requirements, requirementsDeny []slim_metav1.LabelSelectorRequirement
// Iterate over all ToRequires which select ctx.To. These requirements will
// be appended to each EndpointSelector's MatchExpressions in each
// ToEndpoints for all egress rules. This ensures that ToRequires is
// taken into account when evaluating policy at L4.
for _, r := range rules {
if ctx.rulesSelect || r.getSelector().Matches(ctx.From) {
matchedRules = append(matchedRules, r)
for _, egressRule := range r.Egress {
for _, requirement := range egressRule.ToRequires {
requirements = append(requirements, requirement.ConvertToLabelSelectorRequirementSlice()...)
}
}
for _, egressRule := range r.EgressDeny {
for _, requirement := range egressRule.ToRequires {
requirementsDeny = append(requirementsDeny, requirement.ConvertToLabelSelectorRequirementSlice()...)
}
}
}
}
// Only dealing with matching rules from now on. Mark it in the ctx
oldRulesSelect := ctx.rulesSelect
ctx.rulesSelect = true
for i, r := range matchedRules {
state.ruleID = i
_, err := r.resolveEgressPolicy(policyCtx, ctx, &state, result, requirements, requirementsDeny)
if err != nil {
return nil, err
}
state.ruleID++
}
state.trace(len(rules), ctx)
// Restore ctx in case caller uses it again.
ctx.rulesSelect = oldRulesSelect
return result, nil
}
func (rules ruleSlice) resolveCIDRPolicy(ctx *SearchContext) *CIDRPolicy {
result := NewCIDRPolicy()
ctx.PolicyTrace("Resolving L3 (CIDR) policy for %+v\n", ctx.To)
state := traceState{}
for _, r := range rules {
r.resolveCIDRPolicy(ctx, &state, result)
state.ruleID++
}
state.trace(len(rules), ctx)
return result
}
// updateEndpointsCaches iterates over a given list of rules to update the cache
// within the rule which determines whether or not the given identity is
// selected by that rule. If a rule in the list does select said identity, it is
// added to epSet. Note that epSet can be shared across goroutines!
// Returns whether the endpoint was selected by one of the rules, or if the
// endpoint is nil.
func (rules ruleSlice) updateEndpointsCaches(ep Endpoint) (bool, error) {
if ep == nil {
return false, fmt.Errorf("cannot update caches in rules because endpoint is nil")
}
id := ep.GetID16()
securityIdentity, err := ep.GetSecurityIdentity()
if err != nil {
return false, fmt.Errorf("cannot update caches in rules for endpoint %d because it is being deleted: %s", id, err)
}
if securityIdentity == nil {
return false, fmt.Errorf("cannot update caches in rules for endpoint %d because it has a nil identity", id)
}
endpointSelected := false
for _, r := range rules {
// NodeSelector can only match nodes, EndpointSelector only pods.
if (r.NodeSelector.LabelSelector != nil) != ep.IsHost() {
continue
}
// Update the matches cache of each rule, and note if
// the ep is selected by any of them.
if ruleMatches := r.matches(securityIdentity); ruleMatches {
endpointSelected = true
}
}
return endpointSelected, nil
}