feat(manager): SCAN inventory, pagination, request limits, graceful shutdown

- Inventory nodes/servers use cursor SCAN plus MGET batches instead of KEYS
  and per-key GET; a Redis error returns 503 instead of an empty fleet.
- GET /blacklist no longer writes: the legacy metadata backfill moved to a
  one-shot startup task. The list endpoint paginates (limit 100, max 1000,
  offset max 10000) instead of dumping SMEMBERS.
- Router-wide DefaultBodyLimit 8 KiB and TimeoutLayer 10 s; every mutating
  endpoint is rate limited (30/min per client IP) with idle eviction, and the
  login limiter resets on success. reason > 256 bytes and malformed targets
  are rejected with 400.
- SIGTERM/Ctrl-C trigger graceful shutdown; the heartbeat and eviction tasks
  are cancelled and joined so in-flight ban edits reach Redis.
- MANAGER_TRUSTED_PROXIES enables X-Forwarded-For (right-to-left walk) only
  for configured proxy networks; otherwise the TCP peer is used.
- tests/manager_api.rs covers 401/400/413/429/503 and CORS preflight through
  the real router; manager_security gains limiter and proxy-IP tests.
This commit is contained in:
Loki 2026-11-16 12:36:29 +01:00
parent 324ef12b27
commit e456cb583f
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
21 changed files with 1377 additions and 180 deletions

View file

@ -1,13 +1,17 @@
//! `GET /api/v1/nodes`: registered edge nodes from the Redis heartbeat keys.
#[cfg(feature = "store-redis")]
use crate::manager::AppState;
#[cfg(feature = "store-redis")]
use axum::{Json, extract::State};
#[cfg(feature = "store-redis")]
use redis::AsyncCommands;
use axum::{Json, extract::State, http::StatusCode};
use serde::{Deserialize, Serialize};
#[cfg(feature = "store-redis")]
use std::sync::Arc;
/// SCAN pattern of node records.
#[cfg(feature = "store-redis")]
const NODES_PATTERN: &str = "rampart:nodes:*";
#[derive(Debug, Serialize, Deserialize)]
pub struct NodeInfo {
pub id: String,
@ -22,27 +26,24 @@ pub struct NodesResponse {
pub nodes: Vec<NodeInfo>,
}
/// Lists the fleet with SSCAN-style iteration and batched reads.
///
/// # Errors
/// Returns 503 when Redis is unreachable or a command fails; an empty `nodes`
/// array then always means a healthy but empty fleet.
#[cfg(feature = "store-redis")]
pub async fn list_nodes(State(state): State<Arc<AppState>>) -> Json<NodesResponse> {
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
Ok(c) => c,
Err(_) => return Json(NodesResponse { nodes: vec![] }),
};
let keys: Vec<String> = match redis::cmd("KEYS").arg("rampart:nodes:*").query_async(&mut conn).await {
Ok(k) => k,
Err(_) => return Json(NodesResponse { nodes: vec![] }),
};
let mut nodes = Vec::with_capacity(keys.len());
for key in &keys {
let raw: Option<String> = conn.get(key).await.unwrap_or(None);
if let Some(json) = raw
&& let Ok(node) = serde_json::from_str::<NodeInfo>(&json)
{
nodes.push(node);
}
}
Json(NodesResponse { nodes })
pub async fn list_nodes(
State(state): State<Arc<AppState>>,
) -> Result<Json<NodesResponse>, (StatusCode, Json<serde_json::Value>)> {
let mut conn = state
.redis_client
.get_multiplexed_async_connection()
.await
.map_err(|error| super::unavailable(&error))?;
let values = super::scan_values(&mut conn, NODES_PATTERN)
.await
.map_err(|error| super::unavailable(&error))?;
Ok(Json(NodesResponse {
nodes: super::decode_all(&values),
}))
}