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
33 changes: 25 additions & 8 deletions lib/api/apiUtils/object/objectAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ function parseAttributesHeaders(headers, headerName, supportedAttributes) {
return result;
}

/**
* buildRestoreStatusXml - Builds the RestoreStatus element of an object.
* As on AWS, only an object with a restore in progress or a restored copy has one,
* and a restored copy always comes with its expiry date.
* @param {Object} [restoreStatus] - Restore state of the object, from the metadata listing.
* @param {boolean} [restoreStatus.inProgress] - Whether a restore is currently active.
* @param {string} [restoreStatus.expiryDate] - The date after which the restored copy expires.
* @returns {string[]} - the XML elements, empty when the object has no restore state.
*/
function buildRestoreStatusXml(restoreStatus) {
if (restoreStatus?.inProgress) {
return ['<RestoreStatus>', '<IsRestoreInProgress>true</IsRestoreInProgress>', '</RestoreStatus>'];
}
if (restoreStatus?.expiryDate) {
return [
'<RestoreStatus>',
'<IsRestoreInProgress>false</IsRestoreInProgress>',
`<RestoreExpiryDate>${restoreStatus.expiryDate}</RestoreExpiryDate>`,
'</RestoreStatus>',
];
}
return [];
}

/**
* buildAttributesXml - Builds XML reponse for requested object attributes
* @param {Object} objectMD - The internal metadata object for the file/object.
Expand Down Expand Up @@ -100,14 +124,7 @@ function buildAttributesXml(objectMD, userMetadata, requestedAttrs, xml, log) {
break;
}
case 'RestoreStatus':
xml.push('<RestoreStatus>');
xml.push(`<IsRestoreInProgress>${!!objectMD.restoreStatus?.inProgress}</IsRestoreInProgress>`);

if (objectMD.restoreStatus?.expiryDate) {
xml.push(`<RestoreExpiryDate>${objectMD.restoreStatus?.expiryDate}</RestoreExpiryDate>`);
}

xml.push('</RestoreStatus>');
xml.push(...buildRestoreStatusXml(objectMD.restoreStatus));
break;
case 'x-amz-meta-*':
for (const key of Object.keys(userMetadata)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenko/cloudserver",
"version": "9.4.22",
"version": "9.4.23",
"description": "Zenko CloudServer, an open-source Node.js implementation of a server handling the Amazon S3 protocol",
"main": "index.js",
"engines": {
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/aws-node-sdk/test/bucket/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
PutObjectCommand,
ListObjectsCommand,
ListObjectsV2Command,
ListObjectVersionsCommand,
PutBucketPolicyCommand,
DeleteBucketPolicyCommand,
S3Client,
Expand Down Expand Up @@ -536,6 +537,24 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
});
});

it('should not return RestoreStatus for an object that has no restore state', async () => {
const s3 = bucketUtil.s3;
const Bucket = bucketName;
const OptionalObjectAttributes = ['RestoreStatus'];
await s3.send(new PutObjectCommand({ Bucket, Key: 'never-archived' }));

const [v1, v2, versions] = await Promise.all([
s3.send(new ListObjectsCommand({ Bucket, OptionalObjectAttributes })),
s3.send(new ListObjectsV2Command({ Bucket, OptionalObjectAttributes })),
s3.send(new ListObjectVersionsCommand({ Bucket, OptionalObjectAttributes })),
]);
for (const entries of [v1.Contents, v2.Contents, versions.Versions]) {
assert.strictEqual(entries.length, 1);
assert.strictEqual(entries[0].Key, 'never-archived');
assert.strictEqual(entries[0].RestoreStatus, undefined);
}
});

const describeBypass = isVaultScality && internalPortBypassBP ? describe : describe.skip;
describeBypass('x-amz-optional-attributes header', () => {
let policyWithListBucketOnly;
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/api/apiUtils/object/objectAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ describe('buildXmlAttributes', () => {
assert.strictEqual(result[3], '</RestoreStatus>');
});

it('should generate RestoreStatus XML without expiry date while a restore is in progress', () => {
const result = [];
const restoringMD = { ...objectMD, restoreStatus: { inProgress: true } };
buildAttributesXml(restoringMD, userMetadata, ['RestoreStatus'], result, log);

assert.deepStrictEqual(result, [
'<RestoreStatus>',
'<IsRestoreInProgress>true</IsRestoreInProgress>',
'</RestoreStatus>',
]);
});

it('should not generate RestoreStatus XML for an object that was never archived', () => {
const result = [];
const neverArchivedMD = { ...objectMD, restoreStatus: undefined };
buildAttributesXml(neverArchivedMD, userMetadata, ['RestoreStatus'], result, log);

assert.deepStrictEqual(result, []);
});

it('should not generate RestoreStatus XML for an archived object that was never restored', () => {
const result = [];
const archivedMD = { ...objectMD, restoreStatus: { inProgress: false, expiryDate: undefined } };
buildAttributesXml(archivedMD, userMetadata, ['RestoreStatus'], result, log);

assert.deepStrictEqual(result, []);
});

it('should ignore unknown attributes', () => {
const result = [];
buildAttributesXml(objectMD, userMetadata, ['UnknownAttribute', 'ETag'], result, log);
Expand Down
117 changes: 113 additions & 4 deletions tests/unit/api/bucketGet.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const assert = require('assert');
const querystring = require('querystring');
const { promisify } = require('util');

const async = require('async');
const { parseString } = require('xml2js');
const { parseString, parseStringPromise } = require('xml2js');

const { bucketGet } = require('../../../lib/api/bucketGet');
const { bucketPut } = require('../../../lib/api/bucketPut');
const bucketPutVersioning = require('../../../lib/api/bucketPutVersioning');
const { objectDelete } = require('../../../lib/api/objectDelete');
const objectPut = require('../../../lib/api/objectPut');
const { cleanup, DummyRequestLogger, makeAuthInfo } = require('../helpers');
const metadata = require('../../../lib/metadata/wrapper');
const { cleanup, DummyRequestLogger, makeAuthInfo, versioningTestUtils } = require('../helpers');
const DummyRequest = require('../DummyRequest');

const authInfo = makeAuthInfo('accessKey1');
Expand Down Expand Up @@ -313,6 +315,33 @@ describe('bucketGet API V2', () => {
});

describe('x-amz-optional-object-attributes header', () => {
const oneDay = 24 * 60 * 60 * 1000;
const archiveInfo = { archiveId: 'archive-1', archiveVersion: 1 };
const restoreExpiry = new Date(Date.now() + oneDay);
const restoredArchive = {
archiveInfo,
restoreRequestedAt: new Date(Date.now() - oneDay),
restoreRequestedDays: 2,
restoreCompletedAt: new Date(Date.now() - oneDay / 2),
restoreWillExpireAt: restoreExpiry,
};

// sets the archive field of an object, as the cold storage services do
const setArchive = (key, archive, cb) =>
metadata.getObjectMD(bucketName, key, {}, log, (err, objMD) => {
if (err) {
return cb(err);
}
return metadata.putObjectMD(
bucketName,
key,
{ ...objMD, archive },
{ versionId: objMD.versionId },
log,
putErr => cb(putErr),
);
});

it('should return an error if the header is empty', done => {
const testGetRequest = Object.assign(
{
Expand Down Expand Up @@ -518,7 +547,8 @@ describe('bucketGet API V2', () => {
[
next => bucketPut(authInfo, testPutBucketRequest, log, next),
(_, next) => objectPut(authInfo, putRequest, undefined, log, next),
(_, next) => bucketGet(authInfo, testGetRequest, log, next),
(_, next) => setArchive(objectNameMeta, restoredArchive, next),
next => bucketGet(authInfo, testGetRequest, log, next),
(result, _, next) => parseString(result, next),
],
(err, result) => {
Expand Down Expand Up @@ -657,7 +687,8 @@ describe('bucketGet API V2', () => {
next => bucketPut(authInfo, testPutBucketRequest, log, next),
(_, next) => bucketPutVersioning(authInfo, versioningRequest, log, next),
(_, next) => objectPut(authInfo, putRequest, undefined, log, next),
(_, next) => objectDelete(authInfo, deleteRequest, log, next),
(_, next) => setArchive(objectNameMeta, restoredArchive, next),
next => objectDelete(authInfo, deleteRequest, log, next),
(_, next) => bucketGet(authInfo, testGetRequest, log, next),
(result, _, next) => parseString(result, next),
],
Expand Down Expand Up @@ -716,5 +747,83 @@ describe('bucketGet API V2', () => {
},
);
});

describe('RestoreStatus', () => {
// archive field of one object per restore state, keyed by object name
const archiveByState = {
'never-archived': undefined,
'archived-never-restored': { archiveInfo },
'restore-in-progress': {
archiveInfo,
restoreRequestedAt: new Date(),
restoreRequestedDays: 2,
},
'restore-completed': restoredArchive,
};
const expectedRestoreStatusByState = {
'never-archived': undefined,
'archived-never-restored': undefined,
'restore-in-progress': [{ IsRestoreInProgress: ['true'] }],
'restore-completed': [
{ IsRestoreInProgress: ['false'], RestoreExpiryDate: [restoreExpiry.toISOString()] },
],
};

const putObjectInEveryState = async () => {
for (const [key, archive] of Object.entries(archiveByState)) {
const request = versioningTestUtils.createPutObjectRequest(bucketName, key, postBody);
await promisify(objectPut)(authInfo, request, undefined, log);
if (archive) {
await promisify(setArchive)(key, archive);
}
}
};

const listWithRestoreStatus = async query => {
const request = {
...baseGetRequest,
headers: { ...baseGetRequest.headers, 'x-amz-optional-object-attributes': 'RestoreStatus' },
query,
url: `${baseUrl}?${querystring.stringify(query)}`,
};
const [xml] = await bucketGet(authInfo, request, log);
return parseStringPromise(xml);
};

const assertRestoreStatusByState = entries => {
assert.deepStrictEqual(entries.map(entry => entry.Key[0]).sort(), Object.keys(archiveByState).sort());
for (const entry of entries) {
assert.deepStrictEqual(
entry.RestoreStatus,
expectedRestoreStatusByState[entry.Key[0]],
`RestoreStatus of ${entry.Key[0]}`,
);
}
};

[
{ api: 'ListObjects', query: {} },
{ api: 'ListObjectsV2', query: { 'list-type': '2' } },
].forEach(({ api, query }) => {
it(`should return RestoreStatus only for objects with a restore state in ${api}`, async () => {
await promisify(bucketPut)(authInfo, testPutBucketRequest, log);
await putObjectInEveryState();
const result = await listWithRestoreStatus(query);
assertRestoreStatusByState(result.ListBucketResult.Contents);
});
});

it('should return RestoreStatus only for objects with a restore state in ListObjectVersions', async () => {
await promisify(bucketPut)(authInfo, testPutBucketRequest, log);
await promisify(bucketPutVersioning)(
authInfo,
versioningTestUtils.createBucketPutVersioningReq(bucketName, 'Enabled'),
log,
);
await putObjectInEveryState();
const result = await listWithRestoreStatus({ versions: '' });
assertRestoreStatusByState(result.ListVersionsResult.Version);
});
});
});
});
Loading