Add a credential file override for GCP provider authentication (#57188)

## What changed

Support `model_providers.gcp.credential_file` as an absolute path to a Google Application Default Credentials (ADC) file. Pass it as `GOOGLE_APPLICATION_CREDENTIALS` in the provider auth command's environment, retaining the override for token refreshes without changing the parent process environment.

Validate the path before merging GCP defaults and preserve the setting in thread configuration serialization.

## Testing

Add coverage for invalid paths, configuration round trips, separate provider token caches, and credential overrides retained after a `401` refresh. Include a Windows regression test for credential paths containing command metacharacters.

GitOrigin-RevId: d9a1d4b3775a9a4c4325dddaa153d92a2d1430a0
This commit is contained in:
alexsong-oai
2026-10-26 18:33:05 +00:00
committed by copyberry
parent 77eb019ad0
commit be7cd6fd9e
21 changed files with 328 additions and 9 deletions
+4
View File
@@ -3034,6 +3034,10 @@
],
"description": "Optional API capability overrides for a custom Responses-compatible provider. Unspecified capabilities retain their existing provider defaults."
},
"credential_file": {
"description": "Absolute path to a Google ADC credential file. For GCP, overrides GOOGLE_APPLICATION_CREDENTIALS in the auth command's environment only.",
"type": "string"
},
"env_http_headers": {
"additionalProperties": {
"type": "string"
+3
View File
@@ -95,6 +95,7 @@ async fn responses_stream_includes_subagent_header_on_review() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -244,6 +245,7 @@ async fn responses_stream_includes_subagent_header_on_other() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -374,6 +376,7 @@ async fn responses_respects_model_info_overrides_from_config() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
+43
View File
@@ -910,6 +910,7 @@ impl ProviderAuthCommandFixture {
codex_utils_cargo_bin::write_executable(
&script_path,
r#"#!/bin/sh
printf '%s\n' "$GOOGLE_APPLICATION_CREDENTIALS" >> credential-env.txt
if [ -f fail-until-401 ]; then
exit 1
fi
@@ -929,6 +930,9 @@ mv tokens.next tokens.txt
&script_path,
r#"@echo off
setlocal EnableExtensions DisableDelayedExpansion
setlocal EnableDelayedExpansion
echo(!GOOGLE_APPLICATION_CREDENTIALS!>> credential-env.txt
endlocal
if exist fail-until-401 exit /b 1
set "first_line="
@@ -971,6 +975,41 @@ move /y tokens.next tokens.txt >nul
}
}
#[cfg(windows)]
#[test]
fn provider_auth_command_logs_credential_paths_with_cmd_metacharacters() -> anyhow::Result<()> {
let auth_fixture = ProviderAuthCommandFixture::new(&["first-token", "second-token"])?;
let credential_file = auth_fixture
.tempdir
.path()
.join("ADC (team)&^!credentials!%PATH%.json");
for (credential_path, token) in [
(Some(credential_file.as_os_str()), "first-token"),
(None, "second-token"),
] {
let mut command = codex_utils_process::background_command(&auth_fixture.command);
command
.args(&auth_fixture.args)
.current_dir(auth_fixture.tempdir.path());
if let Some(credential_path) = credential_path {
command.env("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
} else {
command.env_remove("GOOGLE_APPLICATION_CREDENTIALS");
}
let output = command.output()?;
assert!(output.status.success(), "{output:?}");
assert_eq!(String::from_utf8(output.stdout)?, format!("{token}\r\n"));
assert_eq!(output.stderr, Vec::<u8>::new());
}
assert_eq!(
std::fs::read_to_string(auth_fixture.tempdir.path().join("credential-env.txt"))?,
format!("{}\r\n\r\n", credential_file.display()),
);
Ok(())
}
fn non_zero_u64(value: u64) -> NonZeroU64 {
NonZeroU64::new(value).expect("expected non-zero value")
}
@@ -1746,6 +1785,7 @@ async fn send_provider_auth_request(server: &MockServer, auth: ModelProviderAuth
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -3254,6 +3294,7 @@ async fn azure_responses_request_does_not_store_and_preserves_prefixed_item_ids(
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -4104,6 +4145,7 @@ async fn azure_overrides_assign_properties_used_for_responses_url() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -4194,6 +4236,7 @@ async fn env_var_overrides_loaded_auth() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -2738,6 +2738,7 @@ fn websocket_provider_with_connect_timeout(
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
}
}
+63
View File
@@ -28,9 +28,72 @@ use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use test_case::test_case;
use wiremock::Mock;
use wiremock::ResponseTemplate;
use wiremock::matchers::header;
use wiremock::matchers::method;
use wiremock::matchers::path;
use super::client::ProviderAuthCommandFixture;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn vertex_credential_file_is_retained_when_refreshing_after_401() -> Result<()> {
skip_if_no_network!(Ok(()));
let server = responses::start_mock_server().await;
let auth_fixture = ProviderAuthCommandFixture::new(&["first-token", "second-token"])?;
let auth = auth_fixture.auth();
let credential_file = auth.cwd.join("adc.json").into_path_buf();
let credential_log = auth.cwd.join("credential-env.txt");
let inherited_credential_file = std::env::var_os("GOOGLE_APPLICATION_CREDENTIALS");
Mock::given(method("POST"))
.and(path("/v1/responses"))
.and(header("authorization", "Bearer first-token"))
.respond_with(ResponseTemplate::new(401).set_body_string("unauthorized"))
.expect(1)
.mount(&server)
.await;
let response = responses::mount_sse_once_match(
&server,
header("authorization", "Bearer second-token"),
responses::sse(vec![
responses::ev_response_created("resp-1"),
responses::ev_completed("resp-1"),
]),
)
.await;
let mut provider = ModelProviderInfo::create_gcp_provider();
provider.base_url = Some(format!("{}/v1", server.uri()));
provider.auth = Some(auth);
provider.credential_file = Some(credential_file.clone());
let test = test_codex()
.with_config(move |config| {
config.model_provider_id = GCP_PROVIDER_ID.to_string();
config.model_provider = provider;
})
.build_with_auto_env(&server)
.await?;
test.submit_turn("say hello").await?;
assert_eq!(
response.single_request().header("authorization").as_deref(),
Some("Bearer second-token"),
);
let observed = std::fs::read_to_string(credential_log)?;
let expected = credential_file.to_string_lossy();
assert_eq!(
observed.lines().collect::<Vec<_>>(),
vec![expected.as_ref(); 2]
);
assert_eq!(
std::env::var_os("GOOGLE_APPLICATION_CREDENTIALS"),
inherited_credential_file,
);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn vertex_requests_use_command_auth_with_namespaces_and_tool_search() -> Result<()> {
skip_if_no_network!(Ok(()));
@@ -87,6 +87,7 @@ async fn continue_after_stream_error() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};
@@ -73,6 +73,7 @@ async fn retries_on_early_close() {
capabilities: None,
project_id: None,
location: None,
credential_file: None,
include_internal_metadata: false,
};