#!/usr/bin/env python3 """Reference-only TLS deployment: CA private key never leaves HQ-SRV.""" import argparse import json from pathlib import Path from pve import run parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--confirm-reference', action='store_true', required=True) args = parser.parse_args() root = Path(__file__).resolve().parents[1] values = dict(line.split(' - ', 1) for line in (root/'задание/stend.md').read_text().splitlines() if ' - ' in line) if values.get('IP') != '10.12.21.1': parser.error('Not the authorised reference hypervisor') for vmid, role in ((401, 'isp'), (404, 'hq-srv'), (406, 'hq-cli')): if f'name: kim27-t23-{role}' not in run(f'qm config {vmid}', capture=True).stdout.splitlines(): parser.error(f'Unexpected VM{vmid}') def guest(vmid, script): data = json.loads(run(f'qm guest exec {vmid} --timeout 180 --pass-stdin 1 -- /usr/bin/python3', stdin=script, capture=True).stdout) if not data.get('exited') or data.get('exitcode') != 0: # Never include captured key-transfer payload in exception output. raise RuntimeError(f'VM{vmid}: TLS action failed: {data.get("err-data", "")}') return data.get('out-data', '') files = json.loads(guest(404, '''from pathlib import Path import base64,json root=Path('/root/kim2027-ca') print(json.dumps({n:base64.b64encode((root/n).read_bytes()).decode() for n in ['ca.crt','web.crt','web.key','docker.crt','docker.key']})) ''')) guest(401, '''from pathlib import Path import base64,subprocess files=FILES p=Path('/etc/nginx/sites-enabled.d/kim2027.conf') s=p.read_text() assert 'ssl_certificate' not in s, 'TLS is already configured; review before repeating' root=Path('/etc/nginx/tls');root.mkdir(mode=0o700,exist_ok=True);root.chmod(0o700) assert not any((root/name).exists() for name in files), 'TLS files already exist; review before repeating' for name,data in files.items(): p=root/name;p.write_bytes(base64.b64decode(data));p.chmod(0o600 if name.endswith('.key') else 0o644) p=Path('/etc/nginx/sites-enabled.d/kim2027.conf') for name,ip in [('web','172.16.2.1'),('docker','172.16.1.1')]: old='listen '+ip+':80;' assert s.count(old)==1 new='listen '+ip+':443 ssl;\\n ssl_certificate /etc/nginx/tls/'+name+'.crt;\\n ssl_certificate_key /etc/nginx/tls/'+name+'.key;\\n ssl_protocols TLSv1.2;\\n ssl_ciphers GOST2012-KUZNYECHIK-KUZNYECHIKOMAC:GOST2012-MAGMA-MAGMAOMAC;' s=s.replace(old,new) s+='\\nserver { listen 172.16.1.1:80; listen 172.16.2.1:80; server_name web.au-team.irpo docker.au-team.irpo; return 301 https://$host$request_uri; }\\n' backup=p.with_suffix('.before-tls');assert not backup.exists();backup.write_text(p.read_text()) p.write_text(s) subprocess.run(['control','openssl-gost','enabled'],check=True) subprocess.run(['nginx','-t'],check=True) subprocess.run(['systemctl','restart','nginx'],check=True) '''.replace('FILES', repr(files))) guest(406, '''from pathlib import Path import base64,subprocess p=Path('/etc/pki/ca-trust/source/anchors/kim2027-gost.crt');p.parent.mkdir(parents=True,exist_ok=True) p.write_bytes(base64.b64decode(CERT));p.chmod(0o644) subprocess.run(['update-ca-trust'],check=True) '''.replace('CERT', repr(files['ca.crt']))) print('New GOST TLS configured; public CA installed on HQ-CLI. Browser verification still required.')