Kea 3.1.1
dbaccess_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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
12
13#include <boost/lexical_cast.hpp>
14
15#include <map>
16#include <string>
17#include <utility>
18
19using namespace std;
20using namespace isc::data;
21
22namespace isc {
23namespace db {
24
25
26// Factory function to build the parser
28 : values_() {
29}
30
31// Parse the configuration and check that the various keywords are consistent.
32void
33DbAccessParser::parse(std::string& access_string,
34 ConstElementPtr database_config) {
35
36 // To cope with incremental updates, the strategy is:
37 // 1. Take a copy of the stored keyword/value pairs.
38 // 2. Update the copy with the passed keywords.
39 // 3. Perform validation checks on the updated keyword/value pairs.
40 // 4. If all is OK, update the stored keyword/value pairs.
41 // 5. Save resulting database access string in the Configuration
42 // Manager.
43
44 // Note only range checks can fail with a database_config from
45 // a flex/bison parser.
46
47 // 1. Take a copy of the stored keyword/value pairs.
48 DatabaseConnection::ParameterMap values_copy = values_;
49
50 int64_t lfc_interval = 0;
51 int64_t connect_timeout = 0;
52 int64_t read_timeout = 0;
53 int64_t write_timeout = 0;
54 int64_t tcp_user_timeout = 0;
55 int64_t port = 0;
56 int64_t max_reconnect_tries = 0;
57 int64_t reconnect_wait_time = 0;
58 int64_t max_row_errors = 0;
59
60 // 2. Update the copy with the passed keywords.
61 for (auto const& param : database_config->mapValue()) {
62 try {
63 if ((param.first == "persist") ||
64 (param.first == "readonly") ||
65 (param.first == "retry-on-startup")) {
66 values_copy[param.first] = (param.second->boolValue() ?
67 "true" : "false");
68
69 } else if (param.first == "lfc-interval") {
70 lfc_interval = param.second->intValue();
71 values_copy[param.first] =
72 boost::lexical_cast<std::string>(lfc_interval);
73
74 } else if (param.first == "connect-timeout") {
75 connect_timeout = param.second->intValue();
76 values_copy[param.first] =
77 boost::lexical_cast<std::string>(connect_timeout);
78
79 } else if (param.first == "read-timeout") {
80 read_timeout = param.second->intValue();
81 values_copy[param.first] =
82 boost::lexical_cast<std::string>(read_timeout);
83
84 } else if (param.first == "write-timeout") {
85 write_timeout = param.second->intValue();
86 values_copy[param.first] =
87 boost::lexical_cast<std::string>(write_timeout);
88
89 } else if (param.first == "tcp-user-timeout") {
90 tcp_user_timeout = param.second->intValue();
91 values_copy[param.first] =
92 boost::lexical_cast<std::string>(tcp_user_timeout);
93
94 } else if (param.first == "max-reconnect-tries") {
95 max_reconnect_tries = param.second->intValue();
96 values_copy[param.first] =
97 boost::lexical_cast<std::string>(max_reconnect_tries);
98
99 } else if (param.first == "reconnect-wait-time") {
100 reconnect_wait_time = param.second->intValue();
101 values_copy[param.first] =
102 boost::lexical_cast<std::string>(reconnect_wait_time);
103
104 } else if (param.first == "port") {
105 port = param.second->intValue();
106 values_copy[param.first] =
107 boost::lexical_cast<std::string>(port);
108
109 } else if (param.first == "max-row-errors") {
110 max_row_errors = param.second->intValue();
111 values_copy[param.first] =
112 boost::lexical_cast<std::string>(max_row_errors);
113 } else {
114
115 // all remaining string parameters
116 // type
117 // user
118 // password
119 // host
120 // name
121 // on-fail
122 // trust-anchor
123 // cert-file
124 // key-file
125 // ssl-mode
126 // cipher-list
127 values_copy[param.first] = param.second->stringValue();
128 }
129 } catch (const isc::data::TypeError& ex) {
130 // Append position of the element.
131 isc_throw(DbConfigError, "invalid value type specified for "
132 "parameter '" << param.first << "' ("
133 << param.second->getPosition() << ")");
134 }
135 }
136
137 // 3. Perform validation checks on the updated set of keyword/values.
138 //
139 // a. Check if the "type" keyword exists and thrown an exception if not.
140 auto type_ptr = values_copy.find("type");
141 if (type_ptr == values_copy.end()) {
143 "database access parameters must "
144 "include the keyword 'type' to determine type of database "
145 "to be accessed (" << database_config->getPosition() << ")");
146 }
147
148 // b. Check if the 'type' keyword known and throw an exception if not.
149 //
150 // Please note when you add a new database backend you have to add
151 // the new type here and in server grammars.
152 string dbtype = type_ptr->second;
153 if ((dbtype != "memfile") &&
154 (dbtype != "mysql") &&
155 (dbtype != "postgresql")) {
156 ConstElementPtr value = database_config->get("type");
157 isc_throw(DbConfigError, "unknown backend database type: " << dbtype
158 << " (" << value->getPosition() << ")");
159 }
160
161 // c. Check that the lfc-interval is within a reasonable range.
162 if ((lfc_interval < 0) ||
163 (lfc_interval > std::numeric_limits<uint32_t>::max())) {
164 ConstElementPtr value = database_config->get("lfc-interval");
165 isc_throw(DbConfigError, "lfc-interval value: " << lfc_interval
166 << " is out of range, expected value: 0.."
167 << std::numeric_limits<uint32_t>::max()
168 << " (" << value->getPosition() << ")");
169 }
170
171 // d. Check that the timeouts are within a reasonable range.
172 if ((connect_timeout < 0) ||
173 (connect_timeout > std::numeric_limits<uint32_t>::max())) {
174 ConstElementPtr value = database_config->get("connect-timeout");
175 isc_throw(DbConfigError, "connect-timeout value: " << connect_timeout
176 << " is out of range, expected value: 0.."
177 << std::numeric_limits<uint32_t>::max()
178 << " (" << value->getPosition() << ")");
179 }
180 if ((read_timeout < 0) ||
181 (read_timeout > std::numeric_limits<uint32_t>::max())) {
182 ConstElementPtr value = database_config->get("read-timeout");
183 isc_throw(DbConfigError, "read-timeout value: " << read_timeout
184 << " is out of range, expected value: 0.."
185 << std::numeric_limits<uint32_t>::max()
186 << " (" << value->getPosition() << ")");
187 }
188 if (read_timeout > 0 && (dbtype != "mysql")) {
189 ConstElementPtr value = database_config->get("read-timeout");
190 isc_throw(DbConfigError, "read-timeout value is only supported by the mysql backend"
191 << " (" << value->getPosition() << ")");
192 }
193 if ((write_timeout < 0) ||
194 (write_timeout > std::numeric_limits<uint32_t>::max())) {
195 ConstElementPtr value = database_config->get("write-timeout");
196 isc_throw(DbConfigError, "write-timeout value: " << write_timeout
197 << " is out of range, expected value: 0.."
198 << std::numeric_limits<uint32_t>::max()
199 << " (" << value->getPosition() << ")");
200 }
201 if (write_timeout > 0 && (dbtype != "mysql")) {
202 ConstElementPtr value = database_config->get("write-timeout");
203 isc_throw(DbConfigError, "write-timeout value is only supported by the mysql backend"
204 << " (" << value->getPosition() << ")");
205 }
206 if ((tcp_user_timeout < 0) ||
207 (tcp_user_timeout > std::numeric_limits<uint32_t>::max())) {
208 ConstElementPtr value = database_config->get("tcp-user-timeout");
209 isc_throw(DbConfigError, "tcp-user-timeout value: " << tcp_user_timeout
210 << " is out of range, expected value: 0.."
211 << std::numeric_limits<uint32_t>::max()
212 << " (" << value->getPosition() << ")");
213 }
214 if (tcp_user_timeout > 0 && (dbtype != "postgresql")) {
215 ConstElementPtr value = database_config->get("tcp-user-timeout");
216 isc_throw(DbConfigError, "tcp-user-timeout value is only supported by the postgresql backend"
217 << " (" << value->getPosition() << ")");
218 }
219
220 // e. Check that the port is within a reasonable range.
221 if ((port < 0) ||
222 (port > std::numeric_limits<uint16_t>::max())) {
223 ConstElementPtr value = database_config->get("port");
224 isc_throw(DbConfigError, "port value: " << port
225 << " is out of range, expected value: 0.."
226 << std::numeric_limits<uint16_t>::max()
227 << " (" << value->getPosition() << ")");
228 }
229
230 // f. Check that the max-row-errors is within a reasonable range.
231 if ((max_row_errors < 0) ||
232 (max_row_errors > std::numeric_limits<uint32_t>::max())) {
233 ConstElementPtr value = database_config->get("max-row-errors");
234 isc_throw(DbConfigError, "max-row-errors value: " << max_row_errors
235 << " is out of range, expected value: 0.."
236 << std::numeric_limits<uint32_t>::max()
237 << " (" << value->getPosition() << ")");
238 }
239
240 // Check that the max-reconnect-tries is reasonable.
241 if (max_reconnect_tries < 0) {
242 ConstElementPtr value = database_config->get("max-reconnect-tries");
244 "max-reconnect-tries cannot be less than zero: ("
245 << value->getPosition() << ")");
246 }
247
248 // Check that the reconnect-wait-time is reasonable.
249 if ((reconnect_wait_time < 0) ||
250 (reconnect_wait_time > std::numeric_limits<uint32_t>::max())) {
251 ConstElementPtr value = database_config->get("reconnect-wait-time");
252 isc_throw(DbConfigError, "reconnect-wait-time " << reconnect_wait_time
253 << " must be in range 0...MAX_UINT32 (4294967295) "
254 << "(" << value->getPosition() << ")");
255 }
256
257 // 4. If all is OK, update the stored keyword/value pairs. We do this by
258 // swapping contents - values_copy is destroyed immediately after the
259 // operation (when the method exits), so we are not interested in its new
260 // value.
261 values_.swap(values_copy);
262
263 // 5. Save the database access string in the Configuration Manager.
264 access_string = getDbAccessString();
265}
266
267// Create the database access string
268std::string
270
271 // Construct the database access string from all keywords and values in the
272 // parameter map where the value is not null.
273 string dbaccess;
274 for (auto const& keyval : values_) {
275 if (!keyval.second.empty()) {
276
277 // Separate keyword/value pair from predecessor (if there is one).
278 if (!dbaccess.empty()) {
279 dbaccess += std::string(" ");
280 }
281
282 // Add the keyword/value pair to the access string.
283 auto val = keyval.second;
284 if (val.find_first_of("\t ") != string::npos){
285 val = "'" + val + "'";
286 }
287 dbaccess += (keyval.first + std::string("=") + val);
288 }
289 }
290
291 return (dbaccess);
292}
293
294} // namespace db
295} // namespace isc
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition data.h:36
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
void parse(std::string &access_string, isc::data::ConstElementPtr database_config)
Parse configuration value.
std::string getDbAccessString() const
Construct database access string.
Error detected in the database configuration.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
Defines the logger used by the top-level component of kea-lfc.