forked from mtusi2026/mtusi2026lab7
Лабораторная 7
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""Operator TLS/policy audit inside an ephemeral container; outputs no credentials."""
|
||||
import http.client,json,socket,ssl
|
||||
from pathlib import Path
|
||||
BASE=Path('/audit');results={}
|
||||
def context(role):
|
||||
c=ssl.create_default_context(cafile=str(BASE/'public/ca.crt'))
|
||||
if role:c.load_cert_chain(BASE/'tls'/role/'cert.crt',BASE/'tls'/role/'key.key')
|
||||
return c
|
||||
def probe(host,port,role,path='/',payload=None):
|
||||
c=context(role)
|
||||
with socket.create_connection((host,port),timeout=3) as raw:
|
||||
with c.wrap_socket(raw,server_hostname=host) as s:
|
||||
s.sendall(payload or f'GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n'.encode())
|
||||
return s.recv(512)
|
||||
def denied(host,port,role,path='/',payload=None):
|
||||
try:
|
||||
response=probe(host,port,role,path,payload)
|
||||
return not response or response.startswith(b'HTTP/1.1 403') or response.startswith(b'HTTP/1.0 403')
|
||||
except (OSError,ssl.SSLError):return True
|
||||
def check(value,label):
|
||||
if not value:raise AssertionError(label)
|
||||
results[label]='PASS'
|
||||
for host,port,role,path,raw in [('api1',8080,'lb','/health/ready',None),('api2',8080,'lb','/health/ready',None),('broker',6379,'worker','/',b'*1\r\n$4\r\nPING\r\n'),('otel-collector',4318,'worker','/',None)]:
|
||||
check(bool(probe(host,port,role,path,raw)),host+'_authorized_identity')
|
||||
check(denied(host,port,None,path,raw),host+'_no_certificate_denied')
|
||||
check(denied(host,port,'foreign',path,raw),host+'_foreign_CA_denied')
|
||||
check(denied(host,port,'outsider',path,raw),host+'_trusted_wrong_SAN_denied')
|
||||
for role in ['api1','api2','dispatcher']:
|
||||
check(bool(probe('otel-collector',4318,role)),role+'_collector_mTLS')
|
||||
check(bool(probe('broker',6379,'dispatcher',payload=b'*1\r\n$4\r\nPING\r\n')),'dispatcher_broker_mTLS')
|
||||
check(denied('broker',6379,'worker',payload=b'*5\r\n$4\r\nXADD\r\n$18\r\nattachment.jobs.v1\r\n$1\r\n*\r\n$3\r\njob\r\n$2\r\n{}\r\n'),'worker_cannot_publish_job_stream')
|
||||
# TLS matrix positive probes and role denials for each dependency path.
|
||||
for host,port,role,path in [('redis',6379,'api1','/'),('s3',9000,'worker','/'),('prometheus',9090,'grafana','/-/ready'),('loki',3100,'grafana','/ready'),('tempo',3200,'grafana','/ready')]:
|
||||
payload=b'*1\r\n$4\r\nPING\r\n' if host=='redis' else None
|
||||
check(bool(probe(host,port,role,path,payload)),host+'_verified_TLS')
|
||||
check(denied(host,port,'outsider',path,payload),host+'_outsider_denied')
|
||||
# Closed local hops cannot be reached through the container network.
|
||||
for host,port in [('broker',16379),('redis',16379),('s3',19000),('otel-collector',14318),('prometheus',19090),('loki',13100),('tempo',13200)]:
|
||||
try: socket.create_connection((host,port),timeout=1).close();closed=False
|
||||
except OSError:closed=True
|
||||
check(closed,host+'_plaintext_bypass_closed')
|
||||
# Actual cert login + scoped policy, not just reading policy text.
|
||||
c=http.client.HTTPSConnection('openbao',8200,context=context('worker'),timeout=5)
|
||||
c.request('POST','/v1/auth/cert/login',json.dumps({'name':'worker'}),{'Content-Type':'application/json'});r=c.getresponse();auth=json.loads(r.read());check(r.status==200,'bao_cert_login')
|
||||
token=auth['auth']['client_token'];headers={'X-Vault-Token':token,'Content-Type':'application/json'}
|
||||
for method,path,status in [('GET','secret/worker',200),('GET','secret/api1',403),('PUT','sys/policies/acl/injected',403)]:
|
||||
c.request(method,'/v1/'+path,'{}' if method=='PUT' else None,headers);r=c.getresponse();r.read();check(r.status==status,'bao_'+path.replace('/','_')+'_policy')
|
||||
check(auth['auth']['lease_duration']<=120,'bao_bounded_token_TTL')
|
||||
print(json.dumps({'status':'PASS','checks':results},indent=2))
|
||||
Reference in New Issue
Block a user