From 51c3e005e6b3c294d724644e8aafc1badc40056e Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Fri, 30 Jun 2023 11:42:19 +0100 Subject: [PATCH 1/5] build: use C++17 to build brski Use C++17 to build brski instead of C++11, so that we can use some C++17-only libraries, such as `std::filesystem`. --- CHANGELOG.md | 6 ++++++ CMakeLists.txt | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da33ec..88e30b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 your system's [jsmn](https://github.com/zserge/jsmn) lib, instead of downloading it automatically. +### Changes + +#### Build + +* C++17 is now required to build the `brski` CLI tool. + ## [0.2.0] - 2023-03-27 ### Added * Voucher artifact implementation as per [RFC8366](https://www.rfc-editor.org/info/rfc8366), diff --git a/CMakeLists.txt b/CMakeLists.txt index 7310abf..6ed32ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,9 +155,9 @@ add_compile_options( $<$:-Wextra> ) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# use -std=c++11 rather than -std=gnu++11 +# use -std=c++17 rather than -std=gnu++17 set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_POSITION_INDEPENDENT_CODE ON) From 2d0651ec6ca26e29d3e9c9e286c55c67dbbaf44c Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Fri, 30 Jun 2023 12:59:42 +0100 Subject: [PATCH 2/5] refactor(brski): use cpr for http client library Use the [Curl for People (CPR) library][1] instead of cpp-http to make HTTP client requests. libcpr is a C++ wrapper around the C [libcurl][2] library. Compared to cpp-httplib, Curl has much better error checking and error handling, which makes debugging failing HTTP/TLS/SSL issues much easier. [1]: https://docs.libcpr.org/ [2]: https://curl.se/libcurl/ --- CMakeLists.txt | 1 + lib/cpr.cmake | 10 ++++++++ src/brski/http/CMakeLists.txt | 17 ++++++++++--- src/brski/http/httplib_wrapper.cpp | 24 ------------------ src/brski/http/httplib_wrapper.hpp | 21 ---------------- src/brski/http/https_client.cpp | 39 +++++++++++++++++++++--------- 6 files changed, 53 insertions(+), 59 deletions(-) create mode 100644 lib/cpr.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ed32ea..afd5b7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,6 +166,7 @@ set(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH};${CMAKE_CURRENT_BINARY_DIR}/lib") # Include the libraries include(cmocka) +include(cpr) # need to call `FetchContent_MakeAvailable(cpr)` later include(openssl3) include(httplib) include(minIni) diff --git a/lib/cpr.cmake b/lib/cpr.cmake new file mode 100644 index 0000000..ba58b00 --- /dev/null +++ b/lib/cpr.cmake @@ -0,0 +1,10 @@ +if (BUILD_ONLY_DOCS) +else() + include(FetchContent) + FetchContent_Declare(cpr + URL https://github.com/libcpr/cpr/archive/refs/tags/1.9.5.tar.gz + URL_HASH SHA3_256=bea98952db1fe1f45f8d7cf88af98ac67178072722fef33c677f6956690fb489 + DOWNLOAD_NAME "cpr-1.9.5.tar.gz" + DOWNLOAD_DIR "${EP_DOWNLOAD_DIR}" # if empty string, uses default dir + ) +endif() diff --git a/src/brski/http/CMakeLists.txt b/src/brski/http/CMakeLists.txt index efd67c8..ba9d262 100644 --- a/src/brski/http/CMakeLists.txt +++ b/src/brski/http/CMakeLists.txt @@ -1,12 +1,23 @@ +set(CMAKE_C_EXTENSIONS ON) # needed to compile cpr +set(BRSKI_BUILD_TESTING "${BUILD_TESTING}") + +FetchContent_GetProperties(cpr) +if(NOT cpr_POPULATED) + FetchContent_Populate(cpr) + # zlib-ng causes issues with `make install`, at least until v2.1.2 + add_subdirectory(${cpr_SOURCE_DIR} ${cpr_BINARY_DIR} EXCLUDE_FROM_ALL) +endif() +# cpr overwrites the BUILD_TESTING var, so we need to reset it +set(BUILD_TESTING "${BRSKI_BUILD_TESTING}" CACHE BOOL "Build the testing tree." FORCE) + add_library(https_server https_server.cpp) add_library(https_client https_client.cpp) +target_link_libraries(https_client PRIVATE os log cpr::cpr) + if (USE_CPPHTTPLIB_LIB) add_library(httplib_wrapper httplib_wrapper.cpp) target_link_libraries(httplib_wrapper PRIVATE os log httplib::httplib OpenSSL3::Crypto) target_compile_definitions(https_server PUBLIC WITH_CPPHTTPLIB_LIB) target_link_libraries(https_server PRIVATE os log httplib_wrapper) - - target_compile_definitions(https_client PUBLIC WITH_CPPHTTPLIB_LIB) - target_link_libraries(https_client PRIVATE os log httplib_wrapper) endif() diff --git a/src/brski/http/httplib_wrapper.cpp b/src/brski/http/httplib_wrapper.cpp index cac1523..f0178ef 100644 --- a/src/brski/http/httplib_wrapper.cpp +++ b/src/brski/http/httplib_wrapper.cpp @@ -229,27 +229,3 @@ int httplib_start(struct http_config *config, return 0; } - -int httplib_post_request(const std::string &client_key_path, - const std::string &client_cert_path, - const std::string &host, int port, - const std::string &path, bool verify, - const std::string &body, - const std::string &content_type, - std::string &response) { - - httplib::SSLClient cli(host, port, client_cert_path, client_key_path); - - cli.enable_server_certificate_verification(verify); - - log_info("Post request to %s:%d%s", host.c_str(), port, path.c_str()); - if (httplib::Result res = cli.Post(path, body, content_type)) { - response = res->body; - return res->status; - } else { - std::string err = to_string(res.error()); - log_error("httplib::Client fail with \"%s\"", err.c_str()); - - return -1; - } -} diff --git a/src/brski/http/httplib_wrapper.hpp b/src/brski/http/httplib_wrapper.hpp index b139ad8..8e392d1 100644 --- a/src/brski/http/httplib_wrapper.hpp +++ b/src/brski/http/httplib_wrapper.hpp @@ -34,25 +34,4 @@ int httplib_start(struct http_config *config, */ void httplib_stop(void *srv_ctx); -/** - * @brief Sends a POST request to an endpoint - * - * @param[in] client_key_path The https client key path - * @param[in] client_cert_path The https client cert path - * @param[in] host The https server host name - * @param[in] port The https server port name - * @param[in] path The endpoint route path string - * @param[in] verify Enable server certificate verification - * @param[in] body The request body string - * @param[in] content_type The content typ string - * @param[out] response The output response string - * @return int the status code on success, -1 on failure - */ -int httplib_post_request(const std::string &client_key_path, - const std::string &client_cert_path, - const std::string &host, int port, - const std::string &path, bool verify, - const std::string &body, - const std::string &content_type, - std::string &response); #endif diff --git a/src/brski/http/https_client.cpp b/src/brski/http/https_client.cpp index 4c9b166..3c5e87b 100644 --- a/src/brski/http/https_client.cpp +++ b/src/brski/http/https_client.cpp @@ -8,30 +8,47 @@ * @brief File containing the implementation of the https client functions. */ +#include #include +#include "./https_client.hpp" + extern "C" { #include "../../utils/log.h" #include "../../utils/os.h" } -#ifdef WITH_CPPHTTPLIB_LIB -#include "httplib_wrapper.hpp" -#endif - int https_post_request(const std::string &client_key_path, const std::string &client_cert_path, const std::string &host, int port, const std::string &path, bool verify, const std::string &body, const std::string &content_type, std::string &response) { -#ifdef WITH_CPPHTTPLIB_LIB - return httplib_post_request(client_key_path, client_cert_path, host, port, - path, verify, body, content_type, response); -#else - log_error("No https client defined"); - return -1; -#endif + auto key = cpr::ssl::PemKey(std::string{client_key_path}); + auto cert = cpr::ssl::PemCert(std::string{client_cert_path}); + + cpr::SslOptions sslOpts = cpr::Ssl(cert, key, cpr::ssl::VerifyHost{verify}, + cpr::ssl::VerifyPeer{verify}); + auto url = cpr::Url{get_https_address(host.c_str(), port) + path}; + log_info("Post request to %s", url.c_str()); + cpr::Response res = cpr::Post( + cpr::Url{get_https_address(host.c_str(), port) + path}, sslOpts, + cpr::Body{body}, cpr::Header{{"Content-Type", content_type}}, + cpr::DebugCallback([&](cpr::DebugCallback::InfoType type, + std::string data, intptr_t userdata) -> void { + if (type == cpr::DebugCallback::InfoType::TEXT) { + log_trace("%s", data.c_str()); + } + })); + + if (res.status_code == 0) { + log_error("Post request to %s returned error %s", url.c_str(), + res.error.message.c_str()); + return -1; + } + + response = res.text; + return res.status_code; } std::string get_https_address(const char *bind_address, int port) { From 31fe00f5124c41a0c001b49950dc96fb24a54976 Mon Sep 17 00:00:00 2001 From: Alexandru Mereacre Date: Mon, 27 Nov 2023 22:21:19 +0000 Subject: [PATCH 3/5] chore: added git as buidl depenedcy --- .github/workflows/build.yml | 2 +- debian/control | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 18003a0..0b94345 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -81,7 +81,7 @@ jobs: timeout-minutes: 10 - name: Install dependencies run: | - sudo apt-get update && sudo apt-get install pbuilder debhelper -y + sudo apt-get update && sudo apt-get install git pbuilder debhelper -y - name: Setup pdebuilderrc for cross-compiling env: PBUILDER_RC: | diff --git a/debian/control b/debian/control index 0a1f333..d731abc 100644 --- a/debian/control +++ b/debian/control @@ -6,7 +6,8 @@ Build-Depends: debhelper-compat (= 12), ca-certificates, cmake (>=3.15.0), libssl-dev (>=3.0.0), libminini-dev (>=1.2), - libjsmn-dev (>=1.1.0) + libjsmn-dev (>=1.1.0), + git Standards-Version: 4.5.0 Homepage: https://github.com/nqminds/brski Vcs-Browser: https://github.com/nqminds/brski From e86e3b282765d849e2e2a3b7e4568da413bd597e Mon Sep 17 00:00:00 2001 From: Alexandru Mereacre Date: Mon, 27 Nov 2023 22:30:59 +0000 Subject: [PATCH 4/5] chore: added git as buidl depenedcy --- debian/control | 3 +-- lib/cpr.cmake | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/debian/control b/debian/control index d731abc..5c744e6 100644 --- a/debian/control +++ b/debian/control @@ -6,8 +6,7 @@ Build-Depends: debhelper-compat (= 12), ca-certificates, cmake (>=3.15.0), libssl-dev (>=3.0.0), libminini-dev (>=1.2), - libjsmn-dev (>=1.1.0), - git + libjsmn-dev (>=1.1.0), git (>=2.30.0) Standards-Version: 4.5.0 Homepage: https://github.com/nqminds/brski Vcs-Browser: https://github.com/nqminds/brski diff --git a/lib/cpr.cmake b/lib/cpr.cmake index ba58b00..9ef7f04 100644 --- a/lib/cpr.cmake +++ b/lib/cpr.cmake @@ -1,6 +1,7 @@ if (BUILD_ONLY_DOCS) else() include(FetchContent) + SET(CURL_ZLIB OFF CACHE STRING "" FORCE) FetchContent_Declare(cpr URL https://github.com/libcpr/cpr/archive/refs/tags/1.9.5.tar.gz URL_HASH SHA3_256=bea98952db1fe1f45f8d7cf88af98ac67178072722fef33c677f6956690fb489 From 0eefecd86a609988162a370f139f355079f95f23 Mon Sep 17 00:00:00 2001 From: Alexandru Mereacre Date: Mon, 27 Nov 2023 22:39:43 +0000 Subject: [PATCH 5/5] chore: added git as buidl depenedcy --- debian/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/control b/debian/control index 5c744e6..51059fd 100644 --- a/debian/control +++ b/debian/control @@ -5,7 +5,7 @@ Maintainer: Alexandru Mereacre Build-Depends: debhelper-compat (= 12), ca-certificates, cmake (>=3.15.0), libssl-dev (>=3.0.0), - libminini-dev (>=1.2), + libminini-dev (>=1.2), zlib1g-dev, libjsmn-dev (>=1.1.0), git (>=2.30.0) Standards-Version: 4.5.0 Homepage: https://github.com/nqminds/brski