Kea 2.7.8
database_connection.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/cfg_to_element.h>
13#include <database/db_log.h>
16#include <util/str.h>
17
18#include <boost/algorithm/string.hpp>
19#include <vector>
20
21using namespace isc::asiolink;
22using namespace isc::data;
23using namespace isc::util;
24using namespace std;
25
26namespace isc {
27namespace db {
28
29const time_t DatabaseConnection::MAX_DB_TIME = 2147483647;
30
31std::string
32DatabaseConnection::getParameter(const std::string& name) const {
33 ParameterMap::const_iterator param = parameters_.find(name);
34 if (param == parameters_.end()) {
35 isc_throw(BadValue, "Parameter " << name << " not found");
36 }
37 return (param->second);
38}
39
41DatabaseConnection::parse(const std::string& dbaccess) {
43 std::string dba = dbaccess;
44
45 if (!dba.empty()) {
46 try {
47 vector<string> tokens;
48
49 // Handle the special case of a password which is enclosed in apostrophes.
50 // Such password may include whitespace.
51 std::string password_prefix = "password='";
52 auto password_pos = dba.find(password_prefix);
53 if (password_pos != string::npos) {
54 // Password starts with apostrophe, so let's find ending apostrophe.
55 auto password_end_pos = dba.find('\'', password_pos + password_prefix.length());
56 if (password_end_pos == string::npos) {
57 // No ending apostrophe. This is wrong.
58 isc_throw(InvalidParameter, "Apostrophe (') expected at the end of password");
59 }
60 // Extract the password value. It starts after the password=' prefix and ends
61 // at the position of ending apostrophe.
62 auto password = dba.substr(password_pos + password_prefix.length(),
63 password_end_pos - password_pos - password_prefix.length());
64 // Refuse default passwords.
66 mapped_tokens.insert(make_pair("password", password));
67
68 // We need to erase the password from the access string because the generic
69 // algorithm parsing other parameters requires that there are no whitespaces
70 // within the parameter values.
71 dba.erase(password_pos, password_prefix.length() + password.length() + 2);
72 // Leading or trailing whitespace may remain after the password removal.
73 dba = util::str::trim(dba);
74 // If the password was the only parameter in the access string, there is
75 // nothing more to do.
76 if (dba.empty()) {
77 return (mapped_tokens);
78 }
79 }
80
81 // We need to pass a string to is_any_of, not just char*. Otherwise
82 // there are cryptic warnings on Debian6 running g++ 4.4 in
83 // /usr/include/c++/4.4/bits/stl_algo.h:2178 "array subscript is above
84 // array bounds"
85 boost::split(tokens, dba, boost::is_any_of(string("\t ")));
86 for (auto const& token : tokens) {
87 size_t pos = token.find("=");
88 if (pos != string::npos) {
89 string name = token.substr(0, pos);
90 string value = token.substr(pos + 1);
91 mapped_tokens.insert(make_pair(name, value));
92 } else {
93 isc_throw(InvalidParameter, "Cannot parse " << token
94 << ", expected format is name=value");
95 }
96 }
97 } catch (const std::exception& ex) {
98 // We'd obscure the password if we could parse the access string.
100 throw;
101 }
102 }
103
104 return (mapped_tokens);
105}
106
107std::string
109 // Reconstruct the access string: start of with an empty string, then
110 // work through all the parameters in the original string and add them.
111 std::string access;
112 for (auto const& i : parameters) {
113
114 // Separate second and subsequent tokens are preceded by a space.
115 if (!access.empty()) {
116 access += " ";
117 }
118
119 // Append name of parameter...
120 access += i.first;
121 access += "=";
122
123 // ... and the value, except in the case of the password, where a
124 // redacted value is appended.
125 if (i.first == std::string("password")) {
126 access += "*****";
127 } else {
128 access += i.second;
129 }
130 }
131
132 return (access);
133}
134
135bool
137 std::string readonly_value = "false";
138 try {
139 readonly_value = getParameter("readonly");
140 boost::algorithm::to_lower(readonly_value);
141 } catch (...) {
142 // Parameter "readonly" hasn't been specified so we simply use
143 // the default value of "false".
144 }
145
146 if ((readonly_value != "false") && (readonly_value != "true")) {
147 isc_throw(DbInvalidReadOnly, "invalid value '" << readonly_value
148 << "' specified for boolean parameter 'readonly'");
149 }
150
151 return (readonly_value == "true");
152}
153
154void
155DatabaseConnection::makeReconnectCtl(const std::string& timer_name, unsigned int id) {
156 string type = "unknown";
157 unsigned int retries = 0;
158 unsigned int interval = 0;
159
160 // Assumes that parsing ensures only valid values are present
161 try {
162 type = getParameter("type");
163 } catch (...) {
164 // Wasn't specified so we'll use default of "unknown".
165 }
166
167 std::string parm_str;
168 try {
169 parm_str = getParameter("max-reconnect-tries");
170 retries = boost::lexical_cast<unsigned int>(parm_str);
171 } catch (...) {
172 // Wasn't specified so we'll use default of 0;
173 }
174
175 try {
176 parm_str = getParameter("reconnect-wait-time");
177 interval = boost::lexical_cast<unsigned int>(parm_str);
178 } catch (...) {
179 // Wasn't specified so we'll use default of 0;
180 }
181
182 OnFailAction action = OnFailAction::STOP_RETRY_EXIT;
183 try {
184 parm_str = getParameter("on-fail");
185 action = ReconnectCtl::onFailActionFromText(parm_str);
186 } catch (...) {
187 // Wasn't specified so we'll use default of "stop-retry-exit";
188 }
189
190 reconnect_ctl_ = boost::make_shared<ReconnectCtl>(type, timer_name, retries,
191 interval, action, id);
192}
193
194bool
197 return (DatabaseConnection::db_lost_callback_(db_reconnect_ctl));
198 }
199
200 return (false);
201}
202
203bool
206 return (DatabaseConnection::db_recovered_callback_(db_reconnect_ctl));
207 }
208
209 return (false);
210}
211
212bool
215 return (DatabaseConnection::db_failed_callback_(db_reconnect_ctl));
216 }
217
218 return (false);
219}
220
224
225 for (auto const& param : params) {
226 std::string keyword = param.first;
227 std::string value = param.second;
228
229 if ((keyword == "lfc-interval") ||
230 (keyword == "connect-timeout") ||
231 (keyword == "read-timeout") ||
232 (keyword == "write-timeout") ||
233 (keyword == "tcp-user-timeout") ||
234 (keyword == "reconnect-wait-time") ||
235 (keyword == "max-reconnect-tries") ||
236 (keyword == "port") ||
237 (keyword == "max-row-errors")) {
238 // integer parameters
239 int64_t int_value;
240 try {
241 int_value = boost::lexical_cast<int64_t>(value);
242 result->set(keyword, isc::data::Element::create(int_value));
243 } catch (...) {
245 .arg(keyword).arg(value);
246 }
247 } else if ((keyword == "persist") ||
248 (keyword == "readonly") ||
249 (keyword == "retry-on-startup")) {
250 if (value == "true") {
251 result->set(keyword, isc::data::Element::create(true));
252 } else if (value == "false") {
253 result->set(keyword, isc::data::Element::create(false));
254 } else {
256 .arg(keyword).arg(value);
257 }
258 } else if ((keyword == "type") ||
259 (keyword == "user") ||
260 (keyword == "password") ||
261 (keyword == "host") ||
262 (keyword == "name") ||
263 (keyword == "on-fail") ||
264 (keyword == "trust-anchor") ||
265 (keyword == "cert-file") ||
266 (keyword == "key-file") ||
267 (keyword == "cipher-list")) {
268 result->set(keyword, isc::data::Element::create(value));
269 } else {
271 .arg(keyword).arg(value);
272 }
273 }
274
275 return (result);
276}
277
280 ParameterMap params = parse(dbaccess);
281 return (toElement(params));
282}
283
287bool DatabaseConnection::retry_ = false;
288IOServicePtr DatabaseConnection::io_service_ = IOServicePtr();
289
291
292} // namespace db
293} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
bool configuredReadOnly() const
Convenience method checking if database should be opened with read only access.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
virtual void makeReconnectCtl(const std::string &timer_name, unsigned int id)
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static isc::data::ElementPtr toElement(const ParameterMap &params)
Unparse a parameter map.
static isc::data::ElementPtr toElementDbAccessString(const std::string &dbaccess)
Unparse an access string.
static DbCallback db_recovered_callback_
Optional callback function to invoke if an opened connection recovery succeeded.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool test_mode_
Test mode flag (default false).
static bool retry_
Flag which indicates if the database connection should be retried on fail.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static DbCallback db_failed_callback_
Optional callback function to invoke if an opened connection recovery failed.
static DbCallback db_lost_callback_
Optional callback function to invoke if an opened connection is lost.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
Invalid 'readonly' value specification.
static OnFailAction onFailActionFromText(const std::string &text)
Convert string to action.
We want to reuse the database backend connection and exchange code for other uses,...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
@ DB_INVALID_ACCESS
Definition db_log.h:52
const isc::log::MessageID DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR
Definition db_messages.h:29
const isc::log::MessageID DATABASE_TO_JSON_INTEGER_ERROR
Definition db_messages.h:28
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
const isc::log::MessageID DATABASE_TO_JSON_BOOLEAN_ERROR
Definition db_messages.h:27
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
OnFailAction
Type of action to take on connection loss.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
static void check(const std::string &value)
Check if the value is a default credential.
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition db_log.h:144