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


##########
encrypt-config/EncryptConfig.cpp:
##########
@@ -56,8 +74,12 @@ EncryptConfig::EncryptConfig(const std::string& minifi_home) 
: minifi_home_(mini
   std::filesystem::current_path(minifi_home_);
 }
 
+EncryptConfig::~EncryptConfig() {
+  std::filesystem::current_path(prev_current_path_);

Review Comment:
   It may be better to use the noexcept overload with error_code as the second 
parameter that can be checked and logged. Same could be applied to the 
constructor.



##########
encrypt-config/PropertiesFileEncryptor.cpp:
##########
@@ -15,33 +15,51 @@
  * limitations under the License.
  */
 
-#include "ConfigFileEncryptor.h"
+#include "PropertiesFileEncryptor.h"
 
 #include <iostream>
 #include <optional>
 #include <string>
 
+#include "properties/Configuration.h"
+#include "properties/Properties.h"
 #include "utils/StringUtils.h"
 
-namespace org::apache::nifi::minifi::encrypt_config {
-
+namespace {
 bool isEncrypted(const std::optional<std::string>& encryption_type) {
   return encryption_type && !encryption_type->empty() && *encryption_type  != 
"plaintext";
 }
+}  // namespace
+
+namespace org::apache::nifi::minifi::encrypt_config {
+
+std::vector<std::string> getSensitiveProperties(const std::filesystem::path& 
properties_file_path) {
+  auto minifi_properties = 
PropertiesImpl{PropertiesImpl::PersistTo::MultipleFiles, "MiNiFi properties"};
+  minifi_properties.loadConfigureFile(properties_file_path);
+
+  auto sensitive_properties = 
Configuration::getSensitiveProperties([&minifi_properties](const std::string& 
property_name) {
+    return minifi_properties.getString(property_name);
+  });
+  const auto not_found = [&minifi_properties](const std::string& 
property_name) { return 
!minifi_properties.getString(property_name).has_value(); };
+  const auto new_end = std::remove_if(sensitive_properties.begin(), 
sensitive_properties.end(), not_found);
+  sensitive_properties.erase(new_end, sensitive_properties.end());

Review Comment:
   Could this be replaced with `std::erase_if`?



##########
encrypt-config/PropertiesFileEncryptor.cpp:
##########
@@ -15,33 +15,51 @@
  * limitations under the License.
  */
 
-#include "ConfigFileEncryptor.h"
+#include "PropertiesFileEncryptor.h"
 
 #include <iostream>
 #include <optional>
 #include <string>
 
+#include "properties/Configuration.h"
+#include "properties/Properties.h"
 #include "utils/StringUtils.h"
 
-namespace org::apache::nifi::minifi::encrypt_config {
-
+namespace {
 bool isEncrypted(const std::optional<std::string>& encryption_type) {
   return encryption_type && !encryption_type->empty() && *encryption_type  != 
"plaintext";
 }
+}  // namespace
+
+namespace org::apache::nifi::minifi::encrypt_config {
+
+std::vector<std::string> getSensitiveProperties(const std::filesystem::path& 
properties_file_path) {
+  auto minifi_properties = 
PropertiesImpl{PropertiesImpl::PersistTo::MultipleFiles, "MiNiFi properties"};
+  minifi_properties.loadConfigureFile(properties_file_path);
+
+  auto sensitive_properties = 
Configuration::getSensitiveProperties([&minifi_properties](const std::string& 
property_name) {
+    return minifi_properties.getString(property_name);
+  });
+  const auto not_found = [&minifi_properties](const std::string& 
property_name) { return 
!minifi_properties.getString(property_name).has_value(); };
+  const auto new_end = std::remove_if(sensitive_properties.begin(), 
sensitive_properties.end(), not_found);
+  sensitive_properties.erase(new_end, sensitive_properties.end());
+
+  return sensitive_properties;
+}
 
-uint32_t encryptSensitivePropertiesInFile(ConfigFile& config_file, const 
utils::crypto::Bytes & encryption_key) {
-  return encryptSensitivePropertiesInFile(config_file, EncryptionKeys{{}, 
encryption_key});
+uint32_t encryptSensitivePropertiesInFile(PropertiesFile& properties_file, 
const std::vector<std::string>& sensitive_properties, const 
utils::crypto::Bytes & encryption_key) {
+  return encryptSensitivePropertiesInFile(properties_file, 
sensitive_properties, EncryptionKeys{{}, encryption_key});
 }
 
-uint32_t encryptSensitivePropertiesInFile(ConfigFile& config_file, const 
EncryptionKeys& keys) {
+uint32_t encryptSensitivePropertiesInFile(PropertiesFile& properties_file, 
const std::vector<std::string>& sensitive_properties, const EncryptionKeys& 
keys) {
   int num_properties_encrypted = 0;

Review Comment:
   This should be `uint32_t` to match the return type.



##########
encrypt-config/tests/EncryptConfigTests.cpp:
##########
@@ -0,0 +1,64 @@
+/**
+ * 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.
+ */
+
+#include "EncryptConfig.h"
+#include "properties/PropertiesFile.h"
+#include "unit/Catch.h"
+#include "unit/TestBase.h"
+
+namespace {
+constexpr std::string llm_api_key = "llm.api.key";

Review Comment:
   I think we usually use UPPER_CASE formatting for constants.



-- 
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