Skip to content

Commit fa59a2b

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

File tree

3 files changed

+84
-34
lines changed

3 files changed

+84
-34
lines changed

src/cmd/manage_nsfs.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -758,40 +758,48 @@ async function notification_management() {
758758
async function connection_management(action, user_input) {
759759
manage_nsfs_validations.validate_connection_args(user_input, action);
760760

761+
//don't reply the internal '_' field.
762+
delete user_input._;
763+
761764
let response = {};
762765
let data;
763766

764-
switch (action) {
765-
case ACTIONS.ADD:
766-
data = await notifications_util.add_connect_file(user_input, config_fs);
767-
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
768-
break;
769-
case ACTIONS.DELETE:
770-
await config_fs.delete_connection_config_file(user_input.name);
771-
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
772-
break;
773-
case ACTIONS.UPDATE:
774-
await notifications_util.update_connect_file(user_input.name, user_input.key,
775-
user_input.value, user_input.remove_key, config_fs);
776-
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
777-
break;
778-
case ACTIONS.STATUS:
779-
data = await new notifications_util.Notificator({
780-
fs_context: config_fs.fs_context,
781-
connect_files_dir: config_fs.connections_dir_path,
782-
nc_config_fs: config_fs,
783-
}).parse_connect_file(user_input.name, user_input.decrypt);
784-
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
785-
break;
786-
case ACTIONS.LIST:
787-
data = await list_connections();
788-
response = { code: ManageCLIResponse.ConnectionList, detail: data };
789-
break;
790-
default:
791-
throw_cli_error(ManageCLIError.InvalidAction);
767+
try {
768+
switch (action) {
769+
case ACTIONS.ADD:
770+
data = await notifications_util.add_connect_file(user_input, config_fs);
771+
response = { code: ManageCLIResponse.ConnectionCreated, detail: data };
772+
break;
773+
case ACTIONS.DELETE:
774+
await config_fs.delete_connection_config_file(user_input.name);
775+
response = { code: ManageCLIResponse.ConnectionDeleted, detail: {name: user_input.name} };
776+
break;
777+
case ACTIONS.UPDATE:
778+
await notifications_util.update_connect_file(user_input.name, user_input.key,
779+
user_input.value, user_input.remove_key, config_fs);
780+
response = { code: ManageCLIResponse.ConnectionUpdated, detail: {name: user_input.name} };
781+
break;
782+
case ACTIONS.STATUS:
783+
data = await new notifications_util.Notificator({
784+
fs_context: config_fs.fs_context,
785+
connect_files_dir: config_fs.connections_dir_path,
786+
nc_config_fs: config_fs,
787+
}).parse_connect_file(user_input.name, user_input.decrypt);
788+
response = { code: ManageCLIResponse.ConnectionStatus, detail: data };
789+
break;
790+
case ACTIONS.LIST:
791+
data = await list_connections();
792+
response = { code: ManageCLIResponse.ConnectionList, detail: data };
793+
break;
794+
default:
795+
throw_cli_error(ManageCLIError.InvalidAction);
796+
}
797+
write_stdout_response(response.code, response.detail, response.event_arg);
798+
} catch (err) {
799+
if (err.code === 'EEXIST') throw_cli_error(ManageCLIError.ConnectionAlreadyExists, user_input.name);
800+
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.NoSuchConnection, user_input.name);
801+
throw err;
792802
}
793-
794-
write_stdout_response(response.code, response.detail, response.event_arg);
795803
}
796804

797805
////////////////////

src/manage_nsfs/manage_nsfs_cli_errors.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ ManageCLIError.LifecycleWorkerReachedTimeout = Object.freeze({
528528
http_code: 400,
529529
});
530530

531+
ManageCLIError.ConnectionAlreadyExists = Object.freeze({
532+
code: 'ConnectionAlreadyExists',
533+
message: 'The requested connection name is not available. Please select a different name and try again.',
534+
http_code: 409,
535+
});
536+
537+
ManageCLIError.NoSuchConnection = Object.freeze({
538+
code: 'NoSuchConnection',
539+
message: 'Connection does not exist.',
540+
http_code: 404,
541+
});
542+
531543
///////////////////////////////
532544
// ERRORS MAPPING //
533545
///////////////////////////////

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)