From 3fef28219c1f7e2f860b408088ca2da21fde96b8 Mon Sep 17 00:00:00 2001 From: Alvin Date: Tue, 1 Dec 2026 23:46:11 +0000 Subject: [PATCH] Surface TLS failures during workload identity sign-in (#65145) ## Why The generic token exchange unavailable error gives users no indication that they should check their trusted CA certificates when a TLS failure occurs. ## What changed Report TLS failures during token exchange with guidance to check the trusted CA certificates on the host or container. Keep these failures transient and continue reporting other request failures as exchange unavailable. ## Testing Add a regression test that checks the certificate guidance for an invalid custom CA file and verifies that both TLS failures and timeouts remain transient, with timeouts still reported as exchange unavailable. GitOrigin-RevId: b4eb9eb9eb86d4703b9b96b3e339d2764bc5644f --- codex-rs/workload-identity/src/exchange.rs | 12 +++++++- codex-rs/workload-identity/src/lib.rs | 5 ++++ .../src/workload_identity_tests.rs | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/codex-rs/workload-identity/src/exchange.rs b/codex-rs/workload-identity/src/exchange.rs index 71802a332c..09928446a6 100644 --- a/codex-rs/workload-identity/src/exchange.rs +++ b/codex-rs/workload-identity/src/exchange.rs @@ -8,6 +8,8 @@ use codex_http_client::ClientRouteClass; use codex_http_client::HttpClient; use codex_http_client::HttpClientBuilder; use codex_http_client::HttpClientFactory; +use codex_http_client::HttpError; +use codex_http_client::RouteFailureClass; use serde::Deserialize; use tokio::sync::Mutex; use url::Host; @@ -194,7 +196,7 @@ impl WorkloadIdentityExchange { .timeout(REQUEST_TIMEOUT) .send() .await - .map_err(|_| WorkloadIdentityError::ExchangeUnavailable)?; + .map_err(classify_exchange_request_error)?; if !response.status().is_success() { return Err(WorkloadIdentityError::ExchangeRejected( response.status().as_u16(), @@ -227,6 +229,14 @@ impl WorkloadIdentityExchange { } } +fn classify_exchange_request_error(error: HttpError) -> WorkloadIdentityError { + if error.failure_class() == Some(RouteFailureClass::TlsError) { + WorkloadIdentityError::ExchangeTlsFailure + } else { + WorkloadIdentityError::ExchangeUnavailable + } +} + fn validate_token_url(url: &Url) -> Result { if !url.username().is_empty() || url.password().is_some() diff --git a/codex-rs/workload-identity/src/lib.rs b/codex-rs/workload-identity/src/lib.rs index ce4311f3b9..f3e1c4c3d2 100644 --- a/codex-rs/workload-identity/src/lib.rs +++ b/codex-rs/workload-identity/src/lib.rs @@ -96,6 +96,10 @@ pub enum WorkloadIdentityError { InvalidTokenUrl, #[error("the workload identity token exchange is unavailable")] ExchangeUnavailable, + #[error( + "Could not establish a secure connection for workload identity sign-in. Check the trusted CA certificates on this host or container." + )] + ExchangeTlsFailure, #[error("the workload identity token exchange was rejected with HTTP {0}")] ExchangeRejected(u16), #[error("the workload identity token exchange returned an invalid response")] @@ -114,6 +118,7 @@ impl WorkloadIdentityError { | std::io::ErrorKind::WouldBlock ), Self::ExchangeUnavailable + | Self::ExchangeTlsFailure | Self::SpiffeUnavailable | Self::ExchangeRejected(408 | 429 | 500..=599) => true, Self::SpiffeRejected(code) => matches!( diff --git a/codex-rs/workload-identity/src/workload_identity_tests.rs b/codex-rs/workload-identity/src/workload_identity_tests.rs index a5814c4d7d..efa8720818 100644 --- a/codex-rs/workload-identity/src/workload_identity_tests.rs +++ b/codex-rs/workload-identity/src/workload_identity_tests.rs @@ -4,8 +4,11 @@ use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::time::Duration; +use codex_http_client::BuildCustomCaTransportError; use codex_http_client::HttpClientFactory; +use codex_http_client::HttpError; use codex_http_client::OutboundProxyPolicy; +use codex_http_client::RouteAwareClientPoolError; use pretty_assertions::assert_eq; use tempfile::TempDir; use url::Url; @@ -20,6 +23,7 @@ use super::ACCESS_TOKEN_TYPE; use super::JWT_BEARER_GRANT_TYPE; use super::WorkloadIdentityExchange; use super::WorkloadIdentityToken; +use super::classify_exchange_request_error; use crate::ApiTokenMetadata; use crate::AssertionSource; use crate::ChatgptTokenMetadata; @@ -27,6 +31,32 @@ use crate::WorkloadIdentityConfig; use crate::WorkloadIdentityError; use crate::WorkloadIdentityMetadata; +#[test] +fn exchange_tls_failure_gives_certificate_guidance_and_remains_transient() { + let failure = BuildCustomCaTransportError::InvalidCaFile { + source_env: "CODEX_CA_CERTIFICATE", + path: PathBuf::from("test-ca.pem"), + detail: "invalid PEM".to_string(), + }; + let error = classify_exchange_request_error(HttpError::Route( + RouteAwareClientPoolError::Build(failure.into()), + )); + + assert!(matches!(error, WorkloadIdentityError::ExchangeTlsFailure)); + assert_eq!( + error.to_string(), + "Could not establish a secure connection for workload identity sign-in. Check the trusted CA certificates on this host or container." + ); + assert!(error.is_transient()); + + let timeout = classify_exchange_request_error(HttpError::Timeout); + assert!(matches!( + timeout, + WorkloadIdentityError::ExchangeUnavailable + )); + assert!(timeout.is_transient()); +} + fn assertion_file(assertion: &str) -> (TempDir, PathBuf) { let temp_dir = TempDir::new().expect("tempdir"); let path = temp_dir.path().join("identity-token");