summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/0005-EventService-https-client-support.patch
blob: 23eac280dba4773191aad318528f13336003426a (plain)
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
From 1f91d5708cbe30610c0c8bbc4910709b99b8f270 Mon Sep 17 00:00:00 2001
From: AppaRao Puli <apparao.puli@linux.intel.com>
Date: Mon, 25 May 2020 16:14:39 +0530
Subject: [PATCH] EventService: https client support

Add https client support for push style
eventing. Using this BMC can push the event
logs/telemetry data to event listener over
secure http channel.

Tested:
 - Created subscription with https destination
   url. Using SubmitTestEvent action set the
   event and can see event on event listener.
 - Validator passed.

Change-Id: I44c3918b39baa2eb5fddda9d635f99aa280a422a
Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
---
 http/http_client.hpp                           | 325 +++++++++++++++++--------
 redfish-core/include/event_service_manager.hpp |   2 +-
 2 files changed, 226 insertions(+), 101 deletions(-)

diff --git a/http/http_client.hpp b/http/http_client.hpp
index e6a7db1..6d3d702 100644
--- a/http/http_client.hpp
+++ b/http/http_client.hpp
@@ -17,6 +17,7 @@
 #include <boost/asio/strand.hpp>
 #include <boost/beast/core.hpp>
 #include <boost/beast/http.hpp>
+#include <boost/beast/ssl.hpp>
 #include <boost/beast/version.hpp>
 
 #include <cstdlib>
@@ -30,12 +31,14 @@ namespace crow
 {
 
 static constexpr uint8_t maxRequestQueueSize = 50;
+static constexpr unsigned int httpReadBodyLimit = 1024;
 
 enum class ConnState
 {
     initialized,
     connectInProgress,
     connectFailed,
+    sslHandshakeInProgress,
     connected,
     sendInProgress,
     sendFailed,
@@ -49,53 +52,97 @@ enum class ConnState
 class HttpClient : public std::enable_shared_from_this<HttpClient>
 {
   private:
+    boost::asio::ssl::context ctx{boost::asio::ssl::context::tlsv12_client};
     boost::beast::tcp_stream conn;
+    std::optional<boost::beast::ssl_stream<boost::beast::tcp_stream&>> sslConn;
     boost::asio::steady_timer timer;
-    boost::beast::flat_buffer buffer;
+    boost::beast::flat_static_buffer<httpReadBodyLimit> buffer;
+    std::optional<
+        boost::beast::http::response_parser<boost::beast::http::string_body>>
+        parser;
     boost::beast::http::request<boost::beast::http::string_body> req;
-    boost::beast::http::response<boost::beast::http::string_body> res;
     boost::asio::ip::tcp::resolver::results_type endpoint;
-    std::vector<std::pair<std::string, std::string>> headers;
+    boost::beast::http::fields fields;
     std::queue<std::string> requestDataQueue;
-    ConnState state;
     std::string subId;
     std::string host;
     std::string port;
     std::string uri;
+    bool useSsl;
     uint32_t retryCount;
     uint32_t maxRetryAttempts;
     uint32_t retryIntervalSecs;
     std::string retryPolicyAction;
     bool runningTimer;
+    ConnState state;
 
     void doConnect()
     {
-        if (state == ConnState::connectInProgress)
+        if (useSsl)
+        {
+            sslConn.emplace(conn, ctx);
+        }
+
+        if ((state == ConnState::connectInProgress) ||
+            (state == ConnState::sslHandshakeInProgress))
         {
             return;
         }
         state = ConnState::connectInProgress;
 
         BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" << port;
-        // Set a timeout on the operation
+
+        auto respHandler =
+            [self(shared_from_this())](const boost::beast::error_code ec,
+                                       const boost::asio::ip::tcp::resolver::
+                                           results_type::endpoint_type& ep) {
+                if (ec)
+                {
+                    BMCWEB_LOG_ERROR << "Connect " << ep
+                                     << " failed: " << ec.message();
+                    self->state = ConnState::connectFailed;
+                    self->checkQueue();
+                    return;
+                }
+                BMCWEB_LOG_DEBUG << "Connected to: " << ep;
+                if (self->sslConn)
+                {
+                    self->performHandshake();
+                }
+                else
+                {
+                    self->state = ConnState::connected;
+                    self->checkQueue();
+                }
+            };
+
         conn.expires_after(std::chrono::seconds(30));
-        conn.async_connect(endpoint, [self(shared_from_this())](
-                                         const boost::beast::error_code& ec,
-                                         const boost::asio::ip::tcp::resolver::
-                                             results_type::endpoint_type& ep) {
-            if (ec)
-            {
-                BMCWEB_LOG_ERROR << "Connect " << ep
-                                 << " failed: " << ec.message();
-                self->state = ConnState::connectFailed;
-                self->checkQueue();
-                return;
-            }
-            self->state = ConnState::connected;
-            BMCWEB_LOG_DEBUG << "Connected to: " << ep;
+        conn.async_connect(endpoint, std::move(respHandler));
+    }
 
-            self->checkQueue();
-        });
+    void performHandshake()
+    {
+        if (state == ConnState::sslHandshakeInProgress)
+        {
+            return;
+        }
+        state = ConnState::sslHandshakeInProgress;
+
+        sslConn->async_handshake(
+            boost::asio::ssl::stream_base::client,
+            [self(shared_from_this())](const boost::beast::error_code ec) {
+                if (ec)
+                {
+                    BMCWEB_LOG_ERROR << "SSL handshake failed: "
+                                     << ec.message();
+                    self->doCloseAndCheckQueue(ConnState::connectFailed);
+                    return;
+                }
+                self->state = ConnState::connected;
+                BMCWEB_LOG_DEBUG << "SSL Handshake successfull";
+
+                self->checkQueue();
+            });
     }
 
     void sendMessage(const std::string& data)
@@ -106,100 +153,167 @@ class HttpClient : public std::enable_shared_from_this<HttpClient>
         }
         state = ConnState::sendInProgress;
 
-        BMCWEB_LOG_DEBUG << __FUNCTION__ << "(): " << host << ":" << port;
-
-        req.version(static_cast<int>(11)); // HTTP 1.1
-        req.target(uri);
-        req.method(boost::beast::http::verb::post);
+        BMCWEB_LOG_DEBUG << host << ":" << port;
 
-        // Set headers
-        for (const auto& [key, value] : headers)
+        req = {};
+        for (const auto& field : fields)
         {
-            req.set(key, value);
+            req.set(field.name_string(), field.value());
         }
         req.set(boost::beast::http::field::host, host);
+        req.set(boost::beast::http::field::content_type, "text/plain");
+
+        req.version(static_cast<int>(11)); // HTTP 1.1
+        req.target(uri);
+        req.method(boost::beast::http::verb::post);
         req.keep_alive(true);
 
         req.body() = data;
         req.prepare_payload();
 
-        // Set a timeout on the operation
-        conn.expires_after(std::chrono::seconds(30));
+        auto respHandler = [self(shared_from_this())](
+                               const boost::beast::error_code ec,
+                               const std::size_t& bytesTransferred) {
+            if (ec)
+            {
+                BMCWEB_LOG_ERROR << "sendMessage() failed: " << ec.message();
+                self->doCloseAndCheckQueue(ConnState::sendFailed);
+                return;
+            }
+            BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: "
+                             << bytesTransferred;
+            boost::ignore_unused(bytesTransferred);
 
-        // Send the HTTP request to the remote host
-        boost::beast::http::async_write(
-            conn, req,
-            [self(shared_from_this())](const boost::beast::error_code& ec,
-                                       const std::size_t& bytesTransferred) {
-                if (ec)
-                {
-                    BMCWEB_LOG_ERROR << "sendMessage() failed: "
-                                     << ec.message();
-                    self->state = ConnState::sendFailed;
-                    self->checkQueue();
-                    return;
-                }
-                BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: "
-                                 << bytesTransferred;
-                boost::ignore_unused(bytesTransferred);
+            self->recvMessage();
+        };
 
-                self->recvMessage();
-            });
+        conn.expires_after(std::chrono::seconds(30));
+        if (sslConn)
+        {
+            boost::beast::http::async_write(*sslConn, req,
+                                            std::move(respHandler));
+        }
+        else
+        {
+            boost::beast::http::async_write(conn, req, std::move(respHandler));
+        }
     }
 
     void recvMessage()
     {
-        // Receive the HTTP response
-        boost::beast::http::async_read(
-            conn, buffer, res,
-            [self(shared_from_this())](const boost::beast::error_code& ec,
-                                       const std::size_t& bytesTransferred) {
+        auto respHandler = [self(shared_from_this())](
+                               const boost::beast::error_code ec,
+                               const std::size_t& bytesTransferred) {
+            if (ec && ec != boost::beast::http::error::partial_message)
+            {
+                BMCWEB_LOG_ERROR << "recvMessage() failed: " << ec.message();
+                self->doCloseAndCheckQueue(ConnState::recvFailed);
+                return;
+            }
+            BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: "
+                             << bytesTransferred;
+            boost::ignore_unused(bytesTransferred);
+
+            // TODO: check for return status code and perform
+            // retry if fails(Ex: 40x). Take action depending on
+            // retry policy.
+            BMCWEB_LOG_DEBUG << "recvMessage() data: "
+                             << self->parser->get().body();
+
+            // Send is successful, Lets remove data from queue
+            // check for next request data in queue.
+            self->requestDataQueue.pop();
+
+            // Transfer ownership of the response
+            self->parser->release();
+
+            // TODO: Implement the keep-alive connections.
+            // Most of the web servers close connection abruptly
+            // and might be reason due to which its observed that
+            // stream_truncated(Next read) or  partial_message
+            // errors. So for now, closing connection and re-open
+            // for all cases.
+            self->doCloseAndCheckQueue(ConnState::closed);
+        };
+
+        parser.emplace(std::piecewise_construct, std::make_tuple());
+        parser->body_limit(httpReadBodyLimit);
+        buffer.consume(buffer.size());
+
+        conn.expires_after(std::chrono::seconds(30));
+        if (sslConn)
+        {
+            boost::beast::http::async_read(*sslConn, buffer, *parser,
+                                           std::move(respHandler));
+        }
+        else
+        {
+            boost::beast::http::async_read(conn, buffer, *parser,
+                                           std::move(respHandler));
+        }
+    }
+
+    void doCloseAndCheckQueue(const ConnState setState = ConnState::closed)
+    {
+        if (sslConn)
+        {
+            conn.expires_after(std::chrono::seconds(30));
+            sslConn->async_shutdown([self = shared_from_this(),
+                                     setState{std::move(setState)}](
+                                        const boost::system::error_code ec) {
                 if (ec)
                 {
-                    BMCWEB_LOG_ERROR << "recvMessage() failed: "
-                                     << ec.message();
-                    self->state = ConnState::recvFailed;
-                    self->checkQueue();
-                    return;
+                    // Many https server closes connection abruptly
+                    // i.e witnout close_notify. More details are at
+                    // https://github.com/boostorg/beast/issues/824
+                    if (ec == boost::asio::ssl::error::stream_truncated)
+                    {
+                        BMCWEB_LOG_ERROR
+                            << "doCloseAndCheckQueue(): Connection "
+                               "closed by server. ";
+                    }
+                    else
+                    {
+                        BMCWEB_LOG_ERROR << "doCloseAndCheckQueue() failed: "
+                                         << ec.message();
+                    }
                 }
-                BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: "
-                                 << bytesTransferred;
-                boost::ignore_unused(bytesTransferred);
-
-                // Discard received data. We are not interested.
-                BMCWEB_LOG_DEBUG << "recvMessage() data: " << self->res;
-
-                // Send is successful, Lets remove data from queue
-                // check for next request data in queue.
-                self->requestDataQueue.pop();
-                self->state = ConnState::idle;
+                else
+                {
+                    BMCWEB_LOG_DEBUG << "Connection closed gracefully...";
+                }
+                self->conn.cancel();
+                self->state = setState;
                 self->checkQueue();
             });
-    }
-
-    void doClose()
-    {
-        boost::beast::error_code ec;
-        conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
-
-        state = ConnState::closed;
-        // not_connected happens sometimes so don't bother reporting it.
-        if (ec && ec != boost::beast::errc::not_connected)
+        }
+        else
         {
-            BMCWEB_LOG_ERROR << "shutdown failed: " << ec.message();
-            return;
+            boost::beast::error_code ec;
+            conn.expires_after(std::chrono::seconds(30));
+            conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both,
+                                   ec);
+            if (ec)
+            {
+                BMCWEB_LOG_ERROR << "doCloseAndCheckQueue() failed: "
+                                 << ec.message();
+            }
+            else
+            {
+                BMCWEB_LOG_DEBUG << "Connection closed gracefully...";
+            }
+
+            conn.close();
+            state = setState;
+            checkQueue();
         }
-        BMCWEB_LOG_DEBUG << "Connection closed gracefully";
+        return;
     }
 
     void checkQueue(const bool newRecord = false)
     {
         if (requestDataQueue.empty())
         {
-            // TODO: Having issue in keeping connection alive. So lets close if
-            // nothing to be transferred.
-            doClose();
-
             BMCWEB_LOG_DEBUG << "requestDataQueue is empty\n";
             return;
         }
@@ -257,16 +371,17 @@ class HttpClient : public std::enable_shared_from_this<HttpClient>
             BMCWEB_LOG_DEBUG << "Attempt retry after " << retryIntervalSecs
                              << " seconds. RetryCount = " << retryCount;
             timer.expires_after(std::chrono::seconds(retryIntervalSecs));
-            timer.async_wait([self = shared_from_this()](
-                                 const boost::system::error_code& ec) {
-                self->runningTimer = false;
-                self->connStateCheck();
-            });
+            timer.async_wait(
+                [self = shared_from_this()](boost::system::error_code) {
+                    self->runningTimer = false;
+                    self->connStateCheck();
+                });
             return;
         }
-        else
+
+        if (state == ConnState::idle)
         {
-            // reset retry count.
+            // State idle means, previous attempt is successful.
             retryCount = 0;
         }
         connStateCheck();
@@ -279,6 +394,7 @@ class HttpClient : public std::enable_shared_from_this<HttpClient>
         switch (state)
         {
             case ConnState::connectInProgress:
+            case ConnState::sslHandshakeInProgress:
             case ConnState::sendInProgress:
             case ConnState::suspended:
             case ConnState::terminated:
@@ -310,15 +426,19 @@ class HttpClient : public std::enable_shared_from_this<HttpClient>
   public:
     explicit HttpClient(boost::asio::io_context& ioc, const std::string& id,
                         const std::string& destIP, const std::string& destPort,
-                        const std::string& destUri) :
+                        const std::string& destUri,
+                        const bool inUseSsl = true) :
         conn(ioc),
         timer(ioc), subId(id), host(destIP), port(destPort), uri(destUri),
-        retryCount(0), maxRetryAttempts(5),
-        retryPolicyAction("TerminateAfterRetries"), runningTimer(false)
+        useSsl(inUseSsl), retryCount(0), maxRetryAttempts(5),
+        retryPolicyAction("TerminateAfterRetries"), runningTimer(false),
+        state(ConnState::initialized)
     {
         boost::asio::ip::tcp::resolver resolver(ioc);
+        // TODO: Use async_resolver. boost asio example
+        // code as is crashing with async_resolve().
+        // It needs debug.
         endpoint = resolver.resolve(host, port);
-        state = ConnState::initialized;
     }
 
     void sendData(const std::string& data)
@@ -344,7 +464,12 @@ class HttpClient : public std::enable_shared_from_this<HttpClient>
     void setHeaders(
         const std::vector<std::pair<std::string, std::string>>& httpHeaders)
     {
-        headers = httpHeaders;
+        // Set headers
+        for (const auto& [key, value] : httpHeaders)
+        {
+            // TODO: Validate the header fileds before assign.
+            fields.set(key, value);
+        }
     }
 
     void setRetryConfig(const uint32_t retryAttempts,
diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 9c42e06..2a02920 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -384,7 +384,7 @@ class Subscription
     {
         conn = std::make_shared<crow::HttpClient>(
             crow::connections::systemBus->get_io_context(), id, host, port,
-            path);
+            path, (uriProto == "https" ? true : false));
     }
 
     Subscription(const std::shared_ptr<crow::Request::Adaptor>& adaptor) :
-- 
2.7.4