-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathobjectPut.js
More file actions
274 lines (249 loc) · 11.7 KB
/
objectPut.js
File metadata and controls
274 lines (249 loc) · 11.7 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
const async = require('async');
const { errors, errorInstances, versioning, s3routes } = require('arsenal');
const { validateObjectKeyLength } = s3routes.routesUtils;
const constants = require('../../constants');
const aclUtils = require('../utilities/aclUtils');
const { cleanUpBucket } = require('./apiUtils/bucket/bucketCreation');
const { getObjectSSEConfiguration } = require('./apiUtils/bucket/bucketEncryption');
const collectCorsHeaders = require('../utilities/collectCorsHeaders');
const createAndStoreObject = require('./apiUtils/object/createAndStoreObject');
const { checkQueryVersionId, decodeVID } = require('./apiUtils/object/versioning');
const { standardMetadataValidateBucketAndObj } = require('../metadata/metadataUtils');
const { pushMetric } = require('../utapi/utilities');
const { validateHeaders } = require('./apiUtils/object/objectLockHelpers');
const { hasNonPrintables } = require('../utilities/stringChecks');
const kms = require('../kms/wrapper');
const monitoring = require('../utilities/monitoringHandler');
const { validatePutVersionId } = require('./apiUtils/object/coldStorage');
const { setExpirationHeaders } = require('./apiUtils/object/expirationHeaders');
const validateChecksumHeaders = require('./apiUtils/object/validateChecksumHeaders');
const writeContinue = require('../utilities/writeContinue');
const { overheadField } = require('../../constants');
const { config } = require('../Config');
const versionIdUtils = versioning.VersionID;
const { updateEncryption } = require('./apiUtils/bucket/updateEncryption');
const invalidSSEError = errorInstances.InvalidArgument.customizeDescription(
'The encryption method specified is not supported');
/**
* PUT Object in the requested bucket. Steps include:
* validating metadata for authorization, bucket and object existence etc.
* store object data in datastore upon successful authorization
* store object location returned by datastore and
* object's (custom) headers in metadata
* return the result in final callback
*
* @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info
* @param {request} request - request object given by router,
* includes normalized headers
* @param {object | undefined } streamingV4Params - if v4 auth,
* object containing accessKey, signatureFromRequest, region, scopeDate,
* timestamp, and credentialScope
* (to be used for streaming v4 auth if applicable)
* @param {object} log - the log request
* @param {Function} callback - final callback to call with the result
* @return {undefined}
*/
function objectPut(authInfo, request, streamingV4Params, log, callback) {
log.debug('processing request', { method: 'objectPut' });
const putVersionId = request.headers['x-scal-s3-version-id'];
const isPutVersion = putVersionId || putVersionId === '';
let versionId;
if (putVersionId) {
const decodedVidResult = decodeVID(putVersionId);
if (decodedVidResult instanceof Error) {
log.trace('invalid x-scal-s3-version-id header', {
versionId: putVersionId,
error: decodedVidResult,
});
return process.nextTick(() => callback(decodedVidResult));
}
versionId = decodedVidResult;
}
const {
bucketName,
headers,
method,
objectKey,
parsedContentLength,
query,
} = request;
if (headers['x-amz-storage-class'] &&
!constants.validStorageClasses.includes(headers['x-amz-storage-class'])) {
log.trace('invalid storage-class header');
monitoring.promMetrics('PUT', request.bucketName,
errorInstances.InvalidStorageClass.code, 'putObject');
return callback(errors.InvalidStorageClass);
}
if (!aclUtils.checkGrantHeaderValidity(headers)) {
log.trace('invalid acl header');
monitoring.promMetrics('PUT', request.bucketName, 400,
'putObject');
return callback(errors.InvalidArgument);
}
const queryContainsVersionId = checkQueryVersionId(query);
if (queryContainsVersionId instanceof Error) {
return callback(queryContainsVersionId);
}
const keyLengthError = validateObjectKeyLength(objectKey, config.objectKeyByteLimit);
if (keyLengthError) {
return callback(keyLengthError);
}
const size = request.parsedContentLength;
if (Number.parseInt(size, 10) > constants.maximumAllowedUploadSize
&& !config.bypassMaxPutObjectSize) {
log.debug('Upload size exceeds maximum allowed for a single PUT',
{ size });
return callback(errors.EntityTooLarge);
}
const requestType = request.apiMethods || 'objectPut';
const valParams = { authInfo, bucketName, objectKey, versionId,
requestType, request, withVersionId: isPutVersion };
const canonicalID = authInfo.getCanonicalID();
if (hasNonPrintables(objectKey)) {
return callback(errorInstances.InvalidInput.customizeDescription(
'object keys cannot contain non-printable characters',
));
}
const checksumHeaderErr = validateChecksumHeaders(headers);
if (checksumHeaderErr) {
return callback(checksumHeaderErr);
}
log.trace('owner canonicalID to send to data', { canonicalID });
return standardMetadataValidateBucketAndObj(valParams, request.actionImplicitDenies, log,
(err, bucket, objMD) => updateEncryption(err, bucket, objMD, objectKey, log, { skipObject: true },
(err, bucket, objMD) => {
const responseHeaders = collectCorsHeaders(headers.origin,
method, bucket);
if (err) {
log.trace('error processing request', {
error: err,
method: 'metadataValidateBucketAndObj',
});
monitoring.promMetrics('PUT', bucketName, err.code, 'putObject');
return callback(err, responseHeaders);
}
if (bucket.hasDeletedFlag() && canonicalID !== bucket.getOwner()) {
log.trace('deleted flag on bucket and request ' +
'from non-owner account');
monitoring.promMetrics('PUT', bucketName, 404, 'putObject');
return callback(errors.NoSuchBucket);
}
if (isPutVersion) {
const error = validatePutVersionId(objMD, putVersionId, log);
if (error) {
return callback(error);
}
}
return async.waterfall([
function handleTransientOrDeleteBuckets(next) {
if (bucket.hasTransientFlag() || bucket.hasDeletedFlag()) {
return cleanUpBucket(bucket, canonicalID, log, next);
}
return next();
},
function getSSEConfig(next) {
return getObjectSSEConfiguration(headers, bucket, log,
(err, sseConfig) => {
if (err) {
log.error('error getting server side encryption config', { err });
return next(invalidSSEError);
}
return next(null, sseConfig);
}
);
},
function createCipherBundle(serverSideEncryptionConfig, next) {
if (serverSideEncryptionConfig) {
return kms.createCipherBundle(
serverSideEncryptionConfig, log, next);
}
return next(null, null);
},
function objectCreateAndStore(cipherBundle, next) {
const objectLockValidationError
= validateHeaders(bucket, headers, log);
if (objectLockValidationError) {
return next(objectLockValidationError);
}
writeContinue(request, request._response);
return createAndStoreObject(bucketName,
bucket, objectKey, objMD, authInfo, canonicalID, cipherBundle,
request, false, streamingV4Params, overheadField, log, 's3:ObjectCreated:Put', next);
},
], (err, storingResult) => {
if (err) {
monitoring.promMetrics('PUT', bucketName, err.code,
'putObject');
return callback(err, responseHeaders);
}
// ingestSize assumes that these custom headers indicate
// an ingestion PUT which is a metadata only operation.
// Since these headers can be modified client side, they
// should be used with caution if needed for precise
// metrics.
const ingestSize = (request.headers['x-amz-meta-mdonly']
&& !Number.isNaN(request.headers['x-amz-meta-size']))
? Number.parseInt(request.headers['x-amz-meta-size'], 10) : null;
const newByteLength = parsedContentLength;
setExpirationHeaders(responseHeaders, {
lifecycleConfig: bucket.getLifecycleConfiguration(),
objectParams: {
key: objectKey,
date: storingResult.lastModified,
tags: storingResult.tags,
},
});
// Utapi expects null or a number for oldByteLength:
// * null - new object
// * 0 or > 0 - existing object with content-length 0 or > 0
// objMD here is the master version that we would
// have overwritten if there was an existing version or object
//
// TODO: Handle utapi metrics for null version overwrites.
const oldByteLength = objMD && objMD['content-length']
!== undefined ? objMD['content-length'] : null;
if (storingResult) {
// ETag's hex should always be enclosed in quotes
responseHeaders.ETag = `"${storingResult.contentMD5}"`;
if (storingResult.checksum) {
const { checksumAlgorithm, checksumValue } = storingResult.checksum;
responseHeaders[`x-amz-checksum-${checksumAlgorithm}`] = checksumValue;
}
}
const vcfg = bucket.getVersioningConfiguration();
const isVersionedObj = vcfg && vcfg.Status === 'Enabled';
if (isVersionedObj) {
if (storingResult && storingResult.versionId) {
responseHeaders['x-amz-version-id'] =
versionIdUtils.encode(storingResult.versionId);
}
}
// Only pre-existing non-versioned objects get 0 all others use 1
const numberOfObjects = !isVersionedObj && oldByteLength !== null ? 0 : 1;
// only the bucket owner's metrics should be updated, regardless of
// who the requester is
pushMetric('putObject', log, {
authInfo,
canonicalID: bucket.getOwner(),
bucket: bucketName,
keys: [objectKey],
newByteLength,
oldByteLength: isVersionedObj ? null : oldByteLength,
versionId: isVersionedObj && storingResult ? storingResult.versionId : undefined,
location: bucket.getLocationConstraint(),
numberOfObjects,
});
monitoring.promMetrics('PUT', bucketName, '200',
'putObject', newByteLength, oldByteLength, isVersionedObj,
null, ingestSize);
if (isPutVersion) {
const durationMs = Date.now() - new Date(objMD.archive.restoreRequestedAt);
monitoring.lifecycleDuration.observe(
{ type: 'restore', location: objMD.dataStoreName },
durationMs / 1000);
}
return callback(null, responseHeaders);
});
}));
}
module.exports = objectPut;