mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
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:
@@ -1809,19 +1809,17 @@ fn auth_config_from_preserves_all_fields() {
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn try_shared_from_config_rejects_partial_workload_identity_configuration() {
|
||||
async fn shared_from_config_prefers_workload_identity_to_explicit_access_token() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let config = test_auth_manager_config(codex_home.path());
|
||||
let _access_token_guard = remove_access_token_env_var();
|
||||
let _access_token_guard = EnvVarGuard::set(CODEX_ACCESS_TOKEN_ENV_VAR, "at-explicit");
|
||||
let _rule_guard = EnvVarGuard::set(OPENAI_FEDERATION_RULE_ID_ENV_VAR, "rule-one");
|
||||
let _assertion_file_guard = EnvVarGuard::remove(OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR);
|
||||
|
||||
let error =
|
||||
AuthManager::try_shared_from_config(&config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect_err("partial workload identity config should fail closed");
|
||||
let error = AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect_err("partial workload identity config should fail closed");
|
||||
|
||||
assert!(matches!(error, RefreshTokenError::Permanent(_)));
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
@@ -2148,7 +2146,9 @@ async fn auth_manager_rejects_disallowed_stored_and_external_auth() {
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_login_methods = Some(vec![ForcedLoginMethod::Chatgpt]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect("auth manager");
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
assert!(
|
||||
@@ -2177,7 +2177,9 @@ async fn api_only_policy_rejects_access_tokens_before_hydration() {
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_login_methods = Some(vec![ForcedLoginMethod::Api]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect("auth manager");
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
assert!(
|
||||
@@ -2210,7 +2212,9 @@ async fn workspace_policy_rejects_agent_identity_before_hydration() {
|
||||
config.managed_auth_policy.allowed_chatgpt_workspaces =
|
||||
Some(vec![WORKSPACE_ID_ALLOWED.to_string()]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect("auth manager");
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
drop(access_token_guard);
|
||||
@@ -2243,7 +2247,9 @@ async fn workspace_policy_rejects_agent_identity_before_hydration() {
|
||||
config.managed_auth_policy.allowed_chatgpt_workspaces =
|
||||
Some(vec![WORKSPACE_ID_ALLOWED.to_string()]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect("auth manager");
|
||||
assert_eq!(manager.auth().await, None);
|
||||
}
|
||||
|
||||
@@ -2279,7 +2285,9 @@ async fn workspace_policy_checks_the_selected_request_account() {
|
||||
)
|
||||
.await;
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false)
|
||||
.await
|
||||
.expect("auth manager");
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -26,8 +26,6 @@ use super::ExternalAuthRefreshContext;
|
||||
use super::RefreshTokenError;
|
||||
use super::RefreshTokenFailedError;
|
||||
use super::RefreshTokenFailedReason;
|
||||
use super::read_codex_access_token_from_env;
|
||||
use super::read_codex_api_key_from_env;
|
||||
use crate::AuthRouteConfig;
|
||||
|
||||
const PROD_TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
|
||||
@@ -97,22 +95,21 @@ pub(super) enum WorkloadIdentitySessionError {
|
||||
InvalidConfiguration(String),
|
||||
}
|
||||
|
||||
pub fn is_workload_identity_selected(
|
||||
auth_config: &AuthConfig,
|
||||
enable_codex_api_key_env: bool,
|
||||
) -> bool {
|
||||
!has_explicit_process_auth(auth_config, enable_codex_api_key_env)
|
||||
&& ProcessEnvironment::read().has_marker()
|
||||
/// Returns whether workload identity was selected through process configuration.
|
||||
///
|
||||
/// Either marker selects workload identity. Partial configuration then fails validation rather
|
||||
/// than falling back to another credential source.
|
||||
pub fn is_workload_identity_selected() -> bool {
|
||||
ProcessEnvironment::read().has_marker()
|
||||
}
|
||||
|
||||
fn resolve_config(
|
||||
chatgpt_base_url: &str,
|
||||
environment: ProcessEnvironment,
|
||||
has_explicit_process_auth: bool,
|
||||
chatgpt_login_allowed: bool,
|
||||
auth_route_config: AuthRouteConfig,
|
||||
) -> Result<Option<WorkloadIdentitySessionConfig>, WorkloadIdentitySessionError> {
|
||||
if has_explicit_process_auth || !environment.has_marker() {
|
||||
if !environment.has_marker() {
|
||||
return Ok(None);
|
||||
}
|
||||
if !chatgpt_login_allowed {
|
||||
@@ -140,14 +137,6 @@ fn resolve_config(
|
||||
}))
|
||||
}
|
||||
|
||||
fn has_explicit_process_auth(auth_config: &AuthConfig, enable_codex_api_key_env: bool) -> bool {
|
||||
(enable_codex_api_key_env
|
||||
&& auth_config.is_login_method_allowed(ForcedLoginMethod::Api)
|
||||
&& read_codex_api_key_from_env().is_some())
|
||||
|| (auth_config.is_login_method_allowed(ForcedLoginMethod::Chatgpt)
|
||||
&& read_codex_access_token_from_env().is_some())
|
||||
}
|
||||
|
||||
fn required_path(
|
||||
value: Option<OsString>,
|
||||
variable: &'static str,
|
||||
@@ -296,17 +285,6 @@ struct WorkloadIdentitySessionEntry {
|
||||
}
|
||||
|
||||
impl WorkloadIdentitySessionRegistry {
|
||||
fn has_active_session(&self) -> Result<bool, WorkloadIdentitySessionError> {
|
||||
let entry = self
|
||||
.entry
|
||||
.lock()
|
||||
.map_err(|_| WorkloadIdentitySessionError::RegistryUnavailable)?;
|
||||
Ok(entry
|
||||
.as_ref()
|
||||
.and_then(|active| active.session.upgrade())
|
||||
.is_some())
|
||||
}
|
||||
|
||||
fn session(
|
||||
&self,
|
||||
config: WorkloadIdentitySessionConfig,
|
||||
@@ -347,17 +325,14 @@ pub(super) struct WorkloadIdentityExternalAuth {
|
||||
impl WorkloadIdentityExternalAuth {
|
||||
pub(super) fn from_process_config(
|
||||
auth_config: &AuthConfig,
|
||||
enable_codex_api_key_env: bool,
|
||||
) -> Result<Option<Self>, WorkloadIdentitySessionError> {
|
||||
let registry = process_registry();
|
||||
let active_session = registry.has_active_session()?;
|
||||
resolve_config(
|
||||
auth_config
|
||||
.chatgpt_base_url
|
||||
.as_deref()
|
||||
.unwrap_or("https://chatgpt.com/backend-api"),
|
||||
ProcessEnvironment::read(),
|
||||
has_explicit_process_auth(auth_config, enable_codex_api_key_env) && !active_session,
|
||||
auth_config.is_login_method_allowed(ForcedLoginMethod::Chatgpt),
|
||||
auth_config.auth_route_config.clone(),
|
||||
)?
|
||||
|
||||
@@ -26,14 +26,12 @@ fn complete_environment() -> ProcessEnvironment {
|
||||
|
||||
fn resolve_for_test(
|
||||
environment: ProcessEnvironment,
|
||||
has_explicit_process_auth: bool,
|
||||
chatgpt_login_allowed: bool,
|
||||
chatgpt_base_url: &str,
|
||||
) -> Result<Option<WorkloadIdentitySessionConfig>, WorkloadIdentitySessionError> {
|
||||
resolve_config(
|
||||
chatgpt_base_url,
|
||||
environment,
|
||||
has_explicit_process_auth,
|
||||
chatgpt_login_allowed,
|
||||
auth_route_config(OutboundProxyPolicy::ReqwestDefault),
|
||||
)
|
||||
@@ -44,27 +42,12 @@ fn markers_select_wif_and_partial_configuration_fails_closed() {
|
||||
assert!(
|
||||
resolve_for_test(
|
||||
ProcessEnvironment::default(),
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
.expect("no markers")
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
resolve_for_test(
|
||||
ProcessEnvironment {
|
||||
federation_rule_id: Some("partial".into()),
|
||||
..Default::default()
|
||||
},
|
||||
/*has_explicit_process_auth*/ true,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
.expect("explicit auth wins")
|
||||
.is_none()
|
||||
);
|
||||
|
||||
for (environment, missing) in [
|
||||
(
|
||||
ProcessEnvironment {
|
||||
@@ -83,7 +66,6 @@ fn markers_select_wif_and_partial_configuration_fails_closed() {
|
||||
] {
|
||||
let error = resolve_for_test(
|
||||
environment,
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
@@ -98,7 +80,6 @@ fn markers_select_wif_and_partial_configuration_fails_closed() {
|
||||
assert!(
|
||||
resolve_for_test(
|
||||
relative,
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
@@ -112,7 +93,6 @@ fn markers_select_wif_and_partial_configuration_fails_closed() {
|
||||
fn auth_policy_and_app_environment_are_enforced() {
|
||||
let policy_error = resolve_for_test(
|
||||
complete_environment(),
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ false,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
@@ -133,7 +113,6 @@ fn auth_policy_and_app_environment_are_enforced() {
|
||||
] {
|
||||
let config = resolve_for_test(
|
||||
complete_environment(),
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
chatgpt_base_url,
|
||||
)
|
||||
@@ -145,7 +124,6 @@ fn auth_policy_and_app_environment_are_enforced() {
|
||||
|
||||
let error = resolve_for_test(
|
||||
complete_environment(),
|
||||
/*has_explicit_process_auth*/ false,
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://example.invalid/backend-api",
|
||||
)
|
||||
|
||||
@@ -33,6 +33,7 @@ pub use auth::AuthHeaders;
|
||||
pub use auth::AuthKeyringBackendKind;
|
||||
pub use auth::AuthManager;
|
||||
pub use auth::AuthManagerConfig;
|
||||
pub use auth::AuthManagerInitializationError;
|
||||
pub use auth::CLIENT_ID;
|
||||
pub use auth::CLIENT_ID_OVERRIDE_ENV_VAR;
|
||||
pub use auth::CODEX_ACCESS_TOKEN_ENV_VAR;
|
||||
|
||||
Reference in New Issue
Block a user