mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
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
This commit is contained in:
@@ -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<bool, WorkloadIdentityError> {
|
||||
if !url.username().is_empty()
|
||||
|| url.password().is_some()
|
||||
|
||||
@@ -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!(
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user