mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Forward workload identity context during token exchange (#38767)
## What changed - Read optional workload identity context from `OPENAI_WORKLOAD_IDENTITY_CONTEXT` and forward it unchanged as the `workload_identity_context` token exchange field. - Treat the context as sensitive by redacting it from session configuration debug output and removing it from model-reachable child environments. - Include the context in workload identity session fingerprints so sessions with different values cannot share an exchange. ## Testing - Cover request forwarding, debug redaction, session compatibility, and child-environment scrubbing. GitOrigin-RevId: fb50700478cf54d9d604944a4ed3e77acc928a0f
This commit is contained in:
1 parent
a7edf37cb4
commit
12933b6955
7 files changed
+188
-17
No files matched your search
@@ -11,6 +11,7 @@ use codex_http_client::HttpClientFactory;
|
||||
use codex_protocol::config_types::ForcedLoginMethod;
|
||||
use codex_protocol::shell_environment::OPENAI_FEDERATION_RULE_ID_ENV_VAR;
|
||||
use codex_protocol::shell_environment::OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR;
|
||||
use codex_protocol::shell_environment::OPENAI_WORKLOAD_IDENTITY_CONTEXT_ENV_VAR;
|
||||
use codex_workload_identity::WorkloadIdentityConfig;
|
||||
use codex_workload_identity::WorkloadIdentityError;
|
||||
use codex_workload_identity::WorkloadIdentityExchange;
|
||||
@@ -47,13 +48,34 @@ impl WorkloadIdentityEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone)]
|
||||
struct WorkloadIdentitySessionConfig {
|
||||
assertion_file: PathBuf,
|
||||
environment: WorkloadIdentityEnvironment,
|
||||
federation_rule_id: String,
|
||||
http_client_factory: HttpClientFactory,
|
||||
token_url: Url,
|
||||
workload_identity_context: Option<String>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for WorkloadIdentitySessionConfig {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("WorkloadIdentitySessionConfig")
|
||||
.field("assertion_file", &self.assertion_file)
|
||||
.field("environment", &self.environment)
|
||||
.field("federation_rule_id", &self.federation_rule_id)
|
||||
.field("http_client_factory", &self.http_client_factory)
|
||||
.field("token_url", &self.token_url)
|
||||
.field(
|
||||
"workload_identity_context",
|
||||
&self
|
||||
.workload_identity_context
|
||||
.as_ref()
|
||||
.map(|_| "[redacted]"),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl WorkloadIdentitySessionConfig {
|
||||
@@ -63,15 +85,17 @@ impl WorkloadIdentitySessionConfig {
|
||||
environment: self.environment,
|
||||
federation_rule_id: self.federation_rule_id.trim().to_string(),
|
||||
token_url: self.token_url.to_string(),
|
||||
workload_identity_context: self.workload_identity_context.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn into_exchange(self) -> Result<WorkloadIdentityExchange, WorkloadIdentityError> {
|
||||
WorkloadIdentityExchange::new(
|
||||
WorkloadIdentityConfig::new(self.federation_rule_id, self.assertion_file)?,
|
||||
self.token_url,
|
||||
self.http_client_factory,
|
||||
)
|
||||
let config = WorkloadIdentityConfig::new(
|
||||
self.federation_rule_id,
|
||||
self.assertion_file,
|
||||
self.workload_identity_context,
|
||||
)?;
|
||||
WorkloadIdentityExchange::new(config, self.token_url, self.http_client_factory)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +105,7 @@ struct WorkloadIdentityFingerprint {
|
||||
environment: WorkloadIdentityEnvironment,
|
||||
federation_rule_id: String,
|
||||
token_url: String,
|
||||
workload_identity_context: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -126,6 +151,10 @@ fn resolve_config(
|
||||
environment.identity_token_file,
|
||||
OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR,
|
||||
)?;
|
||||
let workload_identity_context = optional_unicode(
|
||||
environment.workload_identity_context,
|
||||
OPENAI_WORKLOAD_IDENTITY_CONTEXT_ENV_VAR,
|
||||
)?;
|
||||
let auth_environment = classify_auth_environment(chatgpt_base_url)?;
|
||||
|
||||
Ok(Some(WorkloadIdentitySessionConfig {
|
||||
@@ -134,6 +163,7 @@ fn resolve_config(
|
||||
federation_rule_id,
|
||||
http_client_factory: auth_route_config.http_client_factory().clone(),
|
||||
token_url: auth_environment.token_url()?,
|
||||
workload_identity_context,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -169,6 +199,19 @@ fn required_unicode(
|
||||
Ok(value.to_string())
|
||||
}
|
||||
|
||||
fn optional_unicode(
|
||||
value: Option<OsString>,
|
||||
variable: &'static str,
|
||||
) -> Result<Option<String>, WorkloadIdentitySessionError> {
|
||||
value
|
||||
.map(|value| {
|
||||
value.into_string().map_err(|_| {
|
||||
invalid_config(format!("workload identity variable {variable} is invalid"))
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
fn classify_auth_environment(
|
||||
base_url: &str,
|
||||
) -> Result<WorkloadIdentityEnvironment, WorkloadIdentitySessionError> {
|
||||
@@ -203,6 +246,7 @@ fn invalid_config(message: impl Into<String>) -> WorkloadIdentitySessionError {
|
||||
struct ProcessEnvironment {
|
||||
federation_rule_id: Option<OsString>,
|
||||
identity_token_file: Option<OsString>,
|
||||
workload_identity_context: Option<OsString>,
|
||||
}
|
||||
|
||||
impl ProcessEnvironment {
|
||||
@@ -210,6 +254,7 @@ impl ProcessEnvironment {
|
||||
Self {
|
||||
federation_rule_id: std::env::var_os(OPENAI_FEDERATION_RULE_ID_ENV_VAR),
|
||||
identity_token_file: std::env::var_os(OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR),
|
||||
workload_identity_context: std::env::var_os(OPENAI_WORKLOAD_IDENTITY_CONTEXT_ENV_VAR),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ fn complete_environment() -> ProcessEnvironment {
|
||||
ProcessEnvironment {
|
||||
federation_rule_id: Some("rule-one".into()),
|
||||
identity_token_file: Some(std::env::temp_dir().join("identity-token").into_os_string()),
|
||||
workload_identity_context: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +49,18 @@ fn markers_select_wif_and_partial_configuration_fails_closed() {
|
||||
.expect("no markers")
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
resolve_for_test(
|
||||
ProcessEnvironment {
|
||||
workload_identity_context: Some(r#"{"instance_id":"box-one"}"#.into()),
|
||||
..ProcessEnvironment::default()
|
||||
},
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
.expect("context alone is not a WIF marker")
|
||||
.is_none()
|
||||
);
|
||||
for (environment, missing) in [
|
||||
(
|
||||
ProcessEnvironment {
|
||||
@@ -131,6 +144,24 @@ fn auth_policy_and_app_environment_are_enforced() {
|
||||
assert!(error.to_string().contains("app routing"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workload_context_is_preserved_without_logging_its_value() {
|
||||
let context = r#"{"instance_id":"box-one"}"#;
|
||||
let config = resolve_for_test(
|
||||
ProcessEnvironment {
|
||||
workload_identity_context: Some(context.into()),
|
||||
..complete_environment()
|
||||
},
|
||||
/*chatgpt_login_allowed*/ true,
|
||||
"https://chatgpt.com/backend-api",
|
||||
)
|
||||
.expect("valid configuration")
|
||||
.expect("WIF selected");
|
||||
|
||||
assert_eq!(config.workload_identity_context.as_deref(), Some(context));
|
||||
assert!(!format!("{config:?}").contains(context));
|
||||
}
|
||||
|
||||
fn session_config(directory: &Path, server: &MockServer) -> WorkloadIdentitySessionConfig {
|
||||
let assertion_file = directory.join("identity-token");
|
||||
std::fs::write(&assertion_file, "assertion-one").expect("write assertion");
|
||||
@@ -140,6 +171,7 @@ fn session_config(directory: &Path, server: &MockServer) -> WorkloadIdentitySess
|
||||
federation_rule_id: "rule-one".to_string(),
|
||||
http_client_factory: HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
token_url: Url::parse(&format!("{}/oauth/token", server.uri())).expect("token URL"),
|
||||
workload_identity_context: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +221,9 @@ async fn compatible_adapters_share_exchange() {
|
||||
.mount(&server)
|
||||
.await;
|
||||
let registry = WorkloadIdentitySessionRegistry::default();
|
||||
let first_config = session_config(temp_dir.path(), &server);
|
||||
let context = r#"{"instance_id":"box-one"}"#;
|
||||
let mut first_config = session_config(temp_dir.path(), &server);
|
||||
first_config.workload_identity_context = Some(context.into());
|
||||
let second_config = first_config.clone();
|
||||
let first = WorkloadIdentityExternalAuth::from_config_with_registry(first_config, ®istry)
|
||||
.expect("first adapter");
|
||||
@@ -211,6 +245,15 @@ async fn compatible_adapters_share_exchange() {
|
||||
.get_token()
|
||||
.expect("second token")
|
||||
);
|
||||
|
||||
let requests = server.received_requests().await.expect("requests");
|
||||
assert_eq!(requests.len(), 1);
|
||||
assert_eq!(
|
||||
url::form_urlencoded::parse(&requests[0].body)
|
||||
.find(|(name, _)| name == "workload_identity_context")
|
||||
.map(|(_, value)| value.into_owned()),
|
||||
Some(context.to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -229,6 +272,8 @@ async fn incompatible_process_session_settings_are_rejected() {
|
||||
std::fs::write(&different_file.assertion_file, "assertion-two").expect("write assertion");
|
||||
let mut different_environment = base.clone();
|
||||
different_environment.environment = WorkloadIdentityEnvironment::Production;
|
||||
let mut different_context = base.clone();
|
||||
different_context.workload_identity_context = Some(r#"{"instance_id":"box-two"}"#.into());
|
||||
let mut different_route = base;
|
||||
different_route.http_client_factory =
|
||||
HttpClientFactory::new(OutboundProxyPolicy::RespectSystemProxy);
|
||||
@@ -241,7 +286,12 @@ async fn incompatible_process_session_settings_are_rejected() {
|
||||
&different_route_adapter.session
|
||||
));
|
||||
|
||||
for config in [different_rule, different_file, different_environment] {
|
||||
for config in [
|
||||
different_rule,
|
||||
different_file,
|
||||
different_environment,
|
||||
different_context,
|
||||
] {
|
||||
assert!(matches!(
|
||||
WorkloadIdentityExternalAuth::from_config_with_registry(config, ®istry),
|
||||
Err(WorkloadIdentitySessionError::ConflictingConfiguration)
|
||||
|
||||
Reference in new issue
Block a user