mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Support managed AWS access keys for Amazon Bedrock (#40481)
## What changed - Add the experimental `amazonBedrockAccessKeys` app-server login flow, persist its credentials in the configured auth store, and use them for SigV4-signed Bedrock requests. - Report `bedrockAccessKeys` as a distinct auth mode and document access-key login alongside AWS profile and environment credential setup. - Clear the selected Bedrock provider, AWS settings, and Bedrock-specific model on logout while leaving external AWS credentials and `.env` untouched. - Reject managed access-key login when higher-precedence configuration would prevent its region or profile settings from taking effect. ## Testing - Cover access-key login, storage, request signing, configuration conflicts, status reporting, and logout cleanup. GitOrigin-RevId: bbf297887c6503d3651ceb2aad85c4d626e32723
This commit is contained in:
1 parent
219c203417
commit
4f6d43c267
60 files changed
+1464
-389
No files matched your search
@@ -43,6 +43,7 @@ use super::workload_identity::WorkloadIdentitySessionError;
|
||||
use crate::auth::AuthHeaders;
|
||||
pub use crate::auth::agent_identity::AgentIdentityAuth;
|
||||
pub use crate::auth::agent_identity::AgentIdentityAuthError;
|
||||
pub use crate::auth::bedrock_access_keys::BedrockAccessKeysAuth;
|
||||
pub use crate::auth::bedrock_api_key::BedrockApiKeyAuth;
|
||||
pub use crate::auth::personal_access_token::PersonalAccessTokenAuth;
|
||||
pub use crate::auth::storage::AgentIdentityAuthRecord;
|
||||
@@ -81,6 +82,7 @@ pub enum CodexAuth {
|
||||
AgentIdentity(AgentIdentityAuth),
|
||||
PersonalAccessToken(PersonalAccessTokenAuth),
|
||||
BedrockApiKey(BedrockApiKeyAuth),
|
||||
BedrockAccessKeys(BedrockAccessKeysAuth),
|
||||
}
|
||||
|
||||
/// Policy for resolving Agent Identity auth from a broader Codex auth snapshot.
|
||||
@@ -155,6 +157,7 @@ impl PartialEq for CodexAuth {
|
||||
(Self::Headers(a), Self::Headers(b)) => a == b,
|
||||
(Self::PersonalAccessToken(a), Self::PersonalAccessToken(b)) => a == b,
|
||||
(Self::BedrockApiKey(a), Self::BedrockApiKey(b)) => a == b,
|
||||
(Self::BedrockAccessKeys(a), Self::BedrockAccessKeys(b)) => a == b,
|
||||
_ => self.api_auth_mode() == other.api_auth_mode(),
|
||||
}
|
||||
}
|
||||
@@ -359,6 +362,14 @@ impl CodexAuth {
|
||||
};
|
||||
return Ok(Self::BedrockApiKey(auth));
|
||||
}
|
||||
if auth_mode == AuthMode::BedrockAccessKeys {
|
||||
let Some(auth) = auth_dot_json.bedrock_access_keys else {
|
||||
return Err(std::io::Error::other(
|
||||
"Bedrock access keys auth is missing AWS access keys.",
|
||||
));
|
||||
};
|
||||
return Ok(Self::BedrockAccessKeys(auth));
|
||||
}
|
||||
if auth_mode == AuthMode::Headers {
|
||||
return Err(std::io::Error::other(
|
||||
"externally provided auth cannot be loaded from auth storage.",
|
||||
@@ -391,6 +402,9 @@ impl CodexAuth {
|
||||
unreachable!("personal access token mode is handled above")
|
||||
}
|
||||
AuthMode::BedrockApiKey => unreachable!("bedrock api key mode is handled above"),
|
||||
AuthMode::BedrockAccessKeys => {
|
||||
unreachable!("bedrock access keys mode is handled above")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -473,6 +487,7 @@ impl CodexAuth {
|
||||
Self::AgentIdentity(_) => AuthMode::AgentIdentity,
|
||||
Self::PersonalAccessToken(_) => AuthMode::PersonalAccessToken,
|
||||
Self::BedrockApiKey(_) => AuthMode::BedrockApiKey,
|
||||
Self::BedrockAccessKeys(_) => AuthMode::BedrockAccessKeys,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,6 +501,7 @@ impl CodexAuth {
|
||||
Self::AgentIdentity(_) => AuthMode::AgentIdentity,
|
||||
Self::PersonalAccessToken(_) => AuthMode::PersonalAccessToken,
|
||||
Self::BedrockApiKey(_) => AuthMode::BedrockApiKey,
|
||||
Self::BedrockAccessKeys(_) => AuthMode::BedrockAccessKeys,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -525,7 +541,8 @@ impl CodexAuth {
|
||||
| Self::Headers(_)
|
||||
| Self::AgentIdentity(_)
|
||||
| Self::PersonalAccessToken(_)
|
||||
| Self::BedrockApiKey(_) => None,
|
||||
| Self::BedrockApiKey(_)
|
||||
| Self::BedrockAccessKeys(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,7 +574,7 @@ impl CodexAuth {
|
||||
"header auth does not expose a bearer token",
|
||||
)),
|
||||
Self::PersonalAccessToken(auth) => Ok(auth.access_token().to_string()),
|
||||
Self::BedrockApiKey(_) => Err(std::io::Error::other(
|
||||
Self::BedrockApiKey(_) | Self::BedrockAccessKeys(_) => Err(std::io::Error::other(
|
||||
"Bedrock API key auth does not expose a Codex bearer token",
|
||||
)),
|
||||
}
|
||||
@@ -648,7 +665,8 @@ impl CodexAuth {
|
||||
| Self::Headers(_)
|
||||
| Self::AgentIdentity(_)
|
||||
| Self::PersonalAccessToken(_)
|
||||
| Self::BedrockApiKey(_) => return None,
|
||||
| Self::BedrockApiKey(_)
|
||||
| Self::BedrockAccessKeys(_) => return None,
|
||||
};
|
||||
#[expect(clippy::unwrap_used)]
|
||||
state.auth_dot_json.lock().unwrap().clone()
|
||||
@@ -693,7 +711,8 @@ impl CodexAuth {
|
||||
| Self::ChatgptAuthTokens(_)
|
||||
| Self::Headers(_)
|
||||
| Self::PersonalAccessToken(_)
|
||||
| Self::BedrockApiKey(_) => Ok(None),
|
||||
| Self::BedrockApiKey(_)
|
||||
| Self::BedrockAccessKeys(_) => Ok(None),
|
||||
Self::Chatgpt(_) => {
|
||||
if policy == AgentIdentityAuthPolicy::JwtOnly {
|
||||
return Ok(None);
|
||||
@@ -766,6 +785,7 @@ impl CodexAuth {
|
||||
agent_identity: None,
|
||||
personal_access_token: None,
|
||||
bedrock_api_key: None,
|
||||
bedrock_access_keys: None,
|
||||
};
|
||||
|
||||
let state = ChatgptAuthState {
|
||||
@@ -970,6 +990,7 @@ pub fn login_with_api_key(
|
||||
agent_identity: None,
|
||||
personal_access_token: None,
|
||||
bedrock_api_key: None,
|
||||
bedrock_access_keys: None,
|
||||
};
|
||||
save_auth(
|
||||
codex_home,
|
||||
@@ -1003,6 +1024,7 @@ pub async fn login_with_access_token(
|
||||
agent_identity: None,
|
||||
personal_access_token: Some(access_token.to_string()),
|
||||
bedrock_api_key: None,
|
||||
bedrock_access_keys: None,
|
||||
}
|
||||
}
|
||||
CodexAccessToken::AgentIdentityJwt(jwt) => {
|
||||
@@ -1021,6 +1043,7 @@ pub async fn login_with_access_token(
|
||||
agent_identity: Some(AgentIdentityStorage::Jwt(jwt.to_string())),
|
||||
personal_access_token: None,
|
||||
bedrock_api_key: None,
|
||||
bedrock_access_keys: None,
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1219,7 +1242,10 @@ fn validate_auth_restrictions(
|
||||
let Some(expected_workspaces) = expected_workspaces else {
|
||||
return Ok(());
|
||||
};
|
||||
if matches!(auth, CodexAuth::ApiKey(_) | CodexAuth::BedrockApiKey(_)) {
|
||||
if matches!(
|
||||
auth,
|
||||
CodexAuth::ApiKey(_) | CodexAuth::BedrockApiKey(_) | CodexAuth::BedrockAccessKeys(_)
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -1281,7 +1307,8 @@ async fn enforce_login_restrictions_with_agent_identity_authapi_base_url(
|
||||
if let Some(required_method) = config.forced_login_method {
|
||||
let method_violation = match (required_method, auth.auth_mode()) {
|
||||
(ForcedLoginMethod::Api, AuthMode::ApiKey)
|
||||
| (ForcedLoginMethod::Api, AuthMode::BedrockApiKey) => None,
|
||||
| (ForcedLoginMethod::Api, AuthMode::BedrockApiKey)
|
||||
| (ForcedLoginMethod::Api, AuthMode::BedrockAccessKeys) => None,
|
||||
(ForcedLoginMethod::Chatgpt, AuthMode::Chatgpt)
|
||||
| (ForcedLoginMethod::Chatgpt, AuthMode::ChatgptAuthTokens)
|
||||
| (ForcedLoginMethod::Chatgpt, AuthMode::Headers)
|
||||
@@ -1296,7 +1323,8 @@ async fn enforce_login_restrictions_with_agent_identity_authapi_base_url(
|
||||
.to_string(),
|
||||
),
|
||||
(ForcedLoginMethod::Chatgpt, AuthMode::ApiKey)
|
||||
| (ForcedLoginMethod::Chatgpt, AuthMode::BedrockApiKey) => Some(
|
||||
| (ForcedLoginMethod::Chatgpt, AuthMode::BedrockApiKey)
|
||||
| (ForcedLoginMethod::Chatgpt, AuthMode::BedrockAccessKeys) => Some(
|
||||
"ChatGPT login is required, but an API key is currently being used. Logging out."
|
||||
.to_string(),
|
||||
),
|
||||
@@ -1314,7 +1342,9 @@ async fn enforce_login_restrictions_with_agent_identity_authapi_base_url(
|
||||
|
||||
if let Some(expected_account_ids) = config.forced_chatgpt_workspace_id.as_deref() {
|
||||
let chatgpt_account_id = match &auth {
|
||||
CodexAuth::ApiKey(_) | CodexAuth::BedrockApiKey(_) => {
|
||||
CodexAuth::ApiKey(_)
|
||||
| CodexAuth::BedrockApiKey(_)
|
||||
| CodexAuth::BedrockAccessKeys(_) => {
|
||||
return Ok(());
|
||||
}
|
||||
CodexAuth::Headers(_)
|
||||
@@ -1717,6 +1747,7 @@ impl AuthDotJson {
|
||||
agent_identity: None,
|
||||
personal_access_token: None,
|
||||
bedrock_api_key: None,
|
||||
bedrock_access_keys: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1730,6 +1761,9 @@ impl AuthDotJson {
|
||||
if self.bedrock_api_key.is_some() {
|
||||
return AuthMode::BedrockApiKey;
|
||||
}
|
||||
if self.bedrock_access_keys.is_some() {
|
||||
return AuthMode::BedrockAccessKeys;
|
||||
}
|
||||
if self.openai_api_key.is_some() {
|
||||
return AuthMode::ApiKey;
|
||||
}
|
||||
@@ -2449,6 +2483,7 @@ impl AuthManager {
|
||||
},
|
||||
(AuthMode::PersonalAccessToken, AuthMode::PersonalAccessToken) => a == b,
|
||||
(AuthMode::BedrockApiKey, AuthMode::BedrockApiKey) => a == b,
|
||||
(AuthMode::BedrockAccessKeys, AuthMode::BedrockAccessKeys) => a == b,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
@@ -2810,7 +2845,8 @@ impl AuthManager {
|
||||
| CodexAuth::Headers(_)
|
||||
| CodexAuth::AgentIdentity(_)
|
||||
| CodexAuth::PersonalAccessToken(_)
|
||||
| CodexAuth::BedrockApiKey(_) => Ok(()),
|
||||
| CodexAuth::BedrockApiKey(_)
|
||||
| CodexAuth::BedrockAccessKeys(_) => Ok(()),
|
||||
}
|
||||
};
|
||||
if let Err(RefreshTokenError::Permanent(error)) = &result {
|
||||
|
||||
Reference in new issue
Block a user