lordgamez commented on code in PR #1751:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1751#discussion_r1560971140


##########
conf/minifi.properties:
##########
@@ -90,7 +90,7 @@ nifi.content.repository.class.name=DatabaseContentRepository
 #nifi.c2.rest.url=
 #nifi.c2.rest.url.ack=
 #nifi.c2.rest.ssl.context.service=
-nifi.c2.root.classes=DeviceInfoNode,AgentInformation,FlowInformation
+nifi.c2.root.classes=DeviceInfoNode,AgentInformation,FlowInformation,AssetInformation

Review Comment:
   I think this should be added to the C2.md documentation as well under Base 
Options.



##########
extensions/http-curl/tests/C2AssetSyncTest.cpp:
##########


Review Comment:
   This test seems to be missing from the CMakeLists.txt, so it does not run 
with ctest. It needs to be added with add_test 



##########
extensions/http-curl/tests/C2AssetSyncTest.cpp:
##########
@@ -0,0 +1,256 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#undef NDEBUG
+#include <vector>
+#include <string>
+#include <fstream>
+#include <iterator>
+
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+#include "utils/IntegrationTestUtils.h"
+#include "utils/file/FileUtils.h"
+#include "utils/file/AssetManager.h"
+
+class FileProvider : public ServerAwareHandler {
+ public:
+  explicit FileProvider(std::string file_content): 
file_content_(std::move(file_content)) {}
+
+  bool handleGet(CivetServer* /*server*/, struct mg_connection* conn) override 
{
+    mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: "
+                    "text/plain\r\nContent-Length: %lu\r\nConnection: 
close\r\n\r\n",
+              file_content_.length());
+    mg_printf(conn, "%s", file_content_.c_str());
+    return true;
+  }
+
+ private:
+  std::string file_content_;
+};
+
+class C2HeartbeatHandler : public HeartbeatHandler {
+ public:
+  using HeartbeatHandler::HeartbeatHandler;
+  using AssetDescription = 
org::apache::nifi::minifi::utils::file::AssetDescription;
+
+  void handleHeartbeat(const rapidjson::Document& root, struct mg_connection* 
conn) override {
+    std::string hb_str;
+    {
+      rapidjson::StringBuffer buffer;
+      rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+      root.Accept(writer);
+
+      hb_str = std::string{buffer.GetString(), buffer.GetSize()};
+    }
+    auto& asset_info_node = root["assetInfo"];
+    auto& asset_hash_node = asset_info_node["hash"];
+    std::string asset_hash{asset_hash_node.GetString(), 
asset_hash_node.GetStringLength()};
+
+    std::vector<C2Operation> operations;
+    {
+      std::lock_guard guard(asset_mtx_);
+      agent_asset_hash_ = asset_hash;
+      if (asset_hash != assetHash()) {
+        std::unordered_map<std::string, std::string> args;
+        for (auto& asset : expected_assets_) {
+          args[asset.id + ".path"] = asset.path;
+          args[asset.id + ".url"] = asset.url;
+        }
+        operations.push_back(C2Operation{
+          .operation = "sync",
+          .operand = "asset",
+          .operation_id = std::to_string(next_op_id_++),
+          .args = std::move(args)
+        });
+      }
+    }
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void addAsset(std::string id, std::string path, std::string url) {
+    std::lock_guard guard(asset_mtx_);
+    expected_assets_.insert(AssetDescription{
+      .id = id,
+      .path = path,
+      .url = url
+    });
+  }
+
+  void removeAsset(std::string id) {
+    std::lock_guard guard{asset_mtx_};
+    expected_assets_.erase(AssetDescription{.id = id, .path = {}, .url = {}});
+  }
+
+  std::optional<std::string> getAgentAssetHash() const {
+    std::lock_guard lock(asset_mtx_);
+    return agent_asset_hash_;
+  }
+
+  std::string assetHash() const {

Review Comment:
   I think this could be renamed to something like `calculateAssetHash` because 
having a `getAgentAssetHash` and an `assetHash` is a bit confusing IMO



##########
extensions/http-curl/tests/C2AssetSyncTest.cpp:
##########
@@ -0,0 +1,256 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#undef NDEBUG
+#include <vector>
+#include <string>
+#include <fstream>
+#include <iterator>
+
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+#include "utils/IntegrationTestUtils.h"
+#include "utils/file/FileUtils.h"
+#include "utils/file/AssetManager.h"
+
+class FileProvider : public ServerAwareHandler {
+ public:
+  explicit FileProvider(std::string file_content): 
file_content_(std::move(file_content)) {}
+
+  bool handleGet(CivetServer* /*server*/, struct mg_connection* conn) override 
{
+    mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: "
+                    "text/plain\r\nContent-Length: %lu\r\nConnection: 
close\r\n\r\n",
+              file_content_.length());
+    mg_printf(conn, "%s", file_content_.c_str());
+    return true;
+  }
+
+ private:
+  std::string file_content_;
+};
+
+class C2HeartbeatHandler : public HeartbeatHandler {
+ public:
+  using HeartbeatHandler::HeartbeatHandler;
+  using AssetDescription = 
org::apache::nifi::minifi::utils::file::AssetDescription;
+
+  void handleHeartbeat(const rapidjson::Document& root, struct mg_connection* 
conn) override {
+    std::string hb_str;
+    {
+      rapidjson::StringBuffer buffer;
+      rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+      root.Accept(writer);
+
+      hb_str = std::string{buffer.GetString(), buffer.GetSize()};
+    }

Review Comment:
   This could be made into a single use lambda call returning the hb_str result



##########
extensions/http-curl/tests/C2AssetSyncTest.cpp:
##########
@@ -0,0 +1,256 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#undef NDEBUG
+#include <vector>
+#include <string>
+#include <fstream>
+#include <iterator>
+
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+#include "utils/IntegrationTestUtils.h"
+#include "utils/file/FileUtils.h"
+#include "utils/file/AssetManager.h"
+
+class FileProvider : public ServerAwareHandler {
+ public:
+  explicit FileProvider(std::string file_content): 
file_content_(std::move(file_content)) {}
+
+  bool handleGet(CivetServer* /*server*/, struct mg_connection* conn) override 
{
+    mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: "
+                    "text/plain\r\nContent-Length: %lu\r\nConnection: 
close\r\n\r\n",
+              file_content_.length());
+    mg_printf(conn, "%s", file_content_.c_str());
+    return true;
+  }
+
+ private:
+  std::string file_content_;
+};
+
+class C2HeartbeatHandler : public HeartbeatHandler {
+ public:
+  using HeartbeatHandler::HeartbeatHandler;
+  using AssetDescription = 
org::apache::nifi::minifi::utils::file::AssetDescription;
+
+  void handleHeartbeat(const rapidjson::Document& root, struct mg_connection* 
conn) override {
+    std::string hb_str;
+    {
+      rapidjson::StringBuffer buffer;
+      rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+      root.Accept(writer);
+
+      hb_str = std::string{buffer.GetString(), buffer.GetSize()};
+    }
+    auto& asset_info_node = root["assetInfo"];
+    auto& asset_hash_node = asset_info_node["hash"];
+    std::string asset_hash{asset_hash_node.GetString(), 
asset_hash_node.GetStringLength()};
+
+    std::vector<C2Operation> operations;
+    {
+      std::lock_guard guard(asset_mtx_);
+      agent_asset_hash_ = asset_hash;
+      if (asset_hash != assetHash()) {
+        std::unordered_map<std::string, std::string> args;
+        for (auto& asset : expected_assets_) {
+          args[asset.id + ".path"] = asset.path;
+          args[asset.id + ".url"] = asset.url;
+        }
+        operations.push_back(C2Operation{
+          .operation = "sync",
+          .operand = "asset",
+          .operation_id = std::to_string(next_op_id_++),
+          .args = std::move(args)
+        });
+      }
+    }
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void addAsset(std::string id, std::string path, std::string url) {
+    std::lock_guard guard(asset_mtx_);
+    expected_assets_.insert(AssetDescription{
+      .id = id,
+      .path = path,
+      .url = url

Review Comment:
   These should be moved instead if these strings are passed as value params 
(in removeAsset as well)



##########
minifi_main/MiNiFiMain.cpp:
##########
@@ -400,9 +401,10 @@ int main(int argc, char **argv) {
           .sensitive_properties_encryptor = 
utils::crypto::EncryptionProvider::createSensitivePropertiesEncryptor(minifiHome)
       }, nifi_configuration_class_name);
 
-    std::vector<std::shared_ptr<core::RepositoryMetricsSource>> 
repo_metric_sources{prov_repo, flow_repo, content_repo};
-    auto metrics_publisher_store = 
std::make_unique<minifi::state::MetricsPublisherStore>(configure, 
repo_metric_sources, flow_configuration);
+    auto asset_manager = 
std::make_shared<utils::file::AssetManager>(*configure);
 
+    std::vector<std::shared_ptr<core::RepositoryMetricsSource>> 
repo_metric_sources{prov_repo, flow_repo, content_repo};
+    auto metrics_publisher_store = 
std::make_unique<minifi::state::MetricsPublisherStore>(configure, 
repo_metric_sources, flow_configuration, asset_manager);
     const auto controller = std::make_unique<minifi::FlowController>(
         prov_repo, flow_repo, configure, std::move(flow_configuration), 
content_repo, std::move(metrics_publisher_store), filesystem, request_restart);

Review Comment:
   It seems that the asset manager is not passed here and will never be 
forwarded to the C2Agent



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to