]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3710] use library unique ManagerID
authorRazvan Becheriu <razvan@isc.org>
Sat, 1 Mar 2025 21:13:27 +0000 (23:13 +0200)
committerRazvan Becheriu <razvan@isc.org>
Wed, 5 Mar 2025 12:30:04 +0000 (12:30 +0000)
src/hooks/dhcp/forensic_log/command_callouts.cc
src/hooks/dhcp/forensic_log/lease4_callouts.cc
src/hooks/dhcp/forensic_log/lease6_callouts.cc
src/hooks/dhcp/forensic_log/load_unload.cc
src/hooks/dhcp/forensic_log/rotating_file.cc
src/hooks/dhcp/forensic_log/rotating_file.h

index 2c46f0ba2eeab239dfb05e3f81683c11af463630..a17dd688acd05f2f80e86bbde01069db261b8172 100644 (file)
@@ -19,6 +19,7 @@
 #include <util/str.h>
 #include <legal_log_log.h>
 #include <dhcpsrv/backend_store_factory.h>
+#include <rotating_file.h>
 #include <subnets_user_context.h>
 
 #include <sstream>
@@ -119,7 +120,7 @@ void addDuration(ostringstream& os, ConstElementPtr& arguments) {
     if (!getOptionalInt(arguments, "valid-lft", duration)) {
         int64_t expire = 0;
         if (getOptionalInt(arguments, "expire", expire)) {
-            duration = expire - BackendStoreFactory::instance()->now().tv_sec;
+            duration = expire - BackendStoreFactory::instance(managerID())->now().tv_sec;
         }
     }
 
@@ -187,7 +188,7 @@ bool checkLoggingEnabledSubnet4(ConstElementPtr& arguments) {
 /// @param response The command response.
 int handleLease4Cmds(string& name, ConstElementPtr& arguments,
                      ConstElementPtr& /*response*/) {
-    if (!BackendStoreFactory::instance()) {
+    if (!BackendStoreFactory::instance(managerID())) {
         LOG_ERROR(legal_log_logger,
                   LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
         return (1);
@@ -245,7 +246,7 @@ int handleLease4Cmds(string& name, ConstElementPtr& arguments,
             }
         }
 
-        BackendStoreFactory::instance()->writeln(os.str(), osa.str());
+        BackendStoreFactory::instance(managerID())->writeln(os.str(), osa.str());
     } catch (const exception& ex) {
         LOG_ERROR(legal_log_logger, LEGAL_LOG_COMMAND_WRITE_ERROR)
                   .arg(ex.what());
@@ -287,7 +288,7 @@ bool checkLoggingEnabledSubnet6(ConstElementPtr& arguments) {
 /// @param response The command response.
 int handleLease6Cmds(string& name, ConstElementPtr& arguments,
                      ConstElementPtr& response) {
-    if (!BackendStoreFactory::instance()) {
+    if (!BackendStoreFactory::instance(managerID())) {
         LOG_ERROR(legal_log_logger,
                   LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
         return (1);
@@ -457,7 +458,7 @@ int handleLease6Cmds(string& name, ConstElementPtr& arguments,
             return (status);
         }
 
-        BackendStoreFactory::instance()->writeln(os.str(), osa.str());
+        BackendStoreFactory::instance(managerID())->writeln(os.str(), osa.str());
     } catch (const exception& ex) {
         LOG_ERROR(legal_log_logger, LEGAL_LOG_COMMAND_WRITE_ERROR)
                   .arg(ex.what());
@@ -481,7 +482,7 @@ int handleLease6Cmds(string& name, ConstElementPtr& arguments,
 ///
 /// @return 0 upon success, non-zero otherwise.
 int command_processed(CalloutHandle& handle) {
-    if (!BackendStoreFactory::instance()) {
+    if (!BackendStoreFactory::instance(managerID())) {
         LOG_ERROR(legal_log_logger,
                   LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
         return (1);
index d6fd039f58b8b220ade009c3890693b924d134e8..06d9603e2bc0e9a83407354245be8384cf3be64c 100644 (file)
@@ -16,6 +16,7 @@
 #include <util/str.h>
 #include <legal_log_log.h>
 #include <dhcpsrv/backend_store_factory.h>
+#include <rotating_file.h>
 #include <subnets_user_context.h>
 
 #include <sstream>
@@ -38,13 +39,13 @@ bool getCustomEntry(const Pkt4Ptr& query, const Pkt4Ptr& response,
                     const Lease4Ptr& /*lease*/, std::string& value) {
     bool using_custom_format = false;
 
-    auto expression = BackendStoreFactory::instance()->getRequestFormatExpression();
+    auto expression = BackendStoreFactory::instance(managerID())->getRequestFormatExpression();
     if (expression && query) {
         value = evaluateString(*expression, *query);
         using_custom_format = true;
     }
 
-    expression = BackendStoreFactory::instance()->getResponseFormatExpression();
+    expression = BackendStoreFactory::instance(managerID())->getResponseFormatExpression();
     if (expression && response) {
         value += evaluateString(*expression, *response);
         using_custom_format = true;
@@ -214,7 +215,7 @@ std::string genLease4Entry(const Pkt4Ptr& query,
 ///
 /// @return returns 0 upon success, non-zero otherwise
 int legalLog4Handler(CalloutHandle& handle, const Action& action) {
-    if (!BackendStoreFactory::instance()) {
+    if (!BackendStoreFactory::instance(managerID())) {
         LOG_ERROR(legal_log_logger,
                   LEGAL_LOG_LEASE4_NO_LEGAL_STORE);
         return (1);
@@ -239,9 +240,9 @@ int legalLog4Handler(CalloutHandle& handle, const Action& action) {
         ConstSubnet4Ptr subnet = cfg->getBySubnetId(lease->subnet_id_);
 
         if (!isLoggingDisabled(subnet)) {
-            BackendStoreFactory::instance()->writeln(genLease4Entry(query, response,
-                                                             lease, action),
-                                              lease->addr_.toText());
+            BackendStoreFactory::instance(managerID())->writeln(genLease4Entry(query, response,
+                                                                               lease, action),
+                                                                lease->addr_.toText());
         }
 
     } catch (const std::exception& ex) {
index e96b4d95c32f6af5b0f2b87deb6c9c7a9d8a6182..68e148a56b2163e121261e19761cbc2078786124 100644 (file)
@@ -21,6 +21,7 @@
 #include <util/str.h>
 #include <legal_log_log.h>
 #include <dhcpsrv/backend_store_factory.h>
+#include <rotating_file.h>
 #include <subnets_user_context.h>
 
 #include <sstream>
@@ -484,7 +485,7 @@ bool getCustomEntry(const Pkt6Ptr& query, const Pkt6Ptr& response,
                     const Lease6Ptr& lease, std::string& value) {
     bool using_custom_format = false;
 
-    auto expression = BackendStoreFactory::instance()->getRequestFormatExpression();
+    auto expression = BackendStoreFactory::instance(managerID())->getRequestFormatExpression();
     if (expression && query) {
         replaceTokensForLease(expression, lease);
 
@@ -492,7 +493,7 @@ bool getCustomEntry(const Pkt6Ptr& query, const Pkt6Ptr& response,
         using_custom_format = true;
     }
 
-    expression = BackendStoreFactory::instance()->getResponseFormatExpression();
+    expression = BackendStoreFactory::instance(managerID())->getResponseFormatExpression();
     if (expression && response) {
         replaceTokensForLease(expression, lease);
 
@@ -669,7 +670,7 @@ std::string genLease6Entry(const Pkt6Ptr& query,
 ///
 /// @return returns 0 upon success, non-zero otherwise
 int legalLog6Handler(CalloutHandle& handle, const Action& action) {
-    if (!BackendStoreFactory::instance()) {
+    if (!BackendStoreFactory::instance(managerID())) {
         LOG_ERROR(legal_log_logger, LEGAL_LOG_LEASE6_NO_LEGAL_STORE);
         return (1);
     }
@@ -693,9 +694,9 @@ int legalLog6Handler(CalloutHandle& handle, const Action& action) {
         ConstSubnet6Ptr subnet = cfg->getBySubnetId(lease->subnet_id_);
 
         if (!isLoggingDisabled(subnet)) {
-            BackendStoreFactory::instance()->writeln(genLease6Entry(query, response,
-                                                             lease, action),
-                                              genLease6Addr(lease));
+            BackendStoreFactory::instance(managerID())->writeln(genLease6Entry(query, response,
+                                                                               lease, action),
+                                                                genLease6Addr(lease));
         }
 
     } catch (const std::exception& ex) {
index 6e49e1ac8222c5e546b050f0c485ee18d4e9023a..cb2cb8af57da789ebad61e3e873879fe951ecf42 100644 (file)
@@ -70,7 +70,7 @@ int load(LibraryHandle& handle) {
 
         try {
             BackendStore::parseConfig(parameters, map);
-            BackendStoreFactory::addBackend(map);
+            BackendStoreFactory::addBackend(map, managerID());
         } catch (const isc::db::DbOpenErrorWithRetry& err) {
             string redacted;
             try {
@@ -102,7 +102,7 @@ int unload() {
         // Since it's "global" Let's explicitly destroy it now rather
         // than indeterminately. Note, BackendStore destructor will close
         // the store.
-        BackendStoreFactory::instance().reset();
+        BackendStoreFactory::instance(managerID()).reset();
 
         BackendStoreFactory::unregisterBackendFactory("logfile");
     } catch (const std::exception& ex) {
index 2ba9a2862b85945abd5118efb22cb02c3dfc84e5..95aeb72871a484fcebfa35bf4fffe0965e6f9ed4 100644 (file)
@@ -29,6 +29,17 @@ using namespace std;
 
 namespace isc {
 namespace legal_log {
+
+bool RotatingFile::test_mode_ = false;
+
+isc::dhcp::ManagerID managerID() {
+    static boost::shared_ptr<void*> id = boost::shared_ptr<void*>(new void*);
+    if (RotatingFile::test_mode_) {
+        id.reset();
+    }
+    return (reinterpret_cast<isc::dhcp::ManagerID>(id.get()));
+}
+
 RotatingFile::RotatingFile(const DatabaseConnection::ParameterMap& parameters)
     : BackendStore(parameters), time_unit_(TimeUnit::Day), count_(1), timestamp_(0) {
     apply(parameters);
index 62fddf0b307ab3367c00f12f38c7bc1aa3d2352a..0dc5f6a5dbe7731017a7ff27f2e3205690bc74a5 100644 (file)
@@ -275,21 +275,29 @@ public:
     /// @return The Rotating File Store Backend.
     static isc::dhcp::BackendStorePtr
     factory(const isc::db::DatabaseConnection::ParameterMap& parameters);
+
+    /// @brief Test mode flag (default false).
+    static bool test_mode_;
 };
 
 /// @brief Initialization structure used to register and deregister RotateFile Forensic Store.
 struct RotatingFileInit {
     // Constructor registers
     RotatingFileInit() {
+        RotatingFile::test_mode_ = true;
         isc::dhcp::BackendStoreFactory::registerBackendFactory("logfile", RotatingFile::factory);
     }
 
     // Destructor deregisters
     ~RotatingFileInit() {
         isc::dhcp::BackendStoreFactory::unregisterBackendFactory("logfile");
+        RotatingFile::test_mode_ = false;
     }
 };
 
+/// @brief Return library unique ManagerID.
+isc::dhcp::ManagerID managerID();
+
 } // namespace legal_log
 } // namespace isc