//! Loads executor startup configuration and remote authentication without changing CLI policy. use akode_app_server::in_process::EmbeddedNetworkPolicy; use akode_core::config::ConfigBuilder; use akode_login::AuthManager; use akode_login::CodexAuth; use akode_protocol::auth::AuthMode; use akode_utils_cli::CliConfigOverrides; use std::sync::Arc; const REMOTE_AUTH_MODES: &[AuthMode] = &[AuthMode::ApiKey]; pub(super) async fn load_exec_server_remote_auth_provider( config: &akode_core::config::Config, base_url: &str, ) -> anyhow::Result { let (_auth_manager, auth) = load_exec_server_remote_auth( config, "remote exec-server registration requires API key authentication; run `akode login --with-api-key` or set AKODE_API_KEY", ) .await?; if !is_supported_exec_server_remote_auth(&auth) { anyhow::bail!("remote exec-server registration requires API key authentication"); } validate_api_key_remote_host(base_url)?; Ok(akode_model_provider::auth_provider_from_auth(&auth)) } pub(super) fn is_supported_exec_server_remote_auth(auth: &CodexAuth) -> bool { auth.is_api_key_auth() } pub(super) fn validate_api_key_remote_host(base_url: &str) -> anyhow::Result<()> { let url = url::Url::parse(base_url) .map_err(|err| anyhow::anyhow!("invalid remote exec-server registration URL: {err}"))?; let host = url.host().ok_or_else(|| { anyhow::anyhow!("remote exec-server registration URL must include a host") })?; let is_loopback = match &host { url::Host::Domain(host) => host.eq_ignore_ascii_case("localhost"), url::Host::Ipv4(ip) => ip.is_loopback(), url::Host::Ipv6(ip) => ip.is_loopback(), }; let is_openai_host = match &host { url::Host::Domain(host) => ["openai.com", "openai.org"].into_iter().any(|domain| { host.eq_ignore_ascii_case(domain) || host.to_ascii_lowercase().ends_with(&format!(".{domain}")) }), _ => false, }; let is_allowed = match url.scheme() { "https" => is_loopback || is_openai_host, "http" => is_loopback, _ => false, }; if !is_allowed { anyhow::bail!( "remote exec-server API-key authentication is restricted to HTTPS openai.com and openai.org hosts and subdomains or loopback hosts" ); } Ok(()) } pub(super) async fn load_exec_server_config( root_config_overrides: &CliConfigOverrides, strict_config: bool, ) -> anyhow::Result { let cli_kv_overrides = root_config_overrides .parse_overrides() .map_err(anyhow::Error::msg)?; let builder = ConfigBuilder::default() .cli_overrides(cli_kv_overrides) .strict_config(strict_config); let mut config = builder.build().await?; EmbeddedNetworkPolicy::default().activate(&mut config); Ok(config) } async fn load_exec_server_remote_auth( config: &akode_core::config::Config, missing_auth_error: &'static str, ) -> anyhow::Result<(Arc, akode_login::CodexAuth)> { let auth_manager = AuthManager::shared_from_config_with_auth_modes( config, /*enable_codex_api_key_env*/ true, REMOTE_AUTH_MODES.to_vec(), ) .await?; let auth = match auth_manager.auth().await { Some(auth) => auth, None => { auth_manager.reload().await; auth_manager .auth() .await .ok_or_else(|| anyhow::anyhow!(missing_auth_error))? } }; Ok((auth_manager, auth)) } #[cfg(test)] #[path = "config_tests.rs"] mod tests;