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 26, 2025
1 parent 1e32fa3 commit 6f86439
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

const dbg = require('../util/debug_module')(__filename);
const _ = require('lodash');
const path = require('path');
const minimist = require('minimist');
const config = require('../../config');
const P = require('../util/promise');
Expand Down Expand Up @@ -717,22 +716,38 @@ async function set_bucker_owner(bucket_data) {
////////////////////
//// IP WHITELIST //
////////////////////
/**
* @returns {Promise<[boolean, object]>} - [config_exists, config_data] - config_exists is a boolean that indicates if the config.json exists
* and config_data is the parsed data from the config.json file. returns empty object for config_data if config.json does not exist.
*/
async function _get_config_json_if_exist() {
try {
const config_json = await config_fs.get_config_json();
return [true, config_json];
} catch (err) {
if (err.code !== 'ENOENT') throw err;
return [false, {}];
}
}

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_exists, config_data] = await _get_config_json_if_exist();
config_data.S3_SERVER_IP_WHITELIST = whitelist_ips;
const data = JSON.stringify(config_data);
await config_fs.update_config_json_file(data);
if (config_exists) {
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, path:', config_fs.config_json_path, err);
throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed);
}
write_stdout_response(ManageCLIResponse.WhiteListIPUpdated, ips);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/unit_tests/test_nc_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,21 @@ mocha.describe('manage_nsfs cli', function() {
assert_error(err, ManageCLIError.InvalidArgument);
}
});

mocha.it('cli add whitelist config file doesnt exist', async function() {
//delete existing config file
const config_file_path = config_fs.get_config_json_path();
await fs_utils.file_delete(config_file_path);

const ips = ['127.0.0.1']; // IPV4 format
const res = await exec_manage_cli(type, '', { config_root, ips: JSON.stringify(ips) });
await assert_response('', type, res, ips);
const new_config_options = { S3_SERVER_IP_WHITELIST: ips};
const config_data = await config_fs.get_config_json();
console.log(config_data);
assert_whitelist(config_data, new_config_options);
});

});

});
Expand Down

0 comments on commit 6f86439

Please sign in to comment.