|
| 1 | +import { STS } from "@aws-sdk/client-sts"; |
| 2 | +import { HttpRequest, HttpResponse } from "@smithy/protocol-http"; |
| 3 | +import { SourceProfileInit } from "@smithy/shared-ini-file-loader"; |
| 4 | +import type { NodeHttpHandlerOptions, ParsedIniData } from "@smithy/types"; |
| 5 | +import { PassThrough } from "node:stream"; |
| 6 | +import { beforeEach, describe, expect, test as it, vi } from "vitest"; |
| 7 | + |
| 8 | +import { fromIni } from "./fromIni"; |
| 9 | + |
| 10 | +let iniProfileData: ParsedIniData = null as any; |
| 11 | +vi.mock("@smithy/shared-ini-file-loader", async () => { |
| 12 | + const actual: any = await vi.importActual("@smithy/shared-ini-file-loader"); |
| 13 | + const pkg = { |
| 14 | + ...actual, |
| 15 | + async loadSsoSessionData() { |
| 16 | + return Object.entries(iniProfileData) |
| 17 | + .filter(([key]) => key.startsWith("sso-session.")) |
| 18 | + .reduce( |
| 19 | + (acc, [key, value]) => ({ |
| 20 | + ...acc, |
| 21 | + [key.split("sso-session.")[1]]: value, |
| 22 | + }), |
| 23 | + {} |
| 24 | + ); |
| 25 | + }, |
| 26 | + async parseKnownFiles(init: SourceProfileInit): Promise<ParsedIniData> { |
| 27 | + return iniProfileData; |
| 28 | + }, |
| 29 | + async getSSOTokenFromFile() { |
| 30 | + return { |
| 31 | + accessToken: "mock_sso_token", |
| 32 | + expiresAt: "3000-01-01T00:00:00.000Z", |
| 33 | + }; |
| 34 | + }, |
| 35 | + }; |
| 36 | + return { |
| 37 | + ...pkg, |
| 38 | + default: pkg, |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +class MockNodeHttpHandler { |
| 43 | + static create(instanceOrOptions?: any) { |
| 44 | + if (typeof instanceOrOptions?.handle === "function") { |
| 45 | + return instanceOrOptions; |
| 46 | + } |
| 47 | + return new MockNodeHttpHandler(); |
| 48 | + } |
| 49 | + async handle(request: HttpRequest) { |
| 50 | + const body = new PassThrough({}); |
| 51 | + |
| 52 | + const region = (request.hostname.match(/sts\.(.*?)\./) || [, "unknown"])[1]; |
| 53 | + |
| 54 | + if (request.headers.Authorization === "container-authorization") { |
| 55 | + body.write( |
| 56 | + JSON.stringify({ |
| 57 | + AccessKeyId: "CONTAINER_ACCESS_KEY", |
| 58 | + SecretAccessKey: "CONTAINER_SECRET_ACCESS_KEY", |
| 59 | + Token: "CONTAINER_TOKEN", |
| 60 | + Expiration: "3000-01-01T00:00:00.000Z", |
| 61 | + }) |
| 62 | + ); |
| 63 | + } else if (request.path?.includes("/federation/credentials")) { |
| 64 | + body.write( |
| 65 | + JSON.stringify({ |
| 66 | + roleCredentials: { |
| 67 | + accessKeyId: "SSO_ACCESS_KEY_ID", |
| 68 | + secretAccessKey: "SSO_SECRET_ACCESS_KEY", |
| 69 | + sessionToken: "SSO_SESSION_TOKEN", |
| 70 | + expiration: "3000-01-01T00:00:00.000Z", |
| 71 | + }, |
| 72 | + }) |
| 73 | + ); |
| 74 | + } else if (request.body?.includes("Action=AssumeRoleWithWebIdentity")) { |
| 75 | + body.write(` |
| 76 | +<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> |
| 77 | +<AssumeRoleWithWebIdentityResult> |
| 78 | +<Credentials> |
| 79 | + <AccessKeyId>STS_ARWI_ACCESS_KEY_ID</AccessKeyId> |
| 80 | + <SecretAccessKey>STS_ARWI_SECRET_ACCESS_KEY</SecretAccessKey> |
| 81 | + <SessionToken>STS_ARWI_SESSION_TOKEN_${region}</SessionToken> |
| 82 | + <Expiration>3000-01-01T00:00:00.000Z</Expiration> |
| 83 | +</Credentials> |
| 84 | +</AssumeRoleWithWebIdentityResult> |
| 85 | +<ResponseMetadata> |
| 86 | +<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> |
| 87 | +</ResponseMetadata> |
| 88 | +</AssumeRoleWithWebIdentityResponse>`); |
| 89 | + } else if (request.body?.includes("Action=AssumeRole")) { |
| 90 | + body.write(` |
| 91 | +<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> |
| 92 | +<AssumeRoleResult> |
| 93 | +<Credentials> |
| 94 | + <AccessKeyId>STS_AR_ACCESS_KEY_ID</AccessKeyId> |
| 95 | + <SecretAccessKey>STS_AR_SECRET_ACCESS_KEY</SecretAccessKey> |
| 96 | + <SessionToken>STS_AR_SESSION_TOKEN_${region}</SessionToken> |
| 97 | + <Expiration>3000-01-01T00:00:00.000Z</Expiration> |
| 98 | +</Credentials> |
| 99 | +</AssumeRoleResult> |
| 100 | +<ResponseMetadata> |
| 101 | +<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> |
| 102 | +</ResponseMetadata> |
| 103 | +</AssumeRoleResponse>`); |
| 104 | + } else if (request.body.includes("Action=GetCallerIdentity")) { |
| 105 | + body.write(` |
| 106 | +<GetCallerIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> |
| 107 | +<GetCallerIdentityResult> |
| 108 | +<Arn>arn:aws:iam::123456789012:user/Alice</Arn> |
| 109 | +<UserId>AIDACKCEVSQ6C2EXAMPLE</UserId> |
| 110 | +<Account>123456789012</Account> |
| 111 | +</GetCallerIdentityResult> |
| 112 | +<ResponseMetadata> |
| 113 | +<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> |
| 114 | +</ResponseMetadata> |
| 115 | +</GetCallerIdentityResponse>`); |
| 116 | + } else { |
| 117 | + throw new Error("request not supported."); |
| 118 | + } |
| 119 | + body.end(); |
| 120 | + return { |
| 121 | + response: new HttpResponse({ |
| 122 | + statusCode: 200, |
| 123 | + body, |
| 124 | + headers: {}, |
| 125 | + }), |
| 126 | + }; |
| 127 | + } |
| 128 | + updateHttpClientConfig(key: keyof NodeHttpHandlerOptions, value: NodeHttpHandlerOptions[typeof key]): void {} |
| 129 | + httpHandlerConfigs(): NodeHttpHandlerOptions { |
| 130 | + return null as any; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +describe("fromIni region search order", () => { |
| 135 | + beforeEach(() => { |
| 136 | + iniProfileData = { |
| 137 | + default: { |
| 138 | + region: "us-west-2", |
| 139 | + output: "json", |
| 140 | + }, |
| 141 | + }; |
| 142 | + iniProfileData.assume = { |
| 143 | + region: "us-stsar-1", |
| 144 | + aws_access_key_id: "ASSUME_STATIC_ACCESS_KEY", |
| 145 | + aws_secret_access_key: "ASSUME_STATIC_SECRET_KEY", |
| 146 | + }; |
| 147 | + Object.assign(iniProfileData.default, { |
| 148 | + region: "us-stsar-1", |
| 149 | + role_arn: "ROLE_ARN", |
| 150 | + role_session_name: "ROLE_SESSION_NAME", |
| 151 | + external_id: "EXTERNAL_ID", |
| 152 | + source_profile: "assume", |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should use 1st priority for the clientConfig given to the provider factory", async () => { |
| 157 | + const sts = new STS({ |
| 158 | + requestHandler: new MockNodeHttpHandler(), |
| 159 | + region: "ap-northeast-2", |
| 160 | + credentials: fromIni({ |
| 161 | + clientConfig: { |
| 162 | + requestHandler: new MockNodeHttpHandler(), |
| 163 | + region: "ap-northeast-1", |
| 164 | + }, |
| 165 | + }), |
| 166 | + }); |
| 167 | + |
| 168 | + await sts.getCallerIdentity({}); |
| 169 | + const credentials = await sts.config.credentials(); |
| 170 | + expect(credentials).toContain({ |
| 171 | + accessKeyId: "STS_AR_ACCESS_KEY_ID", |
| 172 | + secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", |
| 173 | + sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-1", |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should use 2nd priority for the context client", async () => { |
| 178 | + const sts = new STS({ |
| 179 | + requestHandler: new MockNodeHttpHandler(), |
| 180 | + region: "ap-northeast-2", |
| 181 | + credentials: fromIni({ |
| 182 | + clientConfig: { |
| 183 | + requestHandler: new MockNodeHttpHandler(), |
| 184 | + }, |
| 185 | + }), |
| 186 | + }); |
| 187 | + |
| 188 | + await sts.getCallerIdentity({}); |
| 189 | + const credentials = await sts.config.credentials(); |
| 190 | + expect(credentials).toContain({ |
| 191 | + accessKeyId: "STS_AR_ACCESS_KEY_ID", |
| 192 | + secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", |
| 193 | + sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-2", |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + it("should use 3rd priority for the profile region if not used in the context of a client with a region", async () => { |
| 198 | + const credentialsData = await fromIni({ |
| 199 | + clientConfig: { |
| 200 | + requestHandler: new MockNodeHttpHandler(), |
| 201 | + }, |
| 202 | + })(); |
| 203 | + |
| 204 | + const sts = new STS({ |
| 205 | + requestHandler: new MockNodeHttpHandler(), |
| 206 | + region: "ap-northeast-2", |
| 207 | + credentials: credentialsData, |
| 208 | + }); |
| 209 | + |
| 210 | + await sts.getCallerIdentity({}); |
| 211 | + const credentials = await sts.config.credentials(); |
| 212 | + expect(credentials).toContain({ |
| 213 | + accessKeyId: "STS_AR_ACCESS_KEY_ID", |
| 214 | + secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", |
| 215 | + sessionToken: "STS_AR_SESSION_TOKEN_us-stsar-1", |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + it("should use 4th priority for the default partition's default region", async () => { |
| 220 | + delete iniProfileData.default.region; |
| 221 | + |
| 222 | + const credentialsData = await fromIni({ |
| 223 | + clientConfig: { |
| 224 | + requestHandler: new MockNodeHttpHandler(), |
| 225 | + }, |
| 226 | + })(); |
| 227 | + |
| 228 | + const sts = new STS({ |
| 229 | + requestHandler: new MockNodeHttpHandler(), |
| 230 | + region: "ap-northeast-2", |
| 231 | + credentials: credentialsData, |
| 232 | + }); |
| 233 | + |
| 234 | + await sts.getCallerIdentity({}); |
| 235 | + const credentials = await sts.config.credentials(); |
| 236 | + expect(credentials).toContain({ |
| 237 | + accessKeyId: "STS_AR_ACCESS_KEY_ID", |
| 238 | + secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", |
| 239 | + sessionToken: "STS_AR_SESSION_TOKEN_us-east-1", |
| 240 | + }); |
| 241 | + }); |
| 242 | +}); |
0 commit comments