@velocitycareerlabs/aws-clients 1.25.0-dev-build.12642c864 → 1.25.0-dev-build.17bda9234
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -5
- package/src/document-storage.js +27 -7
- package/src/email-notifications.js +33 -26
- package/src/index.js +0 -1
- package/src/kms-client.js +26 -11
- package/src/s3-client.js +42 -19
- package/src/sms-notifications.js +14 -7
- package/src/aws-factory.js +0 -85
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@velocitycareerlabs/aws-clients",
|
3
|
-
"version": "1.25.0-dev-build.
|
3
|
+
"version": "1.25.0-dev-build.17bda9234",
|
4
4
|
"description": "Set of aws functions and utils used by Velocity Foundation projects",
|
5
5
|
"repository": "https://github.com/velocitycareerlabs/packages",
|
6
6
|
"main": "index.js",
|
@@ -15,14 +15,22 @@
|
|
15
15
|
"lint:fix": "eslint --fix --ext .js ."
|
16
16
|
},
|
17
17
|
"dependencies": {
|
18
|
-
"aws-sdk": "
|
18
|
+
"@aws-sdk/client-dynamodb": "^3.731.1",
|
19
|
+
"@aws-sdk/client-kms": "^3.731.1",
|
20
|
+
"@aws-sdk/client-s3": "^3.731.1",
|
21
|
+
"@aws-sdk/client-ses": "^3.731.1",
|
22
|
+
"@aws-sdk/client-sesv2": "^3.731.1",
|
23
|
+
"@aws-sdk/client-sns": "^3.731.1",
|
24
|
+
"@aws-sdk/lib-dynamodb": "^3.731.1",
|
25
|
+
"@aws-sdk/s3-request-presigner": "^3.731.1",
|
26
|
+
"aws-sdk": "^2.784.0",
|
19
27
|
"fastify-plugin": "~4.5.1",
|
20
28
|
"lodash": "^4.17.21",
|
21
29
|
"twilio": "^5.0.0"
|
22
30
|
},
|
23
31
|
"devDependencies": {
|
24
|
-
"@velocitycareerlabs/crypto": "1.25.0-dev-build.
|
25
|
-
"@velocitycareerlabs/jwt": "1.25.0-dev-build.
|
32
|
+
"@velocitycareerlabs/crypto": "1.25.0-dev-build.17bda9234",
|
33
|
+
"@velocitycareerlabs/jwt": "1.25.0-dev-build.17bda9234",
|
26
34
|
"eslint": "8.57.1",
|
27
35
|
"eslint-config-airbnb-base": "14.2.1",
|
28
36
|
"eslint-config-prettier": "8.10.0",
|
@@ -35,5 +43,5 @@
|
|
35
43
|
"jest": "29.7.0",
|
36
44
|
"prettier": "2.8.8"
|
37
45
|
},
|
38
|
-
"gitHead": "
|
46
|
+
"gitHead": "074a23a94e8653649e08e2a69408dab75037c436"
|
39
47
|
}
|
package/src/document-storage.js
CHANGED
@@ -14,7 +14,27 @@
|
|
14
14
|
* limitations under the License.
|
15
15
|
*/
|
16
16
|
|
17
|
-
const {
|
17
|
+
const { DynamoDB } = require('@aws-sdk/client-dynamodb');
|
18
|
+
const {
|
19
|
+
DynamoDBDocumentClient,
|
20
|
+
GetCommand,
|
21
|
+
PutCommand,
|
22
|
+
} = require('@aws-sdk/lib-dynamodb');
|
23
|
+
|
24
|
+
const createDynamoDbDocumentClient = ({ awsRegion, awsEndpoint }) =>
|
25
|
+
DynamoDBDocumentClient.from(
|
26
|
+
new DynamoDB({
|
27
|
+
apiVersion: '2012-08-10',
|
28
|
+
region: awsRegion,
|
29
|
+
credentials: awsEndpoint
|
30
|
+
? {
|
31
|
+
accessKeyId: 'tests-key-id',
|
32
|
+
secretAccessKey: 'tests-key',
|
33
|
+
}
|
34
|
+
: awsEndpoint,
|
35
|
+
endpoint: awsEndpoint,
|
36
|
+
})
|
37
|
+
);
|
18
38
|
|
19
39
|
const initReadDocument =
|
20
40
|
({ awsRegion, awsEndpoint }) =>
|
@@ -24,12 +44,12 @@ const initReadDocument =
|
|
24
44
|
awsEndpoint,
|
25
45
|
});
|
26
46
|
|
27
|
-
return dynamoDbDocClient
|
28
|
-
|
47
|
+
return dynamoDbDocClient.send(
|
48
|
+
new GetCommand({
|
29
49
|
TableName: table,
|
30
50
|
Key: key,
|
31
51
|
})
|
32
|
-
|
52
|
+
);
|
33
53
|
};
|
34
54
|
|
35
55
|
const initWriteDocument =
|
@@ -40,12 +60,12 @@ const initWriteDocument =
|
|
40
60
|
awsEndpoint,
|
41
61
|
});
|
42
62
|
|
43
|
-
await dynamoDbDocClient
|
44
|
-
|
63
|
+
await dynamoDbDocClient.send(
|
64
|
+
new PutCommand({
|
45
65
|
TableName: table,
|
46
66
|
Item: document,
|
47
67
|
})
|
48
|
-
|
68
|
+
);
|
49
69
|
};
|
50
70
|
|
51
71
|
module.exports = {
|
@@ -15,28 +15,34 @@
|
|
15
15
|
*/
|
16
16
|
|
17
17
|
const { isEmpty, join } = require('lodash/fp');
|
18
|
-
const {
|
18
|
+
const {
|
19
|
+
SESClient,
|
20
|
+
SendEmailCommand: SESSendEmailCommand,
|
21
|
+
} = require('@aws-sdk/client-ses');
|
22
|
+
const {
|
23
|
+
SESv2Client,
|
24
|
+
SendEmailCommand: SESv2SendEmailCommand,
|
25
|
+
} = require('@aws-sdk/client-sesv2');
|
19
26
|
|
20
|
-
// 2020-10-20: At the moment Localstack doesn't support SESv2,
|
27
|
+
// 2020-10-20: At the moment free Localstack doesn't support SESv2,
|
28
|
+
// so SES is used for testing when a value is passed for endpoint (local address)
|
21
29
|
|
22
|
-
const
|
23
|
-
({
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
const createSesClient = ({ awsRegion, awsEndpoint }) =>
|
31
|
+
new SESClient({
|
32
|
+
apiVersion: '2010-12-01',
|
33
|
+
region: awsRegion,
|
34
|
+
credentials: {
|
35
|
+
accessKeyId: 'tests-key-id',
|
36
|
+
secretAccessKey: 'tests-key',
|
37
|
+
},
|
38
|
+
endpoint: awsEndpoint,
|
39
|
+
});
|
30
40
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
await awsSesV2.createEmailIdentity({ EmailIdentity: domain }).promise();
|
38
|
-
}
|
39
|
-
};
|
41
|
+
const createSesV2Client = ({ awsRegion }) =>
|
42
|
+
new SESv2Client({
|
43
|
+
apiVersion: '2019-09-27',
|
44
|
+
region: awsRegion,
|
45
|
+
});
|
40
46
|
|
41
47
|
const initSendEmailNotification =
|
42
48
|
({ awsRegion, awsEndpoint }) =>
|
@@ -69,8 +75,8 @@ const initSendEmailNotification =
|
|
69
75
|
awsEndpoint,
|
70
76
|
});
|
71
77
|
|
72
|
-
await awsSes
|
73
|
-
|
78
|
+
await awsSes.send(
|
79
|
+
new SESSendEmailCommand({
|
74
80
|
Source: sender,
|
75
81
|
Destination: destination,
|
76
82
|
ReplyToAddresses: [replyTo],
|
@@ -102,14 +108,14 @@ const initSendEmailNotification =
|
|
102
108
|
},
|
103
109
|
},
|
104
110
|
})
|
105
|
-
|
111
|
+
);
|
106
112
|
} else {
|
107
113
|
const awsSesV2 = createSesV2Client({
|
108
114
|
awsRegion,
|
109
115
|
});
|
110
116
|
|
111
|
-
await awsSesV2
|
112
|
-
|
117
|
+
await awsSesV2.send(
|
118
|
+
new SESv2SendEmailCommand({
|
113
119
|
FromEmailAddress: sender,
|
114
120
|
Destination: destination,
|
115
121
|
ReplyToAddresses: [replyTo],
|
@@ -143,7 +149,7 @@ const initSendEmailNotification =
|
|
143
149
|
}),
|
144
150
|
},
|
145
151
|
})
|
146
|
-
|
152
|
+
);
|
147
153
|
}
|
148
154
|
};
|
149
155
|
|
@@ -186,7 +192,8 @@ const buildRawMessage = ({
|
|
186
192
|
};
|
187
193
|
|
188
194
|
module.exports = {
|
189
|
-
|
195
|
+
createSesClient,
|
196
|
+
createSesV2Client,
|
190
197
|
initSendEmailNotification,
|
191
198
|
buildRawMessage,
|
192
199
|
};
|
package/src/index.js
CHANGED
package/src/kms-client.js
CHANGED
@@ -14,29 +14,44 @@
|
|
14
14
|
* limitations under the License.
|
15
15
|
*/
|
16
16
|
|
17
|
-
const {
|
17
|
+
const {
|
18
|
+
KMSClient,
|
19
|
+
EncryptCommand,
|
20
|
+
DecryptCommand,
|
21
|
+
} = require('@aws-sdk/client-kms');
|
18
22
|
|
19
23
|
const initKmsClient = ({ awsRegion, awsEndpoint }) => {
|
20
|
-
const kmsClient =
|
21
|
-
awsRegion,
|
22
|
-
awsEndpoint
|
24
|
+
const kmsClient = new KMSClient({
|
25
|
+
region: awsRegion,
|
26
|
+
credentials: awsEndpoint
|
27
|
+
? {
|
28
|
+
accessKeyId: 'tests-key-id',
|
29
|
+
secretAccessKey: 'tests-key',
|
30
|
+
}
|
31
|
+
: awsEndpoint,
|
32
|
+
endpoint: awsEndpoint,
|
23
33
|
});
|
24
34
|
|
25
35
|
const encrypt = async (keyId, plaintext) => {
|
26
|
-
const
|
36
|
+
const command = new EncryptCommand({
|
27
37
|
KeyId: keyId,
|
28
|
-
Plaintext: plaintext,
|
29
|
-
};
|
38
|
+
Plaintext: Buffer.from(plaintext),
|
39
|
+
});
|
30
40
|
|
31
|
-
return kmsClient.
|
41
|
+
return kmsClient.send(command);
|
32
42
|
};
|
33
43
|
|
34
44
|
const decrypt = async (ciphertextBlob) => {
|
35
|
-
const
|
45
|
+
const command = new DecryptCommand({
|
36
46
|
CiphertextBlob: ciphertextBlob,
|
37
|
-
};
|
47
|
+
});
|
38
48
|
|
39
|
-
|
49
|
+
const result = await kmsClient.send(command);
|
50
|
+
|
51
|
+
return {
|
52
|
+
...result,
|
53
|
+
Plaintext: Buffer.from(result.Plaintext),
|
54
|
+
};
|
40
55
|
};
|
41
56
|
|
42
57
|
return {
|
package/src/s3-client.js
CHANGED
@@ -14,7 +14,15 @@
|
|
14
14
|
* limitations under the License.
|
15
15
|
*/
|
16
16
|
|
17
|
-
const {
|
17
|
+
const {
|
18
|
+
S3Client,
|
19
|
+
GetObjectCommand,
|
20
|
+
PutObjectCommand,
|
21
|
+
DeleteObjectCommand,
|
22
|
+
} = require('@aws-sdk/client-s3');
|
23
|
+
const {
|
24
|
+
getSignedUrl: awsGetSignedUrl,
|
25
|
+
} = require('@aws-sdk/s3-request-presigner');
|
18
26
|
|
19
27
|
const initS3Client = ({
|
20
28
|
awsRegion,
|
@@ -25,50 +33,65 @@ const initS3Client = ({
|
|
25
33
|
secretAccessKey,
|
26
34
|
isTest,
|
27
35
|
}) => {
|
28
|
-
const s3Client =
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
36
|
+
const s3Client = new S3Client({
|
37
|
+
apiVersion: '2006-03-01',
|
38
|
+
region: awsRegion,
|
39
|
+
signatureVersion: 'v4',
|
40
|
+
credentials: {
|
41
|
+
accessKeyId,
|
42
|
+
secretAccessKey,
|
43
|
+
},
|
44
|
+
forcePathStyle: isTest,
|
45
|
+
endpoint: awsEndpoint,
|
34
46
|
});
|
35
47
|
|
36
|
-
const getSignedUrl = async ({ key, contentType, metadata }) =>
|
37
|
-
|
48
|
+
const getSignedUrl = async ({ key, contentType, metadata }) => {
|
49
|
+
const command = new PutObjectCommand({
|
38
50
|
Bucket: s3Bucket,
|
39
51
|
Key: key,
|
40
|
-
Expires: s3PresignedUrlExpiration,
|
41
52
|
ContentType: contentType,
|
42
53
|
Metadata: metadata,
|
43
54
|
});
|
44
55
|
|
56
|
+
return awsGetSignedUrl(s3Client, command, {
|
57
|
+
expiresIn: s3PresignedUrlExpiration,
|
58
|
+
});
|
59
|
+
};
|
60
|
+
|
45
61
|
const getObject = async ({ bucket, key }) => {
|
46
|
-
const
|
62
|
+
const command = new GetObjectCommand({
|
47
63
|
Bucket: bucket,
|
48
64
|
Key: key,
|
49
|
-
};
|
65
|
+
});
|
66
|
+
|
67
|
+
const result = await s3Client.send(command);
|
68
|
+
const body = await result.Body.transformToByteArray();
|
50
69
|
|
51
|
-
return
|
70
|
+
return {
|
71
|
+
...result,
|
72
|
+
Body: body,
|
73
|
+
};
|
52
74
|
};
|
53
75
|
|
54
76
|
const putObject = async ({ bucket, key, body, contentType, metadata }) => {
|
55
|
-
const
|
77
|
+
const command = new PutObjectCommand({
|
56
78
|
Bucket: bucket,
|
57
79
|
Key: key,
|
58
80
|
Body: body,
|
59
81
|
ContentType: contentType,
|
60
82
|
Metadata: metadata,
|
61
|
-
};
|
83
|
+
});
|
62
84
|
|
63
|
-
await s3Client.
|
85
|
+
await s3Client.send(command);
|
64
86
|
};
|
65
87
|
|
66
88
|
const deleteObject = async ({ bucket, key }) => {
|
67
|
-
const
|
89
|
+
const command = new DeleteObjectCommand({
|
68
90
|
Bucket: bucket,
|
69
91
|
Key: key,
|
70
|
-
};
|
71
|
-
|
92
|
+
});
|
93
|
+
|
94
|
+
await s3Client.send(command);
|
72
95
|
};
|
73
96
|
|
74
97
|
return {
|
package/src/sms-notifications.js
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
* limitations under the License.
|
15
15
|
*/
|
16
16
|
const { startsWith } = require('lodash/fp');
|
17
|
-
const {
|
17
|
+
const { SNSClient, PublishCommand } = require('@aws-sdk/client-sns');
|
18
18
|
const { createTwilioClient } = require('./twilio-factory');
|
19
19
|
|
20
20
|
const singaporePhoneCode = '+65';
|
@@ -39,9 +39,16 @@ const initSendSmsNotification =
|
|
39
39
|
onlyTwilioSms,
|
40
40
|
}) =>
|
41
41
|
async ({ message, phoneNumber, senderId }) => {
|
42
|
-
const awsSns =
|
43
|
-
|
44
|
-
|
42
|
+
const awsSns = new SNSClient({
|
43
|
+
apiVersion: '2010-03-31',
|
44
|
+
region: awsRegion,
|
45
|
+
credentials: awsEndpoint
|
46
|
+
? {
|
47
|
+
accessKeyId: 'tests-key-id',
|
48
|
+
secretAccessKey: 'tests-key',
|
49
|
+
}
|
50
|
+
: awsEndpoint,
|
51
|
+
endpoint: awsEndpoint,
|
45
52
|
});
|
46
53
|
const { sendTwilioSms } = createTwilioClient({
|
47
54
|
twilioAccountSid,
|
@@ -54,8 +61,8 @@ const initSendSmsNotification =
|
|
54
61
|
return;
|
55
62
|
}
|
56
63
|
|
57
|
-
await awsSns
|
58
|
-
|
64
|
+
await awsSns.send(
|
65
|
+
new PublishCommand({
|
59
66
|
Message: message,
|
60
67
|
PhoneNumber: phoneNumber,
|
61
68
|
MessageAttributes: {
|
@@ -66,7 +73,7 @@ const initSendSmsNotification =
|
|
66
73
|
},
|
67
74
|
},
|
68
75
|
})
|
69
|
-
|
76
|
+
);
|
70
77
|
};
|
71
78
|
|
72
79
|
module.exports = {
|
package/src/aws-factory.js
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright 2023 Velocity Team
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
|
17
|
-
const AWS = require('aws-sdk');
|
18
|
-
|
19
|
-
const testsConfig = (awsEndpoint) =>
|
20
|
-
awsEndpoint
|
21
|
-
? {
|
22
|
-
credentials: new AWS.Credentials('tests-kei-id', 'tests-key'),
|
23
|
-
endpoint: awsEndpoint,
|
24
|
-
}
|
25
|
-
: {};
|
26
|
-
|
27
|
-
const createKmsClient = ({ awsRegion, awsEndpoint }) => {
|
28
|
-
return new AWS.KMS({
|
29
|
-
region: awsRegion,
|
30
|
-
...testsConfig(awsEndpoint),
|
31
|
-
});
|
32
|
-
};
|
33
|
-
|
34
|
-
const createSesClient = ({ awsRegion, awsEndpoint }) =>
|
35
|
-
new AWS.SES({
|
36
|
-
apiVersion: '2010-12-01',
|
37
|
-
region: awsRegion,
|
38
|
-
...testsConfig(awsEndpoint),
|
39
|
-
});
|
40
|
-
|
41
|
-
const createSesV2Client = ({ awsRegion }) =>
|
42
|
-
new AWS.SESV2({
|
43
|
-
apiVersion: '2019-09-27',
|
44
|
-
region: awsRegion,
|
45
|
-
});
|
46
|
-
|
47
|
-
const createDynamoDbDocumentClient = ({ awsRegion, awsEndpoint }) =>
|
48
|
-
new AWS.DynamoDB.DocumentClient({
|
49
|
-
apiVersion: '2012-08-10',
|
50
|
-
region: awsRegion,
|
51
|
-
...testsConfig(awsEndpoint),
|
52
|
-
});
|
53
|
-
|
54
|
-
const createS3Client = ({
|
55
|
-
awsRegion,
|
56
|
-
awsEndpoint,
|
57
|
-
accessKeyId,
|
58
|
-
secretAccessKey,
|
59
|
-
isTest,
|
60
|
-
}) =>
|
61
|
-
new AWS.S3({
|
62
|
-
apiVersion: '2006-03-01',
|
63
|
-
region: awsRegion,
|
64
|
-
signatureVersion: 'v4',
|
65
|
-
accessKeyId,
|
66
|
-
secretAccessKey,
|
67
|
-
s3ForcePathStyle: isTest,
|
68
|
-
...testsConfig(awsEndpoint),
|
69
|
-
});
|
70
|
-
|
71
|
-
const createSnsClient = ({ awsRegion, awsEndpoint }) =>
|
72
|
-
new AWS.SNS({
|
73
|
-
apiVersion: '2010-03-31',
|
74
|
-
region: awsRegion,
|
75
|
-
...testsConfig(awsEndpoint),
|
76
|
-
});
|
77
|
-
|
78
|
-
module.exports = {
|
79
|
-
createSnsClient,
|
80
|
-
createSesClient,
|
81
|
-
createSesV2Client,
|
82
|
-
createDynamoDbDocumentClient,
|
83
|
-
createS3Client,
|
84
|
-
createKmsClient,
|
85
|
-
};
|