Issue #3 - Unbounded Query Results (OOM): - get_all_archived_history() now uses keyset pagination with bounded max_rows (50k default) - Added 'id' field to records from get_archived_history() and get_archived_history_keyset() - Protocol signature updated with page_size, max_rows, last_ban_id params Issue #7 - Docker Health Check Fails: - Added curl to Dockerfile.backend runtime image - HEALTHCHECK now uses 'curl -f http://localhost:8000/api/health' - compose.prod.yml: increased start_period to 40s, timeout to 10s - Frontend healthcheck proxies to backend /api/health Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
157 lines
4.9 KiB
Python
157 lines
4.9 KiB
Python
"""Config response mappers.
|
|
|
|
Convert domain models (from config_service) to response models (for HTTP API).
|
|
|
|
This is the mapping layer at the router boundary, ensuring the service layer
|
|
remains independent of HTTP response shapes.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.models.config import (
|
|
BantimeEscalation,
|
|
Fail2BanLogResponse,
|
|
FilterConfig,
|
|
FilterListResponse,
|
|
GlobalConfigResponse,
|
|
JailConfig,
|
|
JailConfigListResponse,
|
|
LogPreviewResponse,
|
|
MapColorThresholdsResponse,
|
|
RegexTestResponse,
|
|
ServiceStatusResponse,
|
|
)
|
|
from app.models.config_domain import (
|
|
DomainBantimeEscalation,
|
|
DomainFilterConfig,
|
|
DomainFilterList,
|
|
DomainGlobalConfig,
|
|
DomainJailConfig,
|
|
DomainJailConfigList,
|
|
DomainMapColorThresholds,
|
|
DomainRegexTest,
|
|
DomainServiceStatus,
|
|
)
|
|
from app.utils.pagination import create_pagination_metadata
|
|
|
|
|
|
def _map_domain_bantime_escalation(domain: DomainBantimeEscalation) -> BantimeEscalation:
|
|
"""Convert domain bantime escalation to response model."""
|
|
return BantimeEscalation(
|
|
increment=domain.increment,
|
|
factor=domain.factor,
|
|
formula=domain.formula,
|
|
multipliers=domain.multipliers,
|
|
max_time=domain.max_time,
|
|
rnd_time=domain.rnd_time,
|
|
overall_jails=domain.overall_jails,
|
|
)
|
|
|
|
|
|
def map_domain_jail_config_to_response(domain: DomainJailConfig) -> JailConfig:
|
|
"""Convert domain jail config to response model."""
|
|
return JailConfig(
|
|
name=domain.name,
|
|
ban_time=domain.ban_time,
|
|
max_retry=domain.max_retry,
|
|
find_time=domain.find_time,
|
|
fail_regex=domain.fail_regex,
|
|
ignore_regex=domain.ignore_regex,
|
|
log_paths=domain.log_paths,
|
|
date_pattern=domain.date_pattern,
|
|
log_encoding=domain.log_encoding,
|
|
backend=domain.backend,
|
|
use_dns=domain.use_dns,
|
|
prefregex=domain.prefregex,
|
|
actions=domain.actions,
|
|
bantime_escalation=(
|
|
_map_domain_bantime_escalation(domain.bantime_escalation)
|
|
if domain.bantime_escalation
|
|
else None
|
|
),
|
|
)
|
|
|
|
|
|
def map_domain_jail_config_list_to_response(
|
|
domain_list: DomainJailConfigList,
|
|
) -> JailConfigListResponse:
|
|
"""Convert domain jail config list to response model."""
|
|
return JailConfigListResponse(
|
|
items=[map_domain_jail_config_to_response(c) for c in domain_list.items],
|
|
total=domain_list.total,
|
|
)
|
|
|
|
|
|
def map_domain_global_config_to_response(domain: DomainGlobalConfig) -> GlobalConfigResponse:
|
|
"""Convert domain global config to response model."""
|
|
return GlobalConfigResponse(
|
|
log_level=domain.log_level,
|
|
log_target=domain.log_target,
|
|
db_purge_age=domain.db_purge_age,
|
|
db_max_matches=domain.db_max_matches,
|
|
)
|
|
|
|
|
|
def map_domain_service_status_to_response(
|
|
domain: DomainServiceStatus,
|
|
) -> ServiceStatusResponse:
|
|
"""Convert domain service status to response model."""
|
|
return ServiceStatusResponse(
|
|
online=domain.online,
|
|
version=domain.version or "",
|
|
jail_count=domain.jail_count,
|
|
total_bans=domain.total_bans,
|
|
total_failures=domain.total_failures,
|
|
log_level=domain.log_level or "UNKNOWN",
|
|
log_target=domain.log_target or "UNKNOWN",
|
|
)
|
|
|
|
|
|
def map_domain_map_color_thresholds_to_response(
|
|
domain: DomainMapColorThresholds,
|
|
) -> MapColorThresholdsResponse:
|
|
"""Convert domain map color thresholds to response model."""
|
|
return MapColorThresholdsResponse(
|
|
threshold_high=domain.threshold_high,
|
|
threshold_medium=domain.threshold_medium,
|
|
threshold_low=domain.threshold_low,
|
|
)
|
|
|
|
|
|
def map_domain_regex_test_to_response(domain: DomainRegexTest) -> RegexTestResponse:
|
|
"""Convert domain regex test to response model."""
|
|
return RegexTestResponse(
|
|
matched=domain.matched,
|
|
groups=domain.groups,
|
|
error=domain.error,
|
|
)
|
|
|
|
|
|
def map_domain_filter_config_to_response(domain: DomainFilterConfig) -> FilterConfig:
|
|
"""Convert domain filter config to response model."""
|
|
return FilterConfig(
|
|
name=domain.name,
|
|
filename=domain.filename,
|
|
before=domain.before,
|
|
after=domain.after,
|
|
variables=domain.variables or {},
|
|
prefregex=domain.prefregex,
|
|
failregex=domain.failregex or [],
|
|
ignoreregex=domain.ignoreregex or [],
|
|
maxlines=domain.maxlines,
|
|
datepattern=domain.datepattern,
|
|
journalmatch=domain.journalmatch,
|
|
active=domain.active,
|
|
used_by_jails=domain.used_by_jails or [],
|
|
source_file=domain.source_file,
|
|
has_local_override=domain.has_local_override,
|
|
)
|
|
|
|
|
|
def map_domain_filter_list_to_response(domain_list: DomainFilterList) -> FilterListResponse:
|
|
"""Convert domain filter list to response model."""
|
|
return FilterListResponse(
|
|
items=[map_domain_filter_config_to_response(f) for f in domain_list.items],
|
|
total=domain_list.total,
|
|
)
|