Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/openclaw-rs/blob/main/src/vault.rs # openclaw-rs High-performance sovereign agent runtime in native Rust and Mojo for NVIDIA DGX Spark Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `openclaw-rs / src / vault.rs` 177 lines ยท 5866 bytes Raw Blame History 1 use std::collections::HashMap; 2 use std::process::Command; 3 use std::sync::{Arc, Mutex}; 4 5 pub const REDACTED_MARKER: &str = "[REDACTED_BY_ATLAS_VAULT]"; 6 7 #[derive(Clone)] 8 pub struct VaultResolver { 9 cache: Arc>>, 10 } 11 12 impl Default for VaultResolver { 13 fn default() -> Self { 14 Self::new() 15 } 16 } 17 18 impl VaultResolver { 19 pub fn new() -> Self { 20 Self { 21 cache: Arc::new(Mutex::new(HashMap::new())), 22 } 23 } 24 25 pub fn resolve_secret(&self, key: &str) -> Result { 26 if key.trim().is_empty() { 27 return Err("Secret key cannot be empty".to_string()); 28 } 29 30 let mut cache = self.cache.lock().map_err(|e| e.to_string())?; 31 if let Some(val) = cache.get(key) { 32 return Ok(val.clone()); 33 } 34 35 // Try atlas-vault CLI binary first 36 if let Ok(output) = Command::new("atlas-vault").arg("get").arg(key).output() { 37 if output.status.success() { 38 let val = String::from_utf8_lossy(&output.stdout).trim().to_string(); 39 if !val.is_empty() { 40 cache.insert(key.to_string(), val.clone()); 41 return Ok(val); 42 } 43 } 44 } 45 46 // Check process environment variable as fallback 47 if let Ok(val) = std::env::var(key) { 48 cache.insert(key.to_string(), val.clone()); 49 return Ok(val); 50 } 51 52 Err(format!( 53 "Secret '{}' not found in hardware TPM vault or process environment", 54 key 55 )) 56 } 57 58 pub fn insert_cached(&self, key: &str, secret: &str) { 59 if let Ok(mut cache) = self.cache.lock() { 60 cache.insert(key.to_string(), secret.to_string()); 61 } 62 } 63 64 pub fn has_cached(&self, key: &str) -> bool { 65 if let Ok(cache) = self.cache.lock() { 66 cache.contains_key(key) 67 } else { 68 false 69 } 70 } 71 72 pub fn redact_sensitive_text(&self, text: &str) -> String { 73 let cache = match self.cache.lock() { 74 Ok(c) => c, 75 Err(_) => return text.to_string(), 76 }; 77 78 let mut sanitized = text.to_string(); 79 for val in cache.values() { 80 if val.len() >= 4 { 81 sanitized = sanitized.replace(val, REDACTED_MARKER); 82 } 83 } 84 sanitized 85 } 86 } 87 88 #[cfg(test)] 89 mod tests { 90 use super::*; 91 92 #[test] 93 fn test_vault_resolution_and_redaction() { 94 let vault = VaultResolver::new(); 95 std::env::set_var("TEST_TOKEN_SECRET", "super_secret_12345"); 96 97 let secret = vault.resolve_secret("TEST_TOKEN_SECRET").unwrap(); 98 assert_eq!(secret, "super_secret_12345"); 99 100 let output = "Connecting with bearer super_secret_12345 to endpoint".to_string(); 101 let redacted = vault.redact_sensitive_text(&output); 102 assert_eq!( 103 redacted, 104 format!("Connecting with bearer {} to endpoint", REDACTED_MARKER) 105 ); 106 } 107 108 #[test] 109 fn test_vault_empty_key_resolution() { 110 let vault = VaultResolver::new(); 111 assert!(vault.resolve_secret("").is_err()); 112 assert!(vault.resolve_secret(" ").is_err()); 113 let err = vault.resolve_secret("").unwrap_err(); 114 assert_eq!(err, "Secret key cannot be empty"); 115 } 116 117 #[test] 118 fn test_vault_missing_key_fallback() { 119 let vault = VaultResolver::new(); 120 let err = vault.resolve_secret("NONEXISTENT_KEY_12345_XYZ").unwrap_err(); 121 assert!(err.contains("not found in hardware TPM vault or process environment")); 122 } 123 124 #[test] 125 fn test_vault_multiline_redaction() { 126 let vault = VaultResolver::new(); 127 vault.insert_cached("KEY_A", "secret_alpha_99"); 128 vault.insert_cached("KEY_B", "secret_beta_88"); 129 130 let multiline = "Line 1: secret_alpha_99\nLine 2: benign text\nLine 3: bearer secret_beta_88 token"; 131 let redacted = vault.redact_sensitive_text(multiline); 132 assert!(!redacted.contains("secret_alpha_99")); 133 assert!(!redacted.contains("secret_beta_88")); 134 assert!(redacted.contains(REDACTED_MARKER)); 135 assert!(redacted.contains("Line 2: benign text")); 136 } 137 138 #[test] 139 fn test_vault_streaming_chunk_redaction() { 140 let vault = VaultResolver::new(); 141 vault.insert_cached("API_KEY", "openclaw_sk_live_998877"); 142 143 let chunks = vec![ 144 "data: {\"choices\": [{\"delta\": {\"content\": \"key is \"}}]}\\n\\n", 145 "data: {\"choices\": [{\"delta\": {\"content\": \"openclaw_sk_live_998877\"}}]}\\n\\n", 146 "data: [DONE]\\n\\n", 147 ]; 148 149 let mut sanitized_stream = Vec::new(); 150 for chunk in chunks { 151 sanitized_stream.push(vault.redact_sensitive_text(chunk)); 152 } 153 154 assert_eq!(sanitized_stream.len(), 3); 155 assert!(!sanitized_stream[1].contains("openclaw_sk_live_998877")); 156 assert!(sanitized_stream[1].contains(REDACTED_MARKER)); 157 assert_eq!(sanitized_stream[2], "data: [DONE]\\n\\n"); 158 } 159 160 #[test] 161 fn test_vault_prevention_of_disk_secret_writes() { 162 let temp_dir = tempfile::tempdir().unwrap(); 163 let vault = VaultResolver::new(); 164 vault.insert_cached("CRITICAL_VAULT_KEY", "sovereign_in_memory_only_key_value"); 165 166 let resolved = vault.resolve_secret("CRITICAL_VAULT_KEY").unwrap(); 167 assert_eq!(resolved, "sovereign_in_memory_only_key_value"); 168 169 // Verify that no .env or plaintext secret files were written anywhere in temp_dir 170 let entries = std::fs::read_dir(temp_dir.path()).unwrap(); 171 assert_eq!(entries.count(), 0); 172 173 // Verify current directory has no new .env files created 174 assert!(!std::path::Path::new(".env").exists()); 175 assert!(!std::path::Path::new(".env.local").exists()); 176 } 177 }