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

31 lines
1.2 KiB
Python

"""Verify the Collector accepts its publisher role and rejects other callers."""
import os
import socket
import ssl
def request(certificate):
ctx = ssl.create_default_context(cafile=os.environ['OTEL_EXPORTER_OTLP_CERTIFICATE'])
if certificate:
ctx.load_cert_chain(certificate)
with socket.create_connection(('otel-collector', 4318), timeout=5) as raw:
with ctx.wrap_socket(raw, server_hostname='otel-collector') as conn:
conn.sendall(b'GET / HTTP/1.1\r\nHost: otel-collector\r\nConnection: close\r\n\r\n')
return conn.recv(1024)
assert b'404' in request(os.environ['OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE'])
for cert in (None, os.environ['BAO_CLIENT_CERT']):
try:
result = request(cert)
except (ssl.SSLError, ConnectionResetError, BrokenPipeError):
continue
assert not result, 'Collector accepted an unauthorized caller'
try:
socket.create_connection(('otel-collector', 14318), timeout=2).close()
except ConnectionRefusedError:
pass
else:
raise AssertionError('Collector plaintext listener reachable from outside')
print('PASS: Collector publisher accepted; no cert and wrong role rejected; local listener closed')