The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(credentials): seed credentials from environment variables on boot
Steven Enamakelb0e3aea6faa0
diff --git a/crates/openhuman-core/src/core/jsonrpc.rs b/crates/openhuman-core/src/core/jsonrpc.rsindex 147dfedf3e..f689ee04eb 100644--- a/crates/openhuman-core/src/core/jsonrpc.rs+++ b/crates/openhuman-core/src/core/jsonrpc.rs@@ -1678,6 +1678,19 @@ fn register_domain_subscribers(// battery-powered hosts).crate::cron::scheduler_gate::init_global(&config);+ // A headless host (Docker / VPS / CI) has no interactive login; it+ // hands the core a credential through the environment instead. The+ // API key is a plain profile write, so it lands before the gate is+ // seeded from the store below; a session token runs the full+ // `set_credential` path and seeds the gate itself when it finishes.+ crate::security::credentials::seed_api_key_from_env(&config);+ if std::env::var_os(crate::security::credentials::BACKEND_SESSION_TOKEN_ENV).is_some() {+ let config = config.clone();+ tokio::spawn(async move {+ crate::security::credentials::seed_session_from_env(&config).await;+ });+ }+// Seed the scheduler-gate signed-out override from the on-disk// credential — an API key (library runtime) or the app session.// Without this, a sidecar that boots with no stored credential woulddiff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rsindex dc382e0a98..df6b4a68f8 100644--- a/crates/openhuman-core/src/core/runtime/context.rs+++ b/crates/openhuman-core/src/core/runtime/context.rs@@ -908,6 +908,10 @@ pub async fn init_stores(cfg: &crate::config::Config, domains: crate::core::runtif let Some(uid) = state.user_id.as_deref() {crate::security::credentials::sentry_scope::bind(uid);}+ // The host-supplied user payload survives restarts in the profile+ // store; seed the identity slot from it so prompt composition sees+ // the signed-in user before any host RPC runs.+ crate::security::credentials::identity::set_current_user(state.user);}Err(e) => {log::debug!("[boot] sentry scope user bind skipped — build_session_state failed: {e}")diff --git a/crates/openhuman-core/src/security/credentials/ops.rs b/crates/openhuman-core/src/security/credentials/ops.rsindex 71d87c9132..ea1dba92cc 100644--- a/crates/openhuman-core/src/security/credentials/ops.rs+++ b/crates/openhuman-core/src/security/credentials/ops.rs@@ -1,5 +1,6 @@//! JSON-RPC / CLI controller surface for credentials and app session auth.+mod boot_env;mod composio;mod credential;mod gated_services;@@ -14,6 +15,9 @@ pub use composio::{clear_composio_api_key, get_composio_api_key, rpc_store_composio_api_key,store_composio_api_key, COMPOSIO_DIRECT_PROVIDER,};+pub use boot_env::{+ seed_api_key_from_env, seed_session_from_env, BACKEND_API_KEY_ENV, BACKEND_SESSION_TOKEN_ENV,+};pub use credential::{clear_credential, clear_session, set_credential, store_session, SetCredentialRequest,PENDING_BACKEND_VALIDATION_FIELD,diff --git a/crates/openhuman-core/src/security/credentials/ops/boot_env.rs b/crates/openhuman-core/src/security/credentials/ops/boot_env.rsnew file mode 100644index 0000000000..64d5d3e76c--- /dev/null+++ b/crates/openhuman-core/src/security/credentials/ops/boot_env.rs@@ -0,0 +1,77 @@+//! Boot-time credential seeding for hosts with no interactive login: a+//! Docker / VPS core, a CI sidecar, an operator's shell. The credential still+//! has to be obtained elsewhere (the TinyHumans dashboard, a shell login);+//! this only installs it.+//!+//! * `OPENHUMAN_BACKEND_API_KEY` — a TinyHumans API key.+//! * `OPENHUMAN_BACKEND_SESSION_TOKEN` — a session JWT (must carry a subject+//! claim, since no host is around to supply the user id).+//!+//! A credential already in the store is never overwritten: the env var seeds+//! a fresh install and is otherwise ignored, so rotating it means clearing the+//! stored one first (`openhuman-core auth clear_credential`).++use crate::config::Config;+use crate::security::credentials::api_key;+use crate::security::credentials::session_support::{CredentialKind, get_session_token};++use super::credential::{set_credential, SetCredentialRequest};++pub const BACKEND_API_KEY_ENV: &str = "OPENHUMAN_BACKEND_API_KEY";+pub const BACKEND_SESSION_TOKEN_ENV: &str = "OPENHUMAN_BACKEND_SESSION_TOKEN";++fn env_secret(name: &str) -> Option<String> {+ std::env::var(name)+ .ok()+ .map(|v| v.trim().to_string())+ .filter(|v| !v.is_empty())+}++/// Install the API key from `OPENHUMAN_BACKEND_API_KEY` when none is stored.+/// Synchronous (a plain profile write) so it can run before the scheduler+/// gate is seeded from the store.+pub fn seed_api_key_from_env(config: &Config) {+ let Some(key) = env_secret(BACKEND_API_KEY_ENV) else {+ return;+ };+ if api_key::has_api_key(config) {+ log::debug!("[auth][boot] {BACKEND_API_KEY_ENV} set but an api key is already stored; keeping the stored one");+ return;+ }+ match api_key::store_api_key(config, &key) {+ Ok(()) => log::info!("[auth][boot] api key installed from {BACKEND_API_KEY_ENV}"),+ Err(error) => log::warn!("[auth][boot] failed to install api key from {BACKEND_API_KEY_ENV}: {error}"),+ }+}++/// Install the session JWT from `OPENHUMAN_BACKEND_SESSION_TOKEN` when no+/// session is stored. Runs the full `set_credential` path (user-dir+/// activation, gated services, scheduler gate), so it is async.+pub async fn seed_session_from_env(config: &Config) {+ let Some(token) = env_secret(BACKEND_SESSION_TOKEN_ENV) else {+ return;+ };+ if matches!(get_session_token(config), Ok(Some(_))) {+ log::debug!("[auth][boot] {BACKEND_SESSION_TOKEN_ENV} set but a session is already stored; keeping the stored one");+ return;+ }+ let request = SetCredentialRequest {+ token,+ kind: Some(CredentialKind::Session.as_str().to_string()),+ user_id: None,+ user: None,+ };+ match set_credential(config, request).await {+ Ok(outcome) => log::info!(+ "[auth][boot] session installed from {BACKEND_SESSION_TOKEN_ENV} (user_id={})",+ outcome.value.user_id.as_deref().unwrap_or("?")+ ),+ Err(error) => log::warn!(+ "[auth][boot] failed to install session from {BACKEND_SESSION_TOKEN_ENV}: {error}"+ ),+ }+}++#[cfg(test)]+#[path = "boot_env_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/security/credentials/ops/boot_env_tests.rs b/crates/openhuman-core/src/security/credentials/ops/boot_env_tests.rsnew file mode 100644index 0000000000..2f14773fa2--- /dev/null+++ b/crates/openhuman-core/src/security/credentials/ops/boot_env_tests.rs@@ -0,0 +1,65 @@+use super::*;+use crate::config::Config;+use tempfile::TempDir;++fn config_in(tmp: &TempDir) -> Config {+ Config {+ workspace_dir: tmp.path().join("workspace"),+ action_dir: tmp.path().join("workspace"),+ config_path: tmp.path().join("config.toml"),+ ..Config::default()+ }+}++struct EnvGuard(&'static str);+impl Drop for EnvGuard {+ fn drop(&mut self) {+ unsafe { std::env::remove_var(self.0) };+ }+}++#[test]+fn api_key_env_seeds_only_an_empty_store() {+ let _lock = crate::config::TEST_ENV_LOCK+ .lock()+ .unwrap_or_else(|e| e.into_inner());+ let tmp = TempDir::new().unwrap();+ let config = config_in(&tmp);+ let _guard = EnvGuard(BACKEND_API_KEY_ENV);++ unsafe { std::env::set_var(BACKEND_API_KEY_ENV, " ") };+ seed_api_key_from_env(&config);+ assert!(!api_key::has_api_key(&config), "blank env must not install a key");++ unsafe { std::env::set_var(BACKEND_API_KEY_ENV, "sk-boot") };+ seed_api_key_from_env(&config);+ assert_eq!(api_key::get_api_key(&config).unwrap().as_deref(), Some("sk-boot"));++ unsafe { std::env::set_var(BACKEND_API_KEY_ENV, "sk-rotated") };+ seed_api_key_from_env(&config);+ assert_eq!(+ api_key::get_api_key(&config).unwrap().as_deref(),+ Some("sk-boot"),+ "a stored key is never overwritten from the environment"+ );+}++#[tokio::test]+async fn session_env_is_ignored_without_a_subject_and_when_unset() {+ let _lock = crate::config::TEST_ENV_LOCK+ .lock()+ .unwrap_or_else(|e| e.into_inner());+ let tmp = TempDir::new().unwrap();+ let config = config_in(&tmp);+ let _guard = EnvGuard(BACKEND_SESSION_TOKEN_ENV);++ unsafe { std::env::remove_var(BACKEND_SESSION_TOKEN_ENV) };+ seed_session_from_env(&config).await;+ assert!(get_session_token(&config).unwrap().is_none());++ // An opaque token has no subject claim and no host to supply one, so the+ // install is refused (logged) and the store stays empty.+ unsafe { std::env::set_var(BACKEND_SESSION_TOKEN_ENV, "opaque-token") };+ seed_session_from_env(&config).await;+ assert!(get_session_token(&config).unwrap().is_none());+}