feat: add API key authentication support for Bedrock provider (#8101)

This commit is contained in:
Nate Sesti 2025-10-05 16:53:49 -07:00 • committed by GitHub
parent 33e39b5b16
commit e78cca5d38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 105 additions and 8 deletions

View file

@ -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<BedrockRuntimeClient> {
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,
});
}

View file

@ -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<ModelConfig & { options?: TestConfigOptions }, "name">[] = [
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");
});
});
});

View file

@ -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<typeof BedrockConfigSchema>;