@@ -478,19 +478,26 @@ function get_new_buckets_path_by_test_env(new_buckets_full_path, new_buckets_dir
478
478
/**
479
479
* write_manual_config_file writes config file directly to the file system without using config FS
480
480
* used for creating backward compatibility tests, invalid config files etc
481
+ * 1. if it's account -
482
+ * 1.1. create identity directory /{config_dir_path}/identities/{id}/
483
+ * 2. create the config file -
484
+ * 2.1. if it's a bucket - create it in /{config_dir_path}/buckets/{bucket_name}.json
485
+ * 2.2. if it's an account - create it in /{config_dir_path}/identities/{id}/identity.json
486
+ * 3. if it's an account and symlink_name is true - create /{config_dir_path}/accounts_by_name/{account_name}.symlink -> /{config_dir_path}/identities/{id}/identity.json
487
+ * 4. if it's an account and symlink_access_key is true and there is access key in the account config - create /{config_dir_path}/access_keys/{access_key}.symlink -> /{config_dir_path}/identities/{id}/identity.json
481
488
* @param {String } type
482
489
* @param {import('../../sdk/config_fs').ConfigFS } config_fs
483
490
* @param {Object } config_data
484
491
* @param {String } [invalid_str]
492
+ * @param {{symlink_name?: Boolean, symlink_access_key?: Boolean} } [options]
485
493
* @returns {Promise<Void> }
486
494
*/
487
- async function write_manual_config_file ( type , config_fs , config_data , invalid_str = '' ) {
495
+ async function write_manual_config_file ( type , config_fs , config_data , invalid_str = '' , { symlink_name , symlink_access_key } = { symlink_name : true , symlink_access_key : true } ) {
488
496
const config_path = type === CONFIG_TYPES . BUCKET ?
489
497
config_fs . get_bucket_path_by_name ( config_data . name ) :
490
498
config_fs . get_identity_path_by_id ( config_data . _id ) ;
491
499
if ( type === CONFIG_TYPES . ACCOUNT ) {
492
- const dir_path = config_fs . get_identity_dir_path_by_id ( config_data . _id ) ;
493
- await nb_native ( ) . fs . mkdir ( config_fs . fs_context , dir_path , native_fs_utils . get_umasked_mode ( config . BASE_MODE_DIR ) ) ;
500
+ await create_identity_dir_if_missing ( config_fs , config_data . _id ) ;
494
501
}
495
502
await nb_native ( ) . fs . writeFile (
496
503
config_fs . fs_context ,
@@ -500,22 +507,69 @@ async function write_manual_config_file(type, config_fs, config_data, invalid_st
500
507
mode : native_fs_utils . get_umasked_mode ( config . BASE_MODE_FILE )
501
508
}
502
509
) ;
510
+ const id_relative_path = config_fs . get_account_relative_path_by_id ( config_data . _id ) ;
503
511
504
- if ( type === CONFIG_TYPES . ACCOUNT ) {
505
- const id_relative_path = config_fs . get_account_relative_path_by_id ( config_data . _id ) ;
506
- const name_symlink_path = config_fs . get_account_or_user_path_by_name ( config_data . name ) ;
507
- await nb_native ( ) . fs . symlink ( config_fs . fs_context , id_relative_path , name_symlink_path ) ;
512
+ if ( type === CONFIG_TYPES . ACCOUNT && symlink_name ) {
513
+ await symlink_account_name ( config_fs , config_data . name , id_relative_path ) ;
514
+ }
515
+
516
+ if ( type === CONFIG_TYPES . ACCOUNT && symlink_access_key && config_data . access_keys &&
517
+ Object . keys ( config_data . access_keys ) . length > 0 ) {
518
+ await symlink_account_access_keys ( config_fs , config_data . access_keys , id_relative_path ) ;
519
+ }
520
+ }
521
+
522
+ /**
523
+ * symlink_account_name symlinks the account's name path to the target link path
524
+ * used for manual creation of the account name symlink
525
+ * @param {import('../../sdk/config_fs').ConfigFS } config_fs
526
+ * @param {String } account_name
527
+ * @param {String } link_target
528
+ */
529
+ async function symlink_account_name ( config_fs , account_name , link_target ) {
530
+ const name_symlink_path = config_fs . get_account_or_user_path_by_name ( account_name ) ;
531
+ await nb_native ( ) . fs . symlink ( config_fs . fs_context , link_target , name_symlink_path ) ;
532
+ }
533
+
534
+ /**
535
+ * symlink_account_access_keys symlinks the account's access key path to the target link path
536
+ * used for manual creation of the account access key symlink
537
+ * @param {import('../../sdk/config_fs').ConfigFS } config_fs
538
+ * @param {Object } access_keys
539
+ * @param {String } link_target
540
+ */
541
+ async function symlink_account_access_keys ( config_fs , access_keys , link_target ) {
542
+ for ( const { access_key } of access_keys ) {
543
+ const access_key_symlink_path = config_fs . get_account_or_user_path_by_access_key ( access_key ) ;
544
+ await nb_native ( ) . fs . symlink ( config_fs . fs_context , link_target , access_key_symlink_path ) ;
508
545
}
509
546
}
510
547
548
+ /**
549
+ * create_identity_dir_if_missing created the identity directory if missing
550
+ * @param {import('../../sdk/config_fs').ConfigFS } config_fs
551
+ * @param {String } _id
552
+ * @returns {Promise<Void> }
553
+ */
554
+ async function create_identity_dir_if_missing ( config_fs , _id ) {
555
+ const dir_path = config_fs . get_identity_dir_path_by_id ( _id ) ;
556
+ try {
557
+ await nb_native ( ) . fs . mkdir ( config_fs . fs_context , dir_path , native_fs_utils . get_umasked_mode ( config . BASE_MODE_DIR ) ) ;
558
+ } catch ( err ) {
559
+ if ( err . code !== 'ENOENT' ) throw err ;
560
+ }
561
+ }
511
562
512
563
/**
513
564
* write_manual_old_account_config_file writes account config file directly to the old file system account path without using config FS
565
+ * 1. create old json file in /config_dir_path/accounts/account.json
566
+ * 2. if symlink_access_key is true - create old access key symlink /config_dir_path/access_keys/{access_key}.symlink -> /config_dir_path/accounts/account.json
514
567
* @param {import('../../sdk/config_fs').ConfigFS } config_fs
515
568
* @param {Object } config_data
569
+ * @param {{symlink_access_key?: Boolean} } [options]
516
570
* @returns {Promise<Void> }
517
571
*/
518
- async function write_manual_old_account_config_file ( config_fs , config_data ) {
572
+ async function write_manual_old_account_config_file ( config_fs , config_data , { symlink_access_key } = { symlink_access_key : false } ) {
519
573
const config_path = config_fs . _get_old_account_path_by_name ( config_data . name ) ;
520
574
await nb_native ( ) . fs . writeFile (
521
575
config_fs . fs_context ,
@@ -525,6 +579,12 @@ async function write_manual_old_account_config_file(config_fs, config_data) {
525
579
mode : native_fs_utils . get_umasked_mode ( config . BASE_MODE_FILE )
526
580
}
527
581
) ;
582
+
583
+ const account_name_relative_path = config_fs . get_old_account_relative_path_by_name ( config_data . name ) ;
584
+ if ( symlink_access_key ) {
585
+ const access_key_symlink_path = config_fs . get_account_or_user_path_by_access_key ( config_data . access_keys [ 0 ] . access_key ) ;
586
+ await nb_native ( ) . fs . symlink ( config_fs . fs_context , account_name_relative_path , access_key_symlink_path ) ;
587
+ }
528
588
}
529
589
530
590
/**
@@ -556,9 +616,9 @@ async function delete_manual_config_file(type, config_fs, config_data) {
556
616
557
617
/**
558
618
* @param {any } test_name
559
- * @param {Object } [config_fs]
619
+ * @param {import('../../sdk/config_fs').ConfigFS } [config_fs]
560
620
*/
561
- async function fail_test_if_default_config_dir_exists ( test_name , config_fs = { } ) {
621
+ async function fail_test_if_default_config_dir_exists ( test_name , config_fs ) {
562
622
const fs_context = config_fs ?. fs_context || native_fs_utils . get_process_fs_context ( ) ;
563
623
const config_dir_exists = await native_fs_utils . is_path_exists ( fs_context , config . NSFS_NC_DEFAULT_CONF_DIR ) ;
564
624
const msg = `${ test_name } found an existing default config directory ${ config . NSFS_NC_DEFAULT_CONF_DIR } ,` +
@@ -583,6 +643,10 @@ async function create_config_dir(config_dir) {
583
643
584
644
/**
585
645
* clean_config_dir cleans the config directory
646
+ * custom_config_dir_path is created in some tests that are not using the default /etc/noobaa.conf.d/
647
+ * config directory path
648
+ * @param {import('../../sdk/config_fs').ConfigFS } config_fs
649
+ * @param {String } [custom_config_dir_path]
586
650
* @returns {Promise<Void> }
587
651
*/
588
652
async function clean_config_dir ( config_fs , custom_config_dir_path ) {
@@ -593,17 +657,35 @@ async function clean_config_dir(config_fs, custom_config_dir_path) {
593
657
const system_json = '/system.json' ;
594
658
for ( const dir of [ buckets_dir_name , identities_dir_name , access_keys_dir_name , accounts_by_name , config . NSFS_TEMP_CONF_DIR_NAME ] ) {
595
659
const default_path = path . join ( config . NSFS_NC_DEFAULT_CONF_DIR , dir ) ;
596
- await fs_utils . folder_delete ( default_path ) ;
597
- const custom_path = path . join ( custom_config_dir_path , dir ) ;
598
- await fs_utils . folder_delete ( custom_path ) ;
599
-
660
+ await fs_utils . folder_delete_skip_enoent ( default_path ) ;
661
+ if ( custom_config_dir_path ) {
662
+ const custom_path = path . join ( custom_config_dir_path , dir ) ;
663
+ await fs_utils . folder_delete_skip_enoent ( custom_path ) ;
664
+ }
600
665
}
666
+
601
667
await delete_redirect_file ( config_fs ) ;
602
668
await fs_utils . file_delete ( system_json ) ;
603
- await fs_utils . folder_delete ( config . NSFS_NC_DEFAULT_CONF_DIR ) ;
604
- await fs_utils . folder_delete ( custom_config_dir_path ) ;
669
+ await fs_utils . folder_delete_skip_enoent ( config . NSFS_NC_DEFAULT_CONF_DIR ) ;
670
+ await fs_utils . folder_delete_skip_enoent ( custom_config_dir_path ) ;
605
671
}
606
672
673
+ /**
674
+ * create_file creates a file in the file system
675
+ * @param {nb.NativeFSContext } fs_context
676
+ * @param {String } file_path
677
+ * @param {Object } file_data
678
+ */
679
+ async function create_file ( fs_context , file_path , file_data ) {
680
+ await nb_native ( ) . fs . writeFile (
681
+ fs_context ,
682
+ file_path ,
683
+ Buffer . from ( JSON . stringify ( file_data ) ) ,
684
+ {
685
+ mode : native_fs_utils . get_umasked_mode ( config . BASE_MODE_FILE )
686
+ }
687
+ ) ;
688
+ }
607
689
608
690
exports . blocks_exist_on_cloud = blocks_exist_on_cloud ;
609
691
exports . create_hosts_pool = create_hosts_pool ;
@@ -629,6 +711,10 @@ exports.get_new_buckets_path_by_test_env = get_new_buckets_path_by_test_env;
629
711
exports . write_manual_config_file = write_manual_config_file ;
630
712
exports . write_manual_old_account_config_file = write_manual_old_account_config_file ;
631
713
exports . delete_manual_config_file = delete_manual_config_file ;
714
+ exports . create_identity_dir_if_missing = create_identity_dir_if_missing ;
715
+ exports . symlink_account_name = symlink_account_name ;
716
+ exports . symlink_account_access_keys = symlink_account_access_keys ;
717
+ exports . create_file = create_file ;
632
718
exports . create_redirect_file = create_redirect_file ;
633
719
exports . delete_redirect_file = delete_redirect_file ;
634
720
exports . fail_test_if_default_config_dir_exists = fail_test_if_default_config_dir_exists ;
0 commit comments