From bb4cff3b0e05c93657c77ad2b9cf026e5145c6d1 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 5 Jul 2021 22:13:00 +0200
Subject: [PATCH] WIP: ssl

---
 CMakeLists.txt              | 10 ++++++++--
 include/caosdb/connection.h |  2 ++
 src/caosdb/connection.cpp   | 10 ++++++----
 src/caosdbcli.cpp           |  1 +
 test/test_utils.cpp         |  8 ++++----
 5 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c803bca..063015a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,6 +35,7 @@ set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
 ###########################################
 ### DEPENDENCY MANAGEMENT with CONAN
 ###########################################
+message(STATUS "Build directory ${CMAKE_BINARY_DIR}")
 include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
 conan_basic_setup()
 
@@ -53,10 +54,15 @@ add_executable(caosdbcli src/caosdbcli.cpp)
 ### LINTING with CLANG-TIDY and INCLUDE-WHAT-YOU-USE
 #######################################################
 
-# linting is active when BUILD_TYPE == "Debug"
-if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
+option(LINTING "Enable linting with clang-tidy and iwyu when in non-Debug build-type" OFF)
+if("${CMAKE_BUILD_TYPE}" MATCHES "Debug" OR LINTING)
     set(_LINTING ON)
 endif()
+option(SKIP_LINTING "Skip linting even when in Debug build-type" OFF)
+if("${CMAKE_BUILD_TYPE}" MATCHES "Debug" AND SKIP_LINTING)
+    message(WARNING "Skipping linting due to SKIP_LINTING option")
+    set(_LINTING OFF)
+endif()
 if(_LINTING)
     find_program(iwyu
         NAMES include-what-you-use iwyu
diff --git a/include/caosdb/connection.h b/include/caosdb/connection.h
index 3e5be41..7c971c0 100644
--- a/include/caosdb/connection.h
+++ b/include/caosdb/connection.h
@@ -110,6 +110,7 @@ public:
 class SslCaosDBConnectionConfig : public CaosDBConnectionConfig {
 private:
   std::shared_ptr<ChannelCredentials> credentials;
+  std::string cacert;
 
 public:
   SslCaosDBConnectionConfig(
@@ -128,6 +129,7 @@ public:
  * @brief A reusable connection to a CaosDBServer.
  */
 class CaosDBConnection {
+  std::shared_ptr<grpc::Channel> channel;
   std::shared_ptr<CaosDBConnectionConfig> config;
   std::unique_ptr<GeneralInfoService::Stub> stub_;
 
diff --git a/src/caosdb/connection.cpp b/src/caosdb/connection.cpp
index 2205133..81339c2 100644
--- a/src/caosdb/connection.cpp
+++ b/src/caosdb/connection.cpp
@@ -115,6 +115,7 @@ SslCaosDBConnectionConfig::SslCaosDBConnectionConfig(
   : CaosDBConnectionConfig(host, port) {
   SslCredentialsOptions options;
   options.pem_root_certs = cacert->getCACertPem();
+  this->cacert = cacert->getCACertPem();
   this->credentials = SslCredentials(options);
 }
 
@@ -126,6 +127,7 @@ SslCaosDBConnectionConfig::SslCaosDBConnectionConfig(
 
   SslCredentialsOptions options;
   options.pem_root_certs = cacert->getCACertPem();
+  this->cacert = cacert->getCACertPem();
   this->credentials = grpc::CompositeChannelCredentials(
     SslCredentials(options), authenticator->getCallCredentials());
 }
@@ -137,7 +139,7 @@ auto SslCaosDBConnectionConfig::getChannelCredentials() const
 
 auto SslCaosDBConnectionConfig::toString() const -> std::string {
   return "SslCaosDBConnectionConfig(" + this->getHost() + "," +
-         std::to_string(this->getPort()) + ")";
+         std::to_string(this->getPort()) + "," + this->cacert + ")";
 }
 
 CaosDBConnection::CaosDBConnection(
@@ -145,9 +147,9 @@ CaosDBConnection::CaosDBConnection(
   this->config = config;
   const std::string target =
     this->config->getHost() + ":" + std::to_string(this->config->getPort());
-  const std::shared_ptr<grpc::Channel> &channel =
+  this->channel =
     grpc::CreateChannel(target, this->config->getChannelCredentials());
-  this->stub_ = GeneralInfoService::NewStub(channel);
+  this->stub_ = GeneralInfoService::NewStub(this->channel);
 }
 
 auto operator<<(std::ostream &out, const CaosDBConnection &connection)
@@ -162,7 +164,7 @@ auto operator<<(std::ostream &out, const CaosDBConnection &connection)
   GetVersionInfoResponse response;
   grpc::ClientContext context;
   const grpc::Status status =
-    stub_->GetVersionInfo(&context, request, &response);
+    this->stub_->GetVersionInfo(&context, request, &response);
 
   if (!status.ok()) {
     switch (status.error_code()) {
diff --git a/src/caosdbcli.cpp b/src/caosdbcli.cpp
index 1aaab54..7dc2d71 100644
--- a/src/caosdbcli.cpp
+++ b/src/caosdbcli.cpp
@@ -49,6 +49,7 @@ auto main() -> int {
   std::cout << "USER: " << user << "\n";
   std::cout << "PORT: " << port_str << "\n";
   std::cout << "HOST: " << host << "\n";
+  std::cout << "CACERT: " << pem_file << "\n";
 
   auto auth =
     std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>(
diff --git a/test/test_utils.cpp b/test/test_utils.cpp
index 245b0af..619caee 100644
--- a/test/test_utils.cpp
+++ b/test/test_utils.cpp
@@ -28,9 +28,9 @@
 #include <boost/beast/core/detail/base64.hpp>
 
 TEST(test_utils, base64_encode) {
-  auto test_plain = std::string("Test");
-  auto test_encoded = std::string("VGVzdA==");
-  ASSERT_EQ(4, test_plain.size());
-  ASSERT_EQ(8, boost::beast::detail::base64::encoded_size(test_plain.size()));
+  auto test_plain = std::string("admin:caosdb");
+  auto test_encoded = std::string("YWRtaW46Y2Fvc2Ri");
+  ASSERT_EQ(12, test_plain.size());
+  ASSERT_EQ(16, boost::beast::detail::base64::encoded_size(test_plain.size()));
   ASSERT_EQ(test_encoded, caosdb::utils::base64_encode(test_plain));
 }
-- 
GitLab