"""Operator-only local PKI. Private CA never mounted into any service.""" import os from pathlib import Path import secrets import subprocess ROOT=Path(__file__).resolve().parents[1] def command(*args): subprocess.run(['openssl',*map(str,args)],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) def issue(role,days=1,foreign=False,identity=None,uri_identity=None): name=identity or role folder=ROOT/'pki'/role folder.mkdir(parents=True,exist_ok=True) ca=ROOT/'operator'/('foreign' if foreign else 'ca') config=folder/'request.cnf' config.write_text('[req]\nprompt=no\ndistinguished_name=dn\n[dn]\nCN='+name+'\n[ext]\nbasicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth,clientAuth\nsubjectAltName=DNS:'+name+',DNS:localhost,IP:127.0.0.1,URI:spiffe://mtusi.lab/'+(uri_identity or name)+'\n') command('req','-new','-newkey','rsa:2048','-nodes','-keyout',folder/'tls.key','-out',folder/'request.csr','-config',config) if days: command('x509','-req','-in',folder/'request.csr','-CA',str(ca)+'.crt','-CAkey',str(ca)+'.key','-set_serial','0x'+secrets.token_hex(16),'-out',folder/'tls.crt','-days',days,'-sha256','-extfile',config,'-extensions','ext') else: op=ROOT/'operator' (op/'issued').mkdir(exist_ok=True) (op/'index.txt').touch() (op/'serial').write_text(secrets.token_hex(16)+'\n') cfg=op/'expired-ca.cnf' cfg.write_text('[ca]\ndefault_ca=local\n[local]\ndatabase='+str(op/'index.txt')+'\nserial='+str(op/'serial')+'\nnew_certs_dir='+str(op/'issued')+'\ncertificate='+str(ca)+'.crt\nprivate_key='+str(ca)+'.key\ndefault_md=sha256\npolicy=policy\n[policy]\ncommonName=supplied\n') command('ca','-batch','-notext','-config',cfg,'-in',folder/'request.csr','-out',folder/'tls.crt','-startdate','20000101000000Z','-enddate','20000102000000Z','-extfile',config,'-extensions','ext') (folder/'tls.pem').write_bytes((folder/'tls.crt').read_bytes()+(folder/'tls.key').read_bytes()) (folder/'ca.crt').write_bytes((ROOT/'operator/ca.crt').read_bytes()) if not (folder/'revoked.list').exists(): (folder/'revoked.list').write_text('00\n') # Parent pki is 0700 on host; a service mounts only its own folder. for f in folder.iterdir():f.chmod(0o644) return folder def bootstrap(): for n in ['operator','pki','secrets']: f=ROOT/n;f.mkdir(exist_ok=True);f.chmod(0o700) for role in ['ca','foreign']: base=ROOT/'operator'/role if not base.with_suffix('.key').exists(): config=ROOT/'operator'/(role+'.cnf') config.write_text('[req]\nprompt=no\ndistinguished_name=dn\nx509_extensions=ext\n[dn]\nCN=MTUSI-LAB7-'+role+'\n[ext]\nbasicConstraints=critical,CA:TRUE\nkeyUsage=critical,keyCertSign,cRLSign\n') command('req','-x509','-newkey','rsa:3072','-nodes','-sha256','-days','30','-keyout',str(base)+'.key','-out',str(base)+'.crt','-config',config) base.with_suffix('.key').chmod(0o600) for role in ['lb','api','worker','dispatcher','operator','s3','broker','redis','otel-collector','collector','prometheus','loki','tempo','grafana','bao','postgres','outsider']: if not (ROOT/'pki'/role/'tls.key').exists():issue(role) if not (ROOT/'pki/foreign-client/tls.key').exists():issue('foreign-client',foreign=True) for identity in ['lb','api','worker','dispatcher','collector','prometheus','grafana']: if not (ROOT/'pki'/('expired-'+identity)/'tls.crt').exists():issue('expired-'+identity,days=0,identity=identity) if not (ROOT/'pki/wrong-lb/tls.crt').exists():issue('wrong-lb',identity='lb',uri_identity='outsider') print('Local PKI ready; CA keys remain in operator/ and are not mounted into services.') if __name__=='__main__':bootstrap()