"""Local operator PKI. Keys/operator shares remain in ignored .secrets only.""" import os from pathlib import Path import secrets import subprocess root=Path(__file__).resolve().parents[1] os.chdir(root);os.umask(0o077) d=Path('.secrets');d.mkdir(exist_ok=True) if not Path('.env').exists(): values={k:secrets.token_hex(24) for k in ('POSTGRES_PASSWORD','S3_ROOT_PASSWORD','GRAFANA_PASSWORD')} values.update(APP_PORT='8443',LOCAL_UID=str(os.getuid()),LOCAL_GID=str(os.getgid())) Path('.env').write_text(''.join(f'{k}={v}\n' for k,v in values.items())) def run(*args):subprocess.run(['openssl',*args],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) if not (d/'ca.key').exists(): run('req','-x509','-newkey','rsa:3072','-nodes','-days','30','-keyout',str(d/'ca.key'),'-out',str(d/'ca.crt'),'-subj','/CN=lab7-operator-ca','-addext','keyUsage=critical,keyCertSign,cRLSign','-addext','basicConstraints=critical,CA:TRUE') print('Operator CA and local .env prepared; private material excluded from Git')