Выполнение лабораторной работы номер 7

This commit is contained in:
2026-12-04 18:15:18 +03:00
parent 20afc2c790
commit 2fae615742
77 changed files with 5212 additions and 64 deletions
+59
View File
@@ -0,0 +1,59 @@
"""Verify only the local lab's configured TLS identity and scoped secret boundaries."""
import json
from pathlib import Path
import subprocess
ROOT=Path(__file__).resolve().parents[1]
PROBE='''
import ssl,socket,sys,json,urllib.request,urllib.error
host,port,identity,expected,kind=sys.argv[1:]
ctx=ssl.create_default_context(cafile='/ca.crt')
if identity=='yes': ctx.load_cert_chain('/identity/tls.crt','/identity/tls.key')
ok=False
try:
if kind=='http':
try:
urllib.request.urlopen('https://'+host+':'+port+'/health/live',context=ctx,timeout=5).read()
ok=True
except urllib.error.HTTPError:
ok=True
elif kind=='redis':
with socket.create_connection((host,int(port)),timeout=5) as sock:
with ctx.wrap_socket(sock,server_hostname=host) as tls:
tls.sendall(b'*1\\r\\n$4\\r\\nPING\\r\\n')
ok=bool(tls.recv(256))
except (OSError,urllib.error.URLError): pass
if ok != (expected=='yes'): raise SystemExit('unexpected TLS result')
print('PASS')
'''
def probe(host,port,role,expected,kind):
pki=ROOT/'certs/private'
cmd=['docker','run','--rm','--network','messenger-lab7_default','-v',f'{pki/("ca.crt")}:/ca.crt:ro']
if role:
cmd+=['-v',f'{pki/role}:/identity:ro']
cmd+=['python:3.11.9-slim','python','-c',PROBE,host,str(port),'yes' if role else 'no','yes' if expected else 'no',kind]
subprocess.run(cmd,check=True,cwd=ROOT)
def main():
results=[]
for host,port,kind,allowed,wrong in [
('api',8443,'http','lb-client','api-client'),
('broker',6379,'redis','worker-client','api-client'),
('collector',4319,'http','worker-client','lb-client'),
('collector',8890,'http','prometheus-client','worker-client'),
('loki',3101,'http','collector-client','worker-client'),
('tempo',4319,'http','collector-client','worker-client'),
('tempo',3201,'http','grafana-client','worker-client'),
('prometheus',9090,'http','grafana-client','worker-client'),
]:
for role,expected in [(allowed,True),(None,False),(wrong,False)]:
probe(host,port,role,expected,kind)
results.append({'service':host,'port':port,'client':role or 'none','accepted':expected})
(ROOT/'evidence/tls-boundaries.json').write_text(json.dumps(results,indent=2)+'\n',encoding='utf-8')
if __name__=='__main__':
main()