-
Notifications
You must be signed in to change notification settings - Fork 612
Expand file tree
/
Copy pathhttpserverconnection.cpp
More file actions
581 lines (463 loc) · 16.1 KB
/
Copy pathhttpserverconnection.cpp
File metadata and controls
581 lines (463 loc) · 16.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "remote/httpserverconnection.hpp"
#include "remote/httphandler.hpp"
#include "remote/httputility.hpp"
#include "remote/apilistener.hpp"
#include "remote/apifunction.hpp"
#include "remote/jsonrpc.hpp"
#include "base/application.hpp"
#include "base/base64.hpp"
#include "base/convert.hpp"
#include "base/configtype.hpp"
#include "base/defer.hpp"
#include "base/exception.hpp"
#include "base/io-engine.hpp"
#include "base/logger.hpp"
#include "base/objectlock.hpp"
#include "base/timer.hpp"
#include "base/tlsstream.hpp"
#include "base/utility.hpp"
#include <limits>
#include <memory>
#include <stdexcept>
#include <boost/asio/error.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/thread/once.hpp>
using namespace icinga;
auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion());
HttpServerConnection::HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream)
: HttpServerConnection(waitGroup, identity, authenticated, stream, IoEngine::Get().GetIoContext())
{
}
HttpServerConnection::HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io)
: m_WaitGroup(waitGroup), m_Stream(stream), m_IoStrand(io), m_ShuttingDown(false), m_ConnectionReusable(true), m_CheckLivenessTimer(io)
{
if (authenticated) {
m_ApiUser = ApiUser::GetByClientCN(identity);
}
{
std::ostringstream address;
auto endpoint (stream->lowest_layer().remote_endpoint());
address << '[' << endpoint.address() << "]:" << endpoint.port();
m_PeerAddress = address.str();
}
}
void HttpServerConnection::Start()
{
namespace asio = boost::asio;
HttpServerConnection::Ptr keepAlive (this);
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) { ProcessMessages(yc); });
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) { CheckLiveness(yc); });
}
/**
* Tries to asynchronously shut down the SSL stream and underlying socket.
*
* It is important to note that this method should only be called from within a coroutine that uses `m_IoStrand`.
*
* @param yc boost::asio::yield_context The coroutine yield context which you are calling this method from.
*/
void HttpServerConnection::Disconnect(boost::asio::yield_context yc)
{
namespace asio = boost::asio;
if (m_ShuttingDown) {
return;
}
m_ShuttingDown = true;
Log(LogInformation, "HttpServerConnection")
<< "HTTP client disconnected (from " << m_PeerAddress << ")";
m_CheckLivenessTimer.cancel();
m_Stream->GracefulDisconnect(m_IoStrand, yc);
auto listener (ApiListener::GetInstance());
if (listener) {
listener->RemoveHttpClient(this);
}
}
/**
* Starts a coroutine that continually reads from the stream to detect a disconnect from the client.
*
* This can be accessed inside an @c HttpHandler via the HttpResponse::StartStreaming() method by
* passing true as the argument, expressing that disconnect detection is desired.
*/
void HttpServerConnection::StartDetectClientSideShutdown()
{
namespace asio = boost::asio;
m_ConnectionReusable = false;
HttpServerConnection::Ptr keepAlive (this);
/* Technically it would be possible to detect disconnects on the TCP-side by setting the
* socket to non-blocking and then performing a read directly on the socket with the message_peek
* flag. As the TCP FIN message will put the connection into a CLOSE_WAIT even if the kernel
* buffer is full, this would technically be reliable way of detecting a shutdown and free
* of side-effects.
*
* However, for detecting the close_notify on the SSL/TLS-side, an async_fill() would be necessary
* when the check on the TCP level above returns that there are readable bytes (and no FIN/eof).
* If this async_fill() then buffers more application data and not an immediate eof, we could
* attempt to read another message before disconnecting.
*
* This could either be done at the level of the handlers, via the @c HttpApiResponse class, or
* generally as a separate coroutine here in @c HttpServerConnection, both (mostly) side-effect
* free and without affecting the state of the connection.
*
* However, due to the complexity of this approach, involving several asio operations, message
* flags, synchronous and asynchronous operations in blocking and non-blocking mode, ioctl cmds,
* etc., it was decided to stick with a simple reading loop, started conditionally on request by
* the handler.
*/
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
if (!m_ShuttingDown) {
char buf[128];
asio::mutable_buffer readBuf (buf, 128);
boost::system::error_code ec;
do {
m_Stream->async_read_some(readBuf, yc[ec]);
} while (!ec);
Disconnect(yc);
}
});
}
bool HttpServerConnection::Disconnected()
{
return m_ShuttingDown;
}
void HttpServerConnection::SetLivenessTimeout(std::chrono::milliseconds timeout)
{
m_LivenessTimeout = timeout;
}
static inline
bool EnsureValidHeaders(
boost::beast::flat_buffer& buf,
HttpApiRequest& request,
HttpApiResponse& response,
bool& shuttingDown,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
if (shuttingDown)
return false;
bool httpError = false;
String errorMsg;
boost::system::error_code ec;
request.ParseHeader(buf, yc[ec]);
if (ec) {
if (ec == boost::asio::error::operation_aborted)
return false;
errorMsg = ec.message();
httpError = true;
} else {
switch (request.version()) {
case 10:
case 11:
break;
default:
errorMsg = "Unsupported HTTP version";
}
}
if (!errorMsg.IsEmpty() || httpError) {
response.result(http::status::bad_request);
if (!httpError && request[http::field::accept] == "application/json") {
HttpUtility::SendJsonError(response, nullptr, 400, "Bad Request: " + errorMsg);
} else {
response.set(http::field::content_type, "text/html");
response.body() << "<h1>Bad Request</h1><p><pre>" << errorMsg << "</pre></p>";
}
response.set(http::field::connection, "close");
response.Flush(yc);
return false;
}
return true;
}
static inline
void HandleExpect100(
const Shared<AsioTlsStream>::Ptr& stream,
const HttpApiRequest& request,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
if (request[http::field::expect] == "100-continue") {
HttpApiResponse response{stream};
response.result(http::status::continue_);
response.Flush(yc);
}
}
static inline
bool HandleAccessControl(
const HttpApiRequest& request,
HttpApiResponse& response,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
auto listener (ApiListener::GetInstance());
if (listener) {
auto headerAllowOrigin (listener->GetAccessControlAllowOrigin());
if (headerAllowOrigin) {
auto allowedOrigins (headerAllowOrigin->ToSet<String>());
if (!allowedOrigins.empty()) {
auto& origin (request[http::field::origin]);
if (allowedOrigins.find(std::string(origin)) != allowedOrigins.end()) {
response.set(http::field::access_control_allow_origin, origin);
}
response.set(http::field::access_control_allow_credentials, "true");
if (request.method() == http::verb::options && !request[http::field::access_control_request_method].empty()) {
response.result(http::status::ok);
response.set(http::field::access_control_allow_methods, "GET, POST, PUT, DELETE");
response.set(http::field::access_control_allow_headers, "Authorization, Content-Type, X-HTTP-Method-Override");
response.body() << "Preflight OK";
response.set(http::field::connection, "close");
response.Flush(yc);
return false;
}
}
}
}
return true;
}
static inline
bool EnsureAcceptHeader(
const HttpApiRequest& request,
HttpApiResponse& response,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
if (request.method() != http::verb::get && request[http::field::accept] != "application/json") {
response.result(http::status::bad_request);
response.set(http::field::content_type, "text/html");
response.body() << "<h1>Accept header is missing or not set to 'application/json'.</h1>";
response.set(http::field::connection, "close");
response.Flush(yc);
return false;
}
return true;
}
static inline
bool EnsureAuthenticatedUser(
const HttpApiRequest& request,
HttpApiResponse& response,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
if (!request.User()) {
Log(LogWarning, "HttpServerConnection")
<< "Unauthorized request: " << request.method_string() << ' ' << request.target();
response.result(http::status::unauthorized);
response.set(http::field::www_authenticate, "Basic realm=\"Icinga 2\"");
response.set(http::field::connection, "close");
if (request[http::field::accept] == "application/json") {
HttpUtility::SendJsonError(response, nullptr, 401, "Unauthorized. Please check your user credentials.");
} else {
response.set(http::field::content_type, "text/html");
response.body() << "<h1>Unauthorized. Please check your user credentials.</h1>";
}
response.Flush(yc);
return false;
}
return true;
}
static inline
bool EnsureValidBody(
boost::beast::flat_buffer& buf,
HttpApiRequest& request,
HttpApiResponse& response,
bool& shuttingDown,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
{
size_t maxSize = 1024 * 1024;
Array::Ptr permissions = request.User()->GetPermissions();
if (permissions) {
ObjectLock olock(permissions);
for (const Value& permissionInfo : permissions) {
String permission;
if (permissionInfo.IsObjectType<Dictionary>()) {
permission = static_cast<Dictionary::Ptr>(permissionInfo)->Get("permission");
} else {
permission = permissionInfo;
}
static std::vector<std::pair<String, size_t>> specialContentLengthLimits {
{ "config/modify", 512 * 1024 * 1024 }
};
for (const auto& limitInfo : specialContentLengthLimits) {
if (limitInfo.second <= maxSize) {
continue;
}
if (Utility::Match(permission, limitInfo.first)) {
maxSize = limitInfo.second;
}
}
}
}
request.Parser().body_limit(maxSize);
}
if (shuttingDown)
return false;
boost::system::error_code ec;
request.ParseBody(buf, yc[ec]);
if (ec) {
if (ec == boost::asio::error::operation_aborted)
return false;
/**
* Unfortunately there's no way to tell an HTTP protocol error
* from an error on a lower layer:
*
* <https://github.com/boostorg/beast/issues/643>
*/
response.result(http::status::bad_request);
if (request[http::field::accept] == "application/json") {
HttpUtility::SendJsonError(response, nullptr, 400, "Bad Request: " + ec.message());
} else {
response.set(http::field::content_type, "text/html");
response.body() << "<h1>Bad Request</h1><p><pre>" << ec.message() << "</pre></p>";
}
response.set(http::field::connection, "close");
response.Flush(yc);
return false;
}
return true;
}
static inline
void ProcessRequest(
HttpApiRequest& request,
HttpApiResponse& response,
const WaitGroup::Ptr& waitGroup,
std::chrono::steady_clock::duration& cpuBoundWorkTime,
boost::asio::yield_context& yc,
boost::asio::io_context::strand& strand
)
{
try {
// Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads.
auto start (std::chrono::steady_clock::now());
response.StartCpuBoundWork(yc, strand);
cpuBoundWorkTime = std::chrono::steady_clock::now() - start;
HttpHandler::ProcessRequest(waitGroup, request, response, yc);
response.body().Finish();
} catch (const std::exception& ex) {
/* Since we don't know the state the stream is in, we can't send an error response and
* have to just cause a disconnect here.
*/
if (response.HasSerializationStarted()) {
throw;
}
HttpUtility::SendJsonError(response, request.Params(), 500, "Unhandled exception", DiagnosticInformation(ex));
}
response.Flush(yc);
}
void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
{
namespace beast = boost::beast;
namespace http = beast::http;
namespace ch = std::chrono;
try {
/* Do not reset the buffer in the state machine.
* EnsureValidHeaders already reads from the stream into the buffer,
* EnsureValidBody continues. ProcessRequest() actually handles the request
* and needs the full buffer.
*/
beast::flat_buffer buf;
while (m_WaitGroup->IsLockable()) {
m_Seen = ch::steady_clock::now();
HttpApiRequest request(m_Stream);
HttpApiResponse response(m_Stream, this);
request.Parser().header_limit(1024 * 1024);
request.Parser().body_limit(-1);
response.set(http::field::server, l_ServerHeader);
if (auto listener (ApiListener::GetInstance()); listener) {
if (Dictionary::Ptr headers = listener->GetHttpResponseHeaders(); headers) {
ObjectLock lock(headers);
for (auto& [header, value] : headers) {
if (value.IsString()) {
response.set(header, value.Get<String>());
}
}
}
}
if (!EnsureValidHeaders(buf, request, response, m_ShuttingDown, yc)) {
break;
}
m_Seen = ch::steady_clock::now();
auto start (ch::steady_clock::now());
{
auto method (http::string_to_verb(request["X-Http-Method-Override"]));
if (method != http::verb::unknown) {
request.method(method);
}
}
HandleExpect100(m_Stream, request, yc);
if (m_ApiUser) {
request.User(m_ApiUser);
} else {
request.User(ApiUser::GetByAuthHeader(std::string(request[http::field::authorization])));
}
Log logMsg (LogNotice, "HttpServerConnection");
logMsg << "Request " << request.method_string() << ' ' << request.target()
<< " (from " << m_PeerAddress
<< ", user: " << (request.User() ? request.User()->GetName() : "<unauthenticated>")
<< ", agent: " << request[http::field::user_agent]; //operator[] - Returns the value for a field, or "" if it does not exist.
ch::steady_clock::duration cpuBoundWorkTime(0);
Defer addRespCode ([&response, start, &logMsg, &cpuBoundWorkTime]() {
logMsg << ", status: " << response.result() << ")";
if (cpuBoundWorkTime >= ch::seconds(1)) {
logMsg << " waited " << ch::duration_cast<ch::milliseconds>(cpuBoundWorkTime).count() << "ms on semaphore and";
}
logMsg << " took total " << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now() - start).count() << "ms.";
});
if (!HandleAccessControl(request, response, yc)) {
break;
}
if (!EnsureAcceptHeader(request, response, yc)) {
break;
}
if (!EnsureAuthenticatedUser(request, response, yc)) {
break;
}
if (!EnsureValidBody(buf, request, response, m_ShuttingDown, yc)) {
break;
}
m_Seen = ch::steady_clock::time_point::max();
ProcessRequest(request, response, m_WaitGroup, cpuBoundWorkTime, yc, m_IoStrand);
if (!request.keep_alive() || !m_ConnectionReusable) {
break;
}
}
} catch (const std::exception& ex) {
if (!m_ShuttingDown) {
Log(LogWarning, "HttpServerConnection")
<< "Exception while processing HTTP request from " << m_PeerAddress << ": " << ex.what();
}
}
Disconnect(yc);
}
void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc)
{
boost::system::error_code ec;
for (;;) {
// Wait for the half of the liveness timeout to give the connection some leeway to do other work.
// But never wait longer than 5 seconds to ensure timely shutdowns.
auto sleepTime = std::min(5000ms, m_LivenessTimeout / 2);
m_CheckLivenessTimer.expires_from_now(boost::posix_time::milliseconds(sleepTime.count()));
m_CheckLivenessTimer.async_wait(yc[ec]);
if (m_ShuttingDown) {
break;
}
if (m_LivenessTimeout < std::chrono::steady_clock::now() - m_Seen) {
Log(LogInformation, "HttpServerConnection")
<< "No messages for HTTP connection have been received in the last "
<< std::chrono::duration_cast<std::chrono::seconds>(m_LivenessTimeout).count()
<< " seconds, disconnecting (from " << m_PeerAddress << ").";
Disconnect(yc);
break;
}
}
}