Kea 3.0.3-git
bin/agent/simple_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2026 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>
14#include <hooks/hooks_manager.h>
15#include <hooks/hooks_parser.h>
17
18using namespace isc::data;
19using namespace isc::dhcp;
20using namespace isc::asiolink;
21using namespace isc::config;
22
23namespace isc {
24namespace agent {
39
44 { "http-host", Element::string, "127.0.0.1" },
45 { "http-port", Element::integer, "8000" },
46 { "trust-anchor", Element::string, "" },
47 { "cert-file", Element::string, "" },
48 { "key-file", Element::string, "" },
49 { "cert-required", Element::boolean, "true" }
50};
51
54 { "type", Element::string, "basic" },
55 { "realm", Element::string, "kea-control-agent" },
56 { "directory", Element::string, "" }
57};
58
62 { "socket-type", Element::string, "unix" }
63};
64
66
70
72 size_t cnt = 0;
73
74 // Set global defaults first.
75 cnt = setDefaults(global, AGENT_DEFAULTS);
76
77 // After set the defaults for authentication if it exists.
78 ConstElementPtr authentication = global->get("authentication");
79 if (authentication) {
80 ElementPtr auth = boost::const_pointer_cast<Element>(authentication);
81 if (auth) {
83 }
84 }
85
86 // Now set the defaults for control-sockets, if any.
87 ConstElementPtr sockets = global->get("control-sockets");
88 if (sockets) {
89 ElementPtr d2 = boost::const_pointer_cast<Element>(sockets->get("d2"));
90 if (d2) {
92 }
93
94 ElementPtr d4 = boost::const_pointer_cast<Element>(sockets->get("dhcp4"));
95 if (d4) {
97 }
98
99 ElementPtr d6 = boost::const_pointer_cast<Element>(sockets->get("dhcp6"));
100 if (d6) {
102 }
103 }
104
105 return (cnt);
106}
107
108void
110 ConstElementPtr ca = config->get("trust-anchor");
111 ConstElementPtr cert = config->get("cert-file");
112 ConstElementPtr key = config->get("key-file");
113 bool have_ca = (ca && !ca->stringValue().empty());
114 bool have_cert = (cert && !cert->stringValue().empty());
115 bool have_key = (key && !key->stringValue().empty());
116 if (!have_ca && !have_cert && !have_key) {
117 // No TLS parameter so TLS is not used.
118 return;
119 }
120 // TLS is used: all 3 parameters are required.
121 if (!have_ca) {
122 isc_throw(ConfigError, "trust-anchor parameter is missing or empty:"
123 " all or none of TLS parameters must be set");
124 }
125 if (!have_cert) {
126 isc_throw(ConfigError, "cert-file parameter is missing or empty:"
127 " all or none of TLS parameters must be set");
128 }
129 if (!have_key) {
130 isc_throw(ConfigError, "key-file parameter is missing or empty:"
131 " all or none of TLS parameters must be set");
132 }
133}
134
135void
138 bool check_only) {
139
140 // Let's get the HTTP parameters first.
141 ctx->setHttpHost(SimpleParser::getString(config, "http-host"));
142 ctx->setHttpPort(SimpleParser::getIntType<uint16_t>(config, "http-port"));
143
144 // TLS parameter are second.
145 ctx->setTrustAnchor(SimpleParser::getString(config, "trust-anchor"));
146 ctx->setCertFile(SimpleParser::getString(config, "cert-file"));
147 ctx->setKeyFile(SimpleParser::getString(config, "key-file"));
148 ctx->setCertRequired(SimpleParser::getBoolean(config, "cert-required"));
149
150 // Control sockets are third.
151 ConstElementPtr ctrl_sockets = config->get("control-sockets");
152 if (ctrl_sockets) {
153 auto const& sockets_map = ctrl_sockets->mapValue();
154 for (auto const& cs : sockets_map) {
155 if (!cs.second->get("socket-name")) {
156 isc_throw(DhcpConfigError, "missing parameter 'socket-name' from '" << cs.first
157 << "' service map (" << cs.second->getPosition() << ")");
158 }
159 // Add a validated socket name so we can suppress it in
160 // toElement() but don't have to revalidate it every time we
161 // want to use it.
162 auto mutable_socket_info = boost::const_pointer_cast<Element>(cs.second);
163 std::string socket_name = mutable_socket_info->get("socket-name")->stringValue();
164 auto validated_name = UnixCommandConfig::validatePath(socket_name);
165 mutable_socket_info->set("validated-socket-name", Element::create(validated_name));
166 ctx->setControlSocketInfo(mutable_socket_info, cs.first);
167 }
168 }
169
170 // Basic HTTP authentications are fourth.
171 ConstElementPtr auth_config = config->get("authentication");
172 if (auth_config) {
173 using namespace isc::http;
175 auth->parse(auth_config);
176 ctx->setAuthConfig(auth);
177 }
178
179 // HTTP headers are fifth.
180 ConstElementPtr headers_config = config->get("http-headers");
181 if (headers_config) {
182 using namespace isc::http;
183 ctx->setHttpHeaders(parseCfgHttpHeaders(headers_config));
184 }
185
186 // User context can be done at anytime.
187 ConstElementPtr user_context = config->get("user-context");
188 if (user_context) {
189 ctx->setContext(user_context);
190 }
191
192 // Finally, let's get the hook libs!
193 using namespace isc::hooks;
194 HooksConfig& libraries = ctx->getHooksConfig();
195 ConstElementPtr hooks = config->get("hooks-libraries");
196 if (hooks) {
197 HooksLibrariesParser hooks_parser;
198 hooks_parser.parse(libraries, hooks);
199 libraries.verifyLibraries(hooks->getPosition(), false);
200 }
201
202 if (!check_only) {
203 // This occurs last as if it succeeds, there is no easy way
204 // revert it. As a result, the failure to commit a subsequent
205 // change causes problems when trying to roll back.
207 static_cast<void>(HooksManager::unloadLibraries());
209 libraries.loadLibraries(false);
210 }
211}
212
213}
214}
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
@ integer
Definition data.h:153
@ boolean
Definition data.h:155
@ string
Definition data.h:157
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 validatePath(const std::string socket_path)
Validates a path against the supported path for unix control sockets.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
int_type getIntType(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer value with range checking 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.
To be removed. Please use ConfigError instead.
Wrapper class that holds hooks libraries configuration.
void verifyLibraries(const isc::data::Element::Position &position, bool multi_threading_enabled) const
Verifies that libraries stored in libraries_ are valid.
void loadLibraries(bool multi_threading_enabled) const
Commits hooks libraries configuration.
Parser for hooks library list.
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
static bool unloadLibraries()
Unload libraries.
static void prepareUnloadLibraries()
Prepare the unloading of libraries.
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:24
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
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:29
boost::shared_ptr< BasicHttpAuthConfig > BasicHttpAuthConfigPtr
Type of shared pointers to basic HTTP authentication configuration.
CfgHttpHeaders parseCfgHttpHeaders(const ConstElementPtr &config)
Parse config HTTP headers.
Defines the logger used by the top-level component of kea-lfc.