Skip to content

Commit

Permalink
Add transformation for TokenProviders and TokenProviderChain (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 24, 2023
1 parent cf5a104 commit 4a0d7b4
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-dancers-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Add transformation for TokenProviders and TokenProviderChain
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/chain.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.TokenProviderChain(providers);
6 changes: 6 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/chain.output.js
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 token providers from classes to functions.
// The TokenProviderChain is now a chain of providers.
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
providerChain(providers);
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/sso.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.SSOTokenProvider(options);
6 changes: 6 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/sso.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromSso } from "@aws-sdk/token-providers";

// JS SDK v3 switched token providers from classes to functions.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
fromSso(options);
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/static.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

new AWS.StaticTokenProvider(options);
6 changes: 6 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/token/static.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fromStatic } from "@aws-sdk/token-providers";

// JS SDK v3 switched token providers from classes to functions.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
fromStatic(options);
93 changes: 48 additions & 45 deletions src/transforms/v2-to-v3/apis/replaceAwsIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Collection, JSCodeshift, NewExpression } from "jscodeshift";
import { AWS_CREDENTIALS_MAP } from "../config";
import { AWS_CREDENTIALS_MAP, AWS_TOKEN_MAP } from "../config";
import { ImportType, addNamedModule } from "../modules";

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

const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched credential providers from classes to functions.`;

const getNewExpression = (identifier: string, className: string) =>
({
type: "NewExpression",
Expand All @@ -29,60 +27,65 @@ export const replaceAwsIdentity = (
) => {
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,
})
);
}
for (const [identity, identityMap] of Object.entries({
Credential: AWS_CREDENTIALS_MAP,
Token: AWS_TOKEN_MAP,
})) {
const identitySwitchComment = ` JS SDK v3 switched ${identity.toLowerCase()} providers from classes to functions.`;
const identityPackageName = `@aws-sdk/${identity.toLowerCase()}-providers`;

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

if (credsNewExpressions.size() > 0) {
if (chainNewExpressions.size() > 0) {
const localName = "providerChain";
addNamedModule(j, source, {
importType,
importedName: v3ProviderName,
packageName: "@aws-sdk/credential-providers",
localName,
importedName: "chain",
packageName: "@smithy/property-provider",
});
credsNewExpressions.replaceWith(({ node }) =>
chainNewExpressions.replaceWith(({ node }) =>
j.callExpression.from({
callee: j.identifier(v3ProviderName),
callee: j.identifier(localName),
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"
),
j.commentLine(identitySwitchComment),
j.commentLine(` The ${identityProviderChain} is now a chain of providers.`),
j.commentLine(` Reference: https://www.npmjs.com/package/${identityPackageName}`),
],
arguments: node.arguments,
})
);
}

for (const [v2IdentityName, v3ProviderName] of Object.entries(identityMap)) {
const credsNewExpressions = source.find(
j.NewExpression,
getNewExpression(v2GlobalName, v2IdentityName)
);

if (credsNewExpressions.size() > 0) {
addNamedModule(j, source, {
importType,
importedName: v3ProviderName,
packageName: identityPackageName,
});
credsNewExpressions.replaceWith(({ node }) =>
j.callExpression.from({
callee: j.identifier(v3ProviderName),
comments: [
j.commentLine(identitySwitchComment),
j.commentLine(
" This is the closest approximation from codemod of what your application needs."
),
j.commentLine(` Reference: https://www.npmjs.com/package/${identityPackageName}`),
],
arguments: node.arguments,
})
);
}
}
}
};
7 changes: 7 additions & 0 deletions src/transforms/v2-to-v3/config/AWS_TOKEN_MAP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Maps the AWS token class name in v2 to the v3 equivalent provider.
*/
export const AWS_TOKEN_MAP: Record<string, string> = {
SSOTokenProvider: "fromSso",
StaticTokenProvider: "fromStatic",
};
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,4 +1,5 @@
export * from "./AWS_CREDENTIALS_MAP";
export * from "./AWS_TOKEN_MAP";
export * from "./CLIENT_NAMES";
export * from "./CLIENT_NAMES_MAP";
export * from "./CLIENT_PACKAGE_NAMES_MAP";
Expand Down

0 comments on commit 4a0d7b4

Please sign in to comment.