Skip to content

Commit 1bb032c

Browse files
Add security tls test
Two tests to ensure the security of OpenSSL - TLSv1 & TLSv1_1 should be disabled - TLSv2 should be enabled Signed-off-by: Lucas RAVAGNIER <[email protected]>
1 parent 1b91380 commit 1bb032c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

jobs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"paths": [
2525
"tests/misc",
26+
"tests/security",
2627
"tests/migration",
2728
"tests/network",
2829
"tests/snapshot",

tests/security/test_tls.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
import ssl
3+
import socket
4+
import logging
5+
6+
# This test is designed to verify that TLS connections is secured
7+
#
8+
# Requirements:
9+
# - An XCP-ng host
10+
11+
@pytest.mark.parametrize("protocol_name", ["TLSv1", "TLSv1.1"])
12+
def test_tls_disabled(host: str, protocol_name: str):
13+
"""
14+
Verifies that specified TLS protocols are disabled on the XCP-ng host.
15+
Uses the ssl library directly. Should raise SSLError.
16+
"""
17+
PORT = 443
18+
19+
protocol = {
20+
"TLSv1": ssl.PROTOCOL_TLSv1,
21+
"TLSv1.1": ssl.PROTOCOL_TLSv1_1
22+
}[protocol_name]
23+
24+
logging.info(f"Testing if protocol {protocol_name} is disabled on host {host}")
25+
26+
with pytest.raises(ssl.SSLError):
27+
context = ssl.SSLContext(protocol)
28+
with socket.create_connection((str(host), PORT), timeout=10) as sock:
29+
with context.wrap_socket(sock, server_hostname=str(host)) as ssock:
30+
ssock.do_handshake()
31+
# If we reach this point, the protocol is enabled (test should fail)
32+
pytest.fail(f"Protocol {protocol} should be disabled but connection succeeded")
33+
34+
@pytest.mark.parametrize("protocol_name", ["TLSv1.2"])
35+
def test_enabled(host: str, protocol_name: str):
36+
"""
37+
Verifies that TLSv1.2 is enabled on the XCP-ng host.
38+
Uses the ssl library directly.
39+
"""
40+
PORT = 443
41+
42+
protocol = {
43+
"TLSv1.2": ssl.PROTOCOL_TLSv1_2
44+
}[protocol_name]
45+
46+
logging.info(f"Testing if protocol {protocol_name} is enabled on host {host}")
47+
48+
try:
49+
context = ssl.SSLContext(protocol)
50+
with socket.create_connection((str(host), PORT), timeout=10) as sock:
51+
with context.wrap_socket(sock, server_hostname=str(host)) as ssock:
52+
ssock.do_handshake()
53+
assert ssock.version()
54+
except ssl.SSLError as e:
55+
pytest.fail(f"{protocol_name} should be enabled, but got SSLError: {e}")

0 commit comments

Comments
 (0)