/* * 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/>. * */ #include <memory> // for allocator, make_shared, shared_ptr #include <string> // for stoi, string #include "caosdb/authentication.h" // for PlainPasswordAuthenticator #include "caosdb/connection.h" // for PemFileCertificateProvider, TlsCaosDBC... #include "caosdb/utility.h" // for get_env_var namespace caosdb::connection { using caosdb::authentication::PlainPasswordAuthenticator; inline auto create_test_connection_config() -> std::unique_ptr<TlsConnectionConfig> { auto port_str = caosdb::utility::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); auto port = std::stoi(port_str); const auto host = caosdb::utility::get_env_var("CAOSDB_SERVER_HOST", "localhost"); const auto path = caosdb::utility::get_env_var("CAOSDB_SERVER_CERT", std::string()); const auto user = caosdb::utility::get_env_var("CAOSDB_USER", "admin"); const auto password = caosdb::utility::get_env_var("CAOSDB_PASSWORD", "caosdb"); auto auth = PlainPasswordAuthenticator(user, password); auto cert = PemFileCertificateProvider(path); return std::make_unique<TlsConnectionConfig>(host, port, cert, auth); } /** * Return a fresh Connection pointer. The caller has the ownership. */ inline auto create_test_connection() -> std::unique_ptr<Connection> { auto config = create_test_connection_config(); return std::make_unique<Connection>(*(config.get())); }; /** * Singleton which holds a single global Connection */ class ConnectionProvider { private: std::shared_ptr<Connection> connection; ConnectionProvider() : connection(create_test_connection()){}; public: static ConnectionProvider &GetInstance() { static ConnectionProvider instance; return instance; }; inline auto GetConnection() -> std::shared_ptr<Connection> & { return this->connection; } ConnectionProvider(ConnectionProvider const &) = delete; void operator=(ConnectionProvider const &) = delete; }; /** * Return a connection for testing purposes. */ inline auto get_test_connection() -> const std::shared_ptr<Connection> & { return ConnectionProvider::GetInstance().GetConnection(); }; } // namespace caosdb::connection