Skip to content

Commit

Permalink
Merge pull request #8224 from shirady/nsfs-nc-delete-bucket-no-path
Browse files Browse the repository at this point in the history
NC | NSFS | S3 Flow `delete_bucket` When ULS Was Deleted + Refactor Function `translate_error_codes`
  • Loading branch information
shirady authored Jul 28, 2024
2 parents c5026ab + 1f2cfcf commit c1a4b5d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 94 deletions.
45 changes: 14 additions & 31 deletions src/sdk/accountspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ const { get_symlink_config_file_path, get_config_file_path, get_config_data,
const nc_mkm = require('../manage_nsfs/nc_master_key_manager').get_instance();
const { account_cache } = require('./object_sdk');

const entity_enum = Object.freeze({
USER: 'USER',
ACCESS_KEY: 'ACCESS_KEY',
});

// TODO - rename (the typo), move and reuse in manage_nsfs
const acounts_dir_relative_path = '../accounts/';

Expand Down Expand Up @@ -87,7 +82,7 @@ class AccountSpaceFS {
};
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.USER);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.USER);
}
}

Expand Down Expand Up @@ -125,7 +120,7 @@ class AccountSpaceFS {
};
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.USER);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.USER);
}
}

Expand Down Expand Up @@ -179,7 +174,7 @@ class AccountSpaceFS {
};
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.USER);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.USER);
}
}

Expand Down Expand Up @@ -213,7 +208,7 @@ class AccountSpaceFS {
await native_fs_utils.delete_config_file(this.fs_context, this.accounts_dir, account_config_path);
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.USER);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.USER);
}
}

Expand All @@ -237,7 +232,7 @@ class AccountSpaceFS {
return { members, is_truncated };
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.USER);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.USER);
}
}

Expand Down Expand Up @@ -297,7 +292,7 @@ class AccountSpaceFS {
};
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.ACCESS_KEY);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.ACCESS_KEY);
}
}

Expand Down Expand Up @@ -329,7 +324,7 @@ class AccountSpaceFS {
};
} catch (err) {
dbg.error('AccountSpaceFS.get_access_key_last_used error', err);
throw this._translate_error_codes(err, entity_enum.ACCESS_KEY);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.ACCESS_KEY);
}
}

Expand Down Expand Up @@ -380,7 +375,7 @@ class AccountSpaceFS {
this._clean_account_cache(requested_account);
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.ACCESS_KEY);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.ACCESS_KEY);
}
}

Expand Down Expand Up @@ -426,7 +421,7 @@ class AccountSpaceFS {
this._clean_account_cache(requested_account);
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.ACCESS_KEY);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.ACCESS_KEY);
}
}

Expand Down Expand Up @@ -460,26 +455,14 @@ class AccountSpaceFS {
return { members, is_truncated, username: name_for_access_key };
} catch (err) {
dbg.error(`AccountSpaceFS.${action} error`, err);
throw this._translate_error_codes(err, entity_enum.ACCESS_KEY);
throw native_fs_utils.translate_error_codes(err, native_fs_utils.entity_enum.ACCESS_KEY);
}
}

////////////////////////
// INTERNAL FUNCTIONS //
////////////////////////

// this function was copied from namespace_fs and bucketspace_fs
// It is a fallback that we use, but might be not accurate
_translate_error_codes(err, entity) {
if (err.rpc_code) return err;
if (err.code === 'ENOENT') err.rpc_code = `NO_SUCH_${entity}`;
if (err.code === 'EEXIST') err.rpc_code = `${entity}_ALREADY_EXISTS`;
if (err.code === 'EPERM' || err.code === 'EACCES') err.rpc_code = 'UNAUTHORIZED';
if (err.code === 'IO_STREAM_ITEM_TIMEOUT') err.rpc_code = 'IO_STREAM_ITEM_TIMEOUT';
if (err.code === 'INTERNAL_ERROR') err.rpc_code = 'INTERNAL_ERROR';
return err;
}

_get_account_config_path(name) {
return get_config_file_path(this.accounts_dir, name);
}
Expand Down Expand Up @@ -572,7 +555,7 @@ class AccountSpaceFS {
const basic_message = `User: ${arn_for_requesting_account} is not authorized to perform:` +
`${full_action_name} on resource: `;
let message_with_details;
if (entity === entity_enum.USER) {
if (entity === native_fs_utils.entity_enum.USER) {
let user_message;
if (action === 'list_access_keys') {
user_message = `user ${requesting_account.name.unwrap()}`;
Expand All @@ -581,7 +564,7 @@ class AccountSpaceFS {
}
message_with_details = basic_message +
`${user_message} because no identity-based policy allows the ${full_action_name} action`;
} else { // entity_enum.ACCESS_KEY
} else { // native_fs_utils.entity_enum.ACCESS_KEY
message_with_details = basic_message + `access key ${details.access_key}`;
}
const { code, http_code, type } = IamError.AccessDeniedException;
Expand Down Expand Up @@ -658,7 +641,7 @@ class AccountSpaceFS {
if (!is_root_account) {
dbg.error(`AccountSpaceFS.${action} requesting account is not a root account`,
requesting_account);
this._throw_access_denied_error(action, requesting_account, user_details, entity_enum.USER);
this._throw_access_denied_error(action, requesting_account, user_details, native_fs_utils.entity_enum.USER);
}
}

Expand Down Expand Up @@ -895,7 +878,7 @@ class AccountSpaceFS {
dbg.error(`AccountSpaceFS.${action} requesting account is neither a root account ` +
`nor user requester on himself`,
requesting_account);
this._throw_access_denied_error(action, requesting_account, { username }, entity_enum.USER);
this._throw_access_denied_error(action, requesting_account, { username }, native_fs_utils.entity_enum.USER);
}
return requester;
}
Expand Down
71 changes: 38 additions & 33 deletions src/sdk/bucketspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const { CONFIG_SUBDIRS } = require('../manage_nsfs/manage_nsfs_constants');

const KeysSemaphore = require('../util/keys_semaphore');
const { get_umasked_mode, isDirectory, validate_bucket_creation,
create_config_file, delete_config_file, get_bucket_tmpdir_full_path, folder_delete } = require('../util/native_fs_utils');
create_config_file, delete_config_file, get_bucket_tmpdir_full_path, folder_delete,
entity_enum, translate_error_codes } = require('../util/native_fs_utils');
const NoobaaEvent = require('../manage_nsfs/manage_nsfs_events_utils').NoobaaEvent;
const { anonymous_access_key } = require('./object_sdk');

Expand Down Expand Up @@ -86,16 +87,6 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
}

_translate_bucket_error_codes(err) {
if (err.rpc_code) return err;
if (err.code === 'ENOENT') err.rpc_code = 'NO_SUCH_BUCKET';
if (err.code === 'EEXIST') err.rpc_code = 'BUCKET_ALREADY_EXISTS';
if (err.code === 'EPERM' || err.code === 'EACCES') err.rpc_code = 'UNAUTHORIZED';
if (err.code === 'IO_STREAM_ITEM_TIMEOUT') err.rpc_code = 'IO_STREAM_ITEM_TIMEOUT';
if (err.code === 'INTERNAL_ERROR') err.rpc_code = 'INTERNAL_ERROR';
return err;
}

async read_account_by_access_key({ access_key }) {
try {
if (!access_key) throw new Error('no access key');
Expand Down Expand Up @@ -178,7 +169,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
return bucket;
} catch (err) {
const rpc_error = this._translate_bucket_error_codes(err);
const rpc_error = translate_error_codes(err, entity_enum.BUCKET);
if (err.rpc_code === 'INVALID_SCHEMA') err.rpc_code = 'INVALID_BUCKET_STATE';
new NoobaaEvent(NoobaaEvent[rpc_error.rpc_code]).create_event(name, {bucket_name: name}, err);
throw rpc_error;
Expand Down Expand Up @@ -222,7 +213,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
dbg.error('BucketSpaceFS: root dir not found', err, this.bucket_schema_dir);
throw new S3Error(S3Error.NoSuchBucket);
}
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}

const account = object_sdk.requesting_account;
Expand Down Expand Up @@ -302,7 +293,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
await create_config_file(this.fs_context, this.bucket_schema_dir, bucket_config_path, bucket_config);
} catch (err) {
new NoobaaEvent(NoobaaEvent.BUCKET_CREATION_FAILED).create_event(name, {bucket_name: name}, err);
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}

// create bucket's underlying storage directory
Expand All @@ -313,7 +304,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
dbg.error('BucketSpaceFS: create_bucket could not create underlying directory - nsfs, deleting bucket', err);
new NoobaaEvent(NoobaaEvent.BUCKET_DIR_CREATION_FAILED).create_event(name, {bucket: name, path: bucket_storage_path}, err);
await nb_native().fs.unlink(this.fs_context, bucket_config_path);
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
});
}
Expand Down Expand Up @@ -359,13 +350,27 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
if (!namespace_bucket_config) throw new RpcError('INTERNAL_ERROR', 'Invalid Bucket configuration');

if (namespace_bucket_config.should_create_underlying_storage) {
// 1. delete underlying storage
// 1. delete underlying storage (ULS = Underline Storage)
dbg.log1('BucketSpaceFS.delete_bucket: deleting uls', this.fs_root, namespace_bucket_config.write_resource.path);
const bucket_storage_path = path.join(this.fs_root, namespace_bucket_config.write_resource.path); // includes write_resource.path + bucket name (s3 flow)
await ns.delete_uls({ name, full_path: bucket_storage_path }, object_sdk);
try {
await ns.delete_uls({ name, full_path: bucket_storage_path }, object_sdk);
} catch (err) {
dbg.warn('delete_bucket: bucket name', name, 'with bucket_storage_path', bucket_storage_path,
'got an error while trying to delete_uls', err);
// in case the ULS was deleted - we will continue
if (err.rpc_code !== 'NO_SUCH_BUCKET') throw err;
}
} else {
// 2. delete only bucket tmpdir
const list = await ns.list_objects({ ...params, bucket: name, limit: 1 }, object_sdk);
let list;
try {
list = await ns.list_objects({ ...params, bucket: name, limit: 1 }, object_sdk);
} catch (err) {
dbg.warn('delete_bucket: bucket name', name, 'got an error while trying to list_objects', err);
// in case the ULS was deleted - we will continue
if (err.rpc_code !== 'NO_SUCH_BUCKET') throw err;
}
if (list && list.objects && list.objects.length > 0) throw new RpcError('NOT_EMPTY', 'underlying directory has files in it');
const bucket_tmpdir_path = get_bucket_tmpdir_full_path(namespace_bucket_config.write_resource.path, bucket._id);
await folder_delete(bucket_tmpdir_path, this.fs_context, true);
Expand All @@ -375,10 +380,10 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
await delete_config_file(this.fs_context, this.bucket_schema_dir, bucket_config_path);
new NoobaaEvent(NoobaaEvent.BUCKET_DELETE).create_event(name, { bucket_name: name });
} catch (err) {
dbg.error('BucketSpaceFS: delete_bucket: error', err);
dbg.error('BucketSpaceFS: delete_bucket: bucket name', name, 'error', err);
new NoobaaEvent(NoobaaEvent.BUCKET_DELETE_FAILED).create_event(name,
{ bucket_name: name, bucket_path: bucket_config_path }, err);
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
});
}
Expand Down Expand Up @@ -426,7 +431,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand Down Expand Up @@ -484,7 +489,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -508,7 +513,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
}
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -521,7 +526,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
const bucket = JSON.parse(data.toString());
return bucket.logging;
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand Down Expand Up @@ -550,7 +555,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -563,7 +568,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
const bucket = JSON.parse(data.toString());
return bucket.encryption;
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -586,7 +591,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand Down Expand Up @@ -614,7 +619,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -637,7 +642,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -654,7 +659,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
const bucket = JSON.parse(data.toString());
return { website: bucket.website };
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand Down Expand Up @@ -685,7 +690,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -708,7 +713,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
{ mode: get_umasked_mode(config.BASE_MODE_CONFIG_FILE) }
);
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand All @@ -722,7 +727,7 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
policy: bucket_policy_info.s3_policy
};
} catch (err) {
throw this._translate_bucket_error_codes(err);
throw translate_error_codes(err, entity_enum.BUCKET);
}
}

Expand Down
Loading

0 comments on commit c1a4b5d

Please sign in to comment.