forked from mtusi2026/mtusi2026lab7
73 lines
2.7 KiB
Python
Executable File
73 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Renew short-lived role leaves and publish fresh CRLs on an interval.
|
|
|
|
The controller runs on the operator host next to `securityctl.py`: it needs the
|
|
operator identity and the Docker CLI to reload native owners. It is not mounted
|
|
into application containers. Run it under a supervisor (systemd/launchd) or as
|
|
`make renewer` for a foreground lab session.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import datetime as dt
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'scripts'))
|
|
from lab7_environment import ROLES
|
|
|
|
SKIP = {'rogue'}
|
|
|
|
|
|
def run(arguments: list[str]) -> tuple[bool, str]:
|
|
result = subprocess.run([sys.executable, 'scripts/securityctl.py', *arguments],
|
|
cwd=ROOT, capture_output=True, text=True, timeout=300)
|
|
output = (result.stdout or result.stderr).strip()
|
|
return result.returncode == 0, output[-600:]
|
|
|
|
|
|
def cycle(ttl: str) -> dict:
|
|
started = dt.datetime.now(dt.timezone.utc).isoformat()
|
|
roles = {}
|
|
for role in ROLES:
|
|
if role in SKIP:
|
|
continue
|
|
ok, output = run(['renew', role, '--ttl', ttl])
|
|
mode = 'renew'
|
|
if not ok and 'expired' in output.lower():
|
|
# Expiry is the only fallback: a revoked/disabled identity must
|
|
# stay contained instead of being reissued by the operator.
|
|
mode = 'issue'
|
|
ok, output = run(['issue', role, '--ttl', ttl])
|
|
roles[role] = {'ok': ok, 'mode': mode, 'output': output}
|
|
crls_ok, crls_output = run(['sync-crls'])
|
|
return {'timestamp': started, 'ttl': ttl, 'roles': roles,
|
|
'crls': {'ok': crls_ok, 'output': crls_output},
|
|
'passed': crls_ok and all(item['ok'] for item in roles.values())}
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--interval', type=float, default=1800, help='seconds between cycles')
|
|
parser.add_argument('--ttl', default='1h', help='leaf TTL requested from OpenBao')
|
|
parser.add_argument('--once', action='store_true')
|
|
parser.add_argument('--output', type=Path, default=None)
|
|
args = parser.parse_args()
|
|
while True:
|
|
result = cycle(args.ttl)
|
|
print(json.dumps(result, ensure_ascii=False), flush=True)
|
|
if args.output:
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
args.output.write_text(json.dumps(result, indent=2, ensure_ascii=False) + '\n')
|
|
if args.once:
|
|
return 0 if result['passed'] else 1
|
|
time.sleep(max(1, args.interval if result['passed'] else min(60, args.interval)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|