Skip to content

Commit

Permalink
CR changes
Browse files Browse the repository at this point in the history
Signed-off-by: shirady <57721533+shirady@users.noreply.github.com>
  • Loading branch information
shirady committed Jan 18, 2024
1 parent 64f0a26 commit 10f7555
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
49 changes: 21 additions & 28 deletions src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function get_symlink_config_file_path(config_type_path, file_name) {

async function add_bucket(data) {
await validate_bucket_args(data, ACTIONS.ADD);
await verify_create_bucket(data.bucket_owner.unwrap());
await verify_bucket_owner(data.bucket_owner.unwrap());
const fs_context = native_fs_utils.get_process_fs_context(config_root_backend);
const bucket_conf_path = get_config_file_path(buckets_dir_path, data.name);
const exists = await native_fs_utils.config_file_exists(fs_context, bucket_conf_path);
Expand All @@ -303,25 +303,30 @@ async function add_bucket(data) {
write_stdout_response(ManageCLIResponse.BucketCreated, data_json);
}

/** check_bucket_owner checks if the bucket_owner has an account
* @param {string} bucket_owner
*/
async function check_bucket_owner(bucket_owner) {
const account_config_files = await list_config_files(accounts_dir_path);
const is_bucket_owner_exist = account_config_files.some(bucket_config_file =>
bucket_config_file.email === bucket_owner);
return is_bucket_owner_exist;
}

/** verify_create_bucket will check if the bucket_owner has an account
/** verify_bucket_owner will check if the bucket_owner has an account
* in case it does not find one, it would throw an error
* @param {string} bucket_owner
*/
async function verify_create_bucket(bucket_owner) {
const is_bucket_owner_exist = await check_bucket_owner(bucket_owner);
async function verify_bucket_owner(bucket_owner) {
let is_bucket_owner_exist = false;
const show_secrets = false;
const fs_context = native_fs_utils.get_process_fs_context();
const entries = await nb_native().fs.readdir(fs_context, accounts_dir_path);
await P.map_with_concurrency(10, entries, async entry => {
if (entry.name.endsWith('.json')) {
const full_path = path.join(accounts_dir_path, entry.name);
const data = await get_config_data(full_path, show_secrets);
if (data.email === bucket_owner) {
is_bucket_owner_exist = true;
return;
}
return data;
}
});

if (!is_bucket_owner_exist) {
const detail_msg = `bucket owner ${bucket_owner} does not exists`;
throw_cli_error(ManageCLIError.BucketCreateForbiddenNoBucketOwner, detail_msg);
throw_cli_error(ManageCLIError.BucketSetForbiddenNoBucketOwner, detail_msg);
}
}

Expand All @@ -340,7 +345,7 @@ async function get_bucket_status(data) {

async function update_bucket(data) {
await validate_bucket_args(data, ACTIONS.UPDATE);
await verify_update_bucket(data.bucket_owner.unwrap());
await verify_bucket_owner(data.bucket_owner.unwrap());

const fs_context = native_fs_utils.get_process_fs_context(config_root_backend);

Expand Down Expand Up @@ -377,18 +382,6 @@ async function update_bucket(data) {
write_stdout_response(ManageCLIResponse.BucketUpdated, data);
}

/** verify_update_bucket will check if the bucket_owner has an account
* in case it does not find one, it would throw an error
* @param {string} bucket_owner
*/
async function verify_update_bucket(bucket_owner) {
const is_bucket_owner_exist = await check_bucket_owner(bucket_owner);
if (!is_bucket_owner_exist) {
const detail_msg = `bucket owner ${bucket_owner} does not exists`;
throw_cli_error(ManageCLIError.BucketUpdateForbiddenNoBucketOwner, detail_msg);
}
}

async function delete_bucket(data) {
await validate_bucket_args(data, ACTIONS.DELETE);

Expand Down
9 changes: 1 addition & 8 deletions src/manage_nsfs/manage_nsfs_cli_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,13 @@ ManageCLIError.BucketAlreadyExists = Object.freeze({
http_code: 409,
});

ManageCLIError.BucketCreateForbiddenNoBucketOwner = Object.freeze({
ManageCLIError.BucketSetForbiddenNoBucketOwner = Object.freeze({
code: 'BucketCreateForbiddenNoBucketOwner',
message: 'The bucket owner you set for the bucket does not exist. ' +
'Please set the bucket owner from existing account',
http_code: 403,
});

ManageCLIError.BucketUpdateForbiddenNoBucketOwner = Object.freeze({
code: 'BucketUpdateForbiddenNoBucketOwner',
message: 'The bucket owner you set for the bucket does not exist. ' +
'Please set the bucket owner from existing account',
http_code: 403,
});

/////////////////////////////////
//// BUCKET ARGUMENTS ERRORS ////
/////////////////////////////////
Expand Down
19 changes: 9 additions & 10 deletions src/test/unit_tests/test_nc_nsfs_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ mocha.describe('manage_nsfs cli', function() {
await exec_manage_cli(type, action, bucket_options);
assert.fail('should have failed since the bucket owner does not exist');
} catch (err) {
assert_error(err, ManageCLIError.BucketCreateForbiddenNoBucketOwner);
assert_error(err, ManageCLIError.BucketSetForbiddenNoBucketOwner);
}
});

mocha.it('cli create account for bucket (bucket create requirement to have a bucket owner)', async function() {
const account_name = 'user1';
const account_name2 = 'user2';
const owner_email = email;
const owner_email2 = 'user2@noobaa.io';
const new_buckets_path1 = `${root_path}new_buckets_path_user1111/`;
const new_buckets_path2 = `${root_path}new_buckets_path_user2222/`;
Expand Down Expand Up @@ -300,16 +301,14 @@ mocha.describe('manage_nsfs cli', function() {
await assert_config_file_permissions(config_root, schema_dir, gpfs_bucket_options.name);
});

mocha.it('cli bucket update owner - should fail bucket owner does not exist', async function() {
mocha.it('cli bucket update owner', async function() {
const action = nc_nsfs_manage_actions.UPDATE;
try {
const owner_email_not_exist = 'blalal';
const bucket_options_no_bucket_owner = {...gpfs_bucket_options, owner_email: owner_email_not_exist};
await exec_manage_cli(type, action, bucket_options_no_bucket_owner);
assert.fail('cli bucket update - should fail bucket owner does not exist');
} catch (err) {
assert_error(err, ManageCLIError.BucketUpdateForbiddenNoBucketOwner);
}
gpfs_bucket_options.owner_email = 'user2@noobaa.io';
const bucket_status = await exec_manage_cli(type, action, gpfs_bucket_options);
assert_response(action, type, bucket_status, gpfs_bucket_options);
const bucket = await read_config_file(config_root, schema_dir, gpfs_bucket_options.name);
assert_bucket(bucket, gpfs_bucket_options);
await assert_config_file_permissions(config_root, schema_dir, gpfs_bucket_options.name);
});

mocha.it('cli bucket update to non GPFS', async function() {
Expand Down

0 comments on commit 10f7555

Please sign in to comment.