-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathobjectGet.js
More file actions
381 lines (363 loc) · 17.2 KB
/
objectGet.js
File metadata and controls
381 lines (363 loc) · 17.2 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
const { errors, errorInstances, s3middleware } = require('arsenal');
const { parseRange } = require('arsenal').network.http.utils;
const async = require('async');
const { data } = require('../data/wrapper');
const { decodeVersionId } = require('./apiUtils/object/versioning');
const collectCorsHeaders = require('../utilities/collectCorsHeaders');
const collectResponseHeaders = require('../utilities/collectResponseHeaders');
const { pushMetric } = require('../utapi/utilities');
const { getVersionIdResHeader } = require('./apiUtils/object/versioning');
const setPartRanges = require('./apiUtils/object/setPartRanges');
const locationHeaderCheck =
require('./apiUtils/object/locationHeaderCheck');
const getReplicationBackendDataLocator =
require('./apiUtils/object/getReplicationBackendDataLocator');
const checkReadLocation = require('./apiUtils/object/checkReadLocation');
const { standardMetadataValidateBucketAndObj } = require('../metadata/metadataUtils');
const { config } = require('../Config');
const monitoring = require('../utilities/monitoringHandler');
const { getPartCountFromMd5 } = require('./apiUtils/object/partInfo');
const { setExpirationHeaders } = require('./apiUtils/object/expirationHeaders');
const { verifyColdObjectAvailable } = require('./apiUtils/object/coldStorage');
const kms = require('../kms/wrapper');
const { updateEncryption } = require('./apiUtils/bucket/updateEncryption');
const validateHeaders = s3middleware.validateConditionalHeaders;
/**
* GET Object - Get an object
* @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info
* @param {object} request - normalized request object
* @param {boolean} returnTagCount - returns the x-amz-tagging-count header
* @param {object} log - Werelogs instance
* @param {function} callback - callback to function in route
* @return {undefined}
*/
function objectGet(authInfo, request, returnTagCount, log, callback) {
log.debug('processing request', { method: 'objectGet' });
const bucketName = request.bucketName;
const objectKey = request.objectKey;
const checksumMode = request.headers['x-amz-checksum-mode'];
if (checksumMode !== undefined && checksumMode !== 'ENABLED') {
log.debug('invalid x-amz-checksum-mode', { checksumMode });
return callback(errors.InvalidArgument);
}
// returns name of location to get from and key if successful
const locCheckResult =
locationHeaderCheck(request.headers, objectKey, bucketName);
if (locCheckResult instanceof Error) {
log.trace('invalid location constraint to get from', {
location: request.headers['x-amz-location-constraint'],
error: locCheckResult,
});
return callback(locCheckResult);
}
const decodedVidResult = decodeVersionId(request.query);
if (decodedVidResult instanceof Error) {
log.trace('invalid versionId query', {
versionId: request.query.versionId,
error: decodedVidResult,
});
return callback(decodedVidResult);
}
const versionId = decodedVidResult;
const mdValParams = {
authInfo,
bucketName,
objectKey,
versionId,
getDeleteMarker: true,
requestType: request.apiMethods || 'objectGet',
request,
returnTagCount,
};
return standardMetadataValidateBucketAndObj(mdValParams, request.actionImplicitDenies, log,
(err, bucket, objMD) => updateEncryption(err, bucket, objMD, objectKey, log, {},
(err, bucket, objMD) => {
const corsHeaders = collectCorsHeaders(request.headers.origin,
request.method, bucket);
if (err) {
log.debug('error processing request', {
error: err,
method: 'metadataValidateBucketAndObj',
});
monitoring.promMetrics(
'GET', bucketName, err.code, 'getObject');
return callback(err, null, corsHeaders);
}
if (!objMD) {
const err = versionId ? errors.NoSuchVersion : errors.NoSuchKey;
monitoring.promMetrics(
'GET', bucketName, err.code, 'getObject');
return callback(err, null, corsHeaders);
}
const verCfg = bucket.getVersioningConfiguration();
// check if object data is in a cold storage
const coldErr = verifyColdObjectAvailable(objMD);
if (coldErr) {
monitoring.promMetrics(
'GET', bucketName, coldErr.code, 'getObject');
return callback(coldErr, null, corsHeaders);
}
if (objMD.isDeleteMarker) {
const responseMetaHeaders = Object.assign({},
{ 'x-amz-delete-marker': true }, corsHeaders);
if (!versionId) {
monitoring.promMetrics(
'GET', bucketName, 404, 'getObject');
return callback(errors.NoSuchKey, null, responseMetaHeaders);
}
// return MethodNotAllowed if requesting a specific
// version that has a delete marker
responseMetaHeaders['x-amz-version-id'] =
getVersionIdResHeader(verCfg, objMD);
monitoring.promMetrics(
'GET', bucketName, 405, 'getObject');
return callback(errors.MethodNotAllowed, null,
responseMetaHeaders);
}
const headerValResult = validateHeaders(request.headers,
objMD['last-modified'], objMD['content-md5']);
if (headerValResult.error) {
return callback(headerValResult.error, null, corsHeaders);
}
const responseMetaHeaders = collectResponseHeaders(objMD,
corsHeaders, verCfg,
returnTagCount && objMD.returnTagCount); // IAM and Bucket policy should both authorize tagging.
setExpirationHeaders(responseMetaHeaders, {
lifecycleConfig: bucket.getLifecycleConfiguration(),
objectParams: {
key: objectKey,
tags: objMD.tags,
date: objMD['last-modified'],
},
isVersionedReq: !!versionId,
});
const objLength = (objMD.location === null ?
0 : parseInt(objMD['content-length'], 10));
// Store full object size for server access logs
if (request.serverAccessLog) {
// eslint-disable-next-line no-param-reassign
request.serverAccessLog.objectSize = objLength;
}
let byteRange;
let partNumber = null;
const streamingParams = {};
if (request.headers.range) {
const { range, error } = parseRange(request.headers.range,
objLength);
if (error) {
monitoring.promMetrics(
'GET', bucketName, 400, 'getObject');
return callback(error, null, corsHeaders);
}
responseMetaHeaders['Accept-Ranges'] = 'bytes';
if (range) {
byteRange = range;
// End of range should be included so + 1
responseMetaHeaders['Content-Length'] =
range[1] - range[0] + 1;
responseMetaHeaders['Content-Range'] =
`bytes ${range[0]}-${range[1]}/${objLength}`;
streamingParams.rangeStart = (range[0] || typeof range[0] === 'number') ?
range[0].toString() : undefined;
streamingParams.rangeEnd = range[1] ?
range[1].toString() : undefined;
}
}
let dataLocator = null;
if (objMD.location !== null) {
// To provide for backwards compatibility before
// md-model-version 2, need to handle cases where
// objMD.location is just a string
dataLocator = Array.isArray(objMD.location) ?
objMD.location : [{ key: objMD.location }];
const repConf = bucket.getReplicationConfiguration();
const prefReadLocation = repConf && repConf.preferredReadLocation;
const prefReadDataLocator = checkReadLocation(config,
prefReadLocation, objectKey, bucketName);
const targetLocation = locCheckResult || prefReadDataLocator ||
null;
if (targetLocation &&
targetLocation.location !== objMD.dataStoreName) {
const repBackendResult = getReplicationBackendDataLocator(
targetLocation, objMD.replicationInfo);
if (repBackendResult.error) {
log.error('Error with location constraint header', {
bucketName, objectKey, versionId,
error: repBackendResult.error,
status: repBackendResult.status,
});
return callback(repBackendResult.error, null, corsHeaders);
}
const targetDataLocator = repBackendResult.dataLocator;
if (targetDataLocator) {
dataLocator = targetDataLocator;
} else {
log.debug('using source location as preferred read ' +
'is unavailable', {
bucketName, objectKey, versionId,
reason: repBackendResult.reason,
});
}
}
// if the data backend is azure, there will only ever be at
// most one item in the dataLocator array
if (dataLocator[0] && dataLocator[0].dataStoreType === 'azure') {
dataLocator[0].azureStreamingOptions = streamingParams;
}
if (request.query && request.query.partNumber !== undefined) {
if (byteRange) {
const error = errorInstances.InvalidRequest
.customizeDescription('Cannot specify both Range ' +
'header and partNumber query parameter.');
monitoring.promMetrics(
'GET', bucketName, 400, 'getObject');
return callback(error, null, corsHeaders);
}
partNumber = Number.parseInt(request.query.partNumber, 10);
if (Number.isNaN(partNumber)) {
const error = errorInstances.InvalidArgument
.customizeDescription('Part number must be a number.');
monitoring.promMetrics(
'GET', bucketName, 400, 'getObject');
return callback(error, null, corsHeaders);
}
if (partNumber < 1 || partNumber > 10000) {
const error = errorInstances.InvalidArgument
.customizeDescription('Part number must be an ' +
'integer between 1 and 10000, inclusive.');
monitoring.promMetrics(
'GET', bucketName, 400, 'getObject');
return callback(error, null, corsHeaders);
}
}
// If have a data model before version 2, cannot support
// get range for objects with multiple parts
if (byteRange && dataLocator.length > 1 &&
dataLocator[0].start === undefined) {
monitoring.promMetrics(
'GET', bucketName, 501, 'getObject');
return callback(errors.NotImplemented, null, corsHeaders);
}
if (objMD['x-amz-server-side-encryption']) {
for (let i = 0; i < dataLocator.length; i++) {
dataLocator[i].masterKeyId =
objMD['x-amz-server-side-encryption-aws-kms-key-id'];
dataLocator[i].algorithm =
objMD['x-amz-server-side-encryption'];
}
}
if (partNumber) {
const locations = [];
let locationPartNumber;
for (let i = 0; i < objMD.location.length; i++) {
const { dataStoreETag } = objMD.location[i];
if (dataStoreETag) {
locationPartNumber =
Number.parseInt(dataStoreETag.split(':')[0], 10);
} else {
/**
* Location objects prior to GA7.1 do not include the
* dataStoreETag field so we cannot find the part range,
* the objects are treated as if they only have 1 part
*/
locationPartNumber = 1;
}
// Get all parts that belong to the requested part number
if (partNumber === locationPartNumber) {
locations.push(objMD.location[i]);
} else if (locationPartNumber > partNumber) {
break;
}
}
if (locations.length === 0) {
monitoring.promMetrics(
'GET', bucketName, 400, 'getObject');
return callback(errors.InvalidPartNumber, null,
corsHeaders);
}
const { start } = locations[0];
const endLocation = locations[locations.length - 1];
const end = endLocation.start + endLocation.size - 1;
responseMetaHeaders['Content-Length'] = end - start + 1;
const partByteRange = [start, end];
dataLocator = setPartRanges(dataLocator, partByteRange);
const partsCount = getPartCountFromMd5(objMD);
if (partsCount) {
responseMetaHeaders['x-amz-mp-parts-count'] =
partsCount;
}
} else {
dataLocator = setPartRanges(dataLocator, byteRange);
}
}
// Checksum is not returned for partNumber requests because per-part
// checksums are not yet stored (S3C-11073).
if (checksumMode === 'ENABLED' && !byteRange && !partNumber) {
const checksum = objMD.checksum;
if (checksum) {
responseMetaHeaders[`x-amz-checksum-${checksum.checksumAlgorithm}`]
= checksum.checksumValue;
responseMetaHeaders['x-amz-checksum-type'] = checksum.checksumType;
}
}
// Check KMS Key access and usability before checking data
// diff with AWS: for empty object (no dataLocator) KMS not checked
return async.each(dataLocator || [],
(objectGetInfo, next) => {
if (!objectGetInfo.cipheredDataKey) {
return next();
}
const serverSideEncryption = {
cryptoScheme: objectGetInfo.cryptoScheme,
masterKeyId: objectGetInfo.masterKeyId,
cipheredDataKey: Buffer.from(
objectGetInfo.cipheredDataKey, 'base64'),
};
const offset = objectGetInfo.range ? objectGetInfo.range[0] : 0;
return kms.createDecipherBundle(serverSideEncryption,
offset, log, (err, decipherBundle) => {
if (err) {
log.error('cannot get decipher bundle from kms',
{ method: 'objectGet' });
return next(err);
}
// eslint-disable-next-line no-param-reassign
objectGetInfo.decipherStream = decipherBundle.decipher;
return next();
});
},
err => {
if (err) {
monitoring.promMetrics(
'GET', bucketName, err.code, 'getObject');
return callback(err);
}
return data.head(dataLocator, log, err => {
if (err) {
if (!err.is.LocationNotFound) {
log.error('error from external backend checking for ' +
'object existence', { error: err });
}
monitoring.promMetrics(
'GET', bucketName, err.code, 'getObject');
return callback(err);
}
pushMetric('getObject', log, {
authInfo,
bucket: bucketName,
keys: [objectKey],
newByteLength:
Number.parseInt(responseMetaHeaders['Content-Length'], 10),
versionId: objMD.versionId,
location: objMD.dataStoreName,
});
monitoring.promMetrics('GET', bucketName, '200', 'getObject',
Number.parseInt(responseMetaHeaders['Content-Length'], 10));
return callback(null, dataLocator, responseMetaHeaders,
byteRange);
});
}
);
}));
}
module.exports = objectGet;