-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathobjectCopy.js
More file actions
1185 lines (1141 loc) · 53 KB
/
Copy pathobjectCopy.js
File metadata and controls
1185 lines (1141 loc) · 53 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const async = require('async');
const { PassThrough } = require('stream');
const { errors, errorInstances, jsutil, versioning, s3middleware, s3routes } = require('arsenal');
const { validateObjectKeyLength } = s3routes.routesUtils;
const getMetaHeaders = s3middleware.userMetadata.getMetaHeaders;
const validateHeaders = s3middleware.validateConditionalHeaders;
const constants = require('../../constants');
const collectCorsHeaders = require('../utilities/collectCorsHeaders');
const locationConstraintCheck = require('./apiUtils/object/locationConstraintCheck');
const { checkQueryVersionId, versioningPreprocessing, decodeVID } = require('./apiUtils/object/versioning');
const getReplicationInfo = require('./apiUtils/object/getReplicationInfo');
const { data } = require('../data/wrapper');
const services = require('../services');
const { pushMetric } = require('../utapi/utilities');
const removeAWSChunked = require('./apiUtils/object/removeAWSChunked');
const { standardMetadataValidateBucketAndObj } = require('../metadata/metadataUtils');
const validateWebsiteHeader = require('./apiUtils/object/websiteServing').validateWebsiteHeader;
const { config } = require('../Config');
const monitoring = require('../utilities/monitoringHandler');
const applyZenkoUserMD = require('./apiUtils/object/applyZenkoUserMD');
const { getObjectSSEConfiguration } = require('./apiUtils/bucket/bucketEncryption');
const { setExpirationHeaders } = require('./apiUtils/object/expirationHeaders');
const { verifyColdObjectAvailable } = require('./apiUtils/object/coldStorage');
const { setSSEHeaders } = require('./apiUtils/object/sseHeaders');
const { updateEncryption } = require('./apiUtils/bucket/updateEncryption');
const { initializeInternalLogRequestQueue, queueInternalLogRequest } = require('../utilities/serverAccessLogger');
const {
algorithms,
arsenalErrorFromChecksumError,
getCopyObjectChecksumAlgorithm,
} = require('./apiUtils/integrity/validateChecksums');
const ChecksumTransform = require('../auth/streamingV4/ChecksumTransform');
const ChecksumWritable = require('../auth/streamingV4/ChecksumWritable');
const kms = require('../kms/wrapper');
const versionIdUtils = versioning.VersionID;
const locationHeader = constants.objectLocationConstraintHeader;
const versioningNotImplBackends = constants.versioningNotImplBackends;
const externalVersioningErrorMessage =
'We do not currently support putting a versioned object to a location-constraint of type AWS or Azure or GCP.';
/**
* Compute the prior data locations that are orphaned.
*
* @param {Array} dataToDelete - prior data locations from versioningPreprocessing
* @param {Array} newDataGetInfo - new data locations stored in the new metadata
* @returns {Array|null} orphaned entries to batchDelete, or null if none
*/
function _orphanedDataLocations(dataToDelete, newDataGetInfo) {
// Identity is (dataStoreName, key): the same key under a different
// dataStoreName points to a different backend slot, so the old slot
// is an orphan even when the key string collides. dataStoreName is a
// location-constraint name (never contains '|'), so the delimiter is
// unambiguous.
// We need to normalize to support old legacy location names.
const normalize = loc => (typeof loc === 'string' ? { key: loc } : loc);
const locId = loc => {
const n = normalize(loc);
return `${n.dataStoreName}|${n.key}`;
};
const newIds = new Set((newDataGetInfo || []).map(locId));
const orphans = (dataToDelete || []).filter(loc => !newIds.has(locId(loc)));
return orphans.length > 0 ? orphans : null;
}
/**
* Concatenate the source object's parts into a single Readable stream by
* reading them sequentially through `data.get`. Returns the PassThrough
* immediately; consumers should pipe it to the next stage and observe
* `error` on this stream for any read failure along the way.
*
* @param {Array} dataLocator - ordered source parts
* @param {object} log - request logger
* @return {PassThrough}
*/
function _pipeSourcePartsThrough(dataLocator, log) {
const passthrough = new PassThrough();
const wrapErr = (err, part) =>
Object.assign(err, {
copyPart: { key: part.key, dataStoreName: part.dataStoreName, dataStoreType: part.dataStoreType },
});
async.eachSeries(
dataLocator,
(part, cb) => {
const done = jsutil.once(cb);
if (part.dataStoreType === 'azure') {
// Azure's data.get writes part bytes into the provided writable
// instead of returning a Readable. Pipe a per-part PassThrough
// into the master passthrough and use its 'end' as the completion
// signal — same pattern arsenal's data.copyObject uses.
const perPart = new PassThrough();
perPart.once('error', err => done(wrapErr(err, part)));
perPart.once('end', () => done());
perPart.pipe(passthrough, { end: false });
return data.get(part, perPart, log, err => {
if (err) {
perPart.destroy(err);
done(wrapErr(err, part));
}
});
}
return data.get(part, null, log, (err, partStream) => {
if (err) {
return done(wrapErr(err, part));
}
partStream.once('error', err => done(wrapErr(err, part)));
partStream.once('end', () => done());
partStream.pipe(passthrough, { end: false });
return undefined;
});
},
err => {
if (err) {
passthrough.destroy(err);
} else {
passthrough.end();
}
},
);
return passthrough;
}
/**
* Decide whether the destination's checksum needs to be recomputed by
* streaming the source bytes through a ChecksumTransform.
*
* @param {object} headers - request headers
* @param {object} sourceObjMD - source object metadata
* @returns {boolean}
*/
function _shouldRecomputeChecksum(headers, sourceObjMD) {
const requestedAlgo = headers['x-amz-checksum-algorithm']?.toLowerCase();
if (
sourceObjMD.checksum?.checksumType === 'FULL_OBJECT' &&
(!requestedAlgo || requestedAlgo === sourceObjMD.checksum.checksumAlgorithm)
) {
return false;
}
return true;
}
/**
* Pick the checksum algorithm to write on CopyObject. Defaults to CRC64NVME when the source object has no checksum.
*
* @param {string|null} requestedAlgo - validated request algorithm
* @param {object} sourceObjMD - source object metadata
* @returns {string}
*/
function _getCopyObjectChecksumAlgorithm(requestedAlgo, sourceObjMD) {
return requestedAlgo || sourceObjMD.checksum?.checksumAlgorithm || 'crc64nvme';
}
/**
* Recompute the destination's checksum by streaming the source bytes through
* a checksum stream. When the bytes don't have to move (copy-to-self, same
* location, no SSE, not versioned) the source is GET'ed solely to compute the
* new digest via a ChecksumWritable sink; the bytes are then discarded and the
* existing data location is reused — only cloudserver's metadata is updated.
* Otherwise the source is piped through a ChecksumTransform into a single put.
*
* @param {object} log - request logger
* @param {object} request - HTTP request
* @param {string|null} requestedAlgo - client-requested algorithm, lowercase
* @param {object} sourceObjMD - source object metadata
* @param {Array} dataLocator - ordered source parts
* @param {boolean} sourceIsDestination - source key == destination key
* @param {boolean} isVersionedObj - destination bucket has versioning enabled
* @param {boolean} needsEncryption - destination requires SSE
* @param {object} storeMetadataParams - mutable carrier for destination metadata
* @param {object} destObjMD - existing destination object metadata
* @param {object} destBucketMD - destination bucket metadata
* @param {object} serverSideEncryption - SSE config for the destination
* @param {object} dataStoreContext - context passed to data.put
* @param {object} backendInfoDest - backend info for the destination
* @param {function} next - waterfall callback
* @return {undefined}
*/
function _recomputeChecksumAndStore(
log,
request,
requestedAlgo,
sourceObjMD,
dataLocator,
sourceIsDestination,
isVersionedObj,
needsEncryption,
storeMetadataParams,
destObjMD,
destBucketMD,
serverSideEncryption,
dataStoreContext,
backendInfoDest,
next,
) {
const algoName = _getCopyObjectChecksumAlgorithm(requestedAlgo, sourceObjMD);
const wrapChecksumErr = err => Object.assign(err, { checksumStream: { algorithm: algoName } });
// Copy-to-self where the bytes don't have to move (same location, no SSE
// change, not versioned): GET the source into a ChecksumWritable to compute
// the new digest, discard the bytes, and reuse the existing data locations.
// No PUT, no DELETE — only the metadata gets updated.
if (sourceIsDestination && storeMetadataParams.locationMatch && !needsEncryption && !isVersionedObj) {
log.debug('computing checksum on copy-to-self without rewriting data', {
algorithm: algoName,
size: storeMetadataParams.size,
});
const sourceStream = _pipeSourcePartsThrough(dataLocator, log);
const checksumSink = new ChecksumWritable(algoName, log);
const finish = jsutil.once(err => {
if (err) {
sourceStream.destroy(err);
checksumSink.destroy(err);
if (request.sourceServerAccessLog) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog.error = err;
}
return next(err, destBucketMD);
}
// eslint-disable-next-line no-param-reassign
storeMetadataParams.checksum = {
algorithm: algoName,
value: checksumSink.digest,
type: 'FULL_OBJECT',
};
return next(null, storeMetadataParams, dataLocator, destObjMD, serverSideEncryption, destBucketMD);
});
sourceStream.once('error', finish);
checksumSink.once('error', err => finish(wrapChecksumErr(err)));
checksumSink.once('finish', () => finish());
sourceStream.pipe(checksumSink);
return undefined;
}
// Stream source bytes through a ChecksumTransform and write them out as a single put.
log.debug('recomputing checksum on CopyObject', { algorithm: algoName, size: storeMetadataParams.size });
const sourceStream = _pipeSourcePartsThrough(dataLocator, log);
const checksumStream = new ChecksumTransform(algoName, undefined, false, log);
const done = jsutil.once((err, results) => {
if (err) {
sourceStream.destroy(err);
checksumStream.destroy(err);
if (request.sourceServerAccessLog) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog.error = err;
}
return next(err, destBucketMD);
}
return next(null, storeMetadataParams, results, destObjMD, serverSideEncryption, destBucketMD);
});
sourceStream.once('error', done);
checksumStream.once('error', err => done(wrapChecksumErr(err)));
sourceStream.pipe(checksumStream);
const doPut = cipherBundle =>
data.put(
cipherBundle,
checksumStream,
storeMetadataParams.size,
dataStoreContext,
backendInfoDest,
log,
(err, dataRetrievalInfo, hashedStream) => {
if (err) {
return done(err);
}
// eslint-disable-next-line no-param-reassign
storeMetadataParams.checksum = {
algorithm: algoName,
value: checksumStream.digest,
type: 'FULL_OBJECT',
};
// Prefix the part number like createAndStoreObject does, so
// partNumber-based reads see the copy as a single-part object.
const dataStoreETag = dataRetrievalInfo.dataStoreETag || hashedStream?.completedHash;
const putResult = {
key: dataRetrievalInfo.key,
size: storeMetadataParams.size,
start: 0,
dataStoreName: dataRetrievalInfo.dataStoreName,
dataStoreType: dataRetrievalInfo.dataStoreType,
dataStoreETag: dataStoreETag ? `1:${dataStoreETag}` : undefined,
dataStoreVersionId: dataRetrievalInfo.dataStoreVersionId,
};
if (cipherBundle) {
putResult.cryptoScheme = cipherBundle.cryptoScheme;
putResult.cipheredDataKey = cipherBundle.cipheredDataKey;
}
return done(null, [putResult]);
},
);
if (serverSideEncryption?.algorithm) {
return kms.createCipherBundle(serverSideEncryption, log, (err, cipherBundle) => {
if (err) {
return done(err);
}
return doPut(cipherBundle);
});
}
return doPut(null);
}
/**
* Preps metadata to be saved (based on copy or replace request header)
* @param {object} request - request
* @param {object} sourceObjMD - object md of source object
* @param {object} headers - request headers
* @param {boolean} sourceIsDestination - whether or not source is same as
* destination
* @param {AuthInfo} authInfo - authInfo from Vault
* @param {string} objectKey - destination key name
* @param {object} sourceBucketMD - bucket metadata of source bucket
* @param {object} destBucketMD - bucket metadata of bucket being copied to
* @param {string} sourceVersionId - versionId of source object for copy
* @param {object} log - logger object
* @return {object}
* - (storeMetadataParams
* - sourceLocationConstraintName {string} - location type of the source
* - OR error
*/
function _prepMetadata(
request,
sourceObjMD,
headers,
sourceIsDestination,
authInfo,
objectKey,
sourceBucketMD,
destBucketMD,
sourceVersionId,
log,
) {
let whichMetadata = headers['x-amz-metadata-directive'];
// Default is COPY
whichMetadata = whichMetadata === undefined ? 'COPY' : whichMetadata;
if (whichMetadata !== 'COPY' && whichMetadata !== 'REPLACE') {
return { error: errors.InvalidArgument };
}
let whichTagging = headers['x-amz-tagging-directive'];
// Default is COPY
whichTagging = whichTagging === undefined ? 'COPY' : whichTagging;
if (whichTagging !== 'COPY' && whichTagging !== 'REPLACE') {
return { error: errorInstances.InvalidArgument.customizeDescription('Unknown tagging directive') };
}
const overrideMetadata = {};
if (headers['x-amz-server-side-encryption']) {
overrideMetadata['x-amz-server-side-encryption'] = headers['x-amz-server-side-encryption'];
}
if (headers['x-amz-storage-class']) {
// TODO: remove in CLDSRV-639
overrideMetadata['x-amz-storage-class'] = headers['x-amz-storage-class'];
}
if (headers['x-amz-website-redirect-location']) {
overrideMetadata['x-amz-website-redirect-location'] = headers['x-amz-website-redirect-location'];
}
const retentionHeaders = headers['x-amz-object-lock-mode'] && headers['x-amz-object-lock-retain-until-date'];
const legalHoldHeader = headers['x-amz-object-lock-legal-hold'];
if ((retentionHeaders || legalHoldHeader) && !destBucketMD.isObjectLockEnabled()) {
return {
error: errorInstances.InvalidRequest.customizeDescription('Bucket is missing ObjectLockConfiguration'),
};
}
// Cannot copy from same source and destination if no MD
// changed and no source version id
if (
sourceIsDestination &&
whichMetadata === 'COPY' &&
Object.keys(overrideMetadata).length === 0 &&
!sourceVersionId
) {
return {
error: errorInstances.InvalidRequest.customizeDescription(
'This copy' +
' request is illegal because it is trying to copy an ' +
"object to itself without changing the object's metadata, " +
'storage class, website redirect location or encryption ' +
'attributes.',
),
};
}
// If COPY, pull all x-amz-meta keys/values from source object
// Otherwise, pull all x-amz-meta keys/values from request headers
const userMetadata = whichMetadata === 'COPY' ? getMetaHeaders(sourceObjMD) : getMetaHeaders(headers);
if (userMetadata instanceof Error) {
log.debug('user metadata validation failed', {
error: userMetadata,
method: 'objectCopy',
});
return { error: userMetadata };
}
// if the request occurs within a Zenko deployment, we place a user-metadata
// field on the object
applyZenkoUserMD(userMetadata);
// If metadataDirective is:
// - 'COPY' and source object has a location constraint in its metadata
// we use the bucket destination location constraint
if (whichMetadata === 'COPY' && userMetadata[locationHeader] && destBucketMD.getLocationConstraint()) {
userMetadata[locationHeader] = destBucketMD.getLocationConstraint();
}
const backendInfoObjSource = locationConstraintCheck(request, sourceObjMD, sourceBucketMD, log);
if (backendInfoObjSource.err) {
return { error: backendInfoObjSource.err };
}
const sourceLocationConstraintName = backendInfoObjSource.controllingLC;
const backendInfoObjDest = locationConstraintCheck(request, userMetadata, destBucketMD, log);
if (backendInfoObjDest.err) {
return { error: backendInfoObjDest.err };
}
const destLocationConstraintName = backendInfoObjDest.controllingLC;
// If location constraint header is not included, locations match
const locationMatch = sourceLocationConstraintName === destLocationConstraintName;
// If tagging directive is REPLACE but you don't specify any
// tags in the request, the destination object will
// not have any tags.
// If tagging directive is COPY but the source object does not have tags,
// the destination object will not have any tags.
let tagging;
let taggingCopy;
if (whichTagging === 'COPY') {
taggingCopy = sourceObjMD.tags || {};
} else {
tagging = headers['x-amz-tagging'] || '';
}
// If COPY, pull the necessary headers from source object
// Otherwise, pull them from request headers
const headersToStoreSource = whichMetadata === 'COPY' ? sourceObjMD : headers;
const storeMetadataParams = {
objectKey,
log,
headers,
authInfo,
metaHeaders: userMetadata,
size: sourceObjMD['content-length'],
contentType: headersToStoreSource['content-type'],
contentMD5: sourceObjMD['content-md5'],
cacheControl: headersToStoreSource['cache-control'],
contentDisposition: headersToStoreSource['content-disposition'],
contentEncoding: removeAWSChunked(headersToStoreSource['content-encoding']),
dataStoreName: destLocationConstraintName,
expires: headersToStoreSource.expires,
overrideMetadata,
lastModifiedDate: new Date().toJSON(),
tagging,
taggingCopy,
replicationInfo: getReplicationInfo(config, objectKey, destBucketMD, false, sourceObjMD['content-length']),
locationMatch,
originOp: 's3:ObjectCreated:Copy',
};
const defaultRetentionConfig = destBucketMD.getObjectLockConfiguration();
if (defaultRetentionConfig && !legalHoldHeader) {
storeMetadataParams.defaultRetention = defaultRetentionConfig;
}
if (sourceObjMD.checksum && !_shouldRecomputeChecksum(headers, sourceObjMD)) {
storeMetadataParams.checksum = {
algorithm: sourceObjMD.checksum.checksumAlgorithm,
value: sourceObjMD.checksum.checksumValue,
type: sourceObjMD.checksum.checksumType,
};
}
// In case whichMetadata === 'REPLACE' but contentType is undefined in copy
// request headers, make sure to keep the original header instead
if (!storeMetadataParams.contentType) {
storeMetadataParams.contentType = sourceObjMD['content-type'];
}
if (authInfo.getCanonicalID() !== destBucketMD.getOwner()) {
storeMetadataParams.bucketOwnerId = destBucketMD.getOwner();
}
return { storeMetadataParams, sourceLocationConstraintName, backendInfoDest: backendInfoObjDest.backendInfo };
}
/**
* PUT Object Copy in the requested bucket.
* @param {AuthInfo} authInfo - Instance of AuthInfo class with
* requester's info
* @param {request} request - request object given by router,
* includes normalized headers
* @param {string} sourceBucket - name of source bucket for object copy
* @param {string} sourceObject - name of source object for object copy
* @param {string} sourceVersionId - versionId of source object for copy
* @param {object} log - the log request
* @param {function} callback - final callback to call with the result
* @return {undefined}
*/
function objectCopy(authInfo, request, sourceBucket, sourceObject, sourceVersionId, log, callback) {
log.debug('processing request', { method: 'objectCopy' });
const destBucketName = request.bucketName;
const destObjectKey = request.objectKey;
const keyLengthError = validateObjectKeyLength(destObjectKey, config.objectKeyByteLimit);
if (keyLengthError) {
return callback(keyLengthError);
}
const sourceIsDestination = destBucketName === sourceBucket && destObjectKey === sourceObject;
const valGetParams = {
authInfo,
bucketName: sourceBucket,
objectKey: sourceObject,
versionId: sourceVersionId,
getDeleteMarker: true,
requestType: 'objectGet',
/**
* Authorization will first check the target object, with an objectPut
* action. But in this context, the source object metadata is still
* unknown. In the context of quotas, to know the number of bytes that
* are being written, we explicitly enable the quota evaluation logic
* during the objectGet action instead.
*/
checkQuota: true,
request,
};
const valPutParams = {
authInfo,
bucketName: destBucketName,
objectKey: destObjectKey,
requestType: 'objectPut',
checkQuota: false,
request,
};
const dataStoreContext = {
bucketName: destBucketName,
owner: authInfo.getCanonicalID(),
namespace: request.namespace,
objectKey: destObjectKey,
};
const websiteRedirectHeader = request.headers['x-amz-website-redirect-location'];
const responseHeaders = {};
if (
request.headers['x-amz-storage-class'] &&
!constants.validStorageClasses.includes(request.headers['x-amz-storage-class'])
) {
log.trace('invalid storage-class header');
monitoring.promMetrics('PUT', destBucketName, errorInstances.InvalidStorageClass.code, 'copyObject');
return callback(errors.InvalidStorageClass);
}
if (!validateWebsiteHeader(websiteRedirectHeader)) {
const err = errors.InvalidRedirectLocation;
log.debug(`invalid x-amz-website-redirect-location value ${websiteRedirectHeader}`, { error: err });
monitoring.promMetrics('PUT', destBucketName, err.code, 'copyObject');
return callback(err);
}
const { error: checksumAlgoErr, algorithm: requestedAlgo } = getCopyObjectChecksumAlgorithm(request.headers);
if (checksumAlgoErr) {
const err = arsenalErrorFromChecksumError(checksumAlgoErr);
log.debug('invalid x-amz-checksum-algorithm', { error: checksumAlgoErr });
monitoring.promMetrics('PUT', destBucketName, err.code, 'copyObject');
return callback(err);
}
const queryContainsVersionId = checkQueryVersionId(request.query);
if (queryContainsVersionId instanceof Error) {
return callback(queryContainsVersionId);
}
return async.waterfall(
[
function checkDestAuth(next) {
return standardMetadataValidateBucketAndObj(
valPutParams,
request.actionImplicitDenies,
log,
(err, destBucketMD, destObjMD) =>
updateEncryption(
err,
destBucketMD,
destObjMD,
destObjectKey,
log,
{ skipObject: true },
(err, destBucketMD, destObjMD) => {
if (err) {
log.debug('error validating put part of request', { error: err });
return next(err, destBucketMD);
}
const flag = destBucketMD.hasDeletedFlag() || destBucketMD.hasTransientFlag();
if (flag) {
log.trace('deleted flag or transient flag on destination bucket', { flag });
return next(errors.NoSuchBucket);
}
return next(null, destBucketMD, destObjMD);
},
),
);
},
function checkSourceAuthorization(destBucketMD, destObjMD, next) {
return standardMetadataValidateBucketAndObj(
{
...valGetParams,
destObjMD,
serverAccessLogOptions: { copySource: true },
},
request.actionImplicitDenies,
log,
(err, sourceBucketMD, sourceObjMD) => {
if (err) {
log.debug('error validating get part of request', { error: err });
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = err);
return next(err, null, destBucketMD);
}
if (!sourceObjMD) {
const err = sourceVersionId ? errors.NoSuchVersion : errors.NoSuchKey;
log.debug('no source object', { sourceObject });
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = err);
return next(err, null, destBucketMD);
}
// check if object data is in a cold storage
const coldErr = verifyColdObjectAvailable(sourceObjMD);
if (coldErr) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = coldErr);
return next(coldErr, null);
}
if (sourceObjMD.isDeleteMarker) {
log.debug('delete marker on source object', { sourceObject });
let err;
if (sourceVersionId) {
err = errorInstances.InvalidRequest.customizeDescription(
'The source of a copy ' +
'request may not specifically refer to a delete' +
'marker by version id.',
);
} else {
// if user specifies a key in a versioned source bucket
// without specifying a version, and the object has
// a delete marker, return NoSuchKey
err = errors.NoSuchKey;
}
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = err);
return next(err, destBucketMD);
}
const sourceSize = parseInt(sourceObjMD['content-length'], 10);
if (sourceSize > constants.maximumAllowedUploadSize && !config.bypassMaxPutObjectSize) {
log.debug('copy source object too large', { sourceSize });
const err = errorInstances.InvalidRequest.customizeDescription(
'The specified copy source is larger than the maximum ' +
`allowable size for a copy source: ${constants.maximumAllowedUploadSize}`,
);
if (request.sourceServerAccessLog) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog.error = err;
}
return next(err, destBucketMD);
}
const headerValResult = validateHeaders(
request.headers,
sourceObjMD['last-modified'],
sourceObjMD['content-md5'],
);
if (headerValResult.error) {
request.sourceServerAccessLog &&
// eslint-disable-next-line no-param-reassign
(request.sourceServerAccessLog.error = errors.PreconditionFailed);
return next(errors.PreconditionFailed, destBucketMD);
}
const {
storeMetadataParams,
error: metadataError,
sourceLocationConstraintName,
backendInfoDest,
} = _prepMetadata(
request,
sourceObjMD,
request.headers,
sourceIsDestination,
authInfo,
destObjectKey,
sourceBucketMD,
destBucketMD,
sourceVersionId,
log,
);
if (metadataError) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = metadataError);
return next(metadataError, destBucketMD);
}
if (storeMetadataParams.metaHeaders) {
dataStoreContext.metaHeaders = storeMetadataParams.metaHeaders;
}
storeMetadataParams.overheadField = constants.overheadField;
let dataLocator;
// If 0 byte object just set dataLocator to empty array
if (!sourceObjMD.location) {
dataLocator = [];
} else {
// To provide for backwards compatibility before
// md-model-version 2, need to handle cases where
// objMD.location is just a string
dataLocator = Array.isArray(sourceObjMD.location)
? sourceObjMD.location
: [{ key: sourceObjMD.location }];
}
if (sourceObjMD['x-amz-server-side-encryption']) {
for (let i = 0; i < dataLocator.length; i++) {
dataLocator[i].masterKeyId = sourceObjMD['x-amz-server-side-encryption-aws-kms-key-id'];
dataLocator[i].algorithm = sourceObjMD['x-amz-server-side-encryption'];
}
}
// If the destination key already exists
if (destObjMD) {
// Re-use creation-time if we can
if (destObjMD['creation-time']) {
storeMetadataParams.creationTime = destObjMD['creation-time'];
// Otherwise fallback to last-modified
} else {
storeMetadataParams.creationTime = destObjMD['last-modified'];
}
// If this is a new key, create a new timestamp
} else {
storeMetadataParams.creationTime = new Date().toJSON();
}
return next(
null,
storeMetadataParams,
dataLocator,
sourceBucketMD,
destBucketMD,
destObjMD,
sourceLocationConstraintName,
backendInfoDest,
sourceObjMD,
);
},
);
},
function getSSEConfiguration(
storeMetadataParams,
dataLocator,
sourceBucketMD,
destBucketMD,
destObjMD,
sourceLocationConstraintName,
backendInfoDest,
sourceObjMD,
next,
) {
getObjectSSEConfiguration(request.headers, destBucketMD, log, (err, sseConfig) =>
next(
err,
storeMetadataParams,
dataLocator,
sourceBucketMD,
destBucketMD,
destObjMD,
sourceLocationConstraintName,
backendInfoDest,
sseConfig,
sourceObjMD,
),
);
},
function goGetData(
storeMetadataParams,
dataLocator,
sourceBucketMD,
destBucketMD,
destObjMD,
sourceLocationConstraintName,
backendInfoDest,
serverSideEncryption,
sourceObjMD,
next,
) {
const vcfg = destBucketMD.getVersioningConfiguration();
const isVersionedObj = vcfg && vcfg.Status === 'Enabled';
const destLocationConstraintName = storeMetadataParams.dataStoreName;
const needsEncryption = serverSideEncryption && !!serverSideEncryption.algo;
const shouldRecomputeChecksum = _shouldRecomputeChecksum(request.headers, sourceObjMD);
let destLocationConstraintType;
if (config.backends.data === 'multiple') {
destLocationConstraintType = config.getLocationConstraintType(destLocationConstraintName);
}
// skip if source and dest and location constraint the same and
// versioning is not enabled, unless we need to recompute a
// checksum (which requires streaming the bytes through us).
if (
sourceIsDestination &&
storeMetadataParams.locationMatch &&
!isVersionedObj &&
!needsEncryption &&
!shouldRecomputeChecksum
) {
return next(null, storeMetadataParams, dataLocator, destObjMD, serverSideEncryption, destBucketMD);
}
// also skip if 0 byte object, unless location constraint is an
// external backend and differs from source, in which case put
// metadata to backend
if (
destLocationConstraintType &&
versioningNotImplBackends[destLocationConstraintType] &&
isVersionedObj
) {
log.debug(externalVersioningErrorMessage, {
method: 'multipleBackendGateway',
error: errors.NotImplemented,
});
return next(
errorInstances.NotImplemented.customizeDescription(externalVersioningErrorMessage),
destBucketMD,
);
}
if (dataLocator.length === 0) {
const finishZeroByte = () => {
if (
!storeMetadataParams.locationMatch &&
destLocationConstraintType &&
constants.externalBackends[destLocationConstraintType]
) {
return data.put(
null,
null,
storeMetadataParams.size,
dataStoreContext,
backendInfoDest,
log,
(error, objectRetrievalInfo) => {
if (error) {
return next(error, destBucketMD);
}
const putResult = {
key: objectRetrievalInfo.key,
dataStoreName: objectRetrievalInfo.dataStoreName,
dataStoreType: objectRetrievalInfo.dataStoreType,
size: storeMetadataParams.size,
};
return next(
null,
storeMetadataParams,
[putResult],
destObjMD,
serverSideEncryption,
destBucketMD,
);
},
);
}
return next(
null,
storeMetadataParams,
dataLocator,
destObjMD,
serverSideEncryption,
destBucketMD,
);
};
if (shouldRecomputeChecksum) {
// No bytes to stream, but AWS still writes the empty-bytes digest of the chosen algorithm.
const algoName = _getCopyObjectChecksumAlgorithm(requestedAlgo, sourceObjMD);
return Promise.resolve(algorithms[algoName].digest(Buffer.alloc(0))).then(
digest => {
// eslint-disable-next-line no-param-reassign
storeMetadataParams.checksum = {
algorithm: algoName,
value: digest,
type: 'FULL_OBJECT',
};
return finishZeroByte();
},
err => {
log.error('failed to compute empty checksum digest', {
algorithm: algoName,
error: err,
});
return next(errors.InternalError, destBucketMD);
},
);
}
return finishZeroByte();
}
if (shouldRecomputeChecksum) {
return _recomputeChecksumAndStore(
log,
request,
requestedAlgo,
sourceObjMD,
dataLocator,
sourceIsDestination,
isVersionedObj,
needsEncryption,
storeMetadataParams,
destObjMD,
destBucketMD,
serverSideEncryption,
dataStoreContext,
backendInfoDest,
next,
);
}
const originalIdentityImpDenies = request.actionImplicitDenies;
// eslint-disable-next-line no-param-reassign
delete request.actionImplicitDenies;
return data.copyObject(
request,
sourceLocationConstraintName,
storeMetadataParams,
dataLocator,
dataStoreContext,
backendInfoDest,
sourceBucketMD,
destBucketMD,
serverSideEncryption,
log,
(err, results) => {
// eslint-disable-next-line no-param-reassign
request.actionImplicitDenies = originalIdentityImpDenies;
if (err) {
// eslint-disable-next-line no-param-reassign
request.sourceServerAccessLog && (request.sourceServerAccessLog.error = err);
return next(err, destBucketMD);
}
return next(null, storeMetadataParams, results, destObjMD, serverSideEncryption, destBucketMD);
},
);
},
function getVersioningInfo(
storeMetadataParams,
destDataGetInfoArr,
destObjMD,
serverSideEncryption,
destBucketMD,
next,
) {
if (!destBucketMD.isVersioningEnabled() && destObjMD?.archive?.archiveInfo) {
// Ensure we trigger a "delete" event in the oplog for the previously archived object
// eslint-disable-next-line
storeMetadataParams.needOplogUpdate = 's3:ReplaceArchivedObject';
}
return versioningPreprocessing(
destBucketName,
destBucketMD,
destObjectKey,
destObjMD,
log,
(err, options) => {
if (err) {
log.debug('error processing versioning info', { error: err });
return next(err, null, destBucketMD);
}
const location = destDataGetInfoArr?.[0]?.dataStoreName;
if (location === destBucketMD.getLocationConstraint() && destBucketMD.isIngestionBucket()) {
// If the object is being written to the "ingested" storage location, keep the same
// versionId for consistency and to avoid creating an extra version when it gets
// ingested
const backendVersionId = decodeVID(destDataGetInfoArr[0].dataStoreVersionId);
if (!(backendVersionId instanceof Error)) {
options.versionId = backendVersionId; // eslint-disable-line no-param-reassign
}
}
// eslint-disable-next-line
storeMetadataParams.versionId = options.versionId;
// eslint-disable-next-line
storeMetadataParams.versioning = options.versioning;
// eslint-disable-next-line
storeMetadataParams.isNull = options.isNull;
if (options.extraMD) {
Object.assign(storeMetadataParams, options.extraMD);
}
const dataToDelete = _orphanedDataLocations(options.dataToDelete, destDataGetInfoArr);
return next(
null,
storeMetadataParams,
destDataGetInfoArr,
destObjMD,
serverSideEncryption,
destBucketMD,
dataToDelete,
);
},
);
},
function storeNewMetadata(
storeMetadataParams,
destDataGetInfoArr,
destObjMD,
serverSideEncryption,
destBucketMD,
dataToDelete,
next,
) {