Files
codex/codex-rs/model-provider/src/gateway_export.rs
T
alexsong-oai 6eb51a438a Retry gateway credential exports after unauthorized responses (#59816)
## Why

Cached gateway command credentials need to be refreshed when the gateway rejects them with a `401`.

## What changed

Clear the cached `credential_export` token after an inference HTTP `401` or a WebSocket upgrade rejected with `401`, then rerun the command for one retry. Preserve subsequent primary authentication recovery if the retry is also unauthorized, and document the behavior.

## Testing

Add integration coverage for using a new exported token, reporting an invalid export, recovering primary authentication after the gateway retry, and stopping after a second unauthorized WebSocket upgrade.

GitOrigin-RevId: 3397476dbfd3bec1bd44f90657c4f9e17bb7df67
2026-11-06 03:51:12 +00:00

69 lines
2.2 KiB
Rust

//! Resolves and caches the configured gateway command's credential for this provider.
use std::fmt;
use std::sync::Arc;
use codex_http_client::NetworkPolicy;
use codex_model_provider_info::CredentialExportConfig;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result;
use tokio::sync::Mutex;
pub(crate) struct GatewayExport {
command: CredentialExportConfig,
policy: NetworkPolicy,
cached: Mutex<Option<Arc<str>>>,
}
impl fmt::Debug for GatewayExport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GatewayExport").finish_non_exhaustive()
}
}
impl GatewayExport {
pub(crate) fn new(command: CredentialExportConfig, policy: NetworkPolicy) -> Self {
Self {
command,
policy,
cached: Mutex::default(),
}
}
pub(crate) async fn invalidate(&self) {
*self.cached.lock().await = None;
}
#[expect(
clippy::await_holding_invalid_type,
reason = "concurrent cache misses must share a single credential export"
)]
pub(crate) async fn token(&self) -> Result<Arc<str>> {
let mut cached = self.cached.lock().await;
if let Some(token) = cached.as_ref() {
return Ok(Arc::clone(token));
}
let policy = self.policy.clone().for_current_account();
let permit = policy
.acquire_for_unsupported_sdk()
.map_err(|error| CodexErr::Fatal(error.to_string()))?;
let output = permit
.run(codex_login::run_credential_export(&self.command, "Gateway"))
.await
.map_err(|error| CodexErr::Fatal(error.to_string()))?
.map_err(|error| CodexErr::InvalidRequest(error.to_string()))?;
let token = std::str::from_utf8(&output)
.ok()
.map(str::trim)
.filter(|value| !value.is_empty() && value.bytes().all(|b| b.is_ascii_graphic()))
.ok_or_else(|| {
CodexErr::InvalidRequest(
"Gateway credential export must print one access token".into(),
)
})?;
let token = Arc::<str>::from(token);
*cached = Some(Arc::clone(&token));
Ok(token)
}
}