Files
mtusi2026lab7/tests/test_rotation.py
T
amin 2fae615742
Skeleton contracts / contracts (pull_request) Canceled after 0s
Выполнение лабораторной работы номер 7
2026-12-04 18:15:18 +03:00

49 lines
2.0 KiB
Python

"""Rotate the local LB certificate and Redis password without building images."""
import json
from pathlib import Path
import secrets
import subprocess
import sys
import time
from test_tls_boundaries import probe, ROOT
def run(*args):
subprocess.run(args,cwd=ROOT,check=True)
def redis_check(password,expected):
code="""import redis,sys
pw=sys.stdin.read().strip()
r=redis.Redis(host='redis',port=6379,password=pw,ssl=True,ssl_ca_certs='/ca/ca.crt',ssl_check_hostname=True,socket_timeout=3)
try: ok=r.ping()
except redis.AuthenticationError: ok=False
assert bool(ok)==(sys.argv[1]=='yes')
"""
subprocess.run(['docker','compose','exec','-T','--index','1','api','python','-c',code,'yes' if expected else 'no'],
input=password,text=True,cwd=ROOT,check=True)
def main():
run(sys.executable,'scripts/bootstrap_pki.py','--rotate','lb-client')
run('docker','compose','restart','nginx')
probe('api',8443,'lb-client',True,'http')
probe('api',8443,ROOT/'secrets/rotated/lb-client',False,'http')
env_file=ROOT/'.env'; lines=env_file.read_text(encoding='utf-8').splitlines()
old=next(line.split('=',1)[1] for line in lines if line.startswith('REDIS_PASSWORD='))
new=secrets.token_hex(24)
env_file.write_text('\n'.join('REDIS_PASSWORD='+new if line.startswith('REDIS_PASSWORD=') else line for line in lines)+'\n',encoding='utf-8')
run(sys.executable,'scripts/bootstrap_bao.py')
run('docker','compose','up','-d','--no-deps','--force-recreate','redis')
run('docker','compose','restart','api')
time.sleep(8)
redis_check(old,False);redis_check(new,True)
run(sys.executable,'-X','utf8','tests/smoke.py','--ca-file','certs/private/ca.crt')
(ROOT/'evidence/rotation.json').write_text(json.dumps({'leaf_rotation':True,'old_leaf_rejected':True,'redis_secret_rotation':True,'old_secret_rejected':True},indent=2)+'\n',encoding='utf-8')
print('PASS: certificate and cache secret rotated without image rebuild')
if __name__=='__main__':
main()