Files
ServerKit/backend/app/services/ssl_service.py
T
Juan DenisandClaude Opus 4.8 b9ee243ba2 feat(cloudflare): restore Round 2 engine — DNSSEC, Origin CA, rules, activity (plan 42 ph2)
Restore the plan-36 Cloudflare Round 2 backend lost in the rebuild (migrations
073/074 survived; the engine + test_cloudflare_round2.py did not):

- CfOpsChange model (verbatim from clean pyc dump, matches mig 074) +
  CfOpsChangeService.record (never-raises) / list.
- Core client (dns/cloudflare.py): get/set_dnssec; create/list/revoke Origin
  certificates; X-Auth-User-Service-Key header mode (origin_ca_key, mig 073).
- Extension service: DNSSEC get/set; Origin CA issue (panel-side CSR, key never
  leaves the box, proxied-hostname warning, scoped-cred 400 not 502) /list/revoke
  + Linux-guarded install via upload_custom_cert; Redirect+Transform rules
  (RULE_PHASES/ACTIONS/PRESETS reusing _find_custom_ruleset + injection guard);
  best-effort _record wired into every mutating method; scope_check probe;
  list_activity.
- Extension routes: /zones/<id>/dnssec, origin-ca, /rules/<slug>, /activity,
  /scope-check (admin-gated, CloudflareError->400, provider->502).
- ssl_service.list_certificates walks /etc/ssl/serverkit + "Origin CA
  (proxy-only)" badge; fixed certbot-branch early-return hiding custom certs.

Also registers EmailBounceState (plan 42 ph3) in models/__init__ (shared file).
Deviation: origin_ca_key implemented as the Fernet Origin CA *service key* (plan
Decision 3), not mig-073's docstring "generated private key" — the generated key
is used inline for install and never persisted. R2 preset backend-untouched (it
lives in the storage-provider + frontend layers). Verified: 83 passed
(round2 30 + zone_settings 42 + extraction 8 + ssl 3), no regression.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 05:02:54 -04:00

336 lines
13 KiB
Python

import os
import subprocess
import json
from datetime import datetime
from typing import Dict, List, Optional
from pathlib import Path
from app.utils.system import ServiceControl, run_privileged, PackageManager, is_command_available
class SSLService:
"""Service for SSL certificate management with Let's Encrypt."""
CERTBOT_BIN = os.environ.get('CERTBOT_BIN', '/usr/bin/certbot')
CERTS_DIR = '/etc/letsencrypt/live'
RENEWAL_DIR = '/etc/letsencrypt/renewal'
# Custom-cert install location (e.g. issued Cloudflare Origin CA certs, written
# by AdvancedSSLService.upload_custom_cert). certbot doesn't know about these.
SERVERKIT_CERTS_DIR = '/etc/ssl/serverkit'
@classmethod
def is_certbot_installed(cls) -> bool:
"""Check if certbot is installed."""
return is_command_available('certbot')
@classmethod
def install_certbot(cls) -> Dict:
"""Install certbot if not present."""
if not PackageManager.is_available():
return {'success': False, 'error': 'No supported package manager found'}
try:
result = PackageManager.install(
['certbot', 'python3-certbot-nginx'],
timeout=300,
)
if result.returncode != 0:
return {'success': False, 'error': result.stderr}
return {'success': True, 'message': 'Certbot installed successfully'}
except Exception as e:
return {'success': False, 'error': str(e)}
@classmethod
def obtain_certificate(cls, domains: List[str], email: str,
webroot_path: str = None, use_nginx: bool = True) -> Dict:
"""Obtain a new SSL certificate from Let's Encrypt."""
if not cls.is_certbot_installed():
install_result = cls.install_certbot()
if not install_result['success']:
return install_result
try:
# Build certbot command
cmd = [cls.CERTBOT_BIN, 'certonly']
if use_nginx:
cmd.append('--nginx')
elif webroot_path:
cmd.extend(['--webroot', '-w', webroot_path])
else:
return {'success': False, 'error': 'Either use_nginx or webroot_path is required'}
# Add domains
for domain in domains:
cmd.extend(['-d', domain])
# Add email and agree to TOS
cmd.extend([
'--email', email,
'--agree-tos',
'--non-interactive',
'--expand'
])
result = run_privileged(cmd, timeout=300)
if result.returncode == 0:
primary_domain = domains[0]
cert_path = f'{cls.CERTS_DIR}/{primary_domain}/fullchain.pem'
key_path = f'{cls.CERTS_DIR}/{primary_domain}/privkey.pem'
response = {
'success': True,
'message': 'Certificate obtained successfully',
'certificate_path': cert_path,
'private_key_path': key_path,
'domains': domains
}
# Best-effort: authorize Let's Encrypt via a CAA record on whichever
# connected DNS provider manages the domain. This satisfies CAA
# security scanners and pins issuance to our CA. Never let a CAA
# hiccup fail an otherwise-successful certificate.
try:
from app.services.dns_provider_service import DNSProviderService
response['caa'] = DNSProviderService.ensure_caa_record(primary_domain)
except Exception as e:
response['caa'] = {'created': False, 'reason': 'error', 'error': str(e)}
return response
else:
return {'success': False, 'error': result.stderr}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Certificate request timed out'}
except Exception as e:
return {'success': False, 'error': str(e)}
@classmethod
def renew_certificate(cls, domain: str = None) -> Dict:
"""Renew SSL certificate(s)."""
try:
cmd = [cls.CERTBOT_BIN, 'renew', '--non-interactive']
if domain:
cmd.extend(['--cert-name', domain])
result = run_privileged(cmd, timeout=300)
return {
'success': result.returncode == 0,
'message': result.stdout if result.returncode == 0 else result.stderr
}
except Exception as e:
return {'success': False, 'error': str(e)}
@classmethod
def revoke_certificate(cls, domain: str) -> Dict:
"""Revoke an SSL certificate."""
cert_path = f'{cls.CERTS_DIR}/{domain}/fullchain.pem'
try:
cmd = [
cls.CERTBOT_BIN, 'revoke',
'--cert-path', cert_path,
'--non-interactive'
]
result = run_privileged(cmd, timeout=120)
if result.returncode == 0:
# Also delete the certificate
delete_cmd = [
cls.CERTBOT_BIN, 'delete',
'--cert-name', domain,
'--non-interactive'
]
run_privileged(delete_cmd)
return {'success': True, 'message': f'Certificate for {domain} revoked and deleted'}
return {'success': False, 'error': result.stderr}
except Exception as e:
return {'success': False, 'error': str(e)}
@classmethod
def list_certificates(cls) -> List[Dict]:
"""List all installed certificates."""
certificates = []
try:
result = run_privileged([cls.CERTBOT_BIN, 'certificates'], timeout=60)
# Parse certbot output (skip on a non-zero exit, but still surface the
# custom-installed certs below).
if result.returncode == 0:
current_cert = None
for line in result.stdout.split('\n'):
line = line.strip()
if line.startswith('Certificate Name:'):
if current_cert:
certificates.append(current_cert)
current_cert = {'name': line.split(':', 1)[1].strip()}
elif current_cert:
if line.startswith('Domains:'):
current_cert['domains'] = line.split(':', 1)[1].strip().split()
elif line.startswith('Expiry Date:'):
expiry_str = line.split(':', 1)[1].strip()
# Parse expiry date
try:
# Format: 2024-03-15 12:00:00+00:00
expiry_part = expiry_str.split(' (')[0]
current_cert['expiry'] = expiry_part
current_cert['expiry_valid'] = 'VALID' in expiry_str
except Exception:
current_cert['expiry'] = expiry_str
elif line.startswith('Certificate Path:'):
current_cert['cert_path'] = line.split(':', 1)[1].strip()
elif line.startswith('Private Key Path:'):
current_cert['key_path'] = line.split(':', 1)[1].strip()
if current_cert:
certificates.append(current_cert)
except Exception:
pass
# Also surface custom-installed certs (e.g. Cloudflare Origin CA), which
# certbot doesn't track. Parse the x509 issuer to badge them.
certificates.extend(cls._list_serverkit_certificates())
return certificates
@classmethod
def _list_serverkit_certificates(cls) -> List[Dict]:
"""Walk the custom-cert install dir (``/etc/ssl/serverkit/<domain>/cert.pem``)
and describe each cert, badging Cloudflare Origin CA certs as proxy-only."""
import glob
out: List[Dict] = []
try:
from cryptography import x509
from cryptography.hazmat.backends import default_backend
except Exception:
return out
for cert_path in sorted(glob.glob(f'{cls.SERVERKIT_CERTS_DIR}/*/cert.pem')):
try:
domain = os.path.basename(os.path.dirname(cert_path))
with open(cert_path, 'rb') as fh:
cert = x509.load_pem_x509_certificate(fh.read(), default_backend())
issuer = cert.issuer.rfc4514_string()
is_origin_ca = 'origin' in issuer.lower() and 'cloudflare' in issuer.lower()
try:
expiry = cert.not_valid_after_utc
except AttributeError: # cryptography < 42
expiry = cert.not_valid_after
out.append({
'name': domain,
'domains': [domain],
'cert_path': cert_path,
'key_path': os.path.join(os.path.dirname(cert_path), 'key.pem'),
'issuer': issuer,
'source': 'custom',
'badge': 'Origin CA (proxy-only)' if is_origin_ca else None,
'expiry': expiry.strftime('%Y-%m-%d %H:%M:%S%z') if expiry else None,
})
except Exception:
continue
return out
@classmethod
def get_certificate_info(cls, domain: str) -> Optional[Dict]:
"""Get detailed information about a specific certificate."""
cert_path = f'{cls.CERTS_DIR}/{domain}/fullchain.pem'
try:
# Use openssl to get certificate details
result = run_privileged(
['openssl', 'x509', '-in', cert_path, '-noout',
'-subject', '-issuer', '-dates', '-serial'],
)
if result.returncode != 0:
return None
info = {'domain': domain, 'cert_path': cert_path}
for line in result.stdout.split('\n'):
if line.startswith('subject='):
info['subject'] = line.split('=', 1)[1].strip()
elif line.startswith('issuer='):
info['issuer'] = line.split('=', 1)[1].strip()
elif line.startswith('notBefore='):
info['valid_from'] = line.split('=', 1)[1].strip()
elif line.startswith('notAfter='):
info['valid_until'] = line.split('=', 1)[1].strip()
elif line.startswith('serial='):
info['serial'] = line.split('=', 1)[1].strip()
return info
except Exception:
return None
@classmethod
def check_expiry(cls, domain: str) -> Dict:
"""Check if a certificate is expiring soon."""
try:
cert_path = f'{cls.CERTS_DIR}/{domain}/fullchain.pem'
# Check expiry with openssl
result = run_privileged(
['openssl', 'x509', '-in', cert_path, '-checkend', '2592000'],
)
expiring_soon = result.returncode != 0
# Get actual expiry date
date_result = run_privileged(
['openssl', 'x509', '-in', cert_path, '-noout', '-enddate'],
)
expiry_date = None
if date_result.returncode == 0:
expiry_date = date_result.stdout.replace('notAfter=', '').strip()
return {
'domain': domain,
'expiring_soon': expiring_soon,
'expiry_date': expiry_date,
'needs_renewal': expiring_soon
}
except Exception as e:
return {'domain': domain, 'error': str(e)}
@classmethod
def setup_auto_renewal(cls) -> Dict:
"""Set up automatic certificate renewal via cron/systemd timer."""
try:
# Check if systemd timer exists
if ServiceControl.is_enabled('certbot.timer'):
return {'success': True, 'message': 'Auto-renewal already configured via systemd'}
# Enable systemd timer
enable_result = run_privileged(
['systemctl', 'enable', '--now', 'certbot.timer'],
)
if enable_result.returncode == 0:
return {'success': True, 'message': 'Auto-renewal enabled via systemd timer'}
# Fall back to cron
cron_job = '0 0,12 * * * root certbot renew --quiet --post-hook "systemctl reload nginx"'
cron_file = '/etc/cron.d/certbot-renewal'
run_privileged(['tee', cron_file], input=cron_job + '\n')
return {'success': True, 'message': 'Auto-renewal configured via cron'}
except Exception as e:
return {'success': False, 'error': str(e)}