Kea  2.3.3-git
ca_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-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 
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),
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 
40 }
41 
43 }
44 
45 std::string
46 CtrlAgentCfgMgr::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 lib = libs.begin(); lib != libs.end(); ++lib) {
81  s << lib->first << " ";
82  }
83 
84  return (s.str());
85 }
86 
89  return (ConfigPtr(new CtrlAgentCfgContext()));
90 }
91 
93 CtrlAgentCfgMgr::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 
142 std::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 
152 CtrlAgentCfgContext::getControlSocketInfo(const std::string& service) const {
153  auto si = ctrl_sockets_.find(service);
154  return ((si != ctrl_sockets_.end()) ? si->second : ConstElementPtr());
155 }
156 
157 void
159  const std::string& service) {
160  ctrl_sockets_[service] = control_socket;
161 }
162 
163 std::string
165  std::ostringstream s;
166  for (auto si = ctrl_sockets_.cbegin(); si != ctrl_sockets_.end(); ++si) {
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 
182  ElementPtr ca = ConfigBase::toElement();
183  // Set user-context
184  contextToElement(ca);
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 si = ctrl_sockets_.cbegin(); si != ctrl_sockets_.cend(); ++si) {
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
209  ElementPtr result = Element::createMap();
210  result->set("Control-agent", ca);
211 
212  return (result);
213 }
214 
215 } // namespace isc::agent
216 } // namespace isc
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Control Agent configuration.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
std::string getControlSocketInfoSummary() const
Returns socket configuration summary in a textual format.
Definition: ca_cfg_mgr.cc:164
Base class for all configurations.
Definition: config_base.h:33
virtual std::string getConfigSummary(const uint32_t selection) override
Returns configuration summary in the textual format.
Definition: ca_cfg_mgr.cc:46
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Configuration Manager.
Definition: d_cfg_mgr.h:108
const isc::log::MessageID CTRL_AGENT_CONFIG_CHECK_FAIL
Definition: ca_messages.h:15
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
isc::data::ConstElementPtr getControlSocketInfo(const std::string &service) const
Returns information about control socket.
Definition: ca_cfg_mgr.cc:152
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
isc::log::Logger agent_logger("ctrl-agent")
Control Agent logger.
Definition: ca_log.h:18
virtual ~CtrlAgentCfgMgr()
Destructor.
Definition: ca_cfg_mgr.cc:42
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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:143
CtrlAgentCfgContext()
Default constructor.
Definition: ca_cfg_mgr.cc:25
To be removed. Please use ConfigError instead.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: ca_cfg_mgr.cc:181
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID CTRL_AGENT_CONFIG_FAIL
Definition: ca_messages.h:16
void setControlSocketInfo(const isc::data::ConstElementPtr &control_socket, const std::string &service)
Sets information about the control socket.
Definition: ca_cfg_mgr.cc:158
void checkTlsSetup(const isc::data::ConstElementPtr &config)
Check TLS setup consistency i.e.
This file contains several functions and constants that are used for handling commands and responses ...
void parse(const CtrlAgentCfgContextPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the control agent configuration.
The Element class represents a piece of data, used by the command channel and configuration parts...
Definition: data.h:70
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
CtrlAgentCfgMgr()
Constructor.
Definition: ca_cfg_mgr.cc:38
boost::shared_ptr< CtrlAgentCfgContext > CtrlAgentCfgContextPtr
Pointer to a configuration context.
Definition: ca_cfg_mgr.h:21
CtrlAgentCfgContextPtr getCtrlAgentCfgContext()
Convenience method that returns the Control Agent configuration context.
Definition: ca_cfg_mgr.h:266
Control Agent Configuration Context.
Definition: ca_cfg_mgr.h:32
boost::shared_ptr< HttpAuthConfig > HttpAuthConfigPtr
Type of shared pointers to HTTP authentication configuration.
Definition: auth_config.h:97
virtual process::ConfigPtr createNewContext() override
Creates a new, blank CtrlAgentCfgContext context.
Definition: ca_cfg_mgr.cc:88
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176