Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(security): resolve `credential_ref` handles through the keychain

Maciej Myszkiewicze6f3f4f41ba6
diff --git a/src/openhuman/security/credentials/credential_ref.rs b/src/openhuman/security/credentials/credential_ref.rs
new file mode 100644
index 0000000000..5d934752d5
--- /dev/null
+++ b/src/openhuman/security/credentials/credential_ref.rs
@@ -0,0 +1,289 @@
+//! `credential_ref` resolution — the `[subsystems.*]` credential handle.
+//!
+//! `docs/specs/kernel.md` §3.6 gives every subsystem the same driver-binding
+//! config shape, and `docs/specs/plan-memory.md` §4.5 fixes one field of it:
+//!
+//! ```toml
+//! [subsystems.memory.drivers.supermemory]
+//! credential_ref = "keychain:supermemory"
+//! ```
+//!
+//! The value is a **reference**, never an inline secret. This module is the one
+//! place that turns such a reference into the secret it names, so the rule
+//! "config carries handles, the keychain carries secrets" has a single
+//! enforcement point rather than one per subsystem. It lives in `security/`
+//! rather than beside its first caller because the field is uniform across
+//! subsystems by §3.6 — memory binds first, and inference, channels and sandbox
+//! follow onto the same shape (kernel.md §5).
+//!
+//! ## Three refusals before the keychain is touched
+//!
+//! Resolution is deliberately not a bare [`keyring::get`]:
+//!
+//! 1. **Consent.** [`keyring_consent::policy::check_secret_access`] must return
+//! [`PolicyDecision::Proceed`]. A prompt that has not been answered is not a
+//! missing credential, and conflating the two would train an operator to
+//! "fix" a pending consent dialog by editing config.
+//! 2. **Availability.** A host with no usable keychain backend reports that as
+//! itself rather than as a missing entry — the same order
+//! `web3::wallet`'s `keychain_load_mnemonic` uses.
+//! 3. **Absence.** Only then is a `None` from the keychain a genuine
+//! "not configured".
+//!
+//! ## Nothing here may reach an operator-facing string
+//!
+//! [`CredentialRefError`] carries **no name and no secret**, and
+//! [`CredentialRef`]'s `Debug` redacts the name. That is load-bearing rather
+//! than decorative: `MemoryDriverConfig`'s own doc requires the credential
+//! reference to stay out of `Debug`/error output (plan-memory.md §7, Tier 3),
+//! and `memory::binding`'s `FallbackReason` is rendered into
+//! `subsystems_status` — pinned by
+//! `fallback_reason_never_contains_credential_ref_or_endpoint`.
+//!
+//! The sharp edge is [`KeyringError`], whose own `Display` interpolates the
+//! key: `"OS keychain error for key '{key}': {source}"`. Propagating one
+//! verbatim into a bind failure would leak the very name this module exists to
+//! keep out of that string, so backend failures are deliberately mapped to a
+//! name-free variant and the detail is logged instead.
+
+use zeroize::Zeroizing;
+
+use crate::openhuman::security::keyring;
+use crate::openhuman::security::keyring_consent::{policy, PolicyDecision};
+
+/// Log prefix for this module's diagnostics.
+const LOG_PREFIX: &str = "[security:credential-ref]";
+
+/// The `keychain:` scheme — the only one defined today.
+///
+/// Kept as a named constant because it is persisted in users' `config.toml`
+/// and is therefore a compatibility surface, not an implementation detail.
+pub const KEYCHAIN_SCHEME: &str = "keychain";
+
+/// Why a `credential_ref` could not be parsed or resolved.
+///
+/// Every variant's `Display` is safe to place in an operator-facing string: it
+/// names neither the credential nor the secret. See the module docs.
+#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
+pub enum CredentialRefError {
+ /// The reference was empty or whitespace only.
+ #[error("credential_ref is empty")]
+ Empty,
+
+ /// The reference had no `<scheme>:` prefix.
+ #[error(
+ "credential_ref is missing a '<scheme>:' prefix (expected \"{KEYCHAIN_SCHEME}:<name>\")"
+ )]
+ MissingScheme,
+
+ /// The scheme is not one this build understands.
+ ///
+ /// The scheme itself is a fixed vocabulary word, not user data, so it is
+ /// safe to name; the entry name after it is not and is never included.
+ #[error("credential_ref scheme '{scheme}' is not supported (expected \"{KEYCHAIN_SCHEME}\")")]
+ UnsupportedScheme {
+ /// The offending scheme, lowercased.
+ scheme: String,
+ },
+
+ /// The scheme was valid but the name after it was empty.
+ #[error("credential_ref has an empty name after \"{KEYCHAIN_SCHEME}:\"")]
+ EmptyName,
+
+ /// Keychain access is gated behind a consent prompt that has not been
+ /// answered. Distinct from [`Self::Unavailable`] on purpose.
+ #[error("keychain access is pending user consent")]
+ ConsentPending,
+
+ /// No usable keychain backend on this host.
+ #[error("no keychain backend is available on this host")]
+ Unavailable,
+
+ /// The keychain has no entry under this reference.
+ #[error("no keychain entry matches this credential_ref")]
+ NotFound,
+
+ /// The keychain backend failed. Detail is logged, never rendered — see the
+ /// module docs on [`KeyringError`](keyring::KeyringError).
+ #[error("keychain lookup failed")]
+ Backend,
+}
+
+/// The two gates that run *before* the keychain is touched, as a pure
+/// function of what they observed.
+///
+/// Split out for the same reason `memory::binding::admit` is pure: the rule
+/// worth pinning is the **order** — a pending consent prompt must be reported
+/// as consent, not as an unavailable backend, and neither may be reported as a
+/// missing entry. That ordering is what stops an operator "fixing" an
+/// unanswered dialog by editing `config.toml`, and it is testable here without
+/// a keychain, a consent store, or a booted core.
+///
+/// Returns `None` when the caller may proceed to the lookup.
+#[must_use]
+pub fn preflight(decision: PolicyDecision, keychain_available: bool) -> Option<CredentialRefError> {
+ if decision != PolicyDecision::Proceed {
+ return Some(CredentialRefError::ConsentPending);
+ }
+ if !keychain_available {
+ return Some(CredentialRefError::Unavailable);
+ }
+ None
+}
+
+/// The scheme half of a parsed reference.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+#[non_exhaustive]
+pub enum CredentialRefScheme {
+ /// `keychain:<name>` — resolved through [`keyring`].
+ Keychain,
+}
+
+impl CredentialRefScheme {
+ /// The wire spelling, as it appears in `config.toml`.
+ #[must_use]
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::Keychain => KEYCHAIN_SCHEME,
+ }
+ }
+}
+
+/// A parsed `credential_ref`.
+///
+/// Deliberately **not** `derive(Debug)` — see the manual impl below and the
+/// module docs.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct CredentialRef {
+ scheme: CredentialRefScheme,
+ name: String,
+}
+
+// Manual `Debug` that redacts the name. Deriving it would put the credential
+// reference into every `format!("{ref:?}")`, `tracing::debug!(?r, ...)` and
+// panic message, which plan-memory.md §7 Tier-3 forbids. This mirrors
+// `MemoryDriverConfig`'s own manual redacting `Debug`. NEVER derive `Debug`.
+impl std::fmt::Debug for CredentialRef {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("CredentialRef")
+ .field("scheme", &self.scheme)
+ .field("name", &"<redacted>")
+ .finish()
+ }
+}
+
+impl CredentialRef {
+ /// Parse `"<scheme>:<name>"`.
+ ///
+ /// Surrounding whitespace is trimmed on both halves, so a hand-edited
+ /// `config.toml` with `credential_ref = "keychain: supermemory"` resolves
+ /// the same entry as the canonical spelling. The scheme is matched
+ /// case-insensitively; the name is **not** normalised, because it is a
+ /// keychain key and the backing store is case-sensitive.
+ ///
+ /// # Errors
+ ///
+ /// [`CredentialRefError::Empty`], [`CredentialRefError::MissingScheme`],
+ /// [`CredentialRefError::UnsupportedScheme`] or
+ /// [`CredentialRefError::EmptyName`]. None of them carry the name.
+ pub fn parse(raw: &str) -> Result<Self, CredentialRefError> {
+ let raw = raw.trim();
+ if raw.is_empty() {
+ return Err(CredentialRefError::Empty);
+ }
+
+ // `split_once` rather than `split(':')`: a name is free to contain a
+ // colon, and only the first one separates the scheme.
+ let Some((scheme_raw, name_raw)) = raw.split_once(':') else {
+ return Err(CredentialRefError::MissingScheme);
+ };
+
+ let scheme_norm = scheme_raw.trim().to_ascii_lowercase();
+ let scheme = if scheme_norm == KEYCHAIN_SCHEME {
+ CredentialRefScheme::Keychain
+ } else {
+ return Err(CredentialRefError::UnsupportedScheme {
+ scheme: scheme_norm,
+ });
+ };
+
+ let name = name_raw.trim();
+ if name.is_empty() {
+ return Err(CredentialRefError::EmptyName);
+ }
+
+ Ok(Self {
+ scheme,
+ name: name.to_string(),
+ })
+ }
+
+ /// The scheme this reference names.
+ #[must_use]
+ pub fn scheme(&self) -> CredentialRefScheme {
+ self.scheme
+ }
+
+ /// The entry name, without the scheme prefix.
+ ///
+ /// Callers must treat this as sensitive: it is the one field this type's
+ /// `Debug` deliberately hides, so do not interpolate it into a string that
+ /// can reach an operator (see the module docs).
+ #[must_use]
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ /// Resolve this reference to the secret it names, under `user_id`.
+ ///
+ /// `user_id` is the keychain partition the caller binds under; it is a
+ /// parameter rather than something derived here so this module stays free
+ /// of any one subsystem's notion of identity.
+ ///
+ /// The returned secret is wrapped in [`Zeroizing`] so it is wiped when the
+ /// caller drops it rather than lingering in freed memory.
+ ///
+ /// # Errors
+ ///
+ /// [`CredentialRefError::ConsentPending`], [`CredentialRefError::Unavailable`],
+ /// [`CredentialRefError::NotFound`] or [`CredentialRefError::Backend`], in
+ /// that order of checking. No variant carries the name or the secret.
+ pub fn resolve(&self, user_id: &str) -> Result<Zeroizing<String>, CredentialRefError> {
+ match self.scheme {
+ CredentialRefScheme::Keychain => self.resolve_keychain(user_id),
+ }
+ }
+
+ fn resolve_keychain(&self, user_id: &str) -> Result<Zeroizing<String>, CredentialRefError> {
+ if let Some(refusal) = preflight(policy::check_secret_access(), keyring::is_available()) {
+ log::debug!(
+ "{LOG_PREFIX} keychain access refused before lookup user_id={user_id} \
+ refusal={refusal} backend={}",
+ keyring::backend_name()
+ );
+ return Err(refusal);
+ }
+
+ match keyring::get(user_id, &self.name) {
+ Ok(Some(secret)) => {
+ log::debug!("{LOG_PREFIX} resolved credential_ref user_id={user_id}");
+ Ok(Zeroizing::new(secret))
+ }
+ Ok(None) => {
+ log::debug!("{LOG_PREFIX} no keychain entry for credential_ref user_id={user_id}");
+ Err(CredentialRefError::NotFound)
+ }
+ // Logged, not propagated: `KeyringError`'s Display interpolates the
+ // key, and this error's Display is allowed into operator-facing
+ // strings. See the module docs.
+ Err(e) => {
+ log::warn!("{LOG_PREFIX} keychain lookup failed user_id={user_id}: {e}");
+ Err(CredentialRefError::Backend)
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "credential_ref_tests.rs"]
+mod tests;
diff --git a/src/openhuman/security/credentials/credential_ref_tests.rs b/src/openhuman/security/credentials/credential_ref_tests.rs
new file mode 100644
index 0000000000..5141e33701
--- /dev/null
+++ b/src/openhuman/security/credentials/credential_ref_tests.rs
@@ -0,0 +1,196 @@
+//! Tests for `credential_ref` parsing, redaction, and the pre-lookup gates.
+
+use super::*;
+
+/// The name used everywhere below. Distinctive enough that a substring search
+/// for it cannot pass by accident.
+const SECRET_NAME: &str = "supermemory-prod-key-9f3a";
+
+#[test]
+fn parses_the_canonical_spelling() {
+ let r = CredentialRef::parse("keychain:supermemory").expect("canonical form parses");
+ assert_eq!(r.scheme(), CredentialRefScheme::Keychain);
+ assert_eq!(r.name(), "supermemory");
+}
+
+#[test]
+fn trims_whitespace_around_both_halves() {
+ // A hand-edited config.toml must resolve the same entry as the canonical
+ // spelling rather than looking up a name with a leading space.
+ for raw in [
+ " keychain:supermemory ",
+ "keychain: supermemory",
+ "keychain :supermemory",
+ "\tkeychain:\tsupermemory\n",
+ ] {
+ let r = CredentialRef::parse(raw).unwrap_or_else(|e| panic!("{raw:?} should parse: {e}"));
+ assert_eq!(r.name(), "supermemory", "for input {raw:?}");
+ assert_eq!(r.scheme(), CredentialRefScheme::Keychain);
+ }
+}
+
+#[test]
+fn scheme_is_case_insensitive_but_the_name_is_not() {
+ for raw in ["KEYCHAIN:Prod", "KeyChain:Prod", "keychain:Prod"] {
+ let r = CredentialRef::parse(raw).unwrap_or_else(|e| panic!("{raw:?} should parse: {e}"));
+ assert_eq!(r.scheme(), CredentialRefScheme::Keychain);
+ // The name is a keychain key and the backing store is case-sensitive,
+ // so normalising it would look up the wrong entry.
+ assert_eq!(r.name(), "Prod", "name must survive verbatim for {raw:?}");
+ }
+}
+
+#[test]
+fn only_the_first_colon_separates_the_scheme() {
+ // A name is free to contain a colon; splitting on every ':' would truncate
+ // it and silently resolve a different entry.
+ let r = CredentialRef::parse("keychain:ns:sub:key").expect("colons in the name are allowed");
+ assert_eq!(r.name(), "ns:sub:key");
+}
+
+#[test]
+fn rejects_malformed_references() {
+ use CredentialRefError::*;
+ let cases: &[(&str, CredentialRefError)] = &[
+ ("", Empty),
+ (" ", Empty),
+ ("supermemory", MissingScheme),
+ ("keychain:", EmptyName),
+ ("keychain: ", EmptyName),
+ (
+ "vault:supermemory",
+ UnsupportedScheme {
+ scheme: "vault".to_string(),
+ },
+ ),
+ (
+ "ENV:SUPERMEMORY",
+ UnsupportedScheme {
+ scheme: "env".to_string(),
+ },
+ ),
+ ];
+ for (raw, expected) in cases {
+ let err = CredentialRef::parse(raw).expect_err(&format!("{raw:?} must not parse"));
+ assert_eq!(&err, expected, "for input {raw:?}");
+ }
+}
+
+#[test]
+fn an_unsupported_scheme_is_named_but_the_entry_name_is_not() {
+ // The scheme is fixed vocabulary and is useful to report; everything after
+ // it is operator data and must not travel with the error.
+ let err = CredentialRef::parse(&format!("vault:{SECRET_NAME}")).expect_err("vault is unknown");
+ let rendered = err.to_string();
+ assert!(
+ rendered.contains("vault"),
+ "scheme should be named: {rendered}"
+ );
+ assert!(
+ !rendered.contains(SECRET_NAME),
+ "entry name leaked into the error: {rendered}"
+ );
+}
+
+#[test]
+fn debug_redacts_the_name() {
+ // Deriving Debug here would put the reference into every `{:?}` and panic
+ // message; plan-memory.md §7 Tier 3 forbids that.
+ let r = CredentialRef::parse(&format!("keychain:{SECRET_NAME}")).expect("parses");
+ let rendered = format!("{r:?}");
+ assert!(
+ !rendered.contains(SECRET_NAME),
+ "credential name leaked through Debug: {rendered}"
+ );
+ assert!(
+ rendered.contains("<redacted>"),
+ "redaction marker missing: {rendered}"
+ );
+ // The scheme is still visible — the point is to stay debuggable.
+ assert!(
+ rendered.contains("Keychain"),
+ "scheme should survive: {rendered}"
+ );
+}
+
+#[test]
+fn no_error_display_can_carry_a_name_or_a_secret() {
+ // This is the property `memory::binding`'s FallbackReason depends on: any
+ // of these may be rendered into `subsystems_status`, which is pinned by
+ // `fallback_reason_never_contains_credential_ref_or_endpoint`.
+ let all = [
+ CredentialRefError::Empty,
+ CredentialRefError::MissingScheme,
+ CredentialRefError::UnsupportedScheme {
+ scheme: "vault".to_string(),
+ },
+ CredentialRefError::EmptyName,
+ CredentialRefError::ConsentPending,
+ CredentialRefError::Unavailable,
+ CredentialRefError::NotFound,
+ CredentialRefError::Backend,
+ ];
+ for err in all {
+ let rendered = err.to_string();
+ assert!(
+ !rendered.contains(SECRET_NAME),
+ "{err:?} rendered a credential name: {rendered}"
+ );
+ assert!(!rendered.is_empty(), "{err:?} rendered an empty message");
+ }
+}
+
+#[test]
+fn preflight_reports_consent_before_availability() {
+ // Order is the whole point. With consent pending AND no backend, the
+ // operator must be told about consent — telling them the keychain is
+ // unavailable would send them to fix the wrong thing.
+ assert_eq!(
+ preflight(PolicyDecision::ConsentRequired, false),
+ Some(CredentialRefError::ConsentPending)
+ );
+ assert_eq!(
+ preflight(PolicyDecision::Declined, false),
+ Some(CredentialRefError::ConsentPending)
+ );
+}
+
+#[test]
+fn preflight_reports_unavailability_only_once_consent_is_granted() {
+ assert_eq!(
+ preflight(PolicyDecision::Proceed, false),
+ Some(CredentialRefError::Unavailable)
+ );
+}
+
+#[test]
+fn preflight_allows_the_lookup_when_both_gates_pass() {
+ assert_eq!(preflight(PolicyDecision::Proceed, true), None);
+}
+
+#[test]
+fn preflight_never_reports_a_missing_entry() {
+ // NotFound is a fact about the keychain's contents and can only be learned
+ // by asking it. A gate that guessed it would report "not configured" for a
+ // host that simply has no backend.
+ for decision in [
+ PolicyDecision::Proceed,
+ PolicyDecision::ConsentRequired,
+ PolicyDecision::Declined,
+ ] {
+ for available in [true, false] {
+ assert_ne!(
+ preflight(decision, available),
+ Some(CredentialRefError::NotFound),
+ "preflight invented a NotFound for {decision:?}/available={available}"
+ );
+ }
+ }
+}
+
+#[test]
+fn scheme_round_trips_through_its_wire_spelling() {
+ assert_eq!(CredentialRefScheme::Keychain.as_str(), KEYCHAIN_SCHEME);
+ let r = CredentialRef::parse(&format!("{KEYCHAIN_SCHEME}:x")).expect("parses");
+ assert_eq!(r.scheme().as_str(), KEYCHAIN_SCHEME);
+}
diff --git a/src/openhuman/security/credentials/mod.rs b/src/openhuman/security/credentials/mod.rs
index 4b507cc51e..c206c35f4e 100644
--- a/src/openhuman/security/credentials/mod.rs
+++ b/src/openhuman/security/credentials/mod.rs
@@ -3,6 +3,7 @@
pub mod bus;
pub mod cli;
mod core;
+pub mod credential_ref;
pub mod http_creds;
pub mod ops;
pub mod profiles;
@@ -17,6 +18,7 @@ pub use crate::api::rest::{
BackendOAuthClient, ConnectResponse, IntegrationSummary, IntegrationTokensHandoff,
};
pub use core::*;
+pub use credential_ref::{CredentialRef, CredentialRefError, CredentialRefScheme};
pub use http_creds::{
HttpCredential, HttpCredentialScheme, HttpCredentialSummary, HttpCredentialsStore,
};