Skip to content

fix(ai/bedrock): use resolved apiKey as a bearer-token fallback#5586

Closed
Roman-Galeev wants to merge 1 commit into
earendil-works:mainfrom
Roman-Galeev:fix/bedrock-apikey-bearer-token
Closed

fix(ai/bedrock): use resolved apiKey as a bearer-token fallback#5586
Roman-Galeev wants to merge 1 commit into
earendil-works:mainfrom
Roman-Galeev:fix/bedrock-apikey-bearer-token

Conversation

@Roman-Galeev

Copy link
Copy Markdown
Contributor

Fixes #5584.

Problem

The bedrock-converse-stream provider authenticates only via options.bearerToken or AWS_BEARER_TOKEN_BEDROCK. A gateway token configured through models.json apiKey (e.g. an AI gateway fronting Bedrock with a bearer token) is silently ignored, so a config that looks complete fails to authenticate unless the same token is also exported as AWS_BEARER_TOKEN_BEDROCK. Every other provider authenticates from apiKey; Bedrock was the surprising exception.

Change

packages/ai/src/providers/amazon-bedrock.ts now uses the resolved apiKey as the lowest-precedence bearer-token source:

bearerToken option  >  AWS_BEARER_TOKEN_BEDROCK  >  apiKey

apiKey already reaches the provider via StreamOptions, so no signature changes were needed. The "<authenticated>" sentinel that getEnvApiKey() returns for SigV4 / profile / role credentials is explicitly excluded, so existing SigV4 setups are unaffected, and AWS_BEDROCK_SKIP_AUTH=1 still disables bearer auth.

The sentinel was a string literal duplicated in two places in env-api-keys.ts; it is now an exported AUTHENTICATED_SENTINEL constant so the provider guard and the producer cannot drift.

Tests

New packages/ai/test/bedrock-bearer-token.test.ts (7 cases) covers: apiKey → bearer, precedence of bearerToken and the env var over apiKey, the sentinel guard, the SigV4 (no-token) path, the AWS_BEDROCK_SKIP_AUTH escape hatch, and the streamSimple entrypoint.

Verification

  • npm run check — passes.
  • packages/ai test suite — 339 passed / 0 failed (the package I changed).
  • ./test.sh surfaces 3 failures in packages/coding-agent (package-command-paths, trust-manager, 3302-find-path-glob). These are unrelated to this change — none import the modified modules — and reproduce independently of it; they appear to be environment-sensitive on my machine.

The Bedrock provider only authenticated via `options.bearerToken` or the
AWS_BEARER_TOKEN_BEDROCK env var. A gateway token configured through
models.json `apiKey` (e.g. an AI gateway fronting Bedrock) was silently
ignored, so a config that looked complete failed to authenticate unless
the env var was also exported.

Add `options.apiKey` as the lowest-precedence bearer-token source:
`bearerToken` option > AWS_BEARER_TOKEN_BEDROCK > apiKey. The
"<authenticated>" sentinel that getEnvApiKey() returns for SigV4 /
profile / role credentials is explicitly excluded so existing SigV4
setups are unaffected.

Extract the sentinel into a shared AUTHENTICATED_SENTINEL constant and
add regression tests covering precedence, the sentinel guard, the
SKIP_AUTH escape hatch, and the streamSimple entrypoint.
@github-actions

Copy link
Copy Markdown
Contributor

This PR was auto-closed. Only contributors approved with lgtm can open PRs. Open an issue first.

Maintainers review auto-closed issues daily. Issues that do not meet the quality bar in CONTRIBUTING.md will not be reopened or receive a reply.

If a maintainer replies lgtmi, your future issues will stay open. If a maintainer replies lgtm, your future issues and PRs will stay open.

See CONTRIBUTING.md.

@github-actions github-actions Bot closed this Jun 10, 2026
@hackslashX

Copy link
Copy Markdown

This was the fix I needed :')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock provider ignores models.json apiKey; requires AWS_BEARER_TOKEN_BEDROCK

2 participants