Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(middleware-sdk-sqs): add middleware to prioritize QueueUrl endpoi…
…nt resolution (#5759) * fix(middleware-sdk-sqs): add middleware to prioritize QueueUrl endpoint resolution * fix(middleware-sdk-sqs): add option to use QueueUrl as endpoint * test(middleware-sdk-sqs): initial unit test for queue-url middleware * test(middleware-sdk-sqs): fix unit and add integ tests * test(middleware-sdk-sqs): avoid using QueueUrl if custom endpoint given to client * test(middleware-sdk-sqs): warn with exact error --------- Co-authored-by: George Fu <kuhe@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
276 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./queue-url"; | ||
export * from "./receive-message"; | ||
export * from "./send-message"; | ||
export * from "./send-message-batch"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { HttpRequest } from "@aws-sdk/protocol-http"; | ||
import { FinalizeHandlerArguments, HandlerExecutionContext } from "@aws-sdk/types"; | ||
|
||
import { queueUrlMiddleware } from "./queue-url"; | ||
|
||
describe("queueUrlMiddleware", () => { | ||
const mockNextHandler = jest.fn(); | ||
|
||
const mockContext: HandlerExecutionContext = { | ||
logger: { | ||
...console, | ||
warn: jest.fn(), | ||
}, | ||
endpointV2: void 0, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockNextHandler.mockReset(); | ||
mockNextHandler.mockResolvedValue({ output: {}, response: {} }); | ||
mockContext.endpointV2 = { | ||
url: new URL("https://sqs.us-east-1.amazonaws.com"), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("should use the QueueUrl hostname as the endpoint if useQueueUrlAsEndpoint is true", async () => { | ||
const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: true }); | ||
const input = { QueueUrl: "https://xyz.com/123/MyQueue" }; | ||
const request = new HttpRequest({ | ||
hostname: "sqs.us-east-1.amazonaws.com", | ||
protocol: "https:", | ||
path: "/", | ||
headers: {}, | ||
method: "GET", | ||
}); | ||
const args: FinalizeHandlerArguments<any> = { input, request }; | ||
|
||
await middleware(mockNextHandler, mockContext)(args); | ||
|
||
// Verify that the resolvedEndpoint.url has been modified to match QueueUrl | ||
expect(mockContext.endpointV2?.url.href).toEqual("https://xyz.com/"); | ||
expect(mockNextHandler).toHaveBeenCalled(); | ||
expect(mockContext.logger?.warn).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not modify the endpoint if useQueueUrlAsEndpoint is false", async () => { | ||
const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: false }); | ||
const input = { QueueUrl: "https://xyz.com/123/MyQueue" }; | ||
const request = new HttpRequest({ | ||
hostname: "sqs.us-east-1.amazonaws.com", | ||
protocol: "https:", | ||
path: "/", | ||
}); | ||
const args: FinalizeHandlerArguments<any> = { input, request }; | ||
|
||
await middleware(mockNextHandler, mockContext)(args); | ||
|
||
expect(mockNextHandler).toHaveBeenCalledWith(args); | ||
expect(mockContext.endpointV2?.url.href).toEqual("https://sqs.us-east-1.amazonaws.com/"); | ||
}); | ||
|
||
it("should not modify the endpoint when QueueUrl is not provided", async () => { | ||
const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: true }); | ||
const input = {}; // No QueueUrl provided | ||
const request = new HttpRequest({ | ||
hostname: "sqs.us-east-1.amazonaws.com", | ||
protocol: "https:", | ||
path: "/", | ||
}); | ||
const args: FinalizeHandlerArguments<any> = { input, request }; | ||
|
||
await middleware(mockNextHandler, mockContext)(args); | ||
|
||
expect(mockNextHandler).toHaveBeenCalledWith(args); | ||
expect(mockContext.endpointV2?.url.href).toEqual("https://sqs.us-east-1.amazonaws.com/"); | ||
}); | ||
|
||
it("should not modify the endpoint when a custom endpoint is provided in config", async () => { | ||
const middleware = queueUrlMiddleware({ useQueueUrlAsEndpoint: true, endpoint: "https://my-endpoint.com/" }); | ||
const input = { QueueUrl: "https://xyz.com/123/MyQueue" }; | ||
const request = new HttpRequest({ | ||
hostname: "my-endpoint.com", | ||
protocol: "https:", | ||
path: "/", | ||
}); | ||
const args: FinalizeHandlerArguments<any> = { input, request }; | ||
|
||
await middleware(mockNextHandler, mockContext)(args); | ||
|
||
expect(mockNextHandler).toHaveBeenCalledWith(args); | ||
expect(mockContext.endpointV2?.url.href).toEqual("https://sqs.us-east-1.amazonaws.com/"); | ||
expect(mockContext.logger?.warn).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { | ||
Endpoint, | ||
EndpointV2, | ||
FinalizeHandlerArguments, | ||
FinalizeHandlerOutput, | ||
HandlerExecutionContext, | ||
MiddlewareStack, | ||
Pluggable, | ||
Provider, | ||
RelativeMiddlewareOptions, | ||
} from "@aws-sdk/types"; | ||
import { NoOpLogger } from "@smithy/smithy-client"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface QueueUrlInputConfig { | ||
/** | ||
* In cases where a QueueUrl is given as input, that | ||
* will be preferred as the request endpoint. | ||
* | ||
* Set this value to false to ignore the QueueUrl and use the | ||
* client's resolved endpoint, which may be a custom endpoint. | ||
*/ | ||
useQueueUrlAsEndpoint?: boolean; | ||
} | ||
|
||
export interface QueueUrlResolvedConfig { | ||
useQueueUrlAsEndpoint: boolean; | ||
} | ||
|
||
export interface PreviouslyResolved { | ||
endpoint?: string | Endpoint | Provider<Endpoint> | EndpointV2 | Provider<EndpointV2>; | ||
} | ||
|
||
export const resolveQueueUrlConfig = <T>( | ||
config: T & PreviouslyResolved & QueueUrlInputConfig | ||
): T & QueueUrlResolvedConfig => { | ||
return { | ||
...config, | ||
useQueueUrlAsEndpoint: config.useQueueUrlAsEndpoint ?? true, | ||
}; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function queueUrlMiddleware({ useQueueUrlAsEndpoint, endpoint }: QueueUrlResolvedConfig & PreviouslyResolved) { | ||
return <Output extends object>( | ||
next: (args: FinalizeHandlerArguments<any>) => Promise<FinalizeHandlerOutput<Output>>, | ||
context: HandlerExecutionContext | ||
): ((args: FinalizeHandlerArguments<any>) => Promise<FinalizeHandlerOutput<Output>>) => { | ||
return async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<Output>> => { | ||
const { input } = args; | ||
const resolvedEndpoint = context.endpointV2; | ||
|
||
if (!endpoint && input.QueueUrl && resolvedEndpoint && useQueueUrlAsEndpoint) { | ||
const logger = context.logger instanceof NoOpLogger || !context.logger?.warn ? console : context.logger; | ||
try { | ||
const queueUrl: URL = new URL(input.QueueUrl); | ||
const queueUrlOrigin: URL = new URL(queueUrl.origin); | ||
if (resolvedEndpoint.url.origin !== queueUrlOrigin.origin) { | ||
logger.warn( | ||
`QueueUrl=${ | ||
input.QueueUrl | ||
} differs from SQSClient resolved endpoint=${resolvedEndpoint.url.toString()}, using QueueUrl host as endpoint. | ||
Set [endpoint=string] or [useQueueUrlAsEndpoint=false] on the SQSClient.` | ||
); | ||
resolvedEndpoint.url = queueUrlOrigin; | ||
} | ||
} catch (e: unknown) { | ||
logger.warn(e); | ||
} | ||
} | ||
return next(args); | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const queueUrlMiddlewareOptions: RelativeMiddlewareOptions = { | ||
name: "queueUrlMiddleware", | ||
relation: "after", | ||
toMiddleware: "endpointV2Middleware", | ||
override: true, | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const getQueueUrlPlugin = (config: QueueUrlResolvedConfig): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack: MiddlewareStack<any, any>) => { | ||
clientStack.addRelativeTo(queueUrlMiddleware(config), queueUrlMiddlewareOptions); | ||
}, | ||
}); |