Skip to content

Commit 01fdd79

Browse files
author
developerworks
committed
Consolidate audit file_path from Option<String> to String
- Change AuditConfig.file_path to non-optional String with a default path - Remove redundant unwrap_or_default call in state validation - Update test to pass empty string explicitly instead of relying on None - Gracefully fall back to memory backend when file_path is empty in IPC audit
1 parent d5e7446 commit 01fdd79

4 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/config/audit.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub struct AuditConfig {
2424

2525
/// File path for file backend. Required when backend is "file".
2626
#[schemars(!default)]
27-
#[serde(default)]
28-
pub file_path: Option<String>,
27+
#[serde(default = "default_file_path")]
28+
pub file_path: String,
2929

3030
/// Failure strategy when audit backend is unavailable.
3131
/// - "fail_closed": reject write commands when audit cannot be written.
@@ -47,7 +47,7 @@ impl Default for AuditConfig {
4747
Self {
4848
enabled: true,
4949
backend: "memory".into(),
50-
file_path: None,
50+
file_path: "/tmp/rust-supervisor-demo/audit.jsonl".to_string(),
5151
failure_strategy: "fail_closed".into(),
5252
max_defer_queue: 1000,
5353
}
@@ -64,6 +64,10 @@ fn default_audit_backend() -> String {
6464
"memory".into()
6565
}
6666

67+
fn default_file_path() -> String {
68+
"/tmp/rust-supervisor-demo/audit.jsonl".to_string()
69+
}
70+
6771
/// Serde default helper: returns "fail_closed".
6872
fn default_fail_closed() -> String {
6973
"fail_closed".into()

src/config/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ fn validate_audit(audit: &AuditConfig) -> Result<(), crate::error::types::Superv
858858
match audit.backend.as_str() {
859859
"memory" => {}
860860
"file" => {
861-
let path = audit.file_path.as_deref().unwrap_or_default().trim();
861+
let path = audit.file_path.trim();
862862
if path.is_empty() {
863863
return Err(crate::error::types::SupervisorError::fatal_config(
864864
"audit.file_path is required when audit.backend is file",

src/config/tests/yaml_config_test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,15 @@ fn yaml_config_rejects_invalid_supervision_strategy() {
217217
assert!(result.is_err());
218218
}
219219

220-
/// Verifies that JSON Lines audit storage requires an explicit file path.
220+
/// Verifies that JSON Lines audit storage rejects an empty file path.
221221
#[test]
222222
fn yaml_config_rejects_file_audit_without_path() {
223-
let yaml = valid_yaml().replace("backend: memory", "backend: file");
223+
let yaml = valid_yaml()
224+
.replace("backend: memory", "backend: file")
225+
.replace(
226+
"failure_strategy: fail_closed",
227+
"file_path: \"\"\n failure_strategy: fail_closed",
228+
);
224229
let result = parse_config_state(&yaml);
225230

226231
assert!(result.is_err());

src/ipc/security/audit.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,25 @@ impl AuditBackend {
140140
/// when no file path is provided.
141141
pub fn from_config(config: &AuditConfig) -> Self {
142142
match config.backend.as_str() {
143-
"file" => match &config.file_path {
144-
Some(p) => match AuditBackend::new_file(p.as_str().to_owned()) {
145-
Ok(backend) => backend,
146-
Err(error) => {
147-
tracing::error!(
148-
target: "rust_supervisor::ipc::security::audit",
149-
path = %p.as_str(),
150-
?error,
151-
"failed to open file audit backend, falling back to memory"
152-
);
153-
AuditBackend::new_memory(4096)
143+
"file" => {
144+
let path = config.file_path.trim();
145+
if path.is_empty() {
146+
AuditBackend::new_memory(4096)
147+
} else {
148+
match AuditBackend::new_file(path.to_owned()) {
149+
Ok(backend) => backend,
150+
Err(error) => {
151+
tracing::error!(
152+
target: "rust_supervisor::ipc::security::audit",
153+
path = %path,
154+
?error,
155+
"failed to open file audit backend, falling back to memory"
156+
);
157+
AuditBackend::new_memory(4096)
158+
}
154159
}
155-
},
156-
None => AuditBackend::new_memory(4096),
157-
},
160+
}
161+
}
158162
_ => AuditBackend::new_memory(4096),
159163
}
160164
}

0 commit comments

Comments
 (0)