Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic transformation of AWS Credentials #598

Merged
merged 19 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-insects-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Basic transformation of AWS Credentials
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import AWS from "aws-sdk";

new AWS.ChainableTemporaryCredentials({
params: { RoleArn: "RoleA" },
masterCredentials: existingCredentials,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromTemporaryCredentials({
params: { RoleArn: "RoleA" },
masterCredentials: existingCredentials,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.CognitoIdentityCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromCognitoIdentity } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromCognitoIdentity();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.EC2MetadataCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromInstanceMetadata } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromInstanceMetadata();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.ECSCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromContainerMetadata } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromContainerMetadata();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.EnvironmentCredentials("AWS");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromEnv } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromEnv("AWS");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.SharedIniFileCredentials({ profile: "profile-name" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromIni } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromIni({ profile: "profile-name" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.RemoteCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromContainerMetadata } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromContainerMetadata();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.TokenFileWebIdentityCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromTokenFile } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromTokenFile();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.WebIdentityCredentials();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromWebToken } from "@aws-sdk/credential-providers";

// JS SDK v3 switched to credential providers to functions instead of objects.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromWebToken();

This file was deleted.

This file was deleted.

23 changes: 23 additions & 0 deletions src/transforms/v2-to-v3/apis/getAwsCredentialsNewExpressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Collection, JSCodeshift } from "jscodeshift";

export interface GetAwsCredentialsNewExpressionOptions {
v2GlobalName: string;
className: string;
}

export const getAwsCredentialsNewExpressions = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2GlobalName, className }: GetAwsCredentialsNewExpressionOptions
) =>
source.find(j.NewExpression, {
type: "NewExpression",
callee: {
type: "MemberExpression",
object: {
type: "Identifier",
name: v2GlobalName,
},
property: { name: className },
},
});
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./getS3SignedUrlApiNames";
export * from "./isS3GetSignedUrlApiUsed";
export * from "./isS3UploadApiUsed";
export * from "./removePromiseCalls";
export * from "./replaceAwsCredentials";
export * from "./replaceS3GetSignedUrlApi";
export * from "./replaceS3UploadApi";
export * from "./replaceWaiterApi";
Expand Down
50 changes: 50 additions & 0 deletions src/transforms/v2-to-v3/apis/replaceAwsCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { AWS_CREDENTIALS_MAP } from "../config";
import { ImportType, addNamedModule } from "../modules";
import { getAwsCredentialsNewExpressions } from "./getAwsCredentialsNewExpressions";

export interface ReplaceAwsCredentialsOptions {
v2GlobalName?: string;
importType: ImportType;
}

export const replaceAwsCredentials = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2GlobalName, importType }: ReplaceAwsCredentialsOptions
) => {
if (!v2GlobalName) return;

for (const [v2CredentialsName, v3ProviderName] of Object.entries(AWS_CREDENTIALS_MAP)) {
const credsNewExpressions = getAwsCredentialsNewExpressions(j, source, {
v2GlobalName,
className: v2CredentialsName,
});
const credsNewExpressionCount = credsNewExpressions.size();

if (credsNewExpressionCount > 0) {
addNamedModule(j, source, {
importType,
importedName: v3ProviderName,
packageName: "@aws-sdk/credential-providers",
});
credsNewExpressions.replaceWith(({ node }) =>
j.callExpression.from({
callee: j.identifier(v3ProviderName),
comments: [
j.commentLine(
" JS SDK v3 switched to credential providers to functions instead of objects."
),
j.commentLine(
" This is the closest approximation from codemod of what your application needs."
),
j.commentLine(
" Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"
),
],
arguments: node.arguments,
})
);
}
}
};
14 changes: 14 additions & 0 deletions src/transforms/v2-to-v3/config/AWS_CREDENTIALS_MAP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Maps the AWS credentials class name in v2 to the v3 equivalent provider.
*/
export const AWS_CREDENTIALS_MAP: Record<string, string> = {
ChainableTemporaryCredentials: "fromTemporaryCredentials",
CognitoIdentityCredentials: "fromCognitoIdentity",
EC2MetadataCredentials: "fromInstanceMetadata",
ECSCredentials: "fromContainerMetadata",
EnvironmentCredentials: "fromEnv",
RemoteCredentials: "fromContainerMetadata",
SharedIniFileCredentials: "fromIni",
TokenFileWebIdentityCredentials: "fromTokenFile",
WebIdentityCredentials: "fromWebToken",
};
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./AWS_CREDENTIALS_MAP";
export * from "./CLIENT_NAMES";
export * from "./CLIENT_NAMES_MAP";
export * from "./CLIENT_PACKAGE_NAMES_MAP";
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./addClientModules";
export * from "./addNamedModule";
export * from "./getGlobalNameFromModule";
export * from "./getImportEqualsDeclarationType";
export * from "./getImportSpecifiers";
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
replaceS3UploadApi,
replaceS3GetSignedUrlApi,
getClientIdentifiersRecord,
replaceAwsCredentials,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
Expand Down Expand Up @@ -91,6 +92,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
replaceDocClientCreation(j, source, v2Options);
}
replaceAwsCredentials(j, source, { v2GlobalName, importType });
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, { v2GlobalName, importType });

Expand Down