You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by sz...@apache.org on 2023/01/05 21:57:19 UTC

[nifi-minifi-cpp] 02/02: MINIFICPP-1697 Give a better error message on unsupported algorithms in HashContent

This is an automated email from the ASF dual-hosted git repository.

szaszm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 04da95a7f24ea698fcb8d17bb0e08dd777aff774
Author: Martin Zink <ma...@apache.org>
AuthorDate: Thu Jan 5 19:42:18 2023 +0100

    MINIFICPP-1697 Give a better error message on unsupported algorithms in HashContent
    
    Closes #1479
    Signed-off-by: Marton Szasz <sz...@apache.org>
---
 .../standard-processors/processors/HashContent.cpp | 35 ++++++++++------------
 .../standard-processors/processors/HashContent.h   |  2 +-
 .../tests/unit/HashContentTest.cpp                 | 18 +++++++----
 3 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/extensions/standard-processors/processors/HashContent.cpp b/extensions/standard-processors/processors/HashContent.cpp
index 3c6ecfb79..3852ff542 100644
--- a/extensions/standard-processors/processors/HashContent.cpp
+++ b/extensions/standard-processors/processors/HashContent.cpp
@@ -32,6 +32,8 @@
 #include "core/FlowFile.h"
 #include "core/Resource.h"
 
+#include "range/v3/view.hpp"
+
 namespace org::apache::nifi::minifi::processors {
 
 const core::Property HashContent::HashAttribute("Hash Attribute", "Attribute to store checksum to", "Checksum");
@@ -47,21 +49,20 @@ void HashContent::initialize() {
 }
 
 void HashContent::onSchedule(core::ProcessContext *context, core::ProcessSessionFactory* /*sessionFactory*/) {
-  std::string value;
-
-  attrKey_ = (context->getProperty(HashAttribute.getName(), value)) ? value : "Checksum";
-  algoName_ = (context->getProperty(HashAlgorithm.getName(), value)) ? value : "SHA256";
-
-  if (context->getProperty(FailOnEmpty.getName(), value)) {
-    failOnEmpty_ = utils::StringUtils::toBool(value).value_or(false);
-  } else {
-    failOnEmpty_ = false;
+  context->getProperty(HashAttribute.getName(), attrKey_);
+  context->getProperty(FailOnEmpty.getName(), failOnEmpty_);
+
+  {
+    std::string algo_name;
+    context->getProperty(HashAlgorithm.getName(), algo_name);
+    std::transform(algo_name.begin(), algo_name.end(), algo_name.begin(), ::toupper);
+    std::erase(algo_name, '-');
+    if (!HashAlgos.contains(algo_name)) {
+      const auto supported_algorithms = ranges::views::keys(HashAlgos) | ranges::views::join(std::string_view(", ")) | ranges::to<std::string>();
+      throw Exception(PROCESS_SCHEDULE_EXCEPTION, algo_name + " is not supported, supported algorithms are: " + supported_algorithms);
+    }
+    algorithm_ = HashAlgos.at(algo_name);
   }
-
-  std::transform(algoName_.begin(), algoName_.end(), algoName_.begin(), ::toupper);
-
-  // Erase '-' to make sha-256 and sha-1 work, too
-  algoName_.erase(std::remove(algoName_.begin(), algoName_.end(), '-'), algoName_.end());
 }
 
 void HashContent::onTrigger(core::ProcessContext *, core::ProcessSession *session) {
@@ -80,11 +81,7 @@ void HashContent::onTrigger(core::ProcessContext *, core::ProcessSession *sessio
 
   logger_->log_trace("attempting read");
   session->read(flowFile, [&flowFile, this](const std::shared_ptr<io::InputStream>& stream) {
-    // This throws in case algo is not found, but that's fine
-    logger_->log_trace("Searching for %s", algoName_);
-    auto algo = HashAlgos.at(algoName_);
-
-    const auto& ret_val = algo(stream);
+    const auto& ret_val = algorithm_(stream);
 
     flowFile->setAttribute(attrKey_, ret_val.first);
 
diff --git a/extensions/standard-processors/processors/HashContent.h b/extensions/standard-processors/processors/HashContent.h
index b7818e6a4..7dbdd56c5 100644
--- a/extensions/standard-processors/processors/HashContent.h
+++ b/extensions/standard-processors/processors/HashContent.h
@@ -161,7 +161,7 @@ class HashContent : public core::Processor {
 
  private:
   std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<HashContent>::getLogger();
-  std::string algoName_;
+  std::function<HashReturnType(const std::shared_ptr<io::InputStream>&)> algorithm_ = SHA256Hash;
   std::string attrKey_;
   bool failOnEmpty_{};
 };
diff --git a/extensions/standard-processors/tests/unit/HashContentTest.cpp b/extensions/standard-processors/tests/unit/HashContentTest.cpp
index d9408a9b5..d0a1f6e4d 100644
--- a/extensions/standard-processors/tests/unit/HashContentTest.cpp
+++ b/extensions/standard-processors/tests/unit/HashContentTest.cpp
@@ -19,23 +19,18 @@
 #ifdef OPENSSL_SUPPORT
 
 #include <array>
-#include <fstream>
-#include <map>
 #include <memory>
 #include <utility>
 #include <string>
-#include <set>
 #include <iostream>
 
 #include "TestBase.h"
 #include "Catch.h"
+#include "SingleProcessorTestController.h"
 #include "TestUtils.h"
-#include "core/Core.h"
 #include "unit/ProvenanceTestHelper.h"
 
-#include "core/FlowFile.h"
 #include "core/Processor.h"
-#include "core/ProcessContext.h"
 #include "core/ProcessSession.h"
 #include "core/ProcessorNode.h"
 
@@ -54,6 +49,8 @@ const char* MD5_CHECKSUM = "4FE8A693C64F93F65C5FAF42DC49AB23";
 const char* SHA1_CHECKSUM = "03840DEB949D6CF0C0A624FA7EBA87FBDBCB7783";
 const char* SHA256_CHECKSUM = "66D5B2CC06203137F8A0E9714638DC1085C57A3F1FA26C8823AE5CF89AB26488";
 
+namespace org::apache::nifi::minifi::processors::test {
+
 TEST_CASE("Test Creation of HashContent", "[HashContentCreate]") {
   TestController testController;
   std::shared_ptr<core::Processor> processor = std::make_shared<org::apache::nifi::minifi::processors::HashContent>("processorname");
@@ -172,4 +169,13 @@ TEST_CASE("TestingFailOnEmptyProperty", "[HashContentPropertiesCheck]") {
     REQUIRE(LogTestController::getInstance().contains("Failure as flow file is empty"));
   }
 }
+
+TEST_CASE("Invalid hash algorithm throws in onSchedule", "[HashContent]") {
+  auto hash_content = std::make_shared<HashContent>("HashContent");
+  minifi::test::SingleProcessorTestController controller{hash_content};
+  hash_content->setProperty(HashContent::HashAlgorithm, "My-Algo");
+  REQUIRE_THROWS_WITH(controller.plan->scheduleProcessor(hash_content), "Process Schedule Operation: MYALGO is not supported, supported algorithms are: MD5, SHA1, SHA256");
+}
+
+}  // namespace org::apache::nifi::minifi::processors::test
 #endif  // OPENSSL_SUPPORT