Skip to content

Commit 7abe8a2

Browse files
authored
Basic transformation of AWS Credentials (#598)
1 parent 577fc33 commit 7abe8a2

28 files changed

+184
-10
lines changed

.changeset/hot-insects-protect.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": minor
3+
---
4+
5+
Basic transformation of AWS Credentials
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import AWS from "aws-sdk";
2+
3+
new AWS.ChainableTemporaryCredentials({
4+
params: { RoleArn: "RoleA" },
5+
masterCredentials: existingCredentials,
6+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromTemporaryCredentials({
7+
params: { RoleArn: "RoleA" },
8+
masterCredentials: existingCredentials,
9+
});
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.CognitoIdentityCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromCognitoIdentity } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromCognitoIdentity();
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.EC2MetadataCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromInstanceMetadata } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromInstanceMetadata();
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.ECSCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromContainerMetadata } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromContainerMetadata();
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.EnvironmentCredentials("AWS");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromEnv } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromEnv("AWS");
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.SharedIniFileCredentials({ profile: "profile-name" });
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromIni } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromIni({ profile: "profile-name" });
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.RemoteCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromContainerMetadata } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromContainerMetadata();
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.TokenFileWebIdentityCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromTokenFile } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromTokenFile();
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.WebIdentityCredentials();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromWebToken } from "@aws-sdk/credential-providers";
2+
3+
// JS SDK v3 switched to credential providers to functions instead of objects.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
6+
fromWebToken();

src/transforms/v2-to-v3/__fixtures__/misc/unsupported-feature.input.js

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

src/transforms/v2-to-v3/__fixtures__/misc/unsupported-feature.output.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export interface GetAwsCredentialsNewExpressionOptions {
4+
v2GlobalName: string;
5+
className: string;
6+
}
7+
8+
export const getAwsCredentialsNewExpressions = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
{ v2GlobalName, className }: GetAwsCredentialsNewExpressionOptions
12+
) =>
13+
source.find(j.NewExpression, {
14+
type: "NewExpression",
15+
callee: {
16+
type: "MemberExpression",
17+
object: {
18+
type: "Identifier",
19+
name: v2GlobalName,
20+
},
21+
property: { name: className },
22+
},
23+
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./getS3SignedUrlApiNames";
66
export * from "./isS3GetSignedUrlApiUsed";
77
export * from "./isS3UploadApiUsed";
88
export * from "./removePromiseCalls";
9+
export * from "./replaceAwsCredentials";
910
export * from "./replaceS3GetSignedUrlApi";
1011
export * from "./replaceS3UploadApi";
1112
export * from "./replaceWaiterApi";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { AWS_CREDENTIALS_MAP } from "../config";
3+
import { ImportType, addNamedModule } from "../modules";
4+
import { getAwsCredentialsNewExpressions } from "./getAwsCredentialsNewExpressions";
5+
6+
export interface ReplaceAwsCredentialsOptions {
7+
v2GlobalName?: string;
8+
importType: ImportType;
9+
}
10+
11+
export const replaceAwsCredentials = (
12+
j: JSCodeshift,
13+
source: Collection<unknown>,
14+
{ v2GlobalName, importType }: ReplaceAwsCredentialsOptions
15+
) => {
16+
if (!v2GlobalName) return;
17+
18+
for (const [v2CredentialsName, v3ProviderName] of Object.entries(AWS_CREDENTIALS_MAP)) {
19+
const credsNewExpressions = getAwsCredentialsNewExpressions(j, source, {
20+
v2GlobalName,
21+
className: v2CredentialsName,
22+
});
23+
const credsNewExpressionCount = credsNewExpressions.size();
24+
25+
if (credsNewExpressionCount > 0) {
26+
addNamedModule(j, source, {
27+
importType,
28+
importedName: v3ProviderName,
29+
packageName: "@aws-sdk/credential-providers",
30+
});
31+
credsNewExpressions.replaceWith(({ node }) =>
32+
j.callExpression.from({
33+
callee: j.identifier(v3ProviderName),
34+
comments: [
35+
j.commentLine(
36+
" JS SDK v3 switched to credential providers to functions instead of objects."
37+
),
38+
j.commentLine(
39+
" This is the closest approximation from codemod of what your application needs."
40+
),
41+
j.commentLine(
42+
" Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"
43+
),
44+
],
45+
arguments: node.arguments,
46+
})
47+
);
48+
}
49+
}
50+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Maps the AWS credentials class name in v2 to the v3 equivalent provider.
3+
*/
4+
export const AWS_CREDENTIALS_MAP: Record<string, string> = {
5+
ChainableTemporaryCredentials: "fromTemporaryCredentials",
6+
CognitoIdentityCredentials: "fromCognitoIdentity",
7+
EC2MetadataCredentials: "fromInstanceMetadata",
8+
ECSCredentials: "fromContainerMetadata",
9+
EnvironmentCredentials: "fromEnv",
10+
RemoteCredentials: "fromContainerMetadata",
11+
SharedIniFileCredentials: "fromIni",
12+
TokenFileWebIdentityCredentials: "fromTokenFile",
13+
WebIdentityCredentials: "fromWebToken",
14+
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./AWS_CREDENTIALS_MAP";
12
export * from "./CLIENT_NAMES";
23
export * from "./CLIENT_NAMES_MAP";
34
export * from "./CLIENT_PACKAGE_NAMES_MAP";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./addClientModules";
2+
export * from "./addNamedModule";
23
export * from "./getGlobalNameFromModule";
34
export * from "./getImportEqualsDeclarationType";
45
export * from "./getImportSpecifiers";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
replaceS3UploadApi,
99
replaceS3GetSignedUrlApi,
1010
getClientIdentifiersRecord,
11+
replaceAwsCredentials,
1112
} from "./apis";
1213
import { replaceAwsUtilFunctions } from "./aws-util";
1314
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
@@ -91,6 +92,7 @@ const transformer = async (file: FileInfo, api: API) => {
9192
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
9293
replaceDocClientCreation(j, source, v2Options);
9394
}
95+
replaceAwsCredentials(j, source, { v2GlobalName, importType });
9496
replaceAwsUtilFunctions(j, source, v2GlobalName);
9597
removeGlobalModule(j, source, { v2GlobalName, importType });
9698

0 commit comments

Comments
 (0)