Kea 2.5.8
d_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2013-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
10#include <dhcp/libdhcp++.h>
11#include <process/d_log.h>
12#include <process/d_cfg_mgr.h>
13#include <process/daemon.h>
15#include <util/encode/encode.h>
16
17#include <boost/lexical_cast.hpp>
18#include <boost/algorithm/string.hpp>
19
20#include <limits>
21#include <iostream>
22#include <vector>
23#include <map>
24
25using namespace std;
26using namespace isc;
27using namespace isc::config;
28using namespace isc::dhcp;
29using namespace isc::data;
30using namespace isc::asiolink;
31
32namespace isc {
33namespace process {
34
35// *********************** DCfgMgrBase *************************
36
38 setContext(context);
39}
40
42}
43
44void
46 ConfigPtr context = createNewContext();
47 setContext(context);
48}
49
50void
52 if (!context) {
53 isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
54 }
55
56 context_ = context;
57}
58
61 ConstElementPtr result(config);
62 for (std::list<std::string>& json_path : jsonPathsToRedact()) {
63 result = isc::process::redactConfig(result, json_path);
64 }
65 return result;
66}
67
68list<list<string>> DCfgMgrBase::jsonPathsToRedact() const {
69 static list<list<string>> const list;
70 return list;
71}
72
75 bool check_only,
76 const std::function<void()>& post_config_cb) {
77 if (!config_set) {
79 std::string("Can't parse NULL config")));
80 }
82 .arg(redactConfig(config_set)->str());
83
84 // The parsers implement data inheritance by directly accessing
85 // configuration context. For this reason the data parsers must store
86 // the parsed data into context immediately. This may cause data
87 // inconsistency if the parsing operation fails after the context has been
88 // modified. We need to preserve the original context here
89 // so as we can rollback changes when an error occurs.
90 ConfigPtr original_context = context_;
92 bool rollback = false;
93
94 // Answer will hold the result returned to the caller.
95 ConstElementPtr answer;
96
97 try {
98 // Logging is common so factor it.
99 Daemon::configureLogger(config_set, context_);
100
101 // Let's call the actual implementation
102 answer = parse(config_set, check_only);
103
104 // and check the response returned.
105 int code = 0;
106 isc::config::parseAnswer(code, answer);
107
108 // Everything was fine. Configuration set processed successfully.
109 if (!check_only) {
110 if (code == 0) {
111 // Call the callback only when parsing was successful.
112 if (post_config_cb) {
113 post_config_cb();
114 }
116 // Set the last commit timestamp.
117 auto now = boost::posix_time::second_clock::universal_time();
118 context_->setLastCommitTime(now);
119 } else {
120 rollback = true;
121 }
122
123 // Use the answer provided.
124 //answer = isc::config::createAnswer(CONTROL_RESULT_SUCCESS, "Configuration committed.");
125 } else {
127 .arg(getConfigSummary(0))
128 .arg(config::answerToText(answer));
129 }
130
131 } catch (const std::exception& ex) {
132 LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
134 rollback = true;
135 }
136
137 if (check_only) {
138 // If this is a configuration check only, then don't actually apply
139 // the configuration and reverse to the previous one.
140 context_ = original_context;
141 }
142
143 if (rollback) {
144 // An error occurred, so make sure that we restore original context.
145 context_ = original_context;
146 }
147
148 return (answer);
149}
150
151
152void
154}
155
158 isc_throw(DCfgMgrBaseError, "This class does not implement simple parser paradigm yet");
159}
160
161} // end of isc::dhcp namespace
162} // end of isc namespace
Exception thrown if the configuration manager encounters an error.
Definition: d_cfg_mgr.h:29
virtual void setCfgDefaults(isc::data::ElementPtr mutable_config)
Adds default values to the given config.
Definition: d_cfg_mgr.cc:153
virtual std::list< std::list< std::string > > jsonPathsToRedact() const
Return a list of all paths that contain passwords or secrets.
Definition: d_cfg_mgr.cc:68
DCfgMgrBase(ConfigPtr context)
Constructor.
Definition: d_cfg_mgr.cc:37
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses actual configuration.
Definition: d_cfg_mgr.cc:157
virtual ~DCfgMgrBase()
Destructor.
Definition: d_cfg_mgr.cc:41
isc::data::ConstElementPtr simpleParseConfig(isc::data::ConstElementPtr config, bool check_only=false, const std::function< void()> &post_config_cb=nullptr)
Acts as the receiver of new configurations.
Definition: d_cfg_mgr.cc:74
void resetContext()
Replaces existing context with a new, empty context.
Definition: d_cfg_mgr.cc:45
void setContext(ConfigPtr &context)
Update the current context.
Definition: d_cfg_mgr.cc:51
virtual ConfigPtr createNewContext()=0
Abstract factory which creates a context instance.
virtual std::string getConfigSummary(const uint32_t selection)=0
Returns configuration summary in the textual format.
isc::data::ConstElementPtr redactConfig(isc::data::ConstElementPtr const &config) const
Redact the configuration.
Definition: d_cfg_mgr.cc:60
static void configureLogger(const isc::data::ConstElementPtr &log_config, const isc::process::ConfigPtr &storage)
Configures logger.
Definition: daemon.cc:66
This file contains several functions and constants that are used for handling commands and responses ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
std::string answerToText(const ConstElementPtr &msg)
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
const int DBGLVL_COMMAND
This debug level is reserved for logging the exchange of messages/commands between processes,...
Definition: log_dbglevels.h:54
isc::log::Logger dctl_logger("dctl")
Defines the logger used within libkea-process library.
Definition: d_log.h:18
const isc::log::MessageID DCTL_CONFIG_START
const isc::log::MessageID DCTL_CONFIG_CHECK_COMPLETE
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176
const isc::log::MessageID DCTL_PARSER_FAIL
const isc::log::MessageID DCTL_CONFIG_COMPLETE
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path)
Redact a configuration.
Defines the logger used by the top-level component of kea-lfc.