|
| 1 | +import { |
| 2 | + ENV_CMDS_AUTH_TOKEN, |
| 3 | + ENV_CMDS_FULL_URI, |
| 4 | + ENV_CMDS_RELATIVE_URI, |
| 5 | + fromContainerMetadata |
| 6 | +} from "../lib/fromContainerMetadata"; |
| 7 | +import { httpGet } from "../lib/remoteProvider/httpGet"; |
| 8 | +import { |
| 9 | + fromImdsCredentials, |
| 10 | + ImdsCredentials |
| 11 | +} from "../lib/remoteProvider/ImdsCredentials"; |
| 12 | +import MockInstance = jest.MockInstance; |
| 13 | +import { RequestOptions } from "http"; |
| 14 | + |
| 15 | +interface HttpGet { |
| 16 | + (options: RequestOptions): Promise<Buffer>; |
| 17 | +} |
| 18 | + |
| 19 | +const mockHttpGet = <MockInstance<HttpGet>>(<any>httpGet); |
| 20 | +jest.mock("../lib/remoteProvider/httpGet", () => ({ httpGet: jest.fn() })); |
| 21 | + |
| 22 | +const relativeUri = process.env[ENV_CMDS_RELATIVE_URI]; |
| 23 | +const fullUri = process.env[ENV_CMDS_FULL_URI]; |
| 24 | +const authToken = process.env[ENV_CMDS_AUTH_TOKEN]; |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + mockHttpGet.mockReset(); |
| 28 | + delete process.env[ENV_CMDS_RELATIVE_URI]; |
| 29 | + delete process.env[ENV_CMDS_FULL_URI]; |
| 30 | + delete process.env[ENV_CMDS_AUTH_TOKEN]; |
| 31 | +}); |
| 32 | + |
| 33 | +afterAll(() => { |
| 34 | + process.env[ENV_CMDS_RELATIVE_URI] = relativeUri; |
| 35 | + process.env[ENV_CMDS_FULL_URI] = fullUri; |
| 36 | + process.env[ENV_CMDS_AUTH_TOKEN] = authToken; |
| 37 | +}); |
| 38 | + |
| 39 | +describe("fromContainerMetadata", () => { |
| 40 | + const creds: ImdsCredentials = Object.freeze({ |
| 41 | + AccessKeyId: "foo", |
| 42 | + SecretAccessKey: "bar", |
| 43 | + Token: "baz", |
| 44 | + Expiration: new Date().toISOString() |
| 45 | + }); |
| 46 | + |
| 47 | + it("should reject the promise with a terminal error if the container credentials environment variable is not set", async () => { |
| 48 | + await fromContainerMetadata()().then( |
| 49 | + () => { |
| 50 | + throw new Error("The promise should have been rejected"); |
| 51 | + }, |
| 52 | + err => { |
| 53 | + expect((err as any).tryNextLink).toBeFalsy(); |
| 54 | + } |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it(`should inject an authorization header containing the contents of the ${ENV_CMDS_AUTH_TOKEN} environment variable if defined`, async () => { |
| 59 | + const token = "Basic abcd"; |
| 60 | + process.env[ENV_CMDS_FULL_URI] = "http://localhost:8080/path"; |
| 61 | + process.env[ENV_CMDS_AUTH_TOKEN] = token; |
| 62 | + mockHttpGet.mockReturnValue(Promise.resolve(JSON.stringify(creds))); |
| 63 | + |
| 64 | + await fromContainerMetadata()(); |
| 65 | + |
| 66 | + expect(mockHttpGet.mock.calls.length).toBe(1); |
| 67 | + const [options = {}] = mockHttpGet.mock.calls[0]; |
| 68 | + expect(options.headers).toMatchObject({ |
| 69 | + Authorization: token |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe(ENV_CMDS_RELATIVE_URI, () => { |
| 74 | + beforeEach(() => { |
| 75 | + process.env[ENV_CMDS_RELATIVE_URI] = "/relative/uri"; |
| 76 | + }); |
| 77 | + |
| 78 | + it("should resolve credentials by fetching them from the container metadata service", async () => { |
| 79 | + mockHttpGet.mockReturnValue(Promise.resolve(JSON.stringify(creds))); |
| 80 | + |
| 81 | + expect(await fromContainerMetadata()()).toEqual( |
| 82 | + fromImdsCredentials(creds) |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should retry the fetching operation up to maxRetries times", async () => { |
| 87 | + const maxRetries = 5; |
| 88 | + for (let i = 0; i < maxRetries - 1; i++) { |
| 89 | + mockHttpGet.mockReturnValueOnce(Promise.reject("No!")); |
| 90 | + } |
| 91 | + mockHttpGet.mockReturnValueOnce(Promise.resolve(JSON.stringify(creds))); |
| 92 | + |
| 93 | + expect(await fromContainerMetadata({ maxRetries })()).toEqual( |
| 94 | + fromImdsCredentials(creds) |
| 95 | + ); |
| 96 | + expect(mockHttpGet.mock.calls.length).toEqual(maxRetries); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should retry responses that receive invalid response values", async () => { |
| 100 | + for (let key of Object.keys(creds)) { |
| 101 | + const invalidCreds: any = { ...creds }; |
| 102 | + delete invalidCreds[key]; |
| 103 | + mockHttpGet.mockReturnValueOnce( |
| 104 | + Promise.resolve(JSON.stringify(invalidCreds)) |
| 105 | + ); |
| 106 | + } |
| 107 | + mockHttpGet.mockReturnValueOnce(Promise.resolve(JSON.stringify(creds))); |
| 108 | + |
| 109 | + await fromContainerMetadata({ maxRetries: 100 })(); |
| 110 | + expect(mockHttpGet.mock.calls.length).toEqual( |
| 111 | + Object.keys(creds).length + 1 |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should pass relevant configuration to httpGet", async () => { |
| 116 | + const timeout = Math.ceil(Math.random() * 1000); |
| 117 | + mockHttpGet.mockReturnValue(Promise.resolve(JSON.stringify(creds))); |
| 118 | + await fromContainerMetadata({ timeout })(); |
| 119 | + expect(mockHttpGet.mock.calls.length).toEqual(1); |
| 120 | + expect(mockHttpGet.mock.calls[0][0]).toEqual({ |
| 121 | + hostname: "169.254.170.2", |
| 122 | + path: process.env[ENV_CMDS_RELATIVE_URI], |
| 123 | + timeout |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe(ENV_CMDS_FULL_URI, () => { |
| 129 | + it("should pass relevant configuration to httpGet", async () => { |
| 130 | + process.env[ENV_CMDS_FULL_URI] = "http://localhost:8080/path"; |
| 131 | + |
| 132 | + const timeout = Math.ceil(Math.random() * 1000); |
| 133 | + mockHttpGet.mockReturnValue(Promise.resolve(JSON.stringify(creds))); |
| 134 | + await fromContainerMetadata({ timeout })(); |
| 135 | + expect(mockHttpGet.mock.calls.length).toEqual(1); |
| 136 | + const { |
| 137 | + protocol, |
| 138 | + hostname, |
| 139 | + path, |
| 140 | + port, |
| 141 | + timeout: actualTimeout |
| 142 | + } = mockHttpGet.mock.calls[0][0]; |
| 143 | + expect(protocol).toBe("http:"); |
| 144 | + expect(hostname).toBe("localhost"); |
| 145 | + expect(path).toBe("/path"); |
| 146 | + expect(port).toBe(8080); |
| 147 | + expect(actualTimeout).toBe(timeout); |
| 148 | + }); |
| 149 | + |
| 150 | + it(`should prefer ${ENV_CMDS_RELATIVE_URI} to ${ENV_CMDS_FULL_URI}`, async () => { |
| 151 | + process.env[ENV_CMDS_RELATIVE_URI] = "foo"; |
| 152 | + process.env[ENV_CMDS_FULL_URI] = "http://localhost:8080/path"; |
| 153 | + |
| 154 | + const timeout = Math.ceil(Math.random() * 1000); |
| 155 | + mockHttpGet.mockReturnValue(Promise.resolve(JSON.stringify(creds))); |
| 156 | + await fromContainerMetadata({ timeout })(); |
| 157 | + expect(mockHttpGet.mock.calls.length).toEqual(1); |
| 158 | + expect(mockHttpGet.mock.calls[0][0]).toEqual({ |
| 159 | + hostname: "169.254.170.2", |
| 160 | + path: "foo", |
| 161 | + timeout |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it("should reject the promise with a terminal error if a unexpected protocol is specified", async () => { |
| 166 | + process.env[ENV_CMDS_FULL_URI] = "wss://localhost:8080/path"; |
| 167 | + |
| 168 | + await fromContainerMetadata()().then( |
| 169 | + () => { |
| 170 | + throw new Error("The promise should have been rejected"); |
| 171 | + }, |
| 172 | + err => { |
| 173 | + expect((err as any).tryNextLink).toBeFalsy(); |
| 174 | + } |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + it("should reject the promise with a terminal error if a unexpected hostname is specified", async () => { |
| 179 | + process.env[ENV_CMDS_FULL_URI] = "https://bucket.s3.amazonaws.com/key"; |
| 180 | + |
| 181 | + await fromContainerMetadata()().then( |
| 182 | + () => { |
| 183 | + throw new Error("The promise should have been rejected"); |
| 184 | + }, |
| 185 | + err => { |
| 186 | + expect((err as any).tryNextLink).toBeFalsy(); |
| 187 | + } |
| 188 | + ); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments