feat(brahman-handshake+ente-zero): allowlist explicita de peers libp2p

Capa de politica sobre el trust criptografico de Fase 3. Hasta ahora
cualquier peer Ed25519-valido pasaba el handshake remoto; con
allowlist activa, solo los peers explicitamente listados. Aplica
unicamente al path libp2p — el path Unix sigue usando SO_PEERCRED
del kernel.

API nueva en brahman_handshake::peer_allowlist:
- PeerAllowlist::from_iter / from_file con AllowlistError tipado.
- Formato del archivo: PeerId base58 por linea, # comentarios (linea
  entera o inline), lineas vacias ignoradas. Errores de parseo
  reportan numero de linea.
- is_allowed, len, is_empty, iter.

Wire en el server:
- ServerConfig.allowlist: Option<PeerAllowlist>. None = modo abierto
  (compat). Some = solo los listados.
- Gate en do_handshake ANTES de la verificacion de firma — la
  comparacion BTreeSet O(log n) es mas barata que crypto, asi que
  rechazamos peers invalidos antes de gastar ciclos.
- HandshakeError::Unauthorized("peer X no esta en la allowlist").

Wire en Arje (ente-zero):
- Env var BRAHMAN_PEER_ALLOWLIST apuntando a un archivo.
- setup_brahman_allowlist carga al startup; degrada a None si el
  archivo falla (doctrina PID 1: no romper por subsistemas
  opcionales).

Activacion end-to-end:
  BRAHMAN_LISTEN_MULTIADDR=/ip4/0.0.0.0/tcp/4101 \\
  BRAHMAN_PEER_ALLOWLIST=/etc/brahman/allowlist.txt \\
  ente-zero

Tests: 6 unit en peer_allowlist + 1 E2E en network_libp2p
(libp2p_handshake_allowlist_admits_listed_rejects_others). 25 tests
verdes en brahman-handshake. Sin regresion en ente-zero.

Pendientes: denylist explicita, hot reload via SIGHUP/watch, aplicar
politica a nivel de swarm via libp2p_allow_block_list::Behaviour
para rechazar ANTES del Noise handshake.
This commit is contained in:
Sergio
2026-05-09 15:27:15 +00:00
parent 2e6afd0973
commit 505748dd41
8 changed files with 425 additions and 1 deletions
+40
View File
@@ -168,6 +168,12 @@ async fn primordial_loop(
// reboots).
let brahman_net = setup_brahman_net(dev_mode).await;
// Allowlist opcional de peers libp2p: si BRAHMAN_PEER_ALLOWLIST
// apunta a un archivo, cualquier handshake remoto requiere que
// su peer_id esté en la lista. Sin la env, modo abierto (todo
// peer Ed25519-válido pasa el trust gate de Fase 3).
let brahman_allowlist = setup_brahman_allowlist();
let brahman_sock = brahman_handshake::transport::default_socket_path();
match brahman_handshake::server::Server::bind(
&brahman_sock,
@@ -175,6 +181,7 @@ async fn primordial_loop(
init_attached: true,
broker: Some(brahman_broker.clone()),
net: brahman_net.clone(),
allowlist: brahman_allowlist.clone(),
},
) {
Ok(server) => {
@@ -698,3 +705,36 @@ async fn setup_brahman_net(
Some(net)
}
/// Carga la allowlist de peers libp2p desde el archivo apuntado por
/// `BRAHMAN_PEER_ALLOWLIST`. Sin la env, devuelve `None` (modo abierto:
/// cualquier peer Ed25519-válido pasa el trust gate). Si la env está
/// pero el archivo falla, loggea y degrada a None — la doctrina PID 1
/// de no romper por subsistemas opcionales se mantiene.
fn setup_brahman_allowlist() -> Option<brahman_handshake::peer_allowlist::PeerAllowlist> {
let path = match std::env::var("BRAHMAN_PEER_ALLOWLIST") {
Ok(s) if !s.is_empty() => s,
_ => {
tracing::debug!("BRAHMAN_PEER_ALLOWLIST no set — modo abierto (todo peer pasa)");
return None;
}
};
match brahman_handshake::peer_allowlist::PeerAllowlist::from_file(&path) {
Ok(list) => {
info!(
path = %path,
peers = list.len(),
"allowlist de peers libp2p cargada"
);
Some(list)
}
Err(e) => {
warn!(
path = %path,
?e,
"BRAHMAN_PEER_ALLOWLIST inválido — degradando a modo abierto (sin restricción)"
);
None
}
}
}