Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ const constants = {
rateLimitDefaultConfigCacheTTL: 30000, // 30 seconds
rateLimitDefaultBurstCapacity: 1,
rateLimitCleanupInterval: 10000, // 10 seconds
// Supported attributes for the GetObjectAttributes 'x-amz-optional-attributes' header.
supportedGetObjectAttributes: new Set([
'StorageClass',
'ObjectSize',
'ObjectParts',
'Checksum',
'ETag',
]),
};

module.exports = constants;
2 changes: 2 additions & 0 deletions lib/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const { objectDelete } = require('./objectDelete');
const objectDeleteTagging = require('./objectDeleteTagging');
const objectGet = require('./objectGet');
const objectGetACL = require('./objectGetACL');
const objectGetAttributes = require('./objectGetAttributes.js');
const objectGetLegalHold = require('./objectGetLegalHold');
const objectGetRetention = require('./objectGetRetention');
const objectGetTagging = require('./objectGetTagging');
Expand Down Expand Up @@ -471,6 +472,7 @@ const api = {
objectDeleteTagging,
objectGet,
objectGetACL,
objectGetAttributes,
objectGetLegalHold,
objectGetRetention,
objectGetTagging,
Expand Down
25 changes: 25 additions & 0 deletions lib/api/apiUtils/object/parseAttributesHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { errorInstances } = require('arsenal');
const { supportedGetObjectAttributes } = require('../../../../constants');

/**
* parseAttributesHeaders - Parse and validate the x-amz-object-attributes header
* @param {object} headers - request headers
* @returns {Set<string>} - set of requested attribute names
* @throws {Error} - InvalidRequest if header is missing/empty, InvalidArgument if attribute is invalid
*/
function parseAttributesHeaders(headers) {
const attributes = headers['x-amz-object-attributes']?.split(',').map(attr => attr.trim()) ?? [];
if (attributes.length === 0) {
throw errorInstances.InvalidRequest.customizeDescription(
'The x-amz-object-attributes header specifying the attributes to be retrieved is either missing or empty',
);
}

if (attributes.some(attr => !supportedGetObjectAttributes.has(attr))) {
throw errorInstances.InvalidArgument.customizeDescription('Invalid attribute name specified.');
}

return new Set(attributes);
}

module.exports = parseAttributesHeaders;
170 changes: 170 additions & 0 deletions lib/api/objectGetAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const { promisify } = require('util');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just noticed this is indented with 2 spaces only, while we use 4-space indent overall...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { errors } = require('arsenal');
const { standardMetadataValidateBucketAndObj } = require('../metadata/metadataUtils');
const collectCorsHeaders = require('../utilities/collectCorsHeaders');
const parseAttributesHeaders = require('./apiUtils/object/parseAttributesHeader');
const { decodeVersionId, getVersionIdResHeader } = require('./apiUtils/object/versioning');
const { checkExpectedBucketOwner } = require('./apiUtils/authorization/bucketOwner');
const { pushMetric } = require('../utapi/utilities');
const { getPartCountFromMd5 } = require('./apiUtils/object/partInfo');

const checkExpectedBucketOwnerPromise = promisify(checkExpectedBucketOwner);
const validateBucketAndObj = promisify(standardMetadataValidateBucketAndObj);

const OBJECT_GET_ATTRIBUTES = 'objectGetAttributes';

/**
* buildXmlResponse - Build XML response for GetObjectAttributes
* @param {object} objMD - object metadata
* @param {Set<string>} requestedAttrs - set of requested attribute names
* @returns {string} XML response
*/
function buildXmlResponse(objMD, requestedAttrs) {
const xml = [];
xml.push(
'<?xml version="1.0" encoding="UTF-8"?>',
'<GetObjectAttributesResponse>',
);

for (const attribute of requestedAttrs) {
switch (attribute) {
case 'ETag':
xml.push(`<ETag>${objMD['content-md5']}</ETag>`);
break;
case 'ObjectParts': {
const partCount = getPartCountFromMd5(objMD);
if (partCount) {
xml.push(
'<ObjectParts>',
`<PartsCount>${partCount}</PartsCount>`,
'</ObjectParts>',
);
}
break;
}
case 'StorageClass':
xml.push(`<StorageClass>${objMD['x-amz-storage-class']}</StorageClass>`);
break;
case 'ObjectSize':
xml.push(`<ObjectSize>${objMD['content-length']}</ObjectSize>`);
break;
}
}

xml.push('</GetObjectAttributesResponse>');
return xml.join('');
}

/**
* objectGetAttributes - Retrieves all metadata from an object without returning the object itself
* @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info
* @param {object} request - http request object
* @param {object} log - Werelogs logger
* @param {function} callback - callback optional to keep backward compatibility
* @returns {Promise<object>} - { xml, responseHeaders }
* @throws {ArsenalError} NoSuchVersion - if versionId specified but not found
* @throws {ArsenalError} NoSuchKey - if object not found
* @throws {ArsenalError} MethodNotAllowed - if object is a delete marker
*/
async function objectGetAttributes(authInfo, request, log, callback) {
if (callback) {
return objectGetAttributes(authInfo, request, log)
.then(result => callback(null, result.xml, result.responseHeaders))
.catch(err => callback(err, null, err.responseHeaders ?? {}));
}

log.trace('processing request', { method: OBJECT_GET_ATTRIBUTES });
const { bucketName, objectKey, headers, actionImplicitDenies } = request;

const versionId = decodeVersionId(request.query);
if (versionId instanceof Error) {
log.debug('invalid versionId query', {
method: OBJECT_GET_ATTRIBUTES,
versionId: request.query.versionId,
error: versionId,
});
throw versionId;
}

const metadataValParams = {
authInfo,
bucketName,
objectKey,
versionId,
getDeleteMarker: true,
requestType: request.apiMethods || OBJECT_GET_ATTRIBUTES,
request,
};

let bucket, objectMD;
try {
({ bucket, objectMD } = await validateBucketAndObj(metadataValParams, actionImplicitDenies, log));
await checkExpectedBucketOwnerPromise(headers, bucket, log);
} catch (err) {
log.debug('error validating bucket and object', {
method: OBJECT_GET_ATTRIBUTES,
bucket: bucketName,
key: objectKey,
versionId,
error: err,
});
throw err;
}

const responseHeaders = collectCorsHeaders(headers.origin, request.method, bucket);

if (!objectMD) {
log.debug('object not found', {
method: OBJECT_GET_ATTRIBUTES,
bucket: bucketName,
key: objectKey,
versionId,
});
const err = versionId ? errors.NoSuchVersion : errors.NoSuchKey;
err.responseHeaders = responseHeaders;
throw err;
}

responseHeaders['x-amz-version-id'] = getVersionIdResHeader(bucket.getVersioningConfiguration(), objectMD);
responseHeaders['Last-Modified'] = objectMD['last-modified'] && new Date(objectMD['last-modified']).toUTCString();

if (objectMD.isDeleteMarker) {
log.debug('attempt to get attributes of a delete marker', {
method: OBJECT_GET_ATTRIBUTES,
bucket: bucketName,
key: objectKey,
versionId,
});
responseHeaders['x-amz-delete-marker'] = true;
const err = errors.MethodNotAllowed;
err.responseHeaders = responseHeaders;
throw err;
}

const requestedAttrs = parseAttributesHeaders(headers);

if (requestedAttrs.has('Checksum')) {
log.debug('Checksum attribute requested but not implemented', {
method: OBJECT_GET_ATTRIBUTES,
bucket: bucketName,
key: objectKey,
versionId,
});
const err = errors.NotImplemented.customizeDescription('Checksum attribute is not implemented');
err.responseHeaders = responseHeaders;
throw err;
}

pushMetric(OBJECT_GET_ATTRIBUTES, log, {
authInfo,
bucket: bucketName,
keys: [objectKey],
versionId: objectMD?.versionId,
location: objectMD?.dataStoreName,
});

const xml = buildXmlResponse(objectMD, requestedAttrs);
return { xml, responseHeaders };
}

module.exports = objectGetAttributes;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@azure/storage-blob": "^12.28.0",
"@hapi/joi": "^17.1.1",
"@smithy/node-http-handler": "^3.0.0",
"arsenal": "git+https://github.com/scality/arsenal#8.3.3",
"arsenal": "git+https://github.com/scality/Arsenal#8.3.4",
"async": "2.6.4",
"bucketclient": "scality/bucketclient#8.2.7",
"bufferutil": "^4.0.8",
Expand Down
Loading
Loading