Skip to content

Commit 33bf002

Browse files
authored
Merge pull request ghostunnel#689 from ghostunnel/cs/fix-codeql-python
More code quality fixes for integration tests
2 parents 8c438dc + 9f58c0b commit 33bf002

79 files changed

Lines changed: 3052 additions & 3097 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def status_info():
155155
return json.loads(e.read().decode())
156156
except Exception as e:
157157
print('unable to fetch status:', e)
158+
return None
158159

159160
def wait_for_status(predicate, timeout=30):
160161
"""Poll status_info() until predicate(info) is truthy, with timeout."""
@@ -276,8 +277,8 @@ def assert_connection_rejected(client, server, name, timeout_ok=True):
276277
def create_default_certs(algorithm='ecdsa'):
277278
"""Create standard root, server, and client certificates.
278279
279-
Returns the RootCert object. Callers must keep a reference to it
280-
alive for the duration of the test to prevent __del__ cleanup."""
280+
Returns the RootCert object. Callers should call root.cleanup()
281+
in their finally block to clean up temporary cert files."""
281282
root = RootCert('root', algorithm=algorithm)
282283
root.create_signed_cert('server')
283284
root.create_signed_cert('client')

tests/test-client-auto-reload-certificate.py

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,64 @@
1313
import os
1414
from common import LOCALHOST, RootCert, STATUS_PORT, SocketPair, TcpClient, TlsClient, TlsServer, print_ok, run_ghostunnel, terminate, LISTEN_PORT, TARGET_PORT
1515

16-
if __name__ == "__main__":
17-
ghostunnel = None
18-
try:
19-
# create certs
20-
root1 = RootCert('root1')
21-
root1.create_signed_cert('server1')
22-
root1.create_signed_cert('client1')
23-
root1.create_signed_cert('new_client1')
16+
ghostunnel = None
17+
try:
18+
# create certs
19+
root1 = RootCert('root1')
20+
root1.create_signed_cert('server1')
21+
root1.create_signed_cert('client1')
22+
root1.create_signed_cert('new_client1')
2423

25-
root2 = RootCert('new_root')
26-
root2.create_signed_cert('server2')
24+
root2 = RootCert('new_root')
25+
root2.create_signed_cert('server2')
2726

28-
# start ghostunnel
29-
ghostunnel = run_ghostunnel(['client',
30-
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
31-
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
32-
'--keystore=client1.p12',
33-
'--timed-reload=1s',
34-
'--cacert=root1.crt',
35-
'--status={0}:{1}'.format(LOCALHOST,
36-
STATUS_PORT)])
27+
# start ghostunnel
28+
ghostunnel = run_ghostunnel(['client',
29+
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
30+
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
31+
'--keystore=client1.p12',
32+
'--timed-reload=1s',
33+
'--cacert=root1.crt',
34+
'--status={0}:{1}'.format(LOCALHOST,
35+
STATUS_PORT)])
3736

38-
# ensure ghostunnel connects with server1
39-
pair1 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
40-
'server1', 'root1', TARGET_PORT))
41-
pair1.validate_can_send_from_client("toto", "pair1 works")
42-
pair1.validate_client_cert("client1", "pair1: ou=client1 -> ...")
37+
# ensure ghostunnel connects with server1
38+
pair1 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
39+
'server1', 'root1', TARGET_PORT))
40+
pair1.validate_can_send_from_client("toto", "pair1 works")
41+
pair1.validate_client_cert("client1", "pair1: ou=client1 -> ...")
4342

44-
# check certificate on status port
45-
TlsClient(None, 'root1', STATUS_PORT).connect(20, 'client1')
46-
print_ok("got client1 on /_status")
43+
# check certificate on status port
44+
TlsClient(None, 'root1', STATUS_PORT).connect(20, 'client1')
45+
print_ok("got client1 on /_status")
4746

48-
# replace keystore and check ghostunnel connects with new_client1
49-
print_ok("replacing certificates")
50-
os.rename('new_client1.p12', 'client1.p12')
51-
# reload should happen automatically
52-
TlsClient(None, 'root1', STATUS_PORT).connect(20, 'new_client1')
53-
print_ok("reload done")
47+
# replace keystore and check ghostunnel connects with new_client1
48+
print_ok("replacing certificates")
49+
os.rename('new_client1.p12', 'client1.p12')
50+
# reload should happen automatically
51+
TlsClient(None, 'root1', STATUS_PORT).connect(20, 'new_client1')
52+
print_ok("reload done")
5453

55-
pair2 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
56-
'server1', 'root1', TARGET_PORT))
57-
pair2.validate_can_send_from_client("toto", "pair2 works")
58-
pair2.validate_client_cert(
59-
"new_client1", "pair2: ou=new_client1 -> ...")
60-
pair2.cleanup()
54+
pair2 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
55+
'server1', 'root1', TARGET_PORT))
56+
pair2.validate_can_send_from_client("toto", "pair2 works")
57+
pair2.validate_client_cert(
58+
"new_client1", "pair2: ou=new_client1 -> ...")
59+
pair2.cleanup()
6160

62-
# ensure ghostunnel won't connect to server2
63-
try:
64-
pair3 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
65-
'server2', 'root1', TARGET_PORT))
66-
pair3.validate_can_send_from_client("toto", "pair3 works")
67-
raise Exception("pair3 worked")
68-
except ssl.SSLError:
69-
print_ok("ghostunnel did not connect to incorrect CA")
61+
# ensure ghostunnel won't connect to server2
62+
try:
63+
pair3 = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
64+
'server2', 'root1', TARGET_PORT))
65+
pair3.validate_can_send_from_client("toto", "pair3 works")
66+
raise Exception("pair3 worked")
67+
except ssl.SSLError:
68+
print_ok("ghostunnel did not connect to incorrect CA")
7069

71-
# ensure that pair1 is still alive
72-
pair1.validate_can_send_from_client("toto", "pair1 still works")
73-
pair1.cleanup()
74-
print_ok("OK")
70+
# ensure that pair1 is still alive
71+
pair1.validate_can_send_from_client("toto", "pair1 still works")
72+
pair1.cleanup()
73+
print_ok("OK")
7574

76-
finally:
77-
terminate(ghostunnel)
75+
finally:
76+
terminate(ghostunnel)

tests/test-client-concurrent-connections.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,33 @@ def send_data(i):
3434
"{0} server close -> client close".format(i))
3535

3636

37-
if __name__ == "__main__":
38-
ghostunnel = None
39-
n_clients = 10
40-
try:
41-
# create certs
42-
root = RootCert('root')
43-
root.create_signed_cert('client')
44-
for n in range(1, n_clients):
45-
root.create_signed_cert("server{0}".format(n))
37+
ghostunnel = None
38+
n_clients = 10
39+
try:
40+
# create certs
41+
root = RootCert('root')
42+
root.create_signed_cert('client')
43+
for n in range(1, n_clients):
44+
root.create_signed_cert("server{0}".format(n))
4645

47-
# start ghostunnel
48-
ghostunnel = run_ghostunnel(['client',
49-
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
50-
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
51-
'--keystore=client.p12',
52-
'--status={0}:{1}'.format(LOCALHOST,
53-
STATUS_PORT),
54-
'--cacert=root.crt'])
46+
# start ghostunnel
47+
ghostunnel = run_ghostunnel(['client',
48+
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
49+
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
50+
'--keystore=client.p12',
51+
'--status={0}:{1}'.format(LOCALHOST,
52+
STATUS_PORT),
53+
'--cacert=root.crt'])
5554

56-
# servers should be able to communicate all at the same time.
57-
procs = []
58-
for n in range(1, n_clients):
59-
proc = Process(target=send_data, args=(n,))
60-
proc.start()
61-
procs.append(proc)
62-
for proc in procs:
63-
proc.join()
55+
# servers should be able to communicate all at the same time.
56+
procs = []
57+
for n in range(1, n_clients):
58+
proc = Process(target=send_data, args=(n,))
59+
proc.start()
60+
procs.append(proc)
61+
for proc in procs:
62+
proc.join()
6463

65-
print_ok("OK")
66-
finally:
67-
terminate(ghostunnel)
64+
print_ok("OK")
65+
finally:
66+
terminate(ghostunnel)

tests/test-client-disable-authentication.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,46 @@
88
from common import LOCALHOST, RootCert, STATUS_PORT, SocketPair, TcpClient, TlsServer, print_ok, run_ghostunnel, terminate, LISTEN_PORT, TARGET_PORT, assert_connection_rejected
99
import ssl
1010

11-
if __name__ == "__main__":
12-
ghostunnel = None
13-
try:
14-
# create certs
15-
root = RootCert('root')
16-
root.create_signed_cert('server1')
17-
root.create_signed_cert(
18-
'server2', san="IP:127.0.0.1,IP:::1,DNS:foobar")
19-
20-
other_root = RootCert('other_root')
21-
other_root.create_signed_cert('other_server')
22-
23-
# start ghostunnel
24-
ghostunnel = run_ghostunnel(['client',
25-
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
26-
'--target=localhost:{0}'.format(TARGET_PORT),
27-
'--cacert=root.crt',
28-
'--disable-authentication',
29-
'--status={0}:{1}'.format(LOCALHOST,
30-
STATUS_PORT)])
31-
32-
# connect to server1, confirm that the tunnel is up
33-
pair = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
34-
'server1', 'root', TARGET_PORT, cert_reqs=ssl.CERT_NONE))
35-
pair.validate_can_send_from_client(
36-
"hello world", "1: client -> server")
37-
pair.validate_can_send_from_server(
38-
"hello world", "1: server -> client")
39-
pair.validate_closing_client_closes_server(
40-
"1: client closed -> server closed")
41-
42-
# connect to other_server, confirm that the tunnel isn't up
43-
assert_connection_rejected(
44-
TcpClient(LISTEN_PORT), TlsServer('other_server', 'other_root', TARGET_PORT, cert_reqs=ssl.CERT_NONE),
45-
"other_server with unknown CA", timeout_ok=False)
46-
47-
# connect to server2, confirm that the tunnel isn't up
48-
assert_connection_rejected(
49-
TcpClient(LISTEN_PORT), TlsServer('server2', 'root', TARGET_PORT, cert_reqs=ssl.CERT_NONE),
50-
"server2 with incorrect CN", timeout_ok=False)
51-
52-
print_ok("OK")
53-
finally:
54-
terminate(ghostunnel)
11+
ghostunnel = None
12+
try:
13+
# create certs
14+
root = RootCert('root')
15+
root.create_signed_cert('server1')
16+
root.create_signed_cert(
17+
'server2', san="IP:127.0.0.1,IP:::1,DNS:foobar")
18+
19+
other_root = RootCert('other_root')
20+
other_root.create_signed_cert('other_server')
21+
22+
# start ghostunnel
23+
ghostunnel = run_ghostunnel(['client',
24+
'--listen={0}:{1}'.format(LOCALHOST, LISTEN_PORT),
25+
'--target=localhost:{0}'.format(TARGET_PORT),
26+
'--cacert=root.crt',
27+
'--disable-authentication',
28+
'--status={0}:{1}'.format(LOCALHOST,
29+
STATUS_PORT)])
30+
31+
# connect to server1, confirm that the tunnel is up
32+
pair = SocketPair(TcpClient(LISTEN_PORT), TlsServer(
33+
'server1', 'root', TARGET_PORT, cert_reqs=ssl.CERT_NONE))
34+
pair.validate_can_send_from_client(
35+
"hello world", "1: client -> server")
36+
pair.validate_can_send_from_server(
37+
"hello world", "1: server -> client")
38+
pair.validate_closing_client_closes_server(
39+
"1: client closed -> server closed")
40+
41+
# connect to other_server, confirm that the tunnel isn't up
42+
assert_connection_rejected(
43+
TcpClient(LISTEN_PORT), TlsServer('other_server', 'other_root', TARGET_PORT, cert_reqs=ssl.CERT_NONE),
44+
"other_server with unknown CA", timeout_ok=False)
45+
46+
# connect to server2, confirm that the tunnel isn't up
47+
assert_connection_rejected(
48+
TcpClient(LISTEN_PORT), TlsServer('server2', 'root', TARGET_PORT, cert_reqs=ssl.CERT_NONE),
49+
"server2 with incorrect CN", timeout_ok=False)
50+
51+
print_ok("OK")
52+
finally:
53+
terminate(ghostunnel)

tests/test-client-handles-client-closes-connection-unix.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,42 @@
66

77
from common import LOCALHOST, RootCert, STATUS_PORT, SocketPair, UnixClient, TlsServer, print_ok, run_ghostunnel, terminate, TARGET_PORT
88

9-
if __name__ == "__main__":
10-
ghostunnel = None
11-
try:
12-
# create certs
13-
root = RootCert('root')
14-
root.create_signed_cert('server')
15-
root.create_signed_cert('client')
9+
ghostunnel = None
10+
try:
11+
# create certs
12+
root = RootCert('root')
13+
root.create_signed_cert('server')
14+
root.create_signed_cert('client')
1615

17-
# start ghostunnel
18-
client = UnixClient()
19-
ghostunnel = run_ghostunnel(['client',
20-
'--listen=unix:{0}'.format(client.get_socket_path()),
21-
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
22-
'--cert=client.crt',
23-
'--key=client.key',
24-
'--status={0}:{1}'.format(LOCALHOST,
25-
STATUS_PORT),
26-
'--close-timeout=10s',
27-
'--cacert=root.crt'])
16+
# start ghostunnel
17+
client = UnixClient()
18+
ghostunnel = run_ghostunnel(['client',
19+
'--listen=unix:{0}'.format(client.get_socket_path()),
20+
'--target={0}:{1}'.format(LOCALHOST, TARGET_PORT),
21+
'--cert=client.crt',
22+
'--key=client.key',
23+
'--status={0}:{1}'.format(LOCALHOST,
24+
STATUS_PORT),
25+
'--close-timeout=10s',
26+
'--cacert=root.crt'])
2827

29-
# connect to server, confirm that the tunnel is up
30-
pair = SocketPair(client, TlsServer('server', 'root', TARGET_PORT))
31-
pair.validate_can_send_from_client(
32-
"hello world", "1: client -> server")
33-
pair.validate_can_send_from_server(
34-
"hello world", "1: server -> client")
35-
pair.validate_closing_client_closes_server(
36-
"1: client closed -> server closed")
28+
# connect to server, confirm that the tunnel is up
29+
pair = SocketPair(client, TlsServer('server', 'root', TARGET_PORT))
30+
pair.validate_can_send_from_client(
31+
"hello world", "1: client -> server")
32+
pair.validate_can_send_from_server(
33+
"hello world", "1: server -> client")
34+
pair.validate_closing_client_closes_server(
35+
"1: client closed -> server closed")
3736

38-
pair = SocketPair(client, TlsServer('server', 'root', TARGET_PORT))
39-
pair.validate_can_send_from_client(
40-
"hello world", "2: client -> server")
41-
pair.validate_can_send_from_server(
42-
"hello world", "2: server -> client")
43-
pair.validate_half_closing_client_closes_server(
44-
"2: client closed -> server closed")
37+
pair = SocketPair(client, TlsServer('server', 'root', TARGET_PORT))
38+
pair.validate_can_send_from_client(
39+
"hello world", "2: client -> server")
40+
pair.validate_can_send_from_server(
41+
"hello world", "2: server -> client")
42+
pair.validate_half_closing_client_closes_server(
43+
"2: client closed -> server closed")
4544

46-
print_ok("OK")
47-
finally:
48-
terminate(ghostunnel)
45+
print_ok("OK")
46+
finally:
47+
terminate(ghostunnel)

0 commit comments

Comments
 (0)