From 2ddb999058b8b968f2f7428c5bb81264528b9377 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Sat, 10 Oct 2026 15:47:56 +0200 Subject: [PATCH 01/13] feat(agent): read the token through a secret store --- Cargo.lock | 16 ++ Cargo.toml | 15 ++ src/api/manage_jobs.rs | 4 +- src/api/mod.rs | 45 ++++-- src/api/register_agent.rs | 2 +- src/config/settings.rs | 42 ++++- src/main.rs | 13 +- src/process/agent_job.rs | 6 +- src/process/keep_alive.rs | 6 +- src/secrets/config_store.rs | 41 +++++ src/secrets/import.rs | 83 ++++++++++ src/secrets/mod.rs | 77 +++++++++ src/secrets/replace.rs | 105 ++++++++++++ src/tests/api/client.rs | 76 +++++++-- src/tests/config/mod.rs | 1 + src/tests/config/settings_tests.rs | 25 +++ src/tests/mod.rs | 1 + src/tests/secrets/config_store_tests.rs | 41 +++++ src/tests/secrets/import_tests.rs | 202 ++++++++++++++++++++++++ src/tests/secrets/mod.rs | 78 +++++++++ src/tests/secrets/replace_tests.rs | 126 +++++++++++++++ 21 files changed, 959 insertions(+), 46 deletions(-) create mode 100644 src/secrets/config_store.rs create mode 100644 src/secrets/import.rs create mode 100644 src/secrets/mod.rs create mode 100644 src/secrets/replace.rs create mode 100644 src/tests/config/settings_tests.rs create mode 100644 src/tests/secrets/config_store_tests.rs create mode 100644 src/tests/secrets/import_tests.rs create mode 100644 src/tests/secrets/mod.rs create mode 100644 src/tests/secrets/replace_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 9e84981e..569f529f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1157,11 +1157,14 @@ dependencies = [ "rustls", "serde", "serde_json", + "toml_edit", "tracing-appender", "tracing-subscriber", "webpki-root-certs", "windows-service", "windows-service-detector", + "windows-sys 0.61.2", + "zeroize", ] [[package]] @@ -2047,6 +2050,19 @@ dependencies = [ "serde_core", ] +[[package]] +name = "toml_edit" +version = "0.25.17+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3641d5bbb5349a79e1020a242d251efbc546ad8048d133958323ce9c40a9c9c" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + [[package]] name = "toml_parser" version = "1.1.5+spec-1.1.0" diff --git a/Cargo.toml b/Cargo.toml index cecd12bf..683e837e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ mid = "=3.0.7" tracing-appender = "0.2.3" rolling-file = "0.2.0" webpki-root-certs = "1.0.10" +zeroize = "1.9.0" [dependencies.reqwest] version = "0.13.0" @@ -21,6 +22,14 @@ features = [ ] default-features = false +[dependencies.toml_edit] +version = "0.25.17" +features = [ + "parse", + "display", +] +default-features = false + [dependencies.serde] version = "1.0.203" features = [ @@ -46,6 +55,12 @@ features = [ windows-service = "0.8.0" windows-service-detector = "0.1.0" +[target."cfg(windows)".dependencies.windows-sys] +version = "0.61.2" +features = [ + "Win32_Storage_FileSystem", +] + [dev-dependencies] filetime = "0.2.25" mockito = "1.7.0" diff --git a/src/api/manage_jobs.rs b/src/api/manage_jobs.rs index e21904bf..721fcae2 100644 --- a/src/api/manage_jobs.rs +++ b/src/api/manage_jobs.rs @@ -36,7 +36,7 @@ impl Client { "agent_executed_by_user": executed_by_user }); let jobs_endpoint = format!("/api/tenants/{}/endpoints/jobs", tenant_id); - match self.post(&jobs_endpoint).json(&post_data).send() { + match self.post(&jobs_endpoint)?.json(&post_data).send() { Ok(response) => { if response.status().is_success() { response @@ -59,7 +59,7 @@ impl Client { pub fn clean_job(&self, job_id: &str, tenant_id: String) -> Result<(), Error> { // Post the input to the OpenAEV API let jobs_endpoint = format!("/api/tenants/{}/endpoints/jobs/{}", tenant_id, job_id); - match self.delete(&jobs_endpoint).send() { + match self.delete(&jobs_endpoint)?.send() { Ok(response) => { if response.status().is_success() { Ok(()) diff --git a/src/api/mod.rs b/src/api/mod.rs index 1bf4aa71..f2f9cfff 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,9 @@ +use crate::common::error_model::Error; +use crate::secrets::{SecretStore, TOKEN}; +use reqwest::header::HeaderValue; +use std::sync::Arc; use std::time::Duration; +use zeroize::Zeroizing; mod manage_jobs; mod register_agent; @@ -6,17 +11,16 @@ mod register_agent; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const AUTHORIZATION_HEADER: &str = "Authorization"; -#[derive(Debug)] pub struct Client { http_client: reqwest::blocking::Client, server_url: String, - token: String, + secrets: Arc, } impl Client { pub fn new( server_url: String, - token: String, + secrets: Arc, unsecured_certificate: bool, with_proxy: bool, ) -> Client { @@ -48,29 +52,44 @@ impl Client { Client { http_client: http_client.build().unwrap(), server_url: url, - token, + secrets, } } + /// Reads the token from the store for this request only, so it does not stay in memory + /// between requests. + fn authorization(&self) -> Result { + let token = self.secrets.get(TOKEN)?; + let bearer = Zeroizing::new(format!("Bearer {}", token.as_str())); + let mut value = + HeaderValue::from_str(&bearer).map_err(|err| Error::Internal(err.to_string()))?; + // Keeps it out of reqwest's debug output. + value.set_sensitive(true); + Ok(value) + } + #[cfg(test)] - pub fn get(&self, route: &str) -> reqwest::blocking::RequestBuilder { + pub fn get(&self, route: &str) -> Result { let api_route = format!("{}{}", self.server_url, route); - self.http_client + Ok(self + .http_client .get(&api_route) - .header(AUTHORIZATION_HEADER, &format!("Bearer {}", self.token)) + .header(AUTHORIZATION_HEADER, self.authorization()?)) } - pub fn post(&self, route: &str) -> reqwest::blocking::RequestBuilder { + pub fn post(&self, route: &str) -> Result { let api_route = format!("{}{}", self.server_url, route); - self.http_client + Ok(self + .http_client .post(&api_route) - .header(AUTHORIZATION_HEADER, &format!("Bearer {}", self.token)) + .header(AUTHORIZATION_HEADER, self.authorization()?)) } - pub fn delete(&self, route: &str) -> reqwest::blocking::RequestBuilder { + pub fn delete(&self, route: &str) -> Result { let api_route = format!("{}{}", self.server_url, route); - self.http_client + Ok(self + .http_client .delete(&api_route) - .header(AUTHORIZATION_HEADER, &format!("Bearer {}", self.token)) + .header(AUTHORIZATION_HEADER, self.authorization()?)) } } diff --git a/src/api/register_agent.rs b/src/api/register_agent.rs index eca7e6fb..b5da5f55 100644 --- a/src/api/register_agent.rs +++ b/src/api/register_agent.rs @@ -117,7 +117,7 @@ impl Client { // endregion // Post the input to the OpenAEV API let register_endpoint = format!("/api/tenants/{}/endpoints/register", tenant_id); - match self.post(®ister_endpoint).json(&post_data).send() { + match self.post(®ister_endpoint)?.json(&post_data).send() { Ok(response) => { if response.status().is_success() { response diff --git a/src/config/settings.rs b/src/config/settings.rs index 11a85ff1..963c1ded 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -1,15 +1,18 @@ use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::env; +use std::fmt; +use std::path::PathBuf; -const ENV_PRODUCTION: &str = "production"; +pub const ENV_PRODUCTION: &str = "production"; const ENV_PRODUCTION_CONFIG_FILE: &str = "openaev-agent-config"; -#[derive(Debug, Deserialize, Clone)] +#[derive(Deserialize, Clone)] #[allow(unused)] pub struct OpenAEV { pub url: String, - pub token: String, + // Optional: once moved to the OS secret store, the token is no longer in the config. + pub token: Option, pub unsecured_certificate: bool, pub with_proxy: bool, pub installation_mode: String, @@ -17,6 +20,21 @@ pub struct OpenAEV { pub tenant_id: String, } +// Written by hand so that printing the settings never prints the token. +impl fmt::Debug for OpenAEV { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OpenAEV") + .field("url", &self.url) + .field("token", &self.token.as_ref().map(|_| "")) + .field("unsecured_certificate", &self.unsecured_certificate) + .field("with_proxy", &self.with_proxy) + .field("installation_mode", &self.installation_mode) + .field("service_name", &self.service_name) + .field("tenant_id", &self.tenant_id) + .finish() + } +} + fn default_executing_max_time_minutes() -> u64 { 10 } @@ -60,15 +78,23 @@ impl Settings { env::var("env").unwrap_or_else(|_| ENV_PRODUCTION.into()) } + /// The directory of the executable, where the installers put the config and the token. + pub fn install_dir() -> PathBuf { + let current_exe_patch = env::current_exe().unwrap(); + current_exe_patch.parent().unwrap().to_path_buf() + } + + /// The config file the installers write in production. + pub fn production_config_path() -> PathBuf { + Self::install_dir().join(format!("{ENV_PRODUCTION_CONFIG_FILE}.toml")) + } + pub fn new() -> Result { let run_mode = Self::mode(); let config = Config::builder().add_source(Environment::with_prefix("openaev")); if run_mode == ENV_PRODUCTION { - // Get the current executable path - let current_exe_patch = env::current_exe().unwrap(); - let parent_path = current_exe_patch.parent().unwrap(); - // Join the expected config file with the parent - let config_file = parent_path.join(ENV_PRODUCTION_CONFIG_FILE); + // Join the expected config file with the executable directory + let config_file = Self::install_dir().join(ENV_PRODUCTION_CONFIG_FILE); let config_path = config_file.display(); config .add_source(File::with_name(&config_path.to_string()).required(true)) diff --git a/src/main.rs b/src/main.rs index 3db4d690..b2e5b386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod api; mod common; mod config; mod process; +mod secrets; mod windows; #[cfg(test)] @@ -24,6 +25,7 @@ use crate::process::agent_job; use crate::process::{agent_cleanup, keep_alive}; use crate::windows::service::service_stub; use tracing_subscriber::filter::LevelFilter; +use zeroize::Zeroizing; pub static THREADS_CONTROL: AtomicBool = AtomicBool::new(true); const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -59,9 +61,12 @@ fn compute_working_dir() -> PathBuf { current_exe_path.parent().unwrap().to_path_buf() } -fn agent_start(settings_data: Settings, is_service: bool) -> Result>, Error> { +fn agent_start( + mut settings_data: Settings, + is_service: bool, +) -> Result>, Error> { let url = settings_data.openaev.url; - let token = settings_data.openaev.token; + let secrets = secrets::open(settings_data.openaev.token.take().map(Zeroizing::new)); let unsecured_certificate = settings_data.openaev.unsecured_certificate; let with_proxy = settings_data.openaev.with_proxy; let installation_mode = settings_data.openaev.installation_mode; @@ -81,7 +86,7 @@ fn agent_start(settings_data: Settings, is_service: bool) -> Result Result, unsecured_certificate: bool, with_proxy: bool, execution_details: ExecutionDetails, tenant_id: String, ) -> Result, Error> { info!("Starting listening jobs thread"); - let api = Client::new(uri, token, unsecured_certificate, with_proxy); + let api = Client::new(uri, secrets, unsecured_certificate, with_proxy); let handle = thread::spawn(move || { // While no stop signal received while THREADS_CONTROL.load(Ordering::Relaxed) { diff --git a/src/process/keep_alive.rs b/src/process/keep_alive.rs index 74d7e105..937faa9b 100644 --- a/src/process/keep_alive.rs +++ b/src/process/keep_alive.rs @@ -1,9 +1,11 @@ use crate::api::Client; use crate::config::execution_details::ExecutionDetails; +use crate::secrets::SecretStore; use crate::THREADS_CONTROL; use log::{error, info}; use std::io::Error; use std::sync::atomic::Ordering; +use std::sync::Arc; use std::thread; use std::thread::{sleep, JoinHandle}; use std::time::Duration; @@ -11,7 +13,7 @@ use std::time::Duration; #[allow(clippy::too_many_arguments)] pub fn ping( uri: String, - token: String, + secrets: Arc, unsecured_certificate: bool, with_proxy: bool, installation_mode: String, @@ -20,7 +22,7 @@ pub fn ping( tenant_id: String, ) -> Result, Error> { info!("Starting ping thread"); - let api = Client::new(uri, token, unsecured_certificate, with_proxy); + let api = Client::new(uri, secrets, unsecured_certificate, with_proxy); let handle = thread::spawn(move || { // While no stop signal received while THREADS_CONTROL.load(Ordering::Relaxed) { diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs new file mode 100644 index 00000000..905b2b72 --- /dev/null +++ b/src/secrets/config_store.rs @@ -0,0 +1,41 @@ +use super::{read_bootstrap, SecretStore, TOKEN}; +use crate::common::error_model::Error; +use crate::config::settings::Settings; +use std::path::PathBuf; +use zeroize::Zeroizing; + +/// The token where the installers leave it in clear text: the bootstrap file when one is +/// waiting, otherwise the config. Used where no OS secret store is supported yet, or when +/// moving the token there failed. Read again on each use, never kept in memory. +pub struct ConfigStore { + bootstrap: PathBuf, +} + +impl ConfigStore { + pub fn new(bootstrap: PathBuf) -> Self { + ConfigStore { bootstrap } + } +} + +impl SecretStore for ConfigStore { + fn get(&self, name: &str) -> Result, Error> { + if name != TOKEN { + return Err(Error::Internal(format!("no secret named {name}"))); + } + if let Some(token) = read_bootstrap(&self.bootstrap)? { + return Ok(token); + } + let settings = Settings::new().map_err(|err| Error::Internal(err.to_string()))?; + settings + .openaev + .token + .map(Zeroizing::new) + .ok_or_else(|| Error::Internal("no token in the agent config".to_string())) + } + + fn set(&self, _name: &str, _value: &str) -> Result<(), Error> { + Err(Error::Internal( + "the agent does not write secrets to its config".to_string(), + )) + } +} diff --git a/src/secrets/import.rs b/src/secrets/import.rs new file mode 100644 index 00000000..9f49dbce --- /dev/null +++ b/src/secrets/import.rs @@ -0,0 +1,83 @@ +use super::{read_bootstrap, replace_file, SecretStore, TOKEN}; +use crate::common::error_model::Error; +use log::{info, warn}; +use std::fs; +use std::path::Path; +use toml_edit::{DocumentMut, Item}; +use zeroize::Zeroizing; + +/// The table of the config that holds the token. +const CONFIG_TABLE: &str = "openaev"; + +/// Moves the clear-text token into `store`, then removes the clear-text copies. +/// +/// The token comes from the bootstrap file when there is one, otherwise from the config +/// (`config_token`). Safe to run on every start: a failure leaves the clear-text copies in +/// place for the next attempt. +pub fn import( + store: &dyn SecretStore, + bootstrap: &Path, + config_token: Option<&str>, + config: &Path, +) -> Result<(), Error> { + let bootstrap_token = read_bootstrap(bootstrap)?; + let token = bootstrap_token + .as_ref() + .map(|token| token.as_str()) + .or(config_token) + .filter(|token| !token.is_empty()); + + if let Some(token) = token { + let already_stored = store + .get(TOKEN) + .is_ok_and(|stored| stored.as_str() == token); + if !already_stored { + store.set(TOKEN, token)?; + if store.get(TOKEN)?.as_str() != token { + return Err(Error::Internal( + "the token read back from the store differs from the one written".to_string(), + )); + } + info!("Token moved to the OS secret store"); + } + } + + if bootstrap_token.is_some() { + fs::remove_file(bootstrap)?; + } + if config_token.is_some() { + // The token is safe in the store by now: a config left as it was is worth a + // warning, not an agent that refuses to start. + if let Err(err) = remove_token_from_config(config) { + warn!( + "The token is still in clear text in {}: {err}", + config.display() + ); + } + } + Ok(()) +} + +/// Removes the token from the `[openaev]` table of the config, keeping everything else (other +/// tables, comments, formatting) as it is. Does nothing when the table has no token, for +/// example when it came from an environment variable. +fn remove_token_from_config(config: &Path) -> Result<(), Error> { + let content = Zeroizing::new(fs::read_to_string(config)?); + // The parse error is not shown: it quotes the line it failed on, which can be the token. + let mut document: DocumentMut = content + .parse() + .map_err(|_| Error::Internal("the config is not valid TOML".to_string()))?; + let removed = document + .get_mut(CONFIG_TABLE) + .and_then(Item::as_table_like_mut) + .and_then(|table| table.remove(TOKEN)); + if removed.is_some() { + let mut kept = Zeroizing::new(document.to_string()); + // toml_edit writes LF line endings; keep CRLF when the installer wrote it. + if content.contains("\r\n") { + kept = Zeroizing::new(kept.replace("\r\n", "\n").replace('\n', "\r\n")); + } + replace_file(config, kept.as_bytes())?; + } + Ok(()) +} diff --git a/src/secrets/mod.rs b/src/secrets/mod.rs new file mode 100644 index 00000000..d9f50057 --- /dev/null +++ b/src/secrets/mod.rs @@ -0,0 +1,77 @@ +//! Where the agent keeps the secrets it authenticates with. +//! +//! The installers hand the token over in clear text, in a bootstrap file next to the +//! executable (older installers wrote it in the config). In production the agent moves it +//! into the OS secret store when the platform has one, and reads it back from there on +//! each use rather than keeping it in memory. + +mod config_store; +mod import; +mod replace; + +pub use config_store::ConfigStore; +pub use import::import; +pub(crate) use replace::replace_file; + +use crate::common::error_model::Error; +use crate::config::settings::{Settings, ENV_PRODUCTION}; +use log::error; +use std::fs; +use std::io::ErrorKind; +use std::path::Path; +use std::sync::Arc; +use zeroize::Zeroizing; + +/// Name of the secret the agent authenticates to the platform with. +pub const TOKEN: &str = "token"; +/// Clear-text token written by the installers, removed once moved to the store. +pub const BOOTSTRAP_FILE: &str = "openaev-agent-token.bootstrap"; + +pub trait SecretStore: Send + Sync { + /// Reads the secret. The returned value is wiped from memory when dropped. + fn get(&self, name: &str) -> Result, Error>; + /// Replaces the secret; the next `get` returns the new value. + fn set(&self, name: &str, value: &str) -> Result<(), Error>; +} + +/// Opens the store the agent reads its token from. +/// +/// `config_token` is the token found in the config, if an older installer left it there. +pub fn open(config_token: Option>) -> Arc { + let install_dir = Settings::install_dir(); + let bootstrap = install_dir.join(BOOTSTRAP_FILE); + if Settings::mode() == ENV_PRODUCTION { + if let Some(store) = platform_store(&install_dir) { + let config_token = config_token.as_ref().map(|token| token.as_str()); + match import( + store.as_ref(), + &bootstrap, + config_token, + &Settings::production_config_path(), + ) { + Ok(()) => return store, + // Staying online on the clear-text copy beats losing the agent. + Err(err) => error!("Could not move the token to the OS secret store: {err}"), + } + } + } + Arc::new(ConfigStore::new(bootstrap)) +} + +/// The OS secret store of this platform, if the agent supports one there. +fn platform_store(_install_dir: &Path) -> Option> { + None +} + +/// Reads the token the installer left in the bootstrap file, if there is one. +pub(crate) fn read_bootstrap(path: &Path) -> Result>, Error> { + match fs::read_to_string(path) { + Ok(content) => { + let content = Zeroizing::new(content); + let token = content.trim(); + Ok((!token.is_empty()).then(|| Zeroizing::new(token.to_string()))) + } + Err(err) if err.kind() == ErrorKind::NotFound => Ok(None), + Err(err) => Err(Error::Io(err)), + } +} diff --git a/src/secrets/replace.rs b/src/secrets/replace.rs new file mode 100644 index 00000000..920b7fa5 --- /dev/null +++ b/src/secrets/replace.rs @@ -0,0 +1,105 @@ +use std::fs; +use std::fs::{File, OpenOptions}; +use std::io; +use std::io::{ErrorKind, Write}; +use std::path::{Path, PathBuf}; + +/// Replaces the content of `path` with `content`, never leaving it half-written. +/// +/// The content is written and synced to a temporary file next to `path`, which then takes +/// its place in one step: if anything fails (full disk, missing rights), `path` keeps its +/// previous content. The new file keeps the permissions of the one it replaces (owner and +/// mode on Unix, ACL on Windows), which the installers restricted. +pub(crate) fn replace_file(path: &Path, content: &[u8]) -> io::Result<()> { + let temp = temp_path(path); + // Left over by an interrupted run: never reused, it could hold anything. + remove_if_exists(&temp)?; + let result = write_temp(path, &temp, content).and_then(|()| swap(path, &temp)); + if result.is_err() { + let _ = fs::remove_file(&temp); + } + result +} + +fn temp_path(path: &Path) -> PathBuf { + let mut name = path.file_name().unwrap_or_default().to_os_string(); + name.push(".tmp"); + path.with_file_name(name) +} + +fn remove_if_exists(path: &Path) -> io::Result<()> { + match fs::remove_file(path) { + Err(err) if err.kind() != ErrorKind::NotFound => Err(err), + _ => Ok(()), + } +} + +fn write_temp(path: &Path, temp: &Path, content: &[u8]) -> io::Result<()> { + let mut file = create_temp(path, temp)?; + file.write_all(content)?; + file.sync_all() +} + +#[cfg(unix)] +fn create_temp(path: &Path, temp: &Path) -> io::Result { + use std::os::unix::fs::{fchown, MetadataExt, OpenOptionsExt, PermissionsExt}; + + let metadata = fs::metadata(path)?; + // Readable by its creator only, then given the owner and mode of the file it replaces, + // all before anything is written to it. + let file = OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(temp)?; + fchown(&file, Some(metadata.uid()), Some(metadata.gid()))?; + file.set_permissions(fs::Permissions::from_mode(metadata.mode() & 0o777))?; + Ok(file) +} + +#[cfg(unix)] +fn swap(path: &Path, temp: &Path) -> io::Result<()> { + fs::rename(temp, path)?; + // The rename is only durable once the directory holding it is synced. + let dir = path + .parent() + .filter(|dir| !dir.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + File::open(dir)?.sync_all() +} + +#[cfg(windows)] +fn create_temp(_path: &Path, temp: &Path) -> io::Result { + // Gets the folder's ACL until ReplaceFileW gives it the one of the file it replaces. + // It never holds a secret: callers write content they removed the secrets from. + OpenOptions::new().write(true).create_new(true).open(temp) +} + +#[cfg(windows)] +fn swap(path: &Path, temp: &Path) -> io::Result<()> { + use std::os::windows::ffi::OsStrExt; + use std::ptr; + use windows_sys::Win32::Storage::FileSystem::ReplaceFileW; + + let wide = |path: &Path| -> Vec { path.as_os_str().encode_wide().chain([0]).collect() }; + let (replaced, replacement) = (wide(path), wide(temp)); + // ReplaceFileW gives the new file the ACL and attributes of the one it replaces, and + // fails rather than dropping them. Without a backup file, a failure leaves `path` as it + // was. + // SAFETY: both paths are NUL-terminated and alive for the whole call; the backup file + // and the reserved parameters may be null. + let ok = unsafe { + ReplaceFileW( + replaced.as_ptr(), + replacement.as_ptr(), + ptr::null(), + 0, + ptr::null(), + ptr::null(), + ) + }; + if ok == 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) +} diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index b8e97eb2..3a1e2813 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -5,10 +5,12 @@ mod tests { use crate::api::{Client, AUTHORIZATION_HEADER}; use crate::tests::api::client::TOKEN; use crate::tests::api::tls_server::serve_self_signed_https; + use crate::tests::secrets::{FailingStore, MemoryStore}; use mockito; use std::env; use std::error::Error; use std::io; + use std::sync::Arc; fn is_invalid_certificate(err: &reqwest::Error) -> bool { let mut current: Option<&(dyn Error + 'static)> = Some(err); @@ -39,13 +41,46 @@ mod tests { .match_header(AUTHORIZATION_HEADER, format!("Bearer {}", TOKEN).as_str()) .with_status(200) .create(); - let client = Client::new(server_url, TOKEN.to_string(), false, false); + let client = Client::new(server_url, MemoryStore::with_token(TOKEN), false, false); // -- EXECUTE & ASSERT -- - let res = client.post("/api/test").send(); + let res = client.post("/api/test").unwrap().send(); assert!(res.is_ok(), "User-Agent should match expected format"); } + #[test] + fn test_client_fails_the_request_without_a_token() { + // -- PREPARE -- + let client = Client::new( + "http://localhost".to_string(), + Arc::new(FailingStore), + false, + false, + ); + + // -- EXECUTE & ASSERT -- + assert!(client.post("/api/test").is_err()); + } + + #[test] + fn test_client_keeps_the_token_out_of_debug_output() { + // -- PREPARE -- + let client = Client::new( + "http://localhost".to_string(), + MemoryStore::with_token(TOKEN), + false, + false, + ); + + // -- EXECUTE -- + let request = client.post("/api/test").unwrap().build().unwrap(); + + // -- ASSERT -- + let header = request.headers().get(AUTHORIZATION_HEADER).unwrap(); + assert!(header.is_sensitive()); + assert!(!format!("{request:?}").contains(TOKEN)); + } + #[test] fn test_with_proxy_disables_http_proxy() { // -- PREPARE -- @@ -54,12 +89,22 @@ mod tests { let mut server = mockito::Server::new(); let server_url = server.url(); server.mock("POST", "/api/test").with_status(200).create(); - let client_without_proxy = Client::new(server_url.clone(), TOKEN.to_string(), false, false); - let client_with_proxy = Client::new(server_url.clone(), TOKEN.to_string(), false, true); + let client_without_proxy = Client::new( + server_url.clone(), + MemoryStore::with_token(TOKEN), + false, + false, + ); + let client_with_proxy = Client::new( + server_url.clone(), + MemoryStore::with_token(TOKEN), + false, + true, + ); // -- EXECUTE -- - let res_without_proxy = client_without_proxy.post("/api/test").send(); - let res_with_proxy = client_with_proxy.post("/api/test").send(); + let res_without_proxy = client_without_proxy.post("/api/test").unwrap().send(); + let res_with_proxy = client_with_proxy.post("/api/test").unwrap().send(); // -- ASSERT -- assert!(res_without_proxy.is_ok(), "Client should bypass the proxy"); @@ -77,12 +122,15 @@ mod tests { // -- PREPARE -- let url = serve_self_signed_https(); let client_without_unsecured_certificate = - Client::new(url.clone(), TOKEN.to_string(), false, false); - let client_with_unsecured_certificate = Client::new(url, TOKEN.to_string(), true, false); + Client::new(url.clone(), MemoryStore::with_token(TOKEN), false, false); + let client_with_unsecured_certificate = + Client::new(url, MemoryStore::with_token(TOKEN), true, false); // -- EXECUTE -- - let res_without_unsecured_certificate = client_without_unsecured_certificate.get("").send(); - let res_with_unsecured_certificate = client_with_unsecured_certificate.get("").send(); + let res_without_unsecured_certificate = + client_without_unsecured_certificate.get("").unwrap().send(); + let res_with_unsecured_certificate = + client_with_unsecured_certificate.get("").unwrap().send(); // -- ASSERT -- assert!( @@ -106,10 +154,10 @@ mod tests { // the CI job empties the OS trust store, so only the bundled roots can validate this URL let url = env::var("OAEV_BUNDLED_ROOTS_URL") .expect("OAEV_BUNDLED_ROOTS_URL must be set by the os-trust-store CI job"); - let client = Client::new(url, TOKEN.to_string(), false, false); + let client = Client::new(url, MemoryStore::with_token(TOKEN), false, false); // -- EXECUTE -- - let res = client.get("").send(); + let res = client.get("").unwrap().send(); // -- ASSERT -- assert!( @@ -125,11 +173,11 @@ mod tests { // -- PREPARE -- let url = env::var("OAEV_OS_TRUST_URL") .expect("OAEV_OS_TRUST_URL must be set by the os-trust-store CI job"); - let client = Client::new(url, TOKEN.to_string(), false, false); + let client = Client::new(url, MemoryStore::with_token(TOKEN), false, false); // -- EXECUTE & ASSERT -- assert!( - client.get("").send().is_ok(), + client.get("").unwrap().send().is_ok(), "Client should trust a CA present only in the OS trust store" ); } diff --git a/src/tests/config/mod.rs b/src/tests/config/mod.rs index 38c01cf6..3b13f58c 100644 --- a/src/tests/config/mod.rs +++ b/src/tests/config/mod.rs @@ -1 +1,2 @@ mod execution_details_tests; +mod settings_tests; diff --git a/src/tests/config/settings_tests.rs b/src/tests/config/settings_tests.rs new file mode 100644 index 00000000..3caedee4 --- /dev/null +++ b/src/tests/config/settings_tests.rs @@ -0,0 +1,25 @@ +#[cfg(test)] +mod tests { + use crate::config::settings::OpenAEV; + + #[test] + fn test_debug_output_redacts_the_token() { + // -- PREPARE -- + let settings = OpenAEV { + url: "http://localhost:3001".to_string(), + token: Some("secret-token".to_string()), + unsecured_certificate: false, + with_proxy: false, + installation_mode: "service".to_string(), + service_name: "OAEVAgentService".to_string(), + tenant_id: "tenant".to_string(), + }; + + // -- EXECUTE -- + let output = format!("{settings:?}"); + + // -- ASSERT -- + assert!(!output.contains("secret-token")); + assert!(output.contains("")); + } +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index caa31c5a..8bb934d0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,3 +1,4 @@ pub mod api; pub mod config; pub mod process; +pub mod secrets; diff --git a/src/tests/secrets/config_store_tests.rs b/src/tests/secrets/config_store_tests.rs new file mode 100644 index 00000000..fd4bad0a --- /dev/null +++ b/src/tests/secrets/config_store_tests.rs @@ -0,0 +1,41 @@ +#[cfg(test)] +mod tests { + use crate::secrets::{ConfigStore, SecretStore, BOOTSTRAP_FILE, TOKEN}; + use crate::tests::secrets::test_dir; + use std::fs; + + #[test] + fn test_config_store_reads_the_bootstrap_file_on_each_use() { + // -- PREPARE -- + let dir = test_dir("config-store-bootstrap"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + fs::write(&bootstrap, "first-token\n").unwrap(); + let store = ConfigStore::new(bootstrap.clone()); + + // -- EXECUTE & ASSERT -- + assert_eq!(store.get(TOKEN).unwrap().as_str(), "first-token"); + fs::write(&bootstrap, "second-token").unwrap(); + assert_eq!(store.get(TOKEN).unwrap().as_str(), "second-token"); + } + + #[test] + fn test_config_store_refuses_writes() { + // -- PREPARE -- + let store = ConfigStore::new(test_dir("config-store-write").join(BOOTSTRAP_FILE)); + + // -- EXECUTE & ASSERT -- + assert!(store.set(TOKEN, "token").is_err()); + } + + #[test] + fn test_config_store_only_holds_the_token() { + // -- PREPARE -- + let dir = test_dir("config-store-name"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + fs::write(&bootstrap, "token").unwrap(); + let store = ConfigStore::new(bootstrap); + + // -- EXECUTE & ASSERT -- + assert!(store.get("refresh_token").is_err()); + } +} diff --git a/src/tests/secrets/import_tests.rs b/src/tests/secrets/import_tests.rs new file mode 100644 index 00000000..e48e738f --- /dev/null +++ b/src/tests/secrets/import_tests.rs @@ -0,0 +1,202 @@ +#[cfg(test)] +mod tests { + use crate::secrets::{import, SecretStore, BOOTSTRAP_FILE, TOKEN}; + use crate::tests::secrets::{test_dir, FailingStore, MemoryStore}; + use std::fs; + use std::sync::atomic::Ordering; + + const CONFIG: &str = "debug=false\r\n\r\n[openaev]\r\nurl = \"http://localhost:3001\"\r\ntoken = \"legacy-token\"\r\ntenant_id = \"tenant\"\r\n"; + const CONFIG_WITHOUT_TOKEN: &str = + "debug=false\r\n\r\n[openaev]\r\nurl = \"http://localhost:3001\"\r\ntenant_id = \"tenant\"\r\n"; + + #[test] + fn test_import_moves_the_bootstrap_token_and_deletes_the_file() { + // -- PREPARE -- + let dir = test_dir("import-bootstrap"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "bootstrap-token\r\n").unwrap(); + fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + import(&store, &bootstrap, None, &config).unwrap(); + + // -- ASSERT -- + assert_eq!(store.get(TOKEN).unwrap().as_str(), "bootstrap-token"); + assert!( + !bootstrap.exists(), + "the clear-text bootstrap file should be deleted" + ); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } + + #[test] + fn test_import_moves_the_config_token_and_removes_only_its_line() { + // -- PREPARE -- + let dir = test_dir("import-config"); + let config = dir.join("config.toml"); + fs::write(&config, CONFIG).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + import( + &store, + &dir.join(BOOTSTRAP_FILE), + Some("legacy-token"), + &config, + ) + .unwrap(); + + // -- ASSERT -- + assert_eq!(store.get(TOKEN).unwrap().as_str(), "legacy-token"); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } + + #[test] + fn test_import_prefers_the_bootstrap_token_and_cleans_both_copies() { + // -- PREPARE -- + let dir = test_dir("import-both"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "new-token").unwrap(); + fs::write(&config, CONFIG).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + import(&store, &bootstrap, Some("legacy-token"), &config).unwrap(); + + // -- ASSERT -- + assert_eq!(store.get(TOKEN).unwrap().as_str(), "new-token"); + assert!(!bootstrap.exists()); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } + + #[test] + fn test_import_keeps_the_clear_text_copies_when_the_store_fails() { + // -- PREPARE -- + let dir = test_dir("import-failure"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "bootstrap-token").unwrap(); + fs::write(&config, CONFIG).unwrap(); + + // -- EXECUTE -- + let result = import(&FailingStore, &bootstrap, Some("legacy-token"), &config); + + // -- ASSERT -- + assert!(result.is_err()); + assert!(bootstrap.exists(), "the token must not be lost"); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG); + } + + #[test] + fn test_import_does_not_rewrite_a_token_already_stored() { + // -- PREPARE -- + let dir = test_dir("import-idempotent"); + let config = dir.join("config.toml"); + fs::write(&config, CONFIG).unwrap(); + let store = MemoryStore::with_token("legacy-token"); + + // -- EXECUTE -- + import( + store.as_ref(), + &dir.join(BOOTSTRAP_FILE), + Some("legacy-token"), + &config, + ) + .unwrap(); + + // -- ASSERT -- + assert_eq!(store.writes.load(Ordering::SeqCst), 0); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } + + #[test] + fn test_import_without_any_token_changes_nothing() { + // -- PREPARE -- + let dir = test_dir("import-nothing"); + let config = dir.join("config.toml"); + fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + import(&store, &dir.join(BOOTSTRAP_FILE), None, &config).unwrap(); + + // -- ASSERT -- + assert_eq!(store.writes.load(Ordering::SeqCst), 0); + assert!(store.get(TOKEN).is_err()); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } + + /// Imports `legacy-token` from a config holding `content`, and returns the config after. + fn import_config(name: &str, content: &str) -> String { + let dir = test_dir(name); + let config = dir.join("config.toml"); + fs::write(&config, content).unwrap(); + let store = MemoryStore::default(); + import( + &store, + &dir.join(BOOTSTRAP_FILE), + Some("legacy-token"), + &config, + ) + .unwrap(); + assert_eq!(store.get(TOKEN).unwrap().as_str(), "legacy-token"); + fs::read_to_string(&config).unwrap() + } + + #[test] + fn test_import_removes_only_the_token_of_the_openaev_table() { + // -- PREPARE -- + let content = "token = \"root\"\n\n[openaev]\ntoken_file = \"x\"\ntokens = 2\n token = \"legacy-token\" # from the installer\n\n[other]\ntoken = \"kept\"\n"; + + // -- EXECUTE -- + let after = import_config("import-scoped", content); + + // -- ASSERT -- + assert_eq!( + after, + "token = \"root\"\n\n[openaev]\ntoken_file = \"x\"\ntokens = 2\n\n[other]\ntoken = \"kept\"\n" + ); + } + + #[test] + fn test_import_removes_a_quoted_token_key() { + // -- PREPARE -- + let content = "[openaev]\nurl = \"http://localhost:3001\"\n\"token\" = \"legacy-token\"\n"; + + // -- EXECUTE -- + let after = import_config("import-quoted", content); + + // -- ASSERT -- + assert_eq!(after, "[openaev]\nurl = \"http://localhost:3001\"\n"); + } + + #[test] + fn test_import_removes_a_dotted_token_key() { + // -- PREPARE -- + let content = "openaev.url = \"http://localhost:3001\"\nopenaev.token = \"legacy-token\"\n"; + + // -- EXECUTE -- + let after = import_config("import-dotted", content); + + // -- ASSERT -- + assert_eq!(after, "openaev.url = \"http://localhost:3001\"\n"); + } + + #[test] + fn test_import_leaves_a_config_it_cannot_parse_as_it_was() { + // -- PREPARE -- + let content = "[openaev]\ntoken = \"legacy-token\"\nbroken =\n"; + + // -- EXECUTE -- + let after = import_config("import-invalid", content); + + // -- ASSERT -- + assert_eq!( + after, content, + "a config that cannot be parsed is not touched" + ); + } +} diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs new file mode 100644 index 00000000..8bdf1069 --- /dev/null +++ b/src/tests/secrets/mod.rs @@ -0,0 +1,78 @@ +mod config_store_tests; +mod import_tests; +mod replace_tests; + +use crate::common::error_model::Error; +use crate::secrets::SecretStore; +use std::collections::HashMap; +use std::env; +use std::fs; +use std::path::PathBuf; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use zeroize::Zeroizing; + +/// A secret store kept in memory, standing in for the OS one. +#[derive(Default)] +pub struct MemoryStore { + secrets: Mutex>, + pub writes: AtomicUsize, +} + +impl MemoryStore { + pub fn with_token(token: &str) -> Arc { + let store = MemoryStore::default(); + store + .secrets + .lock() + .unwrap() + .insert(crate::secrets::TOKEN.to_string(), token.to_string()); + Arc::new(store) + } +} + +impl SecretStore for MemoryStore { + fn get(&self, name: &str) -> Result, Error> { + self.secrets + .lock() + .unwrap() + .get(name) + .map(|value| Zeroizing::new(value.clone())) + .ok_or_else(|| Error::Internal(format!("no secret named {name}"))) + } + + fn set(&self, name: &str, value: &str) -> Result<(), Error> { + self.writes.fetch_add(1, Ordering::SeqCst); + self.secrets + .lock() + .unwrap() + .insert(name.to_string(), value.to_string()); + Ok(()) + } +} + +/// A secret store that cannot be written, like an OS store that is unavailable. +pub struct FailingStore; + +impl SecretStore for FailingStore { + fn get(&self, name: &str) -> Result, Error> { + Err(Error::Internal(format!("no secret named {name}"))) + } + + fn set(&self, _name: &str, _value: &str) -> Result<(), Error> { + Err(Error::Internal("store unavailable".to_string())) + } +} + +/// A fresh, empty directory for one test. +pub fn test_dir(name: &str) -> PathBuf { + static COUNTER: AtomicUsize = AtomicUsize::new(0); + let dir = env::temp_dir().join(format!( + "openaev-agent-{name}-{}-{}", + std::process::id(), + COUNTER.fetch_add(1, Ordering::SeqCst) + )); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir +} diff --git a/src/tests/secrets/replace_tests.rs b/src/tests/secrets/replace_tests.rs new file mode 100644 index 00000000..f37279e0 --- /dev/null +++ b/src/tests/secrets/replace_tests.rs @@ -0,0 +1,126 @@ +#[cfg(test)] +mod tests { + use crate::secrets::replace_file; + use crate::tests::secrets::test_dir; + use std::fs; + + #[test] + fn test_replace_file_writes_the_content_and_leaves_no_temporary_file() { + // -- PREPARE -- + let dir = test_dir("replace-content"); + let path = dir.join("config.toml"); + fs::write(&path, "old content").unwrap(); + + // -- EXECUTE -- + replace_file(&path, b"new content").unwrap(); + + // -- ASSERT -- + assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); + assert_eq!(fs::read_dir(&dir).unwrap().count(), 1); + } + + #[test] + fn test_replace_file_ignores_a_temporary_file_left_by_an_interrupted_run() { + // -- PREPARE -- + let dir = test_dir("replace-leftover"); + let path = dir.join("config.toml"); + fs::write(&path, "old content").unwrap(); + fs::write(dir.join("config.toml.tmp"), "leftover").unwrap(); + + // -- EXECUTE -- + replace_file(&path, b"new content").unwrap(); + + // -- ASSERT -- + assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); + assert!(!dir.join("config.toml.tmp").exists()); + } + + #[cfg(unix)] + #[test] + fn test_replace_file_keeps_the_mode() { + use std::os::unix::fs::PermissionsExt; + + // -- PREPARE -- + let dir = test_dir("replace-mode"); + let path = dir.join("config.toml"); + fs::write(&path, "old content").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap(); + + // -- EXECUTE -- + replace_file(&path, b"new content").unwrap(); + + // -- ASSERT -- + let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o640); + } + + #[cfg(windows)] + fn acl(path: &std::path::Path) -> String { + let output = std::process::Command::new("icacls") + .arg(path) + .output() + .unwrap(); + assert!(output.status.success()); + // The first line starts with the path; the last one is a summary. + let text = String::from_utf8_lossy(&output.stdout).replace(&*path.to_string_lossy(), ""); + text.lines() + .map(str::trim) + .filter(|line| line.contains(':') && !line.contains("Successfully")) + .collect::>() + .join("\n") + } + + #[cfg(windows)] + #[test] + fn test_replace_file_keeps_the_restricted_acl() { + // -- PREPARE -- + let dir = test_dir("replace-acl"); + let path = dir.join("config.toml"); + fs::write(&path, "old content").unwrap(); + // Not inherited from the folder, like the ACL the installers set. + let status = std::process::Command::new("icacls") + .arg(&path) + .args(["/inheritance:r", "/grant:r", "*S-1-5-18:F", "*S-1-5-11:F"]) + .status() + .unwrap(); + assert!(status.success()); + let before = acl(&path); + + // -- EXECUTE -- + replace_file(&path, b"new content").unwrap(); + + // -- ASSERT -- + assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); + assert_eq!(acl(&path), before); + assert!( + !before.contains("(I)"), + "the test ACL should not be inherited" + ); + } + + #[cfg(windows)] + #[test] + fn test_replace_file_keeps_the_old_content_when_the_file_cannot_be_replaced() { + use std::os::windows::fs::OpenOptionsExt; + + // -- PREPARE -- + let dir = test_dir("replace-locked"); + let path = dir.join("config.toml"); + fs::write(&path, "old content").unwrap(); + // Opened without sharing: nothing else can replace or delete it meanwhile. + let lock = fs::OpenOptions::new() + .read(true) + .share_mode(0) + .open(&path) + .unwrap(); + + // -- EXECUTE -- + let result = replace_file(&path, b"new content"); + drop(lock); + + // -- ASSERT -- + assert!(result.is_err()); + assert_eq!(fs::read_to_string(&path).unwrap(), "old content"); + assert!(!dir.join("config.toml.tmp").exists()); + } +} From 122890528aaf8a5188e58f87487c7cbeecd70360 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Wed, 14 Oct 2026 17:55:11 +0200 Subject: [PATCH 02/13] feat: read the token through a secret store --- Cargo.lock | 4 +++ Cargo.toml | 8 ++++- src/api/mod.rs | 27 ++++++++++++---- src/config/settings.rs | 6 +++- src/main.rs | 3 +- src/secrets/config_store.rs | 1 - src/tests/api/client.rs | 51 +++++++++++++++++++++++++++++- src/tests/config/settings_tests.rs | 34 +++++++++++++++++++- 8 files changed, 121 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 569f529f..1a24b01a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1144,6 +1144,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" name = "openaev-agent" version = "3.261118.0" dependencies = [ + "bytes", "config", "filetime", "hostname", @@ -2780,6 +2781,9 @@ name = "zeroize" version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" +dependencies = [ + "serde", +] [[package]] name = "zerotrie" diff --git a/Cargo.toml b/Cargo.toml index 683e837e..565ec27a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ mid = "=3.0.7" tracing-appender = "0.2.3" rolling-file = "0.2.0" webpki-root-certs = "1.0.10" -zeroize = "1.9.0" +bytes = "1.12.1" [dependencies.reqwest] version = "0.13.0" @@ -22,6 +22,12 @@ features = [ ] default-features = false +[dependencies.zeroize] +version = "1.9.0" +features = [ + "serde", +] + [dependencies.toml_edit] version = "0.25.17" features = [ diff --git a/src/api/mod.rs b/src/api/mod.rs index f2f9cfff..1b5db542 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,6 @@ use crate::common::error_model::Error; use crate::secrets::{SecretStore, TOKEN}; +use bytes::Bytes; use reqwest::header::HeaderValue; use std::sync::Arc; use std::time::Duration; @@ -10,6 +11,7 @@ mod register_agent; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const AUTHORIZATION_HEADER: &str = "Authorization"; +const BEARER_PREFIX: &[u8] = b"Bearer "; pub struct Client { http_client: reqwest::blocking::Client, @@ -60,12 +62,7 @@ impl Client { /// between requests. fn authorization(&self) -> Result { let token = self.secrets.get(TOKEN)?; - let bearer = Zeroizing::new(format!("Bearer {}", token.as_str())); - let mut value = - HeaderValue::from_str(&bearer).map_err(|err| Error::Internal(err.to_string()))?; - // Keeps it out of reqwest's debug output. - value.set_sensitive(true); - Ok(value) + bearer_header(token.as_bytes()) } #[cfg(test)] @@ -93,3 +90,21 @@ impl Client { .header(AUTHORIZATION_HEADER, self.authorization()?)) } } + +/// The `Authorization` value for `token`, in a buffer wiped once the request is done with it. +/// +/// The header holds this very buffer, not a copy: `HeaderValue` keeps a `Bytes` as it is, and +/// `Bytes::from_owner` drops (so wipes) the `Zeroizing` buffer when its last reference goes. +/// The HTTP and TLS layers still copy the bytes they send into their own buffers, which are +/// not wiped. +pub(crate) fn bearer_header(token: &[u8]) -> Result { + // Allocated at its final size: growing it would leave an unwiped copy behind. + let mut bearer = Zeroizing::new(Vec::with_capacity(BEARER_PREFIX.len() + token.len())); + bearer.extend_from_slice(BEARER_PREFIX); + bearer.extend_from_slice(token); + let mut value = HeaderValue::from_maybe_shared(Bytes::from_owner(bearer)) + .map_err(|err| Error::Internal(err.to_string()))?; + // Keeps it out of reqwest's debug output. + value.set_sensitive(true); + Ok(value) +} diff --git a/src/config/settings.rs b/src/config/settings.rs index 963c1ded..1e828382 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -3,6 +3,7 @@ use serde::Deserialize; use std::env; use std::fmt; use std::path::PathBuf; +use zeroize::Zeroizing; pub const ENV_PRODUCTION: &str = "production"; const ENV_PRODUCTION_CONFIG_FILE: &str = "openaev-agent-config"; @@ -12,7 +13,10 @@ const ENV_PRODUCTION_CONFIG_FILE: &str = "openaev-agent-config"; pub struct OpenAEV { pub url: String, // Optional: once moved to the OS secret store, the token is no longer in the config. - pub token: Option, + // Zeroizing from deserialization on, so that every path that drops it wipes it. The + // copies the config crate makes while loading the file are not: none are left once + // the token has moved to the OS secret store. + pub token: Option>, pub unsecured_certificate: bool, pub with_proxy: bool, pub installation_mode: String, diff --git a/src/main.rs b/src/main.rs index b2e5b386..df311a53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,6 @@ use crate::process::agent_job; use crate::process::{agent_cleanup, keep_alive}; use crate::windows::service::service_stub; use tracing_subscriber::filter::LevelFilter; -use zeroize::Zeroizing; pub static THREADS_CONTROL: AtomicBool = AtomicBool::new(true); const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -66,7 +65,7 @@ fn agent_start( is_service: bool, ) -> Result>, Error> { let url = settings_data.openaev.url; - let secrets = secrets::open(settings_data.openaev.token.take().map(Zeroizing::new)); + let secrets = secrets::open(settings_data.openaev.token.take()); let unsecured_certificate = settings_data.openaev.unsecured_certificate; let with_proxy = settings_data.openaev.with_proxy; let installation_mode = settings_data.openaev.installation_mode; diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 905b2b72..6a52d07a 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -29,7 +29,6 @@ impl SecretStore for ConfigStore { settings .openaev .token - .map(Zeroizing::new) .ok_or_else(|| Error::Internal("no token in the agent config".to_string())) } diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index 3a1e2813..ad400266 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -2,14 +2,17 @@ const TOKEN: &str = "token"; #[cfg(test)] mod tests { - use crate::api::{Client, AUTHORIZATION_HEADER}; + use crate::api::{bearer_header, Client, AUTHORIZATION_HEADER}; use crate::tests::api::client::TOKEN; use crate::tests::api::tls_server::serve_self_signed_https; use crate::tests::secrets::{FailingStore, MemoryStore}; + use bytes::Bytes; use mockito; + use reqwest::header::HeaderValue; use std::env; use std::error::Error; use std::io; + use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; fn is_invalid_certificate(err: &reqwest::Error) -> bool { @@ -181,4 +184,50 @@ mod tests { "Client should trust a CA present only in the OS trust store" ); } + + #[test] + fn test_bearer_header_holds_the_token_and_is_sensitive() { + // -- EXECUTE -- + let value = bearer_header(TOKEN.as_bytes()).unwrap(); + + // -- ASSERT -- + assert_eq!(value.as_bytes(), format!("Bearer {TOKEN}").as_bytes()); + assert!(value.is_sensitive()); + } + + /// Stands in for the `Zeroizing` buffer: records when it is dropped, so wiped. + struct DropFlag(Vec, Arc); + + impl AsRef<[u8]> for DropFlag { + fn as_ref(&self) -> &[u8] { + &self.0 + } + } + + impl Drop for DropFlag { + fn drop(&mut self) { + self.1.store(true, Ordering::SeqCst); + } + } + + #[test] + fn test_header_value_keeps_the_owned_buffer_until_its_last_copy_is_dropped() { + // -- PREPARE -- + let dropped = Arc::new(AtomicBool::new(false)); + let bytes = Bytes::from_owner(DropFlag(b"Bearer token".to_vec(), dropped.clone())); + let buffer = bytes.as_ptr(); + + // -- EXECUTE -- + let value = HeaderValue::from_maybe_shared(bytes).unwrap(); + let copy = value.clone(); + + // -- ASSERT -- + // A copy of the bytes would never be wiped: the header must point at the buffer. + assert_eq!(value.as_bytes().as_ptr(), buffer); + assert_eq!(copy.as_bytes().as_ptr(), buffer); + drop(value); + assert!(!dropped.load(Ordering::SeqCst), "a copy still uses it"); + drop(copy); + assert!(dropped.load(Ordering::SeqCst), "wiped with its last copy"); + } } diff --git a/src/tests/config/settings_tests.rs b/src/tests/config/settings_tests.rs index 3caedee4..26d33743 100644 --- a/src/tests/config/settings_tests.rs +++ b/src/tests/config/settings_tests.rs @@ -1,13 +1,14 @@ #[cfg(test)] mod tests { use crate::config::settings::OpenAEV; + use zeroize::Zeroizing; #[test] fn test_debug_output_redacts_the_token() { // -- PREPARE -- let settings = OpenAEV { url: "http://localhost:3001".to_string(), - token: Some("secret-token".to_string()), + token: Some(Zeroizing::new("secret-token".to_string())), unsecured_certificate: false, with_proxy: false, installation_mode: "service".to_string(), @@ -22,4 +23,35 @@ mod tests { assert!(!output.contains("secret-token")); assert!(output.contains("")); } + + #[test] + fn test_token_is_deserialized_straight_into_a_zeroizing_buffer() { + // -- PREPARE -- + let json = r#"{"url": "http://localhost:3001", "token": "secret-token", + "unsecured_certificate": false, "with_proxy": false, + "installation_mode": "service", "service_name": "OAEVAgentService", + "tenant_id": "tenant"}"#; + + // -- EXECUTE -- + let settings: OpenAEV = serde_json::from_str(json).unwrap(); + + // -- ASSERT -- + let token: Zeroizing = settings.token.unwrap(); + assert_eq!(token.as_str(), "secret-token"); + } + + #[test] + fn test_token_is_optional() { + // -- PREPARE -- + let json = r#"{"url": "http://localhost:3001", + "unsecured_certificate": false, "with_proxy": false, + "installation_mode": "service", "service_name": "OAEVAgentService", + "tenant_id": "tenant"}"#; + + // -- EXECUTE -- + let settings: OpenAEV = serde_json::from_str(json).unwrap(); + + // -- ASSERT -- + assert!(settings.token.is_none()); + } } From 7ab9da28a59db6e5d60d9c9946610dc370b20978 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Thu, 15 Oct 2026 11:54:37 +0200 Subject: [PATCH 03/13] feat(secret-store)): feedback review --- src/secrets/import.rs | 29 ++++++++++--- src/tests/secrets/import_tests.rs | 71 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/src/secrets/import.rs b/src/secrets/import.rs index 9f49dbce..d0facd21 100644 --- a/src/secrets/import.rs +++ b/src/secrets/import.rs @@ -42,15 +42,32 @@ pub fn import( } } - if bootstrap_token.is_some() { - fs::remove_file(bootstrap)?; - } - if config_token.is_some() { + // The config copy goes first. Were the bootstrap file deleted first, a crash or a config + // that cannot be rewritten would leave the config token alone at the next start, and it + // would overwrite the newer bootstrap token in the store. + let config_cleaned = match config_token { + None => true, // The token is safe in the store by now: a config left as it was is worth a // warning, not an agent that refuses to start. - if let Err(err) = remove_token_from_config(config) { + Some(_) => match remove_token_from_config(config) { + Ok(()) => true, + Err(err) => { + warn!( + "The token is still in clear text in {}: {err}", + config.display() + ); + false + } + }, + }; + if let Some(bootstrap_token) = &bootstrap_token { + // Kept while the config holds another token, so that it still wins at the next start. + if config_cleaned || config_token == Some(bootstrap_token.as_str()) { + fs::remove_file(bootstrap)?; + } else { warn!( - "The token is still in clear text in {}: {err}", + "Keeping {} until the older token is removed from {}", + bootstrap.display(), config.display() ); } diff --git a/src/tests/secrets/import_tests.rs b/src/tests/secrets/import_tests.rs index e48e738f..d41298af 100644 --- a/src/tests/secrets/import_tests.rs +++ b/src/tests/secrets/import_tests.rs @@ -199,4 +199,75 @@ mod tests { "a config that cannot be parsed is not touched" ); } + + /// A config that still holds `token` but cannot be rewritten (it does not parse). + fn stuck_config(token: &str) -> String { + format!("[openaev]\ntoken = \"{token}\"\nbroken =\n") + } + + #[test] + fn test_import_keeps_the_bootstrap_file_while_the_config_holds_an_older_token() { + // -- PREPARE -- + let dir = test_dir("import-stuck-config"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "new-token").unwrap(); + fs::write(&config, stuck_config("old-token")).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + // Two starts: the config token cannot be removed either time. + import(&store, &bootstrap, Some("old-token"), &config).unwrap(); + import(&store, &bootstrap, Some("old-token"), &config).unwrap(); + + // -- ASSERT -- + assert_eq!( + store.get(TOKEN).unwrap().as_str(), + "new-token", + "the older config token must never replace the bootstrap one" + ); + assert!(bootstrap.exists(), "the bootstrap file still has to win"); + assert_eq!(store.writes.load(Ordering::SeqCst), 1); + } + + #[test] + fn test_import_deletes_the_bootstrap_file_when_the_config_holds_the_same_token() { + // -- PREPARE -- + let dir = test_dir("import-stuck-same"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "same-token").unwrap(); + fs::write(&config, stuck_config("same-token")).unwrap(); + let store = MemoryStore::default(); + + // -- EXECUTE -- + import(&store, &bootstrap, Some("same-token"), &config).unwrap(); + + // -- ASSERT -- + assert_eq!(store.get(TOKEN).unwrap().as_str(), "same-token"); + assert!( + !bootstrap.exists(), + "the config token cannot overwrite anything" + ); + } + + #[test] + fn test_import_finishes_a_clean_up_interrupted_after_the_config() { + // -- PREPARE -- + // Stopped after the config was cleaned, before the bootstrap file was deleted. + let dir = test_dir("import-interrupted"); + let bootstrap = dir.join(BOOTSTRAP_FILE); + let config = dir.join("config.toml"); + fs::write(&bootstrap, "new-token").unwrap(); + fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); + let store = MemoryStore::with_token("new-token"); + + // -- EXECUTE -- + import(store.as_ref(), &bootstrap, None, &config).unwrap(); + + // -- ASSERT -- + assert_eq!(store.writes.load(Ordering::SeqCst), 0); + assert!(!bootstrap.exists()); + assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); + } } From acfc65d1e95b23b4db4d24d64668fd12e74469ed Mon Sep 17 00:00:00 2001 From: savacano28 Date: Thu, 15 Oct 2026 12:17:37 +0200 Subject: [PATCH 04/13] feat(secret-store): feedback review --- Cargo.lock | 21 +- Cargo.toml | 14 -- src/config/settings.rs | 29 +-- src/main.rs | 7 +- src/secrets/config_store.rs | 32 +-- src/secrets/import.rs | 100 --------- src/secrets/mod.rs | 61 +----- src/secrets/replace.rs | 105 --------- src/tests/config/settings_tests.rs | 10 +- src/tests/secrets/config_store_tests.rs | 35 +-- src/tests/secrets/import_tests.rs | 273 ------------------------ src/tests/secrets/mod.rs | 37 +--- src/tests/secrets/replace_tests.rs | 126 ----------- 13 files changed, 33 insertions(+), 817 deletions(-) delete mode 100644 src/secrets/import.rs delete mode 100644 src/secrets/replace.rs delete mode 100644 src/tests/secrets/import_tests.rs delete mode 100644 src/tests/secrets/replace_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 1a24b01a..3ba07ded 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1145,7 +1145,7 @@ name = "openaev-agent" version = "3.261118.0" dependencies = [ "bytes", - "config", + "config", "filetime", "hostname", "log", @@ -1158,14 +1158,12 @@ dependencies = [ "rustls", "serde", "serde_json", - "toml_edit", "tracing-appender", "tracing-subscriber", "webpki-root-certs", "windows-service", "windows-service-detector", - "windows-sys 0.61.2", - "zeroize", + "zeroize", ] [[package]] @@ -2051,19 +2049,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "toml_edit" -version = "0.25.17+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3641d5bbb5349a79e1020a242d251efbc546ad8048d133958323ce9c40a9c9c" -dependencies = [ - "indexmap", - "toml_datetime", - "toml_parser", - "toml_writer", - "winnow", -] - [[package]] name = "toml_parser" version = "1.1.5+spec-1.1.0" @@ -2782,7 +2767,7 @@ version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" dependencies = [ - "serde", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 565ec27a..3358ac73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,14 +28,6 @@ features = [ "serde", ] -[dependencies.toml_edit] -version = "0.25.17" -features = [ - "parse", - "display", -] -default-features = false - [dependencies.serde] version = "1.0.203" features = [ @@ -61,12 +53,6 @@ features = [ windows-service = "0.8.0" windows-service-detector = "0.1.0" -[target."cfg(windows)".dependencies.windows-sys] -version = "0.61.2" -features = [ - "Win32_Storage_FileSystem", -] - [dev-dependencies] filetime = "0.2.25" mockito = "1.7.0" diff --git a/src/config/settings.rs b/src/config/settings.rs index 1e828382..e7fd907e 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -2,21 +2,18 @@ use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::env; use std::fmt; -use std::path::PathBuf; use zeroize::Zeroizing; -pub const ENV_PRODUCTION: &str = "production"; +const ENV_PRODUCTION: &str = "production"; const ENV_PRODUCTION_CONFIG_FILE: &str = "openaev-agent-config"; #[derive(Deserialize, Clone)] #[allow(unused)] pub struct OpenAEV { pub url: String, - // Optional: once moved to the OS secret store, the token is no longer in the config. // Zeroizing from deserialization on, so that every path that drops it wipes it. The - // copies the config crate makes while loading the file are not: none are left once - // the token has moved to the OS secret store. - pub token: Option>, + // copies the config crate makes while loading the file are not. + pub token: Zeroizing, pub unsecured_certificate: bool, pub with_proxy: bool, pub installation_mode: String, @@ -29,7 +26,7 @@ impl fmt::Debug for OpenAEV { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("OpenAEV") .field("url", &self.url) - .field("token", &self.token.as_ref().map(|_| "")) + .field("token", &"") .field("unsecured_certificate", &self.unsecured_certificate) .field("with_proxy", &self.with_proxy) .field("installation_mode", &self.installation_mode) @@ -82,23 +79,15 @@ impl Settings { env::var("env").unwrap_or_else(|_| ENV_PRODUCTION.into()) } - /// The directory of the executable, where the installers put the config and the token. - pub fn install_dir() -> PathBuf { - let current_exe_patch = env::current_exe().unwrap(); - current_exe_patch.parent().unwrap().to_path_buf() - } - - /// The config file the installers write in production. - pub fn production_config_path() -> PathBuf { - Self::install_dir().join(format!("{ENV_PRODUCTION_CONFIG_FILE}.toml")) - } - pub fn new() -> Result { let run_mode = Self::mode(); let config = Config::builder().add_source(Environment::with_prefix("openaev")); if run_mode == ENV_PRODUCTION { - // Join the expected config file with the executable directory - let config_file = Self::install_dir().join(ENV_PRODUCTION_CONFIG_FILE); + // Get the current executable path + let current_exe_patch = env::current_exe().unwrap(); + let parent_path = current_exe_patch.parent().unwrap(); + // Join the expected config file with the parent + let config_file = parent_path.join(ENV_PRODUCTION_CONFIG_FILE); let config_path = config_file.display(); config .add_source(File::with_name(&config_path.to_string()).required(true)) diff --git a/src/main.rs b/src/main.rs index df311a53..5ea075d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,12 +60,9 @@ fn compute_working_dir() -> PathBuf { current_exe_path.parent().unwrap().to_path_buf() } -fn agent_start( - mut settings_data: Settings, - is_service: bool, -) -> Result>, Error> { +fn agent_start(settings_data: Settings, is_service: bool) -> Result>, Error> { let url = settings_data.openaev.url; - let secrets = secrets::open(settings_data.openaev.token.take()); + let secrets = secrets::open(); let unsecured_certificate = settings_data.openaev.unsecured_certificate; let with_proxy = settings_data.openaev.with_proxy; let installation_mode = settings_data.openaev.installation_mode; diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 6a52d07a..103acb16 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -1,40 +1,18 @@ -use super::{read_bootstrap, SecretStore, TOKEN}; +use super::{SecretStore, TOKEN}; use crate::common::error_model::Error; use crate::config::settings::Settings; -use std::path::PathBuf; use zeroize::Zeroizing; -/// The token where the installers leave it in clear text: the bootstrap file when one is -/// waiting, otherwise the config. Used where no OS secret store is supported yet, or when -/// moving the token there failed. Read again on each use, never kept in memory. -pub struct ConfigStore { - bootstrap: PathBuf, -} - -impl ConfigStore { - pub fn new(bootstrap: PathBuf) -> Self { - ConfigStore { bootstrap } - } -} +/// The token in the config, where the installers write it. Read again on each use, never +/// kept in memory. +pub struct ConfigStore; impl SecretStore for ConfigStore { fn get(&self, name: &str) -> Result, Error> { if name != TOKEN { return Err(Error::Internal(format!("no secret named {name}"))); } - if let Some(token) = read_bootstrap(&self.bootstrap)? { - return Ok(token); - } let settings = Settings::new().map_err(|err| Error::Internal(err.to_string()))?; - settings - .openaev - .token - .ok_or_else(|| Error::Internal("no token in the agent config".to_string())) - } - - fn set(&self, _name: &str, _value: &str) -> Result<(), Error> { - Err(Error::Internal( - "the agent does not write secrets to its config".to_string(), - )) + Ok(settings.openaev.token) } } diff --git a/src/secrets/import.rs b/src/secrets/import.rs deleted file mode 100644 index d0facd21..00000000 --- a/src/secrets/import.rs +++ /dev/null @@ -1,100 +0,0 @@ -use super::{read_bootstrap, replace_file, SecretStore, TOKEN}; -use crate::common::error_model::Error; -use log::{info, warn}; -use std::fs; -use std::path::Path; -use toml_edit::{DocumentMut, Item}; -use zeroize::Zeroizing; - -/// The table of the config that holds the token. -const CONFIG_TABLE: &str = "openaev"; - -/// Moves the clear-text token into `store`, then removes the clear-text copies. -/// -/// The token comes from the bootstrap file when there is one, otherwise from the config -/// (`config_token`). Safe to run on every start: a failure leaves the clear-text copies in -/// place for the next attempt. -pub fn import( - store: &dyn SecretStore, - bootstrap: &Path, - config_token: Option<&str>, - config: &Path, -) -> Result<(), Error> { - let bootstrap_token = read_bootstrap(bootstrap)?; - let token = bootstrap_token - .as_ref() - .map(|token| token.as_str()) - .or(config_token) - .filter(|token| !token.is_empty()); - - if let Some(token) = token { - let already_stored = store - .get(TOKEN) - .is_ok_and(|stored| stored.as_str() == token); - if !already_stored { - store.set(TOKEN, token)?; - if store.get(TOKEN)?.as_str() != token { - return Err(Error::Internal( - "the token read back from the store differs from the one written".to_string(), - )); - } - info!("Token moved to the OS secret store"); - } - } - - // The config copy goes first. Were the bootstrap file deleted first, a crash or a config - // that cannot be rewritten would leave the config token alone at the next start, and it - // would overwrite the newer bootstrap token in the store. - let config_cleaned = match config_token { - None => true, - // The token is safe in the store by now: a config left as it was is worth a - // warning, not an agent that refuses to start. - Some(_) => match remove_token_from_config(config) { - Ok(()) => true, - Err(err) => { - warn!( - "The token is still in clear text in {}: {err}", - config.display() - ); - false - } - }, - }; - if let Some(bootstrap_token) = &bootstrap_token { - // Kept while the config holds another token, so that it still wins at the next start. - if config_cleaned || config_token == Some(bootstrap_token.as_str()) { - fs::remove_file(bootstrap)?; - } else { - warn!( - "Keeping {} until the older token is removed from {}", - bootstrap.display(), - config.display() - ); - } - } - Ok(()) -} - -/// Removes the token from the `[openaev]` table of the config, keeping everything else (other -/// tables, comments, formatting) as it is. Does nothing when the table has no token, for -/// example when it came from an environment variable. -fn remove_token_from_config(config: &Path) -> Result<(), Error> { - let content = Zeroizing::new(fs::read_to_string(config)?); - // The parse error is not shown: it quotes the line it failed on, which can be the token. - let mut document: DocumentMut = content - .parse() - .map_err(|_| Error::Internal("the config is not valid TOML".to_string()))?; - let removed = document - .get_mut(CONFIG_TABLE) - .and_then(Item::as_table_like_mut) - .and_then(|table| table.remove(TOKEN)); - if removed.is_some() { - let mut kept = Zeroizing::new(document.to_string()); - // toml_edit writes LF line endings; keep CRLF when the installer wrote it. - if content.contains("\r\n") { - kept = Zeroizing::new(kept.replace("\r\n", "\n").replace('\n', "\r\n")); - } - replace_file(config, kept.as_bytes())?; - } - Ok(()) -} diff --git a/src/secrets/mod.rs b/src/secrets/mod.rs index d9f50057..ab43e182 100644 --- a/src/secrets/mod.rs +++ b/src/secrets/mod.rs @@ -1,77 +1,26 @@ //! Where the agent keeps the secrets it authenticates with. //! -//! The installers hand the token over in clear text, in a bootstrap file next to the -//! executable (older installers wrote it in the config). In production the agent moves it -//! into the OS secret store when the platform has one, and reads it back from there on -//! each use rather than keeping it in memory. +//! The agent reads its token through a [`SecretStore`] on each use rather than keeping it in +//! memory. The only store today is the config the installers write; OS secret stores plug in +//! behind the same trait. mod config_store; -mod import; -mod replace; pub use config_store::ConfigStore; -pub use import::import; -pub(crate) use replace::replace_file; use crate::common::error_model::Error; -use crate::config::settings::{Settings, ENV_PRODUCTION}; -use log::error; -use std::fs; -use std::io::ErrorKind; -use std::path::Path; use std::sync::Arc; use zeroize::Zeroizing; /// Name of the secret the agent authenticates to the platform with. pub const TOKEN: &str = "token"; -/// Clear-text token written by the installers, removed once moved to the store. -pub const BOOTSTRAP_FILE: &str = "openaev-agent-token.bootstrap"; pub trait SecretStore: Send + Sync { /// Reads the secret. The returned value is wiped from memory when dropped. fn get(&self, name: &str) -> Result, Error>; - /// Replaces the secret; the next `get` returns the new value. - fn set(&self, name: &str, value: &str) -> Result<(), Error>; } /// Opens the store the agent reads its token from. -/// -/// `config_token` is the token found in the config, if an older installer left it there. -pub fn open(config_token: Option>) -> Arc { - let install_dir = Settings::install_dir(); - let bootstrap = install_dir.join(BOOTSTRAP_FILE); - if Settings::mode() == ENV_PRODUCTION { - if let Some(store) = platform_store(&install_dir) { - let config_token = config_token.as_ref().map(|token| token.as_str()); - match import( - store.as_ref(), - &bootstrap, - config_token, - &Settings::production_config_path(), - ) { - Ok(()) => return store, - // Staying online on the clear-text copy beats losing the agent. - Err(err) => error!("Could not move the token to the OS secret store: {err}"), - } - } - } - Arc::new(ConfigStore::new(bootstrap)) -} - -/// The OS secret store of this platform, if the agent supports one there. -fn platform_store(_install_dir: &Path) -> Option> { - None -} - -/// Reads the token the installer left in the bootstrap file, if there is one. -pub(crate) fn read_bootstrap(path: &Path) -> Result>, Error> { - match fs::read_to_string(path) { - Ok(content) => { - let content = Zeroizing::new(content); - let token = content.trim(); - Ok((!token.is_empty()).then(|| Zeroizing::new(token.to_string()))) - } - Err(err) if err.kind() == ErrorKind::NotFound => Ok(None), - Err(err) => Err(Error::Io(err)), - } +pub fn open() -> Arc { + Arc::new(ConfigStore) } diff --git a/src/secrets/replace.rs b/src/secrets/replace.rs deleted file mode 100644 index 920b7fa5..00000000 --- a/src/secrets/replace.rs +++ /dev/null @@ -1,105 +0,0 @@ -use std::fs; -use std::fs::{File, OpenOptions}; -use std::io; -use std::io::{ErrorKind, Write}; -use std::path::{Path, PathBuf}; - -/// Replaces the content of `path` with `content`, never leaving it half-written. -/// -/// The content is written and synced to a temporary file next to `path`, which then takes -/// its place in one step: if anything fails (full disk, missing rights), `path` keeps its -/// previous content. The new file keeps the permissions of the one it replaces (owner and -/// mode on Unix, ACL on Windows), which the installers restricted. -pub(crate) fn replace_file(path: &Path, content: &[u8]) -> io::Result<()> { - let temp = temp_path(path); - // Left over by an interrupted run: never reused, it could hold anything. - remove_if_exists(&temp)?; - let result = write_temp(path, &temp, content).and_then(|()| swap(path, &temp)); - if result.is_err() { - let _ = fs::remove_file(&temp); - } - result -} - -fn temp_path(path: &Path) -> PathBuf { - let mut name = path.file_name().unwrap_or_default().to_os_string(); - name.push(".tmp"); - path.with_file_name(name) -} - -fn remove_if_exists(path: &Path) -> io::Result<()> { - match fs::remove_file(path) { - Err(err) if err.kind() != ErrorKind::NotFound => Err(err), - _ => Ok(()), - } -} - -fn write_temp(path: &Path, temp: &Path, content: &[u8]) -> io::Result<()> { - let mut file = create_temp(path, temp)?; - file.write_all(content)?; - file.sync_all() -} - -#[cfg(unix)] -fn create_temp(path: &Path, temp: &Path) -> io::Result { - use std::os::unix::fs::{fchown, MetadataExt, OpenOptionsExt, PermissionsExt}; - - let metadata = fs::metadata(path)?; - // Readable by its creator only, then given the owner and mode of the file it replaces, - // all before anything is written to it. - let file = OpenOptions::new() - .write(true) - .create_new(true) - .mode(0o600) - .open(temp)?; - fchown(&file, Some(metadata.uid()), Some(metadata.gid()))?; - file.set_permissions(fs::Permissions::from_mode(metadata.mode() & 0o777))?; - Ok(file) -} - -#[cfg(unix)] -fn swap(path: &Path, temp: &Path) -> io::Result<()> { - fs::rename(temp, path)?; - // The rename is only durable once the directory holding it is synced. - let dir = path - .parent() - .filter(|dir| !dir.as_os_str().is_empty()) - .unwrap_or(Path::new(".")); - File::open(dir)?.sync_all() -} - -#[cfg(windows)] -fn create_temp(_path: &Path, temp: &Path) -> io::Result { - // Gets the folder's ACL until ReplaceFileW gives it the one of the file it replaces. - // It never holds a secret: callers write content they removed the secrets from. - OpenOptions::new().write(true).create_new(true).open(temp) -} - -#[cfg(windows)] -fn swap(path: &Path, temp: &Path) -> io::Result<()> { - use std::os::windows::ffi::OsStrExt; - use std::ptr; - use windows_sys::Win32::Storage::FileSystem::ReplaceFileW; - - let wide = |path: &Path| -> Vec { path.as_os_str().encode_wide().chain([0]).collect() }; - let (replaced, replacement) = (wide(path), wide(temp)); - // ReplaceFileW gives the new file the ACL and attributes of the one it replaces, and - // fails rather than dropping them. Without a backup file, a failure leaves `path` as it - // was. - // SAFETY: both paths are NUL-terminated and alive for the whole call; the backup file - // and the reserved parameters may be null. - let ok = unsafe { - ReplaceFileW( - replaced.as_ptr(), - replacement.as_ptr(), - ptr::null(), - 0, - ptr::null(), - ptr::null(), - ) - }; - if ok == 0 { - return Err(io::Error::last_os_error()); - } - Ok(()) -} diff --git a/src/tests/config/settings_tests.rs b/src/tests/config/settings_tests.rs index 26d33743..d0c4b2aa 100644 --- a/src/tests/config/settings_tests.rs +++ b/src/tests/config/settings_tests.rs @@ -8,7 +8,7 @@ mod tests { // -- PREPARE -- let settings = OpenAEV { url: "http://localhost:3001".to_string(), - token: Some(Zeroizing::new("secret-token".to_string())), + token: Zeroizing::new("secret-token".to_string()), unsecured_certificate: false, with_proxy: false, installation_mode: "service".to_string(), @@ -36,12 +36,12 @@ mod tests { let settings: OpenAEV = serde_json::from_str(json).unwrap(); // -- ASSERT -- - let token: Zeroizing = settings.token.unwrap(); + let token: Zeroizing = settings.token; assert_eq!(token.as_str(), "secret-token"); } #[test] - fn test_token_is_optional() { + fn test_token_is_required() { // -- PREPARE -- let json = r#"{"url": "http://localhost:3001", "unsecured_certificate": false, "with_proxy": false, @@ -49,9 +49,9 @@ mod tests { "tenant_id": "tenant"}"#; // -- EXECUTE -- - let settings: OpenAEV = serde_json::from_str(json).unwrap(); + let result = serde_json::from_str::(json); // -- ASSERT -- - assert!(settings.token.is_none()); + assert!(result.is_err()); } } diff --git a/src/tests/secrets/config_store_tests.rs b/src/tests/secrets/config_store_tests.rs index fd4bad0a..b43f0fba 100644 --- a/src/tests/secrets/config_store_tests.rs +++ b/src/tests/secrets/config_store_tests.rs @@ -1,41 +1,10 @@ #[cfg(test)] mod tests { - use crate::secrets::{ConfigStore, SecretStore, BOOTSTRAP_FILE, TOKEN}; - use crate::tests::secrets::test_dir; - use std::fs; - - #[test] - fn test_config_store_reads_the_bootstrap_file_on_each_use() { - // -- PREPARE -- - let dir = test_dir("config-store-bootstrap"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - fs::write(&bootstrap, "first-token\n").unwrap(); - let store = ConfigStore::new(bootstrap.clone()); - - // -- EXECUTE & ASSERT -- - assert_eq!(store.get(TOKEN).unwrap().as_str(), "first-token"); - fs::write(&bootstrap, "second-token").unwrap(); - assert_eq!(store.get(TOKEN).unwrap().as_str(), "second-token"); - } - - #[test] - fn test_config_store_refuses_writes() { - // -- PREPARE -- - let store = ConfigStore::new(test_dir("config-store-write").join(BOOTSTRAP_FILE)); - - // -- EXECUTE & ASSERT -- - assert!(store.set(TOKEN, "token").is_err()); - } + use crate::secrets::{ConfigStore, SecretStore}; #[test] fn test_config_store_only_holds_the_token() { - // -- PREPARE -- - let dir = test_dir("config-store-name"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - fs::write(&bootstrap, "token").unwrap(); - let store = ConfigStore::new(bootstrap); - // -- EXECUTE & ASSERT -- - assert!(store.get("refresh_token").is_err()); + assert!(ConfigStore.get("refresh_token").is_err()); } } diff --git a/src/tests/secrets/import_tests.rs b/src/tests/secrets/import_tests.rs deleted file mode 100644 index d41298af..00000000 --- a/src/tests/secrets/import_tests.rs +++ /dev/null @@ -1,273 +0,0 @@ -#[cfg(test)] -mod tests { - use crate::secrets::{import, SecretStore, BOOTSTRAP_FILE, TOKEN}; - use crate::tests::secrets::{test_dir, FailingStore, MemoryStore}; - use std::fs; - use std::sync::atomic::Ordering; - - const CONFIG: &str = "debug=false\r\n\r\n[openaev]\r\nurl = \"http://localhost:3001\"\r\ntoken = \"legacy-token\"\r\ntenant_id = \"tenant\"\r\n"; - const CONFIG_WITHOUT_TOKEN: &str = - "debug=false\r\n\r\n[openaev]\r\nurl = \"http://localhost:3001\"\r\ntenant_id = \"tenant\"\r\n"; - - #[test] - fn test_import_moves_the_bootstrap_token_and_deletes_the_file() { - // -- PREPARE -- - let dir = test_dir("import-bootstrap"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "bootstrap-token\r\n").unwrap(); - fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - import(&store, &bootstrap, None, &config).unwrap(); - - // -- ASSERT -- - assert_eq!(store.get(TOKEN).unwrap().as_str(), "bootstrap-token"); - assert!( - !bootstrap.exists(), - "the clear-text bootstrap file should be deleted" - ); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } - - #[test] - fn test_import_moves_the_config_token_and_removes_only_its_line() { - // -- PREPARE -- - let dir = test_dir("import-config"); - let config = dir.join("config.toml"); - fs::write(&config, CONFIG).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - import( - &store, - &dir.join(BOOTSTRAP_FILE), - Some("legacy-token"), - &config, - ) - .unwrap(); - - // -- ASSERT -- - assert_eq!(store.get(TOKEN).unwrap().as_str(), "legacy-token"); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } - - #[test] - fn test_import_prefers_the_bootstrap_token_and_cleans_both_copies() { - // -- PREPARE -- - let dir = test_dir("import-both"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "new-token").unwrap(); - fs::write(&config, CONFIG).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - import(&store, &bootstrap, Some("legacy-token"), &config).unwrap(); - - // -- ASSERT -- - assert_eq!(store.get(TOKEN).unwrap().as_str(), "new-token"); - assert!(!bootstrap.exists()); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } - - #[test] - fn test_import_keeps_the_clear_text_copies_when_the_store_fails() { - // -- PREPARE -- - let dir = test_dir("import-failure"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "bootstrap-token").unwrap(); - fs::write(&config, CONFIG).unwrap(); - - // -- EXECUTE -- - let result = import(&FailingStore, &bootstrap, Some("legacy-token"), &config); - - // -- ASSERT -- - assert!(result.is_err()); - assert!(bootstrap.exists(), "the token must not be lost"); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG); - } - - #[test] - fn test_import_does_not_rewrite_a_token_already_stored() { - // -- PREPARE -- - let dir = test_dir("import-idempotent"); - let config = dir.join("config.toml"); - fs::write(&config, CONFIG).unwrap(); - let store = MemoryStore::with_token("legacy-token"); - - // -- EXECUTE -- - import( - store.as_ref(), - &dir.join(BOOTSTRAP_FILE), - Some("legacy-token"), - &config, - ) - .unwrap(); - - // -- ASSERT -- - assert_eq!(store.writes.load(Ordering::SeqCst), 0); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } - - #[test] - fn test_import_without_any_token_changes_nothing() { - // -- PREPARE -- - let dir = test_dir("import-nothing"); - let config = dir.join("config.toml"); - fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - import(&store, &dir.join(BOOTSTRAP_FILE), None, &config).unwrap(); - - // -- ASSERT -- - assert_eq!(store.writes.load(Ordering::SeqCst), 0); - assert!(store.get(TOKEN).is_err()); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } - - /// Imports `legacy-token` from a config holding `content`, and returns the config after. - fn import_config(name: &str, content: &str) -> String { - let dir = test_dir(name); - let config = dir.join("config.toml"); - fs::write(&config, content).unwrap(); - let store = MemoryStore::default(); - import( - &store, - &dir.join(BOOTSTRAP_FILE), - Some("legacy-token"), - &config, - ) - .unwrap(); - assert_eq!(store.get(TOKEN).unwrap().as_str(), "legacy-token"); - fs::read_to_string(&config).unwrap() - } - - #[test] - fn test_import_removes_only_the_token_of_the_openaev_table() { - // -- PREPARE -- - let content = "token = \"root\"\n\n[openaev]\ntoken_file = \"x\"\ntokens = 2\n token = \"legacy-token\" # from the installer\n\n[other]\ntoken = \"kept\"\n"; - - // -- EXECUTE -- - let after = import_config("import-scoped", content); - - // -- ASSERT -- - assert_eq!( - after, - "token = \"root\"\n\n[openaev]\ntoken_file = \"x\"\ntokens = 2\n\n[other]\ntoken = \"kept\"\n" - ); - } - - #[test] - fn test_import_removes_a_quoted_token_key() { - // -- PREPARE -- - let content = "[openaev]\nurl = \"http://localhost:3001\"\n\"token\" = \"legacy-token\"\n"; - - // -- EXECUTE -- - let after = import_config("import-quoted", content); - - // -- ASSERT -- - assert_eq!(after, "[openaev]\nurl = \"http://localhost:3001\"\n"); - } - - #[test] - fn test_import_removes_a_dotted_token_key() { - // -- PREPARE -- - let content = "openaev.url = \"http://localhost:3001\"\nopenaev.token = \"legacy-token\"\n"; - - // -- EXECUTE -- - let after = import_config("import-dotted", content); - - // -- ASSERT -- - assert_eq!(after, "openaev.url = \"http://localhost:3001\"\n"); - } - - #[test] - fn test_import_leaves_a_config_it_cannot_parse_as_it_was() { - // -- PREPARE -- - let content = "[openaev]\ntoken = \"legacy-token\"\nbroken =\n"; - - // -- EXECUTE -- - let after = import_config("import-invalid", content); - - // -- ASSERT -- - assert_eq!( - after, content, - "a config that cannot be parsed is not touched" - ); - } - - /// A config that still holds `token` but cannot be rewritten (it does not parse). - fn stuck_config(token: &str) -> String { - format!("[openaev]\ntoken = \"{token}\"\nbroken =\n") - } - - #[test] - fn test_import_keeps_the_bootstrap_file_while_the_config_holds_an_older_token() { - // -- PREPARE -- - let dir = test_dir("import-stuck-config"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "new-token").unwrap(); - fs::write(&config, stuck_config("old-token")).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - // Two starts: the config token cannot be removed either time. - import(&store, &bootstrap, Some("old-token"), &config).unwrap(); - import(&store, &bootstrap, Some("old-token"), &config).unwrap(); - - // -- ASSERT -- - assert_eq!( - store.get(TOKEN).unwrap().as_str(), - "new-token", - "the older config token must never replace the bootstrap one" - ); - assert!(bootstrap.exists(), "the bootstrap file still has to win"); - assert_eq!(store.writes.load(Ordering::SeqCst), 1); - } - - #[test] - fn test_import_deletes_the_bootstrap_file_when_the_config_holds_the_same_token() { - // -- PREPARE -- - let dir = test_dir("import-stuck-same"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "same-token").unwrap(); - fs::write(&config, stuck_config("same-token")).unwrap(); - let store = MemoryStore::default(); - - // -- EXECUTE -- - import(&store, &bootstrap, Some("same-token"), &config).unwrap(); - - // -- ASSERT -- - assert_eq!(store.get(TOKEN).unwrap().as_str(), "same-token"); - assert!( - !bootstrap.exists(), - "the config token cannot overwrite anything" - ); - } - - #[test] - fn test_import_finishes_a_clean_up_interrupted_after_the_config() { - // -- PREPARE -- - // Stopped after the config was cleaned, before the bootstrap file was deleted. - let dir = test_dir("import-interrupted"); - let bootstrap = dir.join(BOOTSTRAP_FILE); - let config = dir.join("config.toml"); - fs::write(&bootstrap, "new-token").unwrap(); - fs::write(&config, CONFIG_WITHOUT_TOKEN).unwrap(); - let store = MemoryStore::with_token("new-token"); - - // -- EXECUTE -- - import(store.as_ref(), &bootstrap, None, &config).unwrap(); - - // -- ASSERT -- - assert_eq!(store.writes.load(Ordering::SeqCst), 0); - assert!(!bootstrap.exists()); - assert_eq!(fs::read_to_string(&config).unwrap(), CONFIG_WITHOUT_TOKEN); - } -} diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index 8bdf1069..14e7a5c0 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -1,22 +1,15 @@ mod config_store_tests; -mod import_tests; -mod replace_tests; use crate::common::error_model::Error; use crate::secrets::SecretStore; use std::collections::HashMap; -use std::env; -use std::fs; -use std::path::PathBuf; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use zeroize::Zeroizing; -/// A secret store kept in memory, standing in for the OS one. +/// A secret store kept in memory, standing in for a real one. #[derive(Default)] pub struct MemoryStore { secrets: Mutex>, - pub writes: AtomicUsize, } impl MemoryStore { @@ -40,39 +33,13 @@ impl SecretStore for MemoryStore { .map(|value| Zeroizing::new(value.clone())) .ok_or_else(|| Error::Internal(format!("no secret named {name}"))) } - - fn set(&self, name: &str, value: &str) -> Result<(), Error> { - self.writes.fetch_add(1, Ordering::SeqCst); - self.secrets - .lock() - .unwrap() - .insert(name.to_string(), value.to_string()); - Ok(()) - } } -/// A secret store that cannot be written, like an OS store that is unavailable. +/// A secret store that cannot be read. pub struct FailingStore; impl SecretStore for FailingStore { fn get(&self, name: &str) -> Result, Error> { Err(Error::Internal(format!("no secret named {name}"))) } - - fn set(&self, _name: &str, _value: &str) -> Result<(), Error> { - Err(Error::Internal("store unavailable".to_string())) - } -} - -/// A fresh, empty directory for one test. -pub fn test_dir(name: &str) -> PathBuf { - static COUNTER: AtomicUsize = AtomicUsize::new(0); - let dir = env::temp_dir().join(format!( - "openaev-agent-{name}-{}-{}", - std::process::id(), - COUNTER.fetch_add(1, Ordering::SeqCst) - )); - let _ = fs::remove_dir_all(&dir); - fs::create_dir_all(&dir).unwrap(); - dir } diff --git a/src/tests/secrets/replace_tests.rs b/src/tests/secrets/replace_tests.rs deleted file mode 100644 index f37279e0..00000000 --- a/src/tests/secrets/replace_tests.rs +++ /dev/null @@ -1,126 +0,0 @@ -#[cfg(test)] -mod tests { - use crate::secrets::replace_file; - use crate::tests::secrets::test_dir; - use std::fs; - - #[test] - fn test_replace_file_writes_the_content_and_leaves_no_temporary_file() { - // -- PREPARE -- - let dir = test_dir("replace-content"); - let path = dir.join("config.toml"); - fs::write(&path, "old content").unwrap(); - - // -- EXECUTE -- - replace_file(&path, b"new content").unwrap(); - - // -- ASSERT -- - assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); - assert_eq!(fs::read_dir(&dir).unwrap().count(), 1); - } - - #[test] - fn test_replace_file_ignores_a_temporary_file_left_by_an_interrupted_run() { - // -- PREPARE -- - let dir = test_dir("replace-leftover"); - let path = dir.join("config.toml"); - fs::write(&path, "old content").unwrap(); - fs::write(dir.join("config.toml.tmp"), "leftover").unwrap(); - - // -- EXECUTE -- - replace_file(&path, b"new content").unwrap(); - - // -- ASSERT -- - assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); - assert!(!dir.join("config.toml.tmp").exists()); - } - - #[cfg(unix)] - #[test] - fn test_replace_file_keeps_the_mode() { - use std::os::unix::fs::PermissionsExt; - - // -- PREPARE -- - let dir = test_dir("replace-mode"); - let path = dir.join("config.toml"); - fs::write(&path, "old content").unwrap(); - fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap(); - - // -- EXECUTE -- - replace_file(&path, b"new content").unwrap(); - - // -- ASSERT -- - let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777; - assert_eq!(mode, 0o640); - } - - #[cfg(windows)] - fn acl(path: &std::path::Path) -> String { - let output = std::process::Command::new("icacls") - .arg(path) - .output() - .unwrap(); - assert!(output.status.success()); - // The first line starts with the path; the last one is a summary. - let text = String::from_utf8_lossy(&output.stdout).replace(&*path.to_string_lossy(), ""); - text.lines() - .map(str::trim) - .filter(|line| line.contains(':') && !line.contains("Successfully")) - .collect::>() - .join("\n") - } - - #[cfg(windows)] - #[test] - fn test_replace_file_keeps_the_restricted_acl() { - // -- PREPARE -- - let dir = test_dir("replace-acl"); - let path = dir.join("config.toml"); - fs::write(&path, "old content").unwrap(); - // Not inherited from the folder, like the ACL the installers set. - let status = std::process::Command::new("icacls") - .arg(&path) - .args(["/inheritance:r", "/grant:r", "*S-1-5-18:F", "*S-1-5-11:F"]) - .status() - .unwrap(); - assert!(status.success()); - let before = acl(&path); - - // -- EXECUTE -- - replace_file(&path, b"new content").unwrap(); - - // -- ASSERT -- - assert_eq!(fs::read_to_string(&path).unwrap(), "new content"); - assert_eq!(acl(&path), before); - assert!( - !before.contains("(I)"), - "the test ACL should not be inherited" - ); - } - - #[cfg(windows)] - #[test] - fn test_replace_file_keeps_the_old_content_when_the_file_cannot_be_replaced() { - use std::os::windows::fs::OpenOptionsExt; - - // -- PREPARE -- - let dir = test_dir("replace-locked"); - let path = dir.join("config.toml"); - fs::write(&path, "old content").unwrap(); - // Opened without sharing: nothing else can replace or delete it meanwhile. - let lock = fs::OpenOptions::new() - .read(true) - .share_mode(0) - .open(&path) - .unwrap(); - - // -- EXECUTE -- - let result = replace_file(&path, b"new content"); - drop(lock); - - // -- ASSERT -- - assert!(result.is_err()); - assert_eq!(fs::read_to_string(&path).unwrap(), "old content"); - assert!(!dir.join("config.toml.tmp").exists()); - } -} From c0c38bfdf687e72430191a300d376c9b5868b626 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Thu, 15 Oct 2026 14:21:00 +0200 Subject: [PATCH 05/13] fix: review pr Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/secrets/config_store.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 103acb16..1dbf3086 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -12,7 +12,9 @@ impl SecretStore for ConfigStore { if name != TOKEN { return Err(Error::Internal(format!("no secret named {name}"))); } - let settings = Settings::new().map_err(|err| Error::Internal(err.to_string()))?; + let settings = Settings::new().map_err(|_| { + Error::Internal("Unable to read token from agent configuration".to_string()) + })?; Ok(settings.openaev.token) } } From 123e1dd7873ad5842400ab215e584041a1ac4fbd Mon Sep 17 00:00:00 2001 From: savacano28 Date: Thu, 15 Oct 2026 14:28:13 +0200 Subject: [PATCH 06/13] fix(agent): review pr --- src/tests/api/client.rs | 57 ++++++++++++++++++++++++++++++++++++++++ src/tests/secrets/mod.rs | 10 ++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index ad400266..54640eea 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -230,4 +230,61 @@ mod tests { drop(copy); assert!(dropped.load(Ordering::SeqCst), "wiped with its last copy"); } + + #[test] + fn test_client_sends_the_new_token_after_a_rotation() { + // -- PREPARE -- + let mut server = mockito::Server::new(); + let old_token = server + .mock("POST", "/api/test") + .match_header(AUTHORIZATION_HEADER, "Bearer old-token") + .with_status(200) + .expect(1) + .create(); + let new_token = server + .mock("POST", "/api/test") + .match_header(AUTHORIZATION_HEADER, "Bearer new-token") + .with_status(200) + .expect(1) + .create(); + let store = MemoryStore::with_token("old-token"); + let client = Client::new(server.url(), store.clone(), false, false); + + // -- EXECUTE -- + let before = client.post("/api/test").unwrap().send().unwrap(); + store.set_token("new-token"); + let after = client.post("/api/test").unwrap().send().unwrap(); + + // -- ASSERT -- + // The same client picks up the new token: it is read from the store on each call. + assert!(before.status().is_success()); + assert!(after.status().is_success()); + old_token.assert(); + new_token.assert(); + } + + #[test] + fn test_client_recovers_once_the_store_can_be_read_again() { + // -- PREPARE -- + let mut server = mockito::Server::new(); + let request = server + .mock("POST", "/api/test") + .match_header(AUTHORIZATION_HEADER, format!("Bearer {TOKEN}").as_str()) + .with_status(200) + .expect(1) + .create(); + let store = Arc::new(MemoryStore::default()); + let client = Client::new(server.url(), store.clone(), false, false); + + // -- EXECUTE -- + let failed = client.post("/api/test"); + store.set_token(TOKEN); + let recovered = client.post("/api/test").unwrap().send().unwrap(); + + // -- ASSERT -- + assert!(failed.is_err(), "no request without a token"); + assert!(recovered.status().is_success()); + // Exactly one request reached the platform: none was sent while the store failed. + request.assert(); + } } diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index 14e7a5c0..720c533c 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -15,12 +15,16 @@ pub struct MemoryStore { impl MemoryStore { pub fn with_token(token: &str) -> Arc { let store = MemoryStore::default(); - store - .secrets + store.set_token(token); + Arc::new(store) + } + + /// Replaces the token, like a rotation or a store that can be read again. + pub fn set_token(&self, token: &str) { + self.secrets .lock() .unwrap() .insert(crate::secrets::TOKEN.to_string(), token.to_string()); - Arc::new(store) } } From 7b98991fb2d9481da8f73930d455998722c64903 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Thu, 15 Oct 2026 15:39:48 +0200 Subject: [PATCH 07/13] fix(agent): review pr --- src/config/settings.rs | 47 +++++++++----- src/secrets/config_store.rs | 18 +++++- src/secrets/mod.rs | 3 +- src/tests/secrets/config_store_tests.rs | 86 ++++++++++++++++++++++++- src/tests/secrets/mod.rs | 18 ++++++ 5 files changed, 151 insertions(+), 21 deletions(-) diff --git a/src/config/settings.rs b/src/config/settings.rs index e7fd907e..6bb83666 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -2,6 +2,7 @@ use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::env; use std::fmt; +use std::path::{Path, PathBuf}; use zeroize::Zeroizing; const ENV_PRODUCTION: &str = "production"; @@ -80,25 +81,39 @@ impl Settings { } pub fn new() -> Result { - let run_mode = Self::mode(); - let config = Config::builder().add_source(Environment::with_prefix("openaev")); - if run_mode == ENV_PRODUCTION { + match Self::config_file() { + Some(config_file) => Self::from_file(&config_file), + None => { + let run_mode = Self::mode(); + Config::builder() + .add_source(Environment::with_prefix("openaev")) + .add_source(File::with_name("config/default")) + .add_source(File::with_name(&format!("config/{run_mode}")).required(false)) + .build()? + .try_deserialize() + } + } + } + + /// The config file the installers write next to the executable, without its extension. + /// `None` in development, where the settings come from `config/`. + pub fn config_file() -> Option { + (Self::mode() == ENV_PRODUCTION).then(|| { // Get the current executable path let current_exe_patch = env::current_exe().unwrap(); let parent_path = current_exe_patch.parent().unwrap(); // Join the expected config file with the parent - let config_file = parent_path.join(ENV_PRODUCTION_CONFIG_FILE); - let config_path = config_file.display(); - config - .add_source(File::with_name(&config_path.to_string()).required(true)) - .build()? - .try_deserialize() - } else { - config - .add_source(File::with_name("config/default")) - .add_source(File::with_name(&format!("config/{run_mode}")).required(false)) - .build()? - .try_deserialize() - } + parent_path.join(ENV_PRODUCTION_CONFIG_FILE) + }) + } + + /// Loads the settings the way production does, from `config_file` (without its + /// extension). + pub fn from_file(config_file: &Path) -> Result { + Config::builder() + .add_source(Environment::with_prefix("openaev")) + .add_source(File::with_name(&config_file.display().to_string()).required(true)) + .build()? + .try_deserialize() } } diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 1dbf3086..b7b9db71 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -1,18 +1,32 @@ use super::{SecretStore, TOKEN}; use crate::common::error_model::Error; use crate::config::settings::Settings; +use std::path::PathBuf; use zeroize::Zeroizing; /// The token in the config, where the installers write it. Read again on each use, never /// kept in memory. -pub struct ConfigStore; +pub struct ConfigStore { + /// The config file, without its extension; `None` reads the development settings. + config_file: Option, +} + +impl ConfigStore { + pub fn new(config_file: Option) -> Self { + ConfigStore { config_file } + } +} impl SecretStore for ConfigStore { fn get(&self, name: &str) -> Result, Error> { if name != TOKEN { return Err(Error::Internal(format!("no secret named {name}"))); } - let settings = Settings::new().map_err(|_| { + let settings = match &self.config_file { + Some(config_file) => Settings::from_file(config_file), + None => Settings::new(), + } + .map_err(|_| { Error::Internal("Unable to read token from agent configuration".to_string()) })?; Ok(settings.openaev.token) diff --git a/src/secrets/mod.rs b/src/secrets/mod.rs index ab43e182..be3cce2c 100644 --- a/src/secrets/mod.rs +++ b/src/secrets/mod.rs @@ -9,6 +9,7 @@ mod config_store; pub use config_store::ConfigStore; use crate::common::error_model::Error; +use crate::config::settings::Settings; use std::sync::Arc; use zeroize::Zeroizing; @@ -22,5 +23,5 @@ pub trait SecretStore: Send + Sync { /// Opens the store the agent reads its token from. pub fn open() -> Arc { - Arc::new(ConfigStore) + Arc::new(ConfigStore::new(Settings::config_file())) } diff --git a/src/tests/secrets/config_store_tests.rs b/src/tests/secrets/config_store_tests.rs index b43f0fba..62f75dbf 100644 --- a/src/tests/secrets/config_store_tests.rs +++ b/src/tests/secrets/config_store_tests.rs @@ -1,10 +1,92 @@ #[cfg(test)] mod tests { - use crate::secrets::{ConfigStore, SecretStore}; + use crate::secrets::{ConfigStore, SecretStore, TOKEN}; + use crate::tests::secrets::test_dir; + use std::fs; + use std::path::{Path, PathBuf}; + + /// A config like the one the installers write, holding `token`. + fn config(token: &str) -> String { + format!( + "debug = false\n\n[openaev]\nurl = \"http://localhost:3001\"\ntoken = \"{token}\"\n\ + unsecured_certificate = false\nwith_proxy = false\ninstallation_mode = \"service\"\n\ + service_name = \"OAEVAgentService\"\ntenant_id = \"tenant\"\n" + ) + } + + /// A store reading `agent-config.toml` in `dir`, given without its extension as in + /// production. + fn store_in(dir: &Path) -> (ConfigStore, PathBuf) { + let store = ConfigStore::new(Some(dir.join("agent-config"))); + (store, dir.join("agent-config.toml")) + } #[test] fn test_config_store_only_holds_the_token() { + // -- PREPARE -- + let (store, file) = store_in(&test_dir("config-store-name")); + fs::write(&file, config("first-token")).unwrap(); + // -- EXECUTE & ASSERT -- - assert!(ConfigStore.get("refresh_token").is_err()); + assert!(store.get("refresh_token").is_err()); + } + + #[test] + fn test_config_store_reads_the_new_token_after_an_edit() { + // -- PREPARE -- + let (store, file) = store_in(&test_dir("config-store-edit")); + fs::write(&file, config("first-token")).unwrap(); + + // -- EXECUTE -- + let before = store.get(TOKEN).unwrap(); + fs::write(&file, config("second-token")).unwrap(); + let after = store.get(TOKEN).unwrap(); + + // -- ASSERT -- + // Same store, no restart: the config is read again on each use. + assert_eq!(before.as_str(), "first-token"); + assert_eq!(after.as_str(), "second-token"); + } + + #[test] + fn test_config_store_fails_without_a_config_and_recovers_once_it_exists() { + // -- PREPARE -- + let (store, file) = store_in(&test_dir("config-store-missing")); + + // -- EXECUTE -- + let missing = store.get(TOKEN); + fs::write(&file, config("first-token")).unwrap(); + let recovered = store.get(TOKEN); + + // -- ASSERT -- + assert!(missing.is_err()); + assert_eq!(recovered.unwrap().as_str(), "first-token"); + } + + #[test] + fn test_config_store_fails_on_an_invalid_config_and_recovers_after_repair() { + // -- PREPARE -- + let (store, file) = store_in(&test_dir("config-store-invalid")); + // The closing quote is missing on the token line itself. + let broken = config("leaked-token").replace("leaked-token\"", "leaked-token"); + let without_token = config("first-token").replace("token = \"first-token\"\n", ""); + + // -- EXECUTE -- + fs::write(&file, broken).unwrap(); + let invalid = store.get(TOKEN); + fs::write(&file, without_token).unwrap(); + let no_token = store.get(TOKEN); + fs::write(&file, config("first-token")).unwrap(); + let repaired = store.get(TOKEN); + + // -- ASSERT -- + let Err(invalid) = invalid else { + panic!("an invalid config must not give a token"); + }; + // The raw parse error quotes the failing line, here the token: it must not reach the + // error message. + assert!(!invalid.to_string().contains("leaked-token")); + assert!(no_token.is_err()); + assert_eq!(repaired.unwrap().as_str(), "first-token"); } } diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index 720c533c..b86c371a 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -3,6 +3,10 @@ mod config_store_tests; use crate::common::error_model::Error; use crate::secrets::SecretStore; use std::collections::HashMap; +use std::env; +use std::fs; +use std::path::PathBuf; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use zeroize::Zeroizing; @@ -47,3 +51,17 @@ impl SecretStore for FailingStore { Err(Error::Internal(format!("no secret named {name}"))) } } + +/// A fresh, empty directory for one test, so that tests running in parallel never share a +/// file. +pub fn test_dir(name: &str) -> PathBuf { + static COUNTER: AtomicUsize = AtomicUsize::new(0); + let dir = env::temp_dir().join(format!( + "openaev-agent-{name}-{}-{}", + std::process::id(), + COUNTER.fetch_add(1, Ordering::SeqCst) + )); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir +} From 026ae530ca102626b2d0795648f63a9add578a45 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Fri, 16 Oct 2026 11:43:35 +0200 Subject: [PATCH 08/13] fix(agent): review pr --- Cargo.lock | 6 +++--- src/api/manage_jobs.rs | 6 +++++- src/tests/api/client.rs | 2 +- src/tests/secrets/config_store_tests.rs | 12 ++++++++---- src/tests/secrets/mod.rs | 26 +++++++++++++++++++++---- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ba07ded..0bd708a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1145,7 +1145,7 @@ name = "openaev-agent" version = "3.261118.0" dependencies = [ "bytes", - "config", + "config", "filetime", "hostname", "log", @@ -1163,7 +1163,7 @@ dependencies = [ "webpki-root-certs", "windows-service", "windows-service-detector", - "zeroize", + "zeroize", ] [[package]] @@ -2767,7 +2767,7 @@ version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" dependencies = [ - "serde", + "serde", ] [[package]] diff --git a/src/api/manage_jobs.rs b/src/api/manage_jobs.rs index 721fcae2..31b3b4fb 100644 --- a/src/api/manage_jobs.rs +++ b/src/api/manage_jobs.rs @@ -59,7 +59,11 @@ impl Client { pub fn clean_job(&self, job_id: &str, tenant_id: String) -> Result<(), Error> { // Post the input to the OpenAEV API let jobs_endpoint = format!("/api/tenants/{}/endpoints/jobs/{}", tenant_id, job_id); - match self.delete(&jobs_endpoint)?.send() { + // The caller only checks is_ok(), so log here: otherwise a token read failure is silent. + let request = self + .delete(&jobs_endpoint) + .inspect_err(|err| error!("Error API clean_job {}", err))?; + match request.send() { Ok(response) => { if response.status().is_success() { Ok(()) diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index 54640eea..c824f406 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -1,4 +1,4 @@ -const TOKEN: &str = "token"; +const TOKEN: &str = "test-token-value"; #[cfg(test)] mod tests { diff --git a/src/tests/secrets/config_store_tests.rs b/src/tests/secrets/config_store_tests.rs index 62f75dbf..0c2ef179 100644 --- a/src/tests/secrets/config_store_tests.rs +++ b/src/tests/secrets/config_store_tests.rs @@ -24,7 +24,8 @@ mod tests { #[test] fn test_config_store_only_holds_the_token() { // -- PREPARE -- - let (store, file) = store_in(&test_dir("config-store-name")); + let dir = test_dir("config-store-name"); + let (store, file) = store_in(&dir); fs::write(&file, config("first-token")).unwrap(); // -- EXECUTE & ASSERT -- @@ -34,7 +35,8 @@ mod tests { #[test] fn test_config_store_reads_the_new_token_after_an_edit() { // -- PREPARE -- - let (store, file) = store_in(&test_dir("config-store-edit")); + let dir = test_dir("config-store-edit"); + let (store, file) = store_in(&dir); fs::write(&file, config("first-token")).unwrap(); // -- EXECUTE -- @@ -51,7 +53,8 @@ mod tests { #[test] fn test_config_store_fails_without_a_config_and_recovers_once_it_exists() { // -- PREPARE -- - let (store, file) = store_in(&test_dir("config-store-missing")); + let dir = test_dir("config-store-missing"); + let (store, file) = store_in(&dir); // -- EXECUTE -- let missing = store.get(TOKEN); @@ -66,7 +69,8 @@ mod tests { #[test] fn test_config_store_fails_on_an_invalid_config_and_recovers_after_repair() { // -- PREPARE -- - let (store, file) = store_in(&test_dir("config-store-invalid")); + let dir = test_dir("config-store-invalid"); + let (store, file) = store_in(&dir); // The closing quote is missing on the token line itself. let broken = config("leaked-token").replace("leaked-token\"", "leaked-token"); let without_token = config("first-token").replace("token = \"first-token\"\n", ""); diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index b86c371a..e68e36bc 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -5,7 +5,8 @@ use crate::secrets::SecretStore; use std::collections::HashMap; use std::env; use std::fs; -use std::path::PathBuf; +use std::ops::Deref; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use zeroize::Zeroizing; @@ -52,9 +53,26 @@ impl SecretStore for FailingStore { } } +/// A directory for one test, deleted with everything in it when dropped. +pub struct TestDir(PathBuf); + +impl Deref for TestDir { + type Target = Path; + + fn deref(&self) -> &Path { + &self.0 + } +} + +impl Drop for TestDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + /// A fresh, empty directory for one test, so that tests running in parallel never share a -/// file. -pub fn test_dir(name: &str) -> PathBuf { +/// file. Keep it in a variable for the whole test: it is deleted when dropped. +pub fn test_dir(name: &str) -> TestDir { static COUNTER: AtomicUsize = AtomicUsize::new(0); let dir = env::temp_dir().join(format!( "openaev-agent-{name}-{}-{}", @@ -63,5 +81,5 @@ pub fn test_dir(name: &str) -> PathBuf { )); let _ = fs::remove_dir_all(&dir); fs::create_dir_all(&dir).unwrap(); - dir + TestDir(dir) } From 45dd3d826df3a3a00ac678a68f9f76a52707383d Mon Sep 17 00:00:00 2001 From: savacano28 Date: Fri, 23 Oct 2026 18:14:17 +0200 Subject: [PATCH 09/13] feat(secret-store): feedback pr --- src/api/mod.rs | 8 +-- src/config/settings.rs | 70 ++++++++++++++----------- src/main.rs | 6 +-- src/process/agent_job.rs | 4 +- src/process/keep_alive.rs | 4 +- src/secrets/config_store.rs | 47 ++++++++++++----- src/secrets/mod.rs | 4 +- src/tests/secrets/config_store_tests.rs | 67 +++++++++++++++++++++-- 8 files changed, 150 insertions(+), 60 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 1b5db542..cfc0b134 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -16,13 +16,13 @@ const BEARER_PREFIX: &[u8] = b"Bearer "; pub struct Client { http_client: reqwest::blocking::Client, server_url: String, - secrets: Arc, + secret_store: Arc, } impl Client { pub fn new( server_url: String, - secrets: Arc, + secret_store: Arc, unsecured_certificate: bool, with_proxy: bool, ) -> Client { @@ -54,14 +54,14 @@ impl Client { Client { http_client: http_client.build().unwrap(), server_url: url, - secrets, + secret_store, } } /// Reads the token from the store for this request only, so it does not stay in memory /// between requests. fn authorization(&self) -> Result { - let token = self.secrets.get(TOKEN)?; + let token = self.secret_store.get(TOKEN)?; bearer_header(token.as_bytes()) } diff --git a/src/config/settings.rs b/src/config/settings.rs index 6bb83666..fa123e42 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -2,7 +2,7 @@ use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::env; use std::fmt; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use zeroize::Zeroizing; const ENV_PRODUCTION: &str = "production"; @@ -81,39 +81,47 @@ impl Settings { } pub fn new() -> Result { - match Self::config_file() { - Some(config_file) => Self::from_file(&config_file), - None => { - let run_mode = Self::mode(); - Config::builder() - .add_source(Environment::with_prefix("openaev")) - .add_source(File::with_name("config/default")) - .add_source(File::with_name(&format!("config/{run_mode}")).required(false)) - .build()? - .try_deserialize() - } - } + Self::source().load() } - /// The config file the installers write next to the executable, without its extension. - /// `None` in development, where the settings come from `config/`. - pub fn config_file() -> Option { - (Self::mode() == ENV_PRODUCTION).then(|| { - // Get the current executable path - let current_exe_patch = env::current_exe().unwrap(); - let parent_path = current_exe_patch.parent().unwrap(); - // Join the expected config file with the parent - parent_path.join(ENV_PRODUCTION_CONFIG_FILE) - }) + /// Where this run reads its settings from: the config file the installers write next to + /// the executable in production, and the `config/` folder otherwise. + pub fn source() -> SettingsSource { + if Self::mode() != ENV_PRODUCTION { + return SettingsSource::Development; + } + // Get the current executable path + let current_exe_patch = env::current_exe().unwrap(); + let parent_path = current_exe_patch.parent().unwrap(); + // Join the expected config file with the parent + SettingsSource::File(parent_path.join(ENV_PRODUCTION_CONFIG_FILE)) } +} + +/// Where the settings are read from, so that they can be read again later. It holds only the +/// location, never the settings themselves, so keeping it does not keep the token in memory. +#[derive(Clone, Debug)] +pub enum SettingsSource { + /// `config/default.toml`, then `config/.toml` if it exists. + Development, + /// The config file the installers write, given without its extension. + File(PathBuf), +} - /// Loads the settings the way production does, from `config_file` (without its - /// extension). - pub fn from_file(config_file: &Path) -> Result { - Config::builder() - .add_source(Environment::with_prefix("openaev")) - .add_source(File::with_name(&config_file.display().to_string()).required(true)) - .build()? - .try_deserialize() +impl SettingsSource { + /// Reads the settings again from this source, with the `OPENAEV_*` environment variables + /// on top. + pub fn load(&self) -> Result { + let config = Config::builder().add_source(Environment::with_prefix("openaev")); + let config = match self { + SettingsSource::Development => config + .add_source(File::with_name("config/default")) + .add_source( + File::with_name(&format!("config/{}", Settings::mode())).required(false), + ), + SettingsSource::File(config_file) => config + .add_source(File::with_name(&config_file.display().to_string()).required(true)), + }; + config.build()?.try_deserialize() } } diff --git a/src/main.rs b/src/main.rs index 5ea075d5..25757b56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ fn compute_working_dir() -> PathBuf { fn agent_start(settings_data: Settings, is_service: bool) -> Result>, Error> { let url = settings_data.openaev.url; - let secrets = secrets::open(); + let secret_store = secrets::open(); let unsecured_certificate = settings_data.openaev.unsecured_certificate; let with_proxy = settings_data.openaev.with_proxy; let installation_mode = settings_data.openaev.installation_mode; @@ -82,7 +82,7 @@ fn agent_start(settings_data: Settings, is_service: bool) -> Result Result, + secret_store: Arc, unsecured_certificate: bool, with_proxy: bool, execution_details: ExecutionDetails, tenant_id: String, ) -> Result, Error> { info!("Starting listening jobs thread"); - let api = Client::new(uri, secrets, unsecured_certificate, with_proxy); + let api = Client::new(uri, secret_store, unsecured_certificate, with_proxy); let handle = thread::spawn(move || { // While no stop signal received while THREADS_CONTROL.load(Ordering::Relaxed) { diff --git a/src/process/keep_alive.rs b/src/process/keep_alive.rs index 937faa9b..e72fc793 100644 --- a/src/process/keep_alive.rs +++ b/src/process/keep_alive.rs @@ -13,7 +13,7 @@ use std::time::Duration; #[allow(clippy::too_many_arguments)] pub fn ping( uri: String, - secrets: Arc, + secret_store: Arc, unsecured_certificate: bool, with_proxy: bool, installation_mode: String, @@ -22,7 +22,7 @@ pub fn ping( tenant_id: String, ) -> Result, Error> { info!("Starting ping thread"); - let api = Client::new(uri, secrets, unsecured_certificate, with_proxy); + let api = Client::new(uri, secret_store, unsecured_certificate, with_proxy); let handle = thread::spawn(move || { // While no stop signal received while THREADS_CONTROL.load(Ordering::Relaxed) { diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index b7b9db71..1c856234 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -1,19 +1,20 @@ use super::{SecretStore, TOKEN}; use crate::common::error_model::Error; -use crate::config::settings::Settings; -use std::path::PathBuf; +use crate::config::settings::SettingsSource; +use config::ConfigError; +use std::io; use zeroize::Zeroizing; /// The token in the config, where the installers write it. Read again on each use, never /// kept in memory. pub struct ConfigStore { - /// The config file, without its extension; `None` reads the development settings. - config_file: Option, + /// Where the settings are read from. It holds no setting, so no token either. + source: SettingsSource, } impl ConfigStore { - pub fn new(config_file: Option) -> Self { - ConfigStore { config_file } + pub fn new(source: SettingsSource) -> Self { + ConfigStore { source } } } @@ -22,13 +23,35 @@ impl SecretStore for ConfigStore { if name != TOKEN { return Err(Error::Internal(format!("no secret named {name}"))); } - let settings = match &self.config_file { - Some(config_file) => Settings::from_file(config_file), - None => Settings::new(), - } - .map_err(|_| { - Error::Internal("Unable to read token from agent configuration".to_string()) + let settings = self.source.load().map_err(|err| { + Error::Internal(format!( + "Unable to read token from agent configuration: {}", + reason(&err) + )) })?; Ok(settings.openaev.token) } } + +/// Explains why the config could not be read, without using the text of `err`. That text can +/// hold the token: a parse error quotes the line that failed, and a type error quotes the value +/// it found. So the explanation is built only from the kind of error and the setting's name. +pub(crate) fn reason(err: &ConfigError) -> String { + match err { + ConfigError::Foreign(cause) => match cause.downcast_ref::().map(io::Error::kind) + { + Some(io::ErrorKind::NotFound) => "the config file is missing".to_string(), + Some(io::ErrorKind::PermissionDenied) => { + "access to the config file is denied".to_string() + } + _ => "the config file cannot be read".to_string(), + }, + ConfigError::FileParse { .. } => "the config file is not valid".to_string(), + ConfigError::NotFound(key) => format!("the setting `{key}` is missing"), + ConfigError::Type { key: Some(key), .. } => { + format!("the setting `{key}` has the wrong type") + } + ConfigError::At { error, .. } => reason(error), + _ => "a setting is invalid".to_string(), + } +} diff --git a/src/secrets/mod.rs b/src/secrets/mod.rs index be3cce2c..0fc68434 100644 --- a/src/secrets/mod.rs +++ b/src/secrets/mod.rs @@ -4,7 +4,7 @@ //! memory. The only store today is the config the installers write; OS secret stores plug in //! behind the same trait. -mod config_store; +pub(crate) mod config_store; pub use config_store::ConfigStore; @@ -23,5 +23,5 @@ pub trait SecretStore: Send + Sync { /// Opens the store the agent reads its token from. pub fn open() -> Arc { - Arc::new(ConfigStore::new(Settings::config_file())) + Arc::new(ConfigStore::new(Settings::source())) } diff --git a/src/tests/secrets/config_store_tests.rs b/src/tests/secrets/config_store_tests.rs index 0c2ef179..ac3e600b 100644 --- a/src/tests/secrets/config_store_tests.rs +++ b/src/tests/secrets/config_store_tests.rs @@ -1,8 +1,12 @@ #[cfg(test)] mod tests { + use crate::config::settings::SettingsSource; + use crate::secrets::config_store::reason; use crate::secrets::{ConfigStore, SecretStore, TOKEN}; use crate::tests::secrets::test_dir; + use config::ConfigError; use std::fs; + use std::io; use std::path::{Path, PathBuf}; /// A config like the one the installers write, holding `token`. @@ -17,7 +21,7 @@ mod tests { /// A store reading `agent-config.toml` in `dir`, given without its extension as in /// production. fn store_in(dir: &Path) -> (ConfigStore, PathBuf) { - let store = ConfigStore::new(Some(dir.join("agent-config"))); + let store = ConfigStore::new(SettingsSource::File(dir.join("agent-config"))); (store, dir.join("agent-config.toml")) } @@ -62,7 +66,10 @@ mod tests { let recovered = store.get(TOKEN); // -- ASSERT -- - assert!(missing.is_err()); + let Err(missing) = missing else { + panic!("a missing config must not give a token"); + }; + assert!(missing.to_string().contains("the config file is missing")); assert_eq!(recovered.unwrap().as_str(), "first-token"); } @@ -89,8 +96,60 @@ mod tests { }; // The raw parse error quotes the failing line, here the token: it must not reach the // error message. - assert!(!invalid.to_string().contains("leaked-token")); - assert!(no_token.is_err()); + let invalid = invalid.to_string(); + assert!(!invalid.contains("leaked-token"), "{invalid}"); + // It still says why, so that the log is useful. + assert!( + invalid.contains("the config file is not valid"), + "{invalid}" + ); + let Err(no_token) = no_token else { + panic!("a config without a token must not give one"); + }; + let no_token = no_token.to_string(); + assert!( + no_token.contains("the setting `openaev.token` is missing"), + "{no_token}" + ); assert_eq!(repaired.unwrap().as_str(), "first-token"); } + + #[test] + fn test_config_store_names_a_setting_of_the_wrong_type_without_its_value() { + // -- PREPARE -- + let dir = test_dir("config-store-type"); + let (store, file) = store_in(&dir); + let wrong_type = config("first-token").replace( + "unsecured_certificate = false", + "unsecured_certificate = \"leaked-value\"", + ); + fs::write(&file, wrong_type).unwrap(); + + // -- EXECUTE -- + let Err(err) = store.get(TOKEN) else { + panic!("a setting of the wrong type must not give a token"); + }; + + // -- ASSERT -- + // The raw type error quotes the value it found, and that value could be a secret. + let message = err.to_string(); + assert!(!message.contains("leaked-value"), "{message}"); + assert!( + message.contains("the setting `openaev.unsecured_certificate` has the wrong type"), + "{message}" + ); + } + + #[test] + fn test_reason_tells_a_denied_access_from_a_missing_file() { + // -- PREPARE -- + // A denied read is hard to set up the same way on every OS, so the errors are built here. + let denied = + ConfigError::Foreign(Box::new(io::Error::from(io::ErrorKind::PermissionDenied))); + let missing = ConfigError::Foreign(Box::new(io::Error::from(io::ErrorKind::NotFound))); + + // -- EXECUTE & ASSERT -- + assert_eq!(reason(&denied), "access to the config file is denied"); + assert_eq!(reason(&missing), "the config file is missing"); + } } From b7188e580baae544ceb880bbb047b0bcc9d03fc8 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 2 Nov 2026 16:32:52 +0100 Subject: [PATCH 10/13] Update src/config/settings.rs Co-authored-by: antoinemzs --- src/config/settings.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/settings.rs b/src/config/settings.rs index fa123e42..7328f818 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -91,8 +91,8 @@ impl Settings { return SettingsSource::Development; } // Get the current executable path - let current_exe_patch = env::current_exe().unwrap(); - let parent_path = current_exe_patch.parent().unwrap(); + let current_exe_path = env::current_exe().unwrap(); + let parent_path = current_exe_path.parent().unwrap(); // Join the expected config file with the parent SettingsSource::File(parent_path.join(ENV_PRODUCTION_CONFIG_FILE)) } From fbcbb753961dd1fa91a5d9bb06a6306b5d8870d7 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Mon, 2 Nov 2026 19:13:11 +0100 Subject: [PATCH 11/13] feat(agent): feedback pr --- src/api/manage_jobs.rs | 6 +----- src/secrets/config_store.rs | 7 +++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/api/manage_jobs.rs b/src/api/manage_jobs.rs index 31b3b4fb..721fcae2 100644 --- a/src/api/manage_jobs.rs +++ b/src/api/manage_jobs.rs @@ -59,11 +59,7 @@ impl Client { pub fn clean_job(&self, job_id: &str, tenant_id: String) -> Result<(), Error> { // Post the input to the OpenAEV API let jobs_endpoint = format!("/api/tenants/{}/endpoints/jobs/{}", tenant_id, job_id); - // The caller only checks is_ok(), so log here: otherwise a token read failure is silent. - let request = self - .delete(&jobs_endpoint) - .inspect_err(|err| error!("Error API clean_job {}", err))?; - match request.send() { + match self.delete(&jobs_endpoint)?.send() { Ok(response) => { if response.status().is_success() { Ok(()) diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 1c856234..8705f4b2 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -2,6 +2,7 @@ use super::{SecretStore, TOKEN}; use crate::common::error_model::Error; use crate::config::settings::SettingsSource; use config::ConfigError; +use log::error; use std::io; use zeroize::Zeroizing; @@ -24,10 +25,12 @@ impl SecretStore for ConfigStore { return Err(Error::Internal(format!("no secret named {name}"))); } let settings = self.source.load().map_err(|err| { - Error::Internal(format!( + let message = format!( "Unable to read token from agent configuration: {}", reason(&err) - )) + ); + error!("{message}"); + Error::Internal(message) })?; Ok(settings.openaev.token) } From 8b2643a485ca5aed66ed97c44718d66cb0eb6f48 Mon Sep 17 00:00:00 2001 From: savacano28 Date: Mon, 9 Nov 2026 17:29:43 +0100 Subject: [PATCH 12/13] feat(splunk): feedback review --- src/api/mod.rs | 16 +++++++--------- src/config/settings.rs | 19 ++++++++----------- src/secrets/config_store.rs | 10 ++++------ src/secrets/mod.rs | 9 ++++----- src/tests/api/client.rs | 2 +- src/tests/secrets/mod.rs | 2 +- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index cfc0b134..cc19f65b 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -58,8 +58,7 @@ impl Client { } } - /// Reads the token from the store for this request only, so it does not stay in memory - /// between requests. + /// Reads the token from the store for this request only, so it is not kept in memory. fn authorization(&self) -> Result { let token = self.secret_store.get(TOKEN)?; bearer_header(token.as_bytes()) @@ -91,20 +90,19 @@ impl Client { } } -/// The `Authorization` value for `token`, in a buffer wiped once the request is done with it. +/// Builds the `Bearer ` header value in a `Zeroizing` buffer. /// -/// The header holds this very buffer, not a copy: `HeaderValue` keeps a `Bytes` as it is, and -/// `Bytes::from_owner` drops (so wipes) the `Zeroizing` buffer when its last reference goes. -/// The HTTP and TLS layers still copy the bytes they send into their own buffers, which are -/// not wiped. +/// `Bytes::from_owner` makes the header own that buffer instead of copying it, so the token is +/// wiped when the last copy of the header is dropped. Limit: the HTTP and TLS layers copy the +/// bytes they send, and those copies are not wiped. pub(crate) fn bearer_header(token: &[u8]) -> Result { - // Allocated at its final size: growing it would leave an unwiped copy behind. + // Exact capacity: if the Vec had to grow, it would leave an unwiped copy of the token behind. let mut bearer = Zeroizing::new(Vec::with_capacity(BEARER_PREFIX.len() + token.len())); bearer.extend_from_slice(BEARER_PREFIX); bearer.extend_from_slice(token); let mut value = HeaderValue::from_maybe_shared(Bytes::from_owner(bearer)) .map_err(|err| Error::Internal(err.to_string()))?; - // Keeps it out of reqwest's debug output. + // Hides the value in reqwest's `Debug` output. value.set_sensitive(true); Ok(value) } diff --git a/src/config/settings.rs b/src/config/settings.rs index 7328f818..8c1dbb6f 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -12,8 +12,8 @@ const ENV_PRODUCTION_CONFIG_FILE: &str = "openaev-agent-config"; #[allow(unused)] pub struct OpenAEV { pub url: String, - // Zeroizing from deserialization on, so that every path that drops it wipes it. The - // copies the config crate makes while loading the file are not. + // Wiped from memory when dropped. Limit: the copies the `config` crate makes while parsing + // the file are not wiped. pub token: Zeroizing, pub unsecured_certificate: bool, pub with_proxy: bool, @@ -22,7 +22,7 @@ pub struct OpenAEV { pub tenant_id: String, } -// Written by hand so that printing the settings never prints the token. +// Written by hand so that the token is never printed. impl fmt::Debug for OpenAEV { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("OpenAEV") @@ -84,22 +84,20 @@ impl Settings { Self::source().load() } - /// Where this run reads its settings from: the config file the installers write next to - /// the executable in production, and the `config/` folder otherwise. + /// Where to read the settings: the config file next to the executable in production, the + /// `config/` folder otherwise. pub fn source() -> SettingsSource { if Self::mode() != ENV_PRODUCTION { return SettingsSource::Development; } - // Get the current executable path let current_exe_path = env::current_exe().unwrap(); let parent_path = current_exe_path.parent().unwrap(); - // Join the expected config file with the parent SettingsSource::File(parent_path.join(ENV_PRODUCTION_CONFIG_FILE)) } } -/// Where the settings are read from, so that they can be read again later. It holds only the -/// location, never the settings themselves, so keeping it does not keep the token in memory. +/// Where the settings are read from. It stores the location only, not the values, so keeping +/// it does not keep the token in memory. #[derive(Clone, Debug)] pub enum SettingsSource { /// `config/default.toml`, then `config/.toml` if it exists. @@ -109,8 +107,7 @@ pub enum SettingsSource { } impl SettingsSource { - /// Reads the settings again from this source, with the `OPENAEV_*` environment variables - /// on top. + /// Reads the settings from this source. `OPENAEV_*` environment variables override the file. pub fn load(&self) -> Result { let config = Config::builder().add_source(Environment::with_prefix("openaev")); let config = match self { diff --git a/src/secrets/config_store.rs b/src/secrets/config_store.rs index 8705f4b2..1ba19ad7 100644 --- a/src/secrets/config_store.rs +++ b/src/secrets/config_store.rs @@ -6,10 +6,8 @@ use log::error; use std::io; use zeroize::Zeroizing; -/// The token in the config, where the installers write it. Read again on each use, never -/// kept in memory. +/// Reads the token from the agent config file on each call. Nothing is cached. pub struct ConfigStore { - /// Where the settings are read from. It holds no setting, so no token either. source: SettingsSource, } @@ -36,9 +34,9 @@ impl SecretStore for ConfigStore { } } -/// Explains why the config could not be read, without using the text of `err`. That text can -/// hold the token: a parse error quotes the line that failed, and a type error quotes the value -/// it found. So the explanation is built only from the kind of error and the setting's name. +/// Says why the config could not be read, using only the error kind and the setting name. +/// The error's own text is not used: it can contain the token (a parse error quotes the failing +/// line, a type error quotes the value). pub(crate) fn reason(err: &ConfigError) -> String { match err { ConfigError::Foreign(cause) => match cause.downcast_ref::().map(io::Error::kind) diff --git a/src/secrets/mod.rs b/src/secrets/mod.rs index 0fc68434..744f3dae 100644 --- a/src/secrets/mod.rs +++ b/src/secrets/mod.rs @@ -1,8 +1,7 @@ -//! Where the agent keeps the secrets it authenticates with. +//! Secrets the agent authenticates with (today, only the platform token). //! -//! The agent reads its token through a [`SecretStore`] on each use rather than keeping it in -//! memory. The only store today is the config the installers write; OS secret stores plug in -//! behind the same trait. +//! The token is read through a [`SecretStore`] on each use instead of being kept in memory. +//! `ConfigStore` is the only store for now; OS secret stores will implement the same trait. pub(crate) mod config_store; @@ -13,7 +12,7 @@ use crate::config::settings::Settings; use std::sync::Arc; use zeroize::Zeroizing; -/// Name of the secret the agent authenticates to the platform with. +/// Name of the platform token secret. pub const TOKEN: &str = "token"; pub trait SecretStore: Send + Sync { diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index c824f406..033beacc 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -195,7 +195,7 @@ mod tests { assert!(value.is_sensitive()); } - /// Stands in for the `Zeroizing` buffer: records when it is dropped, so wiped. + /// Fake `Zeroizing` buffer: records when it is dropped (when the real one would be wiped). struct DropFlag(Vec, Arc); impl AsRef<[u8]> for DropFlag { diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index e68e36bc..ebf26d9f 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -24,7 +24,7 @@ impl MemoryStore { Arc::new(store) } - /// Replaces the token, like a rotation or a store that can be read again. + /// Replaces the token, to simulate a rotation or a store that recovers. pub fn set_token(&self, token: &str) { self.secrets .lock() From 5239fd8b3d1d20712ad80d6d0a1ede2b033eaf5a Mon Sep 17 00:00:00 2001 From: savacano28 Date: Mon, 9 Nov 2026 18:34:52 +0100 Subject: [PATCH 13/13] feat(splunk): feedback review --- src/tests/api/client.rs | 4 ++-- src/tests/secrets/mod.rs | 12 ++---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/tests/api/client.rs b/src/tests/api/client.rs index 033beacc..feb605d3 100644 --- a/src/tests/api/client.rs +++ b/src/tests/api/client.rs @@ -5,7 +5,7 @@ mod tests { use crate::api::{bearer_header, Client, AUTHORIZATION_HEADER}; use crate::tests::api::client::TOKEN; use crate::tests::api::tls_server::serve_self_signed_https; - use crate::tests::secrets::{FailingStore, MemoryStore}; + use crate::tests::secrets::MemoryStore; use bytes::Bytes; use mockito; use reqwest::header::HeaderValue; @@ -56,7 +56,7 @@ mod tests { // -- PREPARE -- let client = Client::new( "http://localhost".to_string(), - Arc::new(FailingStore), + Arc::new(MemoryStore::default()), false, false, ); diff --git a/src/tests/secrets/mod.rs b/src/tests/secrets/mod.rs index ebf26d9f..61b5dcc4 100644 --- a/src/tests/secrets/mod.rs +++ b/src/tests/secrets/mod.rs @@ -11,7 +11,8 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use zeroize::Zeroizing; -/// A secret store kept in memory, standing in for a real one. +/// A secret store kept in memory, standing in for a real one. When empty, it fails like a +/// store that cannot be read. #[derive(Default)] pub struct MemoryStore { secrets: Mutex>, @@ -44,15 +45,6 @@ impl SecretStore for MemoryStore { } } -/// A secret store that cannot be read. -pub struct FailingStore; - -impl SecretStore for FailingStore { - fn get(&self, name: &str) -> Result, Error> { - Err(Error::Internal(format!("no secret named {name}"))) - } -} - /// A directory for one test, deleted with everything in it when dropped. pub struct TestDir(PathBuf);