Skip to content

Commit 3e92ce7

Browse files
author
awstools
committed
feat(client-bedrock-runtime): Add API Key and document citations support for Bedrock Runtime APIs
1 parent 15f931b commit 3e92ce7

18 files changed

+1355
-584
lines changed

clients/client-bedrock-runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@aws-sdk/middleware-recursion-detection": "*",
3030
"@aws-sdk/middleware-user-agent": "*",
3131
"@aws-sdk/region-config-resolver": "*",
32+
"@aws-sdk/token-providers": "*",
3233
"@aws-sdk/types": "*",
3334
"@aws-sdk/util-endpoints": "*",
3435
"@aws-sdk/util-user-agent-browser": "*",

clients/client-bedrock-runtime/src/BedrockRuntimeClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export class BedrockRuntimeClient extends __Client<
366366
identityProviderConfigProvider: async (config: BedrockRuntimeClientResolvedConfig) =>
367367
new DefaultIdentityProviderConfig({
368368
"aws.auth#sigv4": config.credentials,
369+
"smithy.api#httpBearerAuth": config.token,
369370
}),
370371
})
371372
);

clients/client-bedrock-runtime/src/auth/httpAuthExtensionConfiguration.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// smithy-typescript generated code
2-
import { AwsCredentialIdentity, AwsCredentialIdentityProvider, HttpAuthScheme } from "@smithy/types";
2+
import {
3+
AwsCredentialIdentity,
4+
AwsCredentialIdentityProvider,
5+
HttpAuthScheme,
6+
TokenIdentity,
7+
TokenIdentityProvider,
8+
} from "@smithy/types";
39

410
import { BedrockRuntimeHttpAuthSchemeProvider } from "./httpAuthSchemeProvider";
511

@@ -13,6 +19,8 @@ export interface HttpAuthExtensionConfiguration {
1319
httpAuthSchemeProvider(): BedrockRuntimeHttpAuthSchemeProvider;
1420
setCredentials(credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider): void;
1521
credentials(): AwsCredentialIdentity | AwsCredentialIdentityProvider | undefined;
22+
setToken(token: TokenIdentity | TokenIdentityProvider): void;
23+
token(): TokenIdentity | TokenIdentityProvider | undefined;
1624
}
1725

1826
/**
@@ -22,6 +30,7 @@ export type HttpAuthRuntimeConfig = Partial<{
2230
httpAuthSchemes: HttpAuthScheme[];
2331
httpAuthSchemeProvider: BedrockRuntimeHttpAuthSchemeProvider;
2432
credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider;
33+
token: TokenIdentity | TokenIdentityProvider;
2534
}>;
2635

2736
/**
@@ -33,6 +42,7 @@ export const getHttpAuthExtensionConfiguration = (
3342
const _httpAuthSchemes = runtimeConfig.httpAuthSchemes!;
3443
let _httpAuthSchemeProvider = runtimeConfig.httpAuthSchemeProvider!;
3544
let _credentials = runtimeConfig.credentials;
45+
let _token = runtimeConfig.token;
3646
return {
3747
setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void {
3848
const index = _httpAuthSchemes.findIndex((scheme) => scheme.schemeId === httpAuthScheme.schemeId);
@@ -57,6 +67,12 @@ export const getHttpAuthExtensionConfiguration = (
5767
credentials(): AwsCredentialIdentity | AwsCredentialIdentityProvider | undefined {
5868
return _credentials;
5969
},
70+
setToken(token: TokenIdentity | TokenIdentityProvider): void {
71+
_token = token;
72+
},
73+
token(): TokenIdentity | TokenIdentityProvider | undefined {
74+
return _token;
75+
},
6076
};
6177
};
6278

@@ -68,5 +84,6 @@ export const resolveHttpAuthRuntimeConfig = (config: HttpAuthExtensionConfigurat
6884
httpAuthSchemes: config.httpAuthSchemes(),
6985
httpAuthSchemeProvider: config.httpAuthSchemeProvider(),
7086
credentials: config.credentials(),
87+
token: config.token(),
7188
};
7289
};

clients/client-bedrock-runtime/src/auth/httpAuthSchemeProvider.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
AwsSdkSigV4PreviouslyResolved,
66
resolveAwsSdkSigV4Config,
77
} from "@aws-sdk/core";
8+
import { FromSsoInit } from "@aws-sdk/token-providers";
9+
import { doesIdentityRequireRefresh, isIdentityExpired, memoizeIdentityProvider } from "@smithy/core";
810
import {
911
HandlerExecutionContext,
1012
HttpAuthOption,
@@ -13,6 +15,8 @@ import {
1315
HttpAuthSchemeParametersProvider,
1416
HttpAuthSchemeProvider,
1517
Provider,
18+
TokenIdentity,
19+
TokenIdentityProvider,
1620
} from "@smithy/types";
1721
import { getSmithyContext, normalizeProvider } from "@smithy/util-middleware";
1822

@@ -73,6 +77,28 @@ function createAwsAuthSigv4HttpAuthOption(authParameters: BedrockRuntimeHttpAuth
7377
};
7478
}
7579

80+
function createSmithyApiHttpBearerAuthHttpAuthOption(
81+
authParameters: BedrockRuntimeHttpAuthSchemeParameters
82+
): HttpAuthOption {
83+
return {
84+
schemeId: "smithy.api#httpBearerAuth",
85+
propertiesExtractor: <T>(
86+
{ profile, filepath, configFilepath, ignoreCache }: T & FromSsoInit,
87+
context: HandlerExecutionContext
88+
) => ({
89+
/**
90+
* @internal
91+
*/
92+
identityProperties: {
93+
profile,
94+
filepath,
95+
configFilepath,
96+
ignoreCache,
97+
},
98+
}),
99+
};
100+
}
101+
76102
/**
77103
* @internal
78104
*/
@@ -87,6 +113,7 @@ export const defaultBedrockRuntimeHttpAuthSchemeProvider: BedrockRuntimeHttpAuth
87113
switch (authParameters.operation) {
88114
default: {
89115
options.push(createAwsAuthSigv4HttpAuthOption(authParameters));
116+
options.push(createSmithyApiHttpBearerAuthHttpAuthOption(authParameters));
90117
}
91118
}
92119
return options;
@@ -115,6 +142,11 @@ export interface HttpAuthSchemeInputConfig extends AwsSdkSigV4AuthInputConfig {
115142
* @internal
116143
*/
117144
httpAuthSchemeProvider?: BedrockRuntimeHttpAuthSchemeProvider;
145+
146+
/**
147+
* The token used to authenticate requests.
148+
*/
149+
token?: TokenIdentity | TokenIdentityProvider;
118150
}
119151

120152
/**
@@ -140,6 +172,11 @@ export interface HttpAuthSchemeResolvedConfig extends AwsSdkSigV4AuthResolvedCon
140172
* @internal
141173
*/
142174
readonly httpAuthSchemeProvider: BedrockRuntimeHttpAuthSchemeProvider;
175+
176+
/**
177+
* The token used to authenticate requests.
178+
*/
179+
readonly token?: TokenIdentityProvider;
143180
}
144181

145182
/**
@@ -148,8 +185,10 @@ export interface HttpAuthSchemeResolvedConfig extends AwsSdkSigV4AuthResolvedCon
148185
export const resolveHttpAuthSchemeConfig = <T>(
149186
config: T & HttpAuthSchemeInputConfig & AwsSdkSigV4PreviouslyResolved
150187
): T & HttpAuthSchemeResolvedConfig => {
188+
const token = memoizeIdentityProvider(config.token, isIdentityExpired, doesIdentityRequireRefresh);
151189
const config_0 = resolveAwsSdkSigV4Config(config);
152190
return Object.assign(config_0, {
153191
authSchemePreference: normalizeProvider(config.authSchemePreference ?? []),
192+
token,
154193
}) as T & HttpAuthSchemeResolvedConfig;
155194
};

clients/client-bedrock-runtime/src/commands/ApplyGuardrailCommand.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export interface ApplyGuardrailCommandInput extends ApplyGuardrailRequest {}
3232
export interface ApplyGuardrailCommandOutput extends ApplyGuardrailResponse, __MetadataBearer {}
3333

3434
/**
35-
* <p>The action to apply a guardrail.</p>
36-
* <p>For troubleshooting some of the common errors you might encounter when using the <code>ApplyGuardrail</code> API,
37-
* see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html">Troubleshooting Amazon Bedrock API Error Codes</a> in the Amazon Bedrock User Guide</p>
35+
* <p>The action to apply a guardrail.</p> <p>For troubleshooting some of the common errors you might encounter when using the <code>ApplyGuardrail</code> API, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html">Troubleshooting Amazon Bedrock API Error Codes</a> in the Amazon Bedrock User Guide</p>
3836
* @example
3937
* Use a bare-bones client and the command you need to make an API call.
4038
* ```javascript
@@ -197,27 +195,22 @@ export interface ApplyGuardrailCommandOutput extends ApplyGuardrailResponse, __M
197195
* @see {@link BedrockRuntimeClientResolvedConfig | config} for BedrockRuntimeClient's `config` shape.
198196
*
199197
* @throws {@link AccessDeniedException} (client fault)
200-
* <p>The request is denied because you do not have sufficient permissions to perform the requested action. For troubleshooting this error,
201-
* see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-access-denied">AccessDeniedException</a> in the Amazon Bedrock User Guide</p>
198+
* <p>The request is denied because you do not have sufficient permissions to perform the requested action. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-access-denied">AccessDeniedException</a> in the Amazon Bedrock User Guide</p>
202199
*
203200
* @throws {@link InternalServerException} (server fault)
204-
* <p>An internal server error occurred. For troubleshooting this error,
205-
* see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-internal-failure">InternalFailure</a> in the Amazon Bedrock User Guide</p>
201+
* <p>An internal server error occurred. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-internal-failure">InternalFailure</a> in the Amazon Bedrock User Guide</p>
206202
*
207203
* @throws {@link ResourceNotFoundException} (client fault)
208-
* <p>The specified resource ARN was not found. For troubleshooting this error,
209-
* see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-resource-not-found">ResourceNotFound</a> in the Amazon Bedrock User Guide</p>
204+
* <p>The specified resource ARN was not found. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-resource-not-found">ResourceNotFound</a> in the Amazon Bedrock User Guide</p>
210205
*
211206
* @throws {@link ServiceQuotaExceededException} (client fault)
212207
* <p>Your request exceeds the service quota for your account. You can view your quotas at <a href="https://docs.aws.amazon.com/servicequotas/latest/userguide/gs-request-quota.html">Viewing service quotas</a>. You can resubmit your request later.</p>
213208
*
214209
* @throws {@link ThrottlingException} (client fault)
215-
* <p>Your request was denied due to exceeding the account quotas for <i>Amazon Bedrock</i>. For
216-
* troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-throttling-exception">ThrottlingException</a> in the Amazon Bedrock User Guide</p>
210+
* <p>Your request was denied due to exceeding the account quotas for <i>Amazon Bedrock</i>. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-throttling-exception">ThrottlingException</a> in the Amazon Bedrock User Guide</p>
217211
*
218212
* @throws {@link ValidationException} (client fault)
219-
* <p>The input fails to satisfy the constraints specified by <i>Amazon Bedrock</i>. For troubleshooting this error,
220-
* see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-validation-error">ValidationError</a> in the Amazon Bedrock User Guide</p>
213+
* <p>The input fails to satisfy the constraints specified by <i>Amazon Bedrock</i>. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-validation-error">ValidationError</a> in the Amazon Bedrock User Guide</p>
221214
*
222215
* @throws {@link BedrockRuntimeServiceException}
223216
* <p>Base exception class for all service exceptions from BedrockRuntime service.</p>

0 commit comments

Comments
 (0)