Skip to content

Commit

Permalink
updated the msg, enum obj and created a function for commong error
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Jan 10, 2025
1 parent 5db9a42 commit 7992052
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
12 changes: 4 additions & 8 deletions src/endpoint/s3/ops/s3_put_bucket_lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const _ = require('lodash');
const s3_const = require('../s3_constants');
const s3_utils = require('../s3_utils')
const { v4: uuid } = require('uuid');
const dbg = require('../../../util/debug_module')(__filename);
const S3Error = require('../s3_errors').S3Error;
Expand Down Expand Up @@ -112,9 +113,9 @@ async function put_bucket_lifecycle(req) {
dbg.error('Rule should have status', rule);
throw new S3Error(S3Error.InvalidArgument);
} else if (rule.Status?.length === 1) {
if (rule.Status[0] !== "Enabled" && rule.Status[0] !== "Disabled") {
if (!s3_const.STATUS_VAL.ENUM.includes(rule.Status[0])) {
dbg.error('Rule should not have status value other than "Enabled" and "Disabled" ', rule);
throw new S3Error({ ...S3Error.MalformedXML, message: 'The XML you provided was not well-formed or did not validate against our published schema' });
throw new S3Error(S3Error.MalformedXML);
}
}
current_rule.status = rule.Status[0];
Expand Down Expand Up @@ -182,12 +183,7 @@ async function put_bucket_lifecycle(req) {
rules: lifecycle_rules
});
} catch (error) {
let err = error;
if (error.rpc_code === "INVALID_SCHEMA" || error.rpc_code === "INVALID_SCHEMA_PARAMS") {
err = new S3Error(S3Error.MalformedPolicy);
err.message = "Policy was not well formed or did not validate against the published schema";
}
throw err;
s3_utils.invalid_schema_to_aws_error(error);
}

dbg.log0('set_bucket_lifecycle', lifecycle_rules);
Expand Down
8 changes: 2 additions & 6 deletions src/endpoint/s3/ops/s3_put_bucket_policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const S3Error = require('../s3_errors').S3Error;
const s3_utils = require('../s3_utils');

/**
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTpolicy.html
Expand All @@ -17,12 +18,7 @@ async function put_bucket_policy(req) {
try {
await req.object_sdk.put_bucket_policy({ name: req.params.bucket, policy });
} catch (error) {
let err = error;
if (error.rpc_code === "INVALID_SCHEMA" || error.rpc_code === "INVALID_SCHEMA_PARAMS") {
err = new S3Error(S3Error.MalformedPolicy);
err.message = "Policy was not well formed or did not validate against the published schema";
}
throw err;
s3_utils.invalid_schema_to_aws_error(error);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/endpoint/s3/s3_constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ const s3_const = exports;
///////////////

s3_const.MAX_RULE_ID_LENGTH = 255;
s3_const.STATUS_VAL = Object.freeze({
ENUM: Object.freeze(['Enabled', 'Disabled']),
TYPE: 'string'
});
2 changes: 1 addition & 1 deletion src/endpoint/s3/s3_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ S3Error.MalformedPOSTRequest = Object.freeze({
});
S3Error.MalformedXML = Object.freeze({
code: 'MalformedXML',
message: 'This happens when the user sends malformed xml (xml that doesn\'t conform to the published xsd) for the configuration. The error message is, "The XML you provided was not well-formed or did not validate against our published schema."',
message: 'The XML you provided was not well-formed or did not validate against our published schema.',
http_code: 400,
});
S3Error.InvalidTag = Object.freeze({
Expand Down
10 changes: 10 additions & 0 deletions src/endpoint/s3/s3_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,15 @@ function key_marker_to_cont_tok(key_marker, objects_arr, is_truncated) {
return Buffer.from(j).toString('base64');
}

function invalid_schema_to_aws_error(error) {
if (["INVALID_SCHEMA", "INVALID_SCHEMA_PARAMS"].includes(error.rpc_code)) {
const err = new S3Error(S3Error.MalformedPolicy);
err.message = "Policy was not well-formed or did not validate against the published schema";
throw err;
}
throw error;
}

exports.STORAGE_CLASS_STANDARD = STORAGE_CLASS_STANDARD;
exports.STORAGE_CLASS_GLACIER = STORAGE_CLASS_GLACIER;
exports.STORAGE_CLASS_GLACIER_IR = STORAGE_CLASS_GLACIER_IR;
Expand Down Expand Up @@ -827,6 +836,7 @@ exports.get_default_object_owner = get_default_object_owner;
exports.set_response_supported_storage_classes = set_response_supported_storage_classes;
exports.cont_tok_to_key_marker = cont_tok_to_key_marker;
exports.key_marker_to_cont_tok = key_marker_to_cont_tok;
exports.invalid_schema_to_aws_error = invalid_schema_to_aws_error;
exports.parse_sse_c = parse_sse_c;
exports.OBJECT_ATTRIBUTES = OBJECT_ATTRIBUTES;
exports.OBJECT_ATTRIBUTES_UNSUPPORTED = OBJECT_ATTRIBUTES_UNSUPPORTED;

0 comments on commit 7992052

Please sign in to comment.