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
|
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from base_vyostest_shim import VyOSUnitTestSHIM
from vyos.configsession import ConfigSessionError
from vyos.ifconfig import Section
from vyos.utils.kernel import is_module_loaded
from vyos.utils.kernel import get_module_data
from vyos.utils.process import cmd
module_name = 'ipt_NETFLOW'
base_path = ['system', 'flow-accounting']
class TestSystemFlowAccounting(VyOSUnitTestSHIM.TestCase):
def _get_iptables_watched_interfaces(self, command, table, chain, column_name):
iptables_command = f'{command} -vn -t {table} -L {chain}'
data = cmd(iptables_command, message='Failed to get flows list')
data = data.splitlines()
self.assertGreaterEqual(
len(data), 2, "Unexpected output of {command}, should be at least two lines"
)
column_index = data[1].split().index(column_name)
interfaces = [
line.split()[column_index] for line in data[2:] if 'NETFLOW' in line
]
return interfaces
def _get_iptables_watched_ingress_interfaces(self, command):
return self._get_iptables_watched_interfaces(command, 'raw', 'PREROUTING', 'in')
def _get_iptables_watched_egress_interfaces(self, command):
return self._get_iptables_watched_interfaces(
command, 'mangle', 'POSTROUTING', 'out'
)
def _assert_ingress_interfaces(self, interfaces):
for command in 'iptables', 'ip6tables':
self.assertEqual(
set(self._get_iptables_watched_ingress_interfaces(command)),
set(interfaces),
command,
)
def _assert_egress_interfaces(self, interfaces):
for command in 'iptables', 'ip6tables':
self.assertEqual(
set(self._get_iptables_watched_egress_interfaces(command)),
set(interfaces),
command,
)
@classmethod
def setUpClass(cls):
super(TestSystemFlowAccounting, cls).setUpClass()
# ensure we can also run this test on a live system - so lets clean
# out the current configuration :)
cls.cli_delete(cls, base_path)
def tearDown(self):
# after service removal process must no longer run
self.assertTrue(is_module_loaded(module_name))
self.cli_delete(base_path)
self.cli_commit()
# after service removal process must no longer run
self.assertFalse(is_module_loaded(module_name))
self._assert_ingress_interfaces([])
self._assert_egress_interfaces([])
# always forward to base class
super().tearDown()
def test_basic(self):
engine_id = '33'
self.cli_set(base_path + ['netflow', 'engine-id', engine_id])
# You need to configure at least one interface for flow-accounting
with self.assertRaises(ConfigSessionError):
self.cli_commit()
for interface in Section.interfaces('ethernet'):
self.cli_set(base_path + ['netflow', 'interface', interface])
# You need to configure at least one NetFlow server
with self.assertRaises(ConfigSessionError):
self.cli_commit()
netflow_server = '11.22.33.44'
self.cli_set(base_path + ['netflow', 'server', netflow_server])
# commit changes, this time should work
self.cli_commit()
# verify configuration
self._assert_ingress_interfaces(Section.interfaces('ethernet'))
self._assert_egress_interfaces([])
module_data = get_module_data(module_name)
self.assertEqual(engine_id, module_data['parameters']['engine_id'])
def test_netflow(self):
engine_id = '33'
max_flows = '667'
dummy_if = 'dum3842'
agent_address = '192.0.2.10'
version = '10'
active_timeout = '900'
inactive_timeout = '30'
source_ipv4_address = '192.0.2.1'
source_ipv6_address = '2001:db8::ab'
netflow_server = {
'11.22.33.44': {},
'55.66.77.88': {'port': '6000'},
'100.12.14.1': {'source-interface': dummy_if},
'203.0.113.21': {'port': '3000', 'source-address': source_ipv4_address},
'2001:db8::1': {'source-address': source_ipv6_address},
}
# ipt_NETFLOW sorts destinations by IP
expected_destination = '11.22.33.44:2055,55.66.77.88:6000,100.12.14.1:2055%dum3842,203.0.113.21:3000@192.0.2.1,[2001:db8::1]:2055@2001:db8::ab'
self.cli_set(
['interfaces', 'dummy', dummy_if, 'address', agent_address + '/32']
)
self.cli_set(
['interfaces', 'dummy', dummy_if, 'address', source_ipv4_address + '/32']
)
self.cli_set(
['interfaces', 'dummy', dummy_if, 'address', source_ipv6_address + '/128']
)
for interface in Section.interfaces('ethernet'):
self.cli_set(base_path + ['netflow', 'interface', interface])
self.cli_set(base_path + ['netflow', 'engine-id', engine_id])
self.cli_set(base_path + ['netflow', 'max-flows', max_flows])
self.cli_set(base_path + ['netflow', 'version', version])
self.cli_set(base_path + ['netflow', 'active-timeout', active_timeout])
self.cli_set(base_path + ['netflow', 'inactive-timeout', inactive_timeout])
# You need to configure at least one netflow server
with self.assertRaises(ConfigSessionError):
self.cli_commit()
for server, server_config in netflow_server.items():
self.cli_set(base_path + ['netflow', 'server', server])
if 'port' in server_config:
self.cli_set(base_path + ['netflow', 'server', server, 'port', server_config['port']])
if 'source-address' in server_config:
self.cli_set(
base_path
+ [
'netflow',
'server',
server,
'source-address',
server_config['source-address'],
]
)
if 'source-interface' in server_config:
self.cli_set(
base_path
+ [
'netflow',
'server',
server,
'source-interface',
server_config['source-interface'],
]
)
# commit changes
self.cli_commit()
module_data = get_module_data(module_name)
self.assertEqual(engine_id, module_data['parameters']['engine_id'])
self.assertEqual(max_flows, module_data['parameters']['maxflows'])
self.assertEqual(expected_destination, module_data['parameters']['destination'])
self.assertEqual(version, module_data['parameters']['protocol'])
self.assertEqual(active_timeout, module_data['parameters']['active_timeout'])
self.assertEqual(
inactive_timeout, module_data['parameters']['inactive_timeout']
)
# Test module reload with new parameters
engine_id = '73'
self.cli_set(base_path + ['netflow', 'engine-id', engine_id])
self.cli_commit()
module_data = get_module_data(module_name)
self.assertEqual(engine_id, module_data['parameters']['engine_id'])
self.cli_delete(['interfaces', 'dummy', dummy_if])
def test_iptables(self):
netflow_server = '11.22.33.44'
self.cli_set(base_path + ['netflow', 'server', netflow_server])
dummy_ifs = [
'dum4000',
'dum4001',
'dum4002',
'dum4003',
]
self.cli_set(['interfaces', 'dummy', dummy_ifs[0], 'address', '192.0.2.100/32'])
self.cli_set(['interfaces', 'dummy', dummy_ifs[1], 'address', '192.0.2.101/32'])
self.cli_set(['interfaces', 'dummy', dummy_ifs[2], 'address', '192.0.2.102/32'])
self.cli_set(['interfaces', 'dummy', dummy_ifs[3], 'address', '192.0.2.103/32'])
# * three interfaces
for i in range(3):
self.cli_set(base_path + ['netflow', 'interface', dummy_ifs[i]])
self.cli_commit()
self._assert_ingress_interfaces(dummy_ifs[0:3])
self._assert_egress_interfaces([])
# * Then delete one
self.cli_delete(base_path + ['netflow', 'interface', dummy_ifs[1]])
self.cli_commit()
self._assert_ingress_interfaces([dummy_ifs[0], dummy_ifs[2]])
self._assert_egress_interfaces([])
# * Then add one
self.cli_set(base_path + ['netflow', 'interface', dummy_ifs[3]])
self.cli_commit()
self._assert_ingress_interfaces([dummy_ifs[0], dummy_ifs[2], dummy_ifs[3]])
self._assert_egress_interfaces([])
# * enable egress
self.cli_set(base_path + ['enable-egress'])
self.cli_commit()
self._assert_ingress_interfaces([dummy_ifs[0], dummy_ifs[2], dummy_ifs[3]])
self._assert_egress_interfaces([dummy_ifs[0], dummy_ifs[2], dummy_ifs[3]])
def test_sampler(self):
# Separate test because if --enable-sampler is not given to configure of
# ipt_NETFLOW this parameter is not available
sampling_rate = '100'
self.cli_set(base_path + ['netflow', 'sampling-rate', sampling_rate])
for interface in Section.interfaces('ethernet'):
self.cli_set(base_path + ['netflow', 'interface', interface])
netflow_server = '11.22.33.44'
self.cli_set(base_path + ['netflow', 'server', netflow_server])
# commit changes, this time should work
self.cli_commit()
module_data = get_module_data(module_name)
if 'sampler' not in module_data['parameters']:
self.skipTest("ipt_NETFLOW has no sampler parameter")
self.assertEqual(
f'random:{sampling_rate}', module_data['parameters']['sampler']
)
def test_netflow_v5(self):
version = '5'
netflow_server = '203.0.113.27'
for interface in Section.interfaces('ethernet'):
self.cli_set(base_path + ['netflow', 'interface', interface])
self.cli_set(base_path + ['netflow', 'version', version])
self.cli_set(base_path + ['netflow', 'server', netflow_server])
# commit changes
self.cli_commit()
if __name__ == '__main__':
unittest.main(verbosity=2, failfast=VyOSUnitTestSHIM.TestCase.debug_on())
|