mobhub
Decentralized model and agent weights distribution network
Brought by drakestapleton
| 1 | //! Hardware TPM Key Vault client and memory security layer. |
| 2 | //! Guarantees zero plaintext secret files on disk. |
| 3 | |
| 4 | use std::process::Command; |
| 5 | use std::sync::{LazyLock, OnceLock}; |
| 6 | |
| 7 | static CACHED_TORN_KEY: OnceLock<Option<String>> = OnceLock::new(); |
| 8 | static KEY_PARAM_RE: LazyLock<regex::Regex> = LazyLock::new(|| { |
| 9 | regex::Regex::new(r"key=[^&\s]+").expect("Failed to compile KEY_PARAM_RE regex") |
| 10 | }); |
| 11 | |
| 12 | /// Dynamically resolve a secret token. |
| 13 | /// Checks hardware TPM vault first (atlas-vault get <KEY>), then falls back to in-memory environment. |
| 14 | pub fn resolve_secret(key_name: &str) -> Option<String> { |
| 15 | // 1. Try hardware TPM-bound vault on NVIDIA DGX Spark |
| 16 | if let Ok(out) = Command::new("atlas-vault").args(["get", key_name]).output() { |
| 17 | if out.status.success() { |
| 18 | let val = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 19 | if !val.is_empty() { |
| 20 | return Some(val); |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // 2. Fallback to process environment (e.g. for CI or local overrides) |
| 26 | if let Ok(env_val) = std::env::var(key_name) { |
| 27 | let trimmed = env_val.trim().to_string(); |
| 28 | if !trimmed.is_empty() { |
| 29 | return Some(trimmed); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | None |
| 34 | } |
| 35 | |
| 36 | /// Retrieve the Torn API Key dynamically into memory. |
| 37 | pub fn get_torn_api_key() -> Option<String> { |
| 38 | CACHED_TORN_KEY |
| 39 | .get_or_init(|| resolve_secret("TORN_API_KEY")) |
| 40 | .clone() |
| 41 | } |
| 42 | |
| 43 | /// Verify if the Torn API Key is present in the vault or memory. |
| 44 | pub fn is_torn_key_present() -> bool { |
| 45 | get_torn_api_key().is_some() |
| 46 | } |
| 47 | |
| 48 | /// Actively redact vault secrets and API key signatures from log streams. |
| 49 | pub fn redact_secrets(input: &str) -> String { |
| 50 | let mut sanitized = input.to_string(); |
| 51 | if let Some(key) = get_torn_api_key() { |
| 52 | if !key.is_empty() && key.len() >= 6 { |
| 53 | sanitized = sanitized.replace(&key, "[REDACTED_BY_ATLAS_VAULT]"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Actively redact all key=query parameters across the entire input stream |
| 58 | KEY_PARAM_RE |
| 59 | .replace_all(&sanitized, "key=[REDACTED_BY_ATLAS_VAULT]") |
| 60 | .to_string() |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::*; |
| 66 | |
| 67 | #[test] |
| 68 | fn test_secret_redaction() { |
| 69 | let raw = "GET https://api.torn.com/v2/faction/rankedwars?key=abcdef123456&limit=10 and retry key=9876543210zyx"; |
| 70 | let redacted = redact_secrets(raw); |
| 71 | assert!(!redacted.contains("abcdef123456")); |
| 72 | assert!(!redacted.contains("9876543210zyx")); |
| 73 | assert_eq!(redacted.matches("[REDACTED_BY_ATLAS_VAULT]").count(), 2); |
| 74 | } |
| 75 | } |