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

bucket notification - harden bad name scenario. Also, don't reply internal '_' field. #8796

Merged
merged 1 commit into from
Feb 17, 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
69 changes: 39 additions & 30 deletions src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,40 +781,49 @@ async function notification_management() {
async function connection_management(action, user_input) {
manage_nsfs_validations.validate_connection_args(user_input, action);

//don't reply the internal '_' field.
delete user_input._;

let response = {};
let data;

switch (action) {
case ACTIONS.ADD:
data = await notifications_util.add_connect_file(user_input, config_fs);
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
break;
case ACTIONS.DELETE:
await config_fs.delete_connection_config_file(user_input.name);
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
break;
case ACTIONS.UPDATE:
await notifications_util.update_connect_file(user_input.name, user_input.key,
user_input.value, user_input.remove_key, config_fs);
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
break;
case ACTIONS.STATUS:
data = await new notifications_util.Notificator({
fs_context: config_fs.fs_context,
connect_files_dir: config_fs.connections_dir_path,
nc_config_fs: config_fs,
}).parse_connect_file(user_input.name, user_input.decrypt);
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
break;
case ACTIONS.LIST:
data = await list_connections();
response = { code: ManageCLIResponse.ConnectionList, detail: data };
break;
default:
throw_cli_error(ManageCLIError.InvalidAction);
}
try {
switch (action) {
case ACTIONS.ADD:
data = await notifications_util.add_connect_file(user_input, config_fs);
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
break;
case ACTIONS.DELETE:
await config_fs.delete_connection_config_file(user_input.name);
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
break;
case ACTIONS.UPDATE:
await notifications_util.update_connect_file(user_input.name, user_input.key,
user_input.value, user_input.remove_key, config_fs);
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
break;
case ACTIONS.STATUS:
data = await new notifications_util.Notificator({
fs_context: config_fs.fs_context,
connect_files_dir: config_fs.connections_dir_path,
nc_config_fs: config_fs,
}).parse_connect_file(user_input.name, user_input.decrypt);
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
break;
case ACTIONS.LIST:
data = await list_connections();
response = { code: ManageCLIResponse.ConnectionList, detail: data };
break;
default:
throw_cli_error(ManageCLIError.InvalidAction);
}

write_stdout_response(response.code, response.detail, response.event_arg);
write_stdout_response(response.code, response.detail, response.event_arg);
} catch (err) {
if (err.code === 'EEXIST') throw_cli_error(ManageCLIError.ConnectionAlreadyExists, user_input.name);
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.NoSuchConnection, user_input.name);
throw err;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/manage_nsfs/manage_nsfs_cli_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,18 @@ ManageCLIError.MissingCliParam = Object.freeze({
http_code: 400,
});

ManageCLIError.ConnectionAlreadyExists = Object.freeze({
code: 'ConnectionAlreadyExists',
message: 'The requested connection name is not available. Please select a different name and try again.',
http_code: 409,
});

ManageCLIError.NoSuchConnection = Object.freeze({
code: 'NoSuchConnection',
message: 'Connection does not exist.',
http_code: 404,
});

///////////////////////////////
// ERRORS MAPPING //
///////////////////////////////
Expand Down
35 changes: 32 additions & 3 deletions src/test/unit_tests/jest_tests/test_nc_connection_cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// disabling init_rand_seed as it takes longer than the actual test execution
process.env.DISABLE_INIT_RANDOM_SEED = "true";

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const os_util = require('../../../util/os_utils');
Expand All @@ -14,6 +15,8 @@ const { ConfigFS } = require('../../../sdk/config_fs');
const { TMP_PATH, set_nc_config_dir_in_config } = require('../../system_tests/test_utils');
const { TYPES, ACTIONS } = require('../../../manage_nsfs/manage_nsfs_constants');

const ManageCLIError = require('../../../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;

const tmp_fs_path = path.join(TMP_PATH, 'test_nc_connection_cli.test');
const timeout = 5000;

Expand Down Expand Up @@ -58,7 +61,9 @@ describe('manage nsfs cli connection flow', () => {
it('cli create connection from cli', async () => {
const conn_options = {...defaults, config_root};
conn_options.name = "fromcli";
await exec_manage_cli(TYPES.CONNECTION, ACTIONS.ADD, conn_options);
const res = await exec_manage_cli(TYPES.CONNECTION, ACTIONS.ADD, conn_options);
const res_json = JSON.parse(res.trim());
expect(_.isEqual(new Set(Object.keys(res_json.response)), new Set(['reply', 'code', 'message']))).toBe(true);
const connection = await config_fs.get_connection_by_name(conn_options.name);
assert_connection(connection, conn_options, true);
}, timeout);
Expand Down Expand Up @@ -96,6 +101,26 @@ describe('manage nsfs cli connection flow', () => {
assert_connection(res.response.reply, defaults, false);
}, timeout);

it('conn already exists', async () => {
const action = ACTIONS.ADD;
const { name, agent_request_object, request_options_object, notification_protocol } = defaults;
const conn_options = { config_root, name, agent_request_object, request_options_object, notification_protocol };
await exec_manage_cli(TYPES.CONNECTION, action, conn_options);
const actual = await config_fs.get_connection_by_name(name);
assert_connection(actual, defaults, true);

const res = await exec_manage_cli(TYPES.CONNECTION, action, conn_options, true);
const res_json = JSON.parse(res.trim());
expect(res_json.error.code).toBe(ManageCLIError.ConnectionAlreadyExists.code);
});

it('conn does not exist', async () => {
const conn_options = { config_root, name: "badname" };
const res = await exec_manage_cli(TYPES.CONNECTION, ACTIONS.DELETE, conn_options, true);
const res_json = JSON.parse(res.trim());
expect(res_json.error.code).toBe(ManageCLIError.NoSuchConnection.code);
});

});
});

Expand Down Expand Up @@ -123,13 +148,17 @@ function assert_connection(connection, connection_options, is_encrypted) {
* @param {string} action
* @param {object} options
*/
async function exec_manage_cli(type, action, options) {
async function exec_manage_cli(type, action, options, expect_failure = false) {
const command = create_command(type, action, options);
let res;
try {
res = await os_util.exec(command, { return_stdout: true });
} catch (e) {
res = e;
if (expect_failure) {
res = e.stdout;
} else {
res = e;
}
}
return res;
}
Expand Down