forked from cilium/cilium
/
rules.go
251 lines (214 loc) · 8.04 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
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
// Copyright 2016-2019 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 (
"fmt"
"strconv"
"github.com/cilium/cilium/pkg/policy/api"
v1 "k8s.io/apimachinery/pkg/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) wildcardL3L4Rules(ingress bool, l4Policy L4PolicyMap, requirements []v1.LabelSelectorRequirement, selectorCache *SelectorCache) {
// Duplicate L3-only rules into wildcard L7 rules.
for _, r := range rules {
if ingress {
for _, rule := range r.Ingress {
// Non-label-based rule. Ignore.
if !rule.IsLabelBased() {
continue
}
fromEndpoints := rule.GetSourceEndpointSelectorsWithRequirements(requirements)
ruleLabels := r.Rule.Labels.DeepCopy()
// L3-only rule.
if len(rule.ToPorts) == 0 && len(fromEndpoints) > 0 {
wildcardL3L4Rule(api.ProtoTCP, 0, fromEndpoints, ruleLabels, l4Policy, selectorCache)
wildcardL3L4Rule(api.ProtoUDP, 0, fromEndpoints, ruleLabels, l4Policy, selectorCache)
} else {
// L4-only or L3-dependent L4 rule.
//
// "fromEndpoints" may be empty here, which indicates that all L3 peers should
// be selected. If so, add the wildcard selector.
if len(fromEndpoints) == 0 {
fromEndpoints = append(fromEndpoints, api.WildcardEndpointSelector)
}
for _, toPort := range rule.ToPorts {
// L3/L4-only rule
if toPort.Rules.IsEmpty() {
for _, p := range toPort.Ports {
// Already validated via PortRule.Validate().
port, _ := strconv.ParseUint(p.Port, 0, 16)
wildcardL3L4Rule(p.Protocol, int(port), fromEndpoints, ruleLabels, l4Policy, selectorCache)
}
}
}
}
}
} else {
for _, rule := range r.Egress {
// Non-label-based rule. Ignore.
if !rule.IsLabelBased() {
continue
}
toEndpoints := rule.GetDestinationEndpointSelectorsWithRequirements(requirements)
ruleLabels := r.Rule.Labels.DeepCopy()
// L3-only rule.
if len(rule.ToPorts) == 0 && len(toEndpoints) > 0 {
wildcardL3L4Rule(api.ProtoTCP, 0, toEndpoints, ruleLabels, l4Policy, selectorCache)
wildcardL3L4Rule(api.ProtoUDP, 0, toEndpoints, ruleLabels, l4Policy, selectorCache)
} else {
// L4-only or L3-dependent L4 rule.
//
// "toEndpoints" may be empty here, which indicates that all L3 peers should
// be selected. If so, add the wildcard selector.
if len(toEndpoints) == 0 {
toEndpoints = append(toEndpoints, api.WildcardEndpointSelector)
}
for _, toPort := range rule.ToPorts {
// L3/L4-only rule
if toPort.Rules.IsEmpty() {
for _, p := range toPort.Ports {
// Already validated via PortRule.Validate().
port, _ := strconv.ParseUint(p.Port, 0, 16)
wildcardL3L4Rule(p.Protocol, int(port), toEndpoints, ruleLabels, l4Policy, selectorCache)
}
}
}
}
}
}
}
}
func (rules ruleSlice) resolveL4IngressPolicy(ctx *SearchContext, revision uint64, selectorCache *SelectorCache) (L4PolicyMap, error) {
result := L4PolicyMap{}
ctx.PolicyTrace("\n")
ctx.PolicyTrace("Resolving ingress policy for %+v\n", ctx.To)
state := traceState{}
var matchedRules ruleSlice
var requirements []v1.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.EndpointSelector.Matches(ctx.To) {
matchedRules = append(matchedRules, r)
for _, ingressRule := range r.Ingress {
for _, requirement := range ingressRule.FromRequires {
requirements = append(requirements, 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 {
found, err := r.resolveIngressPolicy(ctx, &state, result, requirements, selectorCache)
if err != nil {
return nil, err
}
state.ruleID++
if found != nil {
state.matchedRules++
}
}
matchedRules.wildcardL3L4Rules(true, result, requirements, selectorCache)
state.trace(len(rules), ctx)
// Restore ctx in case caller uses it again.
ctx.rulesSelect = oldRulesSelect
return result, nil
}
func (rules ruleSlice) resolveL4EgressPolicy(ctx *SearchContext, revision uint64, selectorCache *SelectorCache) (L4PolicyMap, error) {
result := L4PolicyMap{}
ctx.PolicyTrace("\n")
ctx.PolicyTrace("Resolving egress policy for %+v\n", ctx.From)
state := traceState{}
var matchedRules ruleSlice
var requirements []v1.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.EndpointSelector.Matches(ctx.From) {
matchedRules = append(matchedRules, r)
for _, egressRule := range r.Egress {
for _, requirement := range egressRule.ToRequires {
requirements = append(requirements, 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
found, err := r.resolveEgressPolicy(ctx, &state, result, requirements, selectorCache)
if err != nil {
return nil, err
}
state.ruleID++
if found != nil {
state.matchedRules++
}
}
matchedRules.wildcardL3L4Rules(false, result, requirements, selectorCache)
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()
if err := ep.RLockAlive(); err != nil {
return false, fmt.Errorf("cannot update caches in rules for endpoint %d because it is being deleted: %s", id, err)
}
securityIdentity := ep.GetSecurityIdentity()
ep.RUnlock()
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 {
// 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
}