Skip to content

Commit c219c61

Browse files
committed
bucket notification - harden bad name scenario. Also, don't reply internal '_' field. (#8796)
Signed-off-by: Amit Prinz Setter <[email protected]>
1 parent 2122c75 commit c219c61

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

src/cmd/manage_nsfs.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -744,40 +744,49 @@ async function notification_management() {
744744
async function connection_management(action, user_input) {
745745
manage_nsfs_validations.validate_connection_args(user_input, action);
746746

747+
//don't reply the internal '_' field.
748+
delete user_input._;
749+
747750
let response = {};
748751
let data;
749752

750-
switch (action) {
751-
case ACTIONS.ADD:
752-
data = await notifications_util.add_connect_file(user_input, config_fs);
753-
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
754-
break;
755-
case ACTIONS.DELETE:
756-
await config_fs.delete_connection_config_file(user_input.name);
757-
response = { code: ManageCLIResponse.ConnectionDeleted };
758-
break;
759-
case ACTIONS.UPDATE:
760-
await notifications_util.update_connect_file(user_input.name, user_input.key,
761-
user_input.value, user_input.remove_key, config_fs);
762-
response = { code: ManageCLIResponse.ConnectionUpdated };
763-
break;
764-
case ACTIONS.STATUS:
765-
data = await new notifications_util.Notificator({
766-
fs_context: config_fs.fs_context,
767-
connect_files_dir: config_fs.connections_dir_path,
768-
nc_config_fs: config_fs,
769-
}).parse_connect_file(user_input.name, user_input.decrypt);
770-
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
771-
break;
772-
case ACTIONS.LIST:
773-
data = await list_connections();
774-
response = { code: ManageCLIResponse.ConnectionList, detail: data };
775-
break;
776-
default:
777-
throw_cli_error(ManageCLIError.InvalidAction);
778-
}
753+
try {
754+
switch (action) {
755+
case ACTIONS.ADD:
756+
data = await notifications_util.add_connect_file(user_input, config_fs);
757+
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
758+
break;
759+
case ACTIONS.DELETE:
760+
await config_fs.delete_connection_config_file(user_input.name);
761+
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
762+
break;
763+
case ACTIONS.UPDATE:
764+
await notifications_util.update_connect_file(user_input.name, user_input.key,
765+
user_input.value, user_input.remove_key, config_fs);
766+
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
767+
break;
768+
case ACTIONS.STATUS:
769+
data = await new notifications_util.Notificator({
770+
fs_context: config_fs.fs_context,
771+
connect_files_dir: config_fs.connections_dir_path,
772+
nc_config_fs: config_fs,
773+
}).parse_connect_file(user_input.name, user_input.decrypt);
774+
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
775+
break;
776+
case ACTIONS.LIST:
777+
data = await list_connections();
778+
response = { code: ManageCLIResponse.ConnectionList, detail: data };
779+
break;
780+
default:
781+
throw_cli_error(ManageCLIError.InvalidAction);
782+
}
779783

780-
write_stdout_response(response.code, response.detail, response.event_arg);
784+
write_stdout_response(response.code, response.detail, response.event_arg);
785+
} catch (err) {
786+
if (err.code === 'EEXIST') throw_cli_error(ManageCLIError.ConnectionAlreadyExists, user_input.name);
787+
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.NoSuchConnection, user_input.name);
788+
throw err;
789+
}
781790
}
782791

783792
exports.main = main;

src/manage_nsfs/manage_nsfs_cli_errors.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,18 @@ ManageCLIError.MissingCliParam = Object.freeze({
500500
http_code: 400,
501501
});
502502

503+
ManageCLIError.ConnectionAlreadyExists = Object.freeze({
504+
code: 'ConnectionAlreadyExists',
505+
message: 'The requested connection name is not available. Please select a different name and try again.',
506+
http_code: 409,
507+
});
508+
509+
ManageCLIError.NoSuchConnection = Object.freeze({
510+
code: 'NoSuchConnection',
511+
message: 'Connection does not exist.',
512+
http_code: 404,
513+
});
514+
503515
///////////////////////////////
504516
// ERRORS MAPPING //
505517
///////////////////////////////

src/test/unit_tests/jest_tests/test_nc_nsfs_connection_cli.test.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// disabling init_rand_seed as it takes longer than the actual test execution
77
process.env.DISABLE_INIT_RANDOM_SEED = "true";
88

9+
const _ = require('lodash');
910
const fs = require('fs');
1011
const path = require('path');
1112
const os_util = require('../../../util/os_utils');
@@ -14,7 +15,10 @@ const { ConfigFS } = require('../../../sdk/config_fs');
1415
const { TMP_PATH, set_nc_config_dir_in_config } = require('../../system_tests/test_utils');
1516
const { TYPES, ACTIONS } = require('../../../manage_nsfs/manage_nsfs_constants');
1617

17-
const tmp_fs_path = path.join(TMP_PATH, 'test_nc_nsfs_account_cli');
18+
const ManageCLIError = require('../../../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;
19+
20+
const tmp_fs_path = path.join(TMP_PATH, 'test_nc_connection_cli.test');
21+
1822
const timeout = 5000;
1923

2024
// eslint-disable-next-line max-lines-per-function
@@ -58,7 +62,9 @@ describe('manage nsfs cli connection flow', () => {
5862
it('cli create connection from cli', async () => {
5963
const conn_options = {...defaults, config_root};
6064
conn_options.name = "fromcli";
61-
await exec_manage_cli(TYPES.CONNECTION, ACTIONS.ADD, conn_options);
65+
const res = await exec_manage_cli(TYPES.CONNECTION, ACTIONS.ADD, conn_options);
66+
const res_json = JSON.parse(res.trim());
67+
expect(_.isEqual(new Set(Object.keys(res_json.response)), new Set(['reply', 'code']))).toBe(true);
6268
const connection = await config_fs.get_connection_by_name(conn_options.name);
6369
assert_connection(connection, conn_options, true);
6470
}, timeout);
@@ -96,6 +102,26 @@ describe('manage nsfs cli connection flow', () => {
96102
assert_connection(res.response.reply, defaults, false);
97103
}, timeout);
98104

105+
it('conn already exists', async () => {
106+
const action = ACTIONS.ADD;
107+
const { name, agent_request_object, request_options_object, notification_protocol } = defaults;
108+
const conn_options = { config_root, name, agent_request_object, request_options_object, notification_protocol };
109+
await exec_manage_cli(TYPES.CONNECTION, action, conn_options);
110+
const actual = await config_fs.get_connection_by_name(name);
111+
assert_connection(actual, defaults, true);
112+
113+
const res = await exec_manage_cli(TYPES.CONNECTION, action, conn_options, true);
114+
const res_json = JSON.parse(res.trim());
115+
expect(res_json.error.code).toBe(ManageCLIError.ConnectionAlreadyExists.code);
116+
});
117+
118+
it('conn does not exist', async () => {
119+
const conn_options = { config_root, name: "badname" };
120+
const res = await exec_manage_cli(TYPES.CONNECTION, ACTIONS.DELETE, conn_options, true);
121+
const res_json = JSON.parse(res.trim());
122+
expect(res_json.error.code).toBe(ManageCLIError.NoSuchConnection.code);
123+
});
124+
99125
});
100126
});
101127

@@ -123,13 +149,17 @@ function assert_connection(connection, connection_options, is_encrypted) {
123149
* @param {string} action
124150
* @param {object} options
125151
*/
126-
async function exec_manage_cli(type, action, options) {
152+
async function exec_manage_cli(type, action, options, expect_failure = false) {
127153
const command = create_command(type, action, options);
128154
let res;
129155
try {
130156
res = await os_util.exec(command, { return_stdout: true });
131157
} catch (e) {
132-
res = e;
158+
if (expect_failure) {
159+
res = e.stdout;
160+
} else {
161+
res = e;
162+
}
133163
}
134164
return res;
135165
}

0 commit comments

Comments
 (0)