Skip to content

Commit

Permalink
bucket notification - harden bad name scenario. Also, don't reply int…
Browse files Browse the repository at this point in the history
…ernal '_' field.

Signed-off-by: Amit Prinz Setter <[email protected]>
  • Loading branch information
alphaprinz committed Feb 13, 2025
1 parent aef9956 commit b4cc5fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
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

0 comments on commit b4cc5fb

Please sign in to comment.