feat(xdp): auto-ban from ringbuf events; fix feature gating, exit path, pow flake
- xdp: real 24-byte xdp_event parsing (fixes LE byte-swap of src IP), opt-in [xdp] auto_ban (RATE_LIMIT always, CONN_DROP at fails>=threshold; EVENT_BAN/POLICY_DROP excluded by design), rampart_xdp_autobans_total; wired in app before load(); 10 tests in tests/xdp_events.rs - build: --no-default-features compiles — redis paths cfg-gated behind store-redis, manager fail-fasts without it; CI gates the config now - app: RunExit enum replaces process::exit in lib; single exit site in main - tests: seed PoW roundtrip token (was ~1/16 flaky; 50 pre-fix fails -> 0) - docs: TODO statuses refreshed (round 2)
This commit is contained in:
parent
bf6d34c882
commit
f85ea6c380
23 changed files with 714 additions and 50 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use crate::config::Config;
|
||||
use crate::engine::subnet::tracker::SubnetTracker;
|
||||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::protocol::ProtocolRegistry;
|
||||
use crate::xdp::XdpFilter;
|
||||
#[cfg(feature = "xdp")]
|
||||
|
|
@ -10,7 +11,6 @@ use std::collections::HashSet;
|
|||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
|
||||
/// Собирает реестр протоколов из скомпилированных реализаций.
|
||||
|
|
@ -44,7 +44,10 @@ fn register_http(registry: &mut ProtocolRegistry, config: &Config) {
|
|||
pub(crate) fn start_xdp(
|
||||
config: &Arc<Config>,
|
||||
shutdown_rx: &watch::Receiver<bool>,
|
||||
blacklist: &Arc<Blacklist>,
|
||||
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::xdp::XdpMetrics;
|
||||
|
||||
if !config.xdp.enabled {
|
||||
|
|
@ -57,6 +60,10 @@ pub(crate) fn start_xdp(
|
|||
let mut guard = shared.lock().expect("xdp lock poisoned");
|
||||
let globals = XdpGlobals::from_config(&config.xdp)?;
|
||||
guard.set_globals(globals);
|
||||
// Порядок load-bearing: `load()` захватывает blacklist и политику
|
||||
// авто-бана в ringbuf-колбэк (XdpFilter::load -> build_ringbuf),
|
||||
// поэтому set_autoban обязан отработать ДО load(), а не после.
|
||||
guard.set_autoban(blacklist.clone(), &config.xdp);
|
||||
guard.load()?;
|
||||
}
|
||||
let xdp_metrics = XdpMetrics::register()?;
|
||||
|
|
@ -87,6 +94,7 @@ pub(crate) fn start_xdp(
|
|||
pub(crate) fn start_xdp(
|
||||
_config: &Arc<Config>,
|
||||
_shutdown_rx: &watch::Receiver<bool>,
|
||||
_blacklist: &Arc<Blacklist>,
|
||||
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
|
@ -107,7 +115,23 @@ pub(crate) fn start_subnet_tracker(config: &Config) -> Option<Arc<SubnetTracker>
|
|||
}
|
||||
}
|
||||
|
||||
/// Store-фичи проверяются на старте edge-демона: конфиг, запрашивающий redis,
|
||||
/// в бинаре без `store-redis` должен упасть сразу, а не молча потерять
|
||||
/// blacklist-sync (sync-задача в runtime.rs живёт под тем же cfg).
|
||||
/// Точка вызова — `build_whitelist`: ближайший `?`-хук, принимающий конфиг
|
||||
/// до бинда listener'а (сам runtime.rs вне правок этой задачи).
|
||||
fn ensure_store_features(config: &Config) -> anyhow::Result<()> {
|
||||
#[cfg(not(feature = "store-redis"))]
|
||||
if config.store.redis_url.as_deref().is_some_and(|url| !url.is_empty()) {
|
||||
anyhow::bail!("[store].redis_url requires a redis-backed store: built without store-redis feature");
|
||||
}
|
||||
#[cfg(feature = "store-redis")]
|
||||
let _ = config;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpAddr>>> {
|
||||
ensure_store_features(config)?;
|
||||
let mut set = HashSet::with_capacity(config.whitelist.len());
|
||||
for entry in &config.whitelist {
|
||||
let ip: IpAddr = entry
|
||||
|
|
@ -117,3 +141,51 @@ pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpA
|
|||
}
|
||||
Ok(Arc::new(set))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn config_with_redis() -> Config {
|
||||
Config {
|
||||
store: crate::config::StoreConfig {
|
||||
redis_url: Some("redis://127.0.0.1:6379/0".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "store-redis"))]
|
||||
#[test]
|
||||
fn redis_config_without_feature_fails_fast() {
|
||||
let err = ensure_store_features(&config_with_redis()).expect_err("must fail without store-redis");
|
||||
assert!(err.to_string().contains("built without store-redis feature"));
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "store-redis"))]
|
||||
#[test]
|
||||
fn empty_store_config_passes_without_feature() {
|
||||
assert!(ensure_store_features(&Config::default()).is_ok());
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
#[test]
|
||||
fn guard_is_noop_with_feature() {
|
||||
assert!(ensure_store_features(&config_with_redis()).is_ok());
|
||||
}
|
||||
|
||||
/// Wiring-тасовка XDP auto-ban: `start_xdp` обязан принимать `Arc<Blacklist>`
|
||||
/// (т.е. существовать в сигнатуре с blacklist) и при выключенном `[xdp]`
|
||||
/// не трогать ни ядро, ни blacklist. Порядок `set_autoban` -> `load()`
|
||||
/// внутри enabled-ветки без ядра непроверяем — см. комментарий на месте вызова.
|
||||
#[test]
|
||||
fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() {
|
||||
let (_tx, rx) = watch::channel(false);
|
||||
let blacklist = Arc::new(Blacklist::new());
|
||||
let started = start_xdp(&Arc::new(Config::default()), &rx, &blacklist)
|
||||
.expect("xdp disabled: no kernel work and no error");
|
||||
assert!(started.is_none());
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue