Complete lab 7 security implementation
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""lab7: OTel collector mTLS. Без client cert → отказ."""
|
||||
from __future__ import annotations
|
||||
|
||||
import ssl
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
CERT = "infra/certs/otel-collector.crt"
|
||||
CA = "infra/certs/ca.crt"
|
||||
URL = "https://127.0.0.1:4318/v1/traces"
|
||||
|
||||
|
||||
def main():
|
||||
# 1. Без client cert → отказ (collector client_ca_file задан)
|
||||
ctx = ssl.create_default_context(cafile=CA)
|
||||
ctx.check_hostname = True
|
||||
ctx.verify_mode = ssl.CERT_REQUIRED
|
||||
req = urllib.request.Request(URL, data=b"{}", method="POST",
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
urllib.request.urlopen(req, context=ctx, timeout=5)
|
||||
print("FAIL: без client cert OTel принял (ожидали отказ)")
|
||||
sys.exit(1)
|
||||
except Exception as exc:
|
||||
print(f"PASS: без client cert OTel → {type(exc).__name__}")
|
||||
|
||||
# 2. С client cert otel-api → OK (200/4xx, но не TLS-отказ)
|
||||
ctx2 = ssl.create_default_context(cafile=CA)
|
||||
ctx2.check_hostname = True
|
||||
ctx2.verify_mode = ssl.CERT_REQUIRED
|
||||
ctx2.load_cert_chain("infra/certs/otel-api.crt", "infra/certs/otel-api.key")
|
||||
req2 = urllib.request.Request(URL, data=b"{}", method="POST",
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
r = urllib.request.urlopen(req2, context=ctx2, timeout=5)
|
||||
print(f"PASS: с otel-api client cert → {r.status}")
|
||||
except urllib.error.HTTPError as e:
|
||||
# 4xx — TLS прошёл, но payload невалидный — это ок
|
||||
print(f"PASS: с otel-api client cert → HTTP {e.code} (TLS ok)")
|
||||
except Exception as exc:
|
||||
print(f"FAIL: с otel-api client cert → {type(exc).__name__}: {exc}")
|
||||
sys.exit(1)
|
||||
|
||||
print("\nPASS: OTel mTLS (без cert → отказ; с cert → TLS ok)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user