-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
443 lines (417 loc) · 14.8 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { createDataSyncLocation, createTask, startTask } from '#lib/datasync.js';
import { updateBucketPolicy } from '#lib/s3.js';
import { DataSyncClient } from '@aws-sdk/client-datasync';
import { S3Client } from '@aws-sdk/client-s3';
/**
* AWS client [configuration and credential][1] settings.
*
* [1]: https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html#creatingServiceClients
*
* @typedef AwsConfig
* @property {string} region AWS region.
* @property {object} credentials AWS client credentials.
* @property {string} credentials.accessKeyId AWS client access key ID.
* @property {string} credentials.secretAccessKey AWS client secret access key.
*/
/**
* Options for DataSync-S3 bucket transfer.
* @typedef DataSyncS3TransferOptions
*
* @property {'source' | 'destination'} initiatingAccount
* Indicates whether the source or destination AWS account will be used for
* creating the necessary DataSync resources, and will be responsible for
* initiating the S3 object transfer.
*
* @property {string} cloudWatchLogGroup
* ARN of an existing CloudWatch log group, where DataSync logs will be recorded
* into.
*
* The log group must be owned by the initiating AWS account.
*
* @property {string} dataSyncPrincipal
* [AWS principal][1] scoped to the initiating AWS account, to be granted
* `s3:ListBucket` on the the bucket pressumably not owned by the initiating AWS
* account.
*
* This is useful for finer security control over the latter S3 bucket,
* [especially for cross-account S3 transfers][2], wherein the source and
* destination buckets are owned by different AWS accounts. If the buckets
* belong to the same AWS account, you can simply specify the AWS account ID as
* principal, or a narrower version involving it (e.g., an existing IAM role).
*
* [1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html#Principal_specifying
* [2]: https://docs.aws.amazon.com/datasync/latest/userguide/tutorial_s3-s3-cross-account-transfer.html#s3-s3-cross-account-update-s3-policy-destination-account
*
* @property {string} dataSyncRole
* ARN of an existing IAM role that DataSync will assume when executing the
* transfer task.
*
* This role must be owned by the initiating AWS account, and must have the
* [necessary permissions][1] for the transfer task to execute successfully.
*
* [1]: https://docs.aws.amazon.com/datasync/latest/userguide/create-s3-location.html#create-role-manually
*/
/**
* ARNs of DataSync resources created as a result of a transfer task.
* @typedef DataSyncS3TransferOutput
*
* @property {string} dataSyncSrcLocation DataSync source location ARN.
* @property {string} dataSyncDestLocation DataSync destination location ARN.
* @property {string} dataSyncTask ARN of the DataSync transfer task.
* @property {string} dataSyncTaskExec DataSync task execution ARN.
*/
/**
* Build the function that allows you to transfer S3 objects from source to
* destination buckets via DataSync, on demand.
*
* The necessary AWS service clients are created under the hood which, together
* with the provided `options`, the returned function uses to perform
* the DataSync-S3 transfer. These settings are then reused when subsequent
* bucket transfers are made by calling the same function that was returned.
*
* Note that the provided `srcAwsConfig` must be credentials coming from the AWS
* account that owns the source buckets. Similarly, `destAwsConfig` must be
* credentials from the AWS account that owns the destination buckets.
*
* @param {AwsConfig} srcAwsConfig
* AWS config for the AWS account that owns the source S3 bucket, i.e., where
* objects will be copied from.
*
* @param {AwsConfig} destAwsConfig
* AWS config for the AWS account that owns the destination S3 bucket, i.e.,
* where objects will be copied to.
*
* @param {DataSyncS3TransferOptions} options
* Additional options for the DataSync-S3 bucket transfer.
*
* @example
* <caption>Transfer S3 objects from source to destination bucket under the same AWS account.</caption>
*
* ```js
* // prepare your AWS account client credentials
* const awsConfig = {
* region: 'ap-northeast-1',
* credentials: {
* accessKeyId: 'ABCDEFGHIJKLMNOPQRST',
* secretAccessKey: 'WhHGQwvmvDaTne9LnMHV72A4cUkPkZWv2q6ieFtX'
* }
* };
*
* const execDataSyncS3Transfer = initDataSyncS3Transfer(
* awsConfig,
* awsConfig,
* {
* srcDataSyncRole: 'arn:aws:iam::123456789012:role/MyExistingRole',
* srcDataSyncPrincipal: '123456789012',
* srcCloudWatchLogGroup: 'arn:aws:logs:ap-northeast-1:123456789012:log-group:/aws/datasync:*'
* }
* );
* ```
*
* @example
* <caption>Cross-account S3 bucket object transfer.</caption>
*
* ```js
* // prepare client credentials for your source AWS account "123456789012"
* const srcAwsConfig = {
* region: 'ap-northeast-1',
* credentials: {
* accessKeyId: 'ABCDEFGHIJKLMNOPQRST',
* secretAccessKey: 'WhHGQwvmvDaTne9LnMHV72A4cUkPkZWv2q6ieFtX'
* }
* };
*
* // prepare client credentials for the destination AWS account
* const destAwsConfig = {
* region: 'ap-northeast-1',
* credentials: {
* accessKeyId: 'GHIJKLMNOPQRSTUVWXYZ',
* secretAccessKey: 'BtW2Dy57xmExsCtUALxntVToEHk29bXZuU3TmMv2'
* }
* };
*
* const execDataSyncS3Transfer = initDataSyncS3Transfer(
* srcAwsConfig,
* destAwsConfig,
* {
* srcDataSyncRole: 'arn:aws:iam::123456789012:role/MyExistingRole',
* srcDataSyncPrincipal: '123456789012',
* srcCloudWatchLogGroup: 'arn:aws:logs:ap-northeast-1:123456789012:log-group:/aws/datasync:*'
* }
* );
* ```
*
* @see {@link execDataSyncS3Transfer} on how to use the returned function to make transfers.
*/
export function initDataSyncS3Transfer(srcAwsConfig, destAwsConfig, options) {
const dataSyncClient = new DataSyncClient(
options.initiatingAccount === 'source'
? srcAwsConfig
: destAwsConfig
);
const srcS3Client = new S3Client(srcAwsConfig);
const destS3Client = new S3Client(destAwsConfig);
/**
* Transfer S3 objects from source to destination bucket, by creating and
* executing a DataSync task.
*
* Each transfer from source to destination S3 buckt creates the following
* DataSync resources, in order:
* 1. DataSync source location, pointing to the source S3 bucket;
* 1. DataSync destination location, pointing to the destination S3 bucket;
* 1. DataSync task, with the proper transfer configurations; and
* 1. DataSync task execution, which is the execution of the created DataSync
* task.
*
* @param {string} srcBucket
* Existing S3 bucket name where objects will be copied from.
*
* The source bucket must belong to the same AWS account that the
* `srcDatasyncClient` argument is configured for during the initialization
* call to {@link initDataSyncS3Transfer}.
*
* @param {string} destBucket
* Existing S3 bucket name where objects will be copied to.
*
* The destination bucket must belong to the same AWS account that the
* `destS3Client` argument is configured for during the initialization call to
* {@link initDataSyncS3Transfer}.
*
* @param {string} taskName
* name to assign to the DataSync transfer task that will be created and
* executed.
*
* @param {Partial<DataSyncS3TransferOutput>} prevState
* If the call to this function fails for any reason, it likely causes at
* least one resource, meant to be created for the transfer, to not be
* created.
*
* In these cases, this function allows you to retry the failed resource
* creation(s), by feeding onto this parameter the previous output that this
* function has returned, which contains the AWS resources created so far.
* When this is done, this function will reuse any created resources instead
* of creating them, and only create ones that have not been created.
*
* If you pass onto this parameter with all resources created, this function
* will not create anymore resources, and will only create a new execution of
* the provided task.
*
* @returns All DataSync created for the transfer.
*/
function execDataSyncS3Transfer(srcBucket, destBucket, taskName, prevState = {}) {
return _execDataSyncS3Transfer(taskName, srcBucket, destBucket, options, srcS3Client, destS3Client, dataSyncClient, prevState);
}
return execDataSyncS3Transfer;
}
/**
* Check if there is a missing step in making a DataSync transfer, based on the
* given transfer output.
*
* If there is, an {@link Error} is constructed, containing details as to which
* step of the transfer process did the transfer started failing.
*
* @param {Partial<DataSyncS3TransferOutput>} transferOutput
* The transfer output.
*
* @returns An {@link Error} if there is, `null` otherwise.
*/
export function checkIncompleteTransfer(transferOutput) {
let missing = [];
if (!transferOutput.dataSyncSrcLocation) {
missing.push('source location');
}
if (!transferOutput.dataSyncDestLocation) {
missing.push('destination location');
}
if (!transferOutput.dataSyncTask) {
missing.push('task');
}
if (!transferOutput.dataSyncTaskExec) {
missing.push('task execution');
}
return missing.length === 0
? null
: new Error(
'Transfer process is incomplete.',
{ cause: `DataSync resources missing: ${missing.join(', ')}.` }
);
}
/**
* Create and execute a DataSync task to transfer S3 objects from source to
* destination bucket.
*
* Each DataSync transfer from source to destination S3 buckt creates the
* following DataSync resources, in order:
* 1. DataSync source location, pointing to the source S3 bucket;
* 1. DataSync destination location, pointing to the destination S3 bucket;
* 1. DataSync task, with the proper transfer configurations; and
* 1. DataSync task execution, which is the execution of the created DataSync
* task.
*
* @param {string} taskName
* name to assign to the DataSync transfer task.
* @param {string} srcBucket
* Existing S3 bucket name where objects will be copied from.
*
* The source bucket must belong to the same AWS account that the
* `srcDatasyncClient` argument is configured for.
*
* @param {string} destBucket
* Existing S3 bucket name where objects will be copied to.
*
* The destination bucket must belong to the same AWS account that the
* `destS3Client` argument is configured for.
*
* @param {DataSyncS3TransferOptions} options
* Additional options for DataSync-S3 bucket transfer.
*
* @param {S3Client} srcS3Client
* Client for the S3 service of the same AWS account that owns the source
* bucket.
*
* @param {S3Client} destS3Client
* Client for the S3 service of the same AWS account that owns the destination
* bucket.
*
* @param {DataSyncClient} dataSyncClient
* Client for the DataSync service of the initiating AWS account.
*
* @param {Partial<DataSyncS3TransferOutput>} prevState
* If the call to this function fails for any reason, it likely causes at
* least one resource, meant to be created for the transfer, to not be
* created.
*
* In these cases, this function allows you to retry the failed resource
* creation(s), by feeding onto this parameter the previous output that this
* function has returned, which contains the AWS resources created so far.
* When this is done, this function will reuse any created resources instead
* of creating them, and only create ones that have not been created.
*
* If you pass onto this parameter with all resources created, this function
* will not create anymore resources, and will only create a new execution of
* the provided task.
*
* @returns
* Resources created as byproduct of the DataSync task execution, and a
* contained exception if there is any during processing.
*/
async function _execDataSyncS3Transfer(taskName, srcBucket, destBucket, options, srcS3Client, destS3Client, dataSyncClient, prevState) {
/**
* @type {Partial<DataSyncS3TransferOutput>}.
*/
const result = {};
// allow creating datasync location for either source or destination bucket if
// the bucket is not owned by the initiating aws account
if (options.initiatingAccount === 'source') {
try {
await updateBucketPolicy(
destBucket,
options.dataSyncPrincipal,
options.dataSyncRole,
destS3Client
);
}
catch (error) {
return { result, error };
}
}
else {
try {
await updateBucketPolicy(
srcBucket,
options.dataSyncPrincipal,
options.dataSyncRole,
srcS3Client
);
}
catch (error) {
return { result, error };
}
}
// prepare datasync S3 source
if (prevState.dataSyncSrcLocation) {
result.dataSyncSrcLocation = prevState.dataSyncSrcLocation;
}
else {
try {
let response = await createDataSyncLocation(
`arn:aws:s3:::${srcBucket}`,
options.dataSyncRole,
dataSyncClient
);
if (!response.LocationArn) {
throw new Error(
'DataSync location ARN not found.',
{ cause: 'No ARN returned from server.' }
);
}
result.dataSyncSrcLocation = response.LocationArn;
}
catch (error) {
return { result, error };
}
}
// prepare datasync s3 destination
if (prevState.dataSyncDestLocation) {
result.dataSyncDestLocation = prevState.dataSyncDestLocation;
}
else {
try {
let response = await createDataSyncLocation(
`arn:aws:s3:::${destBucket}`,
options.dataSyncRole,
dataSyncClient
);
if (!response.LocationArn) {
throw new Error(
'DataSync location ARN not found.',
{ cause: 'No ARN returned from server.' }
);
}
result.dataSyncDestLocation = response.LocationArn;
}
catch (error) {
return { result, error };
}
}
// prepare transfer task
if (prevState.dataSyncTask) {
result.dataSyncTask = prevState.dataSyncTask;
}
else {
try {
let response = await createTask(
taskName,
result.dataSyncSrcLocation,
result.dataSyncDestLocation,
options.cloudWatchLogGroup,
dataSyncClient
);
if (!response.TaskArn) {
throw new Error(
'DataSync task ARN not found.',
{ cause: 'No ARN returned from server.' }
);
}
result.dataSyncTask = response.TaskArn;
}
catch (error) {
return { result, error };
}
}
// execute transfer task
try {
let response = await startTask(result.dataSyncTask, dataSyncClient);
if (!response.TaskExecutionArn) {
throw new Error(
'DataSync task execution ARN not found.',
{ cause: 'No ARN returned from server.' }
);
}
result.dataSyncTaskExec = response.TaskExecutionArn;
}
catch (error) {
return { result, error };
}
return { result, error: null };
}