Реализовать лабораторную 7: mTLS, OpenBao и защита worker

This commit is contained in:
wpyatzy
2026-12-04 20:35:40 +03:00
parent 20afc2c790
commit 1b865eac83
146 changed files with 9552 additions and 87 deletions
+40
View File
@@ -0,0 +1,40 @@
"""Operator response: quarantine worker before delivering its replacement identity."""
import json,os,secrets,shutil,subprocess,time
from pathlib import Path
from cryptography import x509
from pki import PRIVATE,issue,crl
from bao_operator import call,provision
os.umask(0o077)
def run(*args):subprocess.run(args,check=True,stdout=subprocess.DEVNULL)
def compose(*args):run('./scripts/compose.sh',*args)
def rotate():
start=time.monotonic();compose('stop','-t','5','worker')
old=PRIVATE/'old-worker';old.mkdir(exist_ok=True)
role=PRIVATE/'roles/worker'
for name in ['service.pem','service.crt']:shutil.copyfile(role/name,old/name)
values=call('secret/data/worker')['data']['data'];(old/'values.json').write_text(json.dumps(values))
cert=x509.load_pem_x509_certificate((role/'service.crt').read_bytes());crl(cert.serial_number)
# Revoke existing service tokens, not only new certificate logins.
for accessor in call('auth/token/accessors',method='LIST')['data']['keys']:
try:data=call('auth/token/lookup-accessor',{'accessor':accessor})['data']
except __import__('urllib.error',fromlist=['HTTPError']).HTTPError as exc:
if exc.code in (400,403):continue
raise
if 'worker' in data.get('policies',[]):call('auth/token/revoke-accessor',{'accessor':accessor})
credentials=json.loads((PRIVATE/'credentials.json').read_text())
for k in ('db_password','s3_secret','broker_password'):credentials['worker'][k]=secrets.token_hex(24)
(PRIVATE/'credentials.json').write_text(json.dumps(credentials))
run('.venv/bin/python','scripts/configure.py');provision()
run('./scripts/operator.sh','python','scripts/provision.py')
run('./scripts/operator.sh','python','scripts/rotate_apply.py')
for service in ['db','s3','broker','cache','collector','prometheus','loki','tempo','grafana','lb','api','publisher','observer','janitor']:
ids=subprocess.check_output(['./scripts/compose.sh','ps','-q',service],text=True).split()
if ids:run('docker','kill','--signal','USR2',*ids)
time.sleep(6)
# Probe captured credentials while new identity is still withheld.
result=subprocess.check_output(['./scripts/compose.sh','run','--rm','--no-deps','-v',str(old)+':/run/old-worker:ro','operator','python','service.py','operator','python','tests/revoked_identity.py'],text=True)
proof=json.loads(next(line for line in result.splitlines() if line.startswith('{')))
issue('worker',replace=True)
compose('up','-d','--no-deps','--force-recreate','worker')
return {'revocation_and_rotation_seconds':round(time.monotonic()-start,3),'proof':proof}
if __name__=='__main__':print(json.dumps(rotate(),indent=2))