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