Skip to content

Commit 1a09462

Browse files
authored
Transform config credential keys (#661)
1 parent 72a3e55 commit 1a09462

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

.changeset/itchy-pets-remain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Transform config credential keys
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import AWS from "aws-sdk";
2+
3+
const client = new AWS.DynamoDB({
4+
accessKeyId: "KEY",
5+
secretAccessKey: "SECRET"
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB({
4+
credentials: {
5+
accessKeyId: "KEY",
6+
secretAccessKey: "SECRET"
7+
}
8+
});

src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ const getCodemodUnsuppportedComments = (keyName: string) => [
1616
export const getObjectWithUpdatedAwsConfigKeys = (
1717
j: JSCodeshift,
1818
objectExpression: ObjectExpression
19-
) =>
20-
j.objectExpression(
21-
objectExpression.properties.map((property) => {
19+
) => {
20+
const securityCredentialKeys = ["accessKeyId", "secretAccessKey", "sessionToken"];
21+
const credentials = j.objectExpression([]);
22+
23+
const updatedProperties = objectExpression.properties
24+
.map((property) => {
2225
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
2326
return property;
2427
}
@@ -28,6 +31,11 @@ export const getObjectWithUpdatedAwsConfigKeys = (
2831
return property;
2932
}
3033

34+
if (securityCredentialKeys.includes(propertyKey.name)) {
35+
credentials.properties.push(property);
36+
return undefined;
37+
}
38+
3139
const awsConfigKeyStatus = AWS_CONFIG_KEY_MAP[propertyKey.name];
3240

3341
if (!awsConfigKeyStatus) {
@@ -63,4 +71,13 @@ export const getObjectWithUpdatedAwsConfigKeys = (
6371

6472
return property;
6573
})
66-
);
74+
.filter((property) => property !== undefined);
75+
76+
if (credentials.properties.length > 0) {
77+
updatedProperties.unshift(j.objectProperty(j.identifier("credentials"), credentials));
78+
}
79+
80+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
81+
// @ts-ignore is Argument is not assignable to parameter
82+
return j.objectExpression(updatedProperties);
83+
};

src/transforms/v2-to-v3/client-instances/replaceAwsConfig.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ export const replaceAwsConfig = (
1212
.find(j.NewExpression, {
1313
callee: {
1414
type: "MemberExpression",
15-
object: {
16-
type: "Identifier",
17-
name: v2GlobalName,
18-
},
19-
property: {
20-
type: "Identifier",
21-
name: "Config",
22-
},
15+
object: { type: "Identifier", name: v2GlobalName },
16+
property: { type: "Identifier", name: "Config" },
2317
},
2418
})
2519
.filter(

0 commit comments

Comments
 (0)