From 1be76b0049216ed8fc8d06e8915c0d1ffff59f39 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 3 Feb 2026 16:50:11 -0600 Subject: [PATCH] show partially redacted snmp community --- src/poller/executor.rs | 18 +++++++----------- src/websocket_client.rs | 18 +++++++----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/poller/executor.rs b/src/poller/executor.rs index bc3592f..5aa2162 100644 --- a/src/poller/executor.rs +++ b/src/poller/executor.rs @@ -23,15 +23,11 @@ use log::{error, info, warn}; /// Redact SNMP community string for logging, showing first 2 chars only fn redact_community(community: &str) -> String { - let len = community.len(); - if len == 0 { - return "[redacted]".to_string(); - } - if len <= 2 { - "**".to_string() - } else { - format!("{}**", &community[..2]) + if community.is_empty() { + return "**".to_string(); } + let visible = std::cmp::min(community.len(), 2); + format!("{}**", &community[..visible]) } /// Executor handles polling individual pieces of equipment @@ -258,13 +254,13 @@ mod tests { #[test] fn test_redact_community_short() { - assert_eq!(redact_community("ab"), "**"); - assert_eq!(redact_community("a"), "**"); + assert_eq!(redact_community("ab"), "ab**"); + assert_eq!(redact_community("a"), "a**"); } #[test] fn test_redact_community_empty() { - assert_eq!(redact_community(""), "[redacted]"); + assert_eq!(redact_community(""), "**"); } #[test] diff --git a/src/websocket_client.rs b/src/websocket_client.rs index a09afe8..cb5a737 100644 --- a/src/websocket_client.rs +++ b/src/websocket_client.rs @@ -340,15 +340,11 @@ impl AgentClient { /// Redact SNMP community string for logging, showing first 2 chars only fn redact_community(community: &str) -> String { - let len = community.len(); - if len == 0 { - return "[redacted]".to_string(); - } - if len <= 2 { - "**".to_string() - } else { - format!("{}**", &community[..2]) + if community.is_empty() { + return "**".to_string(); } + let visible = std::cmp::min(community.len(), 2); + format!("{}**", &community[..visible]) } /// Execute an SNMP job and collect results. @@ -1076,13 +1072,13 @@ mod tests { #[test] fn test_redact_community_short() { - assert_eq!(redact_community("ab"), "**"); - assert_eq!(redact_community("a"), "**"); + assert_eq!(redact_community("ab"), "ab**"); + assert_eq!(redact_community("a"), "a**"); } #[test] fn test_redact_community_empty() { - assert_eq!(redact_community(""), "[redacted]"); + assert_eq!(redact_community(""), "**"); } #[test]