Skip to content

Commit 08464c6

Browse files
authored
Add transformation for AWS.CredentialProviderChain (#638)
1 parent c1c3bba commit 08464c6

File tree

8 files changed

+105
-76
lines changed

8 files changed

+105
-76
lines changed

.changeset/green-flies-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Add transformation for AWS.CredentialProviderChain
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS from "aws-sdk";
2+
3+
new AWS.CredentialProviderChain(providers);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { chain as providerChain } from "@smithy/property-provider";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// The CredentialProviderChain is now a chain of providers.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
providerChain(providers);

src/transforms/v2-to-v3/apis/getAwsCredentialsNewExpressions.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/transforms/v2-to-v3/apis/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export * from "./getS3SignedUrlApiNames";
66
export * from "./isS3GetSignedUrlApiUsed";
77
export * from "./isS3UploadApiUsed";
88
export * from "./removePromiseCalls";
9-
export * from "./replaceAwsCredentials";
9+
export * from "./replaceAwsIdentity";
1010
export * from "./replaceS3GetSignedUrlApi";
1111
export * from "./replaceS3UploadApi";
1212
export * from "./replaceWaiterApi";

src/transforms/v2-to-v3/apis/replaceAwsCredentials.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Collection, JSCodeshift, NewExpression } from "jscodeshift";
2+
import { AWS_CREDENTIALS_MAP } from "../config";
3+
import { ImportType, addNamedModule } from "../modules";
4+
5+
export interface ReplaceAwsCredentialsOptions {
6+
v2GlobalName?: string;
7+
importType: ImportType;
8+
}
9+
10+
const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched to credential providers to functions instead of objects.`;
11+
12+
const getNewExpression = (identifier: string, className: string) =>
13+
({
14+
type: "NewExpression",
15+
callee: {
16+
type: "MemberExpression",
17+
object: {
18+
type: "Identifier",
19+
name: identifier,
20+
},
21+
property: { name: className },
22+
},
23+
}) as NewExpression;
24+
25+
export const replaceAwsIdentity = (
26+
j: JSCodeshift,
27+
source: Collection<unknown>,
28+
{ v2GlobalName, importType }: ReplaceAwsCredentialsOptions
29+
) => {
30+
if (!v2GlobalName) return;
31+
32+
// ToDo: Add support for AWS.TokenProviderChain in future.
33+
const chainNewExpressions = source.find(
34+
j.NewExpression,
35+
getNewExpression(v2GlobalName, "CredentialProviderChain")
36+
);
37+
if (chainNewExpressions.size() > 0) {
38+
const localName = "providerChain";
39+
addNamedModule(j, source, {
40+
importType,
41+
localName,
42+
importedName: "chain",
43+
packageName: "@smithy/property-provider",
44+
});
45+
chainNewExpressions.replaceWith(({ node }) =>
46+
j.callExpression.from({
47+
callee: j.identifier(localName),
48+
comments: [
49+
j.commentLine(PROVIDER_SWITCH_COMMENT),
50+
j.commentLine(" The CredentialProviderChain is now a chain of providers."),
51+
j.commentLine(" Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"),
52+
],
53+
arguments: node.arguments,
54+
})
55+
);
56+
}
57+
58+
// ToDo: Add support for AWS.Token in future.
59+
for (const [v2CredentialsName, v3ProviderName] of Object.entries(AWS_CREDENTIALS_MAP)) {
60+
const credsNewExpressions = source.find(
61+
j.NewExpression,
62+
getNewExpression(v2GlobalName, v2CredentialsName)
63+
);
64+
65+
if (credsNewExpressions.size() > 0) {
66+
addNamedModule(j, source, {
67+
importType,
68+
importedName: v3ProviderName,
69+
packageName: "@aws-sdk/credential-providers",
70+
});
71+
credsNewExpressions.replaceWith(({ node }) =>
72+
j.callExpression.from({
73+
callee: j.identifier(v3ProviderName),
74+
comments: [
75+
j.commentLine(PROVIDER_SWITCH_COMMENT),
76+
j.commentLine(
77+
" This is the closest approximation from codemod of what your application needs."
78+
),
79+
j.commentLine(
80+
" Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"
81+
),
82+
],
83+
arguments: node.arguments,
84+
})
85+
);
86+
}
87+
}
88+
};

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
replaceS3UploadApi,
99
replaceS3GetSignedUrlApi,
1010
getClientIdentifiersRecord,
11-
replaceAwsCredentials,
11+
replaceAwsIdentity,
1212
} from "./apis";
1313
import { replaceAwsUtilFunctions } from "./aws-util";
1414
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
@@ -92,7 +92,7 @@ const transformer = async (file: FileInfo, api: API) => {
9292
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
9393
replaceDocClientCreation(j, source, v2Options);
9494
}
95-
replaceAwsCredentials(j, source, { v2GlobalName, importType });
95+
replaceAwsIdentity(j, source, { v2GlobalName, importType });
9696
replaceAwsUtilFunctions(j, source, v2GlobalName);
9797
removeGlobalModule(j, source, { v2GlobalName, importType });
9898

0 commit comments

Comments
 (0)