Skip to content

Commit 1bbd42e

Browse files
authored
bucket notification - harden bad name scenario. Also, don't reply internal '_' field. (#8796)
Signed-off-by: Amit Prinz Setter <alphaprinz@gmail.com>
1 parent 17e9e86 commit 1bbd42e

File tree

3 files changed

+83
-33
lines changed

3 files changed

+83
-33
lines changed

src/cmd/manage_nsfs.js

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

784+
//don't reply the internal '_' field.
785+
delete user_input._;
786+
784787
let response = {};
785788
let data;
786789

787-
switch (action) {
788-
case ACTIONS.ADD:
789-
data = await notifications_util.add_connect_file(user_input, config_fs);
790-
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
791-
break;
792-
case ACTIONS.DELETE:
793-
await config_fs.delete_connection_config_file(user_input.name);
794-
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
795-
break;
796-
case ACTIONS.UPDATE:
797-
await notifications_util.update_connect_file(user_input.name, user_input.key,
798-
user_input.value, user_input.remove_key, config_fs);
799-
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
800-
break;
801-
case ACTIONS.STATUS:
802-
data = await new notifications_util.Notificator({
803-
fs_context: config_fs.fs_context,
804-
connect_files_dir: config_fs.connections_dir_path,
805-
nc_config_fs: config_fs,
806-
}).parse_connect_file(user_input.name, user_input.decrypt);
807-
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
808-
break;
809-
case ACTIONS.LIST:
810-
data = await list_connections();
811-
response = { code: ManageCLIResponse.ConnectionList, detail: data };
812-
break;
813-
default:
814-
throw_cli_error(ManageCLIError.InvalidAction);
815-
}
790+
try {
791+
switch (action) {
792+
case ACTIONS.ADD:
793+
data = await notifications_util.add_connect_file(user_input, config_fs);
794+
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
795+
break;
796+
case ACTIONS.DELETE:
797+
await config_fs.delete_connection_config_file(user_input.name);
798+
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
799+
break;
800+
case ACTIONS.UPDATE:
801+
await notifications_util.update_connect_file(user_input.name, user_input.key,
802+
user_input.value, user_input.remove_key, config_fs);
803+
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
804+
break;
805+
case ACTIONS.STATUS:
806+
data = await new notifications_util.Notificator({
807+
fs_context: config_fs.fs_context,
808+
connect_files_dir: config_fs.connections_dir_path,
809+
nc_config_fs: config_fs,
810+
}).parse_connect_file(user_input.name, user_input.decrypt);
811+
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
812+
break;
813+
case ACTIONS.LIST:
814+
data = await list_connections();
815+
response = { code: ManageCLIResponse.ConnectionList, detail: data };
816+
break;
817+
default:
818+
throw_cli_error(ManageCLIError.InvalidAction);
819+
}
816820

817-
write_stdout_response(response.code, response.detail, response.event_arg);
821+
write_stdout_response(response.code, response.detail, response.event_arg);
822+
} catch (err) {
823+
if (err.code === 'EEXIST') throw_cli_error(ManageCLIError.ConnectionAlreadyExists, user_input.name);
824+
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.NoSuchConnection, user_input.name);
825+
throw err;
826+
}
818827
}
819828

820829
/**

src/manage_nsfs/manage_nsfs_cli_errors.js

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

509+
ManageCLIError.ConnectionAlreadyExists = Object.freeze({
510+
code: 'ConnectionAlreadyExists',
511+
message: 'The requested connection name is not available. Please select a different name and try again.',
512+
http_code: 409,
513+
});
514+
515+
ManageCLIError.NoSuchConnection = Object.freeze({
516+
code: 'NoSuchConnection',
517+
message: 'Connection does not exist.',
518+
http_code: 404,
519+
});
520+
509521
///////////////////////////////
510522
// ERRORS MAPPING //
511523
///////////////////////////////

src/test/unit_tests/jest_tests/test_nc_connection_cli.test.js

Lines changed: 32 additions & 3 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,6 +15,8 @@ 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

18+
const ManageCLIError = require('../../../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;
19+
1720
const tmp_fs_path = path.join(TMP_PATH, 'test_nc_connection_cli.test');
1821
const timeout = 5000;
1922

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

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

@@ -123,13 +148,17 @@ function assert_connection(connection, connection_options, is_encrypted) {
123148
* @param {string} action
124149
* @param {object} options
125150
*/
126-
async function exec_manage_cli(type, action, options) {
151+
async function exec_manage_cli(type, action, options, expect_failure = false) {
127152
const command = create_command(type, action, options);
128153
let res;
129154
try {
130155
res = await os_util.exec(command, { return_stdout: true });
131156
} catch (e) {
132-
res = e;
157+
if (expect_failure) {
158+
res = e.stdout;
159+
} else {
160+
res = e;
161+
}
133162
}
134163
return res;
135164
}

0 commit comments

Comments
 (0)