Fail closed when workload identity initialization fails (#38424)

## What changed

- Treat workload identity environment markers as an explicit authentication selection, even when another process credential is present.
- Return initialization errors from `AuthManager` and propagate them through commands and services instead of continuing with an unusable authentication state.
- Make `codex login status` validate workload identity, keep the TUI on an embedded app server for local workload identity, and reject workload identity in `codex mcp-server`, where it is unsupported.

## Testing

- Cover workload identity precedence and partial configuration errors.
- Verify login status reports an unreadable identity assertion and app-server routing enforces the supported workload identity topology.

GitOrigin-RevId: efc6b6b4cd4d61652617de82aaa3d7ffc75d6618
This commit is contained in:
cooper-oai authored and copyberry committed 2026-08-13 19:35:22 +00:00
1 parent 781445f7c6
commit 990218bbbd
33 files changed
+368 -212

No files matched your search

+36 -32
View File
@@ -206,6 +206,37 @@ pub enum RefreshTokenError {
Transient(#[from] std::io::Error),
}
/// Error returned when constructing an [`AuthManager`] from resolved configuration.
#[derive(Debug, Error)]
#[error(transparent)]
pub struct AuthManagerInitializationError(AuthManagerInitializationErrorSource);
#[derive(Debug, Error)]
enum AuthManagerInitializationErrorSource {
#[error(transparent)]
WorkloadIdentityConfiguration(WorkloadIdentitySessionError),
#[error(transparent)]
InitialAuth(RefreshTokenError),
}
impl From<WorkloadIdentitySessionError> for AuthManagerInitializationError {
fn from(error: WorkloadIdentitySessionError) -> Self {
Self(AuthManagerInitializationErrorSource::WorkloadIdentityConfiguration(error))
}
}
impl From<RefreshTokenError> for AuthManagerInitializationError {
fn from(error: RefreshTokenError) -> Self {
Self(AuthManagerInitializationErrorSource::InitialAuth(error))
}
}
impl From<AuthManagerInitializationError> for std::io::Error {
fn from(error: AuthManagerInitializationError) -> Self {
Self::other(error)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExternalAuthRefreshReason {
Unauthorized,
@@ -2615,40 +2646,20 @@ impl AuthManager {
)
}
/// Convenience constructor returning an `Arc` wrapper from resolved config.
/// Builds a shared manager and activates process-configured workload identity when selected.
pub async fn shared_from_config(
config: &impl AuthManagerConfig,
enable_codex_api_key_env: bool,
) -> Arc<Self> {
) -> Result<Arc<Self>, AuthManagerInitializationError> {
Self::shared_from_auth_config(auth_config_from(config), enable_codex_api_key_env).await
}
/// Builds a shared manager using restrictions resolved before authentication.
/// Activates workload identity against an auth config resolved before full runtime config.
pub async fn shared_from_auth_config(
auth_config: AuthConfig,
enable_codex_api_key_env: bool,
) -> Arc<Self> {
Arc::new(Self::new_from_auth_config(auth_config, enable_codex_api_key_env).await)
}
/// Constructs a manager and activates process-configured workload identity when selected.
pub async fn try_shared_from_config(
config: &impl AuthManagerConfig,
enable_codex_api_key_env: bool,
) -> Result<Arc<Self>, RefreshTokenError> {
Self::try_shared_from_auth_config(auth_config_from(config), enable_codex_api_key_env).await
}
/// Activates workload identity against an auth config resolved before full runtime config.
pub async fn try_shared_from_auth_config(
auth_config: AuthConfig,
enable_codex_api_key_env: bool,
) -> Result<Arc<Self>, RefreshTokenError> {
let external_auth = WorkloadIdentityExternalAuth::from_process_config(
&auth_config,
enable_codex_api_key_env,
)
.map_err(configured_workload_identity_error)?;
) -> Result<Arc<Self>, AuthManagerInitializationError> {
let external_auth = WorkloadIdentityExternalAuth::from_process_config(&auth_config)?;
let mut manager = Self::new_from_auth_config(auth_config, enable_codex_api_key_env).await;
manager.workload_identity_selected = external_auth.is_some();
let manager = Arc::new(manager);
@@ -2967,13 +2978,6 @@ fn auth_config_from(config: &impl AuthManagerConfig) -> AuthConfig {
}
}
fn configured_workload_identity_error(error: WorkloadIdentitySessionError) -> RefreshTokenError {
RefreshTokenError::Permanent(RefreshTokenFailedError::new(
RefreshTokenFailedReason::Other,
error.to_string(),
))
}
#[cfg(test)]
#[path = "auth_tests.rs"]
mod tests;