Skip to content

Commit bf6a967

Browse files
authored
Merge pull request #8813 from nadavMiz/whitelist-empty-config
NSFS | NC | whitelist cli create config.json when it doesn't exist
2 parents 1e32fa3 + 6f86439 commit bf6a967

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/cmd/manage_nsfs.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const dbg = require('../util/debug_module')(__filename);
55
const _ = require('lodash');
6-
const path = require('path');
76
const minimist = require('minimist');
87
const config = require('../../config');
98
const P = require('../util/promise');
@@ -717,22 +716,38 @@ async function set_bucker_owner(bucket_data) {
717716
////////////////////
718717
//// IP WHITELIST //
719718
////////////////////
719+
/**
720+
* @returns {Promise<[boolean, object]>} - [config_exists, config_data] - config_exists is a boolean that indicates if the config.json exists
721+
* and config_data is the parsed data from the config.json file. returns empty object for config_data if config.json does not exist.
722+
*/
723+
async function _get_config_json_if_exist() {
724+
try {
725+
const config_json = await config_fs.get_config_json();
726+
return [true, config_json];
727+
} catch (err) {
728+
if (err.code !== 'ENOENT') throw err;
729+
return [false, {}];
730+
}
731+
}
720732

721733
async function whitelist_ips_management(args) {
722734
const ips = args.ips;
723735
manage_nsfs_validations.validate_whitelist_arg(ips);
724736

725737
const whitelist_ips = JSON.parse(ips);
726738
manage_nsfs_validations.validate_whitelist_ips(whitelist_ips);
727-
const config_path = path.join(config_fs.config_root, 'config.json');
728739
try {
729-
const config_data = require(config_path);
740+
const [config_exists, config_data] = await _get_config_json_if_exist();
730741
config_data.S3_SERVER_IP_WHITELIST = whitelist_ips;
731742
const data = JSON.stringify(config_data);
732-
await config_fs.update_config_json_file(data);
743+
if (config_exists) {
744+
await config_fs.update_config_json_file(data);
745+
} else {
746+
await config_fs.create_config_json_file(data);
747+
}
733748
} 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);
749+
dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, path:', config_fs.config_json_path, err);
750+
throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed);
736751
}
737752
write_stdout_response(ManageCLIResponse.WhiteListIPUpdated, ips);
738753
}

src/test/unit_tests/test_nc_cli.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,21 @@ mocha.describe('manage_nsfs cli', function() {
10551055
assert_error(err, ManageCLIError.InvalidArgument);
10561056
}
10571057
});
1058+
1059+
mocha.it('cli add whitelist config file doesnt exist', async function() {
1060+
//delete existing config file
1061+
const config_file_path = config_fs.get_config_json_path();
1062+
await fs_utils.file_delete(config_file_path);
1063+
1064+
const ips = ['127.0.0.1']; // IPV4 format
1065+
const res = await exec_manage_cli(type, '', { config_root, ips: JSON.stringify(ips) });
1066+
await assert_response('', type, res, ips);
1067+
const new_config_options = { S3_SERVER_IP_WHITELIST: ips};
1068+
const config_data = await config_fs.get_config_json();
1069+
console.log(config_data);
1070+
assert_whitelist(config_data, new_config_options);
1071+
});
1072+
10581073
});
10591074

10601075
});

0 commit comments

Comments
 (0)