nats-pure 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -0
- data/README.md +59 -11
- data/lib/nats/io/client.rb +451 -85
- data/lib/nats/io/errors.rb +4 -0
- data/lib/nats/io/parser.rb +3 -1
- data/lib/nats/io/version.rb +1 -1
- data/lib/nats/io/websocket.rb +1 -1
- data/sig/nats/io/client.rbs +19 -4
- data/sig/nats/io/errors.rbs +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: de4e341849ae05c48b13d964c812114e6d2101bfc846269c111f8dcb8532d6c4
|
|
4
|
+
data.tar.gz: 7d02ff8cac707f0c79d725c33f2e51f958b6553b7be726f881a19b1038f31202
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: adce199af4dcb6c58587d497117463e1f23512f5b2ae3efa4586fe8663e3418e6ee2a84573eb2c48d52d71d1f29b8a867a759c17b1189b46c17521970d615c49
|
|
7
|
+
data.tar.gz: cdf9b5eae05f396ee8189e99e5d441a181ff5879639384ee197e7b74b880e668ae21391841a1a0d8de5108195f9c4fbb57b2a26c6b7bbba3a95505bf92cd5fdf
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
## main
|
|
4
4
|
|
|
5
|
+
## v2.8.0
|
|
6
|
+
|
|
7
|
+
This release fixes security issues in how the client verifies TLS servers and
|
|
8
|
+
which servers it takes from the cluster. The client now checks the server's
|
|
9
|
+
certificate chain and name after every TLS handshake, whatever the TLS context
|
|
10
|
+
it is given checks, takes discovered servers only as `host:port`, over TLS only
|
|
11
|
+
from INFOs received inside TLS, and verifies each discovered server against a
|
|
12
|
+
name the client trusts. Connections that worked only because a certificate or
|
|
13
|
+
its name was not checked, or was checked against another name, now fail before
|
|
14
|
+
anything is sent: read Changed for each case and its way out before upgrading.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- TLS: `tls: {hostname: "nats.example.com"}` sets the name that the certificate of every server is verified against, and that is sent as SNI, on the first connect and on every reconnect. The option used to be ignored. (#204)
|
|
19
|
+
- TLS: `tls: {insecure_skip_verify: true}` turns the client's check of the server's certificate off, as the last resort. With a `context:`, only what the context checks is checked; without one, nothing is. Only `true` turns the check off. (#204)
|
|
20
|
+
- TLS: `tls: {handshake_first: true}` starts the TLS handshake before the server sends INFO, on every connect and reconnect, for servers with `handshake_first` in their `tls` block. The client does not fall back to TLS after INFO: against a server that sends INFO first, the attempt fails with `OpenSSL::SSL::SSLError`. `connect_timeout` does not bound the TLS handshake: a server that accepts the connection and does not answer the handshake holds the connect, or a reconnect, for as long as it keeps the connection open. (#204)
|
|
21
|
+
- TLS: `tls: {verify_discovered_servers_by_ip: true}` verifies a discovered IP address against itself, instead of against the name of the server that listed it, and lets the client connect to a discovered IP address that has no such name. It is meant for clusters whose certificates carry only IP addresses, and is safe only where the CA that issued those certificates is not shared with others. `tls: {hostname:}` overrides it. Only `true` turns it on. (#206)
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
|
|
25
|
+
- TLS: the client checks the server's certificate chain and name after every TLS handshake, before it sends CONNECT, on the first connect and on every reconnect, whatever the `tls: {context:}` checks itself. This breaks connections that worked only because the certificate or its name was not checked. A context made with `OpenSSL::SSL::SSLContext.new`, as the README showed, checked neither the chain nor the name, and one with a CA and `VERIFY_PEER` but without `verify_hostname` checked no name. Such connections now fail with `OpenSSL::SSL::SSLError`, "certificate verify failed (...)" for the chain, or "hostname ... does not match the server certificate of ..." with the names the certificate is issued for (left out on OpenSSL 1.1 when the context itself checks the name), before anything is sent. Call `set_params` on the context (with `ca_file:` for your own CA; without it the system's CAs are trusted) and connect with a name the certificates carry, or set the name with `tls: {hostname:}`; `tls: {insecure_skip_verify: true}` turns the check off. (#204)
|
|
26
|
+
- TLS on JRuby: with jruby-openssl before 0.19.0 (bundled with JRuby before 10.1.3.0) no name was checked at all, also not with the client's own context for `tls://` URLs or with `verify_hostname`; it now is. A context without `VERIFY_PEER` now fails also with the right CA ("certificate verify failed (the context does not set VERIFY_PEER)"): set `VERIFY_PEER`, which `set_params` does. (#204)
|
|
27
|
+
- Discovered servers: a `connect_urls` entry of the server's INFO is taken only as `host:port`, and a discovered server gets the credentials the client uses for the server that listed it, from the options or the URL. User info in an entry is dropped: an entry `user:pass@host:port` used to replace credentials given in the URL, `user@host:port` was sent with the client's password, and `token@host:port` sent that token instead of nkey or JWT authentication. An entry with a scheme, or one that is not `host:port`, is skipped and the other entries are used; an entry such as `"a b:4222"` used to fail the connect with `URI::InvalidURIError`. (#205)
|
|
28
|
+
- TLS: the servers listed in the INFO that a server sends before the TLS handshake are no longer used, also when the attempt fails after that INFO; the discovered servers come from the INFOs received inside TLS. Against nats-server before 2.12, a client configured with one server therefore knows no other server after `connect` until the cluster changes; configure more than one server, use `tls: {handshake_first: true}`, or run nats-server 2.12 or later. The servers of the first INFO are still used without TLS, with `tls: {handshake_first: true}` and over `wss://`, also when the attempt fails. (#205)
|
|
29
|
+
- TLS: on a connection that starts TLS after INFO, the client waits after the first PONG, on connect and on every reconnect, for the INFO that nats-server 2.12 and later send there, inside TLS, for up to `connect_timeout`, so that it knows the servers of the cluster when `connect` returns. The version in the server's first INFO decides whether it waits; a pre-release of 2.12.0 counts as 2.12.0. When no INFO comes in time, the client reports connected and takes the INFO when it comes. With `ignore_discovered_urls: true` it does not wait. A `-ERR` or a closed connection during the wait fails the attempt. (#205)
|
|
30
|
+
- Discovered servers: the `connect_urls` of an INFO on the established connection, and of the INFO that follows the first PONG, now replace the discovered servers instead of only adding to them, as in nats.go. The configured servers and the server the client is connected to stay, and an INFO without `connect_urls`, or with an empty list, changes nothing. A server that left the cluster is no longer dialed, and `servers` and `discovered_servers` can shrink. The first INFO of an attempt still only adds servers. (#205)
|
|
31
|
+
- TLS: a discovered server is verified against a name the client trusts, however the servers are given. A discovered host name is verified against itself. A discovered IP address is verified against the name of the server that listed it, which over any number of servers is the name of a configured server, or a discovered host name. That name is also sent as SNI, and `tls: {hostname:}` overrides it. With `servers:` or several URLs in one String, a discovered IP address used to be verified against itself: a cluster whose other servers have certificates that carry only IP addresses, reached through a server configured by name, now fails over to no discovered server; set `tls: {verify_discovered_servers_by_ip: true}`. With a String of one URL, every server used to be verified against the host of that URL: a cluster whose certificates carry only that name, while its servers advertise other host names, now fails over to no discovered server; set `tls: {hostname:}` to that name. When a discovered server fails the name check, the error also says that each server can advertise a name its certificate is issued for (`client_advertise` in nats-server), and names `tls: {verify_discovered_servers_by_ip: true}` when the certificate carries the server's IP address (left out on OpenSSL 1.1 when the context itself checks the name). (#206)
|
|
32
|
+
- TLS: the client does not connect over TLS to a discovered server that has no name to verify its certificate against: an IP address listed by a server that is verified against an IP address, such as a server configured as `tls://10.0.0.1:4222`. This holds also with `tls: {insecure_skip_verify: true}`. Such a server is left out of `servers` and reported once per `connect` to `on_error` with a `NATS::IO::ConnectError` ("nats: skipping the discovered server ..., which has no name to verify its certificate against"). The report can come while `connect` runs: set `on_error` before `connect` (`NATS::IO::Client.new`, then `on_error`, then `connect`) to see it. It used to be verified against its own address with `servers:` or several URLs in one String, or against the address of the first server with a String of one URL. Set `tls: {hostname:}` to a name the certificates carry, or `tls: {verify_discovered_servers_by_ip: true}`. (#206)
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- While reconnecting, `close`, also the one after the last failed attempt, and `drain` wrote the messages published in that time to the connection of the current or last attempt, which might not have sent CONNECT and, with TLS asked for, might have had no TLS. They are now written only once a connection is established again, so a `close` while reconnecting drops them. `drain` while reconnecting now closes the client, as there is no connection to drain. (#204)
|
|
37
|
+
- `close` or `drain` called from `on_disconnect` or `on_close` runs these callbacks once more at most. `close` there used to call itself until the stack overflowed. (#204)
|
|
38
|
+
- TLS: the client could not read two lines that arrive in one TLS record during the connect, such as the `+OK` and the `PONG` of `verbose: true`: the second read waited for the socket and failed with `NATS::IO::SocketTimeoutError`. It now keeps what it read beyond a line for the next read. (#205)
|
|
39
|
+
- During the connect over `nats://` and `tls://`, `connect_timeout` bounds the read of a whole line from the server, also while the line comes in pieces. A line that had started used to hold `connect` until it ended or the server closed the connection. (#205)
|
|
40
|
+
- During the connect, the client answers a `PING` from the server and skips `+OK` before the first `PONG`. A `PING` there failed the attempt with "expected PONG, got PING"; nats-server can send one from 2 seconds after it received CONNECT. (#205)
|
|
41
|
+
- `publish` and `publish_msg` raise `NATS::IO::MaxPayloadError` (`nats: maximum payload exceeded`) when the payload is over the `max_payload` from the server's `INFO`, before anything is written. The server closes the connection for that, which drops every other request on it. A payload of exactly `max_payload` is sent. For a message with headers the limit applies to the header block and the body together, which is the size in the `HPUB` line. No `max_payload` yet, or `0`, is not a limit. (#200, thanks @theromis)
|
|
42
|
+
|
|
43
|
+
### Improved
|
|
44
|
+
|
|
45
|
+
- Tested against nats-server 2.14, 2.15 and `main`, on Ruby 3.0 to 4.0 and JRuby, where the TLS specs now have to pass. (#202, #203, #204)
|
|
46
|
+
|
|
5
47
|
## v2.7.0
|
|
6
48
|
|
|
7
49
|
This release adds the JetStream features of nats-server 2.10 to 2.14 that the
|
data/README.md
CHANGED
|
@@ -235,22 +235,70 @@ end
|
|
|
235
235
|
|
|
236
236
|
## TLS
|
|
237
237
|
|
|
238
|
-
|
|
239
|
-
|
|
238
|
+
`NATS.connect("tls://nats.example.com:4222")` connects over TLS and verifies
|
|
239
|
+
the server against the system's trusted CAs. To trust your own CA, or to
|
|
240
|
+
present a client certificate, pass an
|
|
241
|
+
[OpenSSL](https://ruby.github.io/openssl/OpenSSL/SSL/SSLContext.html) context:
|
|
240
242
|
|
|
241
243
|
```ruby
|
|
242
244
|
tls_context = OpenSSL::SSL::SSLContext.new
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
})
|
|
245
|
+
# Verifies the server, and trusts only the given CA instead of the system's CAs.
|
|
246
|
+
# Pass ca_file to set_params: set after set_params, the CA is added to the
|
|
247
|
+
# default store that every default context of the process shares.
|
|
248
|
+
tls_context.set_params(ca_file: "./ca.pem")
|
|
249
|
+
# For mutual TLS:
|
|
250
|
+
# tls_context.cert = OpenSSL::X509::Certificate.new(File.read("./client-cert.pem"))
|
|
251
|
+
# tls_context.key = OpenSSL::PKey.read(File.read("./client-key.pem"))
|
|
252
|
+
|
|
253
|
+
NATS.connect(servers: ["tls://nats.example.com:4222"], tls: {context: tls_context})
|
|
252
254
|
```
|
|
253
255
|
|
|
256
|
+
After every TLS handshake, on the first connect and on every reconnect, the
|
|
257
|
+
client checks that the server's certificate chain verifies and that the
|
|
258
|
+
certificate is issued for the server's name, whatever the context checks
|
|
259
|
+
itself. It closes the connection to a server that fails the check before it
|
|
260
|
+
sends CONNECT. Two settings change that check:
|
|
261
|
+
|
|
262
|
+
- `tls: {context: tls_context, hostname: "nats.example.com"}` sets the name
|
|
263
|
+
that the certificate of every server is verified against, and that is sent
|
|
264
|
+
as SNI. Use it for servers reached by an IP address, or by a name that their
|
|
265
|
+
certificates do not carry.
|
|
266
|
+
- `tls: {insecure_skip_verify: true}` turns the client's check off, as the
|
|
267
|
+
last resort. With a context, only what the context checks is checked;
|
|
268
|
+
without one, nothing is.
|
|
269
|
+
|
|
270
|
+
A server that the client discovers from the cluster is verified against a
|
|
271
|
+
name the client trusts: a host name against itself, and an IP address against
|
|
272
|
+
the name of the server that listed it. Over any number of servers, that is the
|
|
273
|
+
name of a server you configured, or a discovered host name.
|
|
274
|
+
`tls: {hostname:}` overrides both. An IP address listed by a server that is
|
|
275
|
+
verified against an IP address, such as one you configured as
|
|
276
|
+
`tls://10.0.0.1:4222`, has no such name. Unless `tls: {hostname:}`, or
|
|
277
|
+
`tls: {verify_discovered_servers_by_ip: true}` below, is set, the client does
|
|
278
|
+
not connect to it, also with `tls: {insecure_skip_verify: true}`, and reports
|
|
279
|
+
it once per `connect` to `on_error`. That report can come while `connect`
|
|
280
|
+
runs, so set `on_error` before `connect`:
|
|
281
|
+
|
|
282
|
+
```ruby
|
|
283
|
+
nats = NATS::IO::Client.new
|
|
284
|
+
nats.on_error { |e| puts "Error: #{e}" }
|
|
285
|
+
nats.connect(servers: ["tls://10.0.0.1:4222"], tls: {context: tls_context})
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
For clusters whose certificates carry only IP addresses,
|
|
289
|
+
`tls: {verify_discovered_servers_by_ip: true}` verifies a discovered IP address
|
|
290
|
+
against itself, and lets the client connect to one that has no name. It is safe
|
|
291
|
+
only where the CA that issued those certificates is not shared with others:
|
|
292
|
+
any certificate of that CA for the address passes. `tls: {hostname:}`
|
|
293
|
+
overrides it.
|
|
294
|
+
|
|
295
|
+
For servers with `handshake_first` in their `tls` block, add
|
|
296
|
+
`handshake_first: true` to `tls:`: the client then starts the TLS handshake
|
|
297
|
+
before the server sends INFO. `connect_timeout` does not bound the TLS
|
|
298
|
+
handshake: a server that accepts the connection and does not answer the
|
|
299
|
+
handshake holds the connect, or a reconnect, for as long as it keeps the
|
|
300
|
+
connection open.
|
|
301
|
+
|
|
254
302
|
## WebSocket
|
|
255
303
|
|
|
256
304
|
Since NATS Server v2.2 it is possible to connect to a NATS server [using WebSocket](https://docs.nats.io/running-a-nats-service/configuration/websocket).
|
data/lib/nats/io/client.rb
CHANGED
|
@@ -27,6 +27,7 @@ require "json"
|
|
|
27
27
|
require "monitor"
|
|
28
28
|
require "uri"
|
|
29
29
|
require "securerandom"
|
|
30
|
+
require "ipaddr"
|
|
30
31
|
require "concurrent"
|
|
31
32
|
|
|
32
33
|
begin
|
|
@@ -213,10 +214,13 @@ module NATS
|
|
|
213
214
|
# Secure TLS options
|
|
214
215
|
@tls = nil
|
|
215
216
|
|
|
216
|
-
#
|
|
217
|
-
#
|
|
217
|
+
# The TLS name of the current server: the name its certificate is
|
|
218
|
+
# verified against unless tls: {hostname:} is set, and the name a
|
|
219
|
+
# discovered IP address it lists inherits.
|
|
218
220
|
@hostname = nil
|
|
219
|
-
|
|
221
|
+
|
|
222
|
+
# The discovered servers reported to on_error for having no such name.
|
|
223
|
+
@without_tls_name = {}
|
|
220
224
|
|
|
221
225
|
# Track whether connect has been already been called.
|
|
222
226
|
@connect_called = false
|
|
@@ -225,6 +229,9 @@ module NATS
|
|
|
225
229
|
# that the connection was closed meanwhile.
|
|
226
230
|
@close_generation = 0
|
|
227
231
|
|
|
232
|
+
# The thread whose close runs the disconnect and close callbacks.
|
|
233
|
+
@close_callbacks_thread = nil
|
|
234
|
+
|
|
228
235
|
# New style request/response implementation.
|
|
229
236
|
@resp_sub = nil
|
|
230
237
|
@resp_map = nil
|
|
@@ -293,6 +300,7 @@ module NATS
|
|
|
293
300
|
private def parse_and_validate_options
|
|
294
301
|
# Reset these in case we have reconnected via fork.
|
|
295
302
|
@server_pool = []
|
|
303
|
+
@without_tls_name = {}
|
|
296
304
|
@resp_sub = nil
|
|
297
305
|
@resp_map = nil
|
|
298
306
|
@resp_sub_prefix = nil
|
|
@@ -319,7 +327,6 @@ module NATS
|
|
|
319
327
|
if srvs.any? { |u| %w[tls wss].include? u.scheme } && !opts[:tls]
|
|
320
328
|
opts[:tls] = {context: tls_context}
|
|
321
329
|
end
|
|
322
|
-
@single_url_connect_used = true if srvs.size == 1
|
|
323
330
|
when Hash
|
|
324
331
|
opts = uri
|
|
325
332
|
end
|
|
@@ -358,12 +365,19 @@ module NATS
|
|
|
358
365
|
else
|
|
359
366
|
URI.parse(u)
|
|
360
367
|
end
|
|
368
|
+
# A configured server is verified against the host of its URL.
|
|
361
369
|
@server_pool << {
|
|
362
370
|
uri: nats_uri,
|
|
363
|
-
hostname: nats_uri.hostname
|
|
371
|
+
hostname: nats_uri.hostname,
|
|
372
|
+
tls_name: nats_uri.hostname
|
|
364
373
|
}
|
|
365
374
|
end
|
|
366
375
|
|
|
376
|
+
# The pool drops a server after failed attempts. Listed again by a
|
|
377
|
+
# server, a configured server is still verified against the host of
|
|
378
|
+
# its URL.
|
|
379
|
+
@configured_servers = @server_pool.map { |srv| [srv[:uri].hostname, srv[:uri].port] }
|
|
380
|
+
|
|
367
381
|
if @options[:old_style_request]
|
|
368
382
|
# Replace for this instance the implementation
|
|
369
383
|
# of request to use the old_request style.
|
|
@@ -399,16 +413,8 @@ module NATS
|
|
|
399
413
|
begin
|
|
400
414
|
srv = select_next_server
|
|
401
415
|
|
|
402
|
-
# Use the hostname from the server for TLS hostname verification.
|
|
403
|
-
if client_using_secure_connection? && single_url_connect_used?
|
|
404
|
-
# Always reuse the original hostname used to connect.
|
|
405
|
-
@hostname ||= srv[:hostname]
|
|
406
|
-
else
|
|
407
|
-
@hostname = srv[:hostname]
|
|
408
|
-
end
|
|
409
|
-
|
|
410
416
|
# Create TCP socket connection to NATS.
|
|
411
|
-
@io = create_socket
|
|
417
|
+
@io = create_socket(srv)
|
|
412
418
|
@io.connect
|
|
413
419
|
|
|
414
420
|
# Capture state that we have had a TCP connection established against
|
|
@@ -479,8 +485,10 @@ module NATS
|
|
|
479
485
|
return publish_msg(NATS::Msg.new(subject: subject, data: msg, reply: opt_reply, header: options[:header]))
|
|
480
486
|
end
|
|
481
487
|
|
|
482
|
-
# Accounting
|
|
483
488
|
msg_size = msg.bytesize
|
|
489
|
+
check_max_payload!(msg_size)
|
|
490
|
+
|
|
491
|
+
# Accounting
|
|
484
492
|
@stats[:out_msgs] += 1
|
|
485
493
|
@stats[:out_bytes] += msg_size
|
|
486
494
|
|
|
@@ -497,10 +505,6 @@ module NATS
|
|
|
497
505
|
msg.data ||= "".dup
|
|
498
506
|
msg_size = msg.data.bytesize
|
|
499
507
|
|
|
500
|
-
# Accounting
|
|
501
|
-
@stats[:out_msgs] += 1
|
|
502
|
-
@stats[:out_bytes] += msg_size
|
|
503
|
-
|
|
504
508
|
if msg.header
|
|
505
509
|
hdr = "".dup
|
|
506
510
|
hdr << NATS_HDR_LINE
|
|
@@ -510,8 +514,20 @@ module NATS
|
|
|
510
514
|
hdr << CR_LF
|
|
511
515
|
hdr_len = hdr.bytesize
|
|
512
516
|
total_size = msg_size + hdr_len
|
|
517
|
+
check_max_payload!(total_size)
|
|
518
|
+
|
|
519
|
+
# Accounting
|
|
520
|
+
@stats[:out_msgs] += 1
|
|
521
|
+
@stats[:out_bytes] += msg_size
|
|
522
|
+
|
|
513
523
|
send_command("HPUB #{msg.subject} #{msg.reply} #{hdr_len} #{total_size}\r\n#{hdr}#{msg.data}\r\n")
|
|
514
524
|
else
|
|
525
|
+
check_max_payload!(msg_size)
|
|
526
|
+
|
|
527
|
+
# Accounting
|
|
528
|
+
@stats[:out_msgs] += 1
|
|
529
|
+
@stats[:out_bytes] += msg_size
|
|
530
|
+
|
|
515
531
|
send_command("PUB #{msg.subject} #{msg.reply} #{msg_size}\r\n#{msg.data}\r\n")
|
|
516
532
|
end
|
|
517
533
|
|
|
@@ -845,6 +861,8 @@ module NATS
|
|
|
845
861
|
# callback option to know when the connection has moved from draining to closed.
|
|
846
862
|
def drain
|
|
847
863
|
return if draining?
|
|
864
|
+
# There is no connection to drain while reconnecting.
|
|
865
|
+
return close if reconnecting?
|
|
848
866
|
|
|
849
867
|
synchronize do
|
|
850
868
|
@drain_t ||= Thread.new { do_drain }
|
|
@@ -876,8 +894,13 @@ module NATS
|
|
|
876
894
|
raise(NATS::IO::ClientError, "custom inbox may not begin with '.'") if @inbox_prefix.start_with?(".")
|
|
877
895
|
end
|
|
878
896
|
|
|
879
|
-
|
|
897
|
+
# Takes an INFO from the server. Unless ignore_discovered_urls is set,
|
|
898
|
+
# or discover is false, the servers it lists replace the discovered
|
|
899
|
+
# servers; those of the first INFO of an attempt (first: true) are only
|
|
900
|
+
# added.
|
|
901
|
+
def process_info(line, discover: true, first: false)
|
|
880
902
|
parsed_info = JSON.parse(line)
|
|
903
|
+
skipped = []
|
|
881
904
|
|
|
882
905
|
# INFO can be received asynchronously too,
|
|
883
906
|
# so has to be done under the lock.
|
|
@@ -889,13 +912,20 @@ module NATS
|
|
|
889
912
|
|
|
890
913
|
# Detect any announced server that we might not be aware of...
|
|
891
914
|
connect_urls = @server_info[:connect_urls]
|
|
892
|
-
if !@options[:ignore_discovered_urls] && connect_urls
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
915
|
+
if discover && !@options[:ignore_discovered_urls] && connect_urls
|
|
916
|
+
uris = connect_urls.filter_map { |url| discovered_uri(url) }
|
|
917
|
+
|
|
918
|
+
# A later list drops the discovered servers it no longer names.
|
|
919
|
+
# The configured servers and the current one stay, and an empty
|
|
920
|
+
# list changes nothing.
|
|
921
|
+
if !first && !connect_urls.empty?
|
|
922
|
+
server_pool.reject! do |srv|
|
|
923
|
+
srv[:discovered] && [@uri, *uris].none? { |u| srv[:uri].hostname == u.hostname && srv[:uri].port == u.port }
|
|
924
|
+
end
|
|
925
|
+
end
|
|
898
926
|
|
|
927
|
+
srvs = []
|
|
928
|
+
uris.each do |u|
|
|
899
929
|
# Skip in case it is the current server which we already know
|
|
900
930
|
next if @uri.hostname == u.hostname && @uri.port == u.port
|
|
901
931
|
|
|
@@ -903,6 +933,8 @@ module NATS
|
|
|
903
933
|
srv[:uri].hostname == u.hostname && srv[:uri].port == u.port
|
|
904
934
|
end
|
|
905
935
|
|
|
936
|
+
# The first server that lists a server decides its credentials
|
|
937
|
+
# and its TLS name.
|
|
906
938
|
if !present
|
|
907
939
|
# Let explicit user and pass options set the credentials.
|
|
908
940
|
u.user = options[:user] if options[:user]
|
|
@@ -914,8 +946,18 @@ module NATS
|
|
|
914
946
|
u.password ||= @uri.password if @uri.password
|
|
915
947
|
end
|
|
916
948
|
|
|
917
|
-
|
|
918
|
-
|
|
949
|
+
tls_name = @configured_servers.include?([u.hostname, u.port]) ? u.hostname : discovered_tls_name(u)
|
|
950
|
+
|
|
951
|
+
# Over TLS, a server with no name to verify its certificate
|
|
952
|
+
# against is not connected to. It is reported once per connect.
|
|
953
|
+
if tls_name.nil? && client_using_secure_connection? && !@tls&.fetch(:hostname, nil)
|
|
954
|
+
key = "#{u.host}:#{u.port}"
|
|
955
|
+
skipped << without_tls_name_error(key) unless @without_tls_name[key]
|
|
956
|
+
@without_tls_name[key] = true
|
|
957
|
+
next
|
|
958
|
+
end
|
|
959
|
+
|
|
960
|
+
srv = {uri: u, reconnect_attempts: 0, discovered: true, hostname: u.hostname, tls_name: tls_name}
|
|
919
961
|
srvs << srv
|
|
920
962
|
end
|
|
921
963
|
end
|
|
@@ -925,10 +967,59 @@ module NATS
|
|
|
925
967
|
server_pool.push(*srvs)
|
|
926
968
|
end
|
|
927
969
|
end
|
|
970
|
+
skipped.each { |e| err_cb_call(self, e, nil) } if @err_cb
|
|
928
971
|
|
|
929
972
|
@server_info
|
|
930
973
|
end
|
|
931
974
|
|
|
975
|
+
# The URI of a connect_urls entry, with the scheme of the current
|
|
976
|
+
# connection, or nil to skip the entry. nats-server writes an entry as
|
|
977
|
+
# host:port, with an IPv6 address in brackets. An entry with a scheme is
|
|
978
|
+
# skipped, and so is one that is not host:port. User info in an entry is
|
|
979
|
+
# dropped: a discovered server gets the client's own credentials.
|
|
980
|
+
def discovered_uri(entry)
|
|
981
|
+
return nil unless entry.is_a?(String) && !entry.include?("://")
|
|
982
|
+
|
|
983
|
+
u = URI.parse("nats://#{entry}")
|
|
984
|
+
return nil if u.host.to_s.empty? || !u.port&.between?(1, 65535) || !u.path.empty? || u.query || u.fragment
|
|
985
|
+
|
|
986
|
+
URI.parse("#{@uri.scheme}://#{u.host}:#{u.port}")
|
|
987
|
+
rescue URI::Error
|
|
988
|
+
nil
|
|
989
|
+
end
|
|
990
|
+
|
|
991
|
+
# The name the certificate of a server discovered on the current
|
|
992
|
+
# connection is verified against, and that is sent as SNI. A host name
|
|
993
|
+
# is verified against itself. An IP address is one more address of the
|
|
994
|
+
# current server: it is verified against the name the current server is
|
|
995
|
+
# verified against, and so, over any number of hops, against the name
|
|
996
|
+
# of a configured server, or a discovered host name. It has no name
|
|
997
|
+
# when that name is an IP address too. With
|
|
998
|
+
# tls: {verify_discovered_servers_by_ip: true}, an IP address is
|
|
999
|
+
# verified against itself. tls: {hostname:} overrides every name.
|
|
1000
|
+
def discovered_tls_name(uri)
|
|
1001
|
+
return uri.hostname unless ip_address?(uri.hostname)
|
|
1002
|
+
return uri.hostname if verify_discovered_servers_by_ip?
|
|
1003
|
+
|
|
1004
|
+
@hostname unless ip_address?(@hostname)
|
|
1005
|
+
end
|
|
1006
|
+
|
|
1007
|
+
# The error for a discovered server, given as host:port, that has no
|
|
1008
|
+
# name to verify its certificate against.
|
|
1009
|
+
def without_tls_name_error(server)
|
|
1010
|
+
NATS::IO::ConnectError.new("nats: skipping the discovered server #{server}, which has no name to verify its certificate against; " \
|
|
1011
|
+
"tls: {hostname:} sets the name every server is verified against, or tls: {verify_discovered_servers_by_ip: true} " \
|
|
1012
|
+
"verifies a discovered IP address against itself, which is safe only where the CA that issued the certificates is not shared with others")
|
|
1013
|
+
end
|
|
1014
|
+
|
|
1015
|
+
# Whether host is an IPv4 or IPv6 address.
|
|
1016
|
+
def ip_address?(host)
|
|
1017
|
+
IPAddr.new(host.to_s)
|
|
1018
|
+
true
|
|
1019
|
+
rescue IPAddr::Error
|
|
1020
|
+
false
|
|
1021
|
+
end
|
|
1022
|
+
|
|
932
1023
|
def process_hdr(header)
|
|
933
1024
|
hdr = nil
|
|
934
1025
|
if header
|
|
@@ -1083,6 +1174,9 @@ module NATS
|
|
|
1083
1174
|
@uri.user = @options[:user] if @options[:user]
|
|
1084
1175
|
@uri.password = @options[:pass] if @options[:pass]
|
|
1085
1176
|
|
|
1177
|
+
# Its TLS name, which a discovered IP address it lists inherits.
|
|
1178
|
+
@hostname = srv[:tls_name]
|
|
1179
|
+
|
|
1086
1180
|
srv
|
|
1087
1181
|
end
|
|
1088
1182
|
|
|
@@ -1107,12 +1201,51 @@ module NATS
|
|
|
1107
1201
|
# Insecure TLS versions not supported already:
|
|
1108
1202
|
# https://github.com/ruby/openssl/commit/3e5a009966bd7f806f7180d82cf830a04be28986
|
|
1109
1203
|
#
|
|
1110
|
-
|
|
1204
|
+
# With insecure_skip_verify there is no context of the user to leave
|
|
1205
|
+
# verification to, so this one verifies nothing.
|
|
1206
|
+
if insecure_skip_verify?
|
|
1207
|
+
tls_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE, verify_hostname: false)
|
|
1208
|
+
else
|
|
1209
|
+
# The client checks the name after the handshake (verify_server!),
|
|
1210
|
+
# where the error can list the names of the certificate.
|
|
1211
|
+
tls_context.set_params(verify_hostname: false)
|
|
1212
|
+
end
|
|
1111
1213
|
end
|
|
1112
1214
|
end
|
|
1113
1215
|
|
|
1114
|
-
|
|
1115
|
-
|
|
1216
|
+
# tls: {insecure_skip_verify: true} turns the client's check of the
|
|
1217
|
+
# server's certificate off. Only true does, so that a value such as
|
|
1218
|
+
# "false" keeps the check on.
|
|
1219
|
+
def insecure_skip_verify?
|
|
1220
|
+
!!@tls && @tls[:insecure_skip_verify] == true
|
|
1221
|
+
end
|
|
1222
|
+
|
|
1223
|
+
# tls: {handshake_first: true} starts the TLS handshake before the server
|
|
1224
|
+
# sends INFO, for servers with handshake_first in their tls block.
|
|
1225
|
+
def handshake_first?
|
|
1226
|
+
!!@tls && !!@tls[:handshake_first]
|
|
1227
|
+
end
|
|
1228
|
+
|
|
1229
|
+
# tls: {verify_discovered_servers_by_ip: true} verifies a discovered IP
|
|
1230
|
+
# address against itself, for clusters whose certificates carry only IP
|
|
1231
|
+
# addresses. Only true does.
|
|
1232
|
+
def verify_discovered_servers_by_ip?
|
|
1233
|
+
!!@tls && @tls[:verify_discovered_servers_by_ip] == true
|
|
1234
|
+
end
|
|
1235
|
+
|
|
1236
|
+
# The server closes the connection when a payload is over max_payload from
|
|
1237
|
+
# INFO (a later async INFO can lower it). Reject before the bytes are
|
|
1238
|
+
# queued, so the socket stays up. The size is the PUB body, or the HPUB
|
|
1239
|
+
# header block and body together: that is the size the server measures.
|
|
1240
|
+
# A limit of 0, or none yet, is not a limit, and the server skips it too.
|
|
1241
|
+
# A closed client is reported as such before the size is considered.
|
|
1242
|
+
private def check_max_payload!(size)
|
|
1243
|
+
raise NATS::IO::ConnectionClosedError if closed?
|
|
1244
|
+
|
|
1245
|
+
limit = @server_info[:max_payload]
|
|
1246
|
+
return unless limit.is_a?(Integer) && limit.positive? && size > limit
|
|
1247
|
+
|
|
1248
|
+
raise NATS::IO::MaxPayloadError, "nats: maximum payload exceeded"
|
|
1116
1249
|
end
|
|
1117
1250
|
|
|
1118
1251
|
def send_command(command)
|
|
@@ -1171,7 +1304,13 @@ module NATS
|
|
|
1171
1304
|
end
|
|
1172
1305
|
|
|
1173
1306
|
def do_drain
|
|
1174
|
-
|
|
1307
|
+
# A reconnect that started after drain was called leaves no
|
|
1308
|
+
# connection to drain.
|
|
1309
|
+
reconnecting = synchronize do
|
|
1310
|
+
@status = DRAINING_SUBS unless reconnecting?
|
|
1311
|
+
reconnecting?
|
|
1312
|
+
end
|
|
1313
|
+
return close if reconnecting
|
|
1175
1314
|
|
|
1176
1315
|
# Do unsubscribe protocol for all the susbcriptions, then have a single thread
|
|
1177
1316
|
# waiting until all subs are done or drain timeout error reported to async error cb.
|
|
@@ -1453,6 +1592,17 @@ module NATS
|
|
|
1453
1592
|
end
|
|
1454
1593
|
|
|
1455
1594
|
def process_connect_init
|
|
1595
|
+
# A server with handshake_first expects the TLS handshake before INFO.
|
|
1596
|
+
# A wss:// connection has its TLS before the WebSocket upgrade.
|
|
1597
|
+
tls_first = handshake_first? && %w[nats tls].include?(@uri.scheme)
|
|
1598
|
+
@io.setup_tls! if tls_first
|
|
1599
|
+
|
|
1600
|
+
# A client that asks for TLS and has none yet starts it after INFO,
|
|
1601
|
+
# or fails the attempt. The servers of an INFO read before the TLS
|
|
1602
|
+
# handshake are not used, whether the attempt goes on or fails: the
|
|
1603
|
+
# discovered servers come from the INFOs received inside TLS.
|
|
1604
|
+
tls_after_info = client_using_secure_connection? && !tls_first && @uri.scheme != "wss"
|
|
1605
|
+
|
|
1456
1606
|
# FIXME: Can receive PING as well here in recent versions.
|
|
1457
1607
|
line = @io.read_line(options[:connect_timeout])
|
|
1458
1608
|
if !line || line.empty?
|
|
@@ -1461,12 +1611,14 @@ module NATS
|
|
|
1461
1611
|
|
|
1462
1612
|
if (match = line.match(NATS::Protocol::INFO))
|
|
1463
1613
|
info_json = match.captures.first
|
|
1464
|
-
process_info(info_json)
|
|
1614
|
+
process_info(info_json, discover: !tls_after_info, first: true)
|
|
1465
1615
|
else
|
|
1466
1616
|
raise NATS::IO::ConnectError.new("nats: protocol exception, INFO not valid")
|
|
1467
1617
|
end
|
|
1468
1618
|
|
|
1469
|
-
if
|
|
1619
|
+
if tls_first
|
|
1620
|
+
# The connection has TLS already.
|
|
1621
|
+
elsif server_using_secure_connection? && client_using_secure_connection?
|
|
1470
1622
|
@io.setup_tls!
|
|
1471
1623
|
# Server > v2.9.19 returns tls_required regardless of no_tls for WebSocket config being used so need to check URI.
|
|
1472
1624
|
elsif server_using_secure_connection? && !client_using_secure_connection? && (@uri.scheme != "ws")
|
|
@@ -1486,27 +1638,68 @@ module NATS
|
|
|
1486
1638
|
# Send ping/pong after connect
|
|
1487
1639
|
@io.write(PING_REQUEST)
|
|
1488
1640
|
|
|
1489
|
-
next_op =
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1641
|
+
next_op = next_connect_op(MonotonicTime.now + options[:connect_timeout])
|
|
1642
|
+
raise NATS::IO::ConnectError.new("expected PONG, got #{next_op}") unless next_op =~ NATS::Protocol::PONG
|
|
1643
|
+
|
|
1644
|
+
# On a connection that started TLS after INFO, nats-server 2.12 and
|
|
1645
|
+
# later send an INFO right after the first PONG, inside TLS. The
|
|
1646
|
+
# client waits for it, to know the servers it lists when connect
|
|
1647
|
+
# returns. The version in the INFO read before the handshake decides
|
|
1648
|
+
# only whether the client waits.
|
|
1649
|
+
if tls_after_info && !@options[:ignore_discovered_urls] && sends_info_after_first_pong?(@server_info[:version])
|
|
1650
|
+
read_info_after_first_pong
|
|
1494
1651
|
end
|
|
1652
|
+
end
|
|
1495
1653
|
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1654
|
+
# Reads the next operation of an attempt after CONNECT, until deadline.
|
|
1655
|
+
# A PING is answered and +OK is skipped: in verbose mode the server
|
|
1656
|
+
# answers CONNECT with +OK. A -ERR fails the attempt with the server's
|
|
1657
|
+
# error.
|
|
1658
|
+
def next_connect_op(deadline)
|
|
1659
|
+
loop do
|
|
1660
|
+
timeout = deadline - MonotonicTime.now
|
|
1661
|
+
raise NATS::IO::SocketTimeoutError if timeout <= 0
|
|
1662
|
+
|
|
1663
|
+
op = @io.read_line(timeout)
|
|
1664
|
+
case op
|
|
1665
|
+
when NATS::Protocol::PING
|
|
1666
|
+
@io.write(PONG_RESPONSE, timeout)
|
|
1667
|
+
when NATS::Protocol::OK
|
|
1668
|
+
# Skipped.
|
|
1669
|
+
when NATS::Protocol::ERR
|
|
1670
|
+
raise(@server_info[:auth_required] ? NATS::IO::AuthError.new($1) : NATS::IO::ServerError.new($1))
|
|
1502
1671
|
else
|
|
1503
|
-
|
|
1672
|
+
return op
|
|
1504
1673
|
end
|
|
1505
|
-
else
|
|
1506
|
-
raise NATS::IO::ConnectError.new("expected PONG, got #{next_op}")
|
|
1507
1674
|
end
|
|
1508
1675
|
end
|
|
1509
1676
|
|
|
1677
|
+
# Reads the INFO that follows the first PONG, for up to connect_timeout
|
|
1678
|
+
# from that PONG. When none comes in time, the client reports connected
|
|
1679
|
+
# without it, and the read loop takes it when it comes. Any other
|
|
1680
|
+
# operation, or a closed connection, fails the attempt.
|
|
1681
|
+
def read_info_after_first_pong
|
|
1682
|
+
op = begin
|
|
1683
|
+
next_connect_op(MonotonicTime.now + options[:connect_timeout])
|
|
1684
|
+
rescue NATS::IO::SocketTimeoutError
|
|
1685
|
+
# A close from another thread can end the wait the same way.
|
|
1686
|
+
raise if closed?
|
|
1687
|
+
|
|
1688
|
+
return
|
|
1689
|
+
end
|
|
1690
|
+
raise NATS::IO::ConnectError.new("expected INFO, got #{op}") unless op =~ NATS::Protocol::INFO
|
|
1691
|
+
|
|
1692
|
+
process_info($1)
|
|
1693
|
+
end
|
|
1694
|
+
|
|
1695
|
+
# Whether a server of this version sends an INFO right after the PONG
|
|
1696
|
+
# to the first PING, as nats-server does from 2.12.0 on. Only the three
|
|
1697
|
+
# numbers count, so a pre-release counts as its release.
|
|
1698
|
+
def sends_info_after_first_pong?(version)
|
|
1699
|
+
numbers = /\A(\d+)\.(\d+)\.(\d+)(?:[-+]|\z)/.match(version.to_s)&.captures&.map(&:to_i)
|
|
1700
|
+
!numbers.nil? && (numbers <=> [2, 12, 0]) >= 0
|
|
1701
|
+
end
|
|
1702
|
+
|
|
1510
1703
|
# Reconnect logic. generation is @close_generation from when the
|
|
1511
1704
|
# reconnect started; a close since then cancels the reconnect.
|
|
1512
1705
|
def attempt_reconnect(generation)
|
|
@@ -1524,16 +1717,8 @@ module NATS
|
|
|
1524
1717
|
|
|
1525
1718
|
srv = select_next_server
|
|
1526
1719
|
|
|
1527
|
-
# Set hostname to use for TLS hostname verification
|
|
1528
|
-
if client_using_secure_connection? && single_url_connect_used?
|
|
1529
|
-
# Reuse original hostname name in case of using TLS.
|
|
1530
|
-
@hostname ||= srv[:hostname]
|
|
1531
|
-
else
|
|
1532
|
-
@hostname = srv[:hostname]
|
|
1533
|
-
end
|
|
1534
|
-
|
|
1535
1720
|
# Establish TCP connection with new server
|
|
1536
|
-
@io = create_socket
|
|
1721
|
+
@io = create_socket(srv)
|
|
1537
1722
|
@io.connect
|
|
1538
1723
|
@stats[:reconnects] += 1
|
|
1539
1724
|
|
|
@@ -1631,8 +1816,10 @@ module NATS
|
|
|
1631
1816
|
end
|
|
1632
1817
|
|
|
1633
1818
|
# Try to write any pending flushes in case
|
|
1634
|
-
# we have a connection then close it.
|
|
1635
|
-
|
|
1819
|
+
# we have a connection then close it. While connecting or
|
|
1820
|
+
# reconnecting there is none: @io is an attempt that has not sent
|
|
1821
|
+
# CONNECT, or has failed.
|
|
1822
|
+
should_flush = !reconnecting? && !connecting? && @pending_queue && @io && @io.socket && !@io.closed?
|
|
1636
1823
|
if should_flush
|
|
1637
1824
|
begin
|
|
1638
1825
|
cmds = []
|
|
@@ -1649,9 +1836,16 @@ module NATS
|
|
|
1649
1836
|
# Destroy any remaining subscriptions.
|
|
1650
1837
|
@subs.clear
|
|
1651
1838
|
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1839
|
+
# A close from one of these callbacks closes without calling them
|
|
1840
|
+
# again.
|
|
1841
|
+
if do_cbs && @close_callbacks_thread != Thread.current
|
|
1842
|
+
begin
|
|
1843
|
+
@close_callbacks_thread = Thread.current
|
|
1844
|
+
@disconnect_cb&.call(@last_err)
|
|
1845
|
+
@close_cb&.call
|
|
1846
|
+
ensure
|
|
1847
|
+
@close_callbacks_thread = nil
|
|
1848
|
+
end
|
|
1655
1849
|
end
|
|
1656
1850
|
|
|
1657
1851
|
@status = conn_status
|
|
@@ -1793,7 +1987,7 @@ module NATS
|
|
|
1793
1987
|
@options[:reconnect]
|
|
1794
1988
|
end
|
|
1795
1989
|
|
|
1796
|
-
def create_socket
|
|
1990
|
+
def create_socket(srv)
|
|
1797
1991
|
socket_class = case @uri.scheme
|
|
1798
1992
|
when "nats", "tls"
|
|
1799
1993
|
NATS::IO::Socket
|
|
@@ -1804,9 +1998,15 @@ module NATS
|
|
|
1804
1998
|
raise NotImplementedError, "#{@uri.scheme} protocol is not supported, check NATS cluster URL spelling"
|
|
1805
1999
|
end
|
|
1806
2000
|
|
|
2001
|
+
# tls: {hostname:} sets the name every server is verified against,
|
|
2002
|
+
# and that is sent as SNI. discovered: a discovered server, verified
|
|
2003
|
+
# against the name the client gave it.
|
|
2004
|
+
hostname = @tls&.fetch(:hostname, nil)
|
|
2005
|
+
|
|
1807
2006
|
socket_class.new(
|
|
1808
2007
|
uri: @uri,
|
|
1809
|
-
tls: {context: tls_context, hostname: @hostname
|
|
2008
|
+
tls: {context: tls_context, hostname: hostname || @hostname, insecure_skip_verify: insecure_skip_verify?,
|
|
2009
|
+
discovered: !!srv[:discovered] && !hostname},
|
|
1810
2010
|
connect_timeout: NATS::IO::DEFAULT_CONNECT_TIMEOUT
|
|
1811
2011
|
)
|
|
1812
2012
|
end
|
|
@@ -2023,44 +2223,56 @@ module NATS
|
|
|
2023
2223
|
|
|
2024
2224
|
# (Re-)connect using secure connection if server and client agreed on using it.
|
|
2025
2225
|
def setup_tls!
|
|
2226
|
+
# Bytes the server sent after INFO and before the handshake are
|
|
2227
|
+
# dropped: from here on, only what comes through TLS is read.
|
|
2228
|
+
@unread&.clear
|
|
2229
|
+
|
|
2026
2230
|
# Setup TLS connection by rewrapping the socket
|
|
2027
2231
|
tls_socket = OpenSSL::SSL::SSLSocket.new(@socket, @tls.fetch(:context))
|
|
2028
2232
|
|
|
2029
2233
|
# Close TCP socket after closing TLS socket as well.
|
|
2030
2234
|
tls_socket.sync_close = true
|
|
2031
2235
|
|
|
2032
|
-
#
|
|
2236
|
+
# Sets SNI, and the name that a context with verify_hostname checks
|
|
2237
|
+
# during the handshake on CRuby:
|
|
2033
2238
|
# https://github.com/ruby/openssl/commit/028e495734e9e6aa5dba1a2e130b08f66cf31a21
|
|
2034
2239
|
tls_socket.hostname = @tls[:hostname]
|
|
2035
2240
|
|
|
2036
|
-
|
|
2241
|
+
begin
|
|
2242
|
+
tls_socket.connect
|
|
2243
|
+
rescue OpenSSL::SSL::SSLError => e
|
|
2244
|
+
e = OpenSSL::SSL::SSLError.new(name_check_failed(tls_socket.peer_cert_chain&.first)) if name_check_failed?(tls_socket)
|
|
2245
|
+
# Nothing is sent on a connection whose handshake failed.
|
|
2246
|
+
tls_socket.close
|
|
2247
|
+
raise e
|
|
2248
|
+
end
|
|
2249
|
+
verify_server!(tls_socket) unless @tls[:insecure_skip_verify]
|
|
2037
2250
|
@socket = tls_socket
|
|
2038
2251
|
end
|
|
2039
2252
|
|
|
2253
|
+
# Reads a line, up to and including "\n", or returns nil when the
|
|
2254
|
+
# connection is closed first. A read from the socket can take more
|
|
2255
|
+
# than the line, as one TLS record can carry several lines, so the
|
|
2256
|
+
# rest is kept for the next read_line or read. The deadline counts
|
|
2257
|
+
# from the call, for the whole line.
|
|
2040
2258
|
def read_line(deadline = nil)
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2259
|
+
@unread ||= String.new
|
|
2260
|
+
limit = deadline && MonotonicTime.now + deadline
|
|
2261
|
+
until (idx = @unread.index("\n"))
|
|
2262
|
+
raise NATS::IO::SocketTimeoutError if limit && MonotonicTime.now >= limit
|
|
2263
|
+
|
|
2264
|
+
@unread << read_socket(MAX_SOCKET_READ_BYTES, limit)
|
|
2044
2265
|
end
|
|
2045
|
-
@
|
|
2266
|
+
@unread.slice!(0, idx + 1)
|
|
2267
|
+
rescue EOFError
|
|
2268
|
+
nil
|
|
2046
2269
|
end
|
|
2047
2270
|
|
|
2048
2271
|
def read(max_bytes, deadline = nil)
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
retry
|
|
2054
|
-
else
|
|
2055
|
-
raise NATS::IO::SocketTimeoutError
|
|
2056
|
-
end
|
|
2057
|
-
rescue ::IO::WaitWritable
|
|
2058
|
-
if ::IO.select(nil, [@socket], nil, deadline)
|
|
2059
|
-
retry
|
|
2060
|
-
else
|
|
2061
|
-
raise NATS::IO::SocketTimeoutError
|
|
2062
|
-
end
|
|
2063
|
-
end
|
|
2272
|
+
# Hand over what read_line read beyond its last line first.
|
|
2273
|
+
return @unread.slice!(0, max_bytes) if @unread && !@unread.empty?
|
|
2274
|
+
|
|
2275
|
+
read_socket(max_bytes, deadline && MonotonicTime.now + deadline)
|
|
2064
2276
|
rescue EOFError => e
|
|
2065
2277
|
if (RUBY_ENGINE == "jruby") && (e.message == "No message available")
|
|
2066
2278
|
# FIXME: <EOFError: No message available> can happen in jruby
|
|
@@ -2117,6 +2329,160 @@ module NATS
|
|
|
2117
2329
|
|
|
2118
2330
|
private
|
|
2119
2331
|
|
|
2332
|
+
# limit: a MonotonicTime.now value, or nil to wait without one. A TLS
|
|
2333
|
+
# socket can be readable and still raise IO::WaitReadable until the
|
|
2334
|
+
# rest of a record comes, so each wait takes only the time left.
|
|
2335
|
+
def read_socket(max_bytes, limit)
|
|
2336
|
+
@socket.read_nonblock(max_bytes)
|
|
2337
|
+
rescue ::IO::WaitReadable
|
|
2338
|
+
if ::IO.select([@socket], nil, nil, time_left(limit))
|
|
2339
|
+
retry
|
|
2340
|
+
else
|
|
2341
|
+
raise NATS::IO::SocketTimeoutError
|
|
2342
|
+
end
|
|
2343
|
+
rescue ::IO::WaitWritable
|
|
2344
|
+
if ::IO.select(nil, [@socket], nil, time_left(limit))
|
|
2345
|
+
retry
|
|
2346
|
+
else
|
|
2347
|
+
raise NATS::IO::SocketTimeoutError
|
|
2348
|
+
end
|
|
2349
|
+
end
|
|
2350
|
+
|
|
2351
|
+
# The seconds until limit, or nil without one. Raises
|
|
2352
|
+
# NATS::IO::SocketTimeoutError once limit has passed.
|
|
2353
|
+
def time_left(limit)
|
|
2354
|
+
return nil unless limit
|
|
2355
|
+
|
|
2356
|
+
left = limit - MonotonicTime.now
|
|
2357
|
+
raise NATS::IO::SocketTimeoutError if left <= 0
|
|
2358
|
+
|
|
2359
|
+
left
|
|
2360
|
+
end
|
|
2361
|
+
|
|
2362
|
+
# A context checks only what it is set up to check: one made with
|
|
2363
|
+
# OpenSSL::SSL::SSLContext.new checks nothing, and jruby-openssl before
|
|
2364
|
+
# 0.19.0 checks no name during the handshake. So the client checks the
|
|
2365
|
+
# chain and the name after every handshake, before it sends anything.
|
|
2366
|
+
def verify_server!(tls_socket)
|
|
2367
|
+
# jruby-openssl keeps the result of the last check per context, so
|
|
2368
|
+
# after a resumed session, or with another handshake on the same
|
|
2369
|
+
# context, the result can be that of another server. With VERIFY_PEER
|
|
2370
|
+
# it fails the handshake when the chain does not verify.
|
|
2371
|
+
if RUBY_ENGINE == "jruby"
|
|
2372
|
+
if (tls_socket.context.verify_mode.to_i & OpenSSL::SSL::VERIFY_PEER).zero?
|
|
2373
|
+
raise OpenSSL::SSL::SSLError, "certificate verify failed (the context does not set VERIFY_PEER) for #{@uri.host}:#{@uri.port}"
|
|
2374
|
+
end
|
|
2375
|
+
elsif (result = tls_socket.verify_result) != OpenSSL::X509::V_OK
|
|
2376
|
+
# A context with verify_hostname but without VERIFY_PEER completes
|
|
2377
|
+
# the handshake after the name check failed.
|
|
2378
|
+
raise OpenSSL::SSL::SSLError, name_check_failed(tls_socket.peer_cert) if name_check_failed?(tls_socket)
|
|
2379
|
+
raise OpenSSL::SSL::SSLError, "certificate verify failed (#{verify_error_string(result)}) for #{@uri.host}:#{@uri.port}"
|
|
2380
|
+
end
|
|
2381
|
+
|
|
2382
|
+
cert = tls_socket.peer_cert
|
|
2383
|
+
unless certificate_identity?(cert, @tls[:hostname])
|
|
2384
|
+
raise OpenSSL::SSL::SSLError, name_check_failed(cert)
|
|
2385
|
+
end
|
|
2386
|
+
rescue
|
|
2387
|
+
tls_socket.close
|
|
2388
|
+
raise
|
|
2389
|
+
end
|
|
2390
|
+
|
|
2391
|
+
# Whether the certificate is issued for name, by the rules of CRuby's
|
|
2392
|
+
# OpenSSL::SSL.verify_certificate_identity: the DNS names and IP
|
|
2393
|
+
# addresses of subjectAltName, else the common name. That of
|
|
2394
|
+
# jruby-openssl before 0.19.0 compares the printed names, in which ::1
|
|
2395
|
+
# is 0:0:0:0:0:0:0:1.
|
|
2396
|
+
def certificate_identity?(cert, name)
|
|
2397
|
+
return false unless cert
|
|
2398
|
+
|
|
2399
|
+
ip = begin
|
|
2400
|
+
IPAddr.new(name).hton
|
|
2401
|
+
rescue IPAddr::Error
|
|
2402
|
+
nil
|
|
2403
|
+
end
|
|
2404
|
+
sans = subject_alt_names(cert).select { |san| san.tag == 2 || san.tag == 7 }
|
|
2405
|
+
if sans.empty?
|
|
2406
|
+
return cert.subject.to_a.any? { |oid, value| oid == "CN" && OpenSSL::SSL.verify_hostname(name, value) }
|
|
2407
|
+
end
|
|
2408
|
+
|
|
2409
|
+
sans.any? { |san| (san.tag == 2) ? OpenSSL::SSL.verify_hostname(name, san.value) : san.value == ip }
|
|
2410
|
+
rescue OpenSSL::ASN1::ASN1Error
|
|
2411
|
+
false
|
|
2412
|
+
end
|
|
2413
|
+
|
|
2414
|
+
# The GeneralName entries of the certificate's subjectAltName: tag 2 is
|
|
2415
|
+
# a DNS name, tag 7 an IP address in network byte order.
|
|
2416
|
+
def subject_alt_names(cert)
|
|
2417
|
+
cert.extensions.select { |ext| ext.oid == "subjectAltName" }.flat_map do |ext|
|
|
2418
|
+
ostr = OpenSSL::ASN1.decode(ext.to_der).value.last
|
|
2419
|
+
OpenSSL::ASN1.decode(ostr.value).value
|
|
2420
|
+
end
|
|
2421
|
+
end
|
|
2422
|
+
|
|
2423
|
+
# Whether the handshake failed on the name: on CRuby a context with
|
|
2424
|
+
# verify_hostname checks it during the handshake.
|
|
2425
|
+
def name_check_failed?(tls_socket)
|
|
2426
|
+
defined?(OpenSSL::X509::V_ERR_HOSTNAME_MISMATCH) &&
|
|
2427
|
+
tls_socket.verify_result == OpenSSL::X509::V_ERR_HOSTNAME_MISMATCH
|
|
2428
|
+
end
|
|
2429
|
+
|
|
2430
|
+
# The error for a certificate that is not issued for the name the
|
|
2431
|
+
# server is verified against: that name, the names the certificate is
|
|
2432
|
+
# issued for (at most four) and the setting for the name. Without the
|
|
2433
|
+
# certificate it leaves the names out: OpenSSL 1.1 keeps none when the
|
|
2434
|
+
# name check fails during the handshake. For a discovered server, also
|
|
2435
|
+
# what the cluster can advertise instead, and the setting for
|
|
2436
|
+
# certificates that carry the server's IP address.
|
|
2437
|
+
def name_check_failed(cert)
|
|
2438
|
+
names = cert ? certificate_names(cert) : []
|
|
2439
|
+
text = "hostname #{@tls[:hostname].inspect} does not match the server certificate of #{@uri.host}:#{@uri.port}"
|
|
2440
|
+
if cert
|
|
2441
|
+
issued_for = names.empty? ? "no name" : names.first(4).map(&:inspect).join(", ")
|
|
2442
|
+
issued_for += " and #{names.size - 4} more" if names.size > 4
|
|
2443
|
+
text += ", which is issued for #{issued_for}"
|
|
2444
|
+
end
|
|
2445
|
+
text += "; tls: {hostname:} sets the name every server is verified against"
|
|
2446
|
+
return text unless @tls[:discovered]
|
|
2447
|
+
|
|
2448
|
+
advertise = %w[ws wss].include?(@uri.scheme) ? "advertise in the websocket block of nats-server" : "client_advertise in nats-server"
|
|
2449
|
+
text += ", or each server of the cluster can advertise a name its certificate is issued for (#{advertise})"
|
|
2450
|
+
if issued_for_address?(names)
|
|
2451
|
+
text += ", or tls: {verify_discovered_servers_by_ip: true} verifies a discovered IP address against itself, " \
|
|
2452
|
+
"which is safe only where the CA that issued the certificates is not shared with others"
|
|
2453
|
+
end
|
|
2454
|
+
text
|
|
2455
|
+
end
|
|
2456
|
+
|
|
2457
|
+
# Whether names hold the IP address the server was dialed at.
|
|
2458
|
+
def issued_for_address?(names)
|
|
2459
|
+
names.include?(IPAddr.new(@uri.hostname).to_s)
|
|
2460
|
+
rescue IPAddr::Error
|
|
2461
|
+
false
|
|
2462
|
+
end
|
|
2463
|
+
|
|
2464
|
+
# The names certificate_identity? checks: the DNS names and IP
|
|
2465
|
+
# addresses of subjectAltName, else the common name.
|
|
2466
|
+
def certificate_names(cert)
|
|
2467
|
+
names = subject_alt_names(cert).filter_map do |san|
|
|
2468
|
+
case san.tag
|
|
2469
|
+
when 2 then san.value
|
|
2470
|
+
when 7 then IPAddr.new_ntoh(san.value).to_s if [4, 16].include?(san.value.bytesize)
|
|
2471
|
+
end
|
|
2472
|
+
end
|
|
2473
|
+
return names unless names.empty?
|
|
2474
|
+
|
|
2475
|
+
cert.subject.to_a.filter_map { |oid, value| value if oid == "CN" }
|
|
2476
|
+
rescue OpenSSL::ASN1::ASN1Error
|
|
2477
|
+
[]
|
|
2478
|
+
end
|
|
2479
|
+
|
|
2480
|
+
def verify_error_string(code)
|
|
2481
|
+
store_ctx = OpenSSL::X509::StoreContext.new(OpenSSL::X509::Store.new)
|
|
2482
|
+
store_ctx.error = code
|
|
2483
|
+
store_ctx.error_string
|
|
2484
|
+
end
|
|
2485
|
+
|
|
2120
2486
|
def connect_addrinfo(ai, port, timeout)
|
|
2121
2487
|
sock = ::Socket.new(::Socket.const_get(ai[0]), ::Socket::SOCK_STREAM, 0)
|
|
2122
2488
|
sockaddr = ::Socket.pack_sockaddr_in(port, ai[3])
|
data/lib/nats/io/errors.rb
CHANGED
|
@@ -48,6 +48,10 @@ module NATS
|
|
|
48
48
|
# When we use an invalid subject.
|
|
49
49
|
class BadSubject < Error; end
|
|
50
50
|
|
|
51
|
+
# When a publish is larger than the max_payload the server advertised.
|
|
52
|
+
# Raised before the message is written: the server would close the connection.
|
|
53
|
+
class MaxPayloadError < Error; end
|
|
54
|
+
|
|
51
55
|
# When an invalid subscription is used, like one already unsubscribed
|
|
52
56
|
# or when the NATS connection is already closed.
|
|
53
57
|
class BadSubscription < Error; end
|
data/lib/nats/io/parser.rb
CHANGED
|
@@ -84,7 +84,9 @@ module NATS
|
|
|
84
84
|
# First INFO message is processed synchronously on connect,
|
|
85
85
|
# and onwards we would be receiving asynchronously INFO commands
|
|
86
86
|
# signaling possible changes in the topology of the NATS cluster.
|
|
87
|
-
|
|
87
|
+
# A read loop that close or a reconnect stopped reads the old
|
|
88
|
+
# connection: the current server may be the next one.
|
|
89
|
+
@nc.send(:process_info, $1) unless @nc.send(:stopping?)
|
|
88
90
|
when UNKNOWN
|
|
89
91
|
@buf = $'
|
|
90
92
|
@nc.send(:process_err, "Unknown protocol: #{$1}")
|
data/lib/nats/io/version.rb
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
module NATS
|
|
18
18
|
module IO
|
|
19
19
|
# VERSION is the version of the client announced on CONNECT to the server.
|
|
20
|
-
VERSION = "2.
|
|
20
|
+
VERSION = "2.8.0"
|
|
21
21
|
|
|
22
22
|
# LANG is the lang runtime of the client announced on CONNECT to the server.
|
|
23
23
|
LANG = "#{RUBY_ENGINE}#{RUBY_VERSION}".freeze
|
data/lib/nats/io/websocket.rb
CHANGED
|
@@ -75,7 +75,7 @@ module NATS
|
|
|
75
75
|
raise HandshakeError, "Attempted to write to socket while WebSocket handshake is in progress" unless @handshaked
|
|
76
76
|
|
|
77
77
|
frame = ::WebSocket::Frame::Outgoing::Client.new(data: data, type: :binary, version: @handshake.version)
|
|
78
|
-
super(frame.to_s)
|
|
78
|
+
super(frame.to_s, deadline)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
81
|
private
|
data/sig/nats/io/client.rbs
CHANGED
|
@@ -80,7 +80,8 @@ module NATS
|
|
|
80
80
|
@tls: bool?
|
|
81
81
|
|
|
82
82
|
@hostname: String?
|
|
83
|
-
@
|
|
83
|
+
@configured_servers: Array[[String?, Integer?]]
|
|
84
|
+
@without_tls_name: Hash[String, bool]
|
|
84
85
|
|
|
85
86
|
@connect_called: bool
|
|
86
87
|
|
|
@@ -159,7 +160,21 @@ module NATS
|
|
|
159
160
|
|
|
160
161
|
def validate_settings!: () -> void
|
|
161
162
|
|
|
162
|
-
def process_info: (String) -> Hash[Symbol, untyped]
|
|
163
|
+
def process_info: (String, ?discover: bool, ?first: bool) -> Hash[Symbol, untyped]
|
|
164
|
+
|
|
165
|
+
def discovered_uri: (untyped) -> URI::Generic?
|
|
166
|
+
|
|
167
|
+
def discovered_tls_name: (URI::Generic) -> String?
|
|
168
|
+
|
|
169
|
+
def without_tls_name_error: (String) -> NATS::IO::ConnectError
|
|
170
|
+
|
|
171
|
+
def ip_address?: (String?) -> bool
|
|
172
|
+
|
|
173
|
+
def next_connect_op: (Float) -> String?
|
|
174
|
+
|
|
175
|
+
def read_info_after_first_pong: () -> void
|
|
176
|
+
|
|
177
|
+
def sends_info_after_first_pong?: (untyped) -> bool
|
|
163
178
|
|
|
164
179
|
def process_hdr: (String) -> Hash[String, untyped]?
|
|
165
180
|
|
|
@@ -177,7 +192,7 @@ module NATS
|
|
|
177
192
|
|
|
178
193
|
def client_using_secure_connection?: () -> bool
|
|
179
194
|
|
|
180
|
-
def
|
|
195
|
+
def verify_discovered_servers_by_ip?: () -> bool
|
|
181
196
|
|
|
182
197
|
def send_command: (String) -> void
|
|
183
198
|
|
|
@@ -225,7 +240,7 @@ module NATS
|
|
|
225
240
|
|
|
226
241
|
def should_reconnect?: () -> bool
|
|
227
242
|
|
|
228
|
-
def create_socket: () -> NATS::IO::Socket
|
|
243
|
+
def create_socket: (Hash[Symbol, untyped]) -> NATS::IO::Socket
|
|
229
244
|
|
|
230
245
|
def setup_nkeys_connect: () -> void
|
|
231
246
|
|
data/sig/nats/io/errors.rbs
CHANGED