Files
mtusi2026lab7/student-tests/security.py
T

19 lines
879 B
Python

"""Small security acceptance check for the local TLS boundary."""
import ssl
import urllib.request
import urllib.error
from pathlib import Path
root = Path(__file__).resolve().parents[1]
context = ssl.create_default_context(cafile=root / 'certs' / 'ca.crt')
with urllib.request.urlopen('https://127.0.0.1:8443/health/live', context=context, timeout=5) as response:
assert response.status == 200
try:
with urllib.request.urlopen('https://127.0.0.1:8443/health/live', context=ssl.create_default_context(), timeout=5):
raise AssertionError('untrusted certificate was accepted')
except (ssl.SSLCertVerificationError, urllib.error.URLError) as error:
if isinstance(error, urllib.error.URLError) and not isinstance(error.reason, ssl.SSLCertVerificationError):
raise
pass
print('PASS: trusted local TLS works and an untrusted certificate is rejected.')