Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a check for duplicate Id in bucket lifecycle rules #8629

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 src/endpoint/s3/ops/s3_put_bucket_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function parse_lifecycle_field(field, field_parser = parseInt) {
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html
*/
async function put_bucket_lifecycle(req) {
const id_set = new Set();
const lifecycle_rules = _.map(req.body.LifecycleConfiguration.Rule, rule => {
const current_rule = {
filter: {},
Expand All @@ -94,6 +95,13 @@ async function put_bucket_lifecycle(req) {
current_rule.id = uuid();
}

// Check for duplicate ID in the rules
if (id_set.has(current_rule.id)) {
dbg.error('Rule ID must be unique. Found same ID for more than one rule: ', current_rule.id);
throw new S3Error({ ...S3Error.InvalidArgument, message: 'Rule ID must be unique. Found same ID for more than one rule'});
}
id_set.add(current_rule.id);

if (rule.Status?.length !== 1) {
dbg.error('Rule should have status', rule);
throw new S3Error(S3Error.InvalidArgument);
Expand Down
43 changes: 43 additions & 0 deletions src/test/lifecycle/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,36 @@ function id_lifecycle_configuration(Bucket, Key) {
};
}

function duplicate_id_lifecycle_configuration(Bucket, Key) {
const ID1 = 'rule_id';
const ID2 = 'rule_id';
return {
Bucket,
LifecycleConfiguration: {
Rules: [{
ID1,
Expiration: {
Days: 17,
},
Filter: {
Prefix: Key,
},
Status: 'Enabled',
},
{
ID2,
Expiration: {
Days: 18,
},
Filter: {
Prefix: Key,
},
Status: 'Enabled',
}, ],
},
};
}

async function put_get_lifecycle_configuration(Bucket, putLifecycleParams, s3) {
const putLifecycleResult = await s3.putBucketLifecycleConfiguration(putLifecycleParams);
console.log('put lifecycle params:', putLifecycleParams, 'result', putLifecycleResult);
Expand Down Expand Up @@ -512,3 +542,16 @@ exports.test_and_prefix_size = async function(Bucket, Key, s3) {
assert(actualFilter.ObjectSizeGreaterThan === expectedFilter.ObjectSizeGreaterThan, 'and prefix size filter - ObjectSizeGreaterThan');
assert(actualFilter.ObjectSizeLessThan === expectedFilter.ObjectSizeLessThan, 'and prefix size filter - ObjectSizeLessThan');
};

exports.test_rule_duplicate_id = async function(Bucket, Key, s3) {
const putLifecycleParams = duplicate_id_lifecycle_configuration(Bucket, Key);

try {
await s3.putBucketLifecycleConfiguration(putLifecycleParams);
// if no error occurs, explicitly fail the test
assert.fail('Expected error for duplicate rule ID, but request was successful');
} catch (error) {
assert(error.code === 'InvalidArgument', 'Expected InvalidArgument: duplicate ID found in the rules');
console.log('Expected error received: each rule must have a unique ID, duplicate ID found');
}
};
1 change: 1 addition & 0 deletions src/test/system_tests/test_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async function main() {
await commonTests.test_rule_id(Bucket, Key, s3);
await commonTests.test_filter_size(Bucket, s3);
await commonTests.test_and_prefix_size(Bucket, Key, s3);
await commonTests.test_rule_duplicate_id(Bucket, Key, s3);

const getObjectParams = {
Bucket,
Expand Down
Loading