From e78cca5d382d555be70d986e458a5603f78fa3cd Mon Sep 17 00:00:00 2001 From: Nate Sesti <33237525+sestinj@users.noreply.github.com> Date: Sun, 5 Oct 2025 16:53:49 -0700 Subject: [PATCH] feat: add API key authentication support for Bedrock provider (#8101) --- packages/openai-adapters/src/apis/Bedrock.ts | 18 ++++- .../openai-adapters/src/test/main.test.ts | 81 +++++++++++++++++++ packages/openai-adapters/src/types.ts | 14 ++-- 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/packages/openai-adapters/src/apis/Bedrock.ts b/packages/openai-adapters/src/apis/Bedrock.ts index 89c96ffa6..cf5588d68 100644 --- a/packages/openai-adapters/src/apis/Bedrock.ts +++ b/packages/openai-adapters/src/apis/Bedrock.ts @@ -30,6 +30,7 @@ import { } from "openai/resources/index"; import { fromNodeProviderChain } from "@aws-sdk/credential-providers"; +import { fromStatic } from "@aws-sdk/token-providers"; import { BedrockConfig } from "../types.js"; import { chatChunk, chatChunkFromDelta, embedding, rerank } from "../util.js"; import { safeParseArgs } from "../util/parseArgs.js"; @@ -81,7 +82,7 @@ export class BedrockApi implements BaseLlmApi { secretAccessKey: this.config.env.secretAccessKey, }; } - const profile = this.config.env.profile ?? "bedrock"; + const profile = this.config.env?.profile ?? "bedrock"; try { return await fromNodeProviderChain({ profile: profile, @@ -95,9 +96,22 @@ export class BedrockApi implements BaseLlmApi { return await fromNodeProviderChain()(); } async getClient(): Promise { + const region = this.config.env?.region; + + // If apiKey is provided, use bearer token authentication + if (this.config.apiKey) { + return new BedrockRuntimeClient({ + region, + token: fromStatic({ + token: { token: this.config.apiKey }, + }), + }); + } + + // Otherwise use IAM credentials (existing behavior) const creds = await this.getCreds(); return new BedrockRuntimeClient({ - region: this.config.env.region, + region, credentials: creds, }); } diff --git a/packages/openai-adapters/src/test/main.test.ts b/packages/openai-adapters/src/test/main.test.ts index ceb4e87b7..3b7b96193 100644 --- a/packages/openai-adapters/src/test/main.test.ts +++ b/packages/openai-adapters/src/test/main.test.ts @@ -1,6 +1,7 @@ import { ModelConfig } from "@continuedev/config-yaml"; import * as dotenv from "dotenv"; import { vi } from "vitest"; +import { BedrockApi } from "../apis/Bedrock.js"; import { DEEPSEEK_API_BASE } from "../apis/DeepSeek.js"; import { INCEPTION_API_BASE } from "../apis/Inception.js"; import { OpenAIApi } from "../apis/OpenAI.js"; @@ -182,6 +183,19 @@ const TESTS: Omit[] = [ apiType: "azure-openai", }, }, + // { + // provider: "bedrock", + // model: "us.anthropic.claude-3-5-sonnet-20240620-v1:0", + // apiKey: process.env.BEDROCK_API_KEY!, + // roles: ["chat"], + // env: { + // region: "us-east-1", + // }, + // options: { + // skipTools: true, + // expectUsage: false, + // }, + // }, ]; if (process.env.IGNORE_API_KEY_TESTS === "true") { @@ -190,6 +204,10 @@ if (process.env.IGNORE_API_KEY_TESTS === "true") { }); } else { TESTS.forEach((config) => { + // Skip tests that don't have API keys configured + if (!config.apiKey) { + return; + } describe(`${config.provider}/${config.model}`, () => { vi.setConfig({ testTimeout: 30000 }); testConfig({ name: config.model, ...config }); @@ -359,4 +377,67 @@ describe("Configuration", () => { ); }); }); + + describe("bedrock authentication", () => { + it("should configure Bedrock with API key authentication", () => { + const bedrock = constructLlmApi({ + provider: "bedrock", + apiKey: "test-api-key", + env: { + region: "us-east-1", + }, + }); + + expect(bedrock).toBeInstanceOf(BedrockApi); + }); + + it("should configure Bedrock with IAM credentials", () => { + const bedrock = constructLlmApi({ + provider: "bedrock", + env: { + region: "us-west-2", + accessKeyId: "test-access-key", + secretAccessKey: "test-secret-key", + }, + }); + + expect(bedrock).toBeInstanceOf(BedrockApi); + }); + + it("should configure Bedrock with AWS profile", () => { + const bedrock = constructLlmApi({ + provider: "bedrock", + env: { + region: "eu-west-1", + profile: "my-profile", + }, + }); + + expect(bedrock).toBeInstanceOf(BedrockApi); + }); + + it("should throw error if only accessKeyId is provided", () => { + expect(() => { + constructLlmApi({ + provider: "bedrock", + env: { + region: "us-east-1", + accessKeyId: "test-access-key", + }, + }); + }).toThrow("secretAccessKey is required"); + }); + + it("should throw error if only secretAccessKey is provided", () => { + expect(() => { + constructLlmApi({ + provider: "bedrock", + env: { + region: "us-east-1", + secretAccessKey: "test-secret-key", + }, + }); + }).toThrow("accessKeyId is required"); + }); + }); }); diff --git a/packages/openai-adapters/src/types.ts b/packages/openai-adapters/src/types.ts index 86d2a31c5..3b74b6faf 100644 --- a/packages/openai-adapters/src/types.ts +++ b/packages/openai-adapters/src/types.ts @@ -79,12 +79,14 @@ export const BedrockConfigSchema = OpenAIConfigSchema.extend({ // cacheSystemMessage: z.boolean().optional(), // cacheConversation: z.boolean().optional(), // }).optional(), - env: z.object({ - region: z.string().optional(), - accessKeyId: z.string().optional(), - secretAccessKey: z.string().optional(), - profile: z.string().optional(), - }), + env: z + .object({ + region: z.string().optional(), + accessKeyId: z.string().optional(), + secretAccessKey: z.string().optional(), + profile: z.string().optional(), + }) + .optional(), }); export type BedrockConfig = z.infer;