Kea  2.3.3-git
bin/agent/simple_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2022 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  { "directory", Element::string, "" }
53 };
54 
57 const SimpleDefaults AgentSimpleParser::SOCKET_DEFAULTS = {
58  { "socket-type", Element::string, "unix" }
59 };
60 
62 
66 
67 size_t AgentSimpleParser::setAllDefaults(const isc::data::ElementPtr& global) {
68  size_t cnt = 0;
69 
70  // Set global defaults first.
71  cnt = setDefaults(global, AGENT_DEFAULTS);
72 
73  // After set the defaults for authentication if it exists.
74  ConstElementPtr authentication = global->get("authentication");
75  if (authentication) {
76  ElementPtr auth = boost::const_pointer_cast<Element>(authentication);
77  if (auth) {
78  cnt += SimpleParser::setDefaults(auth, AUTH_DEFAULTS);
79  }
80  }
81 
82  // Now set the defaults for control-sockets, if any.
83  ConstElementPtr sockets = global->get("control-sockets");
84  if (sockets) {
85  ElementPtr d2 = boost::const_pointer_cast<Element>(sockets->get("d2"));
86  if (d2) {
87  cnt += SimpleParser::setDefaults(d2, SOCKET_DEFAULTS);
88  }
89 
90  ElementPtr d4 = boost::const_pointer_cast<Element>(sockets->get("dhcp4"));
91  if (d4) {
92  cnt += SimpleParser::setDefaults(d4, SOCKET_DEFAULTS);
93  }
94 
95  ElementPtr d6 = boost::const_pointer_cast<Element>(sockets->get("dhcp6"));
96  if (d6) {
97  cnt += SimpleParser::setDefaults(d6, SOCKET_DEFAULTS);
98  }
99  }
100 
101  return (cnt);
102 }
103 
104 void
105 AgentSimpleParser::checkTlsSetup(const isc::data::ConstElementPtr& config) {
106  ConstElementPtr ca = config->get("trust-anchor");
107  ConstElementPtr cert = config->get("cert-file");
108  ConstElementPtr key = config->get("key-file");
109  bool have_ca = (ca && !ca->stringValue().empty());
110  bool have_cert = (cert && !cert->stringValue().empty());
111  bool have_key = (key && !key->stringValue().empty());
112  if (!have_ca && !have_cert && !have_key) {
113  // No TLS parameter so TLS is not used.
114  return;
115  }
116  // TLS is used: all 3 parameters are required.
117  if (!have_ca) {
118  isc_throw(ConfigError, "trust-anchor parameter is missing or empty:"
119  " all or none of TLS parameters must be set");
120  }
121  if (!have_cert) {
122  isc_throw(ConfigError, "cert-file parameter is missing or empty:"
123  " all or none of TLS parameters must be set");
124  }
125  if (!have_key) {
126  isc_throw(ConfigError, "key-file parameter is missing or empty:"
127  " all or none of TLS parameters must be set");
128  }
129 }
130 
131 void
132 AgentSimpleParser::parse(const CtrlAgentCfgContextPtr& ctx,
133  const isc::data::ConstElementPtr& config,
134  bool check_only) {
135 
136  // Let's get the HTTP parameters first.
137  ctx->setHttpHost(SimpleParser::getString(config, "http-host"));
138  ctx->setHttpPort(SimpleParser::getIntType<uint16_t>(config, "http-port"));
139 
140  // TLS parameter are second.
141  ctx->setTrustAnchor(SimpleParser::getString(config, "trust-anchor"));
142  ctx->setCertFile(SimpleParser::getString(config, "cert-file"));
143  ctx->setKeyFile(SimpleParser::getString(config, "key-file"));
144  ctx->setCertRequired(SimpleParser::getBoolean(config, "cert-required"));
145 
146  // Control sockets are third.
147  ConstElementPtr ctrl_sockets = config->get("control-sockets");
148  if (ctrl_sockets) {
149  auto sockets_map = ctrl_sockets->mapValue();
150  for (auto cs = sockets_map.cbegin(); cs != sockets_map.cend(); ++cs) {
151  ctx->setControlSocketInfo(cs->second, cs->first);
152  }
153  }
154 
155  // Basic HTTP authentications are forth.
156  ConstElementPtr auth_config = config->get("authentication");
157  if (auth_config) {
158  using namespace isc::http;
160  auth->parse(auth_config);
161  ctx->setAuthConfig(auth);
162  }
163 
164  // User context can be done at anytime.
165  ConstElementPtr user_context = config->get("user-context");
166  if (user_context) {
167  ctx->setContext(user_context);
168  }
169 
170  // Finally, let's get the hook libs!
171  using namespace isc::hooks;
172  HooksConfig& libraries = ctx->getHooksConfig();
173  ConstElementPtr hooks = config->get("hooks-libraries");
174  if (hooks) {
175  HooksLibrariesParser hooks_parser;
176  hooks_parser.parse(libraries, hooks);
177  libraries.verifyLibraries(hooks->getPosition());
178  }
179 
180  if (!check_only) {
181  // This occurs last as if it succeeds, there is no easy way
182  // revert it. As a result, the failure to commit a subsequent
183  // change causes problems when trying to roll back.
184  HooksManager::prepareUnloadLibraries();
185  static_cast<void>(HooksManager::unloadLibraries());
186  libraries.loadLibraries();
187  }
188 }
189 
190 }
191 }
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
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:24
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:27
void verifyLibraries(const isc::data::Element::Position &position) const
Verifies that libraries stored in libraries_ are valid.
Definition: hooks_config.cc:20
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-lfc.
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
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:70
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