Kea  1.9.9-git
bin/agent/simple_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-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/simple_parser.h>
10 #include <cc/data.h>
11 #include <cc/dhcp_config_error.h>
12 #include <hooks/hooks_manager.h>
13 #include <hooks/hooks_parser.h>
14 #include <http/basic_auth_config.h>
15 #include <boost/foreach.hpp>
16 
17 using namespace isc::data;
18 
19 namespace isc {
20 namespace agent {
35 
39 const SimpleDefaults AgentSimpleParser::AGENT_DEFAULTS = {
40  { "http-host", Element::string, "127.0.0.1" },
41  { "http-port", Element::integer, "8000" },
42  { "trust-anchor", Element::string, "" },
43  { "cert-file", Element::string, "" },
44  { "key-file", Element::string, "" },
45  { "cert-required", Element::boolean, "true" }
46 };
47 
49 const SimpleDefaults AgentSimpleParser::AUTH_DEFAULTS = {
50  { "type", Element::string, "basic" },
51  { "realm", Element::string, "kea-control-agent" }
52 };
53 
56 const SimpleDefaults AgentSimpleParser::SOCKET_DEFAULTS = {
57  { "socket-type", Element::string, "unix" }
58 };
59 
61 
65 
66 size_t AgentSimpleParser::setAllDefaults(const isc::data::ElementPtr& global) {
67  size_t cnt = 0;
68 
69  // Set global defaults first.
70  cnt = setDefaults(global, AGENT_DEFAULTS);
71 
72  // After set the defaults for authentication if it exists.
73  ConstElementPtr authentication = global->get("authentication");
74  if (authentication) {
75  ElementPtr auth = boost::const_pointer_cast<Element>(authentication);
76  if (auth) {
77  cnt += SimpleParser::setDefaults(auth, AUTH_DEFAULTS);
78  }
79  }
80 
81  // Now set the defaults for control-sockets, if any.
82  ConstElementPtr sockets = global->get("control-sockets");
83  if (sockets) {
84  ElementPtr d2 = boost::const_pointer_cast<Element>(sockets->get("d2"));
85  if (d2) {
86  cnt += SimpleParser::setDefaults(d2, SOCKET_DEFAULTS);
87  }
88 
89  ElementPtr d4 = boost::const_pointer_cast<Element>(sockets->get("dhcp4"));
90  if (d4) {
91  cnt += SimpleParser::setDefaults(d4, SOCKET_DEFAULTS);
92  }
93 
94  ElementPtr d6 = boost::const_pointer_cast<Element>(sockets->get("dhcp6"));
95  if (d6) {
96  cnt += SimpleParser::setDefaults(d6, SOCKET_DEFAULTS);
97  }
98  }
99 
100  return (cnt);
101 }
102 
103 void
104 AgentSimpleParser::checkTlsSetup(const isc::data::ConstElementPtr& config) {
105  ConstElementPtr ca = config->get("trust-anchor");
106  ConstElementPtr cert = config->get("cert-file");
107  ConstElementPtr key = config->get("key-file");
108  bool have_ca = (ca && !ca->stringValue().empty());
109  bool have_cert = (cert && !cert->stringValue().empty());
110  bool have_key = (key && !key->stringValue().empty());
111  if (!have_ca && !have_cert && !have_key) {
112  // No TLS parameter so TLS is not used.
113  return;
114  }
115  // TLS is used: all 3 parameters are required.
116  if (!have_ca) {
117  isc_throw(ConfigError, "trust-anchor parameter is missing or empty:"
118  " all or none of TLS parameters must be set");
119  }
120  if (!have_cert) {
121  isc_throw(ConfigError, "cert-file parameter is missing or empty:"
122  " all or none of TLS parameters must be set");
123  }
124  if (!have_key) {
125  isc_throw(ConfigError, "key-file parameter is missing or empty:"
126  " all or none of TLS parameters must be set");
127  }
128 }
129 
130 void
131 AgentSimpleParser::parse(const CtrlAgentCfgContextPtr& ctx,
132  const isc::data::ConstElementPtr& config,
133  bool check_only) {
134 
135  // Let's get the HTTP parameters first.
136  ctx->setHttpHost(SimpleParser::getString(config, "http-host"));
137  ctx->setHttpPort(SimpleParser::getIntType<uint16_t>(config, "http-port"));
138 
139  // TLS parameter are second.
140  ctx->setTrustAnchor(SimpleParser::getString(config, "trust-anchor"));
141  ctx->setCertFile(SimpleParser::getString(config, "cert-file"));
142  ctx->setKeyFile(SimpleParser::getString(config, "key-file"));
143  ctx->setCertRequired(SimpleParser::getBoolean(config, "cert-required"));
144 
145  // Control sockets are third.
146  ConstElementPtr ctrl_sockets = config->get("control-sockets");
147  if (ctrl_sockets) {
148  auto sockets_map = ctrl_sockets->mapValue();
149  for (auto cs = sockets_map.cbegin(); cs != sockets_map.cend(); ++cs) {
150  ctx->setControlSocketInfo(cs->second, cs->first);
151  }
152  }
153 
154  // Basic HTTP authentications are forth.
155  ConstElementPtr auth_config = config->get("authentication");
156  if (auth_config) {
157  using namespace isc::http;
159  auth->parse(auth_config);
160  ctx->setAuthConfig(auth);
161  }
162 
163  // User context can be done at anytime.
164  ConstElementPtr user_context = config->get("user-context");
165  if (user_context) {
166  ctx->setContext(user_context);
167  }
168 
169  // Finally, let's get the hook libs!
170  using namespace isc::hooks;
171  HooksConfig& libraries = ctx->getHooksConfig();
172  ConstElementPtr hooks = config->get("hooks-libraries");
173  if (hooks) {
174  HooksLibrariesParser hooks_parser;
175  hooks_parser.parse(libraries, hooks);
176  libraries.verifyLibraries(hooks->getPosition());
177  }
178 
179  if (!check_only) {
180  // This occurs last as if it succeeds, there is no easy way
181  // revert it. As a result, the failure to commit a subsequent
182  // change causes problems when trying to roll back.
183  HooksManager::prepareUnloadLibraries();
184  static_cast<void>(HooksManager::unloadLibraries());
185  libraries.loadLibraries();
186  }
187 }
188 
189 }
190 }
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
Parser for hooks library list.
Definition: hooks_parser.h:21
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
Basic HTTP authentication configuration.
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
An exception that is thrown if an error occurs while configuring any server.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
void loadLibraries() const
Commits hooks libraries configuration.
Definition: hooks_config.cc:55
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< BasicHttpAuthConfig > BasicHttpAuthConfigPtr
Type of shared pointers to basic HTTP authentication configuration.
The Element class represents a piece of data, used by the command channel and configuration parts...
Definition: data.h:66
boost::shared_ptr< CtrlAgentCfgContext > CtrlAgentCfgContextPtr
Pointer to a configuration context.
Definition: ca_cfg_mgr.h:21
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:28
void verifyLibraries(const isc::data::Element::Position &position) const
Verifies that libraries stored in libraries_ are valid.
Definition: hooks_config.cc:20