forked from mtusi2026/mtusi2026lab7
72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверяет sealed OpenBao, границу TTL и восстановление API без удаления данных."""
|
|
import datetime
|
|
from pathlib import Path
|
|
import ssl
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client, Failure, ROOT, arguments, check, run
|
|
from worker_compromise import cmd
|
|
|
|
|
|
def instances(c):
|
|
return {c.call('GET', '/health/ready')['headers']['X-Instance-Id'] for _ in range(8)}
|
|
|
|
|
|
def main():
|
|
args = arguments('OpenBao sealed / credential expiry')
|
|
ca = args.ca_file or ROOT / 'secrets/tls/service-ca/ca.crt'
|
|
c = Client(args.base_url, ca, timeout=5)
|
|
deadline = time.monotonic() + 90
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
if len(instances(c)) == 2:
|
|
break
|
|
except Failure:
|
|
pass
|
|
time.sleep(1)
|
|
else:
|
|
raise AssertionError('Before sealing, both API replicas must respond')
|
|
|
|
started = time.monotonic()
|
|
since = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
ctx = ssl.create_default_context(cafile=ca)
|
|
cmd('python3', 'scripts/bao.py', 'seal')
|
|
try:
|
|
c.call('GET', '/health/live')
|
|
expired = set()
|
|
while time.monotonic() < started + 80:
|
|
logs = cmd('docker', 'compose', 'logs', '--since', since, '--no-color', 'api', capture=True)
|
|
expired.update(line.split('|', 1)[0].strip() for line in logs.splitlines() if 'secrets.expired' in line)
|
|
if len(expired) == 2:
|
|
break
|
|
time.sleep(1)
|
|
check(len(expired) == 2, 'Both replicas must record credential expiry')
|
|
try:
|
|
urllib.request.urlopen(c.base + '/health/ready', context=ctx, timeout=5).close()
|
|
except urllib.error.HTTPError as exc:
|
|
check(exc.code == 503, 'Expected HTTP 503 from ingress')
|
|
else:
|
|
raise AssertionError('Ingress still routes after both API credentials expired')
|
|
print(f'PASS sealed OpenBao, both replicas expired and ingress returned 503 after {time.monotonic() - started:.1f}s')
|
|
finally:
|
|
cmd('python3', 'scripts/bao.py', 'unseal')
|
|
deadline = time.monotonic() + 90
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
if len(instances(c)) == 2:
|
|
print(f'PASS both replicas recovered after {time.monotonic() - started:.1f}s from seal')
|
|
return
|
|
except Failure:
|
|
pass
|
|
time.sleep(1)
|
|
raise AssertionError('API failed to recover after unseal')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|