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