1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
/// @file
/// @brief Basic HTTP Authentication callout library
#include <config.h>
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <exceptions/exceptions.h>
#include <hooks/hooks.h>
#include <http/basic_auth_config.h>
#include <http/post_request_json.h>
#include <http/response_creator.h>
#include <boost/shared_ptr.hpp>
#include <boost/pointer_cast.hpp>
using namespace isc::data;
using namespace isc::hooks;
using namespace isc::http;
using namespace isc;
using namespace std;
namespace {
/// @brief Response creator.
class ResponseCreator : public HttpResponseCreator {
public:
/// @brief Create a new request.
/// @return Pointer to the new instance of the @ref
/// isc::http::PostHttpRequestJson.
virtual HttpRequestPtr
createNewHttpRequest() const;
/// @brief Create stock HTTP response.
///
/// @param request Pointer to an object representing HTTP request.
/// @param status_code Status code of the response.
/// @return Pointer to an @ref isc::http::HttpResponseJson object
/// representing stock HTTP response.
virtual HttpResponsePtr
createStockHttpResponse(const HttpRequestPtr& request,
const HttpStatusCode& status_code) const;
/// @brief Creates implementation specific HTTP response.
///
/// @param request Pointer to an object representing HTTP request.
/// @return Pointer to an object representing HTTP response.
virtual HttpResponsePtr
createDynamicHttpResponse(HttpRequestPtr request);
};
HttpRequestPtr
ResponseCreator::createNewHttpRequest() const {
return (HttpRequestPtr(new PostHttpRequestJson()));
}
HttpResponsePtr
ResponseCreator::createStockHttpResponse(const HttpRequestPtr& /*request*/,
const HttpStatusCode& status_code) const {
HttpVersion http_version(1, 1);
HttpResponsePtr response(new HttpResponseJson(http_version, status_code));
response->finalize();
return (response);
}
HttpResponsePtr
ResponseCreator::createDynamicHttpResponse(HttpRequestPtr /*request*/) {
isc_throw(NotImplemented, "createDynamicHttpResponse should not be called");
}
/// @brief The type of shared pointers to response creators.
typedef boost::shared_ptr<ResponseCreator> ResponseCreatorPtr;
/// @brief Implementation.
class Impl {
public:
/// @brief Constructor.
Impl();
/// @brief Destructor.
~Impl();
/// @brief Configure.
///
/// @param config element pointer to client list.
void configure(ConstElementPtr config);
/// @brief Basic HTTP authentication configuration.
BasicHttpAuthConfigPtr config_;
/// @brief Response creator.
ResponseCreatorPtr creator_;
};
Impl::Impl()
: config_(new BasicHttpAuthConfig()), creator_(new ResponseCreator()) {
}
Impl::~Impl() {
}
void
Impl::configure(ConstElementPtr config) {
config_->parse(config);
}
/// @brief The type of shared pointers to implementations.
typedef boost::shared_ptr<Impl> ImplPtr;
/// @brief The implementation.
ImplPtr impl;
extern "C" {
// Framework functions.
/// @brief returns Kea hooks version.
int
version() {
return (KEA_HOOKS_VERSION);
}
/// @brief This function is called when the library is loaded.
///
/// @param handle library handle.
/// @return 0 when initialization is successful, 1 otherwise.
int
load(LibraryHandle& handle) {
#ifdef USE_STATIC_LINK
hooksStaticLinkInit();
#endif
try {
impl.reset(new Impl());
ConstElementPtr config = handle.getParameter("config");
impl->configure(config);
} catch (const std::exception& ex) {
std::cerr << "load error: " << ex.what() << std::endl;
return (1);
}
return (0);
}
/// @brief This function is called when the library is unloaded.
///
/// @return always 0.
int
unload() {
impl.reset();
return (0);
}
// Callout functions.
/// @brief This callout is called at the "auth" hook.
///
/// @param handle CalloutHandle.
/// @return 0 upon success, non-zero otherwise.
int
auth(CalloutHandle& handle) {
// Sanity.
if (!impl) {
std::cerr << "no implementation" << std::endl;
return (0);
}
// Get the parameters.
HttpRequestPtr request;
HttpResponseJsonPtr response;
handle.getArgument("request", request);
handle.getArgument("response", response);
if (response) {
std::cerr << "response already set" << std::endl;
return (0);
}
if (!request) {
std::cerr << "no request" << std::endl;
return (0);
}
PostHttpRequestJsonPtr request_json =
boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
if (!request_json) {
std::cerr << "no json post request" << std::endl;
return (0);
}
ConstElementPtr command = request_json->getBodyAsJson();
if (!command) {
std::cerr << "no command" << std::endl;
return (0);
}
if (command->getType() != Element::map) {
std::cerr << "command is not a map" << std::endl;
return (0);
}
// Modify request.
int extra = 0;
ConstElementPtr extra_elem = command->get("extra");
ElementPtr mutable_command = boost::const_pointer_cast<Element>(command);
if (extra_elem) {
if (extra_elem->getType() == Element::integer) {
extra = extra_elem->intValue();
}
mutable_command->remove("extra");
request_json->setBodyAsJson(command);
}
handle.setContext("extra", extra);
// Perform authentication.
response = impl->config_->checkAuth(*impl->creator_, request);
// Set parameters.
handle.setArgument("request", request);
handle.setArgument("response", response);
return (0);
}
/// @brief This callout is called at the "response" hook.
///
/// @param handle CalloutHandle.
/// @return 0 upon success, non-zero otherwise.
int
response(CalloutHandle& handle) {
// Sanity.
if (!impl) {
std::cerr << "no implementation" << std::endl;
return (0);
}
// Get the parameters.
HttpRequestPtr request;
HttpResponseJsonPtr response;
handle.getArgument("request", request);
handle.getArgument("response", response);
if (!request) {
std::cerr << "no request" << std::endl;
return (0);
}
if (!response) {
std::cerr << "no response" << std::endl;
return (0);
}
// Modify response.
ConstElementPtr body = response->getBodyAsJson();
if (!body) {
std::cerr << "no body" << std::endl;
return (0);
}
if (body->getType() != Element::list) {
std::cerr << "body is not a list" << std::endl;
return (0);
}
if (body->size() < 1) {
std::cerr << "body is empty" << std::endl;
return (0);
}
ConstElementPtr answer = body->get(0);
if (!answer || (answer->getType() != Element::map)) {
std::cerr << "answer is not map" << std::endl;
return (0);
}
ElementPtr mutable_answer = boost::const_pointer_cast<Element>(answer);
try {
int extra = 0;
handle.getContext("extra", extra);
mutable_answer->set("got", Element::create(extra));
} catch (const NoSuchCalloutContext&) {
std::cerr << "can't find 'extra' context\n";
} catch (...) {
std::cerr << "getContext('extra') failed\n";
}
response->setBodyAsJson(body);
// Set parameters.
handle.setArgument("response", response);
return (0);
}
}
}
|