fix: let IAM credentials take precedence over stored Bedrock API key#18
Merged
lgarceau768 merged 1 commit intoflexfrom Apr 28, 2026
Merged
Conversation
When a user has configured AWS IAM credentials (access keys or a profile) for Bedrock, a stored API key in auth.json would still be written into AWS_BEARER_TOKEN_BEDROCK and forced through bearer-token auth by @ai-sdk/amazon-bedrock, overriding the credential chain entirely and failing with "Please make sure your API Key is valid" on every request. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Collaborator
Author
|
This may occur for folks leveraging multiple tools, i.e. OpenWork, Claude application (configured to bedrock), & OpenCode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the
Forbidden: {"Message":"Authentication failed: Please make sure your API Key is valid."}error that every Bedrock request hits when a stored API key exists inauth.jsonalongside working AWS IAM credentials (e.g. SSO-exported access keys).Root cause
@ai-sdk/amazon-bedrock@4.0.96gives absolute precedence to bearer-token auth: any non-empty value inoptions.apiKeyorprocess.env.AWS_BEARER_TOKEN_BEDROCKforces the SDK into bearer mode and skips the credential chain entirely, regardless of whatcredentialProvideris passed.The
amazon-bedrockcustom provider loader inprovider.tswould take anyauth.type === "api"entry stored in~/.local/share/opencode/auth.jsonand unconditionally write it toAWS_BEARER_TOKEN_BEDROCK. A stale or invalid Bedrock API key stored at any point (e.g. via the Settings UI) would then be forced onto every request, even whenAWS_ACCESS_KEY_ID/AWS_PROFILEwere set and valid.Fix
Only treat a stored auth key as a bearer token when no IAM credentials (access key or profile) are available. When IAM creds are present, they should drive the credential chain as intended.
Test plan
opencode run "say hi"succeeds against Bedrock after the fixopencode-work) successfully streams fromus.anthropic.claude-sonnet-4-6andglobal.anthropic.claude-haiku-4-5-20251001-v1:0after the fixAWS_BEARER_TOKEN_BEDROCKfrom the environment + starting a fresh terminal resolves the issue even with the old binary — proving the diagnosisRelated
Anyone who hit this should also check
launchctl getenv AWS_BEARER_TOKEN_BEDROCKon macOS — an old bearer token can linger in the user's launchd session and needslaunchctl unsetenv AWS_BEARER_TOKEN_BEDROCKto clear.🤖 Generated with Claude Code