From 122c13976311de921155dc62f6ccd07b139c274a Mon Sep 17 00:00:00 2001 From: Sam Chung Date: Fri, 18 Jun 2021 04:58:00 +1000 Subject: [PATCH] fix(credential-provider-imds): destroy request handle on promise resolve/reject (#2452) Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> --- .../src/remoteProvider/httpRequest.spec.ts | 12 +++++++++++- .../src/remoteProvider/httpRequest.ts | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/credential-provider-imds/src/remoteProvider/httpRequest.spec.ts b/packages/credential-provider-imds/src/remoteProvider/httpRequest.spec.ts index d44fa87e835d..d5ec58c9aded 100644 --- a/packages/credential-provider-imds/src/remoteProvider/httpRequest.spec.ts +++ b/packages/credential-provider-imds/src/remoteProvider/httpRequest.spec.ts @@ -1,10 +1,11 @@ import { ProviderError } from "@aws-sdk/property-provider"; -import { createServer } from "http"; +import http, { createServer } from "http"; import nock from "nock"; import { httpRequest } from "./httpRequest"; describe("httpRequest", () => { + const requestSpy = jest.spyOn(http, "request"); let port: number; const host = "localhost"; const path = "/"; @@ -26,6 +27,10 @@ describe("httpRequest", () => { port = await getOpenPort(); }); + afterEach(() => { + jest.clearAllMocks(); + }); + describe("returns response", () => { it("defaults to method GET", async () => { const expectedResponse = "expectedResponse"; @@ -33,6 +38,7 @@ describe("httpRequest", () => { const response = await httpRequest({ host, path, port }); expect(response.toString()).toStrictEqual(expectedResponse); + expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true); scope.done(); }); @@ -44,6 +50,7 @@ describe("httpRequest", () => { const response = await httpRequest({ host, path, port, method }); expect(response.toString()).toStrictEqual(expectedResponse); + expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true); scope.done(); }); @@ -57,6 +64,7 @@ describe("httpRequest", () => { await expect(httpRequest({ host, path, port })).rejects.toStrictEqual( Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode }) ); + expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true); scope.done(); }); @@ -68,6 +76,7 @@ describe("httpRequest", () => { await expect(httpRequest({ host, path, port })).rejects.toStrictEqual( new ProviderError("Unable to connect to instance metadata service") ); + expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true); scope.done(); }); @@ -91,6 +100,7 @@ describe("httpRequest", () => { await expect(httpRequest({ host, path, port, timeout })).rejects.toStrictEqual( new ProviderError("TimeoutError from instance metadata service") ); + expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true); scope.done(); }); diff --git a/packages/credential-provider-imds/src/remoteProvider/httpRequest.ts b/packages/credential-provider-imds/src/remoteProvider/httpRequest.ts index 7de540e7f80a..8104aa3457d4 100644 --- a/packages/credential-provider-imds/src/remoteProvider/httpRequest.ts +++ b/packages/credential-provider-imds/src/remoteProvider/httpRequest.ts @@ -11,10 +11,12 @@ export function httpRequest(options: RequestOptions): Promise { req.on("error", (err) => { reject(Object.assign(new ProviderError("Unable to connect to instance metadata service"), err)); + req.destroy(); }); req.on("timeout", () => { reject(new ProviderError("TimeoutError from instance metadata service")); + req.destroy(); }); req.on("response", (res: IncomingMessage) => { @@ -23,6 +25,7 @@ export function httpRequest(options: RequestOptions): Promise { reject( Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode }) ); + req.destroy(); } const chunks: Array = []; @@ -31,6 +34,7 @@ export function httpRequest(options: RequestOptions): Promise { }); res.on("end", () => { resolve(Buffer.concat(chunks)); + req.destroy(); }); });