Kea 2.2.0
d_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2021 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/hex.h>
16#include <util/strutil.h>
17
18#include <boost/foreach.hpp>
19#include <boost/lexical_cast.hpp>
20#include <boost/algorithm/string.hpp>
21
22#include <limits>
23#include <iostream>
24#include <vector>
25#include <map>
26
27using namespace std;
28using namespace isc;
29using namespace isc::dhcp;
30using namespace isc::data;
31using namespace isc::asiolink;
32
33namespace isc {
34namespace process {
35
36// *********************** DCfgMgrBase *************************
37
39 setContext(context);
40}
41
43}
44
45void
47 ConfigPtr context = createNewContext();
48 setContext(context);
49}
50
51void
53 if (!context) {
54 isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
55 }
56
57 context_ = context;
58}
59
62 ConstElementPtr result(config);
63 for (std::list<std::string>& json_path : jsonPathsToRedact()) {
64 result = isc::process::redactConfig(result, json_path);
65 }
66 return result;
67}
68
69list<list<string>> DCfgMgrBase::jsonPathsToRedact() const {
70 static list<list<string>> const list;
71 return list;
72}
73
76 bool check_only,
77 const std::function<void()>& post_config_cb) {
78 if (!config_set) {
80 std::string("Can't parse NULL config")));
81 }
83 .arg(redactConfig(config_set)->str());
84
85 // The parsers implement data inheritance by directly accessing
86 // configuration context. For this reason the data parsers must store
87 // the parsed data into context immediately. This may cause data
88 // inconsistency if the parsing operation fails after the context has been
89 // modified. We need to preserve the original context here
90 // so as we can rollback changes when an error occurs.
91 ConfigPtr original_context = context_;
93 bool rollback = false;
94
95 // Answer will hold the result returned to the caller.
96 ConstElementPtr answer;
97
98 try {
99 // Logging is common so factor it.
100 Daemon::configureLogger(config_set, context_);
101
102 // Let's call the actual implementation
103 answer = parse(config_set, check_only);
104
105 // and check the response returned.
106 int code = 0;
107 isc::config::parseAnswer(code, answer);
108
109 // Everything was fine. Configuration set processed successfully.
110 if (!check_only) {
111 if (code == 0) {
112 // Call the callback only when parsing was successful.
113 if (post_config_cb) {
114 post_config_cb();
115 }
117 // Set the last commit timestamp.
118 auto now = boost::posix_time::second_clock::universal_time();
119 context_->setLastCommitTime(now);
120 } else {
121 rollback = true;
122 }
123
124 // Use the answer provided.
125 //answer = isc::config::createAnswer(0, "Configuration committed.");
126 } else {
128 .arg(getConfigSummary(0))
129 .arg(config::answerToText(answer));
130 }
131
132 } catch (const std::exception& ex) {
133 LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
134 answer = isc::config::createAnswer(1, ex.what());
135 rollback = true;
136 }
137
138 if (check_only) {
139 // If this is a configuration check only, then don't actually apply
140 // the configuration and reverse to the previous one.
141 context_ = original_context;
142 }
143
144 if (rollback) {
145 // An error occurred, so make sure that we restore original context.
146 context_ = original_context;
147 }
148
149 return (answer);
150}
151
152
153void
155}
156
159 isc_throw(DCfgMgrBaseError, "This class does not implement simple parser paradigm yet");
160}
161
162} // end of isc::dhcp namespace
163} // 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:154
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:69
DCfgMgrBase(ConfigPtr context)
Constructor.
Definition: d_cfg_mgr.cc:38
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses actual configuration.
Definition: d_cfg_mgr.cc:158
virtual ~DCfgMgrBase()
Destructor.
Definition: d_cfg_mgr.cc:42
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:75
void resetContext()
Replaces existing context with a new, empty context.
Definition: d_cfg_mgr.cc:46
void setContext(ConfigPtr &context)
Update the current context.
Definition: d_cfg_mgr.cc:52
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:61
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
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
std::string answerToText(const ConstElementPtr &msg)
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
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.