-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(credential-provider-node): additional integ tests for cognito
- Loading branch information
Showing
9 changed files
with
418 additions
and
60 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
242 changes: 242 additions & 0 deletions
242
packages/credential-provider-ini/src/fromIni.integ.spec.ts
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,242 @@ | ||
import { STS } from "@aws-sdk/client-sts"; | ||
import { HttpRequest, HttpResponse } from "@smithy/protocol-http"; | ||
import { SourceProfileInit } from "@smithy/shared-ini-file-loader"; | ||
import type { NodeHttpHandlerOptions, ParsedIniData } from "@smithy/types"; | ||
import { PassThrough } from "node:stream"; | ||
import { beforeEach, describe, expect, test as it, vi } from "vitest"; | ||
|
||
import { fromIni } from "./fromIni"; | ||
|
||
let iniProfileData: ParsedIniData = null as any; | ||
vi.mock("@smithy/shared-ini-file-loader", async () => { | ||
const actual: any = await vi.importActual("@smithy/shared-ini-file-loader"); | ||
const pkg = { | ||
...actual, | ||
async loadSsoSessionData() { | ||
return Object.entries(iniProfileData) | ||
.filter(([key]) => key.startsWith("sso-session.")) | ||
.reduce( | ||
(acc, [key, value]) => ({ | ||
...acc, | ||
[key.split("sso-session.")[1]]: value, | ||
}), | ||
{} | ||
); | ||
}, | ||
async parseKnownFiles(init: SourceProfileInit): Promise<ParsedIniData> { | ||
return iniProfileData; | ||
}, | ||
async getSSOTokenFromFile() { | ||
return { | ||
accessToken: "mock_sso_token", | ||
expiresAt: "3000-01-01T00:00:00.000Z", | ||
}; | ||
}, | ||
}; | ||
return { | ||
...pkg, | ||
default: pkg, | ||
}; | ||
}); | ||
|
||
class MockNodeHttpHandler { | ||
static create(instanceOrOptions?: any) { | ||
if (typeof instanceOrOptions?.handle === "function") { | ||
return instanceOrOptions; | ||
} | ||
return new MockNodeHttpHandler(); | ||
} | ||
async handle(request: HttpRequest) { | ||
const body = new PassThrough({}); | ||
|
||
const region = (request.hostname.match(/sts\.(.*?)\./) || [, "unknown"])[1]; | ||
|
||
if (request.headers.Authorization === "container-authorization") { | ||
body.write( | ||
JSON.stringify({ | ||
AccessKeyId: "CONTAINER_ACCESS_KEY", | ||
SecretAccessKey: "CONTAINER_SECRET_ACCESS_KEY", | ||
Token: "CONTAINER_TOKEN", | ||
Expiration: "3000-01-01T00:00:00.000Z", | ||
}) | ||
); | ||
} else if (request.path?.includes("/federation/credentials")) { | ||
body.write( | ||
JSON.stringify({ | ||
roleCredentials: { | ||
accessKeyId: "SSO_ACCESS_KEY_ID", | ||
secretAccessKey: "SSO_SECRET_ACCESS_KEY", | ||
sessionToken: "SSO_SESSION_TOKEN", | ||
expiration: "3000-01-01T00:00:00.000Z", | ||
}, | ||
}) | ||
); | ||
} else if (request.body?.includes("Action=AssumeRoleWithWebIdentity")) { | ||
body.write(` | ||
<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> | ||
<AssumeRoleWithWebIdentityResult> | ||
<Credentials> | ||
<AccessKeyId>STS_ARWI_ACCESS_KEY_ID</AccessKeyId> | ||
<SecretAccessKey>STS_ARWI_SECRET_ACCESS_KEY</SecretAccessKey> | ||
<SessionToken>STS_ARWI_SESSION_TOKEN_${region}</SessionToken> | ||
<Expiration>3000-01-01T00:00:00.000Z</Expiration> | ||
</Credentials> | ||
</AssumeRoleWithWebIdentityResult> | ||
<ResponseMetadata> | ||
<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> | ||
</ResponseMetadata> | ||
</AssumeRoleWithWebIdentityResponse>`); | ||
} else if (request.body?.includes("Action=AssumeRole")) { | ||
body.write(` | ||
<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> | ||
<AssumeRoleResult> | ||
<Credentials> | ||
<AccessKeyId>STS_AR_ACCESS_KEY_ID</AccessKeyId> | ||
<SecretAccessKey>STS_AR_SECRET_ACCESS_KEY</SecretAccessKey> | ||
<SessionToken>STS_AR_SESSION_TOKEN_${region}</SessionToken> | ||
<Expiration>3000-01-01T00:00:00.000Z</Expiration> | ||
</Credentials> | ||
</AssumeRoleResult> | ||
<ResponseMetadata> | ||
<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> | ||
</ResponseMetadata> | ||
</AssumeRoleResponse>`); | ||
} else if (request.body.includes("Action=GetCallerIdentity")) { | ||
body.write(` | ||
<GetCallerIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> | ||
<GetCallerIdentityResult> | ||
<Arn>arn:aws:iam::123456789012:user/Alice</Arn> | ||
<UserId>AIDACKCEVSQ6C2EXAMPLE</UserId> | ||
<Account>123456789012</Account> | ||
</GetCallerIdentityResult> | ||
<ResponseMetadata> | ||
<RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> | ||
</ResponseMetadata> | ||
</GetCallerIdentityResponse>`); | ||
} else { | ||
throw new Error("request not supported."); | ||
} | ||
body.end(); | ||
return { | ||
response: new HttpResponse({ | ||
statusCode: 200, | ||
body, | ||
headers: {}, | ||
}), | ||
}; | ||
} | ||
updateHttpClientConfig(key: keyof NodeHttpHandlerOptions, value: NodeHttpHandlerOptions[typeof key]): void {} | ||
httpHandlerConfigs(): NodeHttpHandlerOptions { | ||
return null as any; | ||
} | ||
} | ||
|
||
describe("fromIni region search order", () => { | ||
beforeEach(() => { | ||
iniProfileData = { | ||
default: { | ||
region: "us-west-2", | ||
output: "json", | ||
}, | ||
}; | ||
iniProfileData.assume = { | ||
region: "us-stsar-1", | ||
aws_access_key_id: "ASSUME_STATIC_ACCESS_KEY", | ||
aws_secret_access_key: "ASSUME_STATIC_SECRET_KEY", | ||
}; | ||
Object.assign(iniProfileData.default, { | ||
region: "us-stsar-1", | ||
role_arn: "ROLE_ARN", | ||
role_session_name: "ROLE_SESSION_NAME", | ||
external_id: "EXTERNAL_ID", | ||
source_profile: "assume", | ||
}); | ||
}); | ||
|
||
it("should use 1st priority for the clientConfig given to the provider factory", async () => { | ||
const sts = new STS({ | ||
requestHandler: new MockNodeHttpHandler(), | ||
region: "ap-northeast-2", | ||
credentials: fromIni({ | ||
clientConfig: { | ||
requestHandler: new MockNodeHttpHandler(), | ||
region: "ap-northeast-1", | ||
}, | ||
}), | ||
}); | ||
|
||
await sts.getCallerIdentity({}); | ||
const credentials = await sts.config.credentials(); | ||
expect(credentials).toContain({ | ||
accessKeyId: "STS_AR_ACCESS_KEY_ID", | ||
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", | ||
sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-1", | ||
}); | ||
}); | ||
|
||
it("should use 2nd priority for the context client", async () => { | ||
const sts = new STS({ | ||
requestHandler: new MockNodeHttpHandler(), | ||
region: "ap-northeast-2", | ||
credentials: fromIni({ | ||
clientConfig: { | ||
requestHandler: new MockNodeHttpHandler(), | ||
}, | ||
}), | ||
}); | ||
|
||
await sts.getCallerIdentity({}); | ||
const credentials = await sts.config.credentials(); | ||
expect(credentials).toContain({ | ||
accessKeyId: "STS_AR_ACCESS_KEY_ID", | ||
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", | ||
sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-2", | ||
}); | ||
}); | ||
|
||
it("should use 3rd priority for the profile region if not used in the context of a client with a region", async () => { | ||
const credentialsData = await fromIni({ | ||
clientConfig: { | ||
requestHandler: new MockNodeHttpHandler(), | ||
}, | ||
})(); | ||
|
||
const sts = new STS({ | ||
requestHandler: new MockNodeHttpHandler(), | ||
region: "ap-northeast-2", | ||
credentials: credentialsData, | ||
}); | ||
|
||
await sts.getCallerIdentity({}); | ||
const credentials = await sts.config.credentials(); | ||
expect(credentials).toContain({ | ||
accessKeyId: "STS_AR_ACCESS_KEY_ID", | ||
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", | ||
sessionToken: "STS_AR_SESSION_TOKEN_us-stsar-1", | ||
}); | ||
}); | ||
|
||
it("should use 4th priority for the default partition's default region", async () => { | ||
delete iniProfileData.default.region; | ||
|
||
const credentialsData = await fromIni({ | ||
clientConfig: { | ||
requestHandler: new MockNodeHttpHandler(), | ||
}, | ||
})(); | ||
|
||
const sts = new STS({ | ||
requestHandler: new MockNodeHttpHandler(), | ||
region: "ap-northeast-2", | ||
credentials: credentialsData, | ||
}); | ||
|
||
await sts.getCallerIdentity({}); | ||
const credentials = await sts.config.credentials(); | ||
expect(credentials).toContain({ | ||
accessKeyId: "STS_AR_ACCESS_KEY_ID", | ||
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY", | ||
sessionToken: "STS_AR_SESSION_TOKEN_us-east-1", | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: ["**/*.integ.spec.ts"], | ||
environment: "node", | ||
}, | ||
}); |
Oops, something went wrong.