Files
haproxy-openmanager/backend/auth_middleware.py
T
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

339 lines
12 KiB
Python

from fastapi import HTTPException, status
from typing import Optional, Dict, Any
from jose import jwt
import logging
from datetime import datetime, timedelta
from config import JWT_SECRET_KEY, JWT_ALGORITHM
from database.connection import get_database_connection, close_database_connection
logger = logging.getLogger(__name__)
async def get_current_user_from_token(authorization: Optional[str] = None) -> Optional[Dict[str, Any]]:
"""Extract and validate user from JWT token"""
if not authorization:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authorization header missing"
)
try:
# Remove 'Bearer ' prefix if present
token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization
# Handle the case where frontend sends the string 'null' instead of null
if token == 'null' or token == 'undefined' or not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token: null or undefined"
)
# Debug logging for token format issues
logger.debug(f"Token received: {token[:50]}... (length: {len(token)})")
# Check if token has proper JWT format (3 segments separated by dots)
token_segments = token.split('.')
if len(token_segments) != 3:
logger.error(f"Invalid JWT format: token has {len(token_segments)} segments, expected 3")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=f"Invalid token format: expected 3 segments, got {len(token_segments)}"
)
# Decode JWT token
payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[JWT_ALGORITHM])
user_id = payload.get("sub") or payload.get("user_id") # Support both formats
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token: missing user ID"
)
# Get user from database
conn = await get_database_connection()
user = await conn.fetchrow("""
SELECT id, username, email, full_name, role, is_active, is_admin
FROM users
WHERE id = $1 AND is_active = TRUE
""", int(user_id))
await close_database_connection(conn)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not found or inactive"
)
return {
"id": user["id"],
"username": user["username"],
"email": user["email"],
"full_name": user["full_name"],
"role": user["role"],
"is_admin": user.get("is_admin", False)
}
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired"
)
except (jwt.JWTError, AttributeError) as e:
logger.error(f"JWT validation error: {e}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token"
)
except Exception as e:
logger.error(f"Auth middleware error: {e}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication failed"
)
async def get_current_user_from_token_no_exception(authorization: Optional[str] = None) -> Optional[Dict[str, Any]]:
"""
Get current user from JWT token without raising HTTPException.
Returns None if authentication fails. Used for optional authentication in agent endpoints.
"""
if not authorization:
return None
try:
# Remove 'Bearer ' prefix if present
token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization
# Handle the case where frontend sends the string 'null' instead of null
if token == 'null' or token == 'undefined' or not token:
return None
# Check if token has proper JWT format (3 segments separated by dots)
token_segments = token.split('.')
if len(token_segments) != 3:
logger.debug(f"Invalid JWT format: token has {len(token_segments)} segments, expected 3")
return None
# Decode JWT token
payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[JWT_ALGORITHM])
user_id = payload.get("sub") or payload.get("user_id") # Support both formats
if not user_id:
return None
# Get user from database
conn = await get_database_connection()
user = await conn.fetchrow("""
SELECT id, username, email, full_name, is_active, is_admin
FROM users
WHERE id = $1 AND is_active = TRUE
""", int(user_id))
await close_database_connection(conn)
if not user:
return None
return {
"id": user["id"],
"username": user["username"],
"email": user["email"],
"full_name": user["full_name"],
"is_admin": user.get("is_admin", False)
}
except Exception as e:
logger.debug(f"JWT validation failed (no exception): {e}")
return None
async def validate_agent_api_key(api_key: Optional[str] = None) -> Optional[Dict[str, Any]]:
"""
Validate agent API key for secure agent authentication.
Returns agent info if valid, None otherwise.
"""
if not api_key:
return None
try:
conn = await get_database_connection()
# Check if API key exists and is valid
agent = await conn.fetchrow("""
SELECT id, name, pool_id, enabled, api_key_expires_at
FROM agents
WHERE api_key = $1 AND enabled = TRUE
""", api_key)
await close_database_connection(conn)
if not agent:
logger.warning(f"Invalid agent API key attempted: {api_key[:10]}...")
return None
# Check if API key has expired
if agent['api_key_expires_at'] and agent['api_key_expires_at'] < datetime.utcnow():
logger.warning(f"Expired agent API key for agent: {agent['name']}")
return None
return {
"id": agent["id"],
"name": agent["name"],
"pool_id": agent["pool_id"],
"type": "agent" # Mark as agent authentication
}
except Exception as e:
logger.error(f"Agent API key validation error: {e}")
return None
async def get_user_permissions(user_id: int) -> Dict[str, Dict[str, bool]]:
"""
Get user permissions from their assigned roles
Returns dict in format: {resource: {action: bool}}
"""
try:
conn = await get_database_connection()
# Get user roles and their permissions
user_roles = await conn.fetch("""
SELECT r.permissions
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = $1 AND r.is_active = true
""", user_id)
await close_database_connection(conn)
# Combine all permissions from all roles
combined_permissions = set()
for role in user_roles:
if role['permissions']:
import json
permissions = json.loads(role['permissions']) if isinstance(role['permissions'], str) else role['permissions']
if isinstance(permissions, list):
combined_permissions.update(permissions)
# Convert flat permission list to nested dict format
permissions_dict = {}
for permission in combined_permissions:
if '.' in permission:
resource, action = permission.split('.', 1)
if resource not in permissions_dict:
permissions_dict[resource] = {}
permissions_dict[resource][action] = True
return permissions_dict
except Exception as e:
logger.error(f"Error getting user permissions for user {user_id}: {e}")
return {}
async def check_user_permission(
user_id: int,
resource: str,
action: str,
*,
current_user: Optional[Dict[str, Any]] = None,
) -> bool:
"""
Check if user has specific permission.
R18c round 7 (Bulgu 1): system-wide admin bypass. A user with
``users.is_admin = TRUE`` is the canonical super-admin and MUST pass
every granular permission check, regardless of the role they're
attached to. Otherwise enterprise admins were getting 403s on
composite endpoints (e.g. wizard CREATE) when their role's
``permissions`` JSONB didn't enumerate every individual action
(backend.create, frontend.create, ssl.create, apply.execute).
Two short-circuit paths:
1. Caller already has ``current_user`` resolved (typical FastAPI
endpoint) — pass it via the kwarg-only ``current_user`` to skip
the DB roundtrip entirely.
2. Caller doesn't have it — we run a single
``SELECT is_admin FROM users WHERE id=$1 AND is_active=TRUE``
before falling back to the role-based permission lookup.
Backward-compat: positional 3-arg signature preserved.
"""
try:
# Path 1: caller-provided current_user dict
if current_user is not None and current_user.get("is_admin") is True:
logger.debug(
"Admin bypass for %s.%s (user_id=%s, via current_user)",
resource, action, user_id,
)
return True
# Path 2: cheap is_admin lookup before role-permissions join
conn = await get_database_connection()
try:
row = await conn.fetchrow(
"SELECT is_admin FROM users WHERE id = $1 AND is_active = TRUE",
int(user_id),
)
finally:
await close_database_connection(conn)
if row and row.get("is_admin") is True:
logger.debug(
"Admin bypass for %s.%s (user_id=%s, via DB lookup)",
resource, action, user_id,
)
return True
permissions = await get_user_permissions(user_id)
return permissions.get(resource, {}).get(action, False)
except Exception as e:
logger.error(f"Error checking permission for user {user_id}: {e}")
return False
async def get_current_user_with_permissions(authorization: Optional[str] = None) -> Dict[str, Any]:
"""
Get current user with their permissions included
"""
user = await get_current_user_from_token(authorization)
permissions = await get_user_permissions(user["id"])
return {
**user,
"permissions": permissions
}
def require_permission(resource: str, action: str):
"""
Decorator to require specific permission for endpoint access
"""
def decorator(func):
import functools
@functools.wraps(func)
async def wrapper(*args, **kwargs):
# Extract authorization header from kwargs or function signature
authorization = kwargs.get('authorization')
if not authorization:
# Try to find authorization in function arguments
import inspect
sig = inspect.signature(func)
for param_name, param in sig.parameters.items():
if param_name == 'authorization' and param_name in kwargs:
authorization = kwargs[param_name]
break
# Get current user
user = await get_current_user_from_token(authorization)
# Check permission
has_permission = await check_user_permission(user["id"], resource, action)
if not has_permission:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"Insufficient permissions: {resource}.{action} required"
)
# Add user to kwargs for endpoint use
kwargs['current_user'] = user
return await func(*args, **kwargs)
return wrapper
return decorator