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:
parent
324ef12b27
commit
e456cb583f
21 changed files with 1377 additions and 180 deletions
|
|
@ -1,14 +1,17 @@
|
|||
//! Rampart manager daemon: HTTP API in front of the redis state store.
|
||||
|
||||
use axum::{
|
||||
Router, middleware,
|
||||
routing::{get, post},
|
||||
};
|
||||
use dashmap::DashMap;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use rampart::manager::api;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use rampart::manager::sync;
|
||||
use rampart::manager::{AppState, api, auth, security};
|
||||
use rampart::manager::{
|
||||
AppState, SharedState,
|
||||
api::{build_router, guard},
|
||||
security,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::watch;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -39,7 +42,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
let api_password = std::env::var("API_PASSWORD").map_err(|_| anyhow::anyhow!("API_PASSWORD must be set"))?;
|
||||
security::validate_api_password(&api_password)?;
|
||||
|
||||
let state = Arc::new(AppState {
|
||||
let state: SharedState = Arc::new(AppState {
|
||||
#[cfg(feature = "store-redis")]
|
||||
redis_client,
|
||||
jwt_secret,
|
||||
|
|
@ -48,41 +51,77 @@ async fn main() -> anyhow::Result<()> {
|
|||
api_password,
|
||||
login_limiter: DashMap::new(),
|
||||
});
|
||||
let guard = Arc::new(guard::RequestGuard::from_env()?);
|
||||
|
||||
// Background tasks share one shutdown signal, so SIGTERM stops them together
|
||||
// with the HTTP server instead of leaving detached tasks behind.
|
||||
let (shutdown_tx, shutdown_rx) = watch::channel(false);
|
||||
#[cfg(feature = "store-redis")]
|
||||
tokio::spawn(sync::heartbeat::start_heartbeat_check(state.clone()));
|
||||
|
||||
let public = Router::new()
|
||||
.route("/api/v1/health", get(api::inventory::health::health_check))
|
||||
.route("/api/v1/auth/login", post(api::auth::login));
|
||||
|
||||
// Redis-backed routes only exist together with the feature; the manager bails
|
||||
// out above without it, so 501 stubs here would be unreachable code.
|
||||
let protected = Router::new();
|
||||
let heartbeat = spawn_heartbeat(state.clone(), shutdown_rx.clone());
|
||||
let eviction = guard::spawn_eviction(state.clone(), guard.clone(), shutdown_rx.clone());
|
||||
// One-shot legacy migration; losing it on shutdown is harmless because the
|
||||
// read path no longer depends on the metadata being present.
|
||||
#[cfg(feature = "store-redis")]
|
||||
let protected = protected
|
||||
.route("/api/v1/servers", get(api::inventory::servers::list_servers))
|
||||
.route(
|
||||
"/api/v1/blacklist",
|
||||
get(api::blacklist::list_blacklist)
|
||||
.post(api::blacklist::add_blacklist)
|
||||
.delete(api::blacklist::remove_blacklist),
|
||||
)
|
||||
.route("/api/v1/nodes", get(api::inventory::nodes::list_nodes));
|
||||
let protected = protected.route_layer(middleware::from_fn_with_state(state.clone(), auth::auth_middleware));
|
||||
tokio::spawn(api::blacklist::backfill_legacy_metadata(state.clone()));
|
||||
|
||||
let cors_policy = security::CorsPolicy::parse(std::env::var("CORS_ORIGIN").ok().as_deref())?;
|
||||
tracing::info!("CORS policy: {cors_policy:?}");
|
||||
|
||||
let mut app = Router::new().merge(public).merge(protected).with_state(state);
|
||||
if let Some(cors) = cors_policy.cors_layer() {
|
||||
app = app.layer(cors);
|
||||
}
|
||||
|
||||
let app = build_router(state, guard, cors_policy.cors_layer());
|
||||
let addr = "0.0.0.0:8080";
|
||||
tracing::info!("Manager API listening on {addr}");
|
||||
let listener = tokio::net::TcpListener::bind(addr).await?;
|
||||
tracing::info!("Manager API listening on {addr}");
|
||||
let app = app.into_make_service_with_connect_info::<std::net::SocketAddr>();
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
// Graceful shutdown drains in-flight requests, so a ban removal that already
|
||||
// reached a handler is written to Redis before the process exits.
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await?;
|
||||
|
||||
let _ = shutdown_tx.send(true);
|
||||
let _ = eviction.await;
|
||||
#[cfg(feature = "store-redis")]
|
||||
let _ = heartbeat.await;
|
||||
tracing::info!("manager stopped");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Spawns the heartbeat check loop and stops it on the shared shutdown signal.
|
||||
#[cfg(feature = "store-redis")]
|
||||
fn spawn_heartbeat(state: SharedState, mut shutdown: watch::Receiver<bool>) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
tokio::select! {
|
||||
() = sync::heartbeat::start_heartbeat_check(state) => {},
|
||||
_ = shutdown.changed() => {},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolves on `SIGTERM` or Ctrl-C, whichever arrives first.
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
if let Err(error) = tokio::signal::ctrl_c().await {
|
||||
tracing::error!("failed to listen for Ctrl-C: {error}");
|
||||
}
|
||||
};
|
||||
#[cfg(unix)]
|
||||
let terminate = async {
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
Ok(mut signal) => {
|
||||
signal.recv().await;
|
||||
},
|
||||
Err(error) => {
|
||||
tracing::error!("failed to listen for SIGTERM: {error}");
|
||||
std::future::pending::<()>().await;
|
||||
},
|
||||
}
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
tokio::select! {
|
||||
() = ctrl_c => {},
|
||||
() = terminate => {},
|
||||
}
|
||||
tracing::info!("shutdown signal received: draining in-flight requests");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue