fix: XDP unsafe loader + Redis sync; enforce 250-line/4-file layout limits

- xdp: CString for if_nametoindex (was UB), real detach via prog fd
  (fabricated borrow_raw(-1) silently never detached), SAFETY comments,
  saturating expiry math; +5 unit tests
- redis: real pubsub reconnect with exponential backoff (was sleep+return);
  KEYS -> SCAN in heartbeat sweep
- ci: cargo test --all-features, repo-gates job — module size gate
  (scripts/check_module_size.sh, ratchet baseline) + default-secrets grep
- refactor src/ to <=250 LOC/file, <=4 .rs/dir without behavior change;
  thin bins (rampart.rs 330 -> 6 LOC), new app/, subnet/, intel/,
  profile/, prefix/, challenge/, filter/, probe/, inventory/, metrics/, node/
- docs: TODO v5.0 (status refresh, new rules, findings backlog),
  README quickstart now matches real binaries
- verify: fmt/clippy -D warnings/test --all-features (164 tests)/clang XDP green
This commit is contained in:
Loki 2026-09-15 23:55:17 +02:00
parent d6bcae54c8
commit aa615a1141
Signed by: boba
GPG key ID: 253067914055423B
64 changed files with 2052 additions and 1408 deletions

View file

@ -1,8 +1,17 @@
use crate::manager::AppState;
use futures::StreamExt;
use redis::AsyncCommands;
use redis::ScanOptions;
use std::sync::Arc;
use tokio::time::{Duration, interval};
/// SCAN pattern for node records (blocking KEYS is forbidden under load).
const NODES_KEY_PATTERN: &str = "rampart:nodes:*";
/// SCAN COUNT hint per round.
const SCAN_BATCH: usize = 100;
/// Node is offline when the last heartbeat is older than this.
const NODE_TTL_SECS: i64 = 60;
pub async fn start_heartbeat_check(state: Arc<AppState>) {
let mut ticker = interval(Duration::from_secs(30));
loop {
@ -15,30 +24,99 @@ pub async fn start_heartbeat_check(state: Arc<AppState>) {
async fn check_nodes(state: &AppState) -> anyhow::Result<()> {
let mut conn = state.redis_client.get_multiplexed_async_connection().await?;
let keys: Vec<String> = redis::cmd("KEYS").arg("rampart:nodes:*").query_async(&mut conn).await?;
let keys = collect_node_keys(&mut conn).await?;
let now = chrono::Utc::now().timestamp();
for key in &keys {
let raw: Option<String> = conn.get(key).await?;
if let Some(json) = raw
&& let Ok(mut node) = serde_json::from_str::<serde_json::Value>(&json)
{
let hb = node["last_heartbeat"]
.as_str()
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
.map(|t| t.timestamp())
.unwrap_or(0);
if now - hb > 60 {
if let Some(obj) = node.as_object_mut() {
obj.insert("status".to_string(), serde_json::Value::String("offline".to_string()));
if let Ok(updated) = serde_json::to_string(&node) {
let _: () = conn.set(key.as_str(), updated).await.unwrap_or_default();
}
}
tracing::warn!("Node {key} is offline (heartbeat expired)");
}
}
let Some(updated) = raw.and_then(|json| offline_update(&json, now)) else {
continue;
};
tracing::warn!("Node {key} is offline (heartbeat expired)");
let _: () = conn.set(key.as_str(), updated).await.unwrap_or_default();
}
Ok(())
}
/// Incrementally iterates the keyspace via SCAN (cursor-based, non-blocking).
async fn collect_node_keys(conn: &mut redis::aio::MultiplexedConnection) -> anyhow::Result<Vec<String>> {
let opts = ScanOptions::default()
.with_pattern(NODES_KEY_PATTERN)
.with_count(SCAN_BATCH);
let mut iter = conn.scan_options::<String>(opts).await?;
let mut keys = Vec::new();
while let Some(key) = iter.next().await {
keys.push(key);
}
Ok(keys)
}
/// Pure decision: returns the updated node JSON with `status: "offline"` when
/// the RFC3339 `last_heartbeat` is older than `NODE_TTL_SECS` (a missing or
/// unparseable timestamp counts as expired, matching the previous behavior);
/// `None` when the node is fresh, unparsable, or not a JSON object.
fn offline_update(raw: &str, now: i64) -> Option<String> {
let mut node = serde_json::from_str::<serde_json::Value>(raw).ok()?;
let hb = node["last_heartbeat"]
.as_str()
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
.map(|t| t.timestamp())
.unwrap_or(0);
if now - hb <= NODE_TTL_SECS {
return None;
}
node.as_object_mut()?
.insert("status".to_string(), serde_json::Value::String("offline".to_string()));
serde_json::to_string(&node).ok()
}
#[cfg(test)]
mod tests {
use super::*;
fn rfc3339(ts: i64) -> String {
chrono::DateTime::from_timestamp(ts, 0)
.expect("in-range timestamp")
.to_rfc3339()
}
#[test]
fn expired_heartbeat_is_marked_offline() {
let now = 2_000_000_i64;
let raw = format!(
r#"{{"name":"edge1","last_heartbeat":"{}","status":"online"}}"#,
rfc3339(now - 120)
);
let updated = offline_update(&raw, now).expect("expired node should be marked offline");
assert!(updated.contains("offline"));
assert!(updated.contains("edge1"));
}
#[test]
fn fresh_heartbeat_is_left_untouched() {
let now = 2_000_000_i64;
let raw = format!(r#"{{"last_heartbeat":"{}","status":"online"}}"#, rfc3339(now - 5));
assert!(offline_update(&raw, now).is_none());
}
#[test]
fn missing_or_broken_timestamp_counts_as_expired() {
let now = 2_000_000_i64;
let updated = offline_update(r#"{"status":"online"}"#, now).expect("missing heartbeat counts as expired");
assert!(updated.contains("offline"));
let updated = offline_update(r#"{"last_heartbeat":"garbage","status":"online"}"#, now)
.expect("unparseable heartbeat counts as expired");
assert!(updated.contains("offline"));
}
#[test]
fn invalid_json_is_ignored() {
assert!(offline_update("not json", 2_000_000).is_none());
}
#[test]
fn node_key_pattern_targets_rampart_nodes() {
assert_eq!(NODES_KEY_PATTERN, "rampart:nodes:*");
}
}