@@ -9,6 +9,8 @@ const nb_native = require('../util/nb_native');
9
9
const native_fs_utils = require ( '../util/native_fs_utils' ) ;
10
10
const nc_mkm = require ( '../manage_nsfs/nc_master_key_manager' ) . get_instance ( ) ;
11
11
const { TYPES } = require ( '../manage_nsfs/manage_nsfs_constants' ) ;
12
+ const nsfs_schema_utils = require ( '../manage_nsfs/nsfs_schema_utils' ) ;
13
+ const { IS_MAC } = require ( '../util/os_utils' ) ;
12
14
13
15
/* Config directory sub directory comments -
14
16
On 5.18 -
@@ -517,7 +519,7 @@ class ConfigFS {
517
519
*/
518
520
async create_account_config_file ( account_data ) {
519
521
const { name, _id, owner = undefined } = account_data ;
520
- const data_string = JSON . stringify ( account_data ) ;
522
+ const data_string = await this . _prepare_for_account_schema ( account_data ) ;
521
523
const account_path = this . get_identity_path_by_id ( _id ) ;
522
524
const account_dir_path = this . get_identity_dir_path_by_id ( _id ) ;
523
525
@@ -544,7 +546,7 @@ class ConfigFS {
544
546
*/
545
547
async update_account_config_file ( account_new_data , options = { } ) {
546
548
const { name, _id, owner = undefined } = account_new_data ;
547
- const data_string = JSON . stringify ( account_new_data ) ;
549
+ const data_string = await this . _prepare_for_account_schema ( account_new_data ) ;
548
550
const account_path = this . get_identity_path_by_id ( _id ) ;
549
551
const account_dir_path = this . get_identity_dir_path_by_id ( _id ) ;
550
552
await native_fs_utils . update_config_file ( this . fs_context , account_dir_path , account_path , data_string ) ;
@@ -607,7 +609,8 @@ class ConfigFS {
607
609
async unlink_account_name_index ( account_name , account_id_config_path ) {
608
610
const account_name_path = this . get_account_path_by_name ( account_name ) ;
609
611
const full_path = await nb_native ( ) . fs . realpath ( this . fs_context , account_name_path ) ;
610
- if ( full_path === account_id_config_path ) {
612
+ if ( full_path === account_id_config_path ||
613
+ ( IS_MAC && full_path === path . join ( '/private/' , account_id_config_path ) ) ) {
611
614
await nb_native ( ) . fs . unlink ( this . fs_context , account_name_path ) ;
612
615
}
613
616
}
@@ -639,7 +642,8 @@ class ConfigFS {
639
642
async unlink_access_key_index ( access_key , account_id_config_path ) {
640
643
const access_key_path = this . get_account_or_user_path_by_access_key ( access_key ) ;
641
644
const full_path = await nb_native ( ) . fs . realpath ( this . fs_context , access_key_path ) ;
642
- if ( full_path === account_id_config_path ) {
645
+ if ( full_path === account_id_config_path ||
646
+ ( IS_MAC && full_path === path . join ( '/private/' , account_id_config_path ) ) ) {
643
647
await nb_native ( ) . fs . unlink ( this . fs_context , access_key_path ) ;
644
648
}
645
649
}
@@ -709,24 +713,63 @@ class ConfigFS {
709
713
710
714
/**
711
715
* create_bucket_config_file creates bucket config file
712
- * @param {string } bucket_name
713
- * @param {* } data
714
- * @returns {Promise<void> }
716
+ * @param {Object } bucket_data
717
+ * @returns {Promise<String> }
715
718
*/
716
- async create_bucket_config_file ( bucket_name , data ) {
717
- const bucket_path = this . get_bucket_path_by_name ( bucket_name ) ;
718
- await native_fs_utils . create_config_file ( this . fs_context , this . buckets_dir_path , bucket_path , data ) ;
719
+ async create_bucket_config_file ( bucket_data ) {
720
+ const bucket_string_data = this . _prepare_for_bucket_schema ( bucket_data ) ;
721
+ const bucket_path = this . get_bucket_path_by_name ( bucket_data . name ) ;
722
+ await native_fs_utils . create_config_file ( this . fs_context , this . buckets_dir_path , bucket_path , bucket_string_data ) ;
723
+ return bucket_string_data ;
719
724
}
720
725
721
726
/**
722
- * update_bucket_config_file updates bucket config file
723
- * @param {string } bucket_name
724
- * @param {* } data
725
- * @returns {Promise<void> }
727
+ * _prepare_for_bucket_schema takes bucket data -
728
+ * 1. removes API bucket properties
729
+ * 2. removes undefined properties, unwrap sensitive_strings and creation_data to string
730
+ * 3. checks bucket schema validation
731
+ * 4. and returns stringified data ready to be written to the config directory
732
+ * @param {Object } bucket_data
733
+ * @returns {String }
726
734
*/
727
- async update_bucket_config_file ( bucket_name , data ) {
728
- const bucket_config_path = this . get_bucket_path_by_name ( bucket_name ) ;
729
- await native_fs_utils . update_config_file ( this . fs_context , this . buckets_dir_path , bucket_config_path , data ) ;
735
+ _prepare_for_bucket_schema ( bucket_data ) {
736
+ const api_bucket_properties_to_remove = [ 'new_name' ] ;
737
+ const bucket_data_api_props_omitted = _ . omit ( bucket_data , api_bucket_properties_to_remove ) ;
738
+ const bucket_string_data = JSON . stringify ( bucket_data_api_props_omitted ) ;
739
+ nsfs_schema_utils . validate_bucket_schema ( JSON . parse ( bucket_string_data ) ) ;
740
+ return bucket_string_data ;
741
+ }
742
+
743
+ /**
744
+ * _prepare_for_account_schema takes account data -
745
+ * 1. encrypts its access keys
746
+ * 2. sets the used master key on the account
747
+ * 3. removes API account properties
748
+ * 4. removes undefined properties, unwrap sensitive_strings and creation_data to string
749
+ * 5. checks accpimt schema validation
750
+ * 6. and returns stringified data ready to be written to the config directory
751
+ * @param {Object } account_data
752
+ * @returns {Promise<String> }
753
+ */
754
+ async _prepare_for_account_schema ( account_data ) {
755
+ const encrypted_account = await nc_mkm . encrypt_access_keys ( account_data ) ;
756
+ const api_account_properties_to_remove = [ 'new_name' , 'new_access_key' ] ;
757
+ const account_data_api_props_omitted = _ . omit ( encrypted_account , api_account_properties_to_remove ) ;
758
+ const account_string_data = JSON . stringify ( account_data_api_props_omitted ) ;
759
+ nsfs_schema_utils . validate_account_schema ( JSON . parse ( account_string_data ) ) ;
760
+ return account_string_data ;
761
+ }
762
+
763
+ /**
764
+ * update_bucket_config_file updates bucket config file
765
+ * @param {Object } bucket_data
766
+ * @returns {Promise<String> }
767
+ */
768
+ async update_bucket_config_file ( bucket_data ) {
769
+ const bucket_string_data = this . _prepare_for_bucket_schema ( bucket_data ) ;
770
+ const bucket_config_path = this . get_bucket_path_by_name ( bucket_data . name ) ;
771
+ await native_fs_utils . update_config_file ( this . fs_context , this . buckets_dir_path , bucket_config_path , bucket_string_data ) ;
772
+ return bucket_string_data ;
730
773
}
731
774
732
775
/**
0 commit comments