#!/usr/bin/env python3 """Renew the development server's public installer certificate before expiry.""" from pathlib import Path import os import subprocess import tempfile def main(): state = Path('/opt/rmm-bootstrap-download') cert = state / 'chain.pem' host = 'discovery.ease-technologies.de' def run(*args, **kwargs): return subprocess.run(args, check=True, timeout=180, **kwargs) if subprocess.run(['openssl', 'x509', '-in', cert, '-noout', '-checkend', str(30*86400)], stdout=subprocess.DEVNULL).returncode == 0: print('Public installer certificate valid beyond renewal window') return rules = run('ufw','show','added', capture_output=True, text=True).stdout.splitlines() rule = ['allow','80/tcp'] already_open = any(line.split()[:3] == ['ufw',*rule] for line in rules) run('systemctl','start','rmm-bootstrap-acme.service') try: if not already_open: run('ufw',*rule) try: with tempfile.NamedTemporaryFile(dir=state, delete=False) as output: candidate = Path(output.name) try: run('/usr/bin/python3','-I',state/'acme_tiny.py','--account-key',state/'account.key', '--csr',state/'domain.csr','--acme-dir','/srv/rmm-bootstrap-acme',stdout=output) output.flush() run('openssl','verify','-verify_hostname',host,'-CAfile','/etc/ssl/certs/ca-certificates.crt', '-untrusted',candidate,candidate) cert_key = run('openssl','x509','-in',candidate,'-noout','-pubkey',capture_output=True).stdout local_key = run('openssl','pkey','-in',state/'domain.key','-pubout',capture_output=True).stdout if cert_key != local_key: raise ValueError('Certificate does not match bootstrap key') os.fsync(output.fileno()) os.replace(candidate,cert) finally: candidate.unlink(missing_ok=True) finally: if not already_open: run('ufw','delete',*rule) finally: run('systemctl','stop','rmm-bootstrap-acme.service') run('systemctl','restart','rmm-install-ingress.service') if __name__ == '__main__': main()