Fix undefined names and config router imports / task status update

This commit is contained in:
2026-04-14 13:53:39 +02:00
parent 09c764cebc
commit 6b436dc354
6 changed files with 573 additions and 167 deletions

View File

@@ -51,7 +51,7 @@ def _not_found(name: str) -> HTTPException:
)
@router.get(
"/",
"",
response_model=ActionListResponse,
summary="List all available actions with active/inactive status",
)
@@ -179,7 +179,7 @@ async def update_action(
@router.post(
"/",
"",
response_model=ActionConfig,
status_code=status.HTTP_201_CREATED,
summary="Create a new user-defined action",

View File

@@ -2,9 +2,11 @@ from __future__ import annotations
from typing import Annotated
import structlog
from fastapi import APIRouter, HTTPException, Query, Request, status
from app.dependencies import AuthDep, DbDep, Fail2BanSocketDep, Fail2BanStartCommandDep
from app.exceptions import ConfigOperationError, JailOperationError
from app.models.config import (
Fail2BanLogResponse,
GlobalConfigResponse,
@@ -20,6 +22,8 @@ from app.models.config import (
from app.services import config_file_service, config_service, jail_service, log_service, setup_service
from app.utils.fail2ban_client import Fail2BanConnectionError
log: structlog.stdlib.BoundLogger = structlog.get_logger()
router: APIRouter = APIRouter(tags=["Config Misc"])
@@ -29,6 +33,13 @@ def _bad_gateway(exc: Exception) -> HTTPException:
detail=f"Cannot reach fail2ban: {exc}",
)
def _bad_request(message: str) -> HTTPException:
return HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=message,
)
@router.get(
"/global",
response_model=GlobalConfigResponse,
@@ -286,8 +297,6 @@ async def get_map_color_thresholds(
:class:`~app.models.config.MapColorThresholdsResponse` with
current thresholds.
"""
from app.services import setup_service
high, medium, low = await setup_service.get_map_color_thresholds(db)
return MapColorThresholdsResponse(
threshold_high=high,
@@ -298,6 +307,7 @@ async def get_map_color_thresholds(
@router.put(
"/map-color-thresholds",
response_model=MapColorThresholdsResponse,
@@ -324,8 +334,6 @@ async def update_map_color_thresholds(
HTTPException: 400 if validation fails (thresholds not
properly ordered).
"""
from app.services import setup_service
try:
await setup_service.set_map_color_thresholds(
db,
@@ -382,7 +390,7 @@ async def get_fail2ban_log(
"""
try:
return await config_service.read_fail2ban_log(socket_path, lines, filter)
except config_service.ConfigOperationError as exc:
except ConfigOperationError as exc:
raise _bad_request(str(exc)) from exc
except Fail2BanConnectionError as exc:
raise _bad_gateway(exc) from exc

View File

@@ -56,7 +56,7 @@ def _not_found(name: str) -> HTTPException:
)
@router.get(
"/",
"",
response_model=FilterListResponse,
summary="List all available filters with active/inactive status",
)
@@ -207,7 +207,7 @@ async def update_filter(
@router.post(
"/",
"",
response_model=FilterConfig,
status_code=status.HTTP_201_CREATED,
summary="Create a new user-defined filter",

View File

@@ -95,7 +95,7 @@ def _action_not_found(name: str) -> HTTPException:
)
@router.get(
"/",
"",
response_model=JailConfigListResponse,
summary="List configuration for all active jails",
)
@@ -151,6 +151,29 @@ async def get_inactive_jails(
return await jail_config_service.list_inactive_jails(config_dir, socket_path)
@router.get(
"/pending-recovery",
response_model=PendingRecovery | None,
summary="Return active crash-recovery record if one exists",
)
async def get_pending_recovery(
_auth: AuthDep,
pending_recovery: PendingRecoveryDep,
) -> PendingRecovery | None:
"""Return the current :class:`~app.models.config.PendingRecovery` record.
A non-null response means fail2ban crashed shortly after a jail activation
and the user should be offered a rollback option. Returns ``null`` (HTTP
200 with ``null`` body) when no recovery is pending.
Args:
request: FastAPI request object.
_auth: Validated session.
Returns:
:class:`~app.models.config.PendingRecovery` or ``None``.
"""
return pending_recovery
@router.get(
@@ -526,35 +549,6 @@ async def validate_jail(
raise _bad_request(str(exc)) from exc
@router.get(
"/pending-recovery",
response_model=PendingRecovery | None,
summary="Return active crash-recovery record if one exists",
)
async def get_pending_recovery(
_auth: AuthDep,
pending_recovery: PendingRecoveryDep,
) -> PendingRecovery | None:
"""Return the current :class:`~app.models.config.PendingRecovery` record.
A non-null response means fail2ban crashed shortly after a jail activation
and the user should be offered a rollback option. Returns ``null`` (HTTP
200 with ``null`` body) when no recovery is pending.
Args:
request: FastAPI request object.
_auth: Validated session.
Returns:
:class:`~app.models.config.PendingRecovery` or ``None``.
"""
return pending_recovery
@router.post(
"/{name}/rollback",
response_model=RollbackResponse,