Skip to content

Commit 7409b0b

Browse files
committed
Added a check for duplicate Id in bucket lifecycle rules
Signed-off-by: Aayush Chouhan <achouhan@redhat.com>
1 parent 4d677c8 commit 7409b0b

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/endpoint/s3/ops/s3_put_bucket_lifecycle.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function parse_lifecycle_field(field, field_parser = parseInt) {
8383
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html
8484
*/
8585
async function put_bucket_lifecycle(req) {
86+
const id_set = new Set();
8687
const lifecycle_rules = _.map(req.body.LifecycleConfiguration.Rule, rule => {
8788
const current_rule = {
8889
filter: {},
@@ -100,6 +101,13 @@ async function put_bucket_lifecycle(req) {
100101
current_rule.id = uuid();
101102
}
102103

104+
// Check for duplicate ID in the rules
105+
if (id_set.has(current_rule.id)) {
106+
dbg.error('Rule ID must be unique. Found same ID for more than one rule: ', current_rule.id);
107+
throw new S3Error({ ...S3Error.InvalidArgument, message: 'Rule ID must be unique. Found same ID for more than one rule'});
108+
}
109+
id_set.add(current_rule.id);
110+
103111
if (rule.Status?.length !== 1) {
104112
dbg.error('Rule should have status', rule);
105113
throw new S3Error(S3Error.InvalidArgument);

src/test/lifecycle/common.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,36 @@ function id_lifecycle_configuration(Bucket, Key) {
359359
};
360360
}
361361

362+
function duplicate_id_lifecycle_configuration(Bucket, Key) {
363+
const ID1 = 'rule_id';
364+
const ID2 = ID1; // set duplicate ID
365+
return {
366+
Bucket,
367+
LifecycleConfiguration: {
368+
Rules: [{
369+
ID1,
370+
Expiration: {
371+
Days: 17,
372+
},
373+
Filter: {
374+
Prefix: Key,
375+
},
376+
Status: 'Enabled',
377+
},
378+
{
379+
ID2,
380+
Expiration: {
381+
Days: 18,
382+
},
383+
Filter: {
384+
Prefix: Key,
385+
},
386+
Status: 'Enabled',
387+
}, ],
388+
},
389+
};
390+
}
391+
362392
async function put_get_lifecycle_configuration(Bucket, putLifecycleParams, s3) {
363393
const putLifecycleResult = await s3.putBucketLifecycleConfiguration(putLifecycleParams);
364394
console.log('put lifecycle params:', putLifecycleParams, 'result', putLifecycleResult);
@@ -528,3 +558,14 @@ exports.test_rule_id_length = async function(Bucket, Key, s3) {
528558
assert(error.code === 'InvalidArgument', `Expected InvalidArgument: id length exceeding ${s3_const.MAX_RULE_ID_LENGTH} characters`);
529559
}
530560
};
561+
562+
exports.test_rule_duplicate_id = async function(Bucket, Key, s3) {
563+
const putLifecycleParams = duplicate_id_lifecycle_configuration(Bucket, Key);
564+
565+
try {
566+
await s3.putBucketLifecycleConfiguration(putLifecycleParams);
567+
assert.fail('Expected error for duplicate rule ID, but request was successful');
568+
} catch (error) {
569+
assert(error.code === 'InvalidArgument', 'Expected InvalidArgument: duplicate ID found in the rules');
570+
}
571+
};

src/test/system_tests/test_lifecycle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async function main() {
5555
await commonTests.test_filter_size(Bucket, s3);
5656
await commonTests.test_and_prefix_size(Bucket, Key, s3);
5757
await commonTests.test_rule_id_length(Bucket, Key, s3);
58+
await commonTests.test_rule_duplicate_id(Bucket, Key, s3);
5859

5960
const getObjectParams = {
6061
Bucket,

0 commit comments

Comments
 (0)