Kea 2.5.8
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
10#include <cc/data.h>
12#include <hooks/hooks_manager.h>
13#include <hooks/hooks_parser.h>
15
16using namespace isc::data;
17
18namespace isc {
19namespace agent {
34
39 { "http-host", Element::string, "127.0.0.1" },
40 { "http-port", Element::integer, "8000" },
41 { "trust-anchor", Element::string, "" },
42 { "cert-file", Element::string, "" },
43 { "key-file", Element::string, "" },
44 { "cert-required", Element::boolean, "true" }
45};
46
49 { "type", Element::string, "basic" },
50 { "realm", Element::string, "kea-control-agent" },
51 { "directory", Element::string, "" }
52};
53
57 { "socket-type", Element::string, "unix" }
58};
59
61
65
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) {
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) {
87 }
88
89 ElementPtr d4 = boost::const_pointer_cast<Element>(sockets->get("dhcp4"));
90 if (d4) {
92 }
93
94 ElementPtr d6 = boost::const_pointer_cast<Element>(sockets->get("dhcp6"));
95 if (d6) {
97 }
98 }
99
100 return (cnt);
101}
102
103void
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
130void
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 const& sockets_map = ctrl_sockets->mapValue();
149 for (auto const& cs : sockets_map) {
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(), false);
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(false);
186 }
187}
188
189}
190}
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.
Definition: hooks_config.h:36
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
void verifyLibraries(const isc::data::Element::Position &position, bool multi_threading_enabled) const
Verifies that libraries stored in libraries_ are valid.
Definition: hooks_config.cc:20
void loadLibraries(bool multi_threading_enabled) const
Commits hooks libraries configuration.
Definition: hooks_config.cc:57
Parser for hooks library list.
Definition: hooks_parser.h:21
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:27
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.