Skip to content

Commit e6596ee

Browse files
committed
Fix CI: clippy warnings and formatting
1 parent 1116ef2 commit e6596ee

File tree

6 files changed

+115
-66
lines changed

6 files changed

+115
-66
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ name = "audit_trail"
4545
path = "examples/audit_trail.rs"
4646

4747
[[example]]
48-
name = "encrypted_logging"
49-
path = "examples/encrypted_logging.rs"
48+
name = "compliance_report"
49+
path = "examples/compliance_report.rs"
50+
51+
[[example]]
52+
name = "siem_integration"
53+
path = "examples/siem_integration.rs"
5054

src/compliance.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ impl ComplianceReporter {
265265
let access_events = filtered
266266
.iter()
267267
.filter(|e| {
268-
e.category.as_ref().is_some_and(|c| {
269-
c.contains("authentication") || c.contains("access")
270-
})
268+
e.category
269+
.as_ref()
270+
.is_some_and(|c| c.contains("authentication") || c.contains("access"))
271271
})
272272
.count();
273273

src/encryption.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ impl LogEncryptor {
8888
}
8989

9090
/// Encrypt a log entry
91-
pub fn encrypt(&self, plaintext: &str, entry_id: &str) -> Result<EncryptedLogEntry, EncryptionError> {
91+
pub fn encrypt(
92+
&self,
93+
plaintext: &str,
94+
entry_id: &str,
95+
) -> Result<EncryptedLogEntry, EncryptionError> {
9296
let mut nonce_bytes = [0u8; 12];
9397
OsRng.fill_bytes(&mut nonce_bytes);
9498
let nonce = Nonce::from_slice(&nonce_bytes);
@@ -117,7 +121,9 @@ impl LogEncryptor {
117121
.map_err(|e| EncryptionError::Base64Error(e.to_string()))?;
118122

119123
if nonce_bytes.len() != 12 {
120-
return Err(EncryptionError::DecryptionFailed("Invalid nonce length".to_string()));
124+
return Err(EncryptionError::DecryptionFailed(
125+
"Invalid nonce length".to_string(),
126+
));
121127
}
122128

123129
let nonce = Nonce::from_slice(&nonce_bytes);
@@ -127,12 +133,14 @@ impl LogEncryptor {
127133
.decrypt(nonce, ciphertext.as_ref())
128134
.map_err(|e| EncryptionError::DecryptionFailed(e.to_string()))?;
129135

130-
String::from_utf8(plaintext)
131-
.map_err(|e| EncryptionError::DecryptionFailed(e.to_string()))
136+
String::from_utf8(plaintext).map_err(|e| EncryptionError::DecryptionFailed(e.to_string()))
132137
}
133138

134139
/// Encrypt multiple log entries in batch
135-
pub fn encrypt_batch(&self, entries: &[(&str, &str)]) -> Vec<Result<EncryptedLogEntry, EncryptionError>> {
140+
pub fn encrypt_batch(
141+
&self,
142+
entries: &[(&str, &str)],
143+
) -> Vec<Result<EncryptedLogEntry, EncryptionError>> {
136144
entries
137145
.iter()
138146
.map(|(plaintext, entry_id)| self.encrypt(plaintext, entry_id))

src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,23 @@
5555
//! - **Enhanced Metrics**: Detailed logging statistics
5656
5757
pub mod compliance;
58+
pub mod encryption;
5859
pub mod entry;
5960
pub mod formats;
61+
pub mod metrics;
6062
pub mod persistence;
61-
pub mod encryption;
6263
pub mod redaction;
63-
pub mod metrics;
6464

6565
pub use compliance::{ComplianceFramework, ComplianceReport, ComplianceReporter};
66+
pub use encryption::{EncryptedLogEntry, LogEncryptor};
6667
pub use entry::{LogEntry, SecurityLevel};
6768
pub use formats::{CEFFormatter, LEEFFormatter, SplunkFormatter, SyslogFormatter};
68-
pub use persistence::{LogWriter, PersistenceConfig};
69-
pub use encryption::{LogEncryptor, EncryptedLogEntry};
70-
pub use redaction::{LogRedactor, RedactionPattern, RedactionConfig};
7169
pub use metrics::{LogMetrics, MetricsSnapshot};
70+
pub use persistence::{LogWriter, PersistenceConfig};
71+
pub use redaction::{LogRedactor, RedactionConfig, RedactionPattern};
7272

7373
use std::sync::{Arc, Mutex};
7474
use std::time::{Duration, Instant};
75-
use std::collections::HashMap;
7675
use thiserror::Error;
7776

7877
/// Logger errors
@@ -166,6 +165,7 @@ impl LoggerConfig {
166165

167166
/// Rate limiter for log flooding prevention
168167
#[derive(Debug)]
168+
#[allow(dead_code)]
169169
struct RateLimiter {
170170
limit: u32,
171171
window_start: Instant,
@@ -199,6 +199,7 @@ impl RateLimiter {
199199

200200
/// Thread-safe secure logger for financial systems
201201
#[derive(Clone)]
202+
#[allow(dead_code)]
202203
pub struct SecureLogger {
203204
entries: Arc<Mutex<Vec<LogEntry>>>,
204205
source: Option<String>,
@@ -258,6 +259,7 @@ impl SecureLogger {
258259
}
259260

260261
/// Check rate limit before logging
262+
#[allow(dead_code)]
261263
fn check_rate_limit(&self) -> bool {
262264
let mut limiter = self.rate_limiter.lock().unwrap();
263265
if let Some(ref mut rl) = *limiter {
@@ -268,6 +270,7 @@ impl SecureLogger {
268270
}
269271

270272
/// Apply redaction to message if enabled
273+
#[allow(dead_code)]
271274
fn apply_redaction(&self, message: &str) -> String {
272275
if self.config.enable_redaction {
273276
self.redactor.redact(message)

src/metrics.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ impl MetricsAggregator {
213213
let total_bytes: u64 = self.snapshots.iter().map(|s| s.bytes_logged).sum();
214214
let total_errors: u64 = self.snapshots.iter().map(|s| s.errors_count).sum();
215215
let total_rate_limited: u64 = self.snapshots.iter().map(|s| s.rate_limited_count).sum();
216-
let peak_rate: u64 = self.snapshots.iter().map(|s| s.peak_rate).max().unwrap_or(0);
216+
let peak_rate: u64 = self
217+
.snapshots
218+
.iter()
219+
.map(|s| s.peak_rate)
220+
.max()
221+
.unwrap_or(0);
217222

218223
AggregatedMetrics {
219224
logger_count: self.snapshots.len(),
@@ -299,7 +304,10 @@ mod tests {
299304
metrics2.record_log("INFO", None, 1024);
300305
}
301306
let snapshot2 = metrics2.snapshot();
302-
assert!(snapshot2.bytes_human_readable().contains("KB") || snapshot2.bytes_human_readable().contains("MB"));
307+
assert!(
308+
snapshot2.bytes_human_readable().contains("KB")
309+
|| snapshot2.bytes_human_readable().contains("MB")
310+
);
303311
}
304312

305313
#[test]

src/redaction.rs

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ impl LogRedactor {
108108
pub fn add_custom_pattern(&mut self, name: &str, pattern: &str) -> Result<(), regex::Error> {
109109
let regex = Regex::new(pattern)?;
110110
self.custom_patterns.insert(name.to_string(), regex);
111-
self.config.enabled_patterns.push(RedactionPattern::Custom(name.to_string()));
111+
self.config
112+
.enabled_patterns
113+
.push(RedactionPattern::Custom(name.to_string()));
112114
Ok(())
113115
}
114116

@@ -118,12 +120,20 @@ impl LogRedactor {
118120

119121
for pattern_type in &self.config.enabled_patterns {
120122
result = match pattern_type {
121-
RedactionPattern::SSN => self.redact_pattern(&result, &self.patterns.ssn, "***-**-****"),
123+
RedactionPattern::SSN => {
124+
self.redact_pattern(&result, &self.patterns.ssn, "***-**-****")
125+
}
122126
RedactionPattern::CreditCard => self.redact_credit_card(&result),
123127
RedactionPattern::Email => self.redact_email(&result),
124-
RedactionPattern::PhoneNumber => self.redact_pattern(&result, &self.patterns.phone, "***-***-****"),
125-
RedactionPattern::IpAddress => self.redact_pattern(&result, &self.patterns.ip_address, "***.***.***.***"),
126-
RedactionPattern::BankAccount => self.redact_pattern(&result, &self.patterns.bank_account, "********"),
128+
RedactionPattern::PhoneNumber => {
129+
self.redact_pattern(&result, &self.patterns.phone, "***-***-****")
130+
}
131+
RedactionPattern::IpAddress => {
132+
self.redact_pattern(&result, &self.patterns.ip_address, "***.***.***.***")
133+
}
134+
RedactionPattern::BankAccount => {
135+
self.redact_pattern(&result, &self.patterns.bank_account, "********")
136+
}
127137
RedactionPattern::ApiKey => self.redact_api_key(&result),
128138
RedactionPattern::Password => self.redact_password(&result),
129139
RedactionPattern::Custom(name) => {
@@ -146,54 +156,66 @@ impl LogRedactor {
146156

147157
/// Redact credit card numbers, preserving last 4 digits
148158
fn redact_credit_card(&self, input: &str) -> String {
149-
self.patterns.credit_card.replace_all(input, |caps: &regex::Captures| {
150-
let matched = caps.get(0).unwrap().as_str();
151-
let digits: String = matched.chars().filter(|c| c.is_ascii_digit()).collect();
152-
if digits.len() >= 4 {
153-
format!("****-****-****-{}", &digits[digits.len()-4..])
154-
} else {
155-
"****-****-****-****".to_string()
156-
}
157-
}).to_string()
159+
self.patterns
160+
.credit_card
161+
.replace_all(input, |caps: &regex::Captures| {
162+
let matched = caps.get(0).unwrap().as_str();
163+
let digits: String = matched.chars().filter(|c| c.is_ascii_digit()).collect();
164+
if digits.len() >= 4 {
165+
format!("****-****-****-{}", &digits[digits.len() - 4..])
166+
} else {
167+
"****-****-****-****".to_string()
168+
}
169+
})
170+
.to_string()
158171
}
159172

160173
/// Redact email addresses, preserving domain
161174
fn redact_email(&self, input: &str) -> String {
162-
self.patterns.email.replace_all(input, |caps: &regex::Captures| {
163-
let matched = caps.get(0).unwrap().as_str();
164-
if let Some(at_pos) = matched.find('@') {
165-
let domain = &matched[at_pos..];
166-
format!("****{}", domain)
167-
} else {
168-
"****@****.***".to_string()
169-
}
170-
}).to_string()
175+
self.patterns
176+
.email
177+
.replace_all(input, |caps: &regex::Captures| {
178+
let matched = caps.get(0).unwrap().as_str();
179+
if let Some(at_pos) = matched.find('@') {
180+
let domain = &matched[at_pos..];
181+
format!("****{}", domain)
182+
} else {
183+
"****@****.***".to_string()
184+
}
185+
})
186+
.to_string()
171187
}
172188

173189
/// Redact API keys and tokens
174190
fn redact_api_key(&self, input: &str) -> String {
175-
self.patterns.api_key.replace_all(input, |caps: &regex::Captures| {
176-
let matched = caps.get(0).unwrap().as_str();
177-
if let Some(eq_pos) = matched.find([':', '=']) {
178-
let prefix = &matched[..=eq_pos];
179-
format!("{} [REDACTED]", prefix.trim_end_matches([':', '=', ' ']))
180-
} else {
181-
"[REDACTED API KEY]".to_string()
182-
}
183-
}).to_string()
191+
self.patterns
192+
.api_key
193+
.replace_all(input, |caps: &regex::Captures| {
194+
let matched = caps.get(0).unwrap().as_str();
195+
if let Some(eq_pos) = matched.find([':', '=']) {
196+
let prefix = &matched[..=eq_pos];
197+
format!("{} [REDACTED]", prefix.trim_end_matches([':', '=', ' ']))
198+
} else {
199+
"[REDACTED API KEY]".to_string()
200+
}
201+
})
202+
.to_string()
184203
}
185204

186205
/// Redact passwords
187206
fn redact_password(&self, input: &str) -> String {
188-
self.patterns.password.replace_all(input, |caps: &regex::Captures| {
189-
let matched = caps.get(0).unwrap().as_str();
190-
if let Some(eq_pos) = matched.find([':', '=']) {
191-
let prefix = &matched[..=eq_pos];
192-
format!("{} [REDACTED]", prefix.trim_end_matches([':', '=', ' ']))
193-
} else {
194-
"[REDACTED PASSWORD]".to_string()
195-
}
196-
}).to_string()
207+
self.patterns
208+
.password
209+
.replace_all(input, |caps: &regex::Captures| {
210+
let matched = caps.get(0).unwrap().as_str();
211+
if let Some(eq_pos) = matched.find([':', '=']) {
212+
let prefix = &matched[..=eq_pos];
213+
format!("{} [REDACTED]", prefix.trim_end_matches([':', '=', ' ']))
214+
} else {
215+
"[REDACTED PASSWORD]".to_string()
216+
}
217+
})
218+
.to_string()
197219
}
198220

199221
/// Check if a string contains sensitive data
@@ -208,9 +230,10 @@ impl LogRedactor {
208230
RedactionPattern::BankAccount => self.patterns.bank_account.is_match(input),
209231
RedactionPattern::ApiKey => self.patterns.api_key.is_match(input),
210232
RedactionPattern::Password => self.patterns.password.is_match(input),
211-
RedactionPattern::Custom(name) => {
212-
self.custom_patterns.get(name).map_or(false, |r| r.is_match(input))
213-
}
233+
RedactionPattern::Custom(name) => self
234+
.custom_patterns
235+
.get(name)
236+
.is_some_and(|r| r.is_match(input)),
214237
};
215238
if has_match {
216239
return true;
@@ -233,9 +256,10 @@ impl LogRedactor {
233256
RedactionPattern::BankAccount => self.patterns.bank_account.is_match(input),
234257
RedactionPattern::ApiKey => self.patterns.api_key.is_match(input),
235258
RedactionPattern::Password => self.patterns.password.is_match(input),
236-
RedactionPattern::Custom(name) => {
237-
self.custom_patterns.get(name).map_or(false, |r| r.is_match(input))
238-
}
259+
RedactionPattern::Custom(name) => self
260+
.custom_patterns
261+
.get(name)
262+
.is_some_and(|r| r.is_match(input)),
239263
};
240264
if has_match {
241265
found.push(pattern_type.clone());
@@ -324,7 +348,9 @@ mod tests {
324348
#[test]
325349
fn test_custom_pattern() {
326350
let mut redactor = LogRedactor::default();
327-
redactor.add_custom_pattern("employee_id", r"EMP-\d{6}").unwrap();
351+
redactor
352+
.add_custom_pattern("employee_id", r"EMP-\d{6}")
353+
.unwrap();
328354
let input = "Employee EMP-123456 accessed system";
329355
let output = redactor.redact(input);
330356
assert!(output.contains("[REDACTED]"));

0 commit comments

Comments
 (0)