forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_iptables.py
354 lines (291 loc) · 11.4 KB
/
test_iptables.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from collections import namedtuple
import iptc
import mock
import pytest
from paasta_tools import iptables
EMPTY_RULE = iptables.Rule(
protocol="ip",
src="0.0.0.0/0.0.0.0",
dst="0.0.0.0/0.0.0.0",
target=None,
matches=(),
target_parameters=(),
)
@pytest.yield_fixture
def mock_Table():
with mock.patch.object(iptc, "Table", autospec=True) as m:
m.return_value.autocommit = True
yield m
@pytest.yield_fixture
def mock_Chain():
with mock.patch.object(iptc, "Chain", autospec=True) as m:
yield m
def test_rule_from_iptc_simple():
rule = iptc.Rule()
rule.create_target("DROP")
rule.src = "169.229.226.0/255.255.255.0"
assert iptables.Rule.from_iptc(rule) == EMPTY_RULE._replace(
src="169.229.226.0/255.255.255.0", target="DROP"
)
def test_rule_from_iptc_mac_match():
rule = iptc.Rule()
rule.create_target("DROP")
rule.create_match("mac")
rule.matches[0].mac_source = "20:C9:D0:2B:6F:F3"
assert iptables.Rule.from_iptc(rule) == EMPTY_RULE._replace(
target="DROP", matches=(("mac", (("mac-source", ("20:C9:D0:2B:6F:F3",)),)),)
)
def test_rule_from_iptc_target_parameters():
rule = iptc.Rule()
target = rule.create_target("LOG")
target.set_parameter("log-prefix", "my-prefix ")
assert iptables.Rule.from_iptc(rule) == EMPTY_RULE._replace(
target="LOG", target_parameters=(("log-prefix", ("my-prefix ",)),)
)
def test_rule_tcp_to_iptc():
rule = EMPTY_RULE._replace(
protocol="tcp", target="ACCEPT", matches=(("tcp", (("dport", ("443",)),)),)
).to_iptc()
assert rule.protocol == "tcp"
assert rule.target.name == "ACCEPT"
assert len(rule.matches) == 1
assert rule.matches[0].name == "tcp"
assert rule.matches[0].parameters["dport"] == "443"
def test_mac_src_to_iptc():
rule = EMPTY_RULE._replace(
target="ACCEPT", matches=(("mac", (("mac-source", ("20:C9:D0:2B:6F:F3",)),)),)
).to_iptc()
assert rule.protocol == "ip"
assert rule.target.name == "ACCEPT"
assert len(rule.matches) == 1
assert rule.matches[0].name == "mac"
assert rule.matches[0].parameters["mac_source"] == "20:C9:D0:2B:6F:F3"
def test_iptables_txn_normal():
table = mock.Mock(autocommit=True)
with iptables.iptables_txn(table):
assert table.autocommit is False
assert table.commit.called is False
assert table.refresh.called is False
assert table.commit.called is True
assert table.refresh.called is True
assert table.autocommit is True
def test_iptables_txn_with_exception():
table = mock.Mock(autocommit=True)
with pytest.raises(ValueError):
with iptables.iptables_txn(table):
raise ValueError("just testing lol")
assert table.commit.called is False
assert table.refresh.called is True
assert table.autocommit is True
def test_all_chains(mock_Table):
chain1 = mock.Mock()
chain1.name = "INPUT"
chain2 = mock.Mock()
chain2.name = "OUTPUT"
mock_Table.return_value = mock.Mock(chains=[chain1, chain2])
assert iptables.all_chains() == {"INPUT", "OUTPUT"}
def test_ensure_chain():
with mock.patch.object(
iptables,
"list_chain",
autospec=True,
return_value={
EMPTY_RULE._replace(target="DROP"),
EMPTY_RULE._replace(target="ACCEPT", src="1.0.0.0/255.255.255.0"),
},
), mock.patch.object(
iptables, "insert_rule", autospec=True
) as mock_insert_rule, mock.patch.object(
iptables, "delete_rules", autospec=True
) as mock_delete_rules:
iptables.ensure_chain(
"PAASTA.service",
(
EMPTY_RULE._replace(target="DROP"),
EMPTY_RULE._replace(target="ACCEPT", src="2.0.0.0/255.255.255.0"),
),
)
# It should add the missing rule
assert mock_insert_rule.mock_calls == [
mock.call(
"PAASTA.service",
EMPTY_RULE._replace(target="ACCEPT", src="2.0.0.0/255.255.255.0"),
)
]
# It should delete the extra rule
assert mock_delete_rules.mock_calls == [
mock.call(
"PAASTA.service",
{EMPTY_RULE._replace(target="ACCEPT", src="1.0.0.0/255.255.255.0")},
)
]
def test_ensure_chain_creates_chain_if_doesnt_exist():
with mock.patch.object(
iptables, "list_chain", side_effect=iptables.ChainDoesNotExist("PAASTA.service")
), mock.patch.object(iptables, "create_chain", autospec=True) as mock_create_chain:
iptables.ensure_chain("PAASTA.service", ())
assert mock_create_chain.mock_calls == [mock.call("PAASTA.service")]
def test_ensure_rule_does_not_exist():
with mock.patch.object(
iptables,
"list_chain",
return_value=(
EMPTY_RULE._replace(target="ACCEPT"),
EMPTY_RULE._replace(src="10.0.0.0/255.255.255.0"),
),
), mock.patch.object(iptables, "insert_rule", autospec=True) as mock_insert_rule:
iptables.ensure_rule("PAASTA.service", EMPTY_RULE._replace(target="DROP"))
assert mock_insert_rule.mock_calls == [
mock.call("PAASTA.service", EMPTY_RULE._replace(target="DROP"))
]
def test_ensure_rule_already_exists():
with mock.patch.object(
iptables,
"list_chain",
return_value=(
EMPTY_RULE._replace(target="DROP"),
EMPTY_RULE._replace(src="10.0.0.0/255.255.255.0"),
),
), mock.patch.object(iptables, "insert_rule", autospec=True) as mock_insert_rule:
iptables.ensure_rule("PAASTA.service", EMPTY_RULE._replace(target="DROP"))
assert mock_insert_rule.called is False
def test_insert_rule(mock_Table, mock_Chain):
iptables.insert_rule("PAASTA.service", EMPTY_RULE._replace(target="DROP"))
(call,) = mock_Chain("filter", "PAASTA.service").insert_rule.call_args_list
args, kwargs = call
(rule,) = args
assert iptables.Rule.from_iptc(rule) == EMPTY_RULE._replace(target="DROP")
def test_delete_rules(mock_Table, mock_Chain):
mock_Chain.return_value.rules = (
EMPTY_RULE._replace(target="DROP").to_iptc(),
EMPTY_RULE._replace(target="ACCEPT").to_iptc(),
EMPTY_RULE._replace(
target="REJECT",
target_parameters=(("reject-with", ("icmp-port-unreachable",)),),
).to_iptc(),
)
iptables.delete_rules(
"PAASTA.service",
(
EMPTY_RULE._replace(target="ACCEPT"),
EMPTY_RULE._replace(
target="REJECT",
target_parameters=(("reject-with", ("icmp-port-unreachable",)),),
),
),
)
assert mock_Chain("filter", "PAASTA.service").delete_rule.mock_calls == [
mock.call(mock_Chain.return_value.rules[1]),
mock.call(mock_Chain.return_value.rules[2]),
]
def test_create_chain(mock_Table):
iptables.create_chain("PAASTA.service")
mock_Table("filter").create_chain.assert_called_once_with("PAASTA.service")
def test_delete_chain(mock_Table, mock_Chain):
iptables.delete_chain("PAASTA.service")
chain = mock_Chain("filter", "PAASTA.service")
assert chain.flush.called is True
assert chain.delete.called is True
def test_list_chain_simple(mock_Table, mock_Chain):
chain = mock_Chain("PAASTA.internet", mock_Table.return_value)
rule = iptc.Rule()
rule.create_target("DROP")
chain.rules = [rule]
mock_Table.return_value.chains = [chain]
assert iptables.list_chain("PAASTA.internet") == (
EMPTY_RULE._replace(target="DROP"),
)
def test_list_chain_does_not_exist(mock_Table, mock_Chain):
mock_Table.return_value.chains = []
with pytest.raises(iptables.ChainDoesNotExist):
iptables.list_chain("PAASTA.internet")
class TestReorderChain:
class FakeRule(namedtuple("FakeRule", ("target", "id"))):
def to_iptc(self):
return self
@pytest.yield_fixture(autouse=True)
def chain_mock(self):
with mock.patch.object(
iptables, "iptables_txn", autospec=True
), mock.patch.object(iptables.iptc, "Table", autospec=True), mock.patch.object(
iptables.iptc, "Chain", autospec=True
) as chain_mock, mock.patch.object(
iptables, "list_chain", autospec=True
) as list_chain_mock:
self.chain_mock = chain_mock
self.list_chain_mock = list_chain_mock
yield
def test_reorder_chain_flip(self):
self.list_chain_mock.return_value = [
self.FakeRule("REJECT", "a"),
self.FakeRule("LOG", "b"),
self.FakeRule("ACCEPT", "c"),
self.FakeRule("ACCEPT", "d"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == [
mock.call(self.FakeRule("ACCEPT", "c"), 0),
mock.call(self.FakeRule("ACCEPT", "d"), 1),
mock.call(self.FakeRule("LOG", "b"), 2),
mock.call(self.FakeRule("REJECT", "a"), 3),
]
def test_reorder_chain_log_first(self):
self.list_chain_mock.return_value = [
self.FakeRule("LOG", "b"),
self.FakeRule("ACCEPT", "c"),
self.FakeRule("ACCEPT", "d"),
self.FakeRule("REJECT", "a"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == [
mock.call(self.FakeRule("ACCEPT", "c"), 0),
mock.call(self.FakeRule("ACCEPT", "d"), 1),
mock.call(self.FakeRule("LOG", "b"), 2),
]
def test_reorder_chain_empty(self):
self.list_chain_mock.return_value = []
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == []
def test_reorder_chain_already_in_order(self):
self.chain_mock.return_value.rules = [
self.FakeRule("ACCEPT", "c"),
self.FakeRule("ACCEPT", "d"),
self.FakeRule("LOG", "b"),
self.FakeRule("REJECT", "a"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == []
def test_reorder_chain_log_at_bottom(self):
self.list_chain_mock.return_value = [
self.FakeRule("ACCEPT", "c"),
self.FakeRule("ACCEPT", "d"),
self.FakeRule("REJECT", "a"),
self.FakeRule("LOG", "b"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == [
mock.call(self.FakeRule("LOG", "b"), 2),
mock.call(self.FakeRule("REJECT", "a"), 3),
]
def test_reorder_chain_reject_in_middle(self):
self.list_chain_mock.return_value = [
self.FakeRule("ACCEPT", "c"),
self.FakeRule("REJECT", "a"),
self.FakeRule("ACCEPT", "d"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == [
mock.call(self.FakeRule("ACCEPT", "d"), 1),
mock.call(self.FakeRule("REJECT", "a"), 2),
]
def test_reorder_chain_other_target_names(self):
self.list_chain_mock.return_value = [
self.FakeRule("HELLOWORLD", "c"),
self.FakeRule("REJECT", "a"),
self.FakeRule("FOOBAR", "d"),
]
iptables.reorder_chain("")
assert self.chain_mock.return_value.replace_rule.mock_calls == [
mock.call(self.FakeRule("FOOBAR", "d"), 1),
mock.call(self.FakeRule("REJECT", "a"), 2),
]