68 lines
3.2 KiB
Python
68 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Fixed server TLS maintenance. No client identity, grant or job changes."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
ROOT = Path('/opt/rust-discovery')
|
|
|
|
|
|
def run(args):
|
|
return subprocess.run(args, capture_output=True, check=True)
|
|
|
|
|
|
def main():
|
|
if os.geteuid() != 0:
|
|
raise RuntimeError('root maintenance required')
|
|
os.umask(0o077)
|
|
pki = ROOT/'devstate/pki'
|
|
current = pki/'server.pem'
|
|
for path in (ROOT, ROOT/'devstate', pki, ROOT/'devstate/api-pki', current,
|
|
ROOT/'devstate/api-pki/server.pem', pki/'ca.pem', pki/'ca.key',
|
|
pki/'ca.srl', pki/'server.key'):
|
|
if path.is_symlink() or path.stat().st_mode & 0o022:
|
|
raise RuntimeError('unsafe PKI path')
|
|
api_certificate = ROOT/'devstate/api-pki/server.pem'
|
|
if current.read_bytes() == api_certificate.read_bytes() and subprocess.run(
|
|
['openssl', 'x509', '-checkend', '604800', '-noout', '-in', str(current)],
|
|
capture_output=True).returncode == 0:
|
|
return
|
|
run(['openssl', 'x509', '-checkend', '2595600', '-noout', '-in', str(pki/'ca.pem')])
|
|
with tempfile.TemporaryDirectory(prefix='server-renew-', dir=ROOT/'devstate') as folder:
|
|
folder = Path(folder)
|
|
config = folder/'extensions.cnf'
|
|
config.write_text('[server]\nbasicConstraints=critical,CA:FALSE\n'
|
|
'keyUsage=critical,digitalSignature\nextendedKeyUsage=serverAuth\n'
|
|
'subjectAltName=DNS:discovery.ease-technologies.de,DNS:rmm-dev.internal,DNS:localhost\n')
|
|
csr = folder/'server.csr'
|
|
run(['openssl', 'req', '-new', '-key', str(pki/'server.key'), '-out', str(csr),
|
|
'-subj', '/CN=discovery.ease-technologies.de'])
|
|
certificate = folder/'server.pem'
|
|
run(['openssl', 'x509', '-req', '-in', str(csr), '-CA', str(pki/'ca.pem'),
|
|
'-CAkey', str(pki/'ca.key'), '-CAserial', str(pki/'ca.srl'), '-out', str(certificate),
|
|
'-days', '30', '-extfile', str(config), '-extensions', 'server'])
|
|
run(['openssl', 'verify', '-CAfile', str(pki/'ca.pem'), '-verify_hostname',
|
|
'discovery.ease-technologies.de', str(certificate)])
|
|
data = certificate.read_bytes()
|
|
for directory in (pki, ROOT/'devstate/api-pki'):
|
|
temporary = directory/'.server-renew.pem'
|
|
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
|
|
with os.fdopen(fd, 'wb') as stream:
|
|
stream.write(data); stream.flush(); os.fsync(stream.fileno())
|
|
os.chown(temporary, 10001, 10001)
|
|
os.replace(temporary, directory/'server.pem')
|
|
# The Rust listeners load certificates at startup. Restart only these own
|
|
# services; durable jobs/journals remain in PostgreSQL and on clients.
|
|
run(['docker', 'compose', '--env-file', str(ROOT/'devstate/.env'), '-f',
|
|
str(ROOT/'deploy/server.compose.yaml'), 'restart', 'api', 'gateway'])
|
|
print('Server TLS certificate renewed with unchanged CA and key')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except Exception:
|
|
print('SERVER_TLS_RENEWAL_FAILED; inspect protected server configuration')
|
|
raise SystemExit(1) from None
|