1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <ha_config_parser.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ha_log.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ha_service_states.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/dhcp_config_error.h>
#include <util/filesystem.h>
#include <boost/make_shared.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <set><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::data;
using namespace isc::http;
namespace {
/// @brief Default values for HA load balancing.
const SimpleDefaults HA_CONFIG_LB_DEFAULTS = {
{ "delayed-updates-limit", Element::integer, "100" },
};
/// @brief Default values for HA configuration.
const SimpleDefaults HA_CONFIG_DEFAULTS = {
{ "delayed-updates-limit", Element::integer, "0" },
{ "heartbeat-delay", Element::integer, "10000" },
{ "max-ack-delay", Element::integer, "10000" },
{ "max-response-delay", Element::integer, "60000" },
{ "max-unacked-clients", Element::integer, "10" },
{ "max-rejected-lease-updates", Element::integer, "10" },
{ "require-client-certs", Element::boolean, "true" },
{ "restrict-commands", Element::boolean, "false" },
{ "send-lease-updates", Element::boolean, "true" },
{ "sync-leases", Element::boolean, "true" },
{ "sync-timeout", Element::integer, "60000" },
{ "sync-page-limit", Element::integer, "10000" },
{ "wait-backup-ack", Element::boolean, "false" }
};
/// @brief Default values for HA multi-threading configuration.
const SimpleDefaults HA_CONFIG_MT_DEFAULTS = {
{ "enable-multi-threading", Element::boolean, "true" },
{ "http-client-threads", Element::integer, "0" },
{ "http-dedicated-listener", Element::boolean, "true" },
{ "http-listener-threads", Element::integer, "0" }
};
/// @brief Default values for HA peer configuration.
const SimpleDefaults HA_CONFIG_PEER_DEFAULTS = {
{ "auto-failover", Element::boolean, "true" }
};
/// @brief Default values for HA state configuration.
const SimpleDefaults HA_CONFIG_STATE_DEFAULTS = {
{ "pause", Element::string, "never" }
};
} // end of anonymous namespace
namespace isc {
namespace ha {
HAConfigMapperPtr
HAConfigParser::parse(const ConstElementPtr& config) {
try {
auto config_storage = boost::make_shared<HAConfigMapper>();
// This may cause different types of exceptions. We catch them here
// and throw unified exception type.
parseAll(config_storage, config);
validateRelationships(config_storage);
logConfigStatus(config_storage);
return (config_storage);
} catch (const ConfigError& ex) {
throw;
} catch (const std::exception& ex) {
isc_throw(ConfigError, ex.what());
}
}
void
HAConfigParser::parseAll(const HAConfigMapperPtr& config_storage,
const ConstElementPtr& config) {
// Config must be provided.
if (!config) {
isc_throw(ConfigError, "HA configuration must not be null");
}
// Config must be a list. Each contains one relationship between servers in the
// HA configuration. Currently we support only one relationship.
if (config->getType() != Element::list) {
isc_throw(ConfigError, "HA configuration must be a list");
}
// Get the HA configuration.
auto const& config_vec = config->listValue();
if (config_vec.empty()) {
isc_throw(ConfigError, "a list of HA configurations must not be empty");
}
for (auto const& cfg : config_vec) {
parseOne(config_storage, cfg);
}
}
void
HAConfigParser::parseOne(const HAConfigMapperPtr& config_storage,
const ElementPtr& config) {
// Config must be provided.
if (!config) {
isc_throw(ConfigError, "HA configuration must not be null");
}
// Config must be a map.
if (config->getType() != Element::map) {
isc_throw(ConfigError, "HA configuration for a relationship must be a map");
}
auto rel_config = HAConfig::create();
// Get 'mode'. That's the first thing to gather because the defaults we
// apply to the configuration depend on the mode.
rel_config->setHAMode(getString(config, "mode"));
// Set load-balancing specific defaults.
if (rel_config->getHAMode() == HAConfig::LOAD_BALANCING) {
setDefaults(config, HA_CONFIG_LB_DEFAULTS);
}
// Set general defaults.
setDefaults(config, HA_CONFIG_DEFAULTS);
// It must contain peers section.
if (!config->contains("peers")) {
isc_throw(ConfigError, "'peers' parameter missing in HA configuration");
}
// Peers configuration must be a list of maps.
ConstElementPtr peers = config->get("peers");
if (peers->getType() != Element::list) {
isc_throw(ConfigError, "'peers' parameter must be a list");
}
// State machine configuration must be a map.
ConstElementPtr state_machine = config->get("state-machine");
ConstElementPtr states_list;
if (state_machine) {
if (state_machine->getType() != Element::map) {
isc_throw(ConfigError, "'state-machine' parameter must be a map");
}
states_list = state_machine->get("states");
if (states_list && (states_list->getType() != Element::list)) {
isc_throw(ConfigError, "'states' parameter must be a list");
}
}
// We have made major sanity checks, so let's try to gather some values.
// Get 'this-server-name'.
rel_config->setThisServerName(getString(config, "this-server-name"));
// Get 'send-lease-updates'.
rel_config->setSendLeaseUpdates(getBoolean(config, "send-lease-updates"));
// Get 'sync-leases'.
rel_config->setSyncLeases(getBoolean(config, "sync-leases"));
// Get 'sync-timeout'.
uint32_t sync_timeout = getAndValidateInteger<uint32_t>(config, "sync-timeout");
rel_config->setSyncTimeout(sync_timeout);
// Get 'sync-page-limit'.
uint32_t sync_page_limit = getAndValidateInteger<uint32_t>(config, "sync-page-limit");
rel_config->setSyncPageLimit(sync_page_limit);
// Get 'delayed-updates-limit'.
uint32_t delayed_updates_limit = getAndValidateInteger<uint32_t>(config, "delayed-updates-limit");
rel_config->setDelayedUpdatesLimit(delayed_updates_limit);
// Get 'heartbeat-delay'.
uint16_t heartbeat_delay = getAndValidateInteger<uint16_t>(config, "heartbeat-delay");
rel_config->setHeartbeatDelay(heartbeat_delay);
// Get 'max-response-delay'.
uint16_t max_response_delay = getAndValidateInteger<uint16_t>(config, "max-response-delay");
rel_config->setMaxResponseDelay(max_response_delay);
// Get 'max-ack-delay'.
uint16_t max_ack_delay = getAndValidateInteger<uint16_t>(config, "max-ack-delay");
rel_config->setMaxAckDelay(max_ack_delay);
// Get 'max-unacked-clients'.
uint32_t max_unacked_clients = getAndValidateInteger<uint32_t>(config, "max-unacked-clients");
rel_config->setMaxUnackedClients(max_unacked_clients);
// Get 'max-rejected-lease-updates'.
uint32_t max_rejected_lease_updates = getAndValidateInteger<uint32_t>(config, "max-rejected-lease-updates");
rel_config->setMaxRejectedLeaseUpdates(max_rejected_lease_updates);
// Get 'wait-backup-ack'.
rel_config->setWaitBackupAck(getBoolean(config, "wait-backup-ack"));
// Get multi-threading map.
ElementPtr mt_config = boost::const_pointer_cast<Element>(config->get("multi-threading"));
if (!mt_config) {
// Not there, make an empty one.
mt_config = Element::createMap();
config->set("multi-threading", mt_config);
} else if (mt_config->getType() != Element::map) {
isc_throw(ConfigError, "multi-threading configuration must be a map");
}
// Backfill the MT defaults.
setDefaults(mt_config, HA_CONFIG_MT_DEFAULTS);
// Get 'enable-multi-threading'.
rel_config->setEnableMultiThreading(getBoolean(mt_config, "enable-multi-threading"));
// Get 'http-dedicated-listener'.
rel_config->setHttpDedicatedListener(getBoolean(mt_config, "http-dedicated-listener"));
// Get 'http-listener-threads'.
uint32_t threads = getAndValidateInteger<uint32_t>(mt_config, "http-listener-threads");
rel_config->setHttpListenerThreads(threads);
// Get 'http-client-threads'.
threads = getAndValidateInteger<uint32_t>(mt_config, "http-client-threads");
rel_config->setHttpClientThreads(threads);
// Get optional 'trust-anchor'.
ConstElementPtr ca = config->get("trust-anchor");
if (ca) {
rel_config->setTrustAnchor(getString(config, "trust-anchor"));
}
// Get optional 'cert-file'.
ConstElementPtr cert = config->get("cert-file");
if (cert) {
rel_config->setCertFile(getString(config, "cert-file"));
}
// Get optional 'key-file'.
ConstElementPtr key = config->get("key-file");
if (key) {
rel_config->setKeyFile(getString(config, "key-file"));
}
// Get 'require-client-certs'.
rel_config->setRequireClientCerts(getBoolean(config, "require-client-certs"));
// Get 'restrict-commands'.
rel_config->setRestrictCommands(getBoolean(config, "restrict-commands"));
// Peers configuration parsing.
auto const& peers_vec = peers->listValue();
// Go over configuration of each peer.
for (auto const& p : peers_vec) {
// Peer configuration is held in a map.
if (p->getType() != Element::map) {
isc_throw(ConfigError, "peer configuration must be a map");
}
setDefaults(p, HA_CONFIG_PEER_DEFAULTS);
// Server name.
auto cfg = rel_config->selectNextPeerConfig(getString(p, "name"));
// URL.
cfg->setUrl(Url(getString(p, "url")));
// Optional trust anchor.
if (p->contains("trust-anchor")) {
cfg->setTrustAnchor(getString(p, ("trust-anchor")));
}
// Optional certificate file.
if (p->contains("cert-file")) {
cfg->setCertFile(getString(p, ("cert-file")));
}
// Optional private key file.
if (p->contains("key-file")) {
cfg->setKeyFile(getString(p, ("key-file")));
}
// Role.
cfg->setRole(getString(p, "role"));
// Auto failover configuration.
cfg->setAutoFailover(getBoolean(p, "auto-failover"));
// Basic HTTP authentication password.
std::string password;
if (p->contains("basic-auth-password")) {
if (p->contains("basic-auth-password-file")) {
isc_throw(dhcp::DhcpConfigError, "only one of "
<< "basic-auth-password and "
<< "basic-auth-password-file parameter can be "
<< "configured in peer '"
<< cfg->getName() << "'");
}
password = getString(p, "basic-auth-password");
}
if (p->contains("basic-auth-password-file")) {
std::string password_file =
getString(p, "basic-auth-password-file");
try {
password = util::file::getContent(password_file);
} catch (const std::exception& ex) {
isc_throw(dhcp::DhcpConfigError, "bad password file in peer '"
<< cfg->getName() << "': " << ex.what());
}
}
// Basic HTTP authentication user.
if (p->contains("basic-auth-user")) {
std::string user = getString(p, "basic-auth-user");
BasicHttpAuthPtr& auth = cfg->getBasicAuth();
try {
if (!user.empty()) {
// Validate the user id value.
auth.reset(new BasicHttpAuth(user, password));
}
} catch (const std::exception& ex) {
isc_throw(dhcp::DhcpConfigError, ex.what() << " in peer '"
<< cfg->getName() << "'");
}
}
}
// Per state configuration is optional.
if (states_list) {
auto const& states_vec = states_list->listValue();
std::set<int> configured_states;
// Go over per state configurations.
for (auto const& s : states_vec) {
// State configuration is held in map.
if (s->getType() != Element::map) {
isc_throw(ConfigError, "state configuration must be a map");
}
setDefaults(s, HA_CONFIG_STATE_DEFAULTS);
// Get state name and set per state configuration.
std::string state_name = getString(s, "state");
int state = stringToState(state_name);
// Check that this configuration doesn't duplicate existing configuration.
if (configured_states.count(state) > 0) {
isc_throw(ConfigError, "duplicated configuration for the '"
<< state_name << "' state");
}
configured_states.insert(state);
rel_config->getStateMachineConfig()->
getStateConfig(state)->setPausing(getString(s, "pause"));
}
}
// We have gone over the entire configuration and stored it in the configuration
// storage. However, we need to still validate it to detect errors like:
// duplicate secondary/primary servers, no configuration for this server etc.
rel_config->validate();
auto peer_configs = rel_config->getAllServersConfig();
for (auto const& peer_config : peer_configs) {
try {
config_storage->map(peer_config.first, rel_config);
} catch (const std::exception& ex) {
isc_throw(HAConfigValidationError, "server names must be unique for different relationships: "
<< ex.what());
}
}
}
template<typename T>
T HAConfigParser::getAndValidateInteger(const ConstElementPtr& config,
const std::string& parameter_name) {
int64_t value = getInteger(config, parameter_name);
if (value < 0) {
isc_throw(ConfigError, "'" << parameter_name << "' must not be negative");
} else if (value > std::numeric_limits<T>::max()) {
isc_throw(ConfigError, "'" << parameter_name << "' must not be greater than "
<< +std::numeric_limits<T>::max());
}
return (static_cast<T>(value));
}
void
HAConfigParser::logConfigStatus(const HAConfigMapperPtr& config_storage) {
LOG_INFO(ha_logger, HA_CONFIGURATION_SUCCESSFUL);
for (auto const& config : config_storage->getAll()) {
// If lease updates are disabled, we want to make sure that the user
// realizes that and that he has configured some other mechanism to
// populate leases.
if (!config->amSendingLeaseUpdates()) {
LOG_WARN(ha_logger, HA_CONFIG_LEASE_UPDATES_DISABLED)
.arg(config->getThisServerName());
}
// Same as above but for lease database synchronization.
if (!config->amSyncingLeases()) {
LOG_WARN(ha_logger, HA_CONFIG_LEASE_SYNCING_DISABLED)
.arg(config->getThisServerName());
}
// Unusual configuration.
if (config->amSendingLeaseUpdates() !=
config->amSyncingLeases()) {
LOG_WARN(ha_logger, HA_CONFIG_LEASE_UPDATES_AND_SYNCING_DIFFER)
.arg(config->getThisServerName())
.arg(config->amSendingLeaseUpdates() ? "true" : "false")
.arg(config->amSyncingLeases() ? "true" : "false");
}
// With this setting the server will not take ownership of the partner's
// scope in case of partner's failure. This setting is OK if the
// administrator desires to have more control over scopes selection.
// The administrator will need to send ha-scopes command to instruct
// the server to take ownership of the scope. In some cases he may
// also need to send dhcp-enable command to enable DHCP service
// (specifically hot-standby mode for standby server).
if (!config->getThisServerConfig()->isAutoFailover()) {
LOG_WARN(ha_logger, HA_CONFIG_AUTO_FAILOVER_DISABLED)
.arg(config->getThisServerName());
}
}
}
void
HAConfigParser::validateRelationships(const HAConfigMapperPtr& config_storage) {
auto configs = config_storage->getAll();
if (configs.size() <= 1) {
return;
}
std::unordered_set<std::string> server_names;<--- Unused variable: server_names
for (auto const& config : configs) {
// Only the hot-standby mode is supported for multiple relationships.
if (config->getHAMode() != HAConfig::HOT_STANDBY) {
isc_throw(HAConfigValidationError, "multiple HA relationships are only supported for 'hot-standby' mode");
}
}
}
} // namespace ha
} // namespace isc
|