Skip to content

Commit 5a6620d

Browse files
Moved functions to shared utils
Signed-off-by: Aayush Chouhan <[email protected]>
1 parent 348bb2e commit 5a6620d

File tree

2 files changed

+122
-117
lines changed

2 files changed

+122
-117
lines changed

src/sdk/bucketspace_fs.js

Lines changed: 120 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,125 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
921921
///// UTILS /////
922922
/////////////////
923923

924-
// TODO: move the following 6 functions - is_bucket_owner(), is_iam_and_same_root_account_owner(), has_bucket_ownership_permission(), has_bucket_action_permission(), validate_fs_bucket_access(), _has_access_to_nsfs_dir()
925-
// so they can be re-used
924+
is_nsfs_containerized_user_anonymous(token) {
925+
return !token && !process.env.NC_NSFS_NO_DB_ENV;
926+
}
927+
928+
is_nsfs_non_containerized_user_anonymous(token) {
929+
return !token && process.env.NC_NSFS_NO_DB_ENV;
930+
}
931+
932+
/**
933+
* returns a list of storage class supported by this bucketspace
934+
* @returns {Array<string>}
935+
*/
936+
_supported_storage_class() {
937+
const storage_classes = [];
938+
if (!config.DENY_UPLOAD_TO_STORAGE_CLASS_STANDARD) {
939+
storage_classes.push(s3_utils.STORAGE_CLASS_STANDARD);
940+
}
941+
if (config.NSFS_GLACIER_ENABLED) {
942+
storage_classes.push(s3_utils.STORAGE_CLASS_GLACIER);
943+
}
944+
945+
return storage_classes;
946+
}
947+
948+
/**
949+
* _is_bucket_empty returns true if the given bucket is empty
950+
*
951+
* @param {*} ns
952+
* @param {*} params
953+
* @param {string} name
954+
* @param {nb.ObjectSDK} object_sdk
955+
*
956+
* @returns {Promise<boolean>}
957+
*/
958+
static async _is_bucket_empty(name, params, ns, object_sdk) {
959+
params = params || {};
960+
961+
let list;
962+
try {
963+
if (ns._is_versioning_disabled()) {
964+
list = await ns.list_objects({ ...params, bucket: name, limit: 1 }, object_sdk);
965+
} else {
966+
list = await ns.list_object_versions({ ...params, bucket: name, limit: 1 }, object_sdk);
967+
}
968+
} catch (err) {
969+
dbg.warn('_is_bucket_empty: bucket name', name, 'got an error while trying to list_objects', err);
970+
// in case the ULS was deleted - we will continue
971+
if (err.rpc_code !== 'NO_SUCH_BUCKET') throw err;
972+
}
973+
974+
return !(list && list.objects && list.objects.length > 0);
975+
}
976+
977+
/**
978+
* _generate_reserved_tag_event_args returns the list of reserved
979+
* bucket tags which have been modified and also returns a variable
980+
* indicating if there has been any modifications at all.
981+
* @param {Record<string, string>} prev_tags_objectified
982+
* @param {Array<{key: string, value: string}>} new_tags
983+
*/
984+
static _generate_reserved_tag_event_args(prev_tags_objectified, new_tags) {
985+
let reserved_tag_modified = false;
986+
const reserved_tag_event_args = new_tags?.reduce((curr, tag) => {
987+
const tag_info = config.NSFS_GLACIER_RESERVED_BUCKET_TAGS[tag.key];
988+
989+
// If not a reserved tag - skip
990+
if (!tag_info) return curr;
991+
992+
// If no event is requested - skip
993+
if (!tag_info.event) return curr;
994+
995+
// If value didn't change - skip
996+
if (_.isEqual(prev_tags_objectified[tag.key], tag.value)) return curr;
997+
998+
reserved_tag_modified = true;
999+
return Object.assign(curr, { [tag.key]: tag.value });
1000+
}, {});
1001+
1002+
return [reserved_tag_event_args, reserved_tag_modified];
1003+
}
1004+
1005+
/**
1006+
* @param {Array<{key: string, value: string}>} tagging
1007+
* @returns {Record<string, string>}
1008+
*/
1009+
static _objectify_tagging_arr(tagging) {
1010+
return (tagging || []).reduce((curr, tag) => Object.assign(curr, { [tag.key]: tag.value }), {});
1011+
}
1012+
1013+
/**
1014+
* _create_uls creates underylying storage at the given storage_path
1015+
* @param {*} fs_context - root fs_context
1016+
* @param {*} acc_fs_context - fs_context associated with the performing account
1017+
* @param {*} name - bucket name
1018+
* @param {*} storage_path - bucket's storage path
1019+
* @param {*} cfg_path - bucket's configuration path
1020+
*/
1021+
static async _create_uls(fs_context, acc_fs_context, name, storage_path, cfg_path) {
1022+
try {
1023+
await nb_native().fs.mkdir(acc_fs_context, storage_path, get_umasked_mode(config.BASE_MODE_DIR));
1024+
} catch (error) {
1025+
dbg.error('BucketSpaceFS: _create_uls could not create underlying directory - nsfs, deleting bucket', error);
1026+
new NoobaaEvent(NoobaaEvent.BUCKET_DIR_CREATION_FAILED)
1027+
.create_event(name, { bucket: name, path: storage_path }, error);
1028+
await nb_native().fs.unlink(fs_context, cfg_path);
1029+
throw translate_error_codes(error, entity_enum.BUCKET);
1030+
}
1031+
}
1032+
1033+
//////////////////
1034+
// SHARED UTILS //
1035+
//////////////////
1036+
1037+
// TODO: move the below functions to a shared utils file so they can be re-used
9261038

9271039
/**
9281040
* is_bucket_owner checks if the account is the direct owner of the bucket
929-
* @param {Record<string, any>} bucket
930-
* @param {Record<string, any>} account
1041+
* @param {nb.Bucket} bucket
1042+
* @param {nb.Account} account
9311043
* @returns {boolean}
9321044
*/
9331045
is_bucket_owner(bucket, account) {
@@ -937,8 +1049,8 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
9371049

9381050
/**
9391051
* is_iam_and_same_root_account_owner checks if the account is an IAM user and the bucket is owned by their root account
940-
* @param {Record<string, any>} account
941-
* @param {Record<string, any>} bucket
1052+
* @param {nb.Account} account
1053+
* @param {nb.Bucket} bucket
9421054
* @returns {boolean}
9431055
*/
9441056
is_iam_and_same_root_account_owner(account, bucket) {
@@ -953,8 +1065,8 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
9531065
* - Root accounts can list buckets they own
9541066
* - IAM users can list their owner buckets
9551067
*
956-
* @param {Record<string, any>} bucket
957-
* @param {Record<string, any>} account
1068+
* @param {nb.Bucket} bucket
1069+
* @param {nb.Account} account
9581070
* @returns {boolean}
9591071
*/
9601072
has_bucket_ownership_permission(bucket, account) {
@@ -1039,115 +1151,6 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
10391151
throw err;
10401152
}
10411153
}
1042-
1043-
is_nsfs_containerized_user_anonymous(token) {
1044-
return !token && !process.env.NC_NSFS_NO_DB_ENV;
1045-
}
1046-
1047-
is_nsfs_non_containerized_user_anonymous(token) {
1048-
return !token && process.env.NC_NSFS_NO_DB_ENV;
1049-
}
1050-
1051-
/**
1052-
* returns a list of storage class supported by this bucketspace
1053-
* @returns {Array<string>}
1054-
*/
1055-
_supported_storage_class() {
1056-
const storage_classes = [];
1057-
if (!config.DENY_UPLOAD_TO_STORAGE_CLASS_STANDARD) {
1058-
storage_classes.push(s3_utils.STORAGE_CLASS_STANDARD);
1059-
}
1060-
if (config.NSFS_GLACIER_ENABLED) {
1061-
storage_classes.push(s3_utils.STORAGE_CLASS_GLACIER);
1062-
}
1063-
1064-
return storage_classes;
1065-
}
1066-
1067-
/**
1068-
* _is_bucket_empty returns true if the given bucket is empty
1069-
*
1070-
* @param {*} ns
1071-
* @param {*} params
1072-
* @param {string} name
1073-
* @param {nb.ObjectSDK} object_sdk
1074-
*
1075-
* @returns {Promise<boolean>}
1076-
*/
1077-
static async _is_bucket_empty(name, params, ns, object_sdk) {
1078-
params = params || {};
1079-
1080-
let list;
1081-
try {
1082-
if (ns._is_versioning_disabled()) {
1083-
list = await ns.list_objects({ ...params, bucket: name, limit: 1 }, object_sdk);
1084-
} else {
1085-
list = await ns.list_object_versions({ ...params, bucket: name, limit: 1 }, object_sdk);
1086-
}
1087-
} catch (err) {
1088-
dbg.warn('_is_bucket_empty: bucket name', name, 'got an error while trying to list_objects', err);
1089-
// in case the ULS was deleted - we will continue
1090-
if (err.rpc_code !== 'NO_SUCH_BUCKET') throw err;
1091-
}
1092-
1093-
return !(list && list.objects && list.objects.length > 0);
1094-
}
1095-
1096-
/**
1097-
* _generate_reserved_tag_event_args returns the list of reserved
1098-
* bucket tags which have been modified and also returns a variable
1099-
* indicating if there has been any modifications at all.
1100-
* @param {Record<string, string>} prev_tags_objectified
1101-
* @param {Array<{key: string, value: string}>} new_tags
1102-
*/
1103-
static _generate_reserved_tag_event_args(prev_tags_objectified, new_tags) {
1104-
let reserved_tag_modified = false;
1105-
const reserved_tag_event_args = new_tags?.reduce((curr, tag) => {
1106-
const tag_info = config.NSFS_GLACIER_RESERVED_BUCKET_TAGS[tag.key];
1107-
1108-
// If not a reserved tag - skip
1109-
if (!tag_info) return curr;
1110-
1111-
// If no event is requested - skip
1112-
if (!tag_info.event) return curr;
1113-
1114-
// If value didn't change - skip
1115-
if (_.isEqual(prev_tags_objectified[tag.key], tag.value)) return curr;
1116-
1117-
reserved_tag_modified = true;
1118-
return Object.assign(curr, { [tag.key]: tag.value });
1119-
}, {});
1120-
1121-
return [reserved_tag_event_args, reserved_tag_modified];
1122-
}
1123-
1124-
/**
1125-
* @param {Array<{key: string, value: string}>} tagging
1126-
* @returns {Record<string, string>}
1127-
*/
1128-
static _objectify_tagging_arr(tagging) {
1129-
return (tagging || []).reduce((curr, tag) => Object.assign(curr, { [tag.key]: tag.value }), {});
1130-
}
1131-
1132-
/**
1133-
* _create_uls creates underylying storage at the given storage_path
1134-
* @param {*} fs_context - root fs_context
1135-
* @param {*} acc_fs_context - fs_context associated with the performing account
1136-
* @param {*} name - bucket name
1137-
* @param {*} storage_path - bucket's storage path
1138-
* @param {*} cfg_path - bucket's configuration path
1139-
*/
1140-
static async _create_uls(fs_context, acc_fs_context, name, storage_path, cfg_path) {
1141-
try {
1142-
await nb_native().fs.mkdir(acc_fs_context, storage_path, get_umasked_mode(config.BASE_MODE_DIR));
1143-
} catch (error) {
1144-
dbg.error('BucketSpaceFS: _create_uls could not create underlying directory - nsfs, deleting bucket', error);
1145-
new NoobaaEvent(NoobaaEvent.BUCKET_DIR_CREATION_FAILED)
1146-
.create_event(name, { bucket: name, path: storage_path }, error);
1147-
await nb_native().fs.unlink(fs_context, cfg_path);
1148-
throw translate_error_codes(error, entity_enum.BUCKET);
1149-
}
1150-
}
11511154
}
11521155

11531156
module.exports = BucketSpaceFS;

src/sdk/nb.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface System extends Base {
6363

6464
interface Account extends Base {
6565
_id: ID;
66+
owner: ID;
6667
name: string;
6768
system: System;
6869
email: SensitiveString;
@@ -179,6 +180,7 @@ interface MirrorStatus {
179180

180181
interface Bucket extends Base {
181182
_id: ID;
183+
owner_account: ID;
182184
deleted?: Date;
183185
name: SensitiveString;
184186
system: System;

0 commit comments

Comments
 (0)