Kea: cfg_db_access.cc Source File

| Kea 3.0.3-git

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>
8#include <database/database_connection.h>
9#include <dhcpsrv/cfg_db_access.h>
10#include <dhcpsrv/db_type.h>
11#include <dhcpsrv/dhcpsrv_log.h>
12#include <dhcpsrv/host_data_source_factory.h>
13#include <dhcpsrv/host_mgr.h>
14#include <dhcpsrv/lease_mgr_factory.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
26CfgDbAccess::CfgDbAccess()
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
33CfgDbAccess::getLeaseDbAccessString() const {
34 return (getAccessString(lease_db_access_));
35}
36
37
38std::string
39CfgDbAccess::getHostDbAccessString() const {
40 if (host_db_access_.empty()) {
41 return ("");
42 } else {
43 return (getAccessString(host_db_access_.front()));
44 }
45}
46
47std::list<std::string>
48CfgDbAccess::getHostDbAccessStringList() const {
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
59CfgDbAccess::createManagers() const {
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 {
67 DatabaseConnection::ParameterMap parameters = DatabaseConnection::parse(access);
68 redacted = DatabaseConnection::redactedAccessString(parameters);
69 } catch (...) {
70 }
71 LOG_INFO(dhcpsrv_logger, DHCPSRV_LEASE_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED)
72 .arg(redacted).arg(err.what());
73 }
74
75 // Recreate host data source.
76 HostMgr::create();
77
78 // Restore the host cache.
79 if (HostDataSourceFactory::registeredFactory("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 {
87 HostMgr::addBackend(hds);
88 } catch (const isc::db::DbOpenErrorWithRetry& err) {
89 std::string redacted;
90 try {
91 DatabaseConnection::ParameterMap parameters = DatabaseConnection::parse(hds);
92 redacted = DatabaseConnection::redactedAccessString(parameters);
93 } catch (...) {
94 }
95 LOG_INFO(dhcpsrv_logger, DHCPSRV_HOST_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED)
96 .arg(redacted).arg(err.what());
97 }
98 }
99
100 // Check for a host cache.
101 HostMgr::checkCacheBackend(true);
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.
109 if (!HostMgr::instance().setIPReservationsUnique(ip_reservations_unique_)) {
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
cfg_db_access.h
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition exceptions/exceptions.h:143
isc::db::DatabaseConnection::redactedAccessString
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
Definition database_connection.cc:108
isc::db::DatabaseConnection::parse
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Definition database_connection.cc:41
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition database_connection.h:137
isc::db::DbOpenErrorWithRetry
Exception thrown on failure to open database but permit retries.
Definition database_connection.h:38
isc::dhcp::CfgDbAccess::ip_reservations_unique_
bool ip_reservations_unique_
Holds the setting whether IP reservations should be unique or can be non-unique.
Definition cfg_db_access.h:143
isc::dhcp::CfgDbAccess::getAccessString
std::string getAccessString(const std::string &access_string) const
Returns lease or host database access string.
Definition cfg_db_access.cc:118
isc::dhcp::CfgDbAccess::createManagers
void createManagers() const
Creates instance of lease manager and host data sources according to the configuration specified.
Definition cfg_db_access.cc:59
isc::dhcp::CfgDbAccess::getHostDbAccessString
std::string getHostDbAccessString() const
Retrieves host database access string.
Definition cfg_db_access.cc:39
isc::dhcp::CfgDbAccess::host_db_access_
std::list< std::string > host_db_access_
Holds host database access strings.
Definition cfg_db_access.h:139
isc::dhcp::CfgDbAccess::appended_parameters_
std::string appended_parameters_
Parameters to be appended to the database access strings.
Definition cfg_db_access.h:133
isc::dhcp::CfgDbAccess::CfgDbAccess
CfgDbAccess()
Constructor.
Definition cfg_db_access.cc:26
isc::dhcp::CfgDbAccess::extended_info_tables_enabled_
bool extended_info_tables_enabled_
Holds the setting whether the lease extended info tables are enabled or disabled.
Definition cfg_db_access.h:147
isc::dhcp::CfgDbAccess::getHostDbAccessStringList
std::list< std::string > getHostDbAccessStringList() const
Retrieves host database access string.
Definition cfg_db_access.cc:48
isc::dhcp::CfgDbAccess::setIPReservationsUnique
void setIPReservationsUnique(const bool unique)
Modifies the setting imposing whether the IP reservations are unique or can be non unique.
Definition cfg_db_access.h:90
isc::dhcp::CfgDbAccess::lease_db_access_
std::string lease_db_access_
Holds lease database access string.
Definition cfg_db_access.h:136
isc::dhcp::CfgDbAccess::getLeaseDbAccessString
std::string getLeaseDbAccessString() const
Retrieves lease database access string.
Definition cfg_db_access.cc:33
isc::dhcp::HostDataSourceFactory::registeredFactory
static bool registeredFactory(const std::string &db_type)
Check if a host data source factory was registered.
Definition host_data_source_factory.cc:162
isc::dhcp::HostMgr::create
static void create()
Creates new instance of the HostMgr.
Definition host_mgr.cc:52
isc::dhcp::HostMgr::addBackend
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition host_mgr.cc:57
isc::dhcp::HostMgr::checkCacheBackend
static bool checkCacheBackend(bool logging=false)
Check for the cache host backend.
Definition host_mgr.cc:92
isc::dhcp::HostMgr::instance
static HostMgr & instance()
Returns a sole instance of the HostMgr.
Definition host_mgr.cc:114
isc::dhcp::LeaseMgrFactory::recreate
static void recreate(const std::string &dbaccess, bool preserve_callbacks=true)
Recreate an instance of a lease manager with optionally preserving registered callbacks.
Definition lease_mgr_factory.cc:100
database_connection.h
db_type.h
dhcpsrv_log.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
host_data_source_factory.h
host_mgr.h
lease_mgr_factory.h
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::data
Definition base_stamped_element.cc:12
isc::db
Definition audit_entry.cc:14
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
isc::dhcp::DHCPSRV_LEASE_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
const isc::log::MessageID DHCPSRV_LEASE_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
Definition dhcpsrv_messages.h:84
isc::dhcp::DHCPSRV_HOST_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
const isc::log::MessageID DHCPSRV_HOST_MGR_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
Definition dhcpsrv_messages.h:74
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148