Skip to content

Commit 596fed0

Browse files
docs(client-sts): defaultRoleAssumers should not be edited directly (#2360)
Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 4904f6a commit 596fed0

File tree

7 files changed

+89
-6
lines changed

7 files changed

+89
-6
lines changed

clients/client-sts/defaultRoleAssumers.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Please do not touch this file. It's generated from template in:
2+
// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultRoleAssumers.spec.ts
13
import { HttpResponse } from "@aws-sdk/protocol-http";
24
import { Readable } from "stream";
35
const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">

clients/client-sts/defaultRoleAssumers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Please do not touch this file. It's generated from template in:
2+
// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultRoleAssumers.ts
13
import {
24
DefaultCredentialProvider,
35
getDefaultRoleAssumer as StsGetDefaultRoleAssumer,

clients/client-sts/defaultStsRoleAssumers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Please do not touch this file. It's generated from template in:
2+
// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultStsRoleAssumers.ts
13
import { Credentials, Provider } from "@aws-sdk/types";
24

35
import { AssumeRoleCommand, AssumeRoleCommandInput } from "./commands/AssumeRoleCommand";

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
public final class AddAwsAuthPlugin implements TypeScriptIntegration {
5454
static final String STS_CLIENT_PREFIX = "sts-client-";
5555
static final String ROLE_ASSUMERS_FILE = "defaultRoleAssumers";
56+
static final String ROLE_ASSUMERS_TEST_FILE = "defaultRoleAssumers.spec";
5657
static final String STS_ROLE_ASSUMERS_FILE = "defaultStsRoleAssumers";
5758

5859
@Override
@@ -155,14 +156,25 @@ public void writeAdditionalFiles(
155156
if (!testServiceId(service, "STS")) {
156157
return;
157158
}
159+
String noTouchNoticePrefix = "// Please do not touch this file. It's generated from template in:\n"
160+
+ "// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/"
161+
+ "src/main/resources/software/amazon/smithy/aws/typescript/codegen/";
158162
writerFactory.accept("defaultRoleAssumers.ts", writer -> {
159-
String source = IoUtils.readUtf8Resource(getClass(),
160-
String.format("%s%s.ts", STS_CLIENT_PREFIX, ROLE_ASSUMERS_FILE));
163+
String resourceName = String.format("%s%s.ts", STS_CLIENT_PREFIX, ROLE_ASSUMERS_FILE);
164+
String source = IoUtils.readUtf8Resource(getClass(), resourceName);
165+
writer.write("$L$L", noTouchNoticePrefix, resourceName);
161166
writer.write("$L", source);
162167
});
163168
writerFactory.accept("defaultStsRoleAssumers.ts", writer -> {
164-
String source = IoUtils.readUtf8Resource(getClass(),
165-
String.format("%s%s.ts", STS_CLIENT_PREFIX, STS_ROLE_ASSUMERS_FILE));
169+
String resourceName = String.format("%s%s.ts", STS_CLIENT_PREFIX, STS_ROLE_ASSUMERS_FILE);
170+
String source = IoUtils.readUtf8Resource(getClass(), resourceName);
171+
writer.write("$L$L", noTouchNoticePrefix, resourceName);
172+
writer.write("$L", source);
173+
});
174+
writerFactory.accept("defaultRoleAssumers.spec.ts", writer -> {
175+
String resourceName = String.format("%s%s.ts", STS_CLIENT_PREFIX, ROLE_ASSUMERS_TEST_FILE);
176+
String source = IoUtils.readUtf8Resource(getClass(), resourceName);
177+
writer.write("$L$L", noTouchNoticePrefix, resourceName);
166178
writer.write("$L", source);
167179
});
168180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { HttpResponse } from "@aws-sdk/protocol-http";
2+
import { Readable } from "stream";
3+
const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
4+
<AssumeRoleResult>
5+
<AssumedRoleUser>
6+
<AssumedRoleId>AROAZOX2IL27GNRBJHWC2:session</AssumedRoleId>
7+
<Arn>arn:aws:sts::123:assumed-role/assume-role-test/session</Arn>
8+
</AssumedRoleUser>
9+
<Credentials>
10+
<AccessKeyId>key</AccessKeyId>
11+
<SecretAccessKey>secrete</SecretAccessKey>
12+
<SessionToken>session-token</SessionToken>
13+
<Expiration>2021-05-05T23:22:08Z</Expiration>
14+
</Credentials>
15+
</AssumeRoleResult>
16+
<ResponseMetadata>
17+
<RequestId>12345678id</RequestId>
18+
</ResponseMetadata>
19+
</AssumeRoleResponse>`;
20+
const mockHandle = jest.fn().mockResolvedValue({
21+
response: new HttpResponse({
22+
statusCode: 200,
23+
body: Readable.from([""]),
24+
}),
25+
});
26+
jest.mock("@aws-sdk/node-http-handler", () => ({
27+
NodeHttpHandler: jest.fn().mockImplementation(() => ({
28+
destroy: () => {},
29+
handle: mockHandle,
30+
})),
31+
streamCollector: async () => Buffer.from(assumeRoleResponse),
32+
}));
33+
34+
import { getDefaultRoleAssumer } from "./defaultRoleAssumers";
35+
import type { AssumeRoleCommandInput } from "./commands/AssumeRoleCommand";
36+
37+
describe("getDefaultRoleAssumer", () => {
38+
beforeEach(() => {
39+
jest.clearAllMocks();
40+
});
41+
it("should use supplied source credentials", async () => {
42+
const roleAssumer = getDefaultRoleAssumer();
43+
const params: AssumeRoleCommandInput = {
44+
RoleArn: "arn:aws:foo",
45+
RoleSessionName: "session",
46+
};
47+
const sourceCred1 = { accessKeyId: "key1", secretAccessKey: "secrete1" };
48+
await roleAssumer(sourceCred1, params);
49+
expect(mockHandle).toBeCalledTimes(1);
50+
// Validate request is signed by sourceCred1
51+
expect(mockHandle.mock.calls[0][0].headers?.authorization).toEqual(
52+
expect.stringContaining("AWS4-HMAC-SHA256 Credential=key1/")
53+
);
54+
const sourceCred2 = { accessKeyId: "key2", secretAccessKey: "secrete1" };
55+
await roleAssumer(sourceCred2, params);
56+
// Validate request is signed by sourceCred2
57+
expect(mockHandle).toBeCalledTimes(2);
58+
expect(mockHandle.mock.calls[1][0].headers?.authorization).toEqual(
59+
expect.stringContaining("AWS4-HMAC-SHA256 Credential=key2/")
60+
);
61+
});
62+
});

codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultStsRoleAssumers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ export const getDefaultRoleAssumer = (
3939
stsClientCtor: new (options: STSClientConfig) => STSClient
4040
): RoleAssumer => {
4141
let stsClient: STSClient;
42+
let closureSourceCreds: Credentials;
4243
return async (sourceCreds, params) => {
44+
closureSourceCreds = sourceCreds;
4345
if (!stsClient) {
4446
const { logger, region } = stsOptions;
4547
stsClient = new stsClientCtor({
4648
logger,
47-
credentials: sourceCreds,
49+
// A hack to make sts client uses the credential in current closure.
50+
credentialDefaultProvider: () => async () => closureSourceCreds,
4851
region: decorateDefaultRegion(region),
4952
});
5053
}

scripts/generate-clients/copy-to-clients.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const getOverwritablePredicate = (packageName) => (pathName) => {
2121
"README.md",
2222
];
2323
const additionalGeneratedFiles = {
24-
"@aws-sdk/client-sts": ["defaultRoleAssumers.ts", "defaultStsRoleAssumers.ts"],
24+
"@aws-sdk/client-sts": ["defaultRoleAssumers.ts", "defaultStsRoleAssumers.ts", "defaultRoleAssumers.spec.ts"],
2525
};
2626
return (
2727
pathName

0 commit comments

Comments
 (0)