You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/03/23 16:54:02 UTC

[GitHub] [nifi-minifi-cpp] lordgamez opened a new pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

lordgamez opened a new pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285


   https://issues.apache.org/jira/browse/MINIFICPP-1779
   
   ---------------------------------------------
   Thank you for submitting a contribution to Apache NiFi - MiNiFi C++.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
   
   - [ ] Does your PR title start with MINIFICPP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   - [ ] If applicable, have you updated the LICENSE file?
   - [ ] If applicable, have you updated the NOTICE file?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions CI results for build issues and submit an update to your PR as soon as possible.
   


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r835738913



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * 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 <string>
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+ private:
+  mutable std::mutex mutex_;
+  std::unordered_set<std::string> acknowledged_operations_;
+};
+
+class MultipleC2CommandHandler: public HeartbeatHandler {
+ public:
+  explicit MultipleC2CommandHandler(AckAuditor& ack_auditor, std::shared_ptr<minifi::Configure> configuration)
+    : HeartbeatHandler(std::move(configuration)),
+      ack_auditor_(ack_auditor) {
+  }
+
+  void handleHeartbeat(const rapidjson::Document&, struct mg_connection * conn) override {
+    std::vector<C2Operation> operations{{"DESCRIBE", "manifest", "889345", {}}, {"DESCRIBE", "corecomponentstate", "889346", {}}};
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void handleAcknowledge(const rapidjson::Document& root) override {
+    if (is_odd_ack) {
+      verifyJsonHasAgentManifest(root);
+      is_odd_ack = false;
+    } else {
+      assert(root.HasMember("corecomponentstate"));
+      is_odd_ack = true;
+    }
+    if (root.IsObject() && root.HasMember("operationId")) {
+      ack_auditor_.addAck(root["operationId"].GetString());
+    }
+  }
+
+ private:
+  AckAuditor& ack_auditor_;
+  bool is_odd_ack = true;

Review comment:
       in that case, we could maintain a queue of ack verifier functions, that would make it seamless to add new operations




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] lordgamez commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
lordgamez commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r839525095



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,132 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+#include <functional>
+
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(acknowledged_operations_mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(acknowledged_operations_mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+  void addVerifier(std::function<void(const rapidjson::Document&)> verifier) {
+    std::lock_guard<std::mutex> guard(verify_ack_mutex_);
+    ack_verifiers_.push_back(std::move(verifier));
+  }
+
+  void verifyAck(const rapidjson::Document& root) {
+    std::lock_guard<std::mutex> guard(verify_ack_mutex_);
+    if (ack_verifiers_.empty()) {
+      assert(false);
+    }
+
+    if (next_verifier_index_ >= ack_verifiers_.size()) {
+      ack_verifiers_[0](root);
+      next_verifier_index_ = 1;
+    } else {
+      ack_verifiers_[next_verifier_index_](root);
+      ++next_verifier_index_;
+    }

Review comment:
       Updated in 7752fe67bfe1ede48c5c9e8e76b50868a39bd139




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni closed pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
adamdebreceni closed pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285


   


-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] fgerlits commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
fgerlits commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r839488491



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,132 @@
+/**
+ *
+ * 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 <string>
+#include <vector>
+#include <functional>
+
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(acknowledged_operations_mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(acknowledged_operations_mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+  void addVerifier(std::function<void(const rapidjson::Document&)> verifier) {
+    std::lock_guard<std::mutex> guard(verify_ack_mutex_);
+    ack_verifiers_.push_back(std::move(verifier));
+  }
+
+  void verifyAck(const rapidjson::Document& root) {
+    std::lock_guard<std::mutex> guard(verify_ack_mutex_);
+    if (ack_verifiers_.empty()) {
+      assert(false);
+    }
+
+    if (next_verifier_index_ >= ack_verifiers_.size()) {
+      ack_verifiers_[0](root);
+      next_verifier_index_ = 1;
+    } else {
+      ack_verifiers_[next_verifier_index_](root);
+      ++next_verifier_index_;
+    }

Review comment:
       nitpicking, but I would prefer this:
   ```suggestion
       ack_verifiers_[next_verifier_index_](root);
   
       ++next_verifier_index_;
       if (next_verifier_index_ >= ack_verifiers_.size()) {
         next_verifier_index_ = 0;
       }
   ```




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] lordgamez commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
lordgamez commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r836190819



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * 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 <string>
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+ private:
+  mutable std::mutex mutex_;
+  std::unordered_set<std::string> acknowledged_operations_;
+};
+
+class MultipleC2CommandHandler: public HeartbeatHandler {
+ public:
+  explicit MultipleC2CommandHandler(AckAuditor& ack_auditor, std::shared_ptr<minifi::Configure> configuration)
+    : HeartbeatHandler(std::move(configuration)),
+      ack_auditor_(ack_auditor) {
+  }
+
+  void handleHeartbeat(const rapidjson::Document&, struct mg_connection * conn) override {
+    std::vector<C2Operation> operations{{"DESCRIBE", "manifest", "889345", {}}, {"DESCRIBE", "corecomponentstate", "889346", {}}};
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void handleAcknowledge(const rapidjson::Document& root) override {
+    if (is_odd_ack) {
+      verifyJsonHasAgentManifest(root);
+      is_odd_ack = false;
+    } else {
+      assert(root.HasMember("corecomponentstate"));
+      is_odd_ack = true;
+    }
+    if (root.IsObject() && root.HasMember("operationId")) {
+      ack_auditor_.addAck(root["operationId"].GetString());
+    }
+  }
+
+ private:
+  AckAuditor& ack_auditor_;
+  bool is_odd_ack = true;

Review comment:
       Updated in 1ade7ca6ec6c6feb7498f4b3be7d86f4e78b1fdb




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r835197229



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * 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 <string>
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+ private:
+  mutable std::mutex mutex_;
+  std::unordered_set<std::string> acknowledged_operations_;
+};
+
+class MultipleC2CommandHandler: public HeartbeatHandler {
+ public:
+  explicit MultipleC2CommandHandler(AckAuditor& ack_auditor, std::shared_ptr<minifi::Configure> configuration)
+    : HeartbeatHandler(std::move(configuration)),
+      ack_auditor_(ack_auditor) {
+  }
+
+  void handleHeartbeat(const rapidjson::Document&, struct mg_connection * conn) override {
+    std::vector<C2Operation> operations{{"DESCRIBE", "manifest", "889345", {}}, {"DESCRIBE", "corecomponentstate", "889346", {}}};
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void handleAcknowledge(const rapidjson::Document& root) override {
+    if (is_odd_ack) {
+      verifyJsonHasAgentManifest(root);
+      is_odd_ack = false;
+    } else {
+      assert(root.HasMember("corecomponentstate"));
+      is_odd_ack = true;
+    }
+    if (root.IsObject() && root.HasMember("operationId")) {
+      ack_auditor_.addAck(root["operationId"].GetString());
+    }
+  }
+
+ private:
+  AckAuditor& ack_auditor_;
+  bool is_odd_ack = true;

Review comment:
       checking validity based on parity seems strange, shouldn't we branch based on the operation id?




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] lordgamez commented on a change in pull request #1285: MINIFICPP-1779 Verify multiple C2 commands in HB response

Posted by GitBox <gi...@apache.org>.
lordgamez commented on a change in pull request #1285:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1285#discussion_r835213387



##########
File path: extensions/http-curl/tests/C2MultipleCommandsTest.cpp
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * 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 <string>
+#include "TestBase.h"
+#include "Catch.h"
+#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
+
+class AckAuditor {
+ public:
+  void addAck(const std::string& ack) {
+    std::lock_guard<std::mutex> guard(mutex_);
+    acknowledged_operations_.insert(ack);
+  }
+
+  bool isAcknowledged(const std::string& operation_id) const {
+    std::lock_guard<std::mutex> guard(mutex_);
+    return acknowledged_operations_.count(operation_id) > 0;
+  }
+
+ private:
+  mutable std::mutex mutex_;
+  std::unordered_set<std::string> acknowledged_operations_;
+};
+
+class MultipleC2CommandHandler: public HeartbeatHandler {
+ public:
+  explicit MultipleC2CommandHandler(AckAuditor& ack_auditor, std::shared_ptr<minifi::Configure> configuration)
+    : HeartbeatHandler(std::move(configuration)),
+      ack_auditor_(ack_auditor) {
+  }
+
+  void handleHeartbeat(const rapidjson::Document&, struct mg_connection * conn) override {
+    std::vector<C2Operation> operations{{"DESCRIBE", "manifest", "889345", {}}, {"DESCRIBE", "corecomponentstate", "889346", {}}};
+    sendHeartbeatResponse(operations, conn);
+  }
+
+  void handleAcknowledge(const rapidjson::Document& root) override {
+    if (is_odd_ack) {
+      verifyJsonHasAgentManifest(root);
+      is_odd_ack = false;
+    } else {
+      assert(root.HasMember("corecomponentstate"));
+      is_odd_ack = true;
+    }
+    if (root.IsObject() && root.HasMember("operationId")) {
+      ack_auditor_.addAck(root["operationId"].GetString());
+    }
+  }
+
+ private:
+  AckAuditor& ack_auditor_;
+  bool is_odd_ack = true;

Review comment:
       We would like to guarantee that the C2 commands are processed in their order in the request, that's why we are checking this way.




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org