Skip to content

bedrock: Add Bedrock API key authentication support#41393

Merged
bennetbo merged 16 commits intozed-industries:mainfrom
5herlocked:feature/bedrock-bearer-token-auth
Dec 17, 2025
Merged

bedrock: Add Bedrock API key authentication support#41393
bennetbo merged 16 commits intozed-industries:mainfrom
5herlocked:feature/bedrock-bearer-token-auth

Conversation

@5herlocked
Copy link
Contributor

@5herlocked 5herlocked commented Oct 28, 2025

Closes #41312

Release Notes:

  • Added support for Bedrock API Keys

Also includes fix for duplicate Region field (#41341).

References:

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Oct 28, 2025
@github-actions github-actions bot added the community champion Issues filed by our amazing community champions! 🫶 label Oct 28, 2025
@5herlocked 5herlocked changed the title Add Bedrock API key authentication support bedrock: Add Bedrock API key authentication support Oct 28, 2025
@5herlocked 5herlocked marked this pull request as draft October 28, 2025 18:00
@SomeoneToIgnore SomeoneToIgnore added the area:ai Improvement related to Agent Panel, Edit Prediction, Copilot, or other AI features label Oct 28, 2025
Fixes zed-industries#41312

This adds support for AWS Bedrock API keys (bearer tokens) as an
authentication method. Users can now authenticate using:

- Traditional access keys (existing)
- Bearer tokens via ZED_AWS_BEARER_TOKEN_BEDROCK environment variable (new)
- Bearer tokens via UI input field (new)

Changes:
- Added bearer_token field to BedrockCredentials struct
- Added ZED_AWS_BEARER_TOKEN_BEDROCK environment variable constant
- Updated authentication logic to check for bearer tokens (read-only from ENV)
- Implemented BearerTokenProvider using AWS SDK's ResolveIdentity trait
- Modified client initialization to use token_provider when bearer token is present
- Enhanced UI with bearer token input field and updated instructions
- Updated all help text and tooltips to mention bearer token option

Bearer tokens are:
- Read from environment variables (never written)
- Stored in Zed's credential store when provided via UI
- Passed to AWS SDK using the proper token_provider configuration

Also includes fix for duplicate Region field (issue zed-industries#41313).

References:
- AWS Bedrock API Key Documentation: https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys-use.html

Co-authored-by: Ona <no-reply@ona.com>
@5herlocked 5herlocked force-pushed the feature/bedrock-bearer-token-auth branch from 5f4a482 to c715051 Compare October 31, 2025 16:05
5herlocked and others added 6 commits October 31, 2025 16:07
Replace unwrap() with proper error handling using context() to
propagate errors instead of panicking when serializing credentials.

Co-authored-by: Ona <no-reply@ona.com>
Support reading credentials from individual environment variables:
- ZED_ACCESS_KEY_ID (required)
- ZED_SECRET_ACCESS_KEY (required)
- ZED_SESSION_TOKEN (optional)
- ZED_AWS_REGION (defaults to us-east-1)

This provides a more intuitive way to set credentials via environment
variables, in addition to the existing ZED_AWS_CREDENTIALS JSON format
and ZED_AWS_BEARER_TOKEN_BEDROCK for bearer tokens.

Co-authored-by: Ona <no-reply@ona.com>
ZED_AWS_CREDENTIALS_VAR was never meant to be read from environment
variables - it was a legacy constant for the credential store key.
Credentials are now stored under AMAZON_AWS_URL in the credential store.

Environment variables are now properly limited to:
- ZED_ACCESS_KEY_ID + ZED_SECRET_ACCESS_KEY + ZED_SESSION_TOKEN (optional)
- ZED_AWS_BEARER_TOKEN_BEDROCK
- ZED_AWS_REGION

Co-authored-by: Ona <no-reply@ona.com>
Improves environment variable handling to match the pattern used by
Anthropic provider:

- Use LazyLock<EnvVar> for all environment variables
- Environment variables are cached and lazy-loaded
- Cleaner code with .value and .name accessors
- More consistent with other providers in the codebase

This provides better performance (cached reads) and cleaner code
structure while maintaining the same functionality.

Co-authored-by: Ona <no-reply@ona.com>
Follow the AWS SDK Rust pattern for bearer token authentication as
documented in release-2025-08-05:

- Add HTTP_BEARER_AUTH_SCHEME_ID constant
- Use auth_scheme_preference([HTTP_BEARER_AUTH_SCHEME_ID]) when bearer token is present
- Create BedrockClient using from_conf() with service-specific config

This ensures the SDK properly prefers httpBearerAuth scheme when
using bearer tokens, matching the official AWS SDK pattern.

Reference: https://github.com/awslabs/aws-sdk-rust/releases/tag/release-2025-08-05

Co-authored-by: Ona <no-reply@ona.com>
@5herlocked 5herlocked marked this pull request as ready for review November 4, 2025 21:47
@JosephTLyons
Copy link
Collaborator

Sorry for the wait time here @5herlocked, we'll get this reviewed ASAP.

@5herlocked
Copy link
Contributor Author

Dw about it -- I know y'all are focused on git stuff!

@benbrandt benbrandt assigned bennetbo and unassigned benbrandt Nov 17, 2025
Resolved conflicts in:
- Cargo.toml: Updated async_zip to 0.0.18, kept PR's aws-config and aws-credential-types versions
- crates/language_models/src/provider/bedrock.rs: Added bearer_token_editor field following upstream pattern

Co-authored-by: Ona <no-reply@ona.com>
@bennetbo
Copy link
Member

Hey, sorry for taking a long time to review. This mostly looks good to me, just two things about the UI that confused me:

  1. We should drop (Optional) from the Bedrock API key input since it is not actually optional if you want to auth via API key
image
  1. When I am using an API key it says "You are using static credentials". Should be "API key configured" like we do for other providers
image

@5herlocked
Copy link
Contributor Author

No issues! I totally know you're busy.

1/ Good point -- in my mind it was open to interpretation -- but I suppose the OR is pretty descriptive

2/ Absolutely. I'll make the changes rn

@5herlocked
Copy link
Contributor Author

Tech Debt cleanup -- writeup

Problem Statement

The Bedrock provider had two parallel authentication paths that created confusion and complexity:

  1. Settings-based authentication (BedrockAuthMethod): NamedProfile, SingleSignOn, Automatic
  2. UX-provided credentials (BedrockCredentials): Access keys or bearer tokens stored in keychain

This led to several issues:

  • Unclear precedence: If a user had both settings-based auth AND UX credentials, the behavior was ambiguous
  • Overloaded None case: When auth_method was None, the code would "use UX creds if present, else default chain"
  • ApiKey enum variant existed but wasn't really used: The code path for UX credentials happened when auth_method was None
  • Confusing is_authenticated() check: Returned true for two different reasons with different behaviors
  • Region was coupled to credentials: Even though region is orthogonal to auth method

Design Goals

  1. Settings override UX credentials — Enterprises can control authentication via managed settings
  2. Single source of truth — One resolved auth type, not multiple fields to check
  3. Explicit auth types — IAM credentials and API Keys behave differently, model them separately
  4. Region independence — Region is orthogonal to auth method, handle it separately
  5. Clear UI feedback — Users should see exactly which auth method is active

Solution: Unified BedrockAuth Enum

We introduced a single enum that represents the fully-resolved authentication configuration:

pub enum BedrockAuth {
    /// Use default AWS credential provider chain (IMDSv2, PodIdentity, env vars, etc.)
    Automatic,
    
    /// Use AWS named profile from ~/.aws/credentials or ~/.aws/config
    NamedProfile { profile_name: String },
    
    /// Use AWS SSO profile
    SingleSignOn { profile_name: String },
    
    /// Use IAM credentials (access key + secret + optional session token)
    IamCredentials {
        access_key_id: String,
        secret_access_key: String,
        session_token: Option<String>,
    },
    
    /// Use Bedrock API Key (bearer token authentication)
    ApiKey { api_key: String },
}

Why split IamCredentials and ApiKey?

These authenticate completely differently in the AWS SDK:

  • IAM CredentialsCredentials::new() with access_key_id, secret_access_key, optional session_token
  • API Keytoken_provider() with httpBearerAuth scheme

Modeling them as separate variants makes the client initialization clean and explicit.

Why separate region?

Region can be used with any auth method. Previously it was stored in BedrockCredentials, but that only made sense for UX-provided credentials. Now region resolution is independent:

  1. Environment variable (ZED_AWS_REGION)
  2. Settings (bedrock.region)
  3. Default (us-east-1)

Authentication Resolution Flow

The new authenticate() method follows a clear priority:

1. Check settings.authentication_method (enterprise control)
   ├─ Automatic      → BedrockAuth::Automatic
   ├─ NamedProfile   → BedrockAuth::NamedProfile { profile_name }
   ├─ SingleSignOn   → BedrockAuth::SingleSignOn { profile_name }
   └─ ApiKey         → Load from keychain/env → BedrockAuth::ApiKey or IamCredentials

2. No settings auth method? Try static credentials
   ├─ Env vars (ZED_BEDROCK_BEARER_TOKEN, ZED_ACCESS_KEY_ID, etc.)
   └─ Keychain

This gives enterprises control via settings while allowing individual users to configure via the UI.

Client Initialization

The get_or_init_client() method is now a clean single match:

match auth {
    Some(BedrockAuth::Automatic) | None => {
        // Default AWS credential provider chain
    }
    Some(BedrockAuth::NamedProfile { profile_name })
    | Some(BedrockAuth::SingleSignOn { profile_name }) => {
        config_builder = config_builder.profile_name(profile_name);
    }
    Some(BedrockAuth::IamCredentials { access_key_id, secret_access_key, session_token }) => {
        let aws_creds = Credentials::new(...);
        config_builder = config_builder.credentials_provider(aws_creds);
    }
    Some(BedrockAuth::ApiKey { api_key }) => {
        config_builder = config_builder
            .auth_scheme_preference(["httpBearerAuth".into()])
            .token_provider(Token::new(api_key, None));
    }
}

UI Improvements

The configuration view now shows exactly which auth method is active:

Auth State Display
Automatic "Using automatic credentials (AWS default chain)"
NamedProfile { "prod" } "Using AWS profile: prod"
SingleSignOn { "dev" } "Using AWS SSO profile: dev"
IamCredentials (from env) "Using IAM credentials from ZED_ACCESS_KEY_ID and ZED_SECRET_ACCESS_KEY environment variables"
IamCredentials (from keychain) "Using IAM credentials"
ApiKey (from env) "Using Bedrock API Key from ZED_BEDROCK_BEARER_TOKEN environment variable"
ApiKey (from keychain) "Using Bedrock API Key"

Users can only reset credentials when using static credentials (not settings-derived auth).

Backward Compatibility

  • BedrockAuthMethod enum retained for settings mapping
  • BedrockCredentials struct retained for keychain serialization (minus region field)
  • All existing settings continue to work
  • Environment variables continue to work

@asmimo
Copy link

asmimo commented Dec 12, 2025

ss

Bedrock API key should be reflected in reset popup too.

And shouldn't we adhere to use AWS_BEARER_TOKEN_BEDROCK instead of ZED_BEDROCK_BEARER_TOKEN and other ZED_XXX too. So, there will be seamless integration with zed, claude code, opencode, crush and others using the same keys.

@5herlocked
Copy link
Contributor Author

5herlocked commented Dec 12, 2025

Thanks for the suggestion. I'll update the tooltip as you recommended.

Regarding the credential isolation approach: we've deliberately chosen to use separate ZED_ prefixed credentials based on real-world customer patterns. Many organizations use isolated accounts specifically for Bedrock and AI consumption, which is exactly what this approach enables. Beyond that organizational pattern, it provides a fundamental level of isolation for the Zed agent itself - making it trivially easy to distinguish actions taken by an LLM from those taken by the actual user through clear credential separation.

I understand the concern about friction, but I think the benefits outweigh it here. That said, if we see significant pushback from the community, I'm open to revisiting the decision. For now, I'd prefer to keep it as-is given the security and auditability benefits.

Worth noting too that we're actively trying to move people away from static credentials entirely, so this specific implementation detail becomes less relevant as we push toward more secure authentication patterns.

@asmimo
Copy link

asmimo commented Dec 13, 2025

I guessed it was deliberate and tried to find the discussion about it, but couldn't. Thanks for the info.

@Jaakkonen
Copy link
Contributor

Why is this being accepted when Azure Foundry PR was closed? #43742 I'm thinking whether that PR should be reconsidered in the light of this one. Or then implement this via the extension API if one for that is coming in the near future.

@tidely
Copy link
Contributor

tidely commented Dec 14, 2025

Why is this being accepted when Azure Foundry PR was closed? #43742 I'm thinking whether that PR should be reconsidered in the light of this one. Or then implement this via the extension API if one for that is coming in the near future.

It's likely because Bedrock is already a existing provider and this PR adds new functionality, while Azure Foundry is a completely new provider.

Copy link
Member

@bennetbo bennetbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good. Any changes you want to get in @5herlocked or can we merge?

@5herlocked
Copy link
Contributor Author

Go ahead merge 🚀

@bennetbo

@bennetbo bennetbo merged commit edf21a3 into zed-industries:main Dec 17, 2025
23 checks passed
@github-project-automation github-project-automation bot moved this from Community Champion PRs to Done in Quality Week – December 2025 Dec 17, 2025
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ai Improvement related to Agent Panel, Edit Prediction, Copilot, or other AI features cla-signed The user has signed the Contributor License Agreement community champion Issues filed by our amazing community champions! 🫶

Projects

Development

Successfully merging this pull request may close these issues.

AI: Amazon Bedrock API keys cannot be used to authenticate

8 participants