"""/https: the settings of HTTPS and the server's own HTTPS certificate.""" import base64 import binascii import ldap import linuxmanager import linuxmanager_pki from .common import PKI_STATUS, section_change #: The settings of the https section PUT /https/settings changes. CHANGEABLE = ('acme_staging', 'certificate', 'self_signed_days', 'ip_sans', 'acme_provider', 'acme_url', 'acme_eab_key_id', 'acme_eab_mac_key', 'zerossl_api_key') #: The secrets of the https section, which the answers give as _set instead. SECRETS = ('acme_eab_mac_key', 'zerossl_api_key') #: The paths of /https. PATHS = ('/https/settings', '/https/csr', '/https/certificate', '/https/pfx') def shown(settings): """The https section without its secrets, each replaced by whether it is set.""" return {**{key: value for key, value in settings.items() if key not in SECRETS}, **{f'{key}_set': bool(settings[key]) for key in SECRETS}} def https(method, path, body): """Answer a /https request: the HTTP status and the JSON document. GET /https/settings {"settings", "certificate"}: the https section, its secrets as _set, and the server's HTTPS certificate (linuxmanager_pki.https_status) PUT /https/settings change any of CHANGEABLE; the other settings of the section are changed on the server with linux-manager-config-edit, and a body naming one is 400 POST /https/csr make a key and a CSR for the HTTPS names: {"csr"} PUT /https/certificate take the certificate another CA signed for the CSR, PEM with its chain: {"certificate"} PUT /https/pfx take a PKCS#12 file of another CA's certificate: {"pfx" in base64, "password"} A change is applied by a run of linux-manager-setup, without waiting for it. """ if body == 'invalid' or (body is not None and not isinstance(body, dict)): return 400, {'error': 'the body must be a JSON object'} body = body or {} try: cfg = linuxmanager.load_config() if path == '/https/settings' and method in ('GET', 'HEAD'): return 200, {'settings': shown(cfg['https']), 'certificate': linuxmanager_pki.https_status(cfg)} if path == '/https/settings' and method == 'PUT': others = sorted(set(body) - set(CHANGEABLE)) if others: return 400, {'error': f'https.{", https.".join(others)}: changed on the server with ' 'linux-manager-config-edit only'} if 'certificate' in body and body['certificate'] not in linuxmanager.HTTPS_CERTIFICATES: return 400, {'error': f'https.certificate must be one of {", ".join(linuxmanager.HTTPS_CERTIFICATES)}'} changed = section_change('https', body) return changed if isinstance(changed, tuple) else (200, {'settings': shown(changed)}) with linuxmanager_pki.locked(): if (path, method) == ('/https/csr', 'POST'): return 200, linuxmanager_pki.https_csr(cfg) if (path, method) == ('/https/certificate', 'PUT'): document = linuxmanager_pki.import_https_certificate(cfg, body.get('certificate')) elif (path, method) == ('/https/pfx', 'PUT'): try: data = base64.b64decode(body.get('pfx') or '', validate=True) except (binascii.Error, TypeError): return 400, {'error': 'pfx must be a PKCS#12 file in base64'} password = body.get('password') if password is not None and not isinstance(password, str): return 400, {'error': 'password must be a string'} document = linuxmanager_pki.import_https_pfx(cfg, data, password) else: return 404, {'error': f'no route {method} {path}'} except linuxmanager_pki.Failure as failure: return PKI_STATUS.get(failure.status, 500), {'error': str(failure)} except ldap.LDAPError as exc: return 500, {'error': f'the directory, which holds the certificate, failed: {exc}'} except SystemExit as exc: return 500, {'error': str(exc.code)} linuxmanager.request_setup() return 200, document