From 03b19f9e09c83517c1cb47811abd2b899438bc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:56:33 -0300 Subject: [PATCH] Removed `cryptopp` and added a `glibmm` use case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Vinícius Ferrão <2031761+viniciusferrao@users.noreply.github.com> --- Dependencies.cmake | 12 ------ README.md | 4 +- cmake/CommonLibraries.cmake | 1 - include/cloysterhpc/diskImage.h | 5 ++- setupDevEnvironment.sh | 4 -- src/diskImage.cpp | 71 +++++++++++++++++++++------------ src/main.cpp | 2 +- 7 files changed, 52 insertions(+), 47 deletions(-) diff --git a/Dependencies.cmake b/Dependencies.cmake index 9efebd31..adc3530a 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -94,18 +94,6 @@ function(cloysterhpc_setup_dependencies) endif() endif() - if(NOT TARGET cryptopp::cryptopp) - if (cloysterhpc_ENABLE_CONAN) - CPMFindPackage(NAME cryptopp) - else() - CPMAddPackage( - NAME cryptopp-cmake - GIT_TAG "CRYPTOPP_8_8_0" - GITHUB_REPOSITORY - "abdes/cryptopp-cmake") - endif() - endif() - if(NOT TARGET SDBusCpp::sdbus-c++) if (cloysterhpc_ENABLE_CONAN) CPMFindPackage(NAME sdbus-c++) diff --git a/README.md b/README.md index 301360b6..2e24715c 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,7 @@ needed. * Adherence with best practices is done with [gsl-lite](https://github.com/gsl-lite/gsl-lite). * [newt](https://pagure.io/newt) for Terminal UI. -* [Perl](https://www.perl.org/) for the libxcrypt, one of our - subdependencies. The lib (and the one who depend on it) will be - removed because this dependency caused a lot of issues +* [glibmm](https://developer.gnome.org/glibmm/stable/) for Glib bindings. Only [newt](https://pagure.io/newt) must be pre-installed for compilation. We don't ship it. Everything else should be found and installed diff --git a/cmake/CommonLibraries.cmake b/cmake/CommonLibraries.cmake index 6a2ea179..f616017c 100644 --- a/cmake/CommonLibraries.cmake +++ b/cmake/CommonLibraries.cmake @@ -15,6 +15,5 @@ set(COMMON_LIBS resolv ${STDC++FS} doctest::doctest - cryptopp::cryptopp SDBusCpp::sdbus-c++ hwinfo) diff --git a/include/cloysterhpc/diskImage.h b/include/cloysterhpc/diskImage.h index 727f7c8e..d24ab51b 100644 --- a/include/cloysterhpc/diskImage.h +++ b/include/cloysterhpc/diskImage.h @@ -20,12 +20,15 @@ class DiskImage { private: std::filesystem::path m_path; + // BUG: This is bad design, and also overrides what's inside the map + // variable on the class that holds the checksums. /** * @brief List of known disk image filenames. */ static constexpr auto m_knownImageFilename { std::to_array( { "rhel-8.8-x86_64-dvd.iso", "OracleLinux-R8-U8-x86_64-dvd.iso", - "Rocky-8.8-x86_64-dvd1.iso", "AlmaLinux-8.8-x86_64-dvd.iso" }) }; + "Rocky-8.8-x86_64-dvd1.iso", "AlmaLinux-8.8-x86_64-dvd.iso", + "Rocky-9.5-x86_64-dvd.iso" }) }; public: const std::filesystem::path& getPath() const; diff --git a/setupDevEnvironment.sh b/setupDevEnvironment.sh index ef8bd0e3..7e3f2e06 100755 --- a/setupDevEnvironment.sh +++ b/setupDevEnvironment.sh @@ -113,10 +113,6 @@ elif [ "$os_version" = "9" ]; then compiler-rt lldb fi -# Install Perl dependencies needed by libxcrypt -dnf -y install perl-FindBin perl-open perl-Thread-Queue perl-Thread \ - perl-File-Compare perl-File-Copy - # Install Conan as user pip3 install --user conan diff --git a/src/diskImage.cpp b/src/diskImage.cpp index e50d05b1..d5d85f45 100644 --- a/src/diskImage.cpp +++ b/src/diskImage.cpp @@ -5,11 +5,14 @@ */ #include -#include #include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include const std::filesystem::path& DiskImage::getPath() const { return m_path; } @@ -29,18 +32,19 @@ void DiskImage::setPath(const std::filesystem::path& path) bool DiskImage::isKnownImage(const std::filesystem::path& path) { - for (const auto& image : m_knownImageFilename) + for (const auto& image : m_knownImageFilename) { if (path.filename().string() == image) { LOG_TRACE("Disk image is recognized") return true; } + } LOG_TRACE("Disk image is unknown. Maybe you're using a custom image or " "changed the default name?"); return false; } -// BUG: Consider removing +// BUG: Consider removing/reimplement this method bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path) { if (!isKnownImage(path)) { @@ -50,6 +54,7 @@ bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path) LOG_TRACE("Verifying disk image checksum... This may take a while") + // BUG: This should no be hardcoded here. An ancillary file should be used std::unordered_map hash_map = { { "rhel-8.8-x86_64-dvd.iso", "517abcc67ee3b7212f57e180f5d30be3e8269e7a99e127a3399b7935c7e00a0" @@ -63,27 +68,43 @@ bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path) { "AlmaLinux-8.8-x86_64-dvd.iso", "635b30b967b509a32a1a3d81401db9861922acb396d065922b39405a43a04a3" "1" }, + { "Rocky-9.5-x86_64-dvd.iso", + "ba60c3653640b5747610ddfb4d09520529bef2d1d83c1feb86b0c84dff31e04" + "e" } }; - CryptoPP::SHA256 hash; - std::string isoHash = hash_map.find(path.filename().string())->second; - std::string output; - auto sink = std::make_unique(output); - auto encoder = std::make_unique(sink.get()); - auto filter = std::make_unique(hash, encoder.get()); - - CryptoPP::FileSource(path.string().c_str(), true, filter.get(), true); - transform(output.begin(), output.end(), output.begin(), ::tolower); - - /* Those release() methods are needed to address the following issue: - * https://github.com/weidai11/cryptopp/issues/1002 - * https://stackoverflow.com/questions/21057393/what-does-double-free-mean - */ - sink.release(); - encoder.release(); - filter.release(); - - if (output == isoHash) { + Glib::Checksum checksum(Glib::Checksum::Type::SHA256); + + std::ifstream file(path, std::ios::in | std::ios::binary); + if (!file.is_open()) { + throw std::filesystem::filesystem_error( + "Failed to open file", path, std::error_code()); + } + + // Read the file in chunks of 16834 bytes + constexpr std::size_t chunk_size = 16384; + std::vector buffer(chunk_size); + + while (file.read(reinterpret_cast(buffer.data()), + static_cast(buffer.size()))) { + std::streamsize bytesRead = file.gcount(); + + checksum.update( + reinterpret_cast(buffer.data()), bytesRead); + } + + // Handle any leftover bytes after the while loop ends + std::streamsize bytesRead = file.gcount(); + if (bytesRead > 0) { + checksum.update( + reinterpret_cast(buffer.data()), bytesRead); + } + + LOG_INFO(fmt::format("SHA256 checksum of file {} is: {}", path.string(), + checksum.get_string())); + + if (checksum.get_string() + == hash_map.find(path.filename().string())->second) { LOG_TRACE("Checksum - The disk image is valid") return true; } diff --git a/src/main.cpp b/src/main.cpp index d472c93b..4924ba84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,7 +87,7 @@ int main(int argc, const char** argv) "-u, --unattended", unattended, "Perform an unattended installation"); CLI11_PARSE(app, argc, argv) - + Log::init([]() { if (std::regex_match(cloyster::logLevelInput, std::regex("^[0-9]+$"))) { return magic_enum::enum_cast(