aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-cli / src / vault.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use regex::Regex; |
| 17 | use serde_json::{json, Value}; |
| 18 | use std::process::Command; |
| 19 | use std::sync::{LazyLock, Mutex, RwLock}; |
| 20 | |
| 21 | fn get_vault_bin() -> std::path::PathBuf { |
| 22 | let platform = crate::platform::PlatformContext::detect(); |
| 23 | let local = platform.home_dir.join(".local/bin/atlas-vault"); |
| 24 | if local.is_file() { |
| 25 | local |
| 26 | } else { |
| 27 | std::path::PathBuf::from("atlas-vault") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static VAULT_SECRET_CACHE: LazyLock<RwLock<Option<Vec<(String, String)>>>> = |
| 32 | LazyLock::new(|| RwLock::new(None)); |
| 33 | static VAULT_MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(())); |
| 34 | |
| 35 | pub fn is_vault_available() -> bool { |
| 36 | get_vault_bin().is_file() |
| 37 | } |
| 38 | |
| 39 | pub fn list_keys() -> Vec<String> { |
| 40 | if !is_vault_available() { |
| 41 | return Vec::new(); |
| 42 | } |
| 43 | let output = Command::new(get_vault_bin()).arg("list").output(); |
| 44 | |
| 45 | match output { |
| 46 | Ok(out) if out.status.success() => { |
| 47 | let stdout = String::from_utf8_lossy(&out.stdout); |
| 48 | stdout |
| 49 | .lines() |
| 50 | .map(|l| l.trim().to_string()) |
| 51 | .filter(|l| !l.is_empty()) |
| 52 | .collect() |
| 53 | } |
| 54 | _ => Vec::new(), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub fn has_key(key: &str) -> bool { |
| 59 | let keys = list_keys(); |
| 60 | keys.iter().any(|k| k == key) |
| 61 | } |
| 62 | |
| 63 | pub fn get_secret(key: &str) -> Result<String, String> { |
| 64 | if !is_vault_available() { |
| 65 | return Err(format!( |
| 66 | "atlas-vault binary not found at {}", |
| 67 | get_vault_bin().display() |
| 68 | )); |
| 69 | } |
| 70 | let output = Command::new(get_vault_bin()) |
| 71 | .arg("get") |
| 72 | .arg(key) |
| 73 | .output() |
| 74 | .map_err(|e| format!("Failed to execute atlas-vault: {}", e))?; |
| 75 | |
| 76 | if !output.status.success() { |
| 77 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 78 | return Err(format!("atlas-vault get failed: {}", stderr.trim())); |
| 79 | } |
| 80 | |
| 81 | let secret = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 82 | if secret.is_empty() { |
| 83 | return Err(format!("Key {} returned empty value", key)); |
| 84 | } |
| 85 | Ok(secret) |
| 86 | } |
| 87 | |
| 88 | pub fn get_cached_vault_secrets() -> Vec<(String, String)> { |
| 89 | if cfg!(test) { |
| 90 | return Vec::new(); |
| 91 | } |
| 92 | |
| 93 | if let Ok(read) = VAULT_SECRET_CACHE.read() { |
| 94 | if let Some(ref cache) = *read { |
| 95 | return cache.clone(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | let _guard = match VAULT_MUTEX.lock() { |
| 100 | Ok(g) => g, |
| 101 | Err(poisoned) => poisoned.into_inner(), |
| 102 | }; |
| 103 | |
| 104 | if let Ok(read) = VAULT_SECRET_CACHE.read() { |
| 105 | if let Some(ref cache) = *read { |
| 106 | return cache.clone(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | let keys = list_keys(); |
| 111 | let mut entries = Vec::new(); |
| 112 | for key in keys { |
| 113 | if let Ok(val) = get_secret(&key) { |
| 114 | if val.len() >= 6 { |
| 115 | entries.push((key, val)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if let Ok(mut write) = VAULT_SECRET_CACHE.write() { |
| 121 | *write = Some(entries.clone()); |
| 122 | } |
| 123 | entries |
| 124 | } |
| 125 | |
| 126 | /// Scrub text to ensure zero secret values or raw token patterns are printed. |
| 127 | pub fn redact_secrets(input: &str) -> String { |
| 128 | let mut result = input.to_string(); |
| 129 | |
| 130 | // 1. Scrub actual values of known vault keys from memory cache |
| 131 | let secrets = get_cached_vault_secrets(); |
| 132 | for (key, val) in &secrets { |
| 133 | result = result.replace(val, &format!("[REDACTED_BY_ATLAS_VAULT:{}]", key)); |
| 134 | } |
| 135 | |
| 136 | // 2. Scrub standard API key patterns |
| 137 | let patterns = [ |
| 138 | ( |
| 139 | r"\b(sk-ant-[a-zA-Z0-9_-]{20,})\b", |
| 140 | "[REDACTED_ANTHROPIC_KEY]", |
| 141 | ), |
| 142 | (r"\b(ghp_[a-zA-Z0-9]{20,})\b", "[REDACTED_GITHUB_TOKEN]"), |
| 143 | (r"\b(AIzaSy[a-zA-Z0-9_-]{33})\b", "[REDACTED_GOOGLE_KEY]"), |
| 144 | (r"\b(sk-[a-zA-Z0-9_-]{20,})\b", "[REDACTED_OPENAI_KEY]"), |
| 145 | ]; |
| 146 | |
| 147 | for (pat, repl) in patterns { |
| 148 | if let Ok(re) = Regex::new(pat) { |
| 149 | result = re.replace_all(&result, repl).to_string(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | result |
| 154 | } |
| 155 | |
| 156 | /// Audit workspace directories for stray plaintext secret files (.env, etc.) |
| 157 | pub fn audit_workspace_secrets() -> Vec<String> { |
| 158 | let platform = crate::platform::PlatformContext::detect(); |
| 159 | let scan_dirs = [ |
| 160 | platform.home_dir.join("workspace"), |
| 161 | platform.home_dir.join("basecamp"), |
| 162 | platform.home_dir.join("spark-neural-os"), |
| 163 | platform.home_dir.join("aien-cli"), |
| 164 | ]; |
| 165 | |
| 166 | let mut findings = Vec::new(); |
| 167 | for d in &scan_dirs { |
| 168 | if !d.exists() { |
| 169 | continue; |
| 170 | } |
| 171 | let output = Command::new("find") |
| 172 | .args([ |
| 173 | d.to_str().unwrap_or("."), |
| 174 | "-maxdepth", |
| 175 | "4", |
| 176 | "-name", |
| 177 | ".env*", |
| 178 | "-not", |
| 179 | "-path", |
| 180 | "*/.git/*", |
| 181 | "-not", |
| 182 | "-path", |
| 183 | "*/target/*", |
| 184 | ]) |
| 185 | .output(); |
| 186 | |
| 187 | if let Ok(out) = output { |
| 188 | let stdout = String::from_utf8_lossy(&out.stdout); |
| 189 | for line in stdout.lines() { |
| 190 | let trimmed = line.trim(); |
| 191 | if !trimmed.is_empty() { |
| 192 | findings.push(trimmed.to_string()); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | findings |
| 198 | } |
| 199 | |
| 200 | pub fn vault_dispatch_tool(args: &Value) -> Value { |
| 201 | let action = args.get("action").and_then(Value::as_str).unwrap_or("list"); |
| 202 | match action { |
| 203 | "list" => { |
| 204 | let keys = list_keys(); |
| 205 | json!({ |
| 206 | "status": "ok", |
| 207 | "vault": "TPM-bound atlas-vault", |
| 208 | "registered_keys": keys, |
| 209 | "note": "Secret values reside in hardware TPM. Values are never written to disk or echoed in transcripts." |
| 210 | }) |
| 211 | } |
| 212 | "check" => { |
| 213 | let key = args.get("key").and_then(Value::as_str).unwrap_or(""); |
| 214 | if key.is_empty() { |
| 215 | return json!({"error": "Missing key argument"}); |
| 216 | } |
| 217 | let exists = has_key(key); |
| 218 | json!({ |
| 219 | "status": "ok", |
| 220 | "key": key, |
| 221 | "exists": exists, |
| 222 | "storage": "TPM-bound atlas-vault" |
| 223 | }) |
| 224 | } |
| 225 | "audit" => { |
| 226 | let findings = audit_workspace_secrets(); |
| 227 | json!({ |
| 228 | "status": "ok", |
| 229 | "stray_env_files_detected": findings.len(), |
| 230 | "findings": findings, |
| 231 | "policy": "Zero .env files on disk. Plaintext secrets strictly forbidden." |
| 232 | }) |
| 233 | } |
| 234 | other => json!({ |
| 235 | "error": format!("Unknown vault action {}. Supported actions: list, check, audit. Direct value retrieval is restricted to in-memory runtime execution.", other) |
| 236 | }), |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | #[cfg(test)] |
| 241 | mod tests { |
| 242 | use super::*; |
| 243 | |
| 244 | #[test] |
| 245 | fn test_secret_redaction_patterns() { |
| 246 | let sample = "Error with key sk-proj-1234567890abcdef123456 and token ghp_abcdefghijklmnopqrstuvwxyz12"; |
| 247 | let redacted = redact_secrets(sample); |
| 248 | assert!(!redacted.contains("sk-proj-1234567890abcdef123456")); |
| 249 | assert!(!redacted.contains("ghp_abcdefghijklmnopqrstuvwxyz12")); |
| 250 | assert!(redacted.contains("[REDACTED_OPENAI_KEY]")); |
| 251 | assert!(redacted.contains("[REDACTED_GITHUB_TOKEN]")); |
| 252 | } |
| 253 | } |