Skip to content

Commit

Permalink
Add transformation for AWS.CredentialProviderChain (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 23, 2023
1 parent c1c3bba commit 08464c6
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 76 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-flies-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add transformation for AWS.CredentialProviderChain
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.CredentialProviderChain(providers);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { chain as providerChain } from "@smithy/property-provider";

// JS SDK v3 switched to credential providers to functions instead of objects.
// The CredentialProviderChain is now a chain of providers.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
providerChain(providers);
23 changes: 0 additions & 23 deletions src/transforms/v2-to-v3/apis/getAwsCredentialsNewExpressions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from "./getS3SignedUrlApiNames";
export * from "./isS3GetSignedUrlApiUsed";
export * from "./isS3UploadApiUsed";
export * from "./removePromiseCalls";
export * from "./replaceAwsCredentials";
export * from "./replaceAwsIdentity";
export * from "./replaceS3GetSignedUrlApi";
export * from "./replaceS3UploadApi";
export * from "./replaceWaiterApi";
Expand Down
50 changes: 0 additions & 50 deletions src/transforms/v2-to-v3/apis/replaceAwsCredentials.ts

This file was deleted.

88 changes: 88 additions & 0 deletions src/transforms/v2-to-v3/apis/replaceAwsIdentity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Collection, JSCodeshift, NewExpression } from "jscodeshift";
import { AWS_CREDENTIALS_MAP } from "../config";
import { ImportType, addNamedModule } from "../modules";

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

const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched to credential providers to functions instead of objects.`;

const getNewExpression = (identifier: string, className: string) =>
({
type: "NewExpression",
callee: {
type: "MemberExpression",
object: {
type: "Identifier",
name: identifier,
},
property: { name: className },
},
}) as NewExpression;

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

// ToDo: Add support for AWS.TokenProviderChain in future.
const chainNewExpressions = source.find(
j.NewExpression,
getNewExpression(v2GlobalName, "CredentialProviderChain")
);
if (chainNewExpressions.size() > 0) {
const localName = "providerChain";
addNamedModule(j, source, {
importType,
localName,
importedName: "chain",
packageName: "@smithy/property-provider",
});
chainNewExpressions.replaceWith(({ node }) =>
j.callExpression.from({
callee: j.identifier(localName),
comments: [
j.commentLine(PROVIDER_SWITCH_COMMENT),
j.commentLine(" The CredentialProviderChain is now a chain of providers."),
j.commentLine(" Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"),
],
arguments: node.arguments,
})
);
}

// ToDo: Add support for AWS.Token in future.
for (const [v2CredentialsName, v3ProviderName] of Object.entries(AWS_CREDENTIALS_MAP)) {
const credsNewExpressions = source.find(
j.NewExpression,
getNewExpression(v2GlobalName, v2CredentialsName)
);

if (credsNewExpressions.size() > 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(PROVIDER_SWITCH_COMMENT),
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,
})
);
}
}
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
replaceS3UploadApi,
replaceS3GetSignedUrlApi,
getClientIdentifiersRecord,
replaceAwsCredentials,
replaceAwsIdentity,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
Expand Down Expand Up @@ -92,7 +92,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
replaceDocClientCreation(j, source, v2Options);
}
replaceAwsCredentials(j, source, { v2GlobalName, importType });
replaceAwsIdentity(j, source, { v2GlobalName, importType });
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, { v2GlobalName, importType });

Expand Down

0 comments on commit 08464c6

Please sign in to comment.