Skip to content

Commit

Permalink
Merge branch 'w/7.8/feature/S3C-2787-retention-parsing' into tmp/octo…
Browse files Browse the repository at this point in the history
…pus/w/8.1/feature/S3C-2787-retention-parsing
  • Loading branch information
bert-e committed Jun 12, 2020
2 parents ee4d94c + 16c4464 commit 750c021
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ module.exports = {
// 604800 seconds (seven days).
defaultPreSignedURLExpiry: 7 * 24 * 60 * 60,
// Regex for ISO-8601 formatted date
iso8601Regex: /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/,
shortIso8601Regex: /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/,
longIso8601Regex: /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/,
};
19 changes: 10 additions & 9 deletions lib/s3middleware/objectRetention.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const errors = require('../errors');

/*
Format of xml request:
<ObjectRetention>
<Retention>
<Mode>COMPLIANCE|GOVERNANCE</Mode>
<RetainUntilDate>2020-05-20T04:58:45.413000Z</RetainUntilDate>
</ObjectRetention>
</Retention>
*/

/**
Expand Down Expand Up @@ -50,7 +50,8 @@ function validateRetainDate(retainDate) {
'request xml does not contain RetainUntilDate');
return dateObj;
}
if (!constants.iso8601Regex.test(retainDate[0])) {
if (!constants.shortIso8601Regex.test(retainDate[0]) &&
!constants.longIso8601Regex.test(retainDate[0])) {
dateObj.error = errors.InvalidRequest.customizeDescription(
'RetainUntilDate timestamp must be ISO-8601 format');
return dateObj;
Expand Down Expand Up @@ -78,10 +79,10 @@ function validateRetention(parsedXml) {
'request xml is undefined or empty');
return retentionObj;
}
const retention = parsedXml.ObjectRetention;
const retention = parsedXml.Retention;
if (!retention) {
retentionObj.error = errors.MalformedXML.customizeDescription(
'request xml does not contain ObjectRetention');
'request xml does not contain Retention');
return retentionObj;
}
const modeObj = validateMode(retention.Mode);
Expand Down Expand Up @@ -140,14 +141,14 @@ function parseRetentionXml(xml, log, cb) {
function convertToXml(retentionInfo) {
const xml = [];
const ret = retentionInfo.retention;
xml.push('<ObjectRetention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">');
xml.push('<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">');
if (ret && ret.mode && ret.retainUntilDate) {
xml.push(`<Mode>${ret.mode}</Mode>` +
`<RetainUntilDate>${ret.retainUntilDate}</RetainUntilDate>`);
xml.push(`<Mode>${ret.mode}</Mode>`);
xml.push(`<RetainUntilDate>${ret.retainUntilDate}</RetainUntilDate>`);
} else {
return '';
}
xml.push('</ObjectRetention>');
xml.push('</Retention>');
return xml.join('');
}

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/s3middleware/objectRetention.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function buildXml(key, value) {
`<RetainUntilDate>${value}</RetainUntilDate>` :
`<RetainUntilDate>${date}</RetainUntilDate>`;
const retention = key === 'Retention' ?
`<ObjectRetention>${value}</ObjectRetention>` :
`<ObjectRetention>${mode}${retainDate}</ObjectRetention>`;
`<Retention>${value}</Retention>` :
`<Retention>${mode}${retainDate}</Retention>`;
return retention;
}

Expand All @@ -35,17 +35,17 @@ const expectedRetention = {
};

const expectedXml =
'<ObjectRetention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
'<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
'<Mode>GOVERNANCE</Mode>' +
`<RetainUntilDate>${passDate.toISOString()}</RetainUntilDate>` +
'</ObjectRetention>';
'</Retention>';

const failTests = [
{
name: 'should fail with empty retention',
params: { key: 'Retention', value: '' },
error: 'MalformedXML',
errMessage: 'request xml does not contain ObjectRetention',
errMessage: 'request xml does not contain Retention',
},
{
name: 'should fail with empty mode',
Expand Down

0 comments on commit 750c021

Please sign in to comment.