Files
taylanbakircioglu 02b1cb2bca feat: v1.5.0 — Site Wizard (Issue #14) + ACME Diagnostic Panel (Issue #13)
Closes #13, Closes #14.

This release squashes the v1.4.0 → v1.5.0 development line. v1.4.0
shipped the ACME stability & enterprise audit (Issues #10/#11/#12).
v1.5.0 builds on that foundation with two co-equal headline features
plus a 22-round audit campaign hardening the prior configuration
surface. License remains MIT for v1.5.0 (relicense to AGPL-3.0
lands in v1.5.2).

------------------------------------------------------------------
HEADLINE FEATURE A — ACME Diagnostic Panel (Issue #13)
------------------------------------------------------------------
A live pre-flight + post-failure diagnostic surface for every ACME
order, reachable from the ACME Automation page. The panel exists
to make ACME failures legible to operators who do NOT have shell
access to the API host.

Endpoints (`backend/routers/acme_diagnostics.py`):
  POST /api/letsencrypt/orders/{order_id}/diagnostics
       Run the full 5-check suite (DNS / port-80 / routing /
       account / agents) and humanize the order's `error_detail`
       (>=11 RFC-8555 problem types, backwards compatible with
       legacy plain-string failures).
  POST /api/letsencrypt/orders/{order_id}/diagnostics/
                                {check_id}/rerun
       Re-run a single check in place — used by the "Re-run"
       button on every row of the modal's pre-flight table.
  GET  /api/letsencrypt/orders/{order_id}/events
       Merged event timeline combining the typed
       `acme_order_events` rows with correlated
       `user_activity_logs` entries (resource_type =
       'letsencrypt_order' AND resource_id = order_id). The
       diagnostic modal auto-tails this timeline every 5 seconds
       while open.

Service-level checks (`backend/services/acme_diagnostics.py`):
  * DNS resolution via stdlib socket.gethostbyname_ex through
    run_in_executor (intentionally avoiding an aiodns runtime
    dep for v1.5.0).
  * Port-80 HEAD probe, target locked to the order's domains,
    success on HTTP 200 OR 404, warns on egress timeout
    (corp egress policies routinely blackhole outbound 80 —
    fail-hard would be too noisy).
  * SSRF guard: probe refuses non-public IPs and surfaces the
    skip in the diagnostic result; IPv4-mapped IPv6 normalisation
    closes the `::ffff:169.254.169.254` cloud-metadata vector.
  * HAProxy routing presence check: matches the order's
    cluster_ids to a port-80 HTTP frontend.
  * ACME account validity check against `letsencrypt_accounts`.
  * Agent presence check (>=1 active agent in target cluster).
  * Every sub-check wrapped in a wall-clock timeout to bound
    impact on the API event loop.

RBAC: ssl.read for run, ssl.read for events. Per-user 5/min rate
limit on both run and rerun, backed by the (user_id, action,
created_at DESC) composite index.

Frontend (`frontend/src/components/ACMEAutomation.js`):
  * "Diagnose" button on every order row + the existing
    "stuck order" warning row.
  * Modal with two tabs:
    - Pre-flight Checks (Antd Table with status pills + Re-run
      buttons + humanized error banner)
    - Event Log (Antd Timeline with auto-tail polling, scroll-
      to-bottom, pause-on-hover)
  * Correlation IDs surfaced in error banners and individual
    check fail details for backend-log lookup.

------------------------------------------------------------------
HEADLINE FEATURE B — Site Setup Wizard (Issue #14)
------------------------------------------------------------------
A single guided flow that creates a Backend + Servers + HTTP
Frontend (and optional HTTPS Frontend) in one atomic transaction.

Endpoints (`backend/routers/site_wizard.py`):
  POST /api/site-wizard/preview     — diff-preview the changeset
  POST /api/site-wizard/create      — atomic execute
  POST /api/site-wizard/reject      — clean rollback (including
                                       any wizard_staged ACME
                                       orders)
  GET  /api/site-wizard/drafts      — draft persistence
  PUT  /api/site-wizard/drafts/{id} — save/update
  DELETE /api/site-wizard/drafts/{id}

Feature surface:
  * One screen captures both backend (mode + servers) AND
    frontend (http + optional https + SSL mode) inputs.
  * SSL modes: ACME (new order, HTTP-01 only for v1.5.0),
    Upload (existing PEM), Existing (link to a stored cert),
    or None.
  * ACME-staged path: wizard_staged_until watermark on the
    `letsencrypt_orders` row defers finalisation until agent
    confirmation; per-mode reject cleanly cancels and rolls
    back the staged order.
  * Live diff preview against the cluster's current generated
    config (renderer-evolution noise stripped — track-sc<N>
    dedup, per-server cookie strip, defaults-cookie
    inheritance, listen-block flattening).
  * Draft persistence with PEM stripped at save time (private
    keys never round-trip through the drafts table).
  * Per-cluster multi-tenancy: drafts and wizard_staged orders
    are isolated to the creating user's cluster scope.

Frontend (`frontend/src/components/SiteWizard.js`):
  * 4-step Antd Steps flow: Backend → Frontend → SSL → Review.
  * Render the live diff preview inline before commit.
  * Antd Form-level validation mirrors backend Pydantic
    validators (numeric bounds, HAProxy reserved keywords, ALPN
    consistency, IPv6 scope-id, domain regex, server name
    dedup).

------------------------------------------------------------------
AUDIT CAMPAIGN — Rounds 1 → 22 (Bulgu #1#82)
------------------------------------------------------------------
v1.5.0 includes 22 adversarial review passes. Each round produced
its own commit set in the corporate development line; this squash
collapses those into the v1.5.0 release artefact. Highlights:

  Round 1-4   Site Wizard core: dry-run parity, single-line
              value injection guard, ACL -f pattern-file block,
              SSL parity, timeout regex, form-state pin.
  Round 5-7   defaults-cookie inheritance, server-named-cookie
              guard, fe/be mode mismatch, duplicate server
              names, health_check_uri + server_address
              validators.
  Round 8-10  cookie_name / cookie_options newline-injection
              guard, dry-run parity (round 9), TCP-mode HTTP-only
              feature blockers.
  Round 11    SSL name path traversal + health-check >= 1.
  Round 12-13 SSL & ACME deep dive (Bulgu #23-#32).
  Round 14    single-line value injection (Bulgu #33).
  Round 15-17 ACME multi-tenant UX, numeric bounds, HAProxy
              reserved keywords, ALPN/TLS consistency,
              all-backup, multi-domain & multi-user enterprise
              edges, drain/HSTS/post-completion (Bulgu
              #34-#53).
  Round 18-21 concurrency, agent state, TCP-mode HTTP-only,
              list size caps, IPv6 scope-id, preview account
              validation, TCP backend + balance uri reject
              (Bulgu #54-#61).
  Round 22    FE error visibility + 3x stale-data lockouts,
              referential integrity + cascade safety,
              authentication & authorization, multi-cluster
              isolation, apply_pending_changes concurrency,
              script injection + bulk import multi-tenancy,
              prefix-stripped signature comparison
              (Bulgu #62-#82).

------------------------------------------------------------------
NO CORPORATE-SPECIFIC ARTIFACTS
------------------------------------------------------------------
This squash deliberately sanitises corporate hostnames, container
registry references, and TLS secret names into generic
placeholders (`your-registry.example.com/your-org`,
`haproxy-openmanager*.example.com`, `wildcard-tls`,
`taylanbakircioglu/haproxy-openmanager-*`) so the public artefact
contains no internal infrastructure detail. Pilot / development
history that retained those values stays in the corporate fork
and is NOT part of this commit.
2026-05-14 00:04:19 +03:00

188 lines
8.1 KiB
Python

from pydantic import BaseModel, field_validator, model_validator
from typing import Optional, List, Dict, Any
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
class SSLCertificateCreate(BaseModel):
name: str
certificate_content: str # PEM format certificate
private_key_content: Optional[str] = None # PEM format private key (optional for server SSL)
chain_content: Optional[str] = None # PEM format certificate chain (optional)
cluster_ids: Optional[List[int]] = None # List of cluster IDs for multi-cluster support
is_global: bool = False # True for global SSL certificates
usage_type: str = "frontend" # "frontend" or "server" - determines if private key is required
@field_validator('usage_type')
@classmethod
def validate_usage_type(cls, v):
if v not in ['frontend', 'server']:
raise ValueError('usage_type must be either "frontend" or "server"')
return v
@field_validator('name')
@classmethod
def validate_name_no_path_traversal(cls, v):
"""Bulgu #21 (round-11 audit): SSL certificate `name` is interpolated
into the on-disk certificate path:
cert_path = f"/etc/ssl/haproxy/{ssl_cert['name']}.pem"
and emitted into the rendered HAProxy config. The agent then
runs `mv "$temp_cert_file" "$cert_file_path"` as root, which
means a name like '../../tmp/evil' resolves to '/tmp/evil.pem'
and lets an operator with ssl.create permission overwrite
arbitrary `*.pem` files on the agent host. The trailing `.pem`
suffix mitigates common exploit paths (cron.d, profile.d,
authorized_keys) but is defense-only — the right fix is to
constrain `name` at the API boundary. Mirror SSLChoice's
constraints so the wizard and direct API give the same answer.
"""
if v is None:
return v
stripped = v.strip()
if not stripped:
raise ValueError('SSL certificate name must not be empty')
if stripped != v:
raise ValueError('SSL certificate name must not contain leading/trailing whitespace')
if len(stripped) > 200:
raise ValueError('SSL certificate name must be 200 characters or fewer')
import re as _re
if not _re.match(r'^[A-Za-z0-9_.-]+$', stripped):
raise ValueError(
f'SSL certificate name={v!r} contains forbidden characters '
'— only letters, digits, underscore, hyphen, and dot are '
'allowed (the name is used as a filename component under '
'/etc/ssl/haproxy/).'
)
if '..' in stripped:
raise ValueError(f'SSL certificate name={v!r} must not contain ".." (path traversal)')
if stripped.startswith('.'):
raise ValueError(f'SSL certificate name={v!r} must not start with "." (hidden filename)')
if stripped.startswith('-'):
raise ValueError(f'SSL certificate name={v!r} must not start with "-" (CLI flag confusion)')
return stripped
@field_validator('certificate_content')
@classmethod
def validate_certificate(cls, v):
if not v or not v.strip():
raise ValueError('Certificate content is required')
# Basic PEM format check
v = v.strip()
if '-----BEGIN CERTIFICATE-----' not in v or '-----END CERTIFICATE-----' not in v:
raise ValueError('Certificate must be in PEM format')
return v
@model_validator(mode='after')
def validate_private_key_based_on_usage(self):
"""Private key is required for frontend SSL, optional for server SSL"""
usage_type = self.usage_type
private_key = self.private_key_content
if usage_type == 'frontend':
# Frontend SSL requires private key
if not private_key or not private_key.strip():
raise ValueError('Private key is required for frontend SSL certificates')
# Basic PEM format check
private_key = private_key.strip()
if '-----BEGIN' not in private_key or '-----END' not in private_key:
raise ValueError('Private key must be in PEM format')
elif usage_type == 'server':
# Server SSL - private key is optional
if private_key and private_key.strip():
# If provided, validate format
private_key = private_key.strip()
if '-----BEGIN' not in private_key or '-----END' not in private_key:
raise ValueError('Private key must be in PEM format')
return self
@field_validator('chain_content')
@classmethod
def validate_chain(cls, v):
if v and v.strip():
# Basic PEM format check for chain
if '-----BEGIN CERTIFICATE-----' not in v or '-----END CERTIFICATE-----' not in v:
raise ValueError('Certificate chain must be in PEM format')
return v
class SSLCertificateUpdate(BaseModel):
name: Optional[str] = None
certificate_content: Optional[str] = None
private_key_content: Optional[str] = None
chain_content: Optional[str] = None
cluster_id: Optional[int] = None
usage_type: Optional[str] = None # "frontend" or "server"
@field_validator('usage_type')
@classmethod
def validate_usage_type(cls, v):
if v is not None and v not in ['frontend', 'server']:
raise ValueError('usage_type must be either "frontend" or "server"')
return v
# Bulgu #63 (round-22 audit) — the path-traversal guard previously
# lived here as a strict Pydantic validator (Bulgu #21, round-11).
# On UPDATE the manual SSL UI re-sends the existing `name` along
# with the field the operator actually edited (usage_type /
# cluster_id / content). If the existing certificate name was
# imported BEFORE Bulgu #21 landed (or by a different upload path)
# and contained a now-rejected character — e.g. legacy uploads
# with `cert (1).pem`, `*.example.com`, `client cert.pem`, or
# `wildcard.example.com` if dot was not yet escaped — every PUT
# would 400 even though the operator was only changing usage
# type or attaching the cert to another cluster. That mirrors
# the Bulgu #62 lockout on frontends.
#
# The guard was moved into `routers/ssl.py::_assert_safe_cert_name`
# and is now invoked by the create + update routes:
# * create: strict — rejects unsafe names outright (Bulgu #21
# contract preserved for fresh inserts).
# * update: skipped if `name` is unchanged from the DB row;
# enforced strictly only when the operator actually renames
# the certificate.
class SSLCertificate(BaseModel):
id: int
name: str
domain: str # Auto-parsed from certificate
all_domains: List[str] = [] # All domains from SAN + CN
certificate_content: str
private_key_content: Optional[str] = None # Optional for server SSL
chain_content: Optional[str] = None
expiry_date: Optional[datetime] = None # Auto-parsed from certificate
issuer: Optional[str] = None # Auto-parsed from certificate
status: str # 'valid', 'expiring_soon', 'expired', 'invalid'
days_until_expiry: int = 0
fingerprint: Optional[str] = None
cluster_id: Optional[int] = None
usage_type: str = "frontend" # "frontend" or "server"
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None
has_pending_config: bool = False
# Certificate details for UI display
certificate_info: Optional[Dict[str, Any]] = None
class SSLCertificateResponse(BaseModel):
"""Response model for SSL certificate API endpoints"""
id: int
name: str
domain: str
all_domains: List[str] = []
status: str
days_until_expiry: int = 0
expiry_date: Optional[datetime] = None
issuer: Optional[str] = None
fingerprint: Optional[str] = None
cluster_id: Optional[int] = None
usage_type: str = "frontend" # "frontend" or "server"
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None
has_pending_config: bool = False