Skip to content

Commit

Permalink
Transform named import of Credentials (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 3, 2024
1 parent 969443b commit bfa8e16
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-doors-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform named import of Credentials
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import AWS from "aws-sdk";
import { CredentialProviderChain } from "aws-sdk";

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

new 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 credential 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/credential-providers
fromCognitoIdentity();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { EC2MetadataCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromInstanceMetadata();
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/credentials/ecs.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ECSCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromContainerMetadata();
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/credentials/env.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { EnvironmentCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromEnv("AWS");
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/credentials/ini.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SharedIniFileCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromIni({ profile: "profile-name" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ProcessCredentials } from "aws-sdk";

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

// JS SDK v3 switched credential 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/credential-providers
fromProcess({ profile: "profile-name" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RemoteCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromContainerMetadata();
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/credentials/sso.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SsoCredentials } from "aws-sdk";

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

// JS SDK v3 switched credential 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/credential-providers
fromSSO({ profile: "profile-name" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { TokenFileWebIdentityCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromTokenFile();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { WebIdentityCredentials } from "aws-sdk";

new 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 credential 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/credential-providers
fromWebToken();
28 changes: 17 additions & 11 deletions src/transforms/v2-to-v3/apis/replaceAwsIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ export interface ReplaceAwsCredentialsOptions {
importType: ImportType;
}

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

Expand All @@ -25,8 +32,6 @@ export const replaceAwsIdentity = (
source: Collection<unknown>,
{ v2GlobalName, importType }: ReplaceAwsCredentialsOptions
) => {
if (!v2GlobalName) return;

for (const [identity, identityMap] of Object.entries({
Credential: AWS_CREDENTIALS_MAP,
Token: AWS_TOKEN_MAP,
Expand All @@ -37,8 +42,9 @@ export const replaceAwsIdentity = (
const identityProviderChain = `${identity}ProviderChain`;
const chainNewExpressions = source.find(
j.NewExpression,
getNewExpression(v2GlobalName, identityProviderChain)
getNewExpression(identityProviderChain, v2GlobalName)
);

if (chainNewExpressions.size() > 0) {
const localName = "providerChain";
addNamedModule(j, source, {
Expand All @@ -63,7 +69,7 @@ export const replaceAwsIdentity = (
for (const [v2IdentityName, v3ProviderName] of Object.entries(identityMap)) {
const credsNewExpressions = source.find(
j.NewExpression,
getNewExpression(v2GlobalName, v2IdentityName)
getNewExpression(v2IdentityName, v2GlobalName)
);

if (credsNewExpressions.size() > 0) {
Expand Down

0 comments on commit bfa8e16

Please sign in to comment.