Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSFS | NC | whitelist cli create config.json when it doesn't exist #8813

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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