diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 773ab811d26dc9dfe299eef504c2c9e584c83882..b60f5df5ec9cfb355446c449958f2e2e355bb568 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -20,14 +20,15 @@ # add all header files to this list set(libcaosdb_INCL - ${CMAKE_CURRENT_BINARY_DIR}/constants.h - ${CMAKE_CURRENT_SOURCE_DIR}/connection.h - ${CMAKE_CURRENT_SOURCE_DIR}/authentication.h - ${CMAKE_CURRENT_SOURCE_DIR}/utils.h + ${CMAKE_CURRENT_BINARY_DIR}/caosdb/constants.h + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.h + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.h + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/utils.h + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/exceptions.h ) # pass variable to parent scope set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE) # initialize constants -configure_file(constants.h.in constants.h) +configure_file(caosdb/constants.h.in caosdb/constants.h) diff --git a/include/authentication.h b/include/caosdb/authentication.h similarity index 96% rename from include/authentication.h rename to include/caosdb/authentication.h index cf8a7bf86830e682231858a180a8c95ea88287f8..a1fdca8c08c92fcd0dff637725851e67b66b7696 100644 --- a/include/authentication.h +++ b/include/caosdb/authentication.h @@ -19,8 +19,8 @@ * */ -#ifndef AUTHENTICATION_H -#define AUTHENTICATION_H +#ifndef CAOSDB_AUTHENTICATION_H +#define CAOSDB_AUTHENTICATION_H /** * @file authentication.h * @author Timm Fitschen @@ -32,7 +32,7 @@ #include <memory> #include <grpc/grpc.h> #include <grpcpp/security/credentials.h> -#include "utils.h" +#include "caosdb/utils.h" namespace caosdb { namespace authentication { diff --git a/include/connection.h b/include/caosdb/connection.h similarity index 96% rename from include/connection.h rename to include/caosdb/connection.h index 0b535fa0642e9f797dac1f6a511506a1a760ca24..978fdcc6394fe311d202bfc91938330223635c0c 100644 --- a/include/connection.h +++ b/include/caosdb/connection.h @@ -19,8 +19,8 @@ * */ -#ifndef CONNECTION_H -#define CONNECTION_H +#ifndef CAOSDB_CONNECTION_H +#define CAOSDB_CONNECTION_H /** * @file connection.h * @author Timm Fitschen @@ -29,7 +29,7 @@ */ #include <string> #include <iosfwd> -#include "authentication.h" +#include "caosdb/authentication.h" #include <memory> #include <grpc/grpc.h> #include <grpcpp/channel.h> @@ -48,7 +48,7 @@ class VersionInfo; } // namespace info } // namespace caosdb -namespace caosdb { +namespace caosdb::connection { using caosdb::authentication::Authenticator; using caosdb::info::v1alpha1::GeneralInfoService; using caosdb::info::v1alpha1::VersionInfo; @@ -142,5 +142,5 @@ public: -> GeneralInfoService::Stub &; [[nodiscard]] auto getVersionInfo() const -> VersionInfo; }; -} // namespace caosdb +} // namespace caosdb::connection #endif diff --git a/include/constants.h.in b/include/caosdb/constants.h.in similarity index 95% rename from include/constants.h.in rename to include/caosdb/constants.h.in index 3d436f9fc5af37dd47be1c180383d4b2beb9733d..02c0341d17b77bbf843debea349af45b91516662 100644 --- a/include/constants.h.in +++ b/include/caosdb/constants.h.in @@ -20,8 +20,8 @@ * */ -#ifndef CAOSDB_CONFIG_H -#define CAOSDB_CONFIG_H +#ifndef CAOSDB_CONSTANTS_H +#define CAOSDB_CONSTANTS_H namespace caosdb { // clang-format off constexpr int LIBCAOSDB_VERSION_MAJOR = @libcaosdb_VERSION_MAJOR@; diff --git a/include/caosdb/exceptions.h b/include/caosdb/exceptions.h new file mode 100644 index 0000000000000000000000000000000000000000..8e70fec23b5276e5156fd9a0d34b04e439357f06 --- /dev/null +++ b/include/caosdb/exceptions.h @@ -0,0 +1,46 @@ +/* + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com> + * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +#ifndef CAOSDB_EXCEPTIONS_H +#define CAOSDB_EXCEPTIONS_H +#include <stdexcept> +#include <string> + +namespace caosdb::exceptions { +using std::runtime_error; + +/** + * @brief Exception for authentication errors. + */ +class AuthenticationError : public runtime_error { +public: + explicit AuthenticationError(const std::string &what_arg) + : runtime_error(what_arg) {} +}; + +class ConnectionError : public runtime_error { +public: + explicit ConnectionError(const std::string &what_arg) + : runtime_error(what_arg) {} +}; + +} // namespace caosdb::exceptions +#endif diff --git a/include/utils.h b/include/caosdb/utils.h similarity index 97% rename from include/utils.h rename to include/caosdb/utils.h index 75e04bee03aa2abb435d740d03db5faede3fb569..79393b12cca134e00c1e874735c6cba886a31deb 100644 --- a/include/utils.h +++ b/include/caosdb/utils.h @@ -19,8 +19,8 @@ * */ -#ifndef UTILS_H -#define UTILS_H +#ifndef CAOSDB_UTILS_H +#define CAOSDB_UTILS_H #include <string_view> #include <fstream> #include <string> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9b793ce6025d3c35b6f6736a687307668387cca4..cb2e20aee19535efcd230ed9be132f8b0cb99b1e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,9 +21,8 @@ # add all source files to this list set(libcaosdb_SRC - src/utils.cpp - src/authentication.cpp - src/connection.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp ) # pass variable to parent scope diff --git a/src/authentication.cpp b/src/caosdb/authentication.cpp similarity index 98% rename from src/authentication.cpp rename to src/caosdb/authentication.cpp index 9286a857b1a886e8b48f083e2ab169045102d98b..5e6a89a08358794edea8f2610ada9ac594bb760c 100644 --- a/src/authentication.cpp +++ b/src/caosdb/authentication.cpp @@ -20,7 +20,7 @@ */ #include <string> #include <memory> -#include "authentication.h" +#include "caosdb/authentication.h" #include <grpcpp/impl/codegen/status.h> #include <grpcpp/impl/codegen/string_ref.h> #include <map> diff --git a/src/connection.cpp b/src/caosdb/connection.cpp similarity index 89% rename from src/connection.cpp rename to src/caosdb/connection.cpp index 5f0d798c0579bc3b3f454293e824a0f0c73727e9..071714b79fc39f44ffb74a85e6724189750684a6 100644 --- a/src/connection.cpp +++ b/src/caosdb/connection.cpp @@ -20,9 +20,9 @@ * */ -#include "connection.h" -#include "utils.h" -#include "authentication.h" +#include "caosdb/connection.h" +#include "caosdb/utils.h" +#include "caosdb/authentication.h" #include <grpcpp/create_channel.h> #include <grpcpp/impl/codegen/client_context.h> #include <grpcpp/impl/codegen/status.h> @@ -32,9 +32,12 @@ #include <string> #include "caosdb/info/v1alpha1/main.grpc.pb.h" #include "caosdb/info/v1alpha1/main.pb.h" +#include "caosdb/exceptions.h" -namespace caosdb { +namespace caosdb::connection { using caosdb::authentication::Authenticator; +using caosdb::exceptions::AuthenticationError; +using caosdb::exceptions::ConnectionError; using caosdb::info::v1alpha1::GeneralInfoService; using caosdb::info::v1alpha1::GetVersionInfoRequest; using caosdb::info::v1alpha1::GetVersionInfoResponse; @@ -154,10 +157,18 @@ auto CaosDBConnection::getGeneralInfoService() const stub_->GetVersionInfo(&context, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + switch (status.error_code()) { + case grpc::StatusCode::UNAUTHENTICATED: + throw AuthenticationError(status.error_message()); + case grpc::StatusCode::UNAVAILABLE: + throw ConnectionError(status.error_message()); + default: + std::cout << status.error_code() << "\n"; + throw std::runtime_error(status.error_message()); + } } VersionInfo result(response.version_info()); return result; } -} // namespace caosdb +} // namespace caosdb::connection diff --git a/src/caosdbcli.cpp b/src/caosdbcli.cpp index 1d18c07b8036089e2e9a539b1c5a3ca44afdc3b6..7b834870fd5d5a0e0dbb299f9994bde65ee4824c 100644 --- a/src/caosdbcli.cpp +++ b/src/caosdbcli.cpp @@ -25,10 +25,10 @@ #include <memory> #include <string> #include "caosdb/info/v1alpha1/main.pb.h" -#include "constants.h" -#include "connection.h" -#include "authentication.h" -#include "utils.h" +#include "caosdb/constants.h" +#include "caosdb/connection.h" +#include "caosdb/authentication.h" +#include "caosdb/utils.h" auto main() -> int { @@ -44,14 +44,17 @@ auto main() -> int { const auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_PORT", "8000"); const auto port = std::stoi(port_str); + const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin"); + const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb"); auto auth = std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>( - "tf", "vuY36moa"); - auto cacert = std::make_shared<caosdb::PemFileCACertProvider>(pem_file); - auto config = std::make_shared<caosdb::SslCaosDBConnectionConfig>( + user, password); + auto cacert = + std::make_shared<caosdb::connection::PemFileCACertProvider>(pem_file); + auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>( host, port, cacert, auth); - caosdb::CaosDBConnection connection(config); + caosdb::connection::CaosDBConnection connection(config); std::cout << std::endl << connection << std::endl; const auto &v_info = connection.getVersionInfo(); // const auto &build = v_info.build(); diff --git a/src/utils.cpp b/src/utils.cpp deleted file mode 100644 index e1afe424e108b76d246f7a27bb225147986a94a4..0000000000000000000000000000000000000000 --- a/src/utils.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "utils.h" - -namespace caosdb::utils {} // namespace caosdb::utils