mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add AWS credential export commands for Amazon Bedrock (#44028)
## What changed Support `model_providers.amazon-bedrock.aws.credential_export` with `command`, `args`, and `timeout_ms` to supply SigV4 signing credentials from a command's JSON output. Accept both flat credential-process output and nested STS `Credentials` objects. Cache credentials in memory, refresh before expiration, and share exports across sessions with matching AWS configuration. On recoverable authentication failures, run the optional `aws.auth_refresh` command before exporting fresh credentials, coalescing concurrent recovery attempts. Bound command execution time and output size, and keep credential values out of errors. Reject combining `aws.credential_export` with `aws.profile`. Bedrock setup and login reject changes while an exporter is configured, preserving configuration and saved credentials. ## Testing Add coverage for output formats, expiration, credential precedence, caching, concurrent recovery, command failures, and secret redaction. Integration tests verify request signing with rotated credentials, bounded recovery attempts, and setup/login rejection across configuration layers. GitOrigin-RevId: 4ef799f88ab5e3b2578f8f41a5e56e4f30e2cd81
This commit is contained in:
1 parent
2ce38ae6d8
commit
fe52d795c9
26 files changed
+1790
-89
No files matched your search
@@ -22,11 +22,14 @@ use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::num::NonZeroU64;
|
||||
use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
const DEFAULT_STREAM_IDLE_TIMEOUT_MS: u64 = 300_000;
|
||||
const DEFAULT_STREAM_MAX_RETRIES: u64 = 5;
|
||||
const DEFAULT_REQUEST_MAX_RETRIES: u64 = 4;
|
||||
const DEFAULT_AWS_CREDENTIAL_EXPORT_TIMEOUT_MS: u64 = 30_000;
|
||||
const DEFAULT_AWS_AUTH_REFRESH_TIMEOUT_MS: u64 = 300_000;
|
||||
pub const DEFAULT_WEBSOCKET_CONNECT_TIMEOUT_MS: u64 = 15_000;
|
||||
/// Hard cap for user-configured `stream_max_retries`.
|
||||
@@ -159,10 +162,39 @@ pub struct ModelProviderAwsAuthInfo {
|
||||
pub profile: Option<String>,
|
||||
/// AWS region to use for provider-specific endpoints.
|
||||
pub region: Option<String>,
|
||||
/// Optional command whose exported credentials replace the AWS SDK credential chain.
|
||||
pub credential_export: Option<AwsCredentialExportConfig>,
|
||||
/// Optional command used to reauthenticate after a refreshable AWS auth failure.
|
||||
pub auth_refresh: Option<AwsAuthRefreshConfig>,
|
||||
}
|
||||
|
||||
/// Command used to export AWS signing credentials for a model provider.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
|
||||
#[schemars(deny_unknown_fields)]
|
||||
pub struct AwsCredentialExportConfig {
|
||||
/// Executable to invoke directly, without a shell.
|
||||
pub command: String,
|
||||
/// Arguments passed to the credential export command.
|
||||
#[serde(default)]
|
||||
pub args: Vec<RedactedString>,
|
||||
/// Maximum time to wait for the credential export command to complete.
|
||||
#[serde(default = "default_aws_credential_export_timeout_ms")]
|
||||
pub timeout_ms: NonZeroU64,
|
||||
}
|
||||
|
||||
impl AwsCredentialExportConfig {
|
||||
pub fn timeout(&self) -> Duration {
|
||||
Duration::from_millis(self.timeout_ms.get())
|
||||
}
|
||||
}
|
||||
|
||||
fn default_aws_credential_export_timeout_ms() -> NonZeroU64 {
|
||||
match NonZeroU64::new(DEFAULT_AWS_CREDENTIAL_EXPORT_TIMEOUT_MS) {
|
||||
Some(timeout_ms) => timeout_ms,
|
||||
None => panic!("AWS credential export timeout must be non-zero"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Command used to refresh AWS credentials for a model provider.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
|
||||
#[schemars(deny_unknown_fields)]
|
||||
@@ -192,7 +224,7 @@ fn default_aws_auth_refresh_timeout_ms() -> NonZeroU64 {
|
||||
|
||||
impl ModelProviderInfo {
|
||||
pub fn validate(&self) -> std::result::Result<(), String> {
|
||||
if self.aws.is_some() {
|
||||
if let Some(aws) = self.aws.as_ref() {
|
||||
if self.supports_websockets {
|
||||
// TODO(celia-oai): Support AWS SigV4 signing for WebSocket
|
||||
// upgrade requests before allowing AWS-authenticated providers
|
||||
@@ -221,8 +253,33 @@ impl ModelProviderInfo {
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(auth_refresh) = self.aws.as_ref().and_then(|aws| aws.auth_refresh.as_ref())
|
||||
{
|
||||
if let Some(credential_export) = aws.credential_export.as_ref() {
|
||||
if aws.profile.is_some() {
|
||||
return Err(
|
||||
"provider aws.credential_export cannot be combined with aws.profile"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
if credential_export.command.trim().is_empty() {
|
||||
return Err(
|
||||
"provider aws.credential_export.command must not be empty".to_string()
|
||||
);
|
||||
}
|
||||
let command = Path::new(&credential_export.command);
|
||||
let mut components = command.components();
|
||||
let is_bare_command = matches!(
|
||||
(components.next(), components.next()),
|
||||
(Some(Component::Normal(name)), None) if name == command.as_os_str()
|
||||
);
|
||||
if !command.is_absolute() && !is_bare_command {
|
||||
return Err(
|
||||
"provider aws.credential_export.command must be an absolute path or a bare executable name"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(auth_refresh) = aws.auth_refresh.as_ref() {
|
||||
if auth_refresh.command.trim().is_empty() {
|
||||
return Err("provider aws.auth_refresh.command must not be empty".to_string());
|
||||
}
|
||||
@@ -437,6 +494,7 @@ impl ModelProviderInfo {
|
||||
aws: Some(aws.unwrap_or(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
})),
|
||||
wire_api: WireApi::Responses,
|
||||
@@ -564,7 +622,8 @@ pub fn merge_configured_model_providers(
|
||||
if provider != ModelProviderInfo::default() {
|
||||
return Err(format!(
|
||||
"model_providers.{key} only supports changing \
|
||||
`base_url`, `auth`, `http_headers`, `aws.profile`, `aws.region`, and `aws.auth_refresh`; \
|
||||
`base_url`, `auth`, `http_headers`, `aws.profile`, `aws.region`, `aws.credential_export`, \
|
||||
and `aws.auth_refresh`; \
|
||||
other non-default provider fields are not supported"
|
||||
));
|
||||
}
|
||||
|
||||
@@ -227,9 +227,12 @@ name = "Amazon Bedrock"
|
||||
base_url = "https://bedrock.example.com/v1"
|
||||
|
||||
[aws]
|
||||
profile = "codex-bedrock"
|
||||
region = "us-west-2"
|
||||
|
||||
[aws.credential_export]
|
||||
command = "aws-vault"
|
||||
args = ["--profile", "codex-bedrock"]
|
||||
|
||||
[aws.auth_refresh]
|
||||
command = "aws"
|
||||
args = ["login", "--profile", "codex-bedrock"]
|
||||
@@ -240,8 +243,13 @@ args = ["login", "--profile", "codex-bedrock"]
|
||||
assert_eq!(
|
||||
provider.aws,
|
||||
Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
profile: None,
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: Some(AwsCredentialExportConfig {
|
||||
command: "aws-vault".to_string(),
|
||||
args: vec!["--profile".into(), "codex-bedrock".into()],
|
||||
timeout_ms: NonZeroU64::new(30_000).expect("timeout should be non-zero"),
|
||||
}),
|
||||
auth_refresh: Some(AwsAuthRefreshConfig {
|
||||
command: "aws".to_string(),
|
||||
args: vec!["login".into(), "--profile".into(), "codex-bedrock".into()],
|
||||
@@ -265,6 +273,7 @@ fn test_create_amazon_bedrock_provider() {
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
wire_api: WireApi::Responses,
|
||||
@@ -303,6 +312,7 @@ fn test_create_amazon_bedrock_runtime_provider_with_aws_configuration() {
|
||||
ModelProviderInfo::create_amazon_bedrock_runtime_provider(Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("runtime-profile".to_string()),
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}));
|
||||
|
||||
@@ -318,6 +328,7 @@ fn test_create_amazon_bedrock_runtime_provider_with_aws_configuration() {
|
||||
Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("runtime-profile".to_string()),
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
None,
|
||||
@@ -415,6 +426,11 @@ fn test_merge_configured_model_providers_adds_custom_provider() {
|
||||
|
||||
#[test]
|
||||
fn test_merge_configured_model_providers_applies_amazon_bedrock_aws_override() {
|
||||
let credential_export = AwsCredentialExportConfig {
|
||||
command: "aws-vault".to_string(),
|
||||
args: vec!["export".into(), "codex-bedrock".into()],
|
||||
timeout_ms: NonZeroU64::new(30_000).expect("timeout should be non-zero"),
|
||||
};
|
||||
let auth_refresh = AwsAuthRefreshConfig {
|
||||
command: "aws".to_string(),
|
||||
args: vec!["login".into(), "--profile".into(), "codex-bedrock".into()],
|
||||
@@ -424,8 +440,9 @@ fn test_merge_configured_model_providers_applies_amazon_bedrock_aws_override() {
|
||||
AMAZON_BEDROCK_PROVIDER_ID.to_string(),
|
||||
ModelProviderInfo {
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
profile: None,
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: Some(credential_export.clone()),
|
||||
auth_refresh: Some(auth_refresh.clone()),
|
||||
}),
|
||||
..ModelProviderInfo::default()
|
||||
@@ -437,8 +454,9 @@ fn test_merge_configured_model_providers_applies_amazon_bedrock_aws_override() {
|
||||
.get_mut(AMAZON_BEDROCK_PROVIDER_ID)
|
||||
.expect("Amazon Bedrock provider should be built in")
|
||||
.aws = Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
profile: None,
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: Some(credential_export),
|
||||
auth_refresh: Some(auth_refresh),
|
||||
});
|
||||
|
||||
@@ -456,6 +474,7 @@ fn test_merge_configured_model_providers_applies_runtime_overrides_independently
|
||||
let runtime_aws = ModelProviderAwsAuthInfo {
|
||||
profile: Some("runtime-profile".to_string()),
|
||||
region: Some("eu-west-1".to_string()),
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
};
|
||||
let configured_model_providers = std::collections::HashMap::from([(
|
||||
@@ -493,6 +512,7 @@ fn test_merge_configured_model_providers_applies_amazon_bedrock_transport_overri
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
http_headers: Some(maplit::hashmap! {
|
||||
@@ -511,6 +531,7 @@ fn test_merge_configured_model_providers_applies_amazon_bedrock_transport_overri
|
||||
expected_provider.aws = Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
});
|
||||
expected_provider
|
||||
@@ -536,6 +557,7 @@ fn test_merge_configured_model_providers_rejects_amazon_bedrock_non_default_fiel
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: Some("codex-bedrock".to_string()),
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
..ModelProviderInfo::default()
|
||||
@@ -548,7 +570,7 @@ fn test_merge_configured_model_providers_rejects_amazon_bedrock_non_default_fiel
|
||||
configured_model_providers,
|
||||
),
|
||||
Err(
|
||||
"model_providers.amazon-bedrock only supports changing `base_url`, `auth`, `http_headers`, `aws.profile`, `aws.region`, and `aws.auth_refresh`; other non-default provider fields are not supported"
|
||||
"model_providers.amazon-bedrock only supports changing `base_url`, `auth`, `http_headers`, `aws.profile`, `aws.region`, `aws.credential_export`, and `aws.auth_refresh`; other non-default provider fields are not supported"
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
@@ -562,6 +584,7 @@ fn test_merge_configured_model_providers_allows_amazon_bedrock_default_fields()
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
wire_api: WireApi::Responses,
|
||||
@@ -584,6 +607,7 @@ fn test_validate_provider_aws_rejects_conflicting_auth() {
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
env_key: Some("AWS_BEARER_TOKEN_BEDROCK".to_string()),
|
||||
@@ -603,6 +627,7 @@ fn test_validate_provider_aws_rejects_websockets() {
|
||||
aws: Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: None,
|
||||
}),
|
||||
requires_openai_auth: false,
|
||||
@@ -633,6 +658,7 @@ fn test_validate_provider_aws_auth_refresh_command() {
|
||||
ModelProviderInfo::create_amazon_bedrock_provider(Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: None,
|
||||
credential_export: None,
|
||||
auth_refresh: Some(AwsAuthRefreshConfig {
|
||||
command: command.to_string(),
|
||||
args: Vec::new(),
|
||||
@@ -644,6 +670,78 @@ fn test_validate_provider_aws_auth_refresh_command() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_provider_aws_credential_export_command() {
|
||||
let absolute_command = std::env::current_exe()
|
||||
.expect("current executable should have a path")
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
for (command, expected) in [
|
||||
(
|
||||
String::new(),
|
||||
Err("provider aws.credential_export.command must not be empty".to_string()),
|
||||
),
|
||||
(
|
||||
" ".to_string(),
|
||||
Err("provider aws.credential_export.command must not be empty".to_string()),
|
||||
),
|
||||
("aws-vault".to_string(), Ok(())),
|
||||
(absolute_command, Ok(())),
|
||||
(
|
||||
"./scripts/export-credentials".to_string(),
|
||||
Err(
|
||||
"provider aws.credential_export.command must be an absolute path or a bare executable name"
|
||||
.to_string(),
|
||||
),
|
||||
),
|
||||
(
|
||||
"scripts/export-credentials".to_string(),
|
||||
Err(
|
||||
"provider aws.credential_export.command must be an absolute path or a bare executable name"
|
||||
.to_string(),
|
||||
),
|
||||
),
|
||||
(
|
||||
"scripts/.".to_string(),
|
||||
Err(
|
||||
"provider aws.credential_export.command must be an absolute path or a bare executable name"
|
||||
.to_string(),
|
||||
),
|
||||
),
|
||||
] {
|
||||
let mut provider =
|
||||
ModelProviderInfo::create_amazon_bedrock_provider(Some(ModelProviderAwsAuthInfo {
|
||||
profile: None,
|
||||
region: Some("us-west-2".to_string()),
|
||||
credential_export: Some(AwsCredentialExportConfig {
|
||||
command,
|
||||
args: vec!["--secret-argument".into()],
|
||||
timeout_ms: NonZeroU64::new(30_000).expect("timeout should be non-zero"),
|
||||
}),
|
||||
auth_refresh: Some(AwsAuthRefreshConfig {
|
||||
command: "aws".to_string(),
|
||||
args: Vec::new(),
|
||||
timeout_ms: NonZeroU64::new(300_000).expect("timeout should be non-zero"),
|
||||
}),
|
||||
}));
|
||||
|
||||
assert_eq!(provider.validate(), expected);
|
||||
if expected.is_ok() {
|
||||
provider.aws.as_mut().expect("AWS config").profile = Some("codex-bedrock".to_string());
|
||||
assert_eq!(
|
||||
provider.validate(),
|
||||
Err("provider aws.credential_export cannot be combined with aws.profile".to_string()),
|
||||
);
|
||||
}
|
||||
let export = provider
|
||||
.aws
|
||||
.as_ref()
|
||||
.and_then(|aws| aws.credential_export.as_ref())
|
||||
.expect("credential export should be configured");
|
||||
assert!(!format!("{export:?}").contains("--secret-argument"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_provider_auth_config_allows_zero_refresh_interval() {
|
||||
let base_dir = tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user