Kea 2.7.5
dbaccess_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2024 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 // cipher-list
126 values_copy[param.first] = param.second->stringValue();
127 }
128 } catch (const isc::data::TypeError& ex) {
129 // Append position of the element.
130 isc_throw(DbConfigError, "invalid value type specified for "
131 "parameter '" << param.first << "' ("
132 << param.second->getPosition() << ")");
133 }
134 }
135
136 // 3. Perform validation checks on the updated set of keyword/values.
137 //
138 // a. Check if the "type" keyword exists and thrown an exception if not.
139 auto type_ptr = values_copy.find("type");
140 if (type_ptr == values_copy.end()) {
142 "database access parameters must "
143 "include the keyword 'type' to determine type of database "
144 "to be accessed (" << database_config->getPosition() << ")");
145 }
146
147 // b. Check if the 'type' keyword known and throw an exception if not.
148 //
149 // Please note when you add a new database backend you have to add
150 // the new type here and in server grammars.
151 string dbtype = type_ptr->second;
152 if ((dbtype != "memfile") &&
153 (dbtype != "mysql") &&
154 (dbtype != "postgresql")) {
155 ConstElementPtr value = database_config->get("type");
156 isc_throw(DbConfigError, "unknown backend database type: " << dbtype
157 << " (" << value->getPosition() << ")");
158 }
159
160 // c. Check that the lfc-interval is within a reasonable range.
161 if ((lfc_interval < 0) ||
162 (lfc_interval > std::numeric_limits<uint32_t>::max())) {
163 ConstElementPtr value = database_config->get("lfc-interval");
164 isc_throw(DbConfigError, "lfc-interval value: " << lfc_interval
165 << " is out of range, expected value: 0.."
166 << std::numeric_limits<uint32_t>::max()
167 << " (" << value->getPosition() << ")");
168 }
169
170 // d. Check that the timeouts are within a reasonable range.
171 if ((connect_timeout < 0) ||
172 (connect_timeout > std::numeric_limits<uint32_t>::max())) {
173 ConstElementPtr value = database_config->get("connect-timeout");
174 isc_throw(DbConfigError, "connect-timeout value: " << connect_timeout
175 << " is out of range, expected value: 0.."
176 << std::numeric_limits<uint32_t>::max()
177 << " (" << value->getPosition() << ")");
178 }
179 if ((read_timeout < 0) ||
180 (read_timeout > std::numeric_limits<uint32_t>::max())) {
181 ConstElementPtr value = database_config->get("read-timeout");
182 isc_throw(DbConfigError, "read-timeout value: " << read_timeout
183 << " is out of range, expected value: 0.."
184 << std::numeric_limits<uint32_t>::max()
185 << " (" << value->getPosition() << ")");
186 }
187 if (read_timeout > 0 && (dbtype != "mysql")) {
188 ConstElementPtr value = database_config->get("read-timeout");
189 isc_throw(DbConfigError, "read-timeout value is only supported by the mysql backend"
190 << " (" << value->getPosition() << ")");
191 }
192 if ((write_timeout < 0) ||
193 (write_timeout > std::numeric_limits<uint32_t>::max())) {
194 ConstElementPtr value = database_config->get("write-timeout");
195 isc_throw(DbConfigError, "write-timeout value: " << write_timeout
196 << " is out of range, expected value: 0.."
197 << std::numeric_limits<uint32_t>::max()
198 << " (" << value->getPosition() << ")");
199 }
200 if (write_timeout > 0 && (dbtype != "mysql")) {
201 ConstElementPtr value = database_config->get("write-timeout");
202 isc_throw(DbConfigError, "write-timeout value is only supported by the mysql backend"
203 << " (" << value->getPosition() << ")");
204 }
205 if ((tcp_user_timeout < 0) ||
206 (tcp_user_timeout > std::numeric_limits<uint32_t>::max())) {
207 ConstElementPtr value = database_config->get("tcp-user-timeout");
208 isc_throw(DbConfigError, "tcp-user-timeout value: " << tcp_user_timeout
209 << " is out of range, expected value: 0.."
210 << std::numeric_limits<uint32_t>::max()
211 << " (" << value->getPosition() << ")");
212 }
213 if (tcp_user_timeout > 0 && (dbtype != "postgresql")) {
214 ConstElementPtr value = database_config->get("tcp-user-timeout");
215 isc_throw(DbConfigError, "tcp-user-timeout value is only supported by the postgresql backend"
216 << " (" << value->getPosition() << ")");
217 }
218
219 // e. Check that the port is within a reasonable range.
220 if ((port < 0) ||
221 (port > std::numeric_limits<uint16_t>::max())) {
222 ConstElementPtr value = database_config->get("port");
223 isc_throw(DbConfigError, "port value: " << port
224 << " is out of range, expected value: 0.."
225 << std::numeric_limits<uint16_t>::max()
226 << " (" << value->getPosition() << ")");
227 }
228
229 // f. Check that the max-row-errors is within a reasonable range.
230 if ((max_row_errors < 0) ||
231 (max_row_errors > std::numeric_limits<uint32_t>::max())) {
232 ConstElementPtr value = database_config->get("max-row-errors");
233 isc_throw(DbConfigError, "max-row-errors value: " << max_row_errors
234 << " is out of range, expected value: 0.."
235 << std::numeric_limits<uint32_t>::max()
236 << " (" << value->getPosition() << ")");
237 }
238
239 // Check that the max-reconnect-tries is reasonable.
240 if (max_reconnect_tries < 0) {
241 ConstElementPtr value = database_config->get("max-reconnect-tries");
243 "max-reconnect-tries cannot be less than zero: ("
244 << value->getPosition() << ")");
245 }
246
247 // Check that the reconnect-wait-time is reasonable.
248 if ((reconnect_wait_time < 0) ||
249 (reconnect_wait_time > std::numeric_limits<uint32_t>::max())) {
250 ConstElementPtr value = database_config->get("reconnect-wait-time");
251 isc_throw(DbConfigError, "reconnect-wait-time " << reconnect_wait_time
252 << " must be in range 0...MAX_UINT32 (4294967295) "
253 << "(" << value->getPosition() << ")");
254 }
255
256 // 4. If all is OK, update the stored keyword/value pairs. We do this by
257 // swapping contents - values_copy is destroyed immediately after the
258 // operation (when the method exits), so we are not interested in its new
259 // value.
260 values_.swap(values_copy);
261
262 // 5. Save the database access string in the Configuration Manager.
263 access_string = getDbAccessString();
264}
265
266// Create the database access string
267std::string
269
270 // Construct the database access string from all keywords and values in the
271 // parameter map where the value is not null.
272 string dbaccess;
273 for (auto const& keyval : values_) {
274 if (!keyval.second.empty()) {
275
276 // Separate keyword/value pair from predecessor (if there is one).
277 if (!dbaccess.empty()) {
278 dbaccess += std::string(" ");
279 }
280
281 // Add the keyword/value pair to the access string.
282 auto val = keyval.second;
283 if (val.find_first_of("\t ") != string::npos){
284 val = "'" + val + "'";
285 }
286 dbaccess += (keyval.first + std::string("=") + val);
287 }
288 }
289
290 return (dbaccess);
291}
292
293} // namespace db
294} // 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.