-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprepare.py
388 lines (343 loc) · 12.1 KB
/
prepare.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import glob
import os
import time
import shutil
import re
import nmci
def setup_libreswan(context, mode, dh_group, phase1_al="aes", phase2_al=None):
"""
Setup Libreswan for a given mode and DH group.
:param context: behave context
:type context: behave.runner.Context
:param mode: mode of the connection
:type mode: str
:param dh_group: DH group
:type dh_group: str
:param phase1_al: phase1 algorithm, defaults to "aes"
:type phase1_al: str, optional
:param phase2_al: phase2 algorithm, defaults to None
:type phase2_al: str, optional
"""
RC = nmci.process.run_code(
f"MODE={mode} bash prepare/libreswan.sh",
shell=True,
ignore_stderr=True,
timeout=60,
)
if RC != 0:
teardown_libreswan(context)
assert False, "Libreswan setup failed"
def teardown_libreswan(context):
"""
Teardown Libreswan.
:param context: behave context
:type context: behave.runner.Context
"""
nmci.process.run_stdout("bash prepare/libreswan.sh teardown", ignore_stderr=True)
print("Attach Libreswan logs")
journal_log = nmci.misc.journal_show(
syslog_identifier="pluto",
cursor=context.log_cursor,
journal_args="-o cat",
)
nmci.embed.embed_data("Libreswan Pluto Journal", journal_log)
conf = nmci.util.file_get_content_simple("/var/ipsec/connection.conf")
nmci.embed.embed_data("Libreswan Config", conf)
def setup_openvpn(context, tags):
"""
Setup OpenVPN server and client for a given mode and DH group.
:param context: behave context
:type context: behave.runner.Context
:param tags: list of tags
:type tags: list
:return: OpenVPN server process
:rtype: pexpect.spawn
"""
path = "%s/contrib/openvpn" % os.getcwd()
samples = glob.glob(os.path.abspath(path))[0]
nmci.process.run_stdout(f"chcon -R system_u:object_r:usr_t:s0 {samples}")
conf = [
"# OpenVPN configuration for client testing",
"mode server",
"tls-server",
"port 1194",
"proto udp",
"dev tun",
"persist-key",
"persist-tun",
f"ca {samples}/sample-keys/ca.crt",
f"cert {samples}/sample-keys/server.crt",
f"key {samples}/sample-keys/server.key",
f"dh {samples}/sample-keys/dh2048.pem",
]
if "openvpn6" not in tags:
conf += [
"server 172.31.70.0 255.255.255.0",
"topology net30",
'push "dhcp-option DNS 172.31.70.53"',
'push "dhcp-option DOMAIN vpn.domain"',
]
if "openvpn4" not in tags:
conf += [
"tun-ipv6",
"push tun-ipv6",
"ifconfig-ipv6 2001:db8:666:dead::1/64 2001:db8:666:dead::1",
'push "ifconfig-ipv6 2001:db8:666:dead::2/64 2001:db8:666:dead::1"',
# Not working for newer Fedoras (rhbz1909741)
# 'ifconfig-ipv6-pool 2001:db8:666:dead::/64',
'push "route-ipv6 2001:db8:666:dead::/64 2001:db8:666:dead::1"',
]
if "oath" in tags:
context.ovpn_key = nmci.process.run_stdout(
"head -10 /dev/urandom | sha256sum | cut -b 1-30", shell=True
).strip("\n")
conf += [
"management 127.0.0.1 7505",
"management-client-auth",
"verb 5",
"script-security 2",
f"auth-user-pass-verify {samples}/oath.sh via-file",
"verify-client-cert none",
]
nmci.util.file_set_content("/etc/openvpn/trest-server.conf", conf)
time.sleep(1)
ovpn_proc = context.pexpect_service(
"openvpn --writepid /tmp/openvpn.pid --config /etc/openvpn/trest-server.conf"
)
res = ovpn_proc.expect(
["Initialization Sequence Completed", nmci.pexpect.TIMEOUT, nmci.pexpect.EOF],
timeout=20,
)
assert res == 0, "OpenVPN Server did not come up in 20 seconds"
if "oath" in tags:
context.ovpn_mgmt = context.pexpect_service("telnet 127.0.0.1 7505")
# enable logging of commands in management console
context.ovpn_mgmt.send("log on all\n")
return ovpn_proc
def setup_strongswan(context):
"""
Setup Strongswan.
:param context: behave context
:type context: behave.runner.Context
"""
RC = nmci.process.run_code(
"sh prepare/strongswan.sh", ignore_stderr=True, timeout=60
)
if RC != 0:
teardown_strongswan(context)
assert False, "Strongswan setup failed"
def teardown_strongswan(context):
"""
Teardown Strongswan
:param context: behave context
:type context: behave.runner.Context
"""
nmci.process.run_stdout("sh prepare/strongswan.sh teardown", ignore_stderr=True)
def setup_racoon(context, mode, dh_group, phase1_al="aes", phase2_al=None):
"""
Setup Racoon for a given mode and DH group.
:param context: behave context
:type context: behave.runner.Context
:param mode: mode of the connection
:type mode: str
:param dh_group: DH group
:type dh_group: str
:param phase1_al: phase1 algorithm, defaults to "aes"
:type phase1_al: str, optional
:param phase2_al: phase2 algorithm, defaults to None
:type phase2_al: str, optional
"""
nmci.veth.wait_for_testeth0()
if context.arch == "s390x":
nmci.process.run_stdout(
f"[ -x /usr/sbin/racoon ] || yum -y install https://vbenes.fedorapeople.org/NM/ipsec-tools-0.8.2-1.el7.{context.arch}.rpm",
shell=True,
timeout=120,
ignore_stderr=True,
)
else:
# Install under RHEL7 only
if "Maipo" in context.rh_release:
nmci.process.run_stdout(
"[ -f /etc/yum.repos.d/epel.repo ] || rpm -i http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm",
shell=True,
timeout=120,
ignore_stderr=True,
)
nmci.process.run_stdout(
"[ -x /usr/sbin/racoon ] || yum -y install ipsec-tools",
shell=True,
timeout=120,
ignore_stderr=True,
)
RC = nmci.process.run_code(
f"sh prepare/racoon.sh {mode} {dh_group} {phase1_al}",
timeout=60,
ignore_stderr=True,
)
if RC != 0:
teardown_racoon(context)
assert False, "Racoon setup failed"
def teardown_racoon(context):
"""
Teardown Racoon.
:param context: behave context
:type context: behave.runner.Context
"""
nmci.process.run_stdout("sh prepare/racoon.sh teardown")
def setup_hostapd(context):
"""
Setup hostapd.
:param context: behave context
:type context: behave.runner.Context
"""
nmci.veth.wait_for_testeth0()
if context.arch != "s390x":
# Install under RHEL7 only
if "Maipo" in context.rh_release:
nmci.process.run_stdout(
"[ -f /etc/yum.repos.d/epel.repo ] || rpm -i http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm",
shell=True,
timeout=120,
ignore_stderr=True,
)
nmci.process.run_stdout(
"[ -x /usr/sbin/hostapd ] || (yum -y install hostapd; sleep 10)",
shell=True,
timeout=120,
ignore_stderr=True,
)
if (
nmci.process.run_code(
"sh prepare/hostapd_wired.sh contrib/8021x/certs",
timeout=60,
ignore_stderr=True,
)
!= 0
):
nmci.process.run_stdout(
"sh prepare/hostapd_wired.sh teardown", ignore_stderr=True
)
assert False, "hostapd setup failed"
def teardown_hostapd(context):
"""
Teardown hostapd.
:param context: behave context
:type context: behave.runner.Context
"""
nmci.process.run_stdout("sh prepare/hostapd_wired.sh teardown", ignore_stderr=True)
nmci.veth.wait_for_testeth0()
def setup_pkcs11(context):
"""
Setup SoftHSM2 token, key and cert for 802.1x testing.
Don't touch token, key or cert if they're already present in order
to avoid SoftHSM errors. No teardown for this reason, too.
:param context: behave context
:type context: behave.runner.Context
"""
install_packages = []
if not shutil.which("softhsm2-util"):
install_packages.append("softhsm")
if not shutil.which("pkcs11-tool"):
install_packages.append("opensc")
if len(install_packages) > 0:
nmci.process.run_stdout(
f"yum -y install {' '.join(install_packages)}",
timeout=120,
ignore_stderr=True,
)
re_token = re.compile(r"(?m)Label:[\s]*nmci[\s]*$")
re_nmclient = re.compile(r"(?m)label:[\s]*nmclient$")
nmci.util.file_set_content(
"/tmp/pkcs11_passwd-file",
["802-1x.identity:test", "802-1x.private-key-password:1234"],
)
if not nmci.process.run_search_stdout(
"softhsm2-util --show-slots", re_token, pattern_flags=0
):
nmci.process.run_stdout(
"softhsm2-util --init-token --free --pin 1234 --so-pin 123456 --label 'nmci'"
)
if not nmci.process.run_search_stdout(
"pkcs11-tool --module /usr/lib64/pkcs11/libsofthsm2.so -l -p 1234 --token-label nmci -y privkey -O",
re_nmclient,
pattern_flags=0,
):
nmci.process.run_stdout(
"pkcs11-tool --module /usr/lib64/pkcs11/libsofthsm2.so -l -p 1234 --token-label nmci --label nmclient -y privkey --write-object contrib/8021x/certs/client/test_user.key.pem"
)
if not nmci.process.run_search_stdout(
"pkcs11-tool --module /usr/lib64/pkcs11/libsofthsm2.so -l -p 1234 --token-label nmci -y cert -O",
re_nmclient,
pattern_flags=0,
):
nmci.process.run_stdout(
"pkcs11-tool --module /usr/lib64/pkcs11/libsofthsm2.so -l -p 1234 --token-label nmci --label nmclient -y cert --write-object contrib/8021x/certs/client/test_user.cert.der"
)
def wifi_rescan(context):
"""
Rescan for wireless networks.
:param context: behave context
:type context: behave.runner.Context
"""
if "wpa2-psk" in nmci.process.nmcli_force("dev wifi list").stdout:
return
print("Commencing wireless network rescan")
timeout_len = 180
timeout = nmci.util.start_timeout(timeout_len)
while timeout.loop_sleep(5):
if (
"wpa2-psk"
not in nmci.process.nmcli_force("dev wifi list --rescan yes").stdout
):
print("* still not seeing wpa2-psk")
else:
return
assert False, f"Not seeing wpa2-psk in {timeout_len} seconds"
def setup_hostapd_wireless(context, args=None):
"""
Setup hostapd for wireless testing.
:param context: behave context
:type context: behave.runner.Context
:param args: additional arguments for hostapd_wireless.sh, defaults to None
:type args: list, optional
"""
nmci.veth.wait_for_testeth0()
if context.arch != "s390x":
# Install under RHEL7 only
if "Maipo" in context.rh_release:
nmci.process.run_stdout(
"[ -f /etc/yum.repos.d/epel.repo ] || rpm -i http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm",
shell=True,
timeout=120,
ignore_stderr=True,
)
nmci.process.run_stdout(
"[ -x /usr/sbin/hostapd ] || (yum -y install hostapd; sleep 10)",
shell=True,
timeout=120,
ignore_stderr=True,
)
argv = ["sh", "prepare/hostapd_wireless.sh", "contrib/8021x/certs"]
if args is not None:
argv.extend(args)
nmci.process.run_stdout(
argv,
ignore_stderr=True,
timeout=180,
)
# "check" file is touched once first check is passed
# so first setup calls rescan, later setups calls touch "check" file
wifi_rescan(context)
def teardown_hostapd_wireless(context):
"""
Teardown hostapd for wireless testing.
:param context: behave context
:type context: behave.runner.Context
"""
nmci.process.run_stdout(
"sh prepare/hostapd_wireless.sh teardown",
ignore_stderr=True,
timeout=15,
)
context.NM_pid = nmci.nmutil.nm_pid()