forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.go
273 lines (246 loc) · 8.34 KB
/
rules_test.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
// Copyright (c) 2016-2019 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 iptables
import (
"bytes"
"errors"
"io"
"strings"
"sync"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var (
rules1 = []Rule{
{Match: MatchCriteria{"-m foobar --foobar baz"}, Action: JumpAction{Target: "biff"}},
}
rules2 = []Rule{
{Match: MatchCriteria{"-m foobar --foobar baz"}, Action: JumpAction{Target: "boff"}},
}
rules3 = []Rule{
{Match: MatchCriteria{"-m foobar --foobar baz"}, Action: JumpAction{Target: "biff"}},
{Match: MatchCriteria{"-m foobar --foobar baz"}, Action: JumpAction{Target: "boff"}},
}
)
var _ = Describe("Rule hashing tests", func() {
It("should generate different hashes for different rules", func() {
hashes1 := calculateHashes("chain", rules1)
hashes2 := calculateHashes("chain", rules2)
Expect(hashes1).NotTo(Equal(hashes2))
})
It("should generate same hash for prefix to same chain", func() {
hashes1 := calculateHashes("chain", rules1)
hashes2 := calculateHashes("chain", rules3)
Expect(hashes1[0]).To(Equal(hashes2[0]))
})
It("should generate different hashes for different chains with same rules", func() {
hashes1 := calculateHashes("chain", rules1)
hashes2 := calculateHashes("chain2", rules1)
Expect(hashes1).NotTo(Equal(hashes2))
})
It("should generate different hashes for same rule at different position", func() {
hashes2 := calculateHashes("chain", rules2)
hashes3 := calculateHashes("chain", rules3)
Expect(hashes2[0]).NotTo(Equal(hashes3[1]))
})
It("should generate a slice of same length as input", func() {
Expect(len(calculateHashes("foo", rules1))).To(Equal(len(rules1)))
Expect(len(calculateHashes("foo", rules2))).To(Equal(len(rules2)))
Expect(len(calculateHashes("foo", rules3))).To(Equal(len(rules3)))
})
})
var _ = Describe("Hash extraction tests", func() {
var table *Table
BeforeEach(func() {
fd := NewFeatureDetector(nil)
fd.GetKernelVersionReader = func() (io.Reader, error) {
return nil, errors.New("not implemented")
}
fd.NewCmd = func(name string, arg ...string) CmdIface {
return NewRealCmd("echo", "iptables v1.4.7")
}
table = NewTable(
"filter",
4,
"cali:",
&sync.Mutex{},
fd,
TableOptions{
HistoricChainPrefixes: []string{"felix-", "cali"},
ExtraCleanupRegexPattern: "an-old-rule",
BackendMode: "legacy",
LookPathOverride: func(file string) (s string, e error) {
return s, nil
},
},
)
})
It("should extract an old felix rule by prefix", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf("-A FORWARD -j felix-FORWARD\n"))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"FORWARD": {"OLD INSERT RULE"},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {"-A FORWARD -j felix-FORWARD"},
}))
})
It("should extract an old felix rule by special case", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf(
"-A FORWARD -j an-old-rule\n" +
"-A FORWARD -j ignore-me\n",
))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"FORWARD": {
"OLD INSERT RULE",
"",
},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {
"-A FORWARD -j an-old-rule",
"-",
},
}))
})
It("should extract a rule with a hash", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf(
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -j cali-FORWARD\n"))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"FORWARD": {"wUHhoiAYhphO9Mso"},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -j cali-FORWARD",
},
}))
})
It("should extract a hash or a gap from each rule", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf(
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -j cali-FORWARD\n" +
"-A FORWARD -m comment --comment \"cali:abcdefghij1234-_\" -j cali-FORWARD\n" +
"-A FORWARD --src '1.2.3.4'\n" +
"-A FORWARD -m comment --comment \"cali:1234567890093213\" -j cali-FORWARD\n"))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"FORWARD": {
"wUHhoiAYhphO9Mso",
"abcdefghij1234-_",
"",
"1234567890093213",
},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -j cali-FORWARD",
"-A FORWARD -m comment --comment \"cali:abcdefghij1234-_\" -j cali-FORWARD",
"-",
"-A FORWARD -m comment --comment \"cali:1234567890093213\" -j cali-FORWARD",
},
}))
})
It("should handle multiple chains", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf(
"-A cali-abcd -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -j cali-FORWARD\n" +
"-A cali-abcd -m comment --comment \"cali:abcdefghij1234-_\" -j cali-FORWARD\n" +
"-A FORWARD --src '1.2.3.4'\n" +
"-A FORWARD -m comment --comment \"cali:1234567890093213\" -j cali-FORWARD\n"))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"cali-abcd": {
"wUHhoiAYhphO9Mso",
"abcdefghij1234-_",
},
"FORWARD": {
"",
"1234567890093213",
},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {
"-",
"-A FORWARD -m comment --comment \"cali:1234567890093213\" -j cali-FORWARD",
},
}))
})
It("should extract a rule with a hash and a label commeent", func() {
hashes, rules, err := table.readHashesAndRulesFrom(newClosableBuf(
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -m comment --comment \"key=value\" -j cali-FORWARD\n"))
Expect(err).NotTo(HaveOccurred())
Expect(hashes).To(Equal(map[string][]string{
"FORWARD": {"wUHhoiAYhphO9Mso"},
}))
Expect(rules).To(Equal(map[string][]string{
"FORWARD": {
"-A FORWARD -m comment --comment \"cali:wUHhoiAYhphO9Mso\" -m comment --comment \"key=value\" -j cali-FORWARD",
},
}))
})
})
var _ = Describe("rule comments", func() {
Context("Rule with multiple comments", func() {
rule := Rule{
Match: MatchCriteria{"-m foobar --foobar baz"},
Action: JumpAction{Target: "biff"},
Comment: []string{"boz", "fizz"},
}
It("should render rule including multiple comments", func() {
render := rule.RenderAppend("test", "TEST", &Features{})
Expect(render).To(ContainSubstring("-m comment --comment \"boz\""))
Expect(render).To(ContainSubstring("-m comment --comment \"fizz\""))
})
})
Context("Rule with comment with newlines", func() {
rule := Rule{
Match: MatchCriteria{"-m foobar --foobar baz"},
Action: JumpAction{Target: "biff"},
Comment: []string{`boz
fizz`},
}
It("should render rule with newline escaped", func() {
render := rule.RenderAppend("test", "TEST", &Features{})
Expect(render).To(ContainSubstring("-m comment --comment \"boz_fizz\""))
})
})
Context("Rule with comment longer than 256 characters", func() {
rule := Rule{
Match: MatchCriteria{"-m foobar --foobar baz"},
Action: JumpAction{Target: "biff"},
Comment: []string{strings.Repeat("a", 257)},
}
It("should render rule with comment truncated", func() {
render := rule.RenderAppend("test", "TEST", &Features{})
Expect(render).To(ContainSubstring("-m comment --comment \"" + strings.Repeat("a", 256) + "\""))
})
})
})
func newClosableBuf(s string) *withDummyClose {
return (*withDummyClose)(bytes.NewBufferString(s))
}
type withDummyClose bytes.Buffer
func (b *withDummyClose) Read(p []byte) (n int, err error) {
return (*bytes.Buffer)(b).Read(p)
}
func (b *withDummyClose) Close() error {
return nil
}
func calculateHashes(chainName string, rules []Rule) []string {
chain := &Chain{
Name: chainName,
Rules: rules,
}
return chain.RuleHashes(&Features{})
}