forked from mtusi2026/mtusi2026lab7
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Verify API listener client-certificate requirements from inside an API container."""
|
|
import ssl
|
|
import http.client
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
|
|
HOST = "https://127.0.0.1:8080/health/ready"
|
|
CA = "/run/secrets/ca_crt"
|
|
|
|
|
|
def context(client_cert=None, client_key=None):
|
|
result = ssl.create_default_context(cafile=CA)
|
|
if client_cert:
|
|
result.load_cert_chain(client_cert, client_key)
|
|
return result
|
|
|
|
|
|
def rejected(client_cert=None, client_key=None):
|
|
try:
|
|
with urllib.request.urlopen(HOST, context=context(client_cert, client_key), timeout=5) as response:
|
|
response.read(1024)
|
|
except (urllib.error.URLError, ssl.SSLError, http.client.HTTPException, OSError):
|
|
return
|
|
raise AssertionError("API accepted a request with an unauthorized client identity")
|
|
|
|
|
|
def main():
|
|
rejected()
|
|
rejected("/run/secrets/wrong_client_cert", "/run/secrets/wrong_client_key")
|
|
rejected("/run/secrets/foreign_client_cert", "/run/secrets/foreign_client_key")
|
|
|
|
with urllib.request.urlopen(
|
|
HOST,
|
|
context=context("/run/secrets/lb_tls_cert", "/run/secrets/lb_tls_key"),
|
|
timeout=5,
|
|
) as response:
|
|
if response.status != 200:
|
|
raise AssertionError(f"authorized LB identity returned HTTP {response.status}")
|
|
print("PASS: missing, foreign-CA and non-LB identities rejected; LB identity accepted.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|