Kea 2.7.0
cfg_db_access.cc
Go to the documentation of this file.
1// Copyright (C) 2016-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>
10#include <dhcpsrv/db_type.h>
11#include <dhcpsrv/dhcpsrv_log.h>
13#include <dhcpsrv/host_mgr.h>
15#include <boost/algorithm/string.hpp>
16#include <boost/lexical_cast.hpp>
17#include <sstream>
18#include <vector>
19
20using namespace isc::data;
21using namespace isc::db;
22
23namespace isc {
24namespace dhcp {
25
27 : appended_parameters_(), lease_db_access_("type=memfile"),
28 host_db_access_(), ip_reservations_unique_(true),
29 extended_info_tables_enabled_(false) {
30}
31
32std::string
36
37
38std::string
40 if (host_db_access_.empty()) {
41 return ("");
42 } else {
43 return (getAccessString(host_db_access_.front()));
44 }
45}
46
47std::list<std::string>
49 std::list<std::string> ret;
50 for (const std::string& dbaccess : host_db_access_) {
51 if (!dbaccess.empty()) {
52 ret.push_back(getAccessString(dbaccess));
53 }
54 }
55 return (ret);
56}
57
58void
60 std::string access = getLeaseDbAccessString();
61 try {
62 // Recreate lease manager without preserving the registered callbacks.
63 LeaseMgrFactory::recreate(access, false);
64 } catch (const isc::db::DbOpenErrorWithRetry& err) {
65 std::string redacted;
66 try {
68 redacted = DatabaseConnection::redactedAccessString(parameters);
69 } catch (...) {
70 }
72 .arg(redacted).arg(err.what());
73 }
74
75 // Recreate host data source.
77
78 // Restore the host cache.
80 HostMgr::addBackend("type=cache");
81 }
82
83 // Add database backends.
84 std::list<std::string> host_db_access_list = getHostDbAccessStringList();
85 for (std::string& hds : host_db_access_list) {
86 try {
88 } catch (const isc::db::DbOpenErrorWithRetry& err) {
89 std::string redacted;
90 try {
92 redacted = DatabaseConnection::redactedAccessString(parameters);
93 } catch (...) {
94 }
96 .arg(redacted).arg(err.what());
97 }
98 }
99
100 // Check for a host cache.
102
103 // Populate the ip-reservations-unique global setting to HostMgr.
104 // This operation may fail if any of the host backends does not support
105 // the new setting. We throw an exception here to signal configuration
106 // error. The exception does not contain the backend name but the called
107 // function in HostMgr logs a warning message that contains the name of
108 // the backend.
110 isc_throw(InvalidOperation, "unable to configure the server to allow "
111 "non unique IP reservations (ip-reservations-unique=false) "
112 "because some host backends in use do not support this "
113 "setting");
114 }
115}
116
117std::string
118CfgDbAccess::getAccessString(const std::string& access_string) const {
119 std::ostringstream s;
120 s << access_string;
121 // Only append additional parameters if any parameters are specified
122 // in a configuration. For host database, no parameters mean that
123 // database access is disabled and thus we don't want to append any
124 // parameters.
125 if ((s.tellp() != std::streampos(0)) && (!appended_parameters_.empty())) {
126 s << " " << appended_parameters_;
127 }
128
129 return (s.str());
130}
131
132} // end of isc::dhcp namespace
133} // end of isc namespace
A generic exception that is thrown if a function is called in a prohibited way.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to open database but permit retries.
bool ip_reservations_unique_
Holds the setting whether IP reservations should be unique or can be non-unique.
std::string getAccessString(const std::string &access_string) const
Returns lease or host database access string.
void createManagers() const
Creates instance of lease manager and host data sources according to the configuration specified.
std::string getHostDbAccessString() const
Retrieves host database access string.
std::list< std::string > host_db_access_
Holds host database access strings.
std::string appended_parameters_
Parameters to be appended to the database access strings.
CfgDbAccess()
Constructor.
std::list< std::string > getHostDbAccessStringList() const
Retrieves host database access string.
void setIPReservationsUnique(const bool unique)
Modifies the setting imposing whether the IP reservations are unique or can be non unique.
std::string lease_db_access_
Holds lease database access string.
std::string getLeaseDbAccessString() const
Retrieves lease database access string.
static bool registeredFactory(const std::string &db_type)
Check if a host data source factory was registered.
static void create()
Creates new instance of the HostMgr.
Definition host_mgr.cc:52
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition host_mgr.cc:57
static bool checkCacheBackend(bool logging=false)
Check for the cache host backend.
Definition host_mgr.cc:92
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition host_mgr.cc:114
static void recreate(const std::string &dbaccess, bool preserve_callbacks=true)
Recreate an instance of a lease manager with optionally preserving registered callbacks.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_LEASE_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
const isc::log::MessageID DHCPSRV_HOST_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
Defines the logger used by the top-level component of kea-lfc.