| Kea 3.0.2-git

ca_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2025 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
9#include <agent/ca_cfg_mgr.h>
10#include <agent/ca_log.h>
11#include <agent/simple_parser.h>
12#include <cc/simple_parser.h>
13#include <cc/command_interpreter.h>
14#include <http/basic_auth_config.h>
15#include <exceptions/exceptions.h>
16
17 using namespace isc::config;
18 using namespace isc::dhcp;
19 using namespace isc::process;
20 using namespace isc::data;
21
22 namespace isc {
23 namespace agent {
24
25 CtrlAgentCfgContext::CtrlAgentCfgContext()
26 : http_host_(""), http_port_(0), http_headers_(),
27 trust_anchor_(""), cert_file_(""), key_file_(""), cert_required_(true) {
28}
29
30 CtrlAgentCfgContext::CtrlAgentCfgContext(const CtrlAgentCfgContext& orig)
31 : ConfigBase(), ctrl_sockets_(orig.ctrl_sockets_),
32 http_host_(orig.http_host_), http_port_(orig.http_port_),
33 http_headers_(orig.http_headers_),
34 trust_anchor_(orig.trust_anchor_), cert_file_(orig.cert_file_),
35 key_file_(orig.key_file_), cert_required_(orig.cert_required_),
36 hooks_config_(orig.hooks_config_), auth_config_(orig.auth_config_) {
37}
38
39 CtrlAgentCfgMgr::CtrlAgentCfgMgr()
40 : DCfgMgrBase(ConfigPtr(new CtrlAgentCfgContext())) {
41}
42
43 CtrlAgentCfgMgr::~CtrlAgentCfgMgr() {
44}
45
46std::string
47 CtrlAgentCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
48
49 CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
50
51 // First print the http stuff.
52 std::ostringstream s;
53 s << "listening on " << ctx->getHttpHost() << ", port "
54 << ctx->getHttpPort();
55
56 // When TLS is setup print its config.
57 if (!ctx->getTrustAnchor().empty()) {
58 s << ", trust anchor " << ctx->getTrustAnchor()
59 << ", cert file " << ctx->getCertFile()
60 << ", key file " << ctx->getKeyFile();
61 if (ctx->getCertRequired()) {
62 s << ", client certs are required";
63 } else {
64 s << ", client certs are optional";
65 }
66 }
67 s << ", control sockets: ";
68
69 // Then print the control-sockets
70 s << ctx->getControlSocketInfoSummary();
71
72 // Add something if authentication is required.
73 const isc::http::HttpAuthConfigPtr& auth = ctx->getAuthConfig();
74 if (auth && !auth->empty()) {
75 s << ", requires basic HTTP authentication";
76 }
77
78 // Finally, print the hook libraries names
79 const isc::hooks::HookLibsCollection libs = ctx->getHooksConfig().get();
80 s << ", " << libs.size() << " lib(s):";
81 for (auto const& lib : libs) {
82 s << lib.cfgname_ << " ";
83 }
84
85 return (s.str());
86}
87
88 ConfigPtr
89 CtrlAgentCfgMgr::createNewContext() {
90 return (ConfigPtr(new CtrlAgentCfgContext()));
91}
92
93 ConstElementPtr
94 CtrlAgentCfgMgr::parse(ConstElementPtr config_set, bool check_only) {
95 // Do a sanity check first.
96 if (!config_set) {
97 isc_throw(DhcpConfigError, "Mandatory config parameter not provided");
98 }
99
100 CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgContext();
101
102 // Set the defaults
103 ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
104 AgentSimpleParser::setAllDefaults(cfg);
105
106 // And parse the configuration.
107 ConstElementPtr answer;
108 std::string excuse;
109 try {
110 // Do the actual parsing
111 AgentSimpleParser parser;
112 parser.checkTlsSetup(cfg);
113 parser.parse(ctx, cfg, check_only);
114 } catch (const isc::Exception& ex) {
115 excuse = ex.what();
116 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
117 } catch (...) {
118 excuse = "undefined configuration parsing error";
119 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
120 }
121
122 // At this stage the answer was created only in case of exception.
123 if (answer) {
124 if (check_only) {
125 LOG_ERROR(agent_logger, CTRL_AGENT_CONFIG_CHECK_FAIL).arg(excuse);
126 } else {
127 LOG_ERROR(agent_logger, CTRL_AGENT_CONFIG_FAIL).arg(excuse);
128 }
129 return (answer);
130 }
131
132 if (check_only) {
133 answer = createAnswer(CONTROL_RESULT_SUCCESS,
134 "Configuration check successful");
135 } else {
136 answer = createAnswer(CONTROL_RESULT_SUCCESS,
137 "Configuration applied successfully.");
138 }
139
140 return (answer);
141}
142
143std::list<std::list<std::string>>
144 CtrlAgentCfgMgr::jsonPathsToRedact() const {
145 static std::list<std::list<std::string>> const list({
146 {"authentication", "clients", "[]"},
147 {"hooks-libraries", "[]", "parameters", "*"},
148 });
149 return list;
150}
151
152 data::ConstElementPtr
153 CtrlAgentCfgContext::getControlSocketInfo(const std::string& service) const {
154 auto si = ctrl_sockets_.find(service);
155 return ((si != ctrl_sockets_.end()) ? si->second : ConstElementPtr());
156}
157
158 void
159 CtrlAgentCfgContext::setControlSocketInfo(const ConstElementPtr& control_socket,
160 const std::string& service) {
161 ctrl_sockets_[service] = control_socket;
162}
163
164std::string
165 CtrlAgentCfgContext::getControlSocketInfoSummary() const {
166 std::ostringstream s;
167 for (auto const& si : ctrl_sockets_) {
168 if (s.tellp() != 0) {
169 s << " ";
170 }
171 s << si.first;
172 }
173
174 if (s.tellp() == 0) {
175 s << "none";
176 }
177
178 return (s.str());
179}
180
181 ElementPtr
182 CtrlAgentCfgContext::toElement() const {
183 ElementPtr ca = ConfigBase::toElement();
184 // Set user-context
185 contextToElement(ca);
186 // Set http-host
187 ca->set("http-host", Element::create(http_host_));
188 // Set http-port
189 ca->set("http-port", Element::create(static_cast<int64_t>(http_port_)));
190 // Set http-headers
191 if (!http_headers_.empty()) {
192 ca->set("http-headers", CfgHttpHeaderstoElement(http_headers_));
193 }
194 // Set TLS setup when enabled
195 if (!trust_anchor_.empty()) {
196 ca->set("trust-anchor", Element::create(trust_anchor_));
197 ca->set("cert-file", Element::create(cert_file_));
198 ca->set("key-file", Element::create(key_file_));
199 ca->set("cert-required", Element::create(cert_required_));
200 }
201 // Set authentication
202 if (auth_config_) {
203 ca->set("authentication", auth_config_->toElement());
204 }
205 ca->set("hooks-libraries", hooks_config_.toElement());
206 // Set control-sockets
207 ElementPtr control_sockets = Element::createMap();
208 for (auto const& si : ctrl_sockets_) {
209 // Remove validated_path.
210 auto mutable_socket_info = boost::const_pointer_cast<Element>(
211 UserContext::toElement(si.second));
212 mutable_socket_info->remove("validated-socket-name");
213 control_sockets->set(si.first, mutable_socket_info);
214 }
215
216 ca->set("control-sockets", control_sockets);
217 // Set Control-agent
218 ElementPtr result = Element::createMap();
219 result->set("Control-agent", ca);
220
221 return (result);
222}
223
224} // namespace isc::agent
225} // namespace isc
basic_auth_config.h
simple_parser.h
ca_cfg_mgr.h
ca_log.h
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition exceptions/exceptions.h:23
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::agent::AgentSimpleParser
Definition bin/agent/simple_parser.h:23
isc::agent::AgentSimpleParser::checkTlsSetup
void checkTlsSetup(const isc::data::ConstElementPtr &config)
Check TLS setup consistency i.e.
Definition bin/agent/simple_parser.cc:108
isc::agent::AgentSimpleParser::parse
void parse(const CtrlAgentCfgContextPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the control agent configuration.
Definition bin/agent/simple_parser.cc:135
isc::agent::AgentSimpleParser::setAllDefaults
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Control Agent configuration.
Definition bin/agent/simple_parser.cc:70
isc::agent::CtrlAgentCfgContext
Control Agent Configuration Context.
Definition ca_cfg_mgr.h:33
isc::agent::CtrlAgentCfgContext::setControlSocketInfo
void setControlSocketInfo(const isc::data::ConstElementPtr &control_socket, const std::string &service)
Sets information about the control socket.
Definition ca_cfg_mgr.cc:159
isc::agent::CtrlAgentCfgContext::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition ca_cfg_mgr.cc:182
isc::agent::CtrlAgentCfgContext::CtrlAgentCfgContext
CtrlAgentCfgContext()
Default constructor.
Definition ca_cfg_mgr.cc:25
isc::agent::CtrlAgentCfgContext::getControlSocketInfo
isc::data::ConstElementPtr getControlSocketInfo(const std::string &service) const
Returns information about control socket.
Definition ca_cfg_mgr.cc:153
isc::agent::CtrlAgentCfgContext::getControlSocketInfoSummary
std::string getControlSocketInfoSummary() const
Returns socket configuration summary in a textual format.
Definition ca_cfg_mgr.cc:165
isc::agent::CtrlAgentCfgMgr::jsonPathsToRedact
std::list< std::list< std::string > > jsonPathsToRedact() const final override
Return a list of all paths that contain passwords or secrets.
Definition ca_cfg_mgr.cc:144
isc::agent::CtrlAgentCfgMgr::CtrlAgentCfgMgr
CtrlAgentCfgMgr()
Constructor.
Definition ca_cfg_mgr.cc:39
isc::agent::CtrlAgentCfgMgr::createNewContext
virtual process::ConfigPtr createNewContext() override
Creates a new, blank CtrlAgentCfgContext context.
Definition ca_cfg_mgr.cc:89
isc::agent::CtrlAgentCfgMgr::parse
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only) override
Parses configuration of the Control Agent.
Definition ca_cfg_mgr.cc:94
isc::agent::CtrlAgentCfgMgr::getCtrlAgentCfgContext
CtrlAgentCfgContextPtr getCtrlAgentCfgContext()
Convenience method that returns the Control Agent configuration context.
Definition ca_cfg_mgr.h:284
isc::agent::CtrlAgentCfgMgr::getConfigSummary
virtual std::string getConfigSummary(const uint32_t selection) override
Returns configuration summary in the textual format.
Definition ca_cfg_mgr.cc:47
isc::agent::CtrlAgentCfgMgr::~CtrlAgentCfgMgr
virtual ~CtrlAgentCfgMgr()
Destructor.
Definition ca_cfg_mgr.cc:43
isc::dhcp::DhcpConfigError
To be removed. Please use ConfigError instead.
Definition dhcp_config_error.h:58
isc::process::ConfigBase
Base class for all configurations.
Definition config_base.h:33
isc::process::ConfigBase::toElement
virtual isc::data::ElementPtr toElement() const
Converts to Element representation.
Definition config_base.cc:118
isc::process::DCfgMgrBase::DCfgMgrBase
DCfgMgrBase(ConfigPtr context)
Constructor.
Definition d_cfg_mgr.cc:37
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
simple_parser.h
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
isc::agent
Definition agent_parser.cc:148
isc::agent::CTRL_AGENT_CONFIG_CHECK_FAIL
const isc::log::MessageID CTRL_AGENT_CONFIG_CHECK_FAIL
Definition ca_messages.h:15
isc::agent::CtrlAgentCfgContextPtr
boost::shared_ptr< CtrlAgentCfgContext > CtrlAgentCfgContextPtr
Pointer to a configuration context.
Definition ca_cfg_mgr.h:24
isc::agent::agent_logger
isc::log::Logger agent_logger("ctrl-agent")
Control Agent logger.
Definition ca_log.h:18
isc::agent::CTRL_AGENT_CONFIG_FAIL
const isc::log::MessageID CTRL_AGENT_CONFIG_FAIL
Definition ca_messages.h:16
isc::config
Definition command_interpreter.cc:23
isc::config::CONTROL_RESULT_ERROR
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
Definition command_interpreter.h:45
isc::config::createAnswer
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
Definition command_interpreter.cc:54
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition command_interpreter.h:42
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::hooks::HookLibsCollection
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition libinfo.h:51
isc::http::HttpAuthConfigPtr
boost::shared_ptr< HttpAuthConfig > HttpAuthConfigPtr
Type of shared pointers to HTTP authentication configuration.
Definition auth_config.h:97
isc::process
Definition cb_ctl_base.h:22
isc::process::ConfigPtr
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition config_base.h:176
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition user_context.cc:15
isc::data::UserContext::toElement
static data::ElementPtr toElement(data::ConstElementPtr map)
Copy an Element map.
Definition user_context.cc:24