Files
mtusi2026lab7/student-tests/dependency_tls.py
T
2026-11-07 15:00:53 +03:00

41 lines
1.7 KiB
Python

"""Probe trusted and untrusted TLS plus inaccessible plaintext dependency ports."""
import os
import socket
import ssl
import urllib.error
import urllib.request
ca = os.environ['BAO_CACERT']
for host, port in [('s3', 8333), ('broker', 5671), ('openbao', 8200)]:
trusted = ssl.create_default_context(cafile=ca)
with socket.create_connection((host, port), timeout=5) as raw:
with trusted.wrap_socket(raw, server_hostname=host) as connection:
assert connection.version() in {'TLSv1.2', 'TLSv1.3'}
untrusted = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
try:
with socket.create_connection((host, port), timeout=5) as raw:
with untrusted.wrap_socket(raw, server_hostname=host):
pass
except ssl.SSLCertVerificationError:
pass
else:
raise AssertionError(f'{host}: untrusted CA accepted')
print(f'PASS {host}: verified TLS; untrusted CA rejected')
for host, port in [('s3', 8888), ('s3', 9333), ('s3', 18333), ('broker', 5672), ('broker', 4369), ('broker', 25672), ('broker', 15692), ('loki', 9095), ('tempo', 9095)]:
try:
socket.create_connection((host, port), timeout=3).close()
except ConnectionRefusedError:
pass
else:
raise AssertionError(f'{host}:{port}: unexpected accessible plaintext port')
print('PASS: S3 internal and broker plaintext ports are closed')
request = urllib.request.Request('https://s3:8333/', data=b'', method='POST')
try:
urllib.request.urlopen(request, context=ssl.create_default_context(cafile=ca), timeout=5)
except urllib.error.HTTPError as error:
assert error.code == 403
else:
raise AssertionError('S3 proxy allowed the blocked POST method')
print('PASS: S3 proxy blocks POST')