Readiness, Electrum status, audit browsing and logs (B-92, API-37, API-46, API-47, AUD-34, AUD-41, ARC-02, ARC-09, ARC-61, ELC-29, SEC-48)
- GET /health/ready (Electrum connected, header < 1800s, scheduler heartbeat < 30s) is the container healthcheck; the scheduler beats on every tick and draw-wait iteration - GET /admin/electrum and an Electrum indicator in /admin (server, header age, last rotation) - GET /admin/audit-log: before_id, event_type, round_id, user_id, from, to; the panel gets filters - registration writes user_registered in its transaction - the app's own logs also go to stdout; uvicorn trusts X-Forwarded-For from Caddy's fixed address only, so the access log shows the client's IP Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9520c80dcf
commit
575f1a5354
23 files changed
+478
-91
No files matched your search
+71
-3
@@ -1,7 +1,8 @@
|
||||
import json
|
||||
import secrets
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, Query, status
|
||||
from fastapi import APIRouter, Depends, Header, Query, Request, status
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -369,13 +370,54 @@ class AdminAuditLogResponse(BaseModel):
|
||||
created_at: str
|
||||
|
||||
|
||||
def _parse_instant(name: str, value: str | None) -> datetime | None:
|
||||
"""An ISO 8601 instant with an offset, as UTC; anything else is a 422 (API-37)."""
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
parsed = None
|
||||
if parsed is None or parsed.tzinfo is None:
|
||||
raise http_error(
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT,
|
||||
"invalid_request",
|
||||
"invalid request",
|
||||
errors=[{"loc": ["query", name], "msg": "an ISO 8601 instant with an offset is required", "type": "value_error"}],
|
||||
)
|
||||
return parsed.astimezone(timezone.utc)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/audit-log", response_model=list[AdminAuditLogResponse], dependencies=[Depends(require_admin)]
|
||||
)
|
||||
async def list_audit_log(
|
||||
session: AsyncSession = Depends(get_session), limit: int = Query(default=200, ge=1, le=500)
|
||||
session: AsyncSession = Depends(get_session),
|
||||
limit: int = Query(default=200, ge=1, le=500),
|
||||
before_id: int | None = Query(default=None),
|
||||
event_type: list[str] | None = Query(default=None),
|
||||
round_id: int | None = Query(default=None),
|
||||
user_id: int | None = Query(default=None),
|
||||
from_: str | None = Query(default=None, alias="from"),
|
||||
to: str | None = Query(default=None),
|
||||
) -> list[AdminAuditLogResponse]:
|
||||
entries = (await session.scalars(select(AuditLog).order_by(AuditLog.id.desc()).limit(limit))).all()
|
||||
"""B-92 (API-37): newest first, a page at a time, every filter optional and combined with
|
||||
"and". The next page asks for `before_id` equal to the last id received."""
|
||||
query = select(AuditLog).order_by(AuditLog.id.desc()).limit(limit)
|
||||
if before_id is not None:
|
||||
query = query.where(AuditLog.id < before_id)
|
||||
if event_type:
|
||||
query = query.where(AuditLog.event_type.in_(event_type))
|
||||
if round_id is not None:
|
||||
query = query.where(AuditLog.round_id == round_id)
|
||||
if user_id is not None:
|
||||
query = query.where(AuditLog.user_id == user_id)
|
||||
start, end = _parse_instant("from", from_), _parse_instant("to", to)
|
||||
if start is not None:
|
||||
query = query.where(AuditLog.created_at >= start)
|
||||
if end is not None:
|
||||
query = query.where(AuditLog.created_at < end)
|
||||
entries = (await session.scalars(query)).all()
|
||||
return [
|
||||
AdminAuditLogResponse(
|
||||
id=e.id,
|
||||
@@ -517,3 +559,29 @@ async def update_bug_report_status(
|
||||
username = user.username if user is not None else None
|
||||
|
||||
return _bug_report_response(report, username)
|
||||
|
||||
|
||||
class AdminElectrumResponse(BaseModel):
|
||||
connected: bool
|
||||
endpoint: str | None
|
||||
tip_height: int
|
||||
last_header_age_seconds: int | None
|
||||
fallback_count: int
|
||||
quorum_enforced: bool
|
||||
last_rotation: dict | None
|
||||
|
||||
|
||||
@router.get("/electrum", response_model=AdminElectrumResponse, dependencies=[Depends(require_admin)])
|
||||
async def electrum_status(request: Request) -> AdminElectrumResponse:
|
||||
"""B-92 (API-47, ELC-29): the connection to the network, for the panel's indicator."""
|
||||
listener = request.app.state.electrum_listener
|
||||
connected = listener.client is not None
|
||||
return AdminElectrumResponse(
|
||||
connected=connected,
|
||||
endpoint=str(listener.current_endpoint) if connected and listener.current_endpoint else None,
|
||||
tip_height=listener.tip_height,
|
||||
last_header_age_seconds=listener.last_header_age_seconds(),
|
||||
fallback_count=listener.fallback_count,
|
||||
quorum_enforced=listener.quorum_enforced,
|
||||
last_rotation=listener.last_rotation,
|
||||
)
|
||||
Reference in new issue
Block a user