WIP fase 2b-2: rimosso login ChatGPT/OAuth/device code, agent/workload identity, backend-client; resta solo chiave API (test non tutti verdi)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -324,7 +324,7 @@ pub fn configure_search_capable_apps(config: &mut Config, apps_base_url: &str) {
|
||||
pub fn apps_enabled_builder(apps_base_url: impl Into<String>) -> TestCodexBuilder {
|
||||
let apps_base_url = apps_base_url.into();
|
||||
test_codex()
|
||||
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
|
||||
.with_auth(CodexAuth::from_api_key("Test API Key"))
|
||||
.with_config(move |config| configure_apps(config, apps_base_url.as_str()))
|
||||
}
|
||||
|
||||
@@ -350,7 +350,7 @@ pub fn set_default_app_approval_mode_and_reviewer(
|
||||
pub fn search_capable_apps_builder(apps_base_url: impl Into<String>) -> TestCodexBuilder {
|
||||
let apps_base_url = apps_base_url.into();
|
||||
test_codex()
|
||||
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
|
||||
.with_auth(CodexAuth::from_api_key("Test API Key"))
|
||||
.with_config(move |config| configure_search_capable_apps(config, apps_base_url.as_str()))
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ pub mod network_policy_mcp;
|
||||
pub mod oauth;
|
||||
pub mod process;
|
||||
pub mod responses;
|
||||
pub mod runtime_auth;
|
||||
pub mod startup;
|
||||
pub mod streaming_sse;
|
||||
pub mod test_codex;
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
//! Route isolated first-party auth tests to loopback without relaxing destination checks.
|
||||
|
||||
use anyhow::Result;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::process::Command;
|
||||
use tokio::task::JoinSet;
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
use tokio_rustls::rustls;
|
||||
use wiremock::MockServer;
|
||||
|
||||
pub const PROBE_SERVER: &str = "CODEX_RUNTIME_AUTH_TEST_SERVER";
|
||||
|
||||
/// Run one test in an isolated subprocess with first-party HTTP routed to the mock server.
|
||||
/// An optional Realtime target receives chatgpt.com Realtime WebSocket upgrades.
|
||||
pub async fn run_harness_through_proxy(
|
||||
server: &MockServer,
|
||||
test_name: &str,
|
||||
realtime_target: Option<std::net::SocketAddr>,
|
||||
) -> Result<()> {
|
||||
run_harness_through_proxy_with_env(server, test_name, realtime_target, &[]).await
|
||||
}
|
||||
|
||||
/// Adds process-local auth inputs without changing the test runner's environment.
|
||||
pub async fn run_harness_through_proxy_with_env(
|
||||
server: &MockServer,
|
||||
test_name: &str,
|
||||
realtime_target: Option<std::net::SocketAddr>,
|
||||
environment: &[(&str, Option<&str>)],
|
||||
) -> Result<()> {
|
||||
// The production destination check stays intact. A test-only CA and CONNECT proxy route
|
||||
// the real host names to wiremock, only inside this isolated test subprocess.
|
||||
codex_utils_rustls_provider::ensure_rustls_crypto_provider();
|
||||
let rcgen::CertifiedKey { cert, signing_key } = rcgen::generate_simple_self_signed(vec![
|
||||
"api.openai.com".into(),
|
||||
"api.openai.org".into(),
|
||||
"auth.openai.com".into(),
|
||||
"chatgpt.com".into(),
|
||||
"us.chatgpt.com".into(),
|
||||
"chatgpt-staging.com".into(),
|
||||
"us.chatgpt-staging.com".into(),
|
||||
"auth.api.openai.org".into(),
|
||||
])?;
|
||||
let ca_dir = tempfile::tempdir()?;
|
||||
let ca_path = ca_dir.path().join("ca.pem");
|
||||
std::fs::write(&ca_path, cert.pem())?;
|
||||
let acceptor = TlsAcceptor::from(Arc::new(
|
||||
rustls::ServerConfig::builder()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(vec![cert.der().clone()], signing_key.into())?,
|
||||
));
|
||||
let proxy = TcpListener::bind("127.0.0.1:0").await?;
|
||||
let proxy_url = format!("http://{}", proxy.local_addr()?);
|
||||
let target = *server.address();
|
||||
let proxy_task = tokio::spawn(async move {
|
||||
let mut connections = JoinSet::new();
|
||||
loop {
|
||||
let (mut stream, _) = proxy.accept().await?;
|
||||
let acceptor = acceptor.clone();
|
||||
connections.spawn(async move {
|
||||
let mut headers = Vec::new();
|
||||
while !headers.ends_with(b"\r\n\r\n") && headers.len() < 8192 {
|
||||
headers.push(stream.read_u8().await?);
|
||||
}
|
||||
let headers = std::str::from_utf8(&headers)?;
|
||||
anyhow::ensure!(
|
||||
matches!(
|
||||
headers.lines().next(),
|
||||
Some(
|
||||
"CONNECT api.openai.com:443 HTTP/1.1"
|
||||
| "CONNECT api.openai.org:443 HTTP/1.1"
|
||||
| "CONNECT auth.openai.com:443 HTTP/1.1"
|
||||
| "CONNECT chatgpt.com:443 HTTP/1.1"
|
||||
| "CONNECT us.chatgpt.com:443 HTTP/1.1"
|
||||
| "CONNECT chatgpt-staging.com:443 HTTP/1.1"
|
||||
| "CONNECT us.chatgpt-staging.com:443 HTTP/1.1"
|
||||
| "CONNECT auth.api.openai.org:443 HTTP/1.1"
|
||||
)
|
||||
),
|
||||
"unexpected proxy destination"
|
||||
);
|
||||
stream
|
||||
.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
|
||||
.await?;
|
||||
let realtime_host = headers.starts_with("CONNECT chatgpt.com:443 ");
|
||||
let mut stream = acceptor.accept(stream).await?;
|
||||
let mut prefix = Vec::new();
|
||||
let mut destination = target;
|
||||
if let Some(realtime_target) = realtime_target.filter(|_| realtime_host) {
|
||||
// Background HTTP requests share this host with Realtime. Only upgrades
|
||||
// belong on the WebSocket fixture; ordinary HTTP still goes to wiremock.
|
||||
while !prefix.ends_with(b"\r\n\r\n") && prefix.len() < 65536 {
|
||||
prefix.push(stream.read_u8().await?);
|
||||
}
|
||||
anyhow::ensure!(
|
||||
prefix.ends_with(b"\r\n\r\n"),
|
||||
"oversized test request headers"
|
||||
);
|
||||
let request_headers = std::str::from_utf8(&prefix)?;
|
||||
let realtime_path = request_headers.lines().next().is_some_and(|line| {
|
||||
line.starts_with("GET /v1/realtime") || line.starts_with("GET /v1/live")
|
||||
});
|
||||
if realtime_path
|
||||
&& request_headers.lines().any(|line| {
|
||||
line.split_once(':').is_some_and(|(name, value)| {
|
||||
name.eq_ignore_ascii_case("upgrade")
|
||||
&& value.trim().eq_ignore_ascii_case("websocket")
|
||||
})
|
||||
})
|
||||
{
|
||||
destination = realtime_target;
|
||||
}
|
||||
}
|
||||
let mut target = TcpStream::connect(destination).await?;
|
||||
target.write_all(&prefix).await?;
|
||||
tokio::io::copy_bidirectional(&mut stream, &mut target).await?;
|
||||
Ok::<_, anyhow::Error>(())
|
||||
});
|
||||
}
|
||||
#[allow(unreachable_code)]
|
||||
Ok::<_, anyhow::Error>(())
|
||||
});
|
||||
let mut command = Command::new(std::env::current_exe()?);
|
||||
command
|
||||
.args(["--exact", test_name, "--nocapture"])
|
||||
.kill_on_drop(true);
|
||||
for &key in codex_network_proxy::PROXY_ENV_KEYS {
|
||||
command.env_remove(key);
|
||||
}
|
||||
command
|
||||
.env(PROBE_SERVER, server.uri())
|
||||
.env("HTTPS_PROXY", proxy_url)
|
||||
.env("NO_PROXY", codex_network_proxy::DEFAULT_NO_PROXY_VALUE)
|
||||
.env("CODEX_CA_CERTIFICATE", ca_path);
|
||||
for (key, value) in environment {
|
||||
match value {
|
||||
Some(value) => command.env(key, value),
|
||||
None => command.env_remove(key),
|
||||
};
|
||||
}
|
||||
let output = tokio::time::timeout(Duration::from_secs(90), command.output()).await;
|
||||
proxy_task.abort();
|
||||
let output = output??;
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"harness subprocess failed\n{}\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -427,9 +427,19 @@ pub struct TestCodexBuilder {
|
||||
thread_store: Option<Arc<dyn ThreadStore>>,
|
||||
agent_graph_store: Option<Arc<dyn AgentGraphStore>>,
|
||||
image_store: Arc<dyn AttachmentStore>,
|
||||
catalog_discovery: bool,
|
||||
}
|
||||
|
||||
impl TestCodexBuilder {
|
||||
/// Authenticate with a stored API key and point the provider's model catalog at the
|
||||
/// mock server, so remote catalog refresh runs. Stored API keys only refresh catalogs
|
||||
/// from an explicit `model_catalog_url`.
|
||||
pub fn with_catalog_discovery_api_key(mut self) -> Self {
|
||||
self.auth = TestAuth::Cached(CodexAuth::from_api_key("Test API Key"));
|
||||
self.catalog_discovery = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_apps_tools_cache(mut self, cache: codex_core::CodexAppsToolsCache) -> Self {
|
||||
self.apps_tools_cache = cache;
|
||||
self
|
||||
@@ -1031,7 +1041,18 @@ impl TestCodexBuilder {
|
||||
// specific model can still override this with a config mutator.
|
||||
config.model = Some("gpt-5.5".to_string());
|
||||
config.cwd = cwd_override;
|
||||
config.model_provider = model_provider;
|
||||
if self.catalog_discovery {
|
||||
let catalog_url = model_provider
|
||||
.base_url
|
||||
.as_deref()
|
||||
.map(|base_url| format!("{}/models", base_url.trim_end_matches('/')));
|
||||
config.model_provider = ModelProviderInfo {
|
||||
model_catalog_url: catalog_url.map(Into::into),
|
||||
..model_provider
|
||||
};
|
||||
} else {
|
||||
config.model_provider = model_provider;
|
||||
}
|
||||
if let Ok(path) = codex_utils_cargo_bin::cargo_bin("codex") {
|
||||
config.codex_self_exe = Some(path);
|
||||
} else if let Ok(path) = codex_utils_cargo_bin::cargo_bin("codex-exec") {
|
||||
@@ -1586,6 +1607,7 @@ pub fn test_codex() -> TestCodexBuilder {
|
||||
thread_store: None,
|
||||
agent_graph_store: None,
|
||||
image_store: codex_core::passthrough_image_store(),
|
||||
catalog_discovery: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user