11// Amazon Bedrock tests cover embedding provider plugin behavior.
2- import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime" ;
2+ import * as bedrockRuntimeSdk from "@aws-sdk/client-bedrock-runtime" ;
3+ import { NodeHttp2Handler } from "@smithy/node-http-handler" ;
34import { afterEach , describe , expect , it , vi } from "vitest" ;
45import { createBedrockEmbeddingProvider , hasAwsCredentials } from "./embedding-provider.js" ;
56import { embeddingTesting as testing } from "./test-support.js" ;
67
8+ vi . mock ( "@aws-sdk/client-bedrock-runtime" , { spy : true } ) ;
9+
710afterEach ( ( ) => {
11+ vi . mocked ( bedrockRuntimeSdk . BedrockRuntimeClient ) . mockReset ( ) ;
812 vi . restoreAllMocks ( ) ;
913 vi . unstubAllEnvs ( ) ;
1014} ) ;
@@ -84,6 +88,110 @@ describe("bedrock embedding endpoint routing", () => {
8488 dualstack : true ,
8589 customEndpoint : false ,
8690 } ,
91+ {
92+ name : "configured canonical FIPS endpoint in its own signing region" ,
93+ remoteBaseUrl : undefined ,
94+ providerBaseUrl : "https://bedrock-runtime-fips.us-west-2.amazonaws.com" ,
95+ hostname : "bedrock-runtime-fips.us-west-2.amazonaws.com" ,
96+ signingRegion : "us-west-2" ,
97+ customEndpoint : false ,
98+ } ,
99+ {
100+ name : "configured canonical FIPS endpoint with SDK FIPS mode" ,
101+ remoteBaseUrl : undefined ,
102+ providerBaseUrl : "https://bedrock-runtime-fips.us-west-2.amazonaws.com" ,
103+ hostname : "bedrock-runtime-fips.us-west-2.amazonaws.com" ,
104+ signingRegion : "us-west-2" ,
105+ fips : true ,
106+ customEndpoint : false ,
107+ } ,
108+ {
109+ name : "configured canonical dual-stack endpoint" ,
110+ remoteBaseUrl : undefined ,
111+ providerBaseUrl : "https://bedrock-runtime.us-west-2.api.aws" ,
112+ hostname : "bedrock-runtime.us-west-2.api.aws" ,
113+ signingRegion : "us-west-2" ,
114+ customEndpoint : false ,
115+ } ,
116+ {
117+ name : "configured canonical dual-stack endpoint with SDK dual-stack mode" ,
118+ remoteBaseUrl : undefined ,
119+ providerBaseUrl : "https://bedrock-runtime.us-west-2.api.aws" ,
120+ hostname : "bedrock-runtime.us-west-2.api.aws" ,
121+ signingRegion : "us-west-2" ,
122+ dualstack : true ,
123+ customEndpoint : false ,
124+ } ,
125+ {
126+ name : "configured canonical combined FIPS and dual-stack endpoint" ,
127+ remoteBaseUrl : undefined ,
128+ providerBaseUrl : "https://bedrock-runtime-fips.us-west-2.api.aws" ,
129+ hostname : "bedrock-runtime-fips.us-west-2.api.aws" ,
130+ signingRegion : "us-west-2" ,
131+ customEndpoint : false ,
132+ } ,
133+ {
134+ name : "configured canonical combined FIPS and dual-stack endpoint with SDK modes" ,
135+ remoteBaseUrl : undefined ,
136+ providerBaseUrl : "https://bedrock-runtime-fips.us-west-2.api.aws" ,
137+ hostname : "bedrock-runtime-fips.us-west-2.api.aws" ,
138+ signingRegion : "us-west-2" ,
139+ fips : true ,
140+ dualstack : true ,
141+ customEndpoint : false ,
142+ } ,
143+ {
144+ name : "configured FIPS endpoint upgraded by SDK dual-stack mode" ,
145+ remoteBaseUrl : undefined ,
146+ providerBaseUrl : "https://bedrock-runtime-fips.us-west-2.amazonaws.com" ,
147+ hostname : "bedrock-runtime-fips.us-west-2.api.aws" ,
148+ signingRegion : "us-west-2" ,
149+ dualstack : true ,
150+ customEndpoint : false ,
151+ } ,
152+ {
153+ name : "configured dual-stack endpoint upgraded by SDK FIPS mode" ,
154+ remoteBaseUrl : undefined ,
155+ providerBaseUrl : "https://bedrock-runtime.us-west-2.api.aws" ,
156+ hostname : "bedrock-runtime-fips.us-west-2.api.aws" ,
157+ signingRegion : "us-west-2" ,
158+ fips : true ,
159+ customEndpoint : false ,
160+ } ,
161+ {
162+ name : "configured GovCloud FIPS endpoint with partition-aware signing" ,
163+ remoteBaseUrl : undefined ,
164+ providerBaseUrl : "https://bedrock-runtime-fips.us-gov-west-1.amazonaws.com" ,
165+ hostname : "bedrock-runtime-fips.us-gov-west-1.amazonaws.com" ,
166+ signingRegion : "us-gov-west-1" ,
167+ customEndpoint : false ,
168+ } ,
169+ {
170+ name : "configured China FIPS dual-stack endpoint with its partition DNS suffix" ,
171+ remoteBaseUrl : undefined ,
172+ providerBaseUrl : "https://bedrock-runtime-fips.cn-north-1.api.amazonwebservices.com.cn" ,
173+ hostname : "bedrock-runtime-fips.cn-north-1.api.amazonwebservices.com.cn" ,
174+ signingRegion : "cn-north-1" ,
175+ customEndpoint : false ,
176+ } ,
177+ {
178+ name : "ISO PrivateLink endpoint when speculative security modes are unsupported" ,
179+ remoteBaseUrl : "https://vpce-memory.bedrock-runtime.us-iso-east-1.c2s.ic.gov" ,
180+ providerBaseUrl : undefined ,
181+ hostname : "vpce-memory.bedrock-runtime.us-iso-east-1.c2s.ic.gov" ,
182+ signingRegion : "us-iso-east-1" ,
183+ unsupportedPartitionModes : true ,
184+ } ,
185+ {
186+ name : "ISO regional endpoint that correctly rejects an explicitly requested unsupported mode" ,
187+ remoteBaseUrl : undefined ,
188+ providerBaseUrl : "https://bedrock-runtime.us-iso-east-1.c2s.ic.gov" ,
189+ hostname : "bedrock-runtime.us-iso-east-1.c2s.ic.gov" ,
190+ signingRegion : "us-iso-east-1" ,
191+ dualstack : true ,
192+ unsupportedPartitionModes : true ,
193+ expectedError : "DualStack is enabled but this partition does not support DualStack" ,
194+ } ,
87195 {
88196 name : "SDK PrivateLink override for a canonical regional provider URL" ,
89197 remoteBaseUrl : undefined ,
@@ -102,6 +210,15 @@ describe("bedrock embedding endpoint routing", () => {
102210 fips : true ,
103211 expectedError : "Invalid Configuration: FIPS and custom endpoint are not supported" ,
104212 } ,
213+ {
214+ name : "explicit PrivateLink endpoint that correctly rejects dual-stack mode" ,
215+ remoteBaseUrl : "https://vpce-memory.bedrock-runtime.us-east-1.vpce.amazonaws.com" ,
216+ providerBaseUrl : undefined ,
217+ hostname : "vpce-memory.bedrock-runtime.us-east-1.vpce.amazonaws.com" ,
218+ signingRegion : "us-east-1" ,
219+ dualstack : true ,
220+ expectedError : "Invalid Configuration: Dualstack and custom endpoint are not supported" ,
221+ } ,
105222 ] ) ( "sends signed requests to the $name" , async ( testCase ) => {
106223 const { remoteBaseUrl, providerBaseUrl, hostname, signingRegion } = testCase ;
107224 const sdkEndpoint = "sdkEndpoint" in testCase ? testCase . sdkEndpoint : undefined ;
@@ -119,29 +236,51 @@ describe("bedrock embedding endpoint routing", () => {
119236 vi . stubEnv ( "AWS_USE_FIPS_ENDPOINT" , fips ? "true" : undefined ) ;
120237 vi . stubEnv ( "AWS_USE_DUALSTACK_ENDPOINT" , dualstack ? "true" : undefined ) ;
121238
122- const observedRequests : Array < { hostname : string ; authorization : string | undefined } > = [ ] ;
123- const originalSend = BedrockRuntimeClient . prototype . send ;
124- vi . spyOn ( BedrockRuntimeClient . prototype , "send" ) . mockImplementation (
125- function ( command , options ) {
126- this . config . requestHandler = {
127- handle : async ( request ) => {
128- observedRequests . push ( {
129- hostname : request . hostname ,
130- authorization : request . headers . authorization ,
239+ if ( "unsupportedPartitionModes" in testCase ) {
240+ const { BedrockRuntimeClient : ActualBedrockRuntimeClient } = await vi . importActual <
241+ typeof import ( "@aws-sdk/client-bedrock-runtime" )
242+ > ( "@aws-sdk/client-bedrock-runtime" ) ;
243+ vi . mocked ( bedrockRuntimeSdk . BedrockRuntimeClient ) . mockImplementation (
244+ class extends ActualBedrockRuntimeClient {
245+ constructor ( ...args : ConstructorParameters < typeof ActualBedrockRuntimeClient > ) {
246+ super ( ...args ) ;
247+ const endpointProvider = this . config . endpointProvider . bind ( this . config ) ;
248+ vi . spyOn ( this . config , "endpointProvider" ) . mockImplementation ( ( parameters , context ) => {
249+ if (
250+ parameters . Region === "us-iso-east-1" &&
251+ ! parameters . Endpoint &&
252+ ( parameters . UseFIPS || parameters . UseDualStack )
253+ ) {
254+ const unsupportedMode = parameters . UseFIPS ? "FIPS" : "DualStack" ;
255+ throw new Error (
256+ `${ unsupportedMode } is enabled but this partition does not support ${ unsupportedMode } ` ,
257+ ) ;
258+ }
259+ return endpointProvider ( parameters , context ) ;
131260 } ) ;
132- return {
133- response : {
134- statusCode : 200 ,
135- headers : { "content-type" : "application/json" } ,
136- body : Buffer . from ( JSON . stringify ( { embedding : [ 3 , 4 ] } ) ) ,
137- } ,
138- } ;
261+ }
262+ } ,
263+ ) ;
264+ }
265+
266+ const observedRequests : Array < { hostname : string ; authorization : string | undefined } > = [ ] ;
267+ vi . spyOn ( NodeHttp2Handler , "create" ) . mockImplementation ( ( ) => {
268+ const requestHandler = new NodeHttp2Handler ( ) ;
269+ vi . spyOn ( requestHandler , "handle" ) . mockImplementation ( async ( request ) => {
270+ observedRequests . push ( {
271+ hostname : request . hostname ,
272+ authorization : request . headers . authorization ,
273+ } ) ;
274+ return {
275+ response : {
276+ statusCode : 200 ,
277+ headers : { "content-type" : "application/json" } ,
278+ body : Buffer . from ( JSON . stringify ( { embedding : [ 3 , 4 ] } ) ) ,
139279 } ,
140- destroy ( ) { } ,
141280 } ;
142- return originalSend . call ( this , command , options ) ;
143- } ,
144- ) ;
281+ } ) ;
282+ return requestHandler ;
283+ } ) ;
145284
146285 const { provider, client } = await createBedrockEmbeddingProvider ( {
147286 config : providerBaseUrl
0 commit comments