Skip to content

Commit

Permalink
Created new function to check for whitespace
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Jan 29, 2025
1 parent c2836b4 commit 0b8a0a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 20 additions & 5 deletions src/manage_nsfs/manage_nsfs_validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,6 @@ function validate_options_type_by_value(input_options_with_data) {
const details = `type of flag ${option} should be ${type_of_option} (and the received value is ${value})`;
throw_cli_error(ManageCLIError.InvalidArgumentType, details);
}
// ensure access_keys values are not empty or whitespace only strings
if (['access_key', 'secret_key'].includes(option) && value.trim() === '') {
const details = `flag ${option} cannot have an empty value or contain only whitespace. Please provide a valid value for ${option}`;
throw_cli_error(ManageCLIError.InvalidArgument, details);
}
}
}

Expand All @@ -216,6 +211,20 @@ function validate_boolean_string_value(value) {
return false;
}

/**
* validate_no_leading_whitespace validates that a given value is not empty or whitespace-only
* @param {string} option
* @param {string} value
* @throws {ManageCLIError}
*/
function validate_no_leading_whitespace(option, value) {
// ensure values are not empty or whitespace only strings
if (value.trim() === '') {
const details = `flag ${option} cannot have an empty value or contain only whitespace. Please provide a valid value for ${option}`;
throw_cli_error(ManageCLIError.InvalidArgument, details);
}
}

/**
* validates supplemental groups array.
* string type: is comma seperated positive numbers. should not begin or end with a comma.
Expand Down Expand Up @@ -579,6 +588,12 @@ function _validate_access_keys(access_key, secret_key) {
throw_cli_error(ManageCLIError.MissingAccountAccessKeyFlag);
}

// checking access_key and secret_key do not contain only whitespace
if (access_key && secret_key) {
validate_no_leading_whitespace('access_key', access_key);
validate_no_leading_whitespace('secret_key', secret_key);
}

// checking access_key length=20 and contains only alphanumeric chars
if (access_key && !string_utils.access_key_regexp.test(access_key)) {
throw_cli_error(ManageCLIError.InvalidAccountAccessKeyFlag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('manage nsfs cli account flow', () => {
const { type, name, uid, gid, secret_key } = defaults;
const account_options = { config_root, name, uid, gid, secret_key};
const action = ACTIONS.ADD;
const command = create_command(type, action, account_options)
const command = create_command(type, action, account_options);
const flag = 'access_key';
const value = ' 12345678912345678ABC'; // leading space for access_key
const res = await exec_manage_cli_add_leading_space_option(command, flag, value);
Expand All @@ -118,7 +118,7 @@ describe('manage nsfs cli account flow', () => {
const { type, name, uid, gid, access_key } = defaults;
const account_options = { config_root, name, uid, gid, access_key};
const action = ACTIONS.ADD;
const command = create_command(type, action, account_options)
const command = create_command(type, action, account_options);
const flag = 'secret_key';
const value = ' a234567891234567891212345678912345678912'; // leading space for secret_key
const res = await exec_manage_cli_add_leading_space_option(command, flag, value);
Expand Down

0 comments on commit 0b8a0a1

Please sign in to comment.