MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
keyring_handler.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_BOOTSTRAP_SRC_KEYRING_HANDLER_H_
27#define ROUTER_SRC_BOOTSTRAP_SRC_KEYRING_HANDLER_H_
28
32#include "mysqlrouter/utils.h"
33
35 public:
36 bool init(mysql_harness::Config &config, const bool is_service) {
37 ki_.init(config);
40 } else if (ki_.use_master_key_file()) {
42 } else { // prompt password
43 if (is_service) return false;
45 }
46
47 return true;
48 }
49
50 KeyringInfo &get_ki() { return ki_; }
51
52 private:
53 static uint32_t get_router_id(const mysql_harness::Config &config) {
54 uint32_t result = 0; // TODO
55
56 if (config.has_any("metadata_cache")) {
57 const auto &metadata_caches = config.get("metadata_cache");
58 for (const auto &section : metadata_caches) {
59 if (section->has("router_id")) {
60 std::istringstream iss(section->get("router_id"));
61 iss >> result;
62 break;
63 }
64 }
65 }
66 return result;
67 }
68
70 std::string master_key =
71 mysqlrouter::prompt_password("Encryption key for router keyring");
72 if (master_key.length() > mysql_harness::kMaxKeyringKeyLength)
73 throw std::runtime_error("Encryption key is too long");
75 false);
76 }
77
80 ki_.get_master_key_file(), false);
81 }
82
84 const mysql_harness::Config &config) {
86 if (!ki_.read_master_key()) {
88 "Cannot fetch master key using master key reader:" +
90 }
93 ki_.get_master_key(), false);
94 }
95
97};
98
99#endif // ROUTER_SRC_BOOTSTRAP_SRC_KEYRING_HANDLER_H_
Definition: keyring_handler.h:34
void init_keyring_using_external_facility(const mysql_harness::Config &config)
Definition: keyring_handler.h:83
bool init(mysql_harness::Config &config, const bool is_service)
Definition: keyring_handler.h:36
static uint32_t get_router_id(const mysql_harness::Config &config)
Definition: keyring_handler.h:53
KeyringInfo & get_ki()
Definition: keyring_handler.h:50
void init_keyring_using_prompted_password()
Definition: keyring_handler.h:69
void init_keyring_using_master_key_file()
Definition: keyring_handler.h:78
KeyringInfo ki_
Definition: keyring_handler.h:96
KeyringInfo class encapsulates loading and storing master key using master-key-reader and master-key-...
Definition: keyring_info.h:76
void add_router_id_to_env(uint32_t router_id) const
Adds ROUTER_ID variable to environment.
Definition: keyring_info.cc:173
bool use_master_key_file() const noexcept
Checks if mysqlrouter.key and keyring files should be used to store master key.
Definition: keyring_info.cc:190
const std::string & get_master_key_file() const noexcept
Definition: keyring_info.h:129
void init(mysql_harness::Config &config)
Initializes KeyringInfo using data read from Config.
Definition: keyring_info.cc:70
bool read_master_key() noexcept
Reads master key using master_key_reader_;.
Definition: keyring_info.cc:86
bool use_master_key_external_facility() const noexcept
Checks if master-key-reader/master-key-writer should be used to load/store master key.
Definition: keyring_info.cc:186
const std::string & get_master_key_reader() const noexcept
Definition: keyring_info.h:137
const std::string & get_master_key() const noexcept
Definition: keyring_info.h:153
const std::string & get_keyring_file() const noexcept
Definition: keyring_info.h:123
void validate_master_key() const
Checks if master key is correct: it cannot be empty, and cannot be longer than mysql_harness::kMaxKey...
Definition: keyring_info.cc:194
MasterKeyReadError class represents error during reading master key using master-key-reader.
Definition: keyring_info.h:55
Configuration.
Definition: config_parser.h:253
ConstSectionList get(const std::string &section) const
Get a list of sections having a name.
Definition: config_parser.cc:237
bool has_any(std::string_view section) const
Definition: config_parser.cc:230
static const int kMaxKeyringKeyLength
Definition: keyring_manager.h:37
HARNESS_EXPORT bool init_keyring(const std::string &keyring_file_path, const std::string &master_key_path, bool create_if_needed)
Initialize an instance of a keyring to be used in the application from the contents of a file,...
Definition: keyring_manager.cc:145
HARNESS_EXPORT bool init_keyring_with_key(const std::string &keyring_file_path, const std::string &master_key, bool create_if_needed)
Initialize an instance of a keyring to be used in the application from the contents of a file,...
Definition: keyring_manager.cc:186
std::string ROUTER_UTILS_EXPORT prompt_password(const std::string &prompt)
Prompts for a password from the console.
Definition: utils.cc:272
struct result result
Definition: result.h:34
Definition: result.h:30