use std::sync::atomic::{AtomicU64, Ordering};
use tracing::warn;
use super::config::TlsCredentials;
pub enum TransportCredentials {
Mtls(TlsCredentials),
Insecure,
}
impl TransportCredentials {
pub fn is_insecure(&self) -> bool {
matches!(self, TransportCredentials::Insecure)
}
}
impl std::fmt::Debug for TransportCredentials {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransportCredentials::Mtls(_) => write!(f, "Mtls(<redacted>)"),
TransportCredentials::Insecure => write!(f, "Insecure"),
}
}
}
static INSECURE_TRANSPORT_COUNT: AtomicU64 = AtomicU64::new(0);
pub fn insecure_transport_count() -> u64 {
INSECURE_TRANSPORT_COUNT.load(Ordering::Relaxed)
}
pub(crate) fn announce_insecure_transport(node_id: u64) {
INSECURE_TRANSPORT_COUNT.fetch_add(1, Ordering::Relaxed);
warn!(
node_id,
"cluster transport running WITHOUT authentication — any peer reaching the QUIC port \
can forge Raft RPCs. Only use on isolated networks. Set cluster.insecure_transport = \
false and provide TLS credentials for production."
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insecure_flag() {
assert!(TransportCredentials::Insecure.is_insecure());
}
#[test]
fn announce_bumps_counter() {
let before = insecure_transport_count();
announce_insecure_transport(42);
assert_eq!(insecure_transport_count(), before + 1);
}
}