mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Enable OAuth discovery by default for config servers (#62824)
## Why Config servers that publish OAuth metadata still required administrators to configure OAuth endpoints explicitly for Codex to use them. ## What changed - Discover OAuth metadata when `config_server.oauth` is omitted. Use explicit settings when provided, and fetch managed configuration anonymously when discovery finds no metadata. - Have the Git credential helper resolve the config server before fetching managed configuration. It refuses explicit or discovered OAuth so it can apply workspace policy without opening the OS keyring. - Apply local network policy to discovery during `codex logout`. ## Testing Cover app-server recovery after a discovery failure, Git credential helper rejection of explicit OAuth without a network request, and logout discovery blocked by local network policy. GitOrigin-RevId: 1ad84effef48fd9b56371ee177bd53c54f3e87b8
This commit is contained in:
@@ -2,7 +2,6 @@ use crate::backend::BackendBundleClient;
|
||||
use crate::chatgpt::ChatGptSource;
|
||||
use crate::chatgpt::plan_requires_managed_config;
|
||||
use crate::config_source::BundleSource;
|
||||
use crate::discovery::ConfigServerDiscovery;
|
||||
use crate::service::CLOUD_CONFIG_BUNDLE_TIMEOUT;
|
||||
use crate::service::CloudConfigBundleService;
|
||||
use codex_config::CloudConfigBundleLoader;
|
||||
@@ -29,13 +28,8 @@ pub async fn managed_config_auth_manager(
|
||||
let Some(service_config) = requirements.config_server.as_ref() else {
|
||||
return Ok(None);
|
||||
};
|
||||
crate::config_server_auth_manager(
|
||||
service_config,
|
||||
auth.codex_home,
|
||||
&auth.auth_route_config,
|
||||
ConfigServerDiscovery::Disabled,
|
||||
)
|
||||
.await
|
||||
crate::config_server_auth_manager(service_config, auth.codex_home, &auth.auth_route_config)
|
||||
.await
|
||||
}
|
||||
|
||||
/// How to sign in when the config server has no saved OAuth grant.
|
||||
@@ -94,7 +88,6 @@ pub async fn managed_config_bundle_loader(
|
||||
service_config.clone(),
|
||||
codex_home,
|
||||
&codex_login::AuthRouteConfig::from_http_client_factory(factory),
|
||||
ConfigServerDiscovery::Disabled,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -122,7 +115,6 @@ pub async fn managed_config_bundle_loader_for_storage(
|
||||
service_config.clone(),
|
||||
auth_config.codex_home,
|
||||
&auth_config.auth_route_config,
|
||||
ConfigServerDiscovery::Disabled,
|
||||
)
|
||||
.await);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ 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;
|
||||
@@ -38,34 +37,47 @@ pub async fn config_server_bundle_loader(
|
||||
config: ConfigServer,
|
||||
codex_home: PathBuf,
|
||||
routes: &AuthRouteConfig,
|
||||
discovery: ConfigServerDiscovery,
|
||||
) -> CloudConfigBundleLoader {
|
||||
let service = create_config_server_service(&config, &codex_home, routes, discovery).await;
|
||||
let service = create_config_server_service(&config, &codex_home, routes).await;
|
||||
|
||||
let loader = match service {
|
||||
Ok(service) => {
|
||||
let (loader, _) = managed_config_bundle_loader_for_service(service);
|
||||
loader
|
||||
}
|
||||
Err(error) => config_server_loader_after_failure(
|
||||
config.clone(),
|
||||
codex_home,
|
||||
routes.clone(),
|
||||
discovery,
|
||||
error,
|
||||
),
|
||||
Err(error) => {
|
||||
config_server_loader_after_failure(config.clone(), codex_home, routes.clone(), error)
|
||||
}
|
||||
};
|
||||
|
||||
loader.with_config_server(config)
|
||||
}
|
||||
|
||||
/// Git must apply the same policy as Codex without opening the OS keyring.
|
||||
/// Use the checked resolution directly so a second discovery cannot select OAuth.
|
||||
pub async fn anonymous_config_server_bundle_loader(
|
||||
config: &ConfigServer,
|
||||
codex_home: PathBuf,
|
||||
routes: &AuthRouteConfig,
|
||||
) -> std::io::Result<CloudConfigBundleLoader> {
|
||||
let resolved = resolve_config_server(config, routes).await?;
|
||||
if resolved.oauth.is_some() {
|
||||
return Err(std::io::Error::other(
|
||||
"Git cannot verify Codex workspace policy using a protected login",
|
||||
));
|
||||
}
|
||||
let service =
|
||||
config_server_service(resolved, &codex_home, routes).map_err(std::io::Error::other)?;
|
||||
let (loader, _) = managed_config_bundle_loader_for_service(service);
|
||||
Ok(loader.with_config_server(config.clone()))
|
||||
}
|
||||
|
||||
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)
|
||||
let resolved = resolve_config_server(config, routes)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
CloudConfigBundleLoadError::new(
|
||||
@@ -75,6 +87,14 @@ async fn create_config_server_service(
|
||||
)
|
||||
})?;
|
||||
|
||||
config_server_service(resolved, codex_home, routes)
|
||||
}
|
||||
|
||||
fn config_server_service(
|
||||
resolved: ConfigServer,
|
||||
codex_home: &Path,
|
||||
routes: &AuthRouteConfig,
|
||||
) -> Result<CloudConfigBundleService<ConfigServerSource>, CloudConfigBundleLoadError> {
|
||||
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)?;
|
||||
@@ -90,7 +110,6 @@ fn config_server_loader_after_failure(
|
||||
config: ConfigServer,
|
||||
codex_home: PathBuf,
|
||||
routes: AuthRouteConfig,
|
||||
discovery: ConfigServerDiscovery,
|
||||
error: CloudConfigBundleLoadError,
|
||||
) -> CloudConfigBundleLoader {
|
||||
let initialized = Arc::new(OnceCell::new());
|
||||
@@ -115,13 +134,8 @@ fn config_server_loader_after_failure(
|
||||
None => {
|
||||
initialized
|
||||
.get_or_try_init(|| async move {
|
||||
let mut service = create_config_server_service(
|
||||
&config,
|
||||
&codex_home,
|
||||
&routes,
|
||||
discovery,
|
||||
)
|
||||
.await?;
|
||||
let mut service =
|
||||
create_config_server_service(&config, &codex_home, &routes).await?;
|
||||
service.policy = policy;
|
||||
let (loader, _) = managed_config_bundle_loader_for_service(service);
|
||||
Ok(loader)
|
||||
@@ -153,9 +167,8 @@ pub async fn config_server_auth_manager(
|
||||
service_config: &ConfigServer,
|
||||
codex_home: PathBuf,
|
||||
routes: &AuthRouteConfig,
|
||||
discovery: ConfigServerDiscovery,
|
||||
) -> std::io::Result<Option<OAuthSessionManager>> {
|
||||
let resolved = resolve_config_server(service_config, routes, discovery).await?;
|
||||
let resolved = resolve_config_server(service_config, routes).await?;
|
||||
let factory = config_server_http_client_factory(&resolved, routes);
|
||||
oauth_session_manager(resolved.oauth.as_deref(), codex_home, &factory)
|
||||
}
|
||||
|
||||
@@ -13,24 +13,14 @@ 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()
|
||||
if config.oauth.is_none()
|
||||
&& let Some(oauth) = discover_oauth(&config.base_url, routes).await?
|
||||
{
|
||||
resolved.oauth = Some(Box::new(oauth));
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
//! 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;
|
||||
@@ -100,7 +99,7 @@ async fn bounds_company_discovery_duration() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn preserves_explicit_oauth_and_flag_off_without_discovery() {
|
||||
async fn preserves_explicit_oauth_without_discovery() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
listener.set_nonblocking(true).unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
@@ -116,25 +115,18 @@ async fn preserves_explicit_oauth_and_flag_off_without_discovery() {
|
||||
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:?}"
|
||||
);
|
||||
}
|
||||
let config = ConfigServer {
|
||||
base_url: format!("https://{address}"),
|
||||
oauth: Some(Box::new(explicit_oauth)),
|
||||
};
|
||||
assert_eq!(
|
||||
resolve_config_server(&config, &routes).await.unwrap(),
|
||||
config
|
||||
);
|
||||
let home = tempfile::tempdir().unwrap();
|
||||
crate::anonymous_config_server_bundle_loader(&config, home.path().into(), &routes)
|
||||
.await
|
||||
.expect_err("Git must refuse explicit OAuth without fetching anonymous policy");
|
||||
assert_eq!(
|
||||
listener
|
||||
.accept()
|
||||
|
||||
@@ -23,6 +23,6 @@ pub use bundle_loader::managed_config_auth_manager;
|
||||
pub use bundle_loader::managed_config_bundle_loader;
|
||||
pub use bundle_loader::managed_config_bundle_loader_for_auth;
|
||||
pub use bundle_loader::managed_config_bundle_loader_for_storage;
|
||||
pub use config_server::anonymous_config_server_bundle_loader;
|
||||
pub use config_server::config_server_auth_manager;
|
||||
pub use config_server::config_server_bundle_loader;
|
||||
pub use discovery::ConfigServerDiscovery;
|
||||
|
||||
Reference in New Issue
Block a user