Skip to content

Commit 361bb76

Browse files
committed
NSFS | NC | whitelist cli create config.json when it doesn't exist
Signed-off-by: nadav mizrahi <[email protected]>
1 parent 0669471 commit 361bb76

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/cmd/manage_nsfs.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,21 +718,34 @@ async function set_bucker_owner(bucket_data) {
718718
//// IP WHITELIST //
719719
////////////////////
720720

721+
async function _get_config_json_if_exist() {
722+
try {
723+
return await config_fs.get_config_json();
724+
} catch (err) {
725+
if (err.code !== 'ENOENT') throw err;
726+
return {};
727+
}
728+
}
729+
721730
async function whitelist_ips_management(args) {
722731
const ips = args.ips;
723732
manage_nsfs_validations.validate_whitelist_arg(ips);
724733

725734
const whitelist_ips = JSON.parse(ips);
726735
manage_nsfs_validations.validate_whitelist_ips(whitelist_ips);
727-
const config_path = path.join(config_fs.config_root, 'config.json');
728736
try {
729-
const config_data = require(config_path);
737+
const config_data = await _get_config_json_if_exist();
738+
const config_created = !_.isEmpty(config_data);
730739
config_data.S3_SERVER_IP_WHITELIST = whitelist_ips;
731740
const data = JSON.stringify(config_data);
732-
await config_fs.update_config_json_file(data);
741+
if (config_created) {
742+
await config_fs.update_config_json_file(data);
743+
} else {
744+
await config_fs.create_config_json_file(data);
745+
}
733746
} catch (err) {
734-
dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, path ' + config_path, err);
735-
throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed, config_path);
747+
dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, err');
748+
throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed);
736749
}
737750
write_stdout_response(ManageCLIResponse.WhiteListIPUpdated, ips);
738751
}

src/test/unit_tests/test_nc_cli.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ const DEFAULT_FS_CONFIG = get_process_fs_context();
2323
const config_fs_account_options = { show_secrets: true, decrypt_secret_key: true };
2424
const root_path = path.join(tmp_fs_path, 'root_path_manage_nsfs/');
2525
const config_root = path.join(tmp_fs_path, 'config_root_manage_nsfs');
26+
const config_root_empty = path.join(tmp_fs_path, 'config_root_manage_nsfs_empty');
2627
const config_fs = new ConfigFS(config_root);
2728
// TODO: needed for NC_CORETEST FLOW - should be handled better
2829
const nc_coretest_master_key_location = config.NC_MASTER_KEYS_FILE_LOCATION;
2930

3031
set_nc_config_dir_in_config(config_root);
32+
set_nc_config_dir_in_config(config_root_empty);
3133

3234
mocha.describe('manage_nsfs cli', function() {
3335

@@ -37,6 +39,7 @@ mocha.describe('manage_nsfs cli', function() {
3739
});
3840
mocha.after(async () => {
3941
await fs_utils.folder_delete(`${config_root}`);
42+
await fs_utils.folder_delete(`${config_root_empty}`);
4043
await fs_utils.folder_delete(`${root_path}`);
4144
await fs_utils.file_delete(path.join(config_root, 'master_keys.json'));
4245
config.NC_MASTER_KEYS_FILE_LOCATION = nc_coretest_master_key_location;
@@ -1055,6 +1058,16 @@ mocha.describe('manage_nsfs cli', function() {
10551058
assert_error(err, ManageCLIError.InvalidArgument);
10561059
}
10571060
});
1061+
1062+
mocha.it('cli add whitelist config file doesnt exist', async function() {
1063+
const new_config_options = {};
1064+
const ips = ['127.0.0.1']; // IPV4 format
1065+
const res = await exec_manage_cli(type, '', { config_root: config_root_empty, ips: JSON.stringify(ips) });
1066+
new_config_options.S3_SERVER_IP_WHITELIST = ips;
1067+
const config_data = await config_fs.get_config_data(path.join(config_root_empty, 'config.json'));
1068+
await assert_response('', type, res, ips);
1069+
assert_whitelist(config_data, new_config_options);
1070+
});
10581071
});
10591072

10601073
});

0 commit comments

Comments
 (0)