Add opt-in OAuth discovery to config server loaders (#62432)

## What changed

Allow config server bundle and auth loaders to discover authorization and
token endpoints, plus an optional device authorization endpoint, from OAuth
metadata when callers enable discovery and omit `config_server.oauth`.
Discovery uses the bootstrap HTTP policy, times out after five seconds,
and keeps the discovered settings in memory. Existing managed-config
entry points pass `ConfigServerDiscovery::Disabled`.

If bundle loader initialization fails, report the error on the first read
and retry on later reads until a loader and its refresh worker can start.

## Testing

Add tests for endpoint parsing and defaults, the discovery timeout,
explicit OAuth precedence, disabled discovery, and local network policy.

GitOrigin-RevId: 7cb72054147ec34f985d55dfe708fccfafcb4ba3
This commit is contained in:
vishnu-oai
2026-11-19 05:12:34 +00:00
committed by copyberry
parent d64451b049
commit 14302d5de9
15 changed files with 447 additions and 50 deletions
+2
View File
@@ -13,11 +13,13 @@ chrono = { workspace = true, features = ["serde"] }
codex-async-utils = { workspace = true }
codex-backend-client = { workspace = true }
codex-config = { workspace = true }
codex-exec-server = { workspace = true }
codex-http-client = { workspace = true }
codex-keyring-store = { workspace = true }
codex-login = { workspace = true }
codex-otel = { workspace = true }
codex-protocol = { workspace = true }
codex-rmcp-client = { workspace = true }
hmac = "0.12.1"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
+21 -14
View File
@@ -1,8 +1,8 @@
use crate::backend::BackendBundleClient;
use crate::chatgpt::ChatGptSource;
use crate::chatgpt::plan_requires_managed_config;
use crate::config_server::config_server_http_client_factory;
use crate::config_source::BundleSource;
use crate::discovery::ConfigServerDiscovery;
use crate::service::CLOUD_CONFIG_BUNDLE_TIMEOUT;
use crate::service::CloudConfigBundleService;
use codex_config::CloudConfigBundleLoader;
@@ -22,15 +22,20 @@ use tokio::task::JoinHandle;
const CONFIG_SERVER_LOGIN_FAILURE: &str =
"Config server sign-in failed. Check config_server.oauth and the credential store.";
pub fn managed_config_auth_manager(
pub async fn managed_config_auth_manager(
auth: AuthConfig,
requirements: &ConfigRequirementsToml,
) -> std::io::Result<Option<OAuthSessionManager>> {
let Some(service_config) = requirements.config_server.as_ref() else {
return Ok(None);
};
let factory = config_server_http_client_factory(service_config, &auth.auth_route_config);
crate::config_server_auth_manager(service_config, auth.codex_home, &factory)
crate::config_server_auth_manager(
service_config,
auth.codex_home,
&auth.auth_route_config,
ConfigServerDiscovery::Disabled,
)
.await
}
/// How to sign in when the config server has no saved OAuth grant.
@@ -45,7 +50,7 @@ pub async fn ensure_config_server_login(
requirements: &ConfigRequirementsToml,
method: ConfigServerLoginMethod,
) -> std::io::Result<()> {
let Some(manager) = managed_config_auth_manager(auth, requirements)? else {
let Some(manager) = managed_config_auth_manager(auth, requirements).await? else {
return Ok(());
};
ensure_config_server_login_with_manager(&manager, method).await
@@ -77,7 +82,7 @@ pub(crate) async fn ensure_config_server_login_with_manager(
}
}
pub fn managed_config_bundle_loader(
pub async fn managed_config_bundle_loader(
auth_manager: Arc<AuthManager>,
chatgpt_base_url: String,
codex_home: PathBuf,
@@ -85,11 +90,13 @@ pub fn managed_config_bundle_loader(
requirements: &ConfigRequirementsToml,
) -> CloudConfigBundleLoader {
if let Some(service_config) = requirements.config_server.as_ref() {
let factory = config_server_http_client_factory(
service_config,
return crate::config_server_bundle_loader(
service_config.clone(),
codex_home,
&codex_login::AuthRouteConfig::from_http_client_factory(factory),
);
return crate::config_server_bundle_loader(service_config.clone(), codex_home, factory);
ConfigServerDiscovery::Disabled,
)
.await;
}
let endpoint = codex_backend_client::Client::new(chatgpt_base_url.clone(), factory.clone())
.config_bundle_url();
@@ -111,13 +118,13 @@ pub async fn managed_config_bundle_loader_for_storage(
enable_codex_api_key_env: bool,
) -> std::io::Result<CloudConfigBundleLoader> {
if let Some(service_config) = requirements.config_server.as_ref() {
let factory =
config_server_http_client_factory(service_config, &auth_config.auth_route_config);
return Ok(crate::config_server_bundle_loader(
service_config.clone(),
auth_config.codex_home,
factory,
));
&auth_config.auth_route_config,
ConfigServerDiscovery::Disabled,
)
.await);
}
let service =
chatgpt_config_bundle_service_for_storage(auth_config, enable_codex_api_key_env).await?;
+125 -20
View File
@@ -1,10 +1,14 @@
//! Fetch managed configuration from a custom config server.
//! Cache entries are scoped to the endpoint and the current OAuth sign-in.
//! Failed discovery is retried on later reads; successful loaders retain their refresh worker.
use crate::backend::BundleRequestError;
use crate::backend::RetryableFailureKind;
use crate::backend::fetch_bundle;
use crate::bundle_loader::managed_config_bundle_loader_for_service;
use crate::config_source::BundleSource;
use crate::discovery::ConfigServerDiscovery;
use crate::discovery::resolve_config_server;
use crate::service::CLOUD_CONFIG_BUNDLE_TIMEOUT;
use crate::service::CloudConfigBundleService;
use codex_backend_client::Client as BackendClient;
@@ -12,6 +16,8 @@ use codex_config::CloudConfigBundle;
use codex_config::CloudConfigBundleLoadError;
use codex_config::CloudConfigBundleLoadErrorCode;
use codex_config::CloudConfigBundleLoader;
use codex_config::CloudConfigBundlePolicy;
use codex_config::CloudConfigBundleSnapshot;
use codex_config::ConfigServer;
use codex_http_client::HttpClientFactory;
use codex_keyring_store::DefaultKeyringStore;
@@ -22,40 +28,139 @@ use codex_login::OAuthSessionManager;
use codex_login::OAuthSessionRequest;
use sha2::Digest;
use sha2::Sha256;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::OnceCell;
pub fn config_server_bundle_loader(
pub async fn config_server_bundle_loader(
config: ConfigServer,
codex_home: PathBuf,
factory: HttpClientFactory,
routes: &AuthRouteConfig,
discovery: ConfigServerDiscovery,
) -> CloudConfigBundleLoader {
match ConfigServerSource::from_config_server(config.clone(), codex_home.clone(), factory) {
Ok(source) => {
let service = CloudConfigBundleService::from_source(
source,
codex_home,
CLOUD_CONFIG_BUNDLE_TIMEOUT,
);
crate::bundle_loader::managed_config_bundle_loader_for_service(service).0
let service = create_config_server_service(&config, &codex_home, routes, discovery).await;
let loader = match service {
Ok(service) => {
let (loader, _) = managed_config_bundle_loader_for_service(service);
loader
}
Err(error) => CloudConfigBundleLoader::new(std::future::ready(Err(error))),
}
.with_config_server(config)
Err(error) => config_server_loader_after_failure(
config.clone(),
codex_home,
routes.clone(),
discovery,
error,
),
};
loader.with_config_server(config)
}
pub fn config_server_auth_manager(
async fn create_config_server_service(
config: &ConfigServer,
codex_home: &Path,
routes: &AuthRouteConfig,
discovery: ConfigServerDiscovery,
) -> Result<CloudConfigBundleService<ConfigServerSource>, CloudConfigBundleLoadError> {
let resolved = resolve_config_server(config, routes, discovery)
.await
.map_err(|error| {
CloudConfigBundleLoadError::new(
CloudConfigBundleLoadErrorCode::Internal,
/*status_code*/ None,
error.to_string(),
)
})?;
let factory = config_server_http_client_factory(&resolved, routes);
let codex_home = codex_home.to_path_buf();
let source = ConfigServerSource::from_config_server(resolved, codex_home.clone(), factory)?;
Ok(CloudConfigBundleService::from_source(
source,
codex_home,
CLOUD_CONFIG_BUNDLE_TIMEOUT,
))
}
fn config_server_loader_after_failure(
config: ConfigServer,
codex_home: PathBuf,
routes: AuthRouteConfig,
discovery: ConfigServerDiscovery,
error: CloudConfigBundleLoadError,
) -> CloudConfigBundleLoader {
let initialized = Arc::new(OnceCell::new());
let initial_error = Arc::new(Mutex::new(Some(error)));
let policy = CloudConfigBundlePolicy::default();
let snapshot_policy = policy.clone();
let get_snapshot = move || {
let initialized = Arc::clone(&initialized);
let initial_error = Arc::clone(&initial_error);
let config = config.clone();
let codex_home = codex_home.clone();
let routes = routes.clone();
let policy = snapshot_policy.clone();
async move {
// Surface the startup error on the first read. Later reads retry
// discovery until one can install a loader and its refresh worker.
let saved_error = initial_error.lock().await.take();
let loader = match saved_error {
Some(error) => Err(error),
None => {
initialized
.get_or_try_init(|| async move {
let mut service = create_config_server_service(
&config,
&codex_home,
&routes,
discovery,
)
.await?;
service.policy = policy;
let (loader, _) = managed_config_bundle_loader_for_service(service);
Ok(loader)
})
.await
}
};
match loader {
Ok(loader) => loader.get_snapshot().await,
Err(error) => CloudConfigBundleSnapshot {
bundle: Err(error),
binding: None,
config_server: None,
},
}
}
};
let get_snapshot_for_bundle = get_snapshot.clone();
CloudConfigBundleLoader::from_getter(move || {
let snapshot = get_snapshot_for_bundle();
async move { snapshot.await.bundle }
})
.with_ema_policy_snapshots(policy, get_snapshot)
}
pub async fn config_server_auth_manager(
service_config: &ConfigServer,
codex_home: PathBuf,
factory: &HttpClientFactory,
routes: &AuthRouteConfig,
discovery: ConfigServerDiscovery,
) -> std::io::Result<Option<OAuthSessionManager>> {
service_config
.validated_base_url()
.map_err(std::io::Error::other)?;
oauth_session_manager(service_config.oauth.as_deref(), codex_home, factory)
let resolved = resolve_config_server(service_config, routes, discovery).await?;
let factory = config_server_http_client_factory(&resolved, routes);
oauth_session_manager(resolved.oauth.as_deref(), codex_home, &factory)
}
pub(crate) fn config_server_http_client_factory(
fn config_server_http_client_factory(
config: &ConfigServer,
routes: &AuthRouteConfig,
) -> HttpClientFactory {
+83
View File
@@ -0,0 +1,83 @@
//! Discovers OAuth endpoints for the config server through RMCP.
//! Explicit OAuth settings take precedence, and discovered settings stay in memory.
use codex_config::ConfigServer;
use codex_exec_server::RouteAwareHttpClient;
use codex_login::AuthRouteConfig;
use codex_login::OAuthSessionConfig;
use codex_rmcp_client::discover_authorization_metadata;
use serde::Deserialize;
use std::io;
use std::sync::Arc;
use std::time::Duration;
const DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5);
/// Controls OAuth discovery when the config server has no explicit OAuth settings.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ConfigServerDiscovery {
#[default]
Disabled,
Enabled,
}
pub(crate) async fn resolve_config_server(
config: &ConfigServer,
routes: &AuthRouteConfig,
discovery: ConfigServerDiscovery,
) -> io::Result<ConfigServer> {
config.validated_base_url().map_err(io::Error::other)?;
let mut resolved = config.clone();
if discovery == ConfigServerDiscovery::Enabled
&& config.oauth.is_none()
&& let Some(oauth) = discover_oauth(&config.base_url, routes).await?
{
resolved.oauth = Some(Box::new(oauth));
resolved.validated_base_url().map_err(io::Error::other)?;
}
Ok(resolved)
}
async fn discover_oauth(
issuer: &str,
routes: &AuthRouteConfig,
) -> io::Result<Option<OAuthSessionConfig>> {
let client = Arc::new(RouteAwareHttpClient::new(
routes.bootstrap_http_client_factory().clone(),
));
let metadata = tokio::time::timeout(
DISCOVERY_TIMEOUT,
discover_authorization_metadata(issuer, client),
)
.await
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "OAuth discovery timed out"))?
.map_err(|_| io::Error::other("OAuth discovery failed"))?;
let Some(metadata) = metadata else {
return Ok(None);
};
let device_authorization_url = metadata
.additional_fields
.get("device_authorization_endpoint")
.map(Option::<String>::deserialize)
.transpose()
.map_err(|_| io::Error::other("OAuth discovery device endpoint is invalid"))?
.flatten();
Ok(Some(OAuthSessionConfig {
authorization_url: metadata.authorization_endpoint,
token_url: metadata.token_endpoint,
device_authorization_url,
client_id: String::from("codex"),
resource: None,
scopes: Vec::new(),
redirect_port: None,
credentials_store: Default::default(),
}))
}
#[cfg(test)]
#[path = "discovery_tests.rs"]
mod tests;
@@ -0,0 +1,175 @@
#![cfg(test)]
//! Tests config-server OAuth defaults, discovery activation, and routing.
use super::ConfigServerDiscovery;
use super::DISCOVERY_TIMEOUT;
use super::discover_oauth;
use super::resolve_config_server;
use codex_config::ConfigServer;
use codex_login::AuthRouteConfig;
use codex_login::OAuthSessionConfig;
use codex_login::test_support::transport_default_auth_route_config;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use std::io;
use std::net::TcpListener;
use std::time::Duration;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::path;
fn metadata(issuer: &str) -> Value {
json!({
"issuer": issuer,
"authorization_endpoint": "https://login.example/authorize",
"token_endpoint": "https://login.example/token",
"scopes_supported": ["openid", "profile"],
})
}
#[tokio::test]
async fn applies_company_defaults_and_optional_device_endpoint() {
let server = MockServer::start().await;
for device_url in [None, Some("https://login.example/device")] {
server.reset().await;
let mut metadata = metadata(&server.uri());
if let Some(device_url) = device_url {
metadata["device_authorization_endpoint"] = json!(device_url);
}
Mock::given(path("/.well-known/oauth-authorization-server"))
.respond_with(ResponseTemplate::new(200).set_body_json(metadata))
.expect(1)
.mount(&server)
.await;
assert_eq!(
discover_oauth(&server.uri(), &transport_default_auth_route_config())
.await
.unwrap(),
Some(OAuthSessionConfig {
authorization_url: "https://login.example/authorize".into(),
token_url: "https://login.example/token".into(),
device_authorization_url: device_url.map(str::to_owned),
client_id: "codex".into(),
resource: None,
scopes: Vec::new(),
redirect_port: None,
credentials_store: Default::default(),
})
);
}
}
#[tokio::test]
async fn rejects_invalid_device_endpoint() {
let server = MockServer::start().await;
let mut metadata = metadata(&server.uri());
metadata["device_authorization_endpoint"] = json!(["secret"]);
Mock::given(path("/.well-known/oauth-authorization-server"))
.respond_with(ResponseTemplate::new(200).set_body_json(metadata))
.expect(1)
.mount(&server)
.await;
let error = discover_oauth(&server.uri(), &transport_default_auth_route_config())
.await
.unwrap_err();
assert_eq!(
error.to_string(),
"OAuth discovery device endpoint is invalid"
);
}
#[tokio::test]
async fn bounds_company_discovery_duration() {
let server = MockServer::start().await;
Mock::given(path("/.well-known/oauth-authorization-server"))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(metadata(&server.uri()))
.set_delay(DISCOVERY_TIMEOUT + Duration::from_secs(1)),
)
.expect(1)
.mount(&server)
.await;
let error = discover_oauth(&server.uri(), &transport_default_auth_route_config())
.await
.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::TimedOut);
}
#[tokio::test]
async fn preserves_explicit_oauth_and_flag_off_without_discovery() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let address = listener.local_addr().unwrap();
let routes = transport_default_auth_route_config();
let explicit_oauth = OAuthSessionConfig {
authorization_url: String::from("https://login.example/authorize"),
token_url: String::from("https://login.example/token"),
device_authorization_url: None,
client_id: String::from("existing-client"),
resource: Some(String::from("existing-resource")),
scopes: vec![String::from("existing-scope")],
redirect_port: Some(1455),
credentials_store: Default::default(),
};
for (discovery, oauth) in [
(ConfigServerDiscovery::Disabled, None),
(
ConfigServerDiscovery::Enabled,
Some(Box::new(explicit_oauth)),
),
] {
let config = ConfigServer {
base_url: format!("https://{address}"),
oauth,
};
assert_eq!(
resolve_config_server(&config, &routes, discovery)
.await
.unwrap(),
config,
"discovery: {discovery:?}"
);
}
assert_eq!(
listener
.accept()
.expect_err("OAuth discovery should be skipped")
.kind(),
io::ErrorKind::WouldBlock
);
}
#[tokio::test]
async fn discovery_obeys_local_network_policy_without_requiring_fetched_policy() {
let server = MockServer::start().await;
let allowed = transport_default_auth_route_config()
.http_client_factory()
.clone();
let blocked = allowed
.clone()
.with_network_policy(codex_http_client::NetworkPolicyController::default().policy());
let routes = AuthRouteConfig::from_http_client_factory(blocked.clone())
.with_local_bootstrap_factory(allowed.clone());
Mock::given(path("/.well-known/oauth-authorization-server"))
.respond_with(ResponseTemplate::new(200).set_body_json(metadata(&server.uri())))
.expect(1)
.mount(&server)
.await;
assert!(
discover_oauth(&server.uri(), &routes)
.await
.unwrap()
.is_some()
);
server.reset().await;
let routes =
AuthRouteConfig::from_http_client_factory(allowed).with_local_bootstrap_factory(blocked);
assert!(discover_oauth(&server.uri(), &routes).await.is_err());
assert!(server.received_requests().await.unwrap().is_empty());
}
+2
View File
@@ -9,6 +9,7 @@ mod cache;
mod chatgpt;
mod config_server;
mod config_source;
mod discovery;
mod metrics;
mod service;
mod validation;
@@ -24,3 +25,4 @@ pub use bundle_loader::managed_config_bundle_loader_for_auth;
pub use bundle_loader::managed_config_bundle_loader_for_storage;
pub use config_server::config_server_auth_manager;
pub use config_server::config_server_bundle_loader;
pub use discovery::ConfigServerDiscovery;