From issues-return-104229-apmail-nifi-issues-archive=nifi.apache.org@nifi.apache.org Thu Oct 1 16:01:41 2020 Return-Path: X-Original-To: apmail-nifi-issues-archive@locus.apache.org Delivered-To: apmail-nifi-issues-archive@locus.apache.org Received: from mailroute1-lw-us.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by minotaur.apache.org (Postfix) with ESMTP id 565B91A99D for ; Thu, 1 Oct 2020 16:01:41 +0000 (UTC) Received: from mail.apache.org (localhost [127.0.0.1]) by mailroute1-lw-us.apache.org (ASF Mail Server at mailroute1-lw-us.apache.org) with SMTP id 0460E123792 for ; Thu, 1 Oct 2020 16:01:41 +0000 (UTC) Received: (qmail 49597 invoked by uid 500); 1 Oct 2020 16:01:40 -0000 Delivered-To: apmail-nifi-issues-archive@nifi.apache.org Received: (qmail 49565 invoked by uid 500); 1 Oct 2020 16:01:40 -0000 Mailing-List: contact issues-help@nifi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nifi.apache.org Delivered-To: mailing list issues@nifi.apache.org Received: (qmail 49552 invoked by uid 99); 1 Oct 2020 16:01:40 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Oct 2020 16:01:40 +0000 From: =?utf-8?q?GitBox?= To: issues@nifi.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bnifi-minifi-cpp=5D_fgerlits_commented_on_a_change_?= =?utf-8?q?in_pull_request_=23914=3A_MINIFICPP-1323_Encrypt_sensitive_proper?= =?utf-8?q?ties_using_libsodium?= Message-ID: <160156810060.32230.18346782265115396742.asfpy@gitbox.apache.org> Date: Thu, 01 Oct 2020 16:01:40 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit In-Reply-To: References: fgerlits commented on a change in pull request #914: URL: https://github.com/apache/nifi-minifi-cpp/pull/914#discussion_r498356448 ########## File path: encrypt-config/EncryptConfig.cpp ########## @@ -0,0 +1,150 @@ +/** + * 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 + +#include + +#include "ConfigFile.h" +#include "ConfigFileEncryptor.h" +#include "cxxopts.hpp" +#include "utils/file/FileUtils.h" +#include "utils/OptionalUtils.h" + +namespace { +constexpr const char* CONF_DIRECTORY_NAME = "conf"; +constexpr const char* BOOTSTRAP_FILE_NAME = "bootstrap.conf"; +constexpr const char* MINIFI_PROPERTIES_FILE_NAME = "minifi.properties"; +constexpr const char* ENCRYPTION_KEY_PROPERTY_NAME = "nifi.bootstrap.sensitive.key"; +} // namespace + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace encrypt_config { + +EncryptConfig::EncryptConfig(int argc, char* argv[]) : minifi_home_(parseMinifiHomeFromTheOptions(argc, argv)) { + if (sodium_init() < 0) { + throw std::runtime_error{"Could not initialize the libsodium library!"}; + } +} + +std::string EncryptConfig::parseMinifiHomeFromTheOptions(int argc, char* argv[]) { + cxxopts::Options options("encrypt-config", "Encrypt sensitive minifi properties"); + options.add_options() + ("h,help", "Shows help") + ("m,minifi-home", "The MINIFI_HOME directory", cxxopts::value()); + + auto parse_result = options.parse(argc, argv); + + if (parse_result.count("help")) { + std::cout << options.help() << '\n'; + std::exit(0); + } + + if (parse_result.count("minifi-home")) { + return parse_result["minifi-home"].as(); + } else { + throw std::runtime_error{"Required parameter missing: --minifi-home"}; + } +} + +void EncryptConfig::encryptSensitiveProperties() const { + utils::crypto::Bytes encryption_key = getEncryptionKey(); + encryptSensitiveProperties(encryption_key); +} + +std::string EncryptConfig::bootstrapFilePath() const { + return utils::file::FileUtils::concat_path( + utils::file::FileUtils::concat_path(minifi_home_, CONF_DIRECTORY_NAME), + BOOTSTRAP_FILE_NAME); +} + +std::string EncryptConfig::propertiesFilePath() const { + return utils::file::FileUtils::concat_path( + utils::file::FileUtils::concat_path(minifi_home_, CONF_DIRECTORY_NAME), + MINIFI_PROPERTIES_FILE_NAME); +} + +utils::crypto::Bytes EncryptConfig::getEncryptionKey() const { + encrypt_config::ConfigFile bootstrap_file{std::ifstream{bootstrapFilePath()}}; + utils::optional key_from_bootstrap_file = bootstrap_file.getValue(ENCRYPTION_KEY_PROPERTY_NAME); + + if (key_from_bootstrap_file && !key_from_bootstrap_file->empty()) { + std::string binary_key = hexDecodeAndValidateKey(*key_from_bootstrap_file); + std::cout << "Using the existing encryption key found in " << bootstrapFilePath() << '\n'; + return utils::crypto::stringToBytes(binary_key); + } else { + std::cout << "Generating a new encryption key...\n"; + utils::crypto::Bytes encryption_key = utils::crypto::generateKey(); + writeEncryptionKeyToBootstrapFile(encryption_key); + std::cout << "Wrote the new encryption key to " << bootstrapFilePath() << '\n'; + return encryption_key; + } +} + +std::string EncryptConfig::hexDecodeAndValidateKey(const std::string& key) const { + std::string binary_key = utils::StringUtils::from_hex(key); Review comment: I have added a comment. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: users@infra.apache.org