show partially redacted snmp community

This commit is contained in:
Graham McIntire 2026-02-03 16:50:11 -06:00
parent 0f19518065
commit 1be76b0049
No known key found for this signature in database
2 changed files with 14 additions and 22 deletions

View file

@ -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]

View file

@ -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]