Files
mtusi2026lab7-fork/tests/security.py
T

31 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Public lab7 external TLS check. Internal mTLS/policies require student tests."""
import ssl
import urllib.error
import urllib.request
from client import Client, LAB, NoRedirect, arguments, check, run
def main():
args = arguments('Lab7 external TLS trust check; provide --ca-file for the positive probe')
check(LAB==7,'this check belongs to lab7')
c = Client(args.base_url,args.ca_file)
c.call('GET','/health/ready',schema='Health')
# An empty trust store deliberately trusts no CA, including public CAs.
untrusted = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
untrusted.verify_mode = ssl.CERT_REQUIRED
untrusted.check_hostname = True
opener = urllib.request.build_opener(NoRedirect(),urllib.request.HTTPSHandler(context=untrusted))
rejected = False
try:
with opener.open(c.base+'/health/ready',timeout=5) as response:
response.read(1024)
except urllib.error.URLError as exc:
check(isinstance(exc.reason,ssl.SSLCertVerificationError),'negative probe failed for a non-certificate reason')
rejected = True
check(rejected,'server unexpectedly passed validation with an empty trust store')
print('PASS: trusted HTTPS works; untrusted CA rejected. This does NOT verify internal mTLS, OpenBao or hardening.')
if __name__=='__main__':
run(main)