forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
282 lines (248 loc) · 7.93 KB
/
rule.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
// Copyright (c) 2017 Tigera, Inc. All rights reserved.
// 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 converters
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
apiv3 "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
"github.com/projectcalico/api/pkg/lib/numorstring"
apiv1 "github.com/dtest11/calico/libcalico-go/lib/apis/v1"
"github.com/dtest11/calico/libcalico-go/lib/backend/model"
"github.com/dtest11/calico/libcalico-go/lib/net"
)
// rulesAPIV1ToBackend converts an API Rule structure slice to a Backend Rule structure slice.
func rulesAPIV1ToBackend(ars []apiv1.Rule) []model.Rule {
if ars == nil {
return []model.Rule{}
}
brs := make([]model.Rule, len(ars))
for idx, ar := range ars {
brs[idx] = ruleAPIToBackend(ar)
}
return brs
}
// rulesV1BackendToV3API converts a Backend Rule structure slice to an API Rule structure slice.
func rulesV1BackendToV3API(brs []model.Rule) []apiv3.Rule {
if brs == nil {
return nil
}
ars := make([]apiv3.Rule, len(brs))
for idx, br := range brs {
ars[idx] = rulebackendToAPIv3(br)
}
return ars
}
// ruleAPIToBackend converts an API Rule structure to a Backend Rule structure.
func ruleAPIToBackend(ar apiv1.Rule) model.Rule {
var icmpCode, icmpType, notICMPCode, notICMPType *int
if ar.ICMP != nil {
icmpCode = ar.ICMP.Code
icmpType = ar.ICMP.Type
}
if ar.NotICMP != nil {
notICMPCode = ar.NotICMP.Code
notICMPType = ar.NotICMP.Type
}
return model.Rule{
Action: ruleActionV1APIToV1Backend(ar.Action),
IPVersion: ar.IPVersion,
Protocol: ar.Protocol,
ICMPCode: icmpCode,
ICMPType: icmpType,
NotProtocol: ar.NotProtocol,
NotICMPCode: notICMPCode,
NotICMPType: notICMPType,
SrcTag: ar.Source.Tag,
SrcNet: normalizeIPNet(ar.Source.Net),
SrcNets: normalizeIPNets(ar.Source.Nets),
SrcSelector: ar.Source.Selector,
SrcPorts: ar.Source.Ports,
DstTag: ar.Destination.Tag,
DstNet: normalizeIPNet(ar.Destination.Net),
DstNets: normalizeIPNets(ar.Destination.Nets),
DstSelector: ar.Destination.Selector,
DstPorts: ar.Destination.Ports,
NotSrcTag: ar.Source.NotTag,
NotSrcNet: normalizeIPNet(ar.Source.NotNet),
NotSrcNets: normalizeIPNets(ar.Source.NotNets),
NotSrcSelector: ar.Source.NotSelector,
NotSrcPorts: ar.Source.NotPorts,
NotDstTag: ar.Destination.NotTag,
NotDstNet: normalizeIPNet(ar.Destination.NotNet),
NotDstNets: normalizeIPNets(ar.Destination.NotNets),
NotDstSelector: ar.Destination.NotSelector,
NotDstPorts: ar.Destination.NotPorts,
}
}
// normalizeIPNet converts an IPNet to a network by ensuring the IP address is correctly masked.
func normalizeIPNet(n *net.IPNet) *net.IPNet {
if n == nil {
return nil
}
return n.Network()
}
// normalizeIPNets converts an []*IPNet to a slice of networks by ensuring the IP addresses
// are correctly masked.
func normalizeIPNets(nets []*net.IPNet) []*net.IPNet {
if nets == nil {
return nil
}
out := make([]*net.IPNet, len(nets))
for i, n := range nets {
out[i] = normalizeIPNet(n)
}
return out
}
// rulebackendToAPIv3 convert a Backend Rule structure to an API Rule structure.
func rulebackendToAPIv3(br model.Rule) apiv3.Rule {
var icmp, notICMP *apiv3.ICMPFields
if br.ICMPCode != nil || br.ICMPType != nil {
icmp = &apiv3.ICMPFields{
Code: br.ICMPCode,
Type: br.ICMPType,
}
}
if br.NotICMPCode != nil || br.NotICMPType != nil {
notICMP = &apiv3.ICMPFields{
Code: br.NotICMPCode,
Type: br.NotICMPType,
}
}
// Normalize the backend source Net/Nets/NotNet/NotNets
// This is because of a bug where we didn't normalize
// source (not)net(s) while converting API to backend in v1.
br.SrcNet = normalizeIPNet(br.SrcNet)
br.SrcNets = normalizeIPNets(br.SrcNets)
br.NotSrcNet = normalizeIPNet(br.NotSrcNet)
br.NotSrcNets = normalizeIPNets(br.NotSrcNets)
// Also normalize destination (Not)Net(s) for consistency.
br.DstNet = normalizeIPNet(br.DstNet)
br.DstNets = normalizeIPNets(br.DstNets)
br.NotDstNet = normalizeIPNet(br.NotDstNet)
br.NotDstNets = normalizeIPNets(br.NotDstNets)
srcNets := br.AllSrcNets()
var srcNetsStr []string
for _, net := range srcNets {
srcNetsStr = append(srcNetsStr, net.String())
}
dstNets := br.AllDstNets()
var dstNetsStr []string
for _, net := range dstNets {
dstNetsStr = append(dstNetsStr, net.String())
}
notSrcNets := br.AllNotSrcNets()
var notSrcNetsStr []string
for _, net := range notSrcNets {
notSrcNetsStr = append(notSrcNetsStr, net.String())
}
notDstNets := br.AllNotDstNets()
var notDstNetsStr []string
for _, net := range notDstNets {
notDstNetsStr = append(notDstNetsStr, net.String())
}
srcSelector := mergeTagsAndSelectors(convertSelector(br.SrcSelector), br.SrcTag)
dstSelector := mergeTagsAndSelectors(convertSelector(br.DstSelector), br.DstTag)
notSrcSelector := mergeTagsAndSelectors(convertSelector(br.NotSrcSelector), br.NotSrcTag)
notDstSelector := mergeTagsAndSelectors(convertSelector(br.NotDstSelector), br.NotDstTag)
var v3Protocol *numorstring.Protocol
if br.Protocol != nil {
protocol := numorstring.ProtocolV3FromProtocolV1(*br.Protocol)
v3Protocol = &protocol
}
var v3NotProtocol *numorstring.Protocol
if br.NotProtocol != nil {
notProtocol := numorstring.ProtocolV3FromProtocolV1(*br.NotProtocol)
v3NotProtocol = ¬Protocol
}
return apiv3.Rule{
Action: ruleActionV1ToV3API(br.Action),
IPVersion: br.IPVersion,
Protocol: v3Protocol,
ICMP: icmp,
NotProtocol: v3NotProtocol,
NotICMP: notICMP,
Source: apiv3.EntityRule{
Nets: srcNetsStr,
Selector: srcSelector,
Ports: br.SrcPorts,
NotNets: notSrcNetsStr,
NotSelector: notSrcSelector,
NotPorts: br.NotSrcPorts,
},
Destination: apiv3.EntityRule{
Nets: dstNetsStr,
Selector: dstSelector,
Ports: br.DstPorts,
NotNets: notDstNetsStr,
NotSelector: notDstSelector,
NotPorts: br.NotDstPorts,
},
}
}
// mergeTagsAndSelectors merges tags into selectors.
// Tags are deprecated in v3.0+, so we convert Tags to selectors.
// For example:
// A rule that looks like this with Calico v1 API:
//
// apiv1.Rule{
// Action: "allow",
// Source: apiv1.EntityRule{
// Tag: "tag1",
// Selector: "label1 == 'value1' || make == 'cake'",
// },
// }
//
// That rule will be converted to the following for Calico v3 API:
//
// apiv3.Rule{
// Action: "allow",
// Source: apiv3.EntityRule{
// Selector: "(label1 == 'value1' || make == 'cake') && tag1 == ''",
// },
// }
func mergeTagsAndSelectors(sel, tag string) string {
if tag != "" {
if sel != "" {
sel = fmt.Sprintf("(%s) && %s == ''", sel, tag)
} else {
sel = fmt.Sprintf("%s == ''", tag)
}
}
return sel
}
// ruleActionV1APIToV1Backend converts the rule action field value from the API
// value to the equivalent backend value.
func ruleActionV1APIToV1Backend(action string) string {
if strings.ToLower(action) == "pass" {
return "next-tier"
}
return action
}
// ruleActionV1ToV3API converts the rule action field value from the backend
// value to the equivalent API value.
func ruleActionV1ToV3API(inAction string) apiv3.Action {
if inAction == "" {
return apiv3.Allow
} else if inAction == "next-tier" {
return apiv3.Pass
} else {
for _, action := range []apiv3.Action{apiv3.Allow, apiv3.Deny, apiv3.Log, apiv3.Pass} {
if strings.EqualFold(inAction, string(action)) {
return action
}
}
}
log.Warnf("Unknown Rule action: '%s'", inAction)
return apiv3.Action(inAction)
}