Skip to content

Commit 4a0d7b4

Browse files
authored
Add transformation for TokenProviders and TokenProviderChain (#641)
1 parent cf5a104 commit 4a0d7b4

File tree

10 files changed

+88
-45
lines changed

10 files changed

+88
-45
lines changed

.changeset/grumpy-dancers-laugh.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+
Add transformation for TokenProviders and TokenProviderChain
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.TokenProviderChain(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 token providers from classes to functions.
4+
// The TokenProviderChain is now a chain of providers.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
6+
providerChain(providers);
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.SSOTokenProvider(options);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromSso } from "@aws-sdk/token-providers";
2+
3+
// JS SDK v3 switched token providers from classes to functions.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
6+
fromSso(options);
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.StaticTokenProvider(options);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { fromStatic } from "@aws-sdk/token-providers";
2+
3+
// JS SDK v3 switched token providers from classes to functions.
4+
// This is the closest approximation from codemod of what your application needs.
5+
// Reference: https://www.npmjs.com/package/@aws-sdk/token-providers
6+
fromStatic(options);
Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Collection, JSCodeshift, NewExpression } from "jscodeshift";
2-
import { AWS_CREDENTIALS_MAP } from "../config";
2+
import { AWS_CREDENTIALS_MAP, AWS_TOKEN_MAP } from "../config";
33
import { ImportType, addNamedModule } from "../modules";
44

55
export interface ReplaceAwsCredentialsOptions {
66
v2GlobalName?: string;
77
importType: ImportType;
88
}
99

10-
const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched credential providers from classes to functions.`;
11-
1210
const getNewExpression = (identifier: string, className: string) =>
1311
({
1412
type: "NewExpression",
@@ -29,60 +27,65 @@ export const replaceAwsIdentity = (
2927
) => {
3028
if (!v2GlobalName) return;
3129

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-
}
30+
for (const [identity, identityMap] of Object.entries({
31+
Credential: AWS_CREDENTIALS_MAP,
32+
Token: AWS_TOKEN_MAP,
33+
})) {
34+
const identitySwitchComment = ` JS SDK v3 switched ${identity.toLowerCase()} providers from classes to functions.`;
35+
const identityPackageName = `@aws-sdk/${identity.toLowerCase()}-providers`;
5736

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(
37+
const identityProviderChain = `${identity}ProviderChain`;
38+
const chainNewExpressions = source.find(
6139
j.NewExpression,
62-
getNewExpression(v2GlobalName, v2CredentialsName)
40+
getNewExpression(v2GlobalName, identityProviderChain)
6341
);
64-
65-
if (credsNewExpressions.size() > 0) {
42+
if (chainNewExpressions.size() > 0) {
43+
const localName = "providerChain";
6644
addNamedModule(j, source, {
6745
importType,
68-
importedName: v3ProviderName,
69-
packageName: "@aws-sdk/credential-providers",
46+
localName,
47+
importedName: "chain",
48+
packageName: "@smithy/property-provider",
7049
});
71-
credsNewExpressions.replaceWith(({ node }) =>
50+
chainNewExpressions.replaceWith(({ node }) =>
7251
j.callExpression.from({
73-
callee: j.identifier(v3ProviderName),
52+
callee: j.identifier(localName),
7453
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-
),
54+
j.commentLine(identitySwitchComment),
55+
j.commentLine(` The ${identityProviderChain} is now a chain of providers.`),
56+
j.commentLine(` Reference: https://www.npmjs.com/package/${identityPackageName}`),
8257
],
8358
arguments: node.arguments,
8459
})
8560
);
8661
}
62+
63+
for (const [v2IdentityName, v3ProviderName] of Object.entries(identityMap)) {
64+
const credsNewExpressions = source.find(
65+
j.NewExpression,
66+
getNewExpression(v2GlobalName, v2IdentityName)
67+
);
68+
69+
if (credsNewExpressions.size() > 0) {
70+
addNamedModule(j, source, {
71+
importType,
72+
importedName: v3ProviderName,
73+
packageName: identityPackageName,
74+
});
75+
credsNewExpressions.replaceWith(({ node }) =>
76+
j.callExpression.from({
77+
callee: j.identifier(v3ProviderName),
78+
comments: [
79+
j.commentLine(identitySwitchComment),
80+
j.commentLine(
81+
" This is the closest approximation from codemod of what your application needs."
82+
),
83+
j.commentLine(` Reference: https://www.npmjs.com/package/${identityPackageName}`),
84+
],
85+
arguments: node.arguments,
86+
})
87+
);
88+
}
89+
}
8790
}
8891
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Maps the AWS token class name in v2 to the v3 equivalent provider.
3+
*/
4+
export const AWS_TOKEN_MAP: Record<string, string> = {
5+
SSOTokenProvider: "fromSso",
6+
StaticTokenProvider: "fromStatic",
7+
};

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

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

0 commit comments

Comments
 (0)