-
Notifications
You must be signed in to change notification settings - Fork 21
/
gost-aws.js
143 lines (131 loc) · 4.64 KB
/
gost-aws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses');
const { S3Client } = require('@aws-sdk/client-s3');
const { SQSClient } = require('@aws-sdk/client-sqs');
const { getSignedUrl: awsGetSignedUrl } = require('@aws-sdk/s3-request-presigner');
/*
----------------------------------------------------------
AWS S3
----------------------------------------------------------
*/
function getS3Client() {
let s3;
if (process.env.LOCALSTACK_HOSTNAME) {
/*
1. Make sure the local environment has awslocal installed.
2. Use the commands to create a bucket to test with.
- awslocal s3api create-bucket --bucket arpa-audit-reports --region us-west-2 --create-bucket-configuration '{"LocationConstraint": "us-west-2"}'
3. Access bucket resource metadata through the following URL.
- awslocal s3api list-buckets
- awslocal s3api list-objects --bucket arpa-audit-reports
*/
console.log('------------ USING LOCALSTACK ------------');
const endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
console.log(`endpoint: ${endpoint}`);
s3 = new S3Client({
endpoint,
forcePathStyle: true,
region: process.env.AWS_DEFAULT_REGION,
});
} else {
s3 = new S3Client();
}
return s3;
}
/**
* This function is a wrapper around the getSignedUrl function from the @aws-sdk/s3-request-presigner package.
* Exists to organize the imports and to make it easier to mock in tests.
*/
async function getSignedUrl(s3, command, options) {
return awsGetSignedUrl(s3, command, options);
}
/*
----------------------------------------------------------
AWS SES
----------------------------------------------------------
*/
function getSESClient() {
const sesOptions = {};
if (process.env.LOCALSTACK_HOSTNAME) {
sesOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
sesOptions.region = process.env.AWS_DEFAULT_REGION;
}
return new SESClient(sesOptions);
}
async function sendEmail(message) {
if (process.env.SUPPRESS_EMAIL) return;
if (!process.env.NOTIFICATIONS_EMAIL) throw new Error('NOTIFICATIONS_EMAIL is not set');
const transport = module.exports.getSESClient();
const source = message.fromName
? `"${message.fromName}" <${process.env.NOTIFICATIONS_EMAIL}>`
: process.env.NOTIFICATIONS_EMAIL;
const params = {
Destination: {
ToAddresses: [message.toAddress],
},
Source: source,
Message: {
Subject: {
Charset: 'UTF-8',
Data: message.subject,
},
Body: {
Html: {
Charset: 'UTF-8',
Data: message.body,
},
Text: {
Charset: 'UTF-8',
Data: message.text,
},
},
},
Tags: message.tags.map((tag) => {
// Tags must be strings of format 'name=value'
const match = tag.match(/(?<name>\w+)=(?<value>\w+)/);
if (!match) {
return null;
}
return {
Name: match.groups.name,
Value: match.groups.value,
};
}).filter((tagObj) => !!tagObj),
};
if (message.ccAddress) {
params.Destination.CcAddresses = [message.ccAddress];
}
if (process.env.SES_CONFIGURATION_SET_DEFAULT) {
params.ConfigurationSetName = process.env.SES_CONFIGURATION_SET_DEFAULT;
}
const command = new SendEmailCommand(params);
try {
const data = await transport.send(command);
console.log('Success sending SES email:', JSON.stringify(data));
} catch (err) {
console.error('Error sending SES email:', err, err.stack);
throw err;
}
}
/*
----------------------------------------------------------
AWS SQS
----------------------------------------------------------
*/
function getSQSClient() {
let sqs;
if (process.env.LOCALSTACK_HOSTNAME) {
console.log('------------ USING LOCALSTACK FOR SQS ------------');
const endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
sqs = new SQSClient({ endpoint, region: process.env.AWS_DEFAULT_REGION });
} else {
sqs = new SQSClient();
}
return sqs;
}
module.exports = {
getS3Client,
getSignedUrl,
getSESClient,
sendEmail,
getSQSClient,
};