From 361bb769d424323b0a0efc1501e2755e5132dcc4 Mon Sep 17 00:00:00 2001 From: nadav mizrahi Date: Wed, 19 Feb 2025 17:59:06 +0200 Subject: [PATCH] NSFS | NC | whitelist cli create config.json when it doesn't exist Signed-off-by: nadav mizrahi --- src/cmd/manage_nsfs.js | 23 ++++++++++++++++++----- src/test/unit_tests/test_nc_cli.js | 13 +++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/cmd/manage_nsfs.js b/src/cmd/manage_nsfs.js index ba82b8231b..1d84dbbc4a 100644 --- a/src/cmd/manage_nsfs.js +++ b/src/cmd/manage_nsfs.js @@ -718,21 +718,34 @@ async function set_bucker_owner(bucket_data) { //// IP WHITELIST // //////////////////// +async function _get_config_json_if_exist() { + try { + return await config_fs.get_config_json(); + } catch (err) { + if (err.code !== 'ENOENT') throw err; + return {}; + } +} + async function whitelist_ips_management(args) { const ips = args.ips; manage_nsfs_validations.validate_whitelist_arg(ips); const whitelist_ips = JSON.parse(ips); manage_nsfs_validations.validate_whitelist_ips(whitelist_ips); - const config_path = path.join(config_fs.config_root, 'config.json'); try { - const config_data = require(config_path); + const config_data = await _get_config_json_if_exist(); + const config_created = !_.isEmpty(config_data); config_data.S3_SERVER_IP_WHITELIST = whitelist_ips; const data = JSON.stringify(config_data); - await config_fs.update_config_json_file(data); + if (config_created) { + await config_fs.update_config_json_file(data); + } else { + await config_fs.create_config_json_file(data); + } } catch (err) { - dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, path ' + config_path, err); - throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed, config_path); + dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, err'); + throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed); } write_stdout_response(ManageCLIResponse.WhiteListIPUpdated, ips); } diff --git a/src/test/unit_tests/test_nc_cli.js b/src/test/unit_tests/test_nc_cli.js index 1b7afc289a..2dbd311df7 100644 --- a/src/test/unit_tests/test_nc_cli.js +++ b/src/test/unit_tests/test_nc_cli.js @@ -23,11 +23,13 @@ const DEFAULT_FS_CONFIG = get_process_fs_context(); const config_fs_account_options = { show_secrets: true, decrypt_secret_key: true }; const root_path = path.join(tmp_fs_path, 'root_path_manage_nsfs/'); const config_root = path.join(tmp_fs_path, 'config_root_manage_nsfs'); +const config_root_empty = path.join(tmp_fs_path, 'config_root_manage_nsfs_empty'); const config_fs = new ConfigFS(config_root); // TODO: needed for NC_CORETEST FLOW - should be handled better const nc_coretest_master_key_location = config.NC_MASTER_KEYS_FILE_LOCATION; set_nc_config_dir_in_config(config_root); +set_nc_config_dir_in_config(config_root_empty); mocha.describe('manage_nsfs cli', function() { @@ -37,6 +39,7 @@ mocha.describe('manage_nsfs cli', function() { }); mocha.after(async () => { await fs_utils.folder_delete(`${config_root}`); + await fs_utils.folder_delete(`${config_root_empty}`); await fs_utils.folder_delete(`${root_path}`); await fs_utils.file_delete(path.join(config_root, 'master_keys.json')); config.NC_MASTER_KEYS_FILE_LOCATION = nc_coretest_master_key_location; @@ -1055,6 +1058,16 @@ mocha.describe('manage_nsfs cli', function() { assert_error(err, ManageCLIError.InvalidArgument); } }); + + mocha.it('cli add whitelist config file doesnt exist', async function() { + const new_config_options = {}; + const ips = ['127.0.0.1']; // IPV4 format + const res = await exec_manage_cli(type, '', { config_root: config_root_empty, ips: JSON.stringify(ips) }); + new_config_options.S3_SERVER_IP_WHITELIST = ips; + const config_data = await config_fs.get_config_data(path.join(config_root_empty, 'config.json')); + await assert_response('', type, res, ips); + assert_whitelist(config_data, new_config_options); + }); }); });