-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathobjectCopy.js
More file actions
343 lines (318 loc) · 13.6 KB
/
objectCopy.js
File metadata and controls
343 lines (318 loc) · 13.6 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
const assert = require('assert');
const async = require('async');
const { storage } = require('arsenal');
const sinon = require('sinon');
const { bucketPut } = require('../../../lib/api/bucketPut');
const bucketPutVersioning = require('../../../lib/api/bucketPutVersioning');
const bucketPutPolicy = require('../../../lib/api/bucketPutPolicy');
const objectPut = require('../../../lib/api/objectPut');
const objectCopy = require('../../../lib/api/objectCopy');
const DummyRequest = require('../DummyRequest');
const { cleanup, DummyRequestLogger, makeAuthInfo, versioningTestUtils }
= require('../helpers');
const mpuUtils = require('../utils/mpuUtils');
const metadata = require('../metadataswitch');
const any = sinon.match.any;
const { ds } = storage.data.inMemory.datastore;
const log = new DummyRequestLogger();
const canonicalID = 'accessKey1';
const authInfo = makeAuthInfo(canonicalID);
const namespace = 'default';
const destBucketName = 'destbucketname';
const sourceBucketName = 'sourcebucketname';
const objectKey = 'objectName';
const originalputObjectMD = metadata.putObjectMD;
function _createBucketPutRequest(bucketName) {
return new DummyRequest({
bucketName,
namespace,
headers: { host: `${bucketName}.s3.amazonaws.com` },
url: '/',
});
}
function _createObjectCopyRequest(destBucketName) {
const params = {
bucketName: destBucketName,
namespace,
objectKey,
headers: {},
url: `/${destBucketName}/${objectKey}`,
socket: {},
};
return new DummyRequest(params);
}
const putDestBucketRequest = _createBucketPutRequest(destBucketName);
const putSourceBucketRequest = _createBucketPutRequest(sourceBucketName);
const enableVersioningRequest = versioningTestUtils
.createBucketPutVersioningReq(destBucketName, 'Enabled');
const suspendVersioningRequest = versioningTestUtils
.createBucketPutVersioningReq(destBucketName, 'Suspended');
const objData = ['foo0', 'foo1', 'foo2'].map(str =>
Buffer.from(str, 'utf8'));
describe('objectCopy with versioning', () => {
const testPutObjectRequests = objData.slice(0, 2).map(data =>
versioningTestUtils.createPutObjectRequest(destBucketName, objectKey,
data));
testPutObjectRequests.push(versioningTestUtils
.createPutObjectRequest(sourceBucketName, objectKey, objData[2]));
before(done => {
cleanup();
sinon.spy(metadata, 'putObjectMD');
async.series([
callback => bucketPut(authInfo, putDestBucketRequest, log,
callback),
callback => bucketPut(authInfo, putSourceBucketRequest, log,
callback),
// putting null version: put obj before versioning configured
// in dest bucket
callback => objectPut(authInfo, testPutObjectRequests[0],
undefined, log, callback),
callback => bucketPutVersioning(authInfo,
enableVersioningRequest, log, callback),
// put another version in dest bucket:
callback => objectPut(authInfo, testPutObjectRequests[1],
undefined, log, callback),
callback => bucketPutVersioning(authInfo,
suspendVersioningRequest, log, callback),
// put source object in source bucket
callback => objectPut(authInfo, testPutObjectRequests[2],
undefined, log, callback),
], err => {
if (err) {
return done(err);
}
versioningTestUtils.assertDataStoreValues(ds, objData);
return done();
});
});
after(() => {
metadata.putObjectMD.restore();
cleanup();
});
it('should delete null version when creating new null version, ' +
'even when null version is not the latest version', done => {
// will have another copy of last object in datastore after objectCopy
const expectedValues = [undefined, objData[1], objData[2], objData[2]];
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey,
undefined, log, err => {
assert.ifError(err, `Unexpected err: ${err}`);
setImmediate(() => {
versioningTestUtils
.assertDataStoreValues(ds, expectedValues);
done();
});
});
});
it('should not copy object with storage-class header not equal to STANDARD', done => {
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
testObjectCopyRequest.headers['x-amz-storage-class'] = 'COLD';
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey,
undefined, log, err => {
setImmediate(() => {
assert.strictEqual(err.is.InvalidStorageClass, true);
done();
});
});
});
it('should not set bucketOwnerId if requesting account owns dest bucket', done => {
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey,
undefined, log, err => {
assert.ifError(err);
sinon.assert.calledWith(
metadata.putObjectMD.lastCall,
destBucketName,
objectKey,
sinon.match({ _data: { bucketOwnerId: sinon.match.typeOf('undefined') } }),
sinon.match.any,
sinon.match.any,
sinon.match.any
);
done();
});
});
// TODO: S3C-9965
// Skipped because the policy is not checked correctly
// When source bucket policy is checked destination arn is used
it.skip('should set bucketOwnerId if requesting account differs from dest bucket owner', done => {
const authInfo2 = makeAuthInfo('accessKey2');
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
const testPutSrcPolicyRequest = new DummyRequest({
bucketName: sourceBucketName,
namespace,
headers: { host: `${sourceBucketName}.s3.amazonaws.com` },
url: '/',
socket: {},
post: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Sid: 'AllowCrossAccountRead',
Effect: 'Allow',
Principal: { AWS: `arn:aws:iam::${authInfo2.shortid}:root` },
Action: ['s3:GetObject'],
Resource: [
`arn:aws:s3:::${sourceBucketName}/*`,
],
},
],
}),
});
const testPutDestPolicyRequest = new DummyRequest({
bucketName: destBucketName,
namespace,
headers: { host: `${destBucketName}.s3.amazonaws.com` },
url: '/',
socket: {},
post: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Sid: 'AllowCrossAccountWrite',
Effect: 'Allow',
Principal: { AWS: `arn:aws:iam::${authInfo2.shortid}:root` },
Action: ['s3:PutObject'],
Resource: [
`arn:aws:s3:::${destBucketName}/*`,
],
},
],
}),
});
bucketPutPolicy(authInfo, testPutSrcPolicyRequest, log, err => {
assert.ifError(err);
bucketPutPolicy(authInfo, testPutDestPolicyRequest, log, err => {
assert.ifError(err);
objectCopy(authInfo2, testObjectCopyRequest, sourceBucketName, objectKey,
undefined, log, err => {
sinon.assert.calledWith(
metadata.putObjectMD.lastCall,
destBucketName,
objectKey,
sinon.match({ _data: { bucketOwnerId: authInfo.canonicalID } }),
sinon.match.any,
sinon.match.any,
sinon.match.any
);
assert.ifError(err);
done();
});
});
});
});
});
describe('non-versioned objectCopy', () => {
const testPutObjectRequest = versioningTestUtils
.createPutObjectRequest(sourceBucketName, objectKey, objData[0]);
before(done => {
cleanup();
sinon.stub(metadata, 'putObjectMD')
.callsFake(originalputObjectMD);
async.series([
callback => bucketPut(authInfo, putDestBucketRequest, log,
callback),
callback => bucketPut(authInfo, putSourceBucketRequest, log,
callback),
// put source object in source bucket
callback => objectPut(authInfo, testPutObjectRequest,
undefined, log, callback),
], err => {
if (err) {
return done(err);
}
versioningTestUtils.assertDataStoreValues(ds, objData.slice(0, 1));
return done();
});
});
after(() => {
cleanup();
sinon.restore();
});
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
it('should not leave orphans in data when overwriting a multipart upload', done => {
mpuUtils.createMPU(namespace, destBucketName, objectKey, log,
(err, testUploadId) => {
assert.ifError(err);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey,
undefined, log, err => {
assert.ifError(err);
sinon.assert.calledWith(metadata.putObjectMD,
any, any, any, sinon.match({ oldReplayId: testUploadId }), any, any);
done();
});
});
});
});
describe('objectCopy overheadField', () => {
beforeEach(done => {
cleanup();
sinon.stub(metadata, 'putObjectMD').callsFake(originalputObjectMD);
async.series([
next => bucketPut(authInfo, putSourceBucketRequest, log, next),
next => bucketPut(authInfo, putDestBucketRequest, log, next),
], done);
});
afterEach(() => {
sinon.restore();
cleanup();
});
it('should pass overheadField to metadata.putObjectMD for a non-versioned request', done => {
const testPutObjectRequest =
versioningTestUtils.createPutObjectRequest(sourceBucketName, objectKey, objData[0]);
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
objectPut(authInfo, testPutObjectRequest, undefined, log, err => {
assert.ifError(err);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey, undefined, log,
err => {
assert.ifError(err);
sinon.assert.calledWith(metadata.putObjectMD.lastCall,
destBucketName, objectKey, any, sinon.match({ overheadField: sinon.match.array }), any, any);
done();
}
);
});
});
it('should pass overheadField to metadata.putObjectMD for a versioned request', done => {
const testPutObjectRequest =
versioningTestUtils.createPutObjectRequest(sourceBucketName, objectKey, objData[0]);
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
objectPut(authInfo, testPutObjectRequest, undefined, log, err => {
assert.ifError(err);
bucketPutVersioning(authInfo, enableVersioningRequest, log, err => {
assert.ifError(err);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey, undefined, log,
err => {
assert.ifError(err);
sinon.assert.calledWith(metadata.putObjectMD.lastCall,
destBucketName, objectKey, any,
sinon.match({ overheadField: sinon.match.array }), any, any
);
done();
}
);
});
});
});
it('should pass overheadField to metadata.putObjectMD for a version-suspended request', done => {
const testPutObjectRequest =
versioningTestUtils.createPutObjectRequest(sourceBucketName, objectKey, objData[0]);
const testObjectCopyRequest = _createObjectCopyRequest(destBucketName);
objectPut(authInfo, testPutObjectRequest, undefined, log, err => {
assert.ifError(err);
bucketPutVersioning(authInfo, suspendVersioningRequest, log, err => {
assert.ifError(err);
objectCopy(authInfo, testObjectCopyRequest, sourceBucketName, objectKey, undefined, log,
err => {
assert.ifError(err);
sinon.assert.calledWith(metadata.putObjectMD.lastCall,
destBucketName, objectKey, any,
sinon.match({ overheadField: sinon.match.array }), any, any
);
done();
}
);
});
});
});
});