diff options
author | Ed Tanous <edtanous@google.com> | 2023-06-28 21:28:38 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-07-12 22:48:32 +0300 |
commit | 2c6ffdb08b2207ff7c31041f77cc3755508d45c4 (patch) | |
tree | 5b2b58102221318866a06a11751d2efa76782fe4 | |
parent | 9eef578458f6dc3587bd27039f1ebb367041000b (diff) | |
download | bmcweb-2c6ffdb08b2207ff7c31041f77cc3755508d45c4.tar.xz |
Use openssl random number generator
We already have a generator class. We should use it. Wrap this into a
function that can be unit tested, and add unit tests.
Note, some files also needed to change name, because random.hpp
conflicts with the built in random, and causes circular build problems.
This commit changes it to ossl_random.
Tested: Unit tests pass. Now has coverage.
Redfish service validator passes.
Change-Id: I5f8eee1af5f4843a352c6fd0e26d67fd3320ef53
Signed-off-by: Ed Tanous <edtanous@google.com>
-rw-r--r-- | include/image_upload.hpp | 8 | ||||
-rw-r--r-- | include/ossl_random.hpp (renamed from include/random.hpp) | 3 | ||||
-rw-r--r-- | include/persistent_data.hpp | 7 | ||||
-rw-r--r-- | include/sessions.hpp | 2 | ||||
-rw-r--r-- | include/ssl_key_handler.hpp | 2 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | redfish-core/include/event_service_manager.hpp | 2 | ||||
-rw-r--r-- | redfish-core/lib/update_service.hpp | 6 | ||||
-rw-r--r-- | src/ossl_random.cpp | 12 | ||||
-rw-r--r-- | test/include/ossl_random.cpp | 23 |
10 files changed, 50 insertions, 17 deletions
diff --git a/include/image_upload.hpp b/include/image_upload.hpp index 18a0c09b78..cdd7dd47d2 100644 --- a/include/image_upload.hpp +++ b/include/image_upload.hpp @@ -3,10 +3,8 @@ #include "app.hpp" #include "dbus_singleton.hpp" #include "dbus_utility.hpp" +#include "ossl_random.hpp" -#include <boost/uuid/uuid.hpp> -#include <boost/uuid/uuid_generators.hpp> -#include <boost/uuid/uuid_io.hpp> #include <sdbusplus/bus/match.hpp> #include <cstdio> @@ -93,9 +91,7 @@ inline void "member='InterfacesAdded',path='/xyz/openbmc_project/software'", callback); - std::string filepath( - "/tmp/images/" + - boost::uuids::to_string(boost::uuids::random_generator()())); + std::string filepath("/tmp/images/" + bmcweb::getRandomUUID()); BMCWEB_LOG_DEBUG << "Writing file to " << filepath; std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); diff --git a/include/random.hpp b/include/ossl_random.hpp index f5f094689e..2cbec849a9 100644 --- a/include/random.hpp +++ b/include/ossl_random.hpp @@ -4,6 +4,7 @@ #include <iostream> #include <limits> +#include <string> namespace bmcweb { @@ -46,4 +47,6 @@ struct OpenSSLGenerator bool err = false; }; +std::string getRandomUUID(); + } // namespace bmcweb diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp index 4344074a14..a08ca7e086 100644 --- a/include/persistent_data.hpp +++ b/include/persistent_data.hpp @@ -3,12 +3,10 @@ #include "event_service_store.hpp" #include "http_request.hpp" #include "http_response.hpp" +#include "ossl_random.hpp" #include "sessions.hpp" #include <boost/beast/http/fields.hpp> -#include <boost/uuid/uuid.hpp> -#include <boost/uuid/uuid_generators.hpp> -#include <boost/uuid/uuid_io.hpp> #include <nlohmann/json.hpp> #include <filesystem> @@ -178,8 +176,7 @@ class ConfigFile if (systemUuid.empty()) { - systemUuid = - boost::uuids::to_string(boost::uuids::random_generator()()); + systemUuid = bmcweb::getRandomUUID(); needWrite = true; } if (fileRevision < jsonRevision) diff --git a/include/sessions.hpp b/include/sessions.hpp index 98912e827a..9179723e5f 100644 --- a/include/sessions.hpp +++ b/include/sessions.hpp @@ -1,7 +1,7 @@ #pragma once #include "logging.hpp" -#include "random.hpp" +#include "ossl_random.hpp" #include "utility.hpp" #include "utils/ip_utils.hpp" diff --git a/include/ssl_key_handler.hpp b/include/ssl_key_handler.hpp index 0794fdcfac..abc9b50d61 100644 --- a/include/ssl_key_handler.hpp +++ b/include/ssl_key_handler.hpp @@ -1,7 +1,7 @@ #pragma once #include "logging.hpp" -#include "random.hpp" +#include "ossl_random.hpp" extern "C" { diff --git a/meson.build b/meson.build index dd8eb81855..e9716be153 100644 --- a/meson.build +++ b/meson.build @@ -355,6 +355,7 @@ srcfiles_bmcweb = files( 'src/boost_url.cpp', 'src/dbus_singleton.cpp', 'src/json_html_serializer.cpp', + 'src/ossl_random.cpp', ) bmcweblib = static_library( @@ -391,6 +392,7 @@ srcfiles_unittest = files( 'test/include/multipart_test.cpp', 'test/include/openbmc_dbus_rest_test.cpp', 'test/include/str_utility_test.cpp', + 'test/include/ossl_random.cpp', 'test/redfish-core/include/privileges_test.cpp', 'test/redfish-core/include/redfish_aggregator_test.cpp', 'test/redfish-core/include/registries_test.cpp', diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp index 3a20a79ba9..a1c4fb9fe1 100644 --- a/redfish-core/include/event_service_manager.hpp +++ b/redfish-core/include/event_service_manager.hpp @@ -19,8 +19,8 @@ #include "event_service_store.hpp" #include "http_client.hpp" #include "metric_report.hpp" +#include "ossl_random.hpp" #include "persistent_data.hpp" -#include "random.hpp" #include "registries.hpp" #include "registries_selector.hpp" #include "str_utility.hpp" diff --git a/redfish-core/lib/update_service.hpp b/redfish-core/lib/update_service.hpp index 28621d5bf8..357c04770b 100644 --- a/redfish-core/lib/update_service.hpp +++ b/redfish-core/lib/update_service.hpp @@ -20,6 +20,7 @@ #include "app.hpp" #include "dbus_utility.hpp" #include "multipart_parser.hpp" +#include "ossl_random.hpp" #include "query.hpp" #include "registries/privilege_registry.hpp" #include "task.hpp" @@ -531,9 +532,8 @@ inline void requestRoutesUpdateServiceActionsSimpleUpdate(App& app) inline void uploadImageFile(crow::Response& res, std::string_view body) { - std::filesystem::path filepath( - "/tmp/images/" + - boost::uuids::to_string(boost::uuids::random_generator()())); + std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID()); + BMCWEB_LOG_DEBUG << "Writing file to " << filepath; std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); diff --git a/src/ossl_random.cpp b/src/ossl_random.cpp new file mode 100644 index 0000000000..f196a8b5a3 --- /dev/null +++ b/src/ossl_random.cpp @@ -0,0 +1,12 @@ +#include "ossl_random.hpp" + +#include <boost/uuid/uuid_generators.hpp> +#include <boost/uuid/uuid_io.hpp> + +std::string bmcweb::getRandomUUID() +{ + using bmcweb::OpenSSLGenerator; + OpenSSLGenerator ossl; + return boost::uuids::to_string( + boost::uuids::basic_random_generator<OpenSSLGenerator>(ossl)()); +} diff --git a/test/include/ossl_random.cpp b/test/include/ossl_random.cpp new file mode 100644 index 0000000000..22935e7d1c --- /dev/null +++ b/test/include/ossl_random.cpp @@ -0,0 +1,23 @@ +#include "ossl_random.hpp" + +#include <string> + +#include <gmock/gmock.h> // IWYU pragma: keep +#include <gtest/gtest.h> // IWYU pragma: keep + +namespace +{ + +using testing::MatchesRegex; + +TEST(Bmcweb, GetRandomUUID) +{ + using bmcweb::getRandomUUID; + // 78e96a4b-62fe-48d8-ac09-7f75a94671e0 + EXPECT_THAT( + getRandomUUID(), + MatchesRegex( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")); +} + +} // namespace |