2424#include < stdexcept>
2525
2626#include " ../core-framework/include/Defaults.h"
27- #include " ConfigFile.h"
28- #include " ConfigFileEncryptor.h"
2927#include " FlowConfigEncryptor.h"
30- #include " utils/Enum.h"
28+ #include " PropertiesFileEncryptor.h"
29+ #include " properties/Properties.h"
30+ #include " properties/PropertiesFile.h"
3131#include " utils/file/FileUtils.h"
3232
3333namespace {
3434constexpr std::string_view ENCRYPTION_KEY_PROPERTY_NAME = " nifi.bootstrap.sensitive.key" ;
3535constexpr std::string_view SENSITIVE_PROPERTIES_KEY_PROPERTY_NAME = " nifi.bootstrap.sensitive.properties.key" ;
3636
37+ namespace minifi = org::apache::nifi::minifi;
38+
3739std::string readFile (const std::filesystem::path& file_path) {
3840 try {
3941 std::ifstream file_stream{file_path, std::ios::binary};
@@ -43,11 +45,27 @@ std::string readFile(const std::filesystem::path& file_path) {
4345 throw std::runtime_error (" Error while reading file \" " + file_path.string () + " \" " );
4446 }
4547}
48+
49+ uint32_t encryptSensitiveValuesInMinifiPropertiesFile (const std::filesystem::path& file_path, const std::vector<std::string>& sensitive_properties,
50+ const minifi::encrypt_config::EncryptionKeys& keys) {
51+ minifi::PropertiesFile properties_file{std::ifstream{file_path}};
52+ if (properties_file.size () == 0 ) {
53+ std::cout << " Properties file " + file_path.string () + " is missing or empty!\n " ;
54+ return 0 ;
55+ }
56+
57+ uint32_t num_properties_encrypted = minifi::encrypt_config::encryptSensitivePropertiesInFile (properties_file, sensitive_properties, keys);
58+ if (num_properties_encrypted > 0 ) {
59+ properties_file.writeTo (file_path);
60+ std::cout << " Encrypted " << num_properties_encrypted << " sensitive " << (num_properties_encrypted == 1 ? " property" : " properties" ) << " in " << file_path.string () << ' \n ' ;
61+ }
62+ return num_properties_encrypted;
63+ }
4664} // namespace
4765
4866namespace org ::apache::nifi::minifi::encrypt_config {
4967
50- EncryptConfig::EncryptConfig (const std::string& minifi_home) : minifi_home_(minifi_home) {
68+ EncryptConfig::EncryptConfig (std::filesystem::path minifi_home) : minifi_home_(std::move( minifi_home)), prev_current_path_(std::filesystem::current_path() ) {
5169 if (sodium_init () < 0 ) {
5270 // encryption/decryption depends on the libsodium library which needs to be initialized
5371 throw std::runtime_error{" Could not initialize the libsodium library!" };
@@ -56,8 +74,12 @@ EncryptConfig::EncryptConfig(const std::string& minifi_home) : minifi_home_(mini
5674 std::filesystem::current_path (minifi_home_);
5775}
5876
77+ EncryptConfig::~EncryptConfig () {
78+ std::filesystem::current_path (prev_current_path_);
79+ }
80+
5981bool EncryptConfig::isReEncrypting () const {
60- encrypt_config::ConfigFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
82+ PropertiesFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
6183
6284 std::string decryption_key_name = utils::string::join_pack (ENCRYPTION_KEY_PROPERTY_NAME , " .old" );
6385 std::optional<std::string> decryption_key_hex = bootstrap_file.getValue (decryption_key_name);
@@ -66,7 +88,7 @@ bool EncryptConfig::isReEncrypting() const {
6688}
6789
6890std::filesystem::path EncryptConfig::flowConfigPath () const {
69- encrypt_config::ConfigFile properties_file{std::ifstream{propertiesFilePath ()}};
91+ PropertiesFile properties_file{std::ifstream{propertiesFilePath ()}};
7092 std::optional<std::filesystem::path> config_path{properties_file.getValue (Configure::nifi_flow_configuration_file)};
7193 if (!config_path) {
7294 config_path = utils::file::PathUtils::resolve (minifi_home_, " conf/config.yml" );
@@ -123,7 +145,7 @@ std::filesystem::path EncryptConfig::propertiesFilePath() const {
123145}
124146
125147EncryptionKeys EncryptConfig::getEncryptionKeys (std::string_view property_name) const {
126- encrypt_config::ConfigFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
148+ PropertiesFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
127149
128150 std::string decryption_key_name = utils::string::join_pack (property_name, " .old" );
129151 std::optional<std::string> decryption_key_hex = bootstrap_file.getValue (decryption_key_name);
@@ -168,7 +190,7 @@ std::string EncryptConfig::hexDecodeAndValidateKey(const std::string& key, const
168190
169191void EncryptConfig::writeEncryptionKeyToBootstrapFile (const std::string& encryption_key_name, const utils::crypto::Bytes& encryption_key) const {
170192 std::string key_encoded = utils::string::to_hex (utils::crypto::bytesToString (encryption_key));
171- encrypt_config::ConfigFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
193+ PropertiesFile bootstrap_file{std::ifstream{bootstrapFilePath ()}};
172194
173195 if (bootstrap_file.hasValue (encryption_key_name)) {
174196 bootstrap_file.update (encryption_key_name, key_encoded);
@@ -180,22 +202,28 @@ void EncryptConfig::writeEncryptionKeyToBootstrapFile(const std::string& encrypt
180202}
181203
182204void EncryptConfig::encryptSensitiveValuesInMinifiProperties () const {
183- EncryptionKeys keys = getEncryptionKeys (ENCRYPTION_KEY_PROPERTY_NAME );
205+ const auto base_properties_file = propertiesFilePath ();
206+ if (!utils::file::exists (base_properties_file)) {
207+ std::cout << " The minifi properties file " << base_properties_file.string () << " does not exist. Did you specify the minifi home directory correctly?\n " ;
208+ return ;
209+ }
184210
185- encrypt_config::ConfigFile properties_file{std::ifstream{propertiesFilePath ()}};
186- if (properties_file.size () == 0 ) {
187- throw std::runtime_error{" Properties file " + propertiesFilePath ().string () + " not found!" };
211+ const std::vector<std::string> sensitive_properties = getSensitiveProperties (base_properties_file);
212+ const EncryptionKeys keys = getEncryptionKeys (ENCRYPTION_KEY_PROPERTY_NAME );
213+
214+ uint32_t num_properties_encrypted = encryptSensitiveValuesInMinifiPropertiesFile (base_properties_file, sensitive_properties, keys);
215+
216+ const auto extra_properties_files_dir = properties::extraPropertiesFilesDirName (base_properties_file);
217+ const auto logger = core::logging::LoggerFactory<EncryptConfig>::getLogger ();
218+ const auto extra_properties_file_names = properties::getExtraPropertiesFileNames (extra_properties_files_dir, logger);
219+
220+ for (const auto & file_name : extra_properties_file_names) {
221+ num_properties_encrypted += encryptSensitiveValuesInMinifiPropertiesFile (extra_properties_files_dir / file_name, sensitive_properties, keys);
188222 }
189223
190- uint32_t num_properties_encrypted = encryptSensitivePropertiesInFile (properties_file, keys);
191224 if (num_properties_encrypted == 0 ) {
192- std::cout << " Could not find any (new) sensitive properties to encrypt in " << propertiesFilePath () << ' \n ' ;
193- return ;
225+ std::cout << " Could not find any (new) sensitive properties to encrypt.\n " ;
194226 }
195-
196- properties_file.writeTo (propertiesFilePath ());
197- std::cout << " Encrypted " << num_properties_encrypted << " sensitive "
198- << (num_properties_encrypted == 1 ? " property" : " properties" ) << " in " << propertiesFilePath () << ' \n ' ;
199227}
200228
201229void EncryptConfig::encryptSensitiveValuesInFlowConfig (
0 commit comments