Make ssh objects throw DB::Exception

This commit is contained in:
Gamezardashvili George 2023-06-17 23:15:07 +03:00
parent 5efed6fe7e
commit 9d2576fc85
9 changed files with 110 additions and 33 deletions

View File

@ -1,7 +1,20 @@
#include <stdexcept>
#include <Access/SSH/SSHPublicKey.h>
#include <Common/Exception.h>
#include <Common/SSH/clibssh.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
extern const int LOGICAL_ERROR;
extern const int BAD_ARGUMENTS;
}
}
namespace ssh
{
@ -9,7 +22,7 @@ SSHPublicKey::SSHPublicKey(ssh_key key_, bool own) : key(key_, own ? &deleter :
{ // disable deleter if class is constructed without ownership
if (!key)
{
throw std::logic_error("No ssh_key provided in explicit constructor");
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "No ssh_key provided in explicit constructor");
}
}
@ -19,7 +32,7 @@ SSHPublicKey::SSHPublicKey(const SSHPublicKey & other) : key(ssh_key_dup(other.g
{
if (!key)
{
throw std::runtime_error("Failed to duplicate ssh_key");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to duplicate ssh_key");
}
}
@ -30,7 +43,7 @@ SSHPublicKey & SSHPublicKey::operator=(const SSHPublicKey & other)
ssh_key new_key = ssh_key_dup(other.get());
if (!new_key)
{
throw std::runtime_error("Failed to duplicate ssh_key");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to duplicate ssh_key");
}
key.reset(new_key);
}
@ -63,7 +76,11 @@ SSHPublicKey SSHPublicKey::createFromBase64(const String & base64, const String
int rc = ssh_pki_import_pubkey_base64(base64.c_str(), ssh_key_type_from_name(key_type.c_str()), &key);
if (rc != SSH_OK)
{
throw std::invalid_argument("Bad ssh public key provided");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed importing public key from base64 format.\n\
Key: {}\n\
Type: {}",
base64, key_type
);
}
return SSHPublicKey(key);
}
@ -76,9 +93,12 @@ SSHPublicKey SSHPublicKey::createFromFile(const std::string & filename)
{
if (rc == SSH_EOF)
{
throw std::invalid_argument("Can't import ssh public key from file as it doesn't exist or permission denied");
throw DB::Exception(
DB::ErrorCodes::BAD_ARGUMENTS,
"Can't import ssh public key from file {} as it doesn't exist or permission denied", filename
);
}
throw std::runtime_error("Can't import ssh public key from file");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Can't import ssh public key from file {}", filename);
}
return SSHPublicKey(key);
}
@ -105,7 +125,7 @@ String SSHPublicKey::getBase64Representation() const
if (rc != SSH_OK)
{
throw std::runtime_error("Failed to export public key to base64");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to export public key to base64");
}
// Create a String from cstring, which makes a copy of the first one and requires freeing memory after it

View File

@ -229,9 +229,10 @@ namespace
{
keys.emplace_back(ssh::SSHPublicKey::createFromBase64(base64_key, type));
}
catch (const std::invalid_argument&)
catch (DB::Exception & e)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad ssh key in entry: {}", entry);
e.addMessage(fmt::format("Error while processing {} entry", entry));
throw;
}
}
else

View File

@ -591,7 +591,8 @@
M(999, KEEPER_EXCEPTION) \
M(1000, POCO_EXCEPTION) \
M(1001, STD_EXCEPTION) \
M(1002, UNKNOWN_EXCEPTION) \
M(1002, SSH_EXCEPTION) \
M(1003, UNKNOWN_EXCEPTION) \
/* See END */
#ifdef APPLY_FOR_EXTERNAL_ERROR_CODES

View File

@ -1,5 +1,15 @@
#include "LibSSHInitializer.h"
#include <stdexcept>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
}
}
namespace ssh
{
@ -9,7 +19,7 @@ LibSSHInitializer::LibSSHInitializer()
int rc = ssh_init();
if (rc != SSH_OK)
{
throw std::runtime_error("Failed to initialize libssh");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to initialize libssh");
}
}

View File

@ -1,8 +1,20 @@
#include "SSHBind.h"
#include <stdexcept>
#include <fmt/format.h>
#include <Common/Exception.h>
#include <Common/SSH/clibssh.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
}
}
namespace ssh
{
@ -10,7 +22,7 @@ SSHBind::SSHBind() : bind_(ssh_bind_new(), &deleter)
{
if (!bind_)
{
throw std::runtime_error("Failed to create ssh_bind");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to create ssh_bind");
}
}
@ -37,7 +49,7 @@ ssh_bind SSHBind::get() const
void SSHBind::setHostKey(const std::string & key_path)
{
if (ssh_bind_options_set(bind_.get(), SSH_BIND_OPTIONS_HOSTKEY, key_path.c_str()) != SSH_OK)
throw std::invalid_argument(fmt::format("Failed setting host key in sshbind due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed setting host key in sshbind due to {}", getError());
}
@ -50,7 +62,7 @@ void SSHBind::disableDefaultConfig()
{
bool enable = false;
if (ssh_bind_options_set(bind_.get(), SSH_BIND_OPTIONS_PROCESS_CONFIG, &enable) != SSH_OK)
throw std::runtime_error(fmt::format("Failed disabling default config in sshbind due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed disabling default config in sshbind due to {}", getError());
}
void SSHBind::setFd(int fd)
@ -61,13 +73,13 @@ void SSHBind::setFd(int fd)
void SSHBind::listen()
{
if (ssh_bind_listen(bind_.get()) != SSH_OK)
throw std::runtime_error(fmt::format("Failed listening in sshbind due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed listening in sshbind due to {}", getError());
}
void SSHBind::acceptFd(ssh_session session, int fd)
{
if (ssh_bind_accept_fd(bind_.get(), session, fd) != SSH_OK)
throw std::runtime_error(fmt::format("Failed accepting fd in sshbind due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed accepting fd in sshbind due to {}", getError());
}
void SSHBind::deleter(ssh_bind bind)

View File

@ -1,7 +1,18 @@
#include "SSHChannel.h"
#include <stdexcept>
#include <Common/Exception.h>
#include <Common/SSH/clibssh.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
}
}
namespace ssh
{
@ -9,7 +20,7 @@ SSHChannel::SSHChannel(ssh_session session) : channel_(ssh_channel_new(session),
{
if (!channel_)
{
throw std::runtime_error("Failed to create ssh_channel");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to create ssh_channel");
}
}

View File

@ -1,7 +1,18 @@
#include "SSHEvent.h"
#include <stdexcept>
#include <Common/Exception.h>
#include <Common/SSH/clibssh.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
}
}
namespace ssh
{
@ -9,7 +20,7 @@ SSHEvent::SSHEvent() : event(ssh_event_new(), &deleter)
{
if (!event)
{
throw std::runtime_error("Failed to create ssh_event");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to create ssh_event");
}
}
@ -36,7 +47,7 @@ ssh_event SSHEvent::get() const
void SSHEvent::addSession(ssh_session session)
{
if (ssh_event_add_session(event.get(), session) == SSH_ERROR)
throw std::runtime_error("Error adding session to ssh event");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Error adding session to ssh event");
}
void SSHEvent::removeSession(ssh_session session)
@ -49,7 +60,7 @@ int SSHEvent::poll(int timeout)
int rc = ssh_event_dopoll(event.get(), timeout);
if (rc == SSH_ERROR)
{
throw std::runtime_error("Error on polling on ssh event");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Error on polling on ssh event");
}
return rc;
}
@ -62,7 +73,7 @@ int SSHEvent::poll()
void SSHEvent::addFd(int fd, int events, ssh_event_callback cb, void * userdata)
{
if (ssh_event_add_fd(event.get(), fd, events, cb, userdata) == SSH_ERROR)
throw std::runtime_error("Error on adding custom file descriptor to ssh event");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Error on adding custom file descriptor to ssh event");
}
void SSHEvent::removeFd(socket_t fd)

View File

@ -330,9 +330,9 @@ private:
channel_callback = std::make_unique<ChannelCallback>(std::move(channel), std::move(db_session));
return channel_callback->channel.get();
}
catch (const std::runtime_error & err)
catch (...)
{
LOG_ERROR(log, "Error while opening channel: {}", err.what());
tryLogCurrentException(log, "Error while opening channel:");
return nullptr;
}
}

View File

@ -1,8 +1,19 @@
#include "SSHSession.h"
#include <stdexcept>
#include <fmt/format.h>
#include <Common/Exception.h>
#include <Common/SSH/clibssh.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SSH_EXCEPTION;
}
}
namespace ssh
{
@ -10,7 +21,7 @@ SSHSession::SSHSession() : session(ssh_new(), &deleter)
{
if (!session)
{
throw std::runtime_error("Failed to create ssh_session");
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed to create ssh_session");
}
}
@ -39,7 +50,7 @@ void SSHSession::connect()
int rc = ssh_connect(session.get());
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed connecting in ssh session due due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed connecting in ssh session due due to {}", getError());
}
}
@ -49,7 +60,7 @@ void SSHSession::disableDefaultConfig()
int rc = ssh_options_set(session.get(), SSH_OPTIONS_PROCESS_CONFIG, &enable);
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed disabling default config for ssh session due due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed disabling default config for ssh session due due to {}", getError());
}
}
@ -59,7 +70,7 @@ void SSHSession::disableSocketOwning()
int rc = ssh_options_set(session.get(), SSH_OPTIONS_OWNS_SOCKET, &owns_socket);
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed disabling socket owning for ssh session due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed disabling socket owning for ssh session due to {}", getError());
}
}
@ -68,7 +79,7 @@ void SSHSession::setPeerHost(const String & host)
int rc = ssh_options_set(session.get(), SSH_OPTIONS_HOST, host.c_str());
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed setting peer host option for ssh session due due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed setting peer host option for ssh session due due to {}", getError());
}
}
@ -77,7 +88,7 @@ void SSHSession::setFd(int fd)
int rc = ssh_options_set(session.get(), SSH_OPTIONS_FD, &fd);
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed setting fd option for ssh session due due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed setting fd option for ssh session due due to {}", getError());
}
}
@ -86,12 +97,12 @@ void SSHSession::setTimeout(int timeout, int timeout_usec)
int rc = ssh_options_set(session.get(), SSH_OPTIONS_TIMEOUT, &timeout);
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed setting for ssh session due timeout option due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed setting for ssh session due timeout option due to {}", getError());
}
rc |= ssh_options_set(session.get(), SSH_OPTIONS_TIMEOUT_USEC, &timeout_usec);
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed setting for ssh session due timeout_usec option due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed setting for ssh session due timeout_usec option due to {}", getError());
}
}
@ -100,7 +111,7 @@ void SSHSession::handleKeyExchange()
int rc = ssh_handle_key_exchange(session.get());
if (rc != SSH_OK)
{
throw std::runtime_error(fmt::format("Failed key exchange for ssh session due to {}", getError()));
throw DB::Exception(DB::ErrorCodes::SSH_EXCEPTION, "Failed key exchange for ssh session due to {}", getError());
}
}