-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transformation for AWS.CredentialProviderChain (#638)
- Loading branch information
Showing
8 changed files
with
105 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": patch | ||
--- | ||
|
||
Add transformation for AWS.CredentialProviderChain |
3 changes: 3 additions & 0 deletions
3
src/transforms/v2-to-v3/__fixtures__/credentials/chain.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
new AWS.CredentialProviderChain(providers); |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/credentials/chain.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/transforms/v2-to-v3/apis/getAwsCredentialsNewExpressions.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters