//! Caches Bedrock signing credentials and provides an exclusive refresh guard. //! The caller holds the guard across login and export to coalesce auth recovery. //! Exported secrets remain in memory; command output is bounded and never logged. use std::io; use std::sync::atomic::AtomicU64; use std::sync::atomic::Ordering; use std::time::Duration; use std::time::SystemTime; use chrono::DateTime; use chrono::Utc; use codex_aws_auth::AwsAccessKeys; use codex_aws_auth::AwsCredentialsProvider; use codex_model_provider_info::AwsCredentialExportConfig; use serde::Deserialize; use tokio::sync::Mutex; use tokio::sync::MutexGuard; const DEFAULT_CREDENTIAL_LIFETIME: Duration = Duration::from_secs(60 * 60); const CREDENTIAL_REFRESH_WINDOW: Duration = Duration::from_secs(5 * 60); #[derive(Debug)] pub(crate) struct AwsCredentialExport { config: AwsCredentialExportConfig, cached: Mutex>, recovery_generation: AtomicU64, } #[derive(Debug)] struct CachedCredentials { access_keys: AwsAccessKeys, refresh_at: SystemTime, } /// Keeps cached credentials invalidated while the caller reauthenticates and exports. pub(super) struct CredentialExportRefresh<'a> { exporter: &'a AwsCredentialExport, cached: MutexGuard<'a, Option>, } impl CredentialExportRefresh<'_> { pub(super) async fn refresh(mut self) -> io::Result<()> { *self.cached = Some(self.exporter.export_credentials().await?); self.exporter .recovery_generation .fetch_add(1, Ordering::Release); Ok(()) } } #[derive(Deserialize)] #[serde(untagged)] enum CredentialOutput { Sts { #[serde(rename = "Credentials")] credentials: ExportedCredentials, }, Process(ExportedCredentials), } #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct ExportedCredentials { access_key_id: String, secret_access_key: String, session_token: Option, expiration: Option, } impl CachedCredentials { fn from_output(output: &[u8], now: SystemTime) -> io::Result { let output: CredentialOutput = serde_json::from_slice(output).map_err(|_| { // Serde's detailed errors may quote credential values. io::Error::new( io::ErrorKind::InvalidData, "AWS credential export returned invalid credentials JSON", ) })?; let credentials = match output { CredentialOutput::Sts { credentials } | CredentialOutput::Process(credentials) => { credentials } }; if credentials.access_key_id.trim().is_empty() || credentials.secret_access_key.trim().is_empty() { return Err(io::Error::new( io::ErrorKind::InvalidData, "AWS credential export returned empty access keys", )); } let refresh_at = if let Some(expiration) = credentials.expiration { let expiration = DateTime::parse_from_rfc3339(&expiration).map_err(|_| { io::Error::new( io::ErrorKind::InvalidData, "AWS credential export Expiration must be an RFC 3339 timestamp", ) })?; let lifetime = expiration .signed_duration_since(DateTime::::from(now)) .to_std() .ok() .filter(|lifetime| !lifetime.is_zero()) .ok_or_else(|| { io::Error::other("AWS credential export returned expired credentials") })?; now.checked_add(lifetime.saturating_sub(CREDENTIAL_REFRESH_WINDOW)) .ok_or_else(|| { io::Error::new( io::ErrorKind::InvalidData, "AWS credential export Expiration is out of range", ) })? } else { now + DEFAULT_CREDENTIAL_LIFETIME }; Ok(Self { access_keys: AwsAccessKeys { access_key_id: credentials.access_key_id, secret_access_key: credentials.secret_access_key, session_token: credentials.session_token, }, refresh_at, }) } } impl AwsCredentialExport { pub(crate) fn new(config: AwsCredentialExportConfig) -> Self { Self { config, cached: Mutex::new(None), recovery_generation: AtomicU64::new(0), } } /// Returns None when another overlapping recovery has already completed. /// Dropping the guard before refresh succeeds leaves the cache empty. pub(super) async fn begin_refresh(&self) -> Option> { let generation = self.recovery_generation.load(Ordering::Acquire); let mut cached = self.cached.lock().await; if self.recovery_generation.load(Ordering::Acquire) != generation { return None; } *cached = None; Some(CredentialExportRefresh { exporter: self, cached, }) } async fn export_credentials(&self) -> io::Result { let output = codex_login::run_credential_export(&self.config, "AWS").await?; CachedCredentials::from_output(&output, SystemTime::now()) } } impl AwsCredentialsProvider for AwsCredentialExport { #[expect( clippy::await_holding_invalid_type, reason = "cache misses serialize command execution to avoid duplicate credential exports" )] async fn credentials(&self) -> std::io::Result { let mut cached = self.cached.lock().await; if let Some(credentials) = cached.as_ref() && SystemTime::now() < credentials.refresh_at { return Ok(credentials.access_keys.clone()); } let credentials = self.export_credentials().await?; let access_keys = credentials.access_keys.clone(); *cached = Some(credentials); Ok(access_keys) } } #[cfg(test)] #[path = "credential_export_tests.rs"] mod tests;