Skip to content

Commit

Permalink
NSFS | NC | whitelist cli create config.json when it doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: nadav mizrahi <[email protected]>
  • Loading branch information
nadavMiz committed Feb 19, 2025
1 parent 0669471 commit 361bb76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/unit_tests/test_nc_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});

});
Expand Down

0 comments on commit 361bb76

Please sign in to comment.