Open History to see the last commit
1import type { AwsCredentialIdentityProvider } from '@smithy/types'
2import type * as CredentialProviders from '@aws-sdk/credential-providers'
3
4export type BedrockEndpoint = 'runtime' | 'mantle'
5
6/** SigV4 service name differs per endpoint. */
7export function sigv4Service(endpoint: BedrockEndpoint): string {
8 return endpoint === 'mantle' ? 'bedrock-mantle' : 'bedrock'
9}
10
11export type ResolvedBedrockAuth =
12 | { kind: 'bearer'; token: string }
13 | {
14 kind: 'sigv4'
15 region: string
16 service: string
17 credentials: AwsCredentialIdentityProvider
18 }
19
20const DEFAULT_REGION = 'us-east-1'
21
22function readApiKeyFromEnv(): string | undefined {
23 // Bedrock is server-only (the AWS SDK is Node-only), so the key always comes
24 // from process.env. Read it directly: a property access never throws, so —
25 // unlike the previous try/catch around getApiKeyFromEnv — we no longer swallow
26 // unrelated errors as "key not set". Each var is blank-checked independently
27 // so a present-but-blank BEDROCK_API_KEY falls through to
28 // AWS_BEARER_TOKEN_BEDROCK (and only then to SigV4) rather than masking it.
29 const env = typeof process !== 'undefined' ? process.env : undefined
30 for (const value of [env?.BEDROCK_API_KEY, env?.AWS_BEARER_TOKEN_BEDROCK]) {
31 if (value && value.trim() !== '') return value
32 }
33 return undefined
34}
35
36export interface BedrockAuthConfig {
37 apiKey?: string
38 region?: string
39 auth?: 'apikey' | 'sigv4' | 'auto'
40}
41
42/** apiKey -> BEDROCK_API_KEY -> AWS_BEARER_TOKEN_BEDROCK -> SigV4 (credential chain). */
43export function resolveBedrockAuth(
44 config: BedrockAuthConfig,
45 endpoint: BedrockEndpoint,
46): ResolvedBedrockAuth {
47 const mode = config.auth ?? 'auto'
48 const region = config.region ?? DEFAULT_REGION
49
50 if (mode !== 'sigv4') {
51 const token = config.apiKey ?? readApiKeyFromEnv()
52 if (token) return { kind: 'bearer', token }
53 if (mode === 'apikey') {
54 throw new Error(
55 'No Bedrock API key found. Set BEDROCK_API_KEY (or ' +
56 'AWS_BEARER_TOKEN_BEDROCK), pass `apiKey`, or use auth: "sigv4".',
57 )
58 }
59 }
60
61 return {
62 kind: 'sigv4',
63 region,
64 service: sigv4Service(endpoint),
65 // Lazy credential provider: the AWS SDK is Node/server-only, so we defer the
66 // dynamic import until SigV4 actually needs to resolve credentials. The
67 // specifier is held in a variable (not a string literal) so bundler dep
68 // scanners (e.g. Vite/esbuild optimizeDeps) cannot statically discover the
69 // AWS SDK and try to pre-bundle it for the browser — it would fail on the
70 // SDK's Node-only `fromTokenFile` export chain. `typeof import(...)` is a
71 // type-only reference (erased at emit) so we keep full typing.
72 credentials: async (...args) => {
73 const mod = '@aws-sdk/credential-providers'
74 const { fromNodeProviderChain } = (await import(
75 /* @vite-ignore */ mod
76 )) as typeof CredentialProviders
77 return fromNodeProviderChain()(...args)
78 },
79 }
80}