forked from mtusi2026/mtusi2026lab7
57 lines
3.3 KiB
Python
57 lines
3.3 KiB
Python
"""Operator lifecycle checks: fresh-connection revocation, secret rotation, sealed lease.
|
|
Runs sequentially; restores OpenBao and worker in finally. No secret output.
|
|
"""
|
|
import json,os,subprocess,time,ssl,urllib.error
|
|
from pipeline_helpers import *
|
|
|
|
def operator(script,*args):
|
|
p=subprocess.run(['python3',str(ROOT/'scripts'/script),*args],cwd=ROOT,capture_output=True,text=True)
|
|
check(p.returncode==0,'operator action failed: '+script)
|
|
|
|
def main():
|
|
c=Client(timeout=10);actor,_,_,chat=c.bootstrap()
|
|
images={s:compose('images','-q',s) for s in ['api1','worker','nginx']}
|
|
old=json.loads((ROOT/'secrets/operator/service-secrets.json').read_text())['worker']['DATABASE_URL']
|
|
operator('rotate-db-secret.py')
|
|
# libpq rejects the previous password on a NEW TLS connection.
|
|
from urllib.parse import urlsplit
|
|
u=urlsplit(old)
|
|
p=subprocess.run(['docker','compose','--env-file','.env','exec','-T','-e','PGPASSWORD','-e','PGSSLMODE=verify-full','-e','PGSSLROOTCERT=/run/ca/ca.crt','postgres','psql','-XAt','-h','postgres','-U','worker','-d','messenger','-c','SELECT 1'],cwd=ROOT,capture_output=True,text=True,env=dict(os.environ,PGPASSWORD=u.password))
|
|
check(p.returncode!=0 and 'password authentication failed' in p.stderr,'old secret still authenticates')
|
|
item=upload(c,actor,chat);wait_state(c,actor,item)
|
|
operator('rotate-leaf.py','worker')
|
|
code='''
|
|
import socket,ssl
|
|
c=ssl.create_default_context(cafile='/audit/public/ca.crt');c.load_cert_chain('/old/cert.crt','/old/key.key')
|
|
try:
|
|
with socket.create_connection(('broker',6379),timeout=3) as raw:
|
|
with c.wrap_socket(raw,server_hostname='broker') as s:
|
|
s.sendall(b'*1\\r\\n$4\\r\\nPING\\r\\n');data=s.recv(512)
|
|
assert not data,'revoked certificate accepted'
|
|
except (OSError,ssl.SSLError):pass
|
|
print('PASS')
|
|
'''
|
|
p=subprocess.run(['docker','compose','--env-file','.env','run','--rm','--no-deps','-v',str(ROOT/'secrets/operator/previous-leaf/worker')+':/old:ro','--entrypoint','python3','audit','-c',code],cwd=ROOT,capture_output=True,text=True)
|
|
check(p.returncode==0,'revoked leaf passed fresh connection')
|
|
item=upload(c,actor,chat);wait_state(c,actor,item)
|
|
# API rejects an expired secret lease while remaining available for /health/live.
|
|
started=time.monotonic()
|
|
try:
|
|
operator('bao-operator.py','seal')
|
|
def expired():
|
|
try:return c.call('GET','/api/v1/users/me',503,actor=actor)
|
|
except Failure:return None
|
|
wait_for(expired,'API used credentials beyond bounded sealed lease',90)
|
|
elapsed=time.monotonic()-started
|
|
c.call('GET','/health/live',schema='Health')
|
|
finally:
|
|
operator('bao-operator.py','unseal')
|
|
compose('start','worker','dispatcher','cleanup')
|
|
ready(c)
|
|
c.call('GET','/api/v1/users/me',actor=actor,schema='User')
|
|
item=upload(c,actor,chat);wait_state(c,actor,item,timeout=120)
|
|
check(all(compose('images','-q',s)==image for s,image in images.items()),'rotation rebuilt images')
|
|
result={'status':'PASS','old_DB_password_new_connection':'rejected','new_DB_password':'working','leaf_rotation':'worker','old_leaf_new_connection':'rejected','images_unchanged':True,'sealed_lease_rejected_seconds':round(elapsed,3),'unsealed_recovery':True,'original_session_preserved':True}
|
|
dump_evidence('lifecycle-grade4.json',result);print('PASS: DB rotation, leaf revocation and sealed/expired secret lease')
|
|
if __name__=='__main__':run(main)
|