1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// Audit logging module with request ID tracing and custom filters
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use regex::Regex;
use serde_json::Value;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Clone)]
struct CustomFilter {
name: String,
pattern: Regex,
file: Arc<Mutex<std::fs::File>>,
}
#[derive(Clone)]
pub struct AuditLogger {
mask_passwords: bool,
sensitive_fields: Vec<String>,
request_file: Option<Arc<Mutex<std::fs::File>>>,
query_file: Option<Arc<Mutex<std::fs::File>>>,
error_file: Option<Arc<Mutex<std::fs::File>>>,
warning_file: Option<Arc<Mutex<std::fs::File>>>,
info_file: Option<Arc<Mutex<std::fs::File>>>,
combined_file: Option<Arc<Mutex<std::fs::File>>>,
custom_filters: Vec<CustomFilter>,
}
impl AuditLogger {
pub fn new(
request_log_path: Option<String>,
query_log_path: Option<String>,
error_log_path: Option<String>,
warning_log_path: Option<String>,
info_log_path: Option<String>,
combined_log_path: Option<String>,
mask_passwords: bool,
sensitive_fields: Vec<String>,
custom_filter_configs: Vec<crate::config::CustomLogFilter>,
) -> Result<Self> {
// Helper function to open a log file if path is provided
let open_log_file = |path: &Option<String>| -> Result<Option<Arc<Mutex<std::fs::File>>>> {
if let Some(path_str) = path {
// Ensure log directories exist
if let Some(parent) = Path::new(path_str).parent() {
std::fs::create_dir_all(parent).context("Failed to create log directory")?;
}
let file = OpenOptions::new()
.create(true)
.append(true)
.open(path_str)
.context(format!("Failed to open log file: {}", path_str))?;
Ok(Some(Arc::new(Mutex::new(file))))
} else {
Ok(None)
}
};
// Initialize custom filters
let mut custom_filters = Vec::new();
for filter_config in custom_filter_configs {
if filter_config.enabled {
// Compile regex pattern
let pattern = Regex::new(&filter_config.pattern).context(format!(
"Invalid regex pattern in filter '{}': {}",
filter_config.name, filter_config.pattern
))?;
// Open filter output file
if let Some(parent) = Path::new(&filter_config.output_file).parent() {
std::fs::create_dir_all(parent)
.context("Failed to create filter log directory")?;
}
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&filter_config.output_file)
.context(format!(
"Failed to open filter log file: {}",
filter_config.output_file
))?;
custom_filters.push(CustomFilter {
name: filter_config.name.clone(),
pattern,
file: Arc::new(Mutex::new(file)),
});
}
}
Ok(Self {
mask_passwords,
sensitive_fields,
request_file: open_log_file(&request_log_path)?,
query_file: open_log_file(&query_log_path)?,
error_file: open_log_file(&error_log_path)?,
warning_file: open_log_file(&warning_log_path)?,
info_file: open_log_file(&info_log_path)?,
combined_file: open_log_file(&combined_log_path)?,
custom_filters,
})
}
/// Generate a unique request ID for transaction tracing
pub fn generate_request_id() -> String {
format!("{}", uuid::Uuid::new_v4().as_u128() & 0xFFFFFFFF_FFFFFFFF) // 16 hex chars
}
/// Write to combined log and apply custom filters
async fn write_combined_and_filter(&self, entry: &str) -> Result<()> {
// Write to combined log if configured
if let Some(ref file_mutex) = self.combined_file {
let mut file = file_mutex.lock().await;
file.write_all(entry.as_bytes())
.context("Failed to write to combined log")?;
file.flush().context("Failed to flush combined log")?;
}
// Apply custom filters
for filter in &self.custom_filters {
if filter.pattern.is_match(entry) {
let mut file = filter.file.lock().await;
file.write_all(entry.as_bytes())
.context(format!("Failed to write to filter log: {}", filter.name))?;
file.flush()
.context(format!("Failed to flush filter log: {}", filter.name))?;
}
}
Ok(())
}
pub async fn log_request(
&self,
request_id: &str,
timestamp: DateTime<Utc>,
_ip: &str,
user: Option<&str>,
power: Option<i32>,
endpoint: &str,
payload: &Value,
) -> Result<()> {
let mut masked_payload = payload.clone();
if self.mask_passwords {
self.mask_sensitive_data(&mut masked_payload);
}
let user_str = user.unwrap_or("anonymous");
let power_str = power
.map(|p| format!("power={}", p))
.unwrap_or_else(|| "power=0".to_string());
let log_entry = format!(
"{} [{}] | REQUEST | user={} | {} | endpoint={} | payload={}\n",
timestamp.format("%Y-%m-%d %H:%M:%S"),
request_id,
user_str,
power_str,
endpoint,
serde_json::to_string(&masked_payload).unwrap_or_else(|_| "invalid_json".to_string())
);
// Write to legacy request log if configured
if let Some(ref file_mutex) = self.request_file {
let mut file = file_mutex.lock().await;
file.write_all(log_entry.as_bytes())
.context("Failed to write to request log")?;
file.flush().context("Failed to flush request log")?;
}
// Write to combined log and apply filters
self.write_combined_and_filter(&log_entry).await?;
Ok(())
}
pub async fn log_query(
&self,
request_id: &str,
timestamp: DateTime<Utc>,
user: &str,
power: Option<i32>,
query: &str,
parameters: Option<&Value>,
rows_affected: Option<u64>,
) -> Result<()> {
let params_str = if let Some(params) = parameters {
serde_json::to_string(params).unwrap_or_else(|_| "invalid_json".to_string())
} else {
"null".to_string()
};
let power_str = power
.map(|p| format!("power={}", p))
.unwrap_or_else(|| "power=0".to_string());
let rows_str = rows_affected
.map(|r| format!("rows={}", r))
.unwrap_or_else(|| "rows=0".to_string());
let log_entry = format!(
"{} [{}] | QUERY | user={} | {} | {} | query={} | params={}\n",
timestamp.format("%Y-%m-%d %H:%M:%S"),
request_id,
user,
power_str,
rows_str,
query,
params_str
);
// Write to legacy query log if configured
if let Some(ref file_mutex) = self.query_file {
let mut file = file_mutex.lock().await;
file.write_all(log_entry.as_bytes())
.context("Failed to write to query log")?;
file.flush().context("Failed to flush query log")?;
}
// Write to combined log and apply filters
self.write_combined_and_filter(&log_entry).await?;
Ok(())
}
pub async fn log_error(
&self,
request_id: &str,
timestamp: DateTime<Utc>,
error: &str,
context: Option<&str>,
user: Option<&str>,
power: Option<i32>,
) -> Result<()> {
let user_str = user.unwrap_or("unknown");
let context_str = context.unwrap_or("general");
let power_str = power
.map(|p| format!("power={}", p))
.unwrap_or_else(|| "power=0".to_string());
let log_entry = format!(
"{} [{}] | ERROR | user={} | {} | context={} | error={}\n",
timestamp.format("%Y-%m-%d %H:%M:%S"),
request_id,
user_str,
power_str,
context_str,
error
);
// Write to legacy error log if configured
if let Some(ref file_mutex) = self.error_file {
let mut file = file_mutex.lock().await;
file.write_all(log_entry.as_bytes())
.context("Failed to write to error log")?;
file.flush().context("Failed to flush error log")?;
}
// Write to combined log and apply filters
self.write_combined_and_filter(&log_entry).await?;
Ok(())
}
pub async fn log_warning(
&self,
request_id: &str,
timestamp: DateTime<Utc>,
message: &str,
context: Option<&str>,
user: Option<&str>,
power: Option<i32>,
) -> Result<()> {
let user_str = user.unwrap_or("unknown");
let context_str = context.unwrap_or("general");
let power_str = power
.map(|p| format!("power={}", p))
.unwrap_or_else(|| "power=0".to_string());
let log_entry = format!(
"{} [{}] | WARNING | user={} | {} | context={} | message={}\n",
timestamp.format("%Y-%m-%d %H:%M:%S"),
request_id,
user_str,
power_str,
context_str,
message
);
// Write to warning log if configured
if let Some(ref file_mutex) = self.warning_file {
let mut file = file_mutex.lock().await;
file.write_all(log_entry.as_bytes())
.context("Failed to write to warning log")?;
file.flush().context("Failed to flush warning log")?;
}
// Write to combined log and apply filters
self.write_combined_and_filter(&log_entry).await?;
Ok(())
}
pub async fn log_info(
&self,
request_id: &str,
timestamp: DateTime<Utc>,
message: &str,
context: Option<&str>,
user: Option<&str>,
power: Option<i32>,
) -> Result<()> {
let user_str = user.unwrap_or("system");
let context_str = context.unwrap_or("general");
let power_str = power
.map(|p| format!("power={}", p))
.unwrap_or_else(|| "power=0".to_string());
let log_entry = format!(
"{} [{}] | INFO | user={} | {} | context={} | message={}\n",
timestamp.format("%Y-%m-%d %H:%M:%S"),
request_id,
user_str,
power_str,
context_str,
message
);
// Write to info log if configured
if let Some(ref file_mutex) = self.info_file {
let mut file = file_mutex.lock().await;
file.write_all(log_entry.as_bytes())
.context("Failed to write to info log")?;
file.flush().context("Failed to flush info log")?;
}
// Write to combined log and apply filters
self.write_combined_and_filter(&log_entry).await?;
Ok(())
}
fn mask_sensitive_data(&self, value: &mut Value) {
match value {
Value::Object(map) => {
for (key, val) in map.iter_mut() {
// Always mask password and pin
if key == "password" || key == "pin" {
*val = Value::String("***MASKED***".to_string());
}
// Also mask any configured sensitive fields
else if self.sensitive_fields.contains(key) {
*val = Value::String("***MASKED***".to_string());
} else {
self.mask_sensitive_data(val);
}
}
}
Value::Array(arr) => {
for item in arr.iter_mut() {
self.mask_sensitive_data(item);
}
}
_ => {}
}
}
}
|