Skip to content

Commit 5222190

Browse files
committed
(cobra) Add TLS options to all cobra commands and classes. Add example to the doc.
1 parent 3e786fe commit 5222190

21 files changed

+297
-78
lines changed

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [7.6.0] - 2019-12-19
5+
6+
(cobra) Add TLS options to all cobra commands and classes. Add example to the doc.
7+
48
## [7.5.8] - 2019-12-18
59

610
(cobra-to-sentry) capture application version from device field

docs/ws.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,127 @@ Options:
243243
--transfer-timeout INT Transfer timeout
244244
```
245245

246-
## Cobra Client
246+
## Cobra client and server
247247

248-
[cobra](https://github.com/machinezone/cobra) is a real time messenging server. ws has a sub-command to interact with cobra.
248+
[cobra](https://github.com/machinezone/cobra) is a real time messenging server. ws has several sub-command to interact with cobra. There is also a minimal cobra compatible server named snake available.
249+
250+
Below are examples on running a snake server and clients with TLS enabled (the server only works with the OpenSSL backend for now).
251+
252+
First, generate certificates.
253+
254+
```
255+
$ cd /path/to/IXWebSocket
256+
$ cd ixsnake/ixsnake
257+
$ bash ../../ws/generate_certs.sh
258+
Generating RSA private key, 2048 bit long modulus
259+
.....+++
260+
.................+++
261+
e is 65537 (0x10001)
262+
generated ./.certs/trusted-ca-key.pem
263+
generated ./.certs/trusted-ca-crt.pem
264+
Generating RSA private key, 2048 bit long modulus
265+
..+++
266+
.......................................+++
267+
e is 65537 (0x10001)
268+
generated ./.certs/trusted-server-key.pem
269+
Signature ok
270+
subject=/O=machinezone/O=IXWebSocket/CN=trusted-server
271+
Getting CA Private Key
272+
generated ./.certs/trusted-server-crt.pem
273+
Generating RSA private key, 2048 bit long modulus
274+
...................................+++
275+
..................................................+++
276+
e is 65537 (0x10001)
277+
generated ./.certs/trusted-client-key.pem
278+
Signature ok
279+
subject=/O=machinezone/O=IXWebSocket/CN=trusted-client
280+
Getting CA Private Key
281+
generated ./.certs/trusted-client-crt.pem
282+
Generating RSA private key, 2048 bit long modulus
283+
..............+++
284+
.......................................+++
285+
e is 65537 (0x10001)
286+
generated ./.certs/untrusted-ca-key.pem
287+
generated ./.certs/untrusted-ca-crt.pem
288+
Generating RSA private key, 2048 bit long modulus
289+
..........+++
290+
................................................+++
291+
e is 65537 (0x10001)
292+
generated ./.certs/untrusted-client-key.pem
293+
Signature ok
294+
subject=/O=machinezone/O=IXWebSocket/CN=untrusted-client
295+
Getting CA Private Key
296+
generated ./.certs/untrusted-client-crt.pem
297+
Generating RSA private key, 2048 bit long modulus
298+
.....................................................................................+++
299+
...........+++
300+
e is 65537 (0x10001)
301+
generated ./.certs/selfsigned-client-key.pem
302+
Signature ok
303+
subject=/O=machinezone/O=IXWebSocket/CN=selfsigned-client
304+
Getting Private key
305+
generated ./.certs/selfsigned-client-crt.pem
306+
```
307+
308+
Now run the snake server.
309+
310+
```
311+
$ export certs=.certs
312+
$ ws snake --tls --port 8765 --cert-file ${certs}/trusted-server-crt.pem --key-file ${certs}/trusted-server-key.pem --ca-file ${certs}/trusted-ca-crt.pem
313+
{
314+
"apps": {
315+
"FC2F10139A2BAc53BB72D9db967b024f": {
316+
"roles": {
317+
"_sub": {
318+
"secret": "66B1dA3ED5fA074EB5AE84Dd8CE3b5ba"
319+
},
320+
"_pub": {
321+
"secret": "1c04DB8fFe76A4EeFE3E318C72d771db"
322+
}
323+
}
324+
}
325+
}
326+
}
327+
328+
redis host: 127.0.0.1
329+
redis password:
330+
redis port: 6379
331+
```
332+
333+
As a new connection comes in, such output should be printed
334+
335+
```
336+
[2019-12-19 20:27:19.724] [info] New connection
337+
id: 0
338+
Uri: /v2?appkey=_health
339+
Headers:
340+
Connection: Upgrade
341+
Host: 127.0.0.1:8765
342+
Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=15; client_max_window_bits=15
343+
Sec-WebSocket-Key: d747B0fE61Db73f7Eh47c0==
344+
Sec-WebSocket-Protocol: json
345+
Sec-WebSocket-Version: 13
346+
Upgrade: websocket
347+
User-Agent: ixwebsocket/7.5.8 macos ssl/OpenSSL OpenSSL 1.0.2q 20 Nov 2018 zlib 1.2.11
348+
```
349+
350+
To connect and publish a message, do:
351+
352+
```
353+
$ export certs=.certs
354+
$ cd /path/to/ws/folder
355+
$ ls cobraMetricsSample.json
356+
cobraMetricsSample.json
357+
$ ws cobra_publish --endpoint wss://127.0.0.1:8765 --appkey FC2F10139A2BAc53BB72D9db967b024f --rolename _pub --rolesecret 1c04DB8fFe76A4EeFE3E318C72d771db --channel foo --cert-file ${certs}/trusted-client-crt.pem --key-file ${certs}/trusted-client-key.pem --ca-file ${certs}/trusted-ca-crt.pem cobraMetricsSample.json
358+
[2019-12-19 20:46:42.656] [info] Publisher connected
359+
[2019-12-19 20:46:42.657] [info] Connection: Upgrade
360+
[2019-12-19 20:46:42.657] [info] Sec-WebSocket-Accept: rs99IFThoBrhSg+k8G4ixH9yaq4=
361+
[2019-12-19 20:46:42.657] [info] Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=15; client_max_window_bits=15
362+
[2019-12-19 20:46:42.657] [info] Server: ixwebsocket/7.5.8 macos ssl/OpenSSL OpenSSL 1.0.2q 20 Nov 2018 zlib 1.2.11
363+
[2019-12-19 20:46:42.657] [info] Upgrade: websocket
364+
[2019-12-19 20:46:42.658] [info] Publisher authenticated
365+
[2019-12-19 20:46:42.658] [info] Published msg 3
366+
[2019-12-19 20:46:42.659] [info] Published message id 3 acked
367+
```
368+
369+
To use OpenSSL on macOS, compile with `make ws_openssl`. First you will have to install OpenSSL libraries, which can be done with Homebrew.

ixcobra/ixcobra/IXCobraConnection.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "IXCobraConnection.h"
88
#include <ixcrypto/IXHMac.h>
99
#include <ixwebsocket/IXWebSocket.h>
10+
#include <ixwebsocket/IXSocketTLSOptions.h>
1011

1112
#include <algorithm>
1213
#include <stdexcept>
@@ -244,7 +245,8 @@ namespace ix
244245
const std::string& endpoint,
245246
const std::string& rolename,
246247
const std::string& rolesecret,
247-
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions)
248+
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions,
249+
const SocketTLSOptions& socketTLSOptions)
248250
{
249251
_roleName = rolename;
250252
_roleSecret = rolesecret;
@@ -257,6 +259,7 @@ namespace ix
257259
std::string url = ss.str();
258260
_webSocket->setUrl(url);
259261
_webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
262+
_webSocket->setTLSOptions(socketTLSOptions);
260263
}
261264

262265
//

ixcobra/ixcobra/IXCobraConnection.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace ix
2121
{
2222
class WebSocket;
23+
struct SocketTLSOptions;
2324

2425
enum CobraConnectionEventType
2526
{
@@ -62,7 +63,8 @@ namespace ix
6263
const std::string& endpoint,
6364
const std::string& rolename,
6465
const std::string& rolesecret,
65-
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions);
66+
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions,
67+
const SocketTLSOptions& socketTLSOptions);
6668

6769
/// Set the traffic tracker callback
6870
static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback);

ixcobra/ixcobra/IXCobraMetricsPublisher.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "IXCobraMetricsPublisher.h"
8+
#include <ixwebsocket/IXSocketTLSOptions.h>
89

910
#include <algorithm>
1011
#include <stdexcept>
@@ -31,14 +32,15 @@ namespace ix
3132
const std::string& channel,
3233
const std::string& rolename,
3334
const std::string& rolesecret,
34-
bool enablePerMessageDeflate)
35+
bool enablePerMessageDeflate,
36+
const SocketTLSOptions& socketTLSOptions)
3537
{
3638
// Configure the satori connection and start its publish background thread
3739
_cobra_metrics_theaded_publisher.start();
3840

3941
_cobra_metrics_theaded_publisher.configure(appkey, endpoint, channel,
4042
rolename, rolesecret,
41-
enablePerMessageDeflate);
43+
enablePerMessageDeflate, socketTLSOptions);
4244
}
4345

4446
Json::Value& CobraMetricsPublisher::getGenericAttributes()

ixcobra/ixcobra/IXCobraMetricsPublisher.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace ix
1717
{
18+
struct SocketTLSOptions;
19+
1820
class CobraMetricsPublisher
1921
{
2022
public:
@@ -43,7 +45,8 @@ namespace ix
4345
const std::string& channel,
4446
const std::string& rolename,
4547
const std::string& rolesecret,
46-
bool enablePerMessageDeflate);
48+
bool enablePerMessageDeflate,
49+
const SocketTLSOptions& socketTLSOptions);
4750

4851
/// Setter for the list of blacklisted metrics ids.
4952
/// That list is sorted internally for fast lookups

ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "IXCobraMetricsThreadedPublisher.h"
88
#include <ixwebsocket/IXSetThreadName.h>
9+
#include <ixwebsocket/IXSocketTLSOptions.h>
910
#include <ixcore/utils/IXCoreLogger.h>
1011

1112
#include <algorithm>
@@ -92,14 +93,15 @@ namespace ix
9293
const std::string& channel,
9394
const std::string& rolename,
9495
const std::string& rolesecret,
95-
bool enablePerMessageDeflate)
96+
bool enablePerMessageDeflate,
97+
const SocketTLSOptions& socketTLSOptions)
9698
{
9799
_channel = channel;
98100

99101
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(enablePerMessageDeflate);
100102
_cobra_connection.configure(appkey, endpoint,
101103
rolename, rolesecret,
102-
webSocketPerMessageDeflateOptions);
104+
webSocketPerMessageDeflateOptions, socketTLSOptions);
103105
}
104106

105107
void CobraMetricsThreadedPublisher::pushMessage(MessageKind messageKind)

ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace ix
2020
{
21+
struct SocketTLSOptions;
22+
2123
class CobraMetricsThreadedPublisher
2224
{
2325
public:
@@ -30,7 +32,8 @@ namespace ix
3032
const std::string& channel,
3133
const std::string& rolename,
3234
const std::string& rolesecret,
33-
bool enablePerMessageDeflate);
35+
bool enablePerMessageDeflate,
36+
const SocketTLSOptions& socketTLSOptions);
3437

3538
/// Start the worker thread, used for background publishing
3639
void start();

ixsnake/ixsnake/IXAppConfig.h

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <nlohmann/json.hpp>
1010
#include <string>
1111
#include <vector>
12+
#include <ixwebsocket/IXSocketTLSOptions.h>
1213

1314
namespace snake
1415
{
@@ -26,6 +27,9 @@ namespace snake
2627
// AppKeys
2728
nlohmann::json apps;
2829

30+
// TLS options
31+
ix::SocketTLSOptions socketTLSOptions;
32+
2933
// Misc
3034
bool verbose;
3135
};

ixsnake/ixsnake/IXSnakeServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace snake
2020
: _appConfig(appConfig)
2121
, _server(appConfig.port, appConfig.hostname)
2222
{
23-
;
23+
_server.setTLSOptions(appConfig.socketTLSOptions);
2424
}
2525

2626
//

0 commit comments

Comments
 (0)