forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_convert.go
284 lines (258 loc) · 8.48 KB
/
rule_convert.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
// Copyright (c) 2020-2021 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 calc
import (
"crypto/sha256"
"encoding/base64"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/api/pkg/lib/numorstring"
"github.com/projectcalico/calico/felix/proto"
"github.com/projectcalico/calico/libcalico-go/lib/net"
)
const (
// Compromise: shorter is better for occupancy and readability. Longer is better for
// collision-resistance. 16 chars gives us 96 bits of entropy, which is fairly collision
// resistant.
RuleIDLength = 16
)
func parsedRulesToProtoRules(in []*ParsedRule, ruleIDSeed string) (out []*proto.Rule) {
out = make([]*proto.Rule, len(in))
for ii, inRule := range in {
out[ii] = parsedRuleToProtoRule(inRule)
}
fillInRuleIDs(out, ruleIDSeed)
return
}
func fillInRuleIDs(rules []*proto.Rule, ruleIDSeed string) {
s := sha256.New224()
_, err := s.Write([]byte(ruleIDSeed))
if err != nil {
log.WithError(err).Panic("failed to write rule hash")
}
hash := s.Sum(nil)
for ii, rule := range rules {
// Each hash chains in the previous hash, so that its position in the chain and
// the rules before it affect its hash.
s.Reset()
_, err = s.Write(hash)
if err != nil {
log.WithError(err).WithField("rule", rule).Panic("Failed to write hash for rule")
}
// We need a form of the rule that we can hash. Convert it to the protobuf
// binary representation, which is deterministic, at least for a given rev of the
// library.
// TODO(smc) Can we do better than hashing the protobuf?
rule.RuleId = ""
data, err := rule.Marshal()
if err != nil {
log.WithError(err).WithField("rule", rule).Panic("Failed to marshal rule")
}
_, err = s.Write(data)
if err != nil {
log.WithError(err).WithField("rule", rule).Panic("Failed to write marshalled rule")
}
hash = s.Sum(hash[0:0])
// Encode the hash using a compact character set. We use the URL-safe base64
// variant because it uses '-' and '_', which are more shell-friendly.
ruleID := base64.RawURLEncoding.EncodeToString(hash)[:RuleIDLength]
if log.GetLevel() >= log.DebugLevel {
log.WithFields(log.Fields{
"rule": rule,
"action": rule.Action,
"position": ii,
"seed": ruleIDSeed,
"ruleID": ruleID,
}).Debug("Calculated rule ID")
}
rule.RuleId = ruleID
}
}
func parsedRuleToProtoRule(in *ParsedRule) *proto.Rule {
out := &proto.Rule{
Action: in.Action,
IpVersion: ipVersionToProtoIPVersion(in.IPVersion, in.Protocol),
Protocol: protocolToProtoProtocol(in.Protocol),
SrcNet: ipNetsToProtoStrings(in.SrcNets),
SrcPorts: portsToProtoPorts(in.SrcPorts),
SrcNamedPortIpSetIds: in.SrcNamedPortIPSetIDs,
DstNet: ipNetsToProtoStrings(in.DstNets),
DstPorts: portsToProtoPorts(in.DstPorts),
DstNamedPortIpSetIds: in.DstNamedPortIPSetIDs,
SrcIpSetIds: in.SrcIPSetIDs,
DstIpSetIds: in.DstIPSetIDs,
DstIpPortSetIds: in.DstIPPortSetIDs,
NotProtocol: protocolToProtoProtocol(in.NotProtocol),
NotSrcNet: ipNetsToProtoStrings(in.NotSrcNets),
NotSrcPorts: portsToProtoPorts(in.NotSrcPorts),
NotSrcNamedPortIpSetIds: in.NotSrcNamedPortIPSetIDs,
NotDstNet: ipNetsToProtoStrings(in.NotDstNets),
NotDstPorts: portsToProtoPorts(in.NotDstPorts),
NotDstNamedPortIpSetIds: in.NotDstNamedPortIPSetIDs,
NotSrcIpSetIds: in.NotSrcIPSetIDs,
NotDstIpSetIds: in.NotDstIPSetIDs,
// Pass through fields for the policy sync API.
OriginalSrcSelector: in.OriginalSrcSelector,
OriginalSrcNamespaceSelector: in.OriginalSrcNamespaceSelector,
OriginalDstSelector: in.OriginalDstSelector,
OriginalDstNamespaceSelector: in.OriginalDstNamespaceSelector,
OriginalNotSrcSelector: in.OriginalNotSrcSelector,
OriginalNotDstSelector: in.OriginalNotDstSelector,
OriginalSrcService: in.OriginalSrcService,
OriginalSrcServiceNamespace: in.OriginalSrcServiceNamespace,
OriginalDstService: in.OriginalDstService,
OriginalDstServiceNamespace: in.OriginalDstServiceNamespace,
}
if len(in.OriginalSrcServiceAccountNames) > 0 || in.OriginalSrcServiceAccountSelector != "" {
out.SrcServiceAccountMatch = &proto.ServiceAccountMatch{
Selector: in.OriginalSrcServiceAccountSelector,
Names: in.OriginalSrcServiceAccountNames,
}
}
if len(in.OriginalDstServiceAccountNames) > 0 || in.OriginalDstServiceAccountSelector != "" {
out.DstServiceAccountMatch = &proto.ServiceAccountMatch{
Selector: in.OriginalDstServiceAccountSelector,
Names: in.OriginalDstServiceAccountNames,
}
}
if in.HTTPMatch != nil {
out.HttpMatch = &proto.HTTPMatch{}
var paths []*proto.HTTPMatch_PathMatch
for _, pathMatch := range in.HTTPMatch.Paths {
if pathMatch.Exact != "" {
protoMatch := &proto.HTTPMatch_PathMatch_Exact{Exact: pathMatch.Exact}
paths = append(paths, &proto.HTTPMatch_PathMatch{PathMatch: protoMatch})
} else if pathMatch.Prefix != "" {
protoMatch := &proto.HTTPMatch_PathMatch_Prefix{Prefix: pathMatch.Prefix}
paths = append(paths, &proto.HTTPMatch_PathMatch{PathMatch: protoMatch})
} else {
log.Error("Ignoring unknown patch match type", pathMatch)
}
}
if len(paths) > 0 {
log.WithFields(log.Fields{"paths": paths}).Debug("protoPaths")
out.HttpMatch.Paths = paths
}
if len(in.HTTPMatch.Methods) > 0 {
out.HttpMatch.Methods = in.HTTPMatch.Methods
}
}
if in.Metadata != nil {
if in.Metadata.Annotations != nil {
out.Metadata = &proto.RuleMetadata{Annotations: make(map[string]string)}
for k, v := range in.Metadata.Annotations {
out.Metadata.Annotations[k] = v
}
}
}
// Fill in the ICMP fields. We can't follow the pattern and make a
// convertICMP() function because we can't name the return type of the
// function (it's private to the protobuf package).
if in.ICMPType != nil {
if in.ICMPCode != nil {
out.Icmp = &proto.Rule_IcmpTypeCode{
IcmpTypeCode: &proto.IcmpTypeAndCode{
Type: int32(*in.ICMPType),
Code: int32(*in.ICMPCode),
},
}
} else {
out.Icmp = &proto.Rule_IcmpType{
IcmpType: int32(*in.ICMPType),
}
}
}
if in.NotICMPType != nil {
if in.NotICMPCode != nil {
out.NotIcmp = &proto.Rule_NotIcmpTypeCode{
NotIcmpTypeCode: &proto.IcmpTypeAndCode{
Type: int32(*in.NotICMPType),
Code: int32(*in.NotICMPCode),
},
}
} else {
out.NotIcmp = &proto.Rule_NotIcmpType{
NotIcmpType: int32(*in.NotICMPType),
}
}
}
log.WithFields(log.Fields{
"in": in,
"out": out,
}).Debug("Converted rule to protobuf format.")
return out
}
func ipVersionToProtoIPVersion(in *int, p *numorstring.Protocol) proto.IPVersion {
if in == nil {
// No explicit version, see if we can work out the version from the protocol.
if p == nil {
return proto.IPVersion_ANY
}
switch p.String() {
case "icmp":
return proto.IPVersion_IPV4
case "icmpv6":
return proto.IPVersion_IPV6
default:
return proto.IPVersion_ANY
}
}
switch *in {
case 4:
return proto.IPVersion_IPV4
case 6:
return proto.IPVersion_IPV6
}
return proto.IPVersion_ANY
}
func protocolToProtoProtocol(in *numorstring.Protocol) (out *proto.Protocol) {
if in != nil {
if in.Type == numorstring.NumOrStringNum {
out = &proto.Protocol{
NumberOrName: &proto.Protocol_Number{
Number: int32(in.NumVal),
},
}
} else {
out = &proto.Protocol{
NumberOrName: &proto.Protocol_Name{Name: in.StrVal},
}
}
}
return
}
func ipNetsToProtoStrings(in []*net.IPNet) (out []string) {
for _, n := range in {
if n != nil {
out = append(out, n.String())
}
}
return
}
func portsToProtoPorts(in []numorstring.Port) (out []*proto.PortRange) {
if len(in) == 0 {
return
}
out = make([]*proto.PortRange, len(in))
for ii, port := range in {
out[ii] = portToProtoPort(port)
}
return
}
func portToProtoPort(in numorstring.Port) (out *proto.PortRange) {
out = &proto.PortRange{
First: int32(in.MinPort),
Last: int32(in.MaxPort),
}
return
}