Skip to content

Commit

Permalink
Removed cryptopp and added a glibmm use case
Browse files Browse the repository at this point in the history
Signed-off-by: Vinícius Ferrão <[email protected]>
  • Loading branch information
viniciusferrao committed Jan 14, 2025
1 parent f316c1e commit 03b19f9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
12 changes: 0 additions & 12 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cmake/CommonLibraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ set(COMMON_LIBS
resolv
${STDC++FS}
doctest::doctest
cryptopp::cryptopp
SDBusCpp::sdbus-c++
hwinfo)
5 changes: 4 additions & 1 deletion include/cloysterhpc/diskImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(
{ "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;
Expand Down
4 changes: 0 additions & 4 deletions setupDevEnvironment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
71 changes: 46 additions & 25 deletions src/diskImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/

#include <cloysterhpc/diskImage.h>
#include <cloysterhpc/os.h>
#include <cloysterhpc/services/log.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <cstddef>
#include <fstream>
#include <glibmm/checksum.h>
#include <ios>
#include <istream>
#include <unordered_map>
#include <vector>

const std::filesystem::path& DiskImage::getPath() const { return m_path; }

Expand All @@ -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)) {
Expand All @@ -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<std::string, std::string> hash_map = {
{ "rhel-8.8-x86_64-dvd.iso",
"517abcc67ee3b7212f57e180f5d30be3e8269e7a99e127a3399b7935c7e00a0"
Expand All @@ -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<CryptoPP::StringSink>(output);
auto encoder = std::make_unique<CryptoPP::HexEncoder>(sink.get());
auto filter = std::make_unique<CryptoPP::HashFilter>(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<std::byte> buffer(chunk_size);

while (file.read(reinterpret_cast<std::istream::char_type*>(buffer.data()),
static_cast<std::streamsize>(buffer.size()))) {
std::streamsize bytesRead = file.gcount();

checksum.update(
reinterpret_cast<const unsigned char*>(buffer.data()), bytesRead);
}

// Handle any leftover bytes after the while loop ends
std::streamsize bytesRead = file.gcount();
if (bytesRead > 0) {
checksum.update(
reinterpret_cast<const unsigned char*>(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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Log::Level>(
Expand Down

0 comments on commit 03b19f9

Please sign in to comment.