| 57 | } |
| 58 | |
| 59 | void EncryptConfig::encryptFlowConfig() const { |
| 60 | encrypt_config::ConfigFile properties_file{std::ifstream{propertiesFilePath()}}; |
| 61 | utils::optional<std::string> config_path = properties_file.getValue(Configure::nifi_flow_configuration_file); |
| 62 | if (!config_path) { |
| 63 | config_path = utils::file::PathUtils::resolve(minifi_home_, "conf/config.yml"); |
| 64 | std::cout << "Couldn't find path of configuration file, using default: \"" << *config_path << "\"\n"; |
| 65 | } else { |
| 66 | config_path = utils::file::PathUtils::resolve(minifi_home_, *config_path); |
| 67 | std::cout << "Encrypting flow configuration file: \"" << *config_path << "\"\n"; |
| 68 | } |
| 69 | std::string config_content; |
| 70 | try { |
| 71 | std::ifstream config_file{*config_path, std::ios::binary}; |
| 72 | config_file.exceptions(std::ios::failbit | std::ios::badbit); |
| 73 | config_content = std::string{std::istreambuf_iterator<char>(config_file), {}}; |
| 74 | } catch (...) { |
| 75 | throw std::runtime_error("Error while reading flow configuration file \"" + *config_path + "\""); |
| 76 | } |
| 77 | try { |
| 78 | utils::crypto::decrypt(config_content, keys_.encryption_key); |
| 79 | std::cout << "Flow config file is already properly encrypted.\n"; |
| 80 | return; |
| 81 | } catch (const std::exception&) {} |
| 82 | |
| 83 | if (utils::crypto::isEncrypted(config_content)) { |
| 84 | if (!keys_.old_key) { |
| 85 | throw std::runtime_error("Config file is encrypted, but no old encryption key is set."); |
| 86 | } |
| 87 | std::cout << "Trying to decrypt flow config file using the old key ...\n"; |
| 88 | try { |
| 89 | config_content = utils::crypto::decrypt(config_content, *keys_.old_key); |
| 90 | } catch (const std::exception&) { |
| 91 | throw std::runtime_error("Flow config is encrypted, but couldn't be decrypted."); |
| 92 | } |
| 93 | } else { |
| 94 | std::cout << "Flow config file is not encrypted, using as-is.\n"; |
| 95 | } |
| 96 | |
| 97 | std::string encrypted_content = utils::crypto::encrypt(config_content, keys_.encryption_key); |
| 98 | try { |
| 99 | std::ofstream encrypted_file{*config_path, std::ios::binary}; |
| 100 | encrypted_file.exceptions(std::ios::failbit | std::ios::badbit); |
| 101 | encrypted_file << encrypted_content; |
| 102 | } catch (...) { |
| 103 | throw std::runtime_error("Error while writing encrypted flow configuration file \"" + *config_path + "\""); |
| 104 | } |
| 105 | std::cout << "Successfully encrypted flow configuration file: \"" << *config_path << "\"\n"; |
| 106 | } |
| 107 | |
| 108 | std::string EncryptConfig::bootstrapFilePath() const { |
| 109 | return utils::file::concat_path(minifi_home_, DEFAULT_BOOTSTRAP_FILE); |