Skip to content

Commit f665bd6

Browse files
authored
Merge pull request #7813 from shirady/nsfs-nc-optimize-verify-bucket-owner
NSFS | NC | Improve Performance in `verify_bucket_owner`
2 parents 075a950 + fb27399 commit f665bd6

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/cmd/manage_nsfs.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,40 +215,30 @@ async function add_bucket(data) {
215215
}
216216

217217
/** verify_bucket_owner will check if the bucket_owner has an account
218+
* bucket_owner is the account name in the account schema
218219
* after it finds one, it returns the account id, otherwise it would throw an error
219220
* (in case the action is add bucket it also checks that the owner has allow_bucket_creation)
220-
* @param {string} bucket_owner account name
221+
* @param {string} bucket_owner
221222
* @param {string} action
222223
*/
223224
async function verify_bucket_owner(bucket_owner, action) {
224-
let is_bucket_owner_exist = false;
225-
let is_allow_bucket_creation = false;
226-
let account_id;
227-
const show_secrets = false;
228-
const fs_context = native_fs_utils.get_process_fs_context();
229-
const entries = await nb_native().fs.readdir(fs_context, accounts_dir_path);
230-
// Gap - should replace this implementation
231-
// it keeps iterating even if we find that the bucket owner exist
232-
await P.map_with_concurrency(10, entries, async entry => {
233-
if (entry.name.endsWith('.json') && !is_bucket_owner_exist) {
234-
const full_path = path.join(accounts_dir_path, entry.name);
235-
const data = await get_config_data(full_path, show_secrets);
236-
if (data.name === bucket_owner) {
237-
is_bucket_owner_exist = true;
238-
is_allow_bucket_creation = data.allow_bucket_creation;
239-
account_id = data._id;
240-
}
225+
// check if bucket owner exists
226+
const account_config_path = get_config_file_path(accounts_dir_path, bucket_owner);
227+
let account;
228+
try {
229+
account = await get_config_data(account_config_path);
230+
} catch (err) {
231+
if (err.code === 'ENOENT') {
232+
const detail_msg = `bucket owner ${bucket_owner} does not exists`;
233+
throw_cli_error(ManageCLIError.BucketSetForbiddenNoBucketOwner, detail_msg, {bucket_owner: bucket_owner});
241234
}
242-
});
243-
244-
if (!is_bucket_owner_exist) {
245-
const detail_msg = `bucket owner ${bucket_owner} does not exists`;
246-
throw_cli_error(ManageCLIError.BucketSetForbiddenNoBucketOwner, detail_msg, {bucket_owner: bucket_owner});
235+
throw err;
247236
}
248-
if (action === ACTIONS.ADD && !is_allow_bucket_creation) {
237+
// check if bucket owner has the permission to create bucket (for bucket add only)
238+
if (action === ACTIONS.ADD && !account.allow_bucket_creation) {
249239
throw_cli_error(ManageCLIError.BucketCreationNotAllowed, bucket_owner);
250240
}
251-
return account_id;
241+
return account._id;
252242
}
253243

254244
async function get_bucket_status(data) {
@@ -552,13 +542,12 @@ async function delete_account(data) {
552542
* @param {string} account_name
553543
*/
554544
async function verify_delete_account(account_name) {
555-
const show_secrets = false; // in buckets we don't save secrets in coofig file
556545
const fs_context = native_fs_utils.get_process_fs_context();
557546
const entries = await nb_native().fs.readdir(fs_context, buckets_dir_path);
558547
await P.map_with_concurrency(10, entries, async entry => {
559548
if (entry.name.endsWith('.json')) {
560549
const full_path = path.join(buckets_dir_path, entry.name);
561-
const data = await get_config_data(full_path, show_secrets);
550+
const data = await get_config_data(full_path);
562551
if (data.bucket_owner === account_name) {
563552
const detail_msg = `Account ${account_name} has bucket ${data.name}`;
564553
throw_cli_error(ManageCLIError.AccountDeleteForbiddenHasBuckets, detail_msg);
@@ -691,7 +680,7 @@ async function list_config_files(type, config_path, wide, show_secrets, filters)
691680
* @param {string} config_file_path
692681
* @param {boolean} [show_secrets]
693682
*/
694-
async function get_config_data(config_file_path, show_secrets) {
683+
async function get_config_data(config_file_path, show_secrets = false) {
695684
const fs_context = native_fs_utils.get_process_fs_context();
696685
const { data } = await nb_native().fs.readFile(fs_context, config_file_path);
697686
const config_data = _.omit(JSON.parse(data.toString()), show_secrets ? [] : ['access_keys']);

0 commit comments

Comments
 (0)