nats-pure 2.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d5c22758be6db2c05382a55c718c2c4f836a2c723f77820d4728bbf1538f30b
4
- data.tar.gz: 1745fb8cfa406b598099779a027a3cc3dcc4cee28c22fd7c08e1b156b6ce4a5b
3
+ metadata.gz: de4e341849ae05c48b13d964c812114e6d2101bfc846269c111f8dcb8532d6c4
4
+ data.tar.gz: 7d02ff8cac707f0c79d725c33f2e51f958b6553b7be726f881a19b1038f31202
5
5
  SHA512:
6
- metadata.gz: 84b049092f60f34c3ca6b5a8ac2b4334a1a32dbaefd2659424f91db3472df105604bbddd4989793a56757a9cdee1c710db6b52fc02e40b95f64bc0b0eae21dd6
7
- data.tar.gz: 38585616a5820faefe491c8c16d815bc52759e0cd2b7f77ab5b3a0227d3728720c4f1ac56d6d2af6354c2c53e54245ab48ce71c2a063efe3fe737e60e21c6af7
6
+ metadata.gz: adce199af4dcb6c58587d497117463e1f23512f5b2ae3efa4586fe8663e3418e6ee2a84573eb2c48d52d71d1f29b8a867a759c17b1189b46c17521970d615c49
7
+ data.tar.gz: cdf9b5eae05f396ee8189e99e5d441a181ff5879639384ee197e7b74b880e668ae21391841a1a0d8de5108195f9c4fbb57b2a26c6b7bbba3a95505bf92cd5fdf
data/CHANGELOG.md CHANGED
@@ -2,6 +2,111 @@
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
+
47
+ ## v2.7.0
48
+
49
+ This release adds the JetStream features of nats-server 2.10 to 2.14 that the
50
+ client lacked, such as per-message TTLs, message schedules, and consumers that
51
+ can be paused, reset and serve priority groups. `fetch` now pulls once and
52
+ waits, like `Fetch` of the nats.go `jetstream` package, and a subscription read
53
+ with `next_msg` no longer drops every message after its first 64 MiB. Code that
54
+ passed stream or KV settings the client used to drop now sends them; see Changed.
55
+
56
+ ### Added
57
+
58
+ - JetStream: `fetch(batch, no_wait: true)` takes what the server delivers at once, without waiting for more messages, like `FetchNoWait` of nats.go and `fetch` of nats.rs: an empty Array when none are pending, or when other pulls wait for more than are pending. While the consumer may not deliver more until some are acked (`max_ack_pending`), the server holds the pull back, and the fetch returns what it got once its timeout is up, 1 second by default. (#195)
59
+ - JetStream: `fetch` takes a block, to which it passes each delivery as it comes, in the calling thread, so that the message can be acked before the `ack_wait` of the consumer is up: `psub.fetch(100, timeout: 60) { |msg| msg.ack }`. It still returns the messages. (#195)
60
+ - JetStream: stream configs take the settings added in nats-server 2.10: `compression`, `first_seq`, `subject_transform` and `consumer_limits` (whose `inactive_threshold` is in nanoseconds, like the other stream durations). They used to be dropped, so a stream could not be created with them, and sending a fetched config back as an update left them out. (#191)
61
+ - JetStream: stream configs take the settings added in nats-server 2.11 to 2.14: `allow_msg_ttl` and `subject_delete_marker_ttl` (2.11; the latter in nanoseconds, like the other stream durations), `allow_msg_counter`, `allow_atomic`, `allow_msg_schedules` and `persist_mode` (2.12), and `allow_batched` (2.14). Like nats.go, the client does not send them at their defaults, such as `false`, as servers since 2.12, and 2.11 ones in strict mode, refuse settings they do not know. (#194)
62
+ - JetStream: `NATS::JetStream::Header` names the JetStream headers: those for deduplication and optimistic concurrency, such as `MSG_ID` and `EXPECTED_LAST_SUBJECT_SEQUENCE`, and those of nats-server 2.11 to 2.14: `EXPECTED_LAST_SUBJECT_SEQUENCE_SUBJECT`, `MSG_TTL` and `MARKER_REASON` (2.11), the atomic batch headers (2.12; `BATCH_COMMIT` `"eob"` needs 2.14), the message schedule headers (2.12 and 2.14), and `REQUIRED_API_LEVEL` (2.12), which JetStream API requests send as a `header:` option, as in `js.stream_info(name, header: {...})`. It also names the headers that the server sets on the messages it republishes, sources or returns from direct gets, such as `STREAM` and `SEQUENCE`. (#194)
63
+ - JetStream: `js.publish` takes `ttl:`, the whole seconds after which the stream removes the message, or `:never` to keep it past the stream's `max_age`, for streams with `allow_msg_ttl` (nats-server 2.11). Other values raise `ArgumentError`. (#194)
64
+ - JetStream: `js.publish` takes `schedule:`, which makes the message a schedule that publishes it to its `target:` subject: once, `at:` a Time (nats-server 2.12), `every:` so many seconds, or on a `cron:` expression with seconds, in UTC or a `time_zone:` (2.14). The schedule can give the messages it publishes a `ttl:` (2.12), and publish the last message of a `source:` subject instead and roll up the target (`rollup: true`) (2.14; 2.12 ignores both). The stream needs `allow_msg_schedules`. An invalid schedule raises `ArgumentError`, before anything is sent; `NATS::JetStream::Header::SCHEDULE_NEXT` and `SCHEDULER` cancel a schedule. (#194)
65
+ - JetStream: `stream_info` reports the stream's `mirror` and `sources`, including their subject transforms, and both `stream_info` and `consumer_info` report when the server reported the info, as `ts`. (#191)
66
+ - JetStream: `create_consumer` and `update_consumer` use the create and update actions of nats-server 2.10. Creating a consumer that exists with a different config raises `NATS::JetStream::Error::ConsumerAlreadyExists`, and updating one that does not exist raises `NATS::JetStream::Error::ConsumerDoesNotExist`; both are `BadRequest` errors. An update replaces the whole config, so fields left out take their defaults. `add_consumer` still creates or updates. (#191)
67
+ - JetStream: `msg.term(reason: "...")` passes the reason on to the server's advisory for the terminated message. Servers before 2.10.4 do not terminate a message given a reason. (#191)
68
+ - JetStream: consumers can be paused (nats-server 2.11): created paused with `pause_until` in their config, as a `Time` or an RFC 3339 String, or paused and resumed with `pause_consumer` and `resume_consumer`; updating a consumer keeps its pause. `consumer_info` reports whether a consumer is `paused`, and the seconds until it resumes, as `pause_remaining`. Servers before 2.11 create consumers unpaused, and let `pause_consumer` and `resume_consumer` time out. (#193)
69
+ - JetStream: pull consumers can serve priority groups (nats-server 2.11). Consumer configs take `priority_policy`, `priority_groups` and `priority_timeout` (in seconds, like the other consumer durations), and `consumer_info` reports the state of the groups as `priority_groups`. `fetch` takes the `group` to pull from, `min_pending` and `min_ack_pending` for the `"overflow"` policy, and `priority` for the `"prioritized"` policy of nats-server 2.12. (#193)
70
+ - JetStream: with the `"pinned_client"` priority policy, a pull subscription keeps the pin id that the server delivers with its messages, and sends it with its pulls. When the server says that the subscription is no longer pinned, as its pin expired or it was unpinned with the new `unpin_consumer`, a fetch that got no messages raises `NATS::JetStream::Error::PinIdMismatch`, and the next fetch can be pinned again. The server counts the consumer's `priority_timeout` from each pull, not while a pull waits, so keep the fetch timeout, plus the time between fetches, below it, or the subscription loses its pin. (#193, #195)
71
+ - JetStream: `reset_consumer` makes a consumer deliver again from after its ack floor, or from the stream sequence given as `seq` (nats-server 2.14). A sequence that the deliver policy of the consumer does not let it be reset to raises `NATS::JetStream::Error::ConsumerInvalidReset`, a `BadRequest` error, and one that is not an integer of 0 or more raises `ArgumentError`. For a stream or consumer that does not exist, and with servers before 2.14, the request times out. (#193)
72
+ - JetStream: `stream_info` and `consumer_info` report the `cluster` of the stream or consumer as a Hash, including when its leader was elected, as `leader_since` (nats-server 2.12). `consumer_info` used to drop it. (#193)
73
+ - KV: buckets can be created with `compression: true` and with `metadata`, which `status.compressed?` and `status.metadata` report. (#191)
74
+ - KV: `watch` takes an Array of keys, watched by a single consumer (nats-server 2.10). An empty Array watches all keys. (#191)
75
+
76
+ ### Changed
77
+
78
+ - JetStream: `fetch` pulls once, for the whole batch, and waits: it returns as soon as it has the batch, and otherwise the messages that came when its timeout is up, like `Fetch` of the nats.go `jetstream` package and `batch` of nats.rs. A fetch of more than one message used to ask first, without waiting, for the messages that were pending. When it got some, it then waited out its timeout without pulling for more, so it neither took the messages that came meanwhile nor returned sooner. When other pulls of the consumer waited for more messages than were pending, the server turned that first pull away (408 Requests Pending), and `fetch` raised `NATS::IO::Timeout` at once. (#195)
79
+ - JetStream: a pull subscription subscribes to `<inbox>.*`, which `subject` now returns, and each pull gets a reply subject of its own under it, as ADR-13 describes, so that a fetch tells the status that ended its own pull from those that ended the pulls of other fetches. A `fetch` with `no_wait: true` pulls with a subscription of its own, which it drops when done, so that its pull does not linger when the server holds it, as while the consumer may not deliver more messages until some are acked (`max_ack_pending`): the fetch then returns what it got once its timeout is up, and leaves what comes later to the next fetch. A server with leafnodes or gateways delivers to a pull until it is 2 seconds old even once the subscription is gone, so the subscription stays until then. (#195)
80
+ - JetStream: `fetch` raises `ArgumentError` for a `timeout` that is not a finite positive number, such as 0, with which it used to raise `NATS::IO::Timeout` at once. It leaves the options it is given unchanged; it used to set their `:timeout`. (#195)
81
+ - JetStream: as `fetch` always pulls with an expiry of its timeout, a consumer whose `max_expires` is shorter than the timeout turns every fetch away, with 409 "Exceeded MaxRequestExpires", raised as `NATS::JetStream::API::Error`. A fetch of more than one message used to get the messages that were pending first, with a pull that had no expiry. Pass a `timeout` no longer than the consumer's `max_expires`. (#195)
82
+ - JetStream: `fetch` returns when the server ends its pull, so it takes its timeout plus the round trip to the server. When no end comes, it waits up to 1 second longer, like nats.go, before it returns what it got or raises `NATS::IO::Timeout`. (#195)
83
+ - JetStream and KV: settings that used to be dropped are now sent. Code that already passed `compression`, `first_seq`, `subject_transform` or `consumer_limits` to `add_stream`, or `compression` or `metadata` to `create_key_value`, created its streams and buckets without them, so calling it again for an existing stream or bucket now fails with "stream name already in use with a different configuration" (err_code 10058). Apply the setting with `update_stream`, or stop passing it. (#191)
84
+ - JetStream: the same goes for the settings of nats-server 2.11 to 2.14: code that passed them to `add_stream` created its streams without them, so calling it again for an existing stream now fails with err_code 10058. `update_stream` applies them, except `allow_msg_counter` and `persist_mode`, which a stream cannot change. (#194)
85
+ - JetStream: consumer configs now send `pause_until`, `priority_policy`, `priority_groups` and `priority_timeout`, which used to be dropped. As `add_consumer` creates or updates, code that already passed the priority settings to it now gives an existing consumer priority groups, after which the server turns away pulls that name no group (400 "Priority Group missing"). (#193)
86
+ - KV: `create_key_value` takes only `true` or `false` for `compression`; anything else, such as `"s2"`, which used to be ignored, raises `ArgumentError`. (#191)
87
+ - JetStream: `add_consumer` leaves the `ConsumerConfig` it is given unchanged. It used to set its `name` and a default `ack_policy`, and convert its durations to nanoseconds, so passing the same config again sent durations a billion times too long. (#191)
88
+ - JetStream: two `stream_info` or `consumer_info` results are no longer `==` unless they were reported at the same time, as they now carry `ts`, and the `cluster`, `mirror` and `sources`, whose lag and activity change all the time, even with servers that do not report `ts`. (#191, #193)
89
+ - KV: `watch` no longer looks up the bucket's stream before creating its consumer. (#191)
90
+
91
+ ### Fixed
92
+
93
+ - JetStream: `update_stream` with the config from `stream_info` failed with err_code 10052, such as "message TTL status can not be disabled", for streams with per-message TTLs, subject delete markers, counters, message schedules or async persistence, and turned off atomic and fast batches, as `StreamConfig` dropped the settings of nats-server 2.11 to 2.14. It now keeps them. (#194)
94
+ - JetStream: `StreamConfig` dropped `discard_new_per_subject`, so `add_stream` created streams without it, and `update_stream` with the config from `stream_info` turned it off: a subject that held `max_msgs_per_subject` messages then discarded its oldest message instead of refusing the new one. It now keeps it. (#199)
95
+ - JetStream: `js.publish` with `stream:` added `Nats-Expected-Stream` to the header Hash it was given, so a caller that reused the Hash sent it with its later publishes. (#194)
96
+ - JetStream: `js.publish` raised `ArgumentError: unknown keywords` after the server had stored the message, when publishing to a stream with counters or committing an atomic batch, as the acks of nats-server 2.12 carry `val`, `batch` and `count`. `PubAck` now has these fields and ignores any it does not know, so fields added to acks later cannot break publish. (#192, #177, thanks @jnowakplacewise)
97
+ - JetStream: `add_consumer`, `consumer_info`, `subscribe` and `pull_subscribe` raised `NoMethodError` for consumers that do not ack (`ack_policy: "none"`) created without an `ack_wait`, after the consumer had been created. The server omits `ack_wait` for them, and `config.ack_wait` is now `nil`. (#192)
98
+ - JetStream: `fetch` took a status that an earlier pull had left in the subscription, such as the 408 of a fetch that timed out, for the reply to its own pull, and raised `NATS::IO::Timeout` at once, even with messages pending: a fetch of more than one message with any such status, a fetch of one message with a second one. `fetch` now first takes what earlier pulls left, and pulls for the rest only. And a fetch that got messages before an error status, such as 409 Consumer Deleted, raised the error and dropped them; like nats.go, it now returns them. (#193, #195)
99
+ - JetStream: when threads fetched from the same subscription at once, a fetch could take the 408 that ended the pull of another for the end of its own, and raise `NATS::IO::Timeout` before its timeout. (#195)
100
+ - JetStream: `fetch` pulled with an expiry a little short of its timeout only after a first pull, which did not wait, came back empty, so its pull outlived it by the time that first pull took: a message delivered meanwhile waited in the subscription for the next fetch, and, if none came, was delivered again once the `ack_wait` was up. A fetch now waits for the server to end its pull, which expires with its timeout. (#195)
101
+ - JetStream: a `fetch` that waited longer than the `ack_wait` of the consumer returned a message twice when the server delivered it again meanwhile. It now returns each message once, as last delivered. (#195)
102
+ - JetStream: `fetch` took a message of the stream with a `Status` header for a status of the server, and raised an error or ended without it. Statuses are now only the messages with a `Status` header that the server sends without a reply. (#195)
103
+ - JetStream: a `fetch` of one message raised `TypeError: nil can't be coerced into Float` when threads fetched from the same subscription at once: woken up for a message that another thread took, it did not wait on. It now waits until its timeout. (#193, #181, thanks @route)
104
+ - JetStream: a `fetch` of one message raised `NATS::IO::Timeout` ("nats: fetch request timeout") before its timeout when a status that ended an earlier pull, such as the 408 of a fetch that had just timed out, came while it waited, and left its own message for the next fetch. Like nats.go, it now waits on for its message until its timeout. (#193)
105
+ - JetStream: `fetch` could miss the reply to its pull when it came before the fetch waited for it, as when its thread was preempted: a fetch of one message then waited out its timeout and raised `NATS::IO::Timeout`, leaving its message to be redelivered after the ack wait, and a fetch of more messages returned only the first one, after its timeout. `fetch` now takes what is there before it waits. (#193)
106
+ - KV: after its consumer was recreated, as after a server restart, a watcher yielded the entries of every key in the bucket instead of the watched keys. The recreated consumer now keeps the keys filter. (#192)
107
+ - KV: after its consumer was recreated, a watcher yielded its last entry again, and a watcher with no entries yet, such as one on an empty bucket, never got its consumer back (`err_code` 10094) and stayed silent. The recreated consumer now starts after the last entry. (#192, #175, thanks @Xayc73)
108
+ - A subscription without a callback, read with `next_msg`, dropped every message and reported `NATS::IO::SlowConsumer` once it had received its `pending_bytes_limit` in all, 64 MiB by default, however fast it was read, as `next_msg` did not take the messages it returned off the pending bytes. (#196)
109
+
5
110
  ## v2.6.0
6
111
 
7
112
  This release prepares nats-pure for nats-server 2.16, whose new JetStream
data/README.md CHANGED
@@ -102,6 +102,89 @@ loop do
102
102
  end
103
103
  ```
104
104
 
105
+ `fetch` returns as soon as it has the batch, or once its timeout is up, the
106
+ messages that came. With `no_wait: true`, it takes what the server delivers at
107
+ once, without waiting for more. Given a block, it passes each message to it as it comes,
108
+ so that the message can be acked before the `ack_wait` of the consumer is up:
109
+
110
+ ```ruby
111
+ psub.fetch(100, timeout: 60) do |msg|
112
+ puts msg.data
113
+ msg.ack
114
+ end
115
+ ```
116
+
117
+ Messages can expire on their own, in streams that allow it (nats-server 2.11):
118
+
119
+ ```ruby
120
+ js.add_stream(name: "sessions", subjects: ["sessions.>"], allow_msg_ttl: true)
121
+
122
+ # Removed after an hour. With ttl: :never, kept past the max_age of the stream.
123
+ js.publish("sessions.42", "token", ttl: 3600)
124
+ ```
125
+
126
+ Messages can also be scheduled (nats-server 2.12; `every:` and `cron:` need 2.14):
127
+
128
+ ```ruby
129
+ js.add_stream(name: "reminders", subjects: ["reminders.>", "due.>"], allow_msg_schedules: true)
130
+
131
+ # Published to due.42 in an hour. Another schedule on reminders.42 replaces it.
132
+ js.publish("reminders.42", "call back", schedule: {at: Time.now + 3600, target: "due.42"})
133
+
134
+ # Published to due.standup at 9:00 on weekdays, Warsaw time.
135
+ js.publish("reminders.standup", "stand-up",
136
+ schedule: {cron: "0 0 9 * * 1-5", time_zone: "Europe/Warsaw", target: "due.standup"})
137
+
138
+ # Cancels the schedule on reminders.42.
139
+ js.publish("reminders.cancelled", "", header: {
140
+ NATS::JetStream::Header::SCHEDULE_NEXT => "purge",
141
+ NATS::JetStream::Header::SCHEDULER => "reminders.42"
142
+ })
143
+ ```
144
+
145
+ Streams and consumers can also be managed directly:
146
+
147
+ ```ruby
148
+ # Compressed, and no consumer may have more than 1000 unacknowledged messages.
149
+ js.add_stream(name: "orders", subjects: ["orders.>"], compression: "s2",
150
+ consumer_limits: {max_ack_pending: 1000})
151
+
152
+ config = {durable_name: "billing", filter_subject: "orders.paid", max_ack_pending: 100}
153
+
154
+ # Fails with NATS::JetStream::Error::ConsumerAlreadyExists if "billing"
155
+ # exists with a different config.
156
+ js.create_consumer("orders", config)
157
+
158
+ # An update replaces the whole config, so send all of it again.
159
+ # Fails with NATS::JetStream::Error::ConsumerDoesNotExist if there is no "billing".
160
+ js.update_consumer("orders", config.merge(max_ack_pending: 200))
161
+
162
+ # Deliver nothing for an hour, or until resumed.
163
+ js.pause_consumer("orders", "billing", Time.now + 3600)
164
+ js.resume_consumer("orders", "billing")
165
+ ```
166
+
167
+ Pull consumers can serve priority groups. With the `"pinned_client"` policy,
168
+ the server delivers to one subscription at a time, and to another once it
169
+ stops pulling for `priority_timeout` seconds. The server counts it from
170
+ each pull, not while a pull waits: keep the fetch timeout, 5 seconds by
171
+ default, plus the time between fetches, below `priority_timeout`, or the
172
+ subscription can lose its pin:
173
+
174
+ ```ruby
175
+ js.create_consumer("orders", durable_name: "shipping", filter_subject: "orders.paid",
176
+ priority_policy: "pinned_client", priority_groups: ["workers"], priority_timeout: 60)
177
+
178
+ psub = js.pull_subscribe("orders.paid", "shipping", stream: "orders")
179
+ begin
180
+ psub.fetch(5, group: "workers").each(&:ack)
181
+ rescue NATS::JetStream::Error::PinIdMismatch
182
+ # This subscription is no longer pinned; the next fetch may be pinned again.
183
+ rescue NATS::IO::Timeout
184
+ # Nothing to do, or another subscription is pinned.
185
+ end
186
+ ```
187
+
105
188
  ## Service API
106
189
 
107
190
  The service API allows you to easily [build NATS services](docs/service_api.md).
@@ -152,22 +235,70 @@ end
152
235
 
153
236
  ## TLS
154
237
 
155
- It is possible to setup a custom TLS connection to NATS by passing
156
- an [OpenSSL](http://ruby-doc.org/stdlib-2.3.2/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html) context to the client to be used on connect:
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:
157
242
 
158
243
  ```ruby
159
244
  tls_context = OpenSSL::SSL::SSLContext.new
160
- tls_context.ssl_version = :TLSv1_2
161
-
162
- NATS.connect({
163
- servers: ['tls://127.0.0.1:4444'],
164
- reconnect: false,
165
- tls: {
166
- context: tls_context
167
- }
168
- })
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})
169
254
  ```
170
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
+
171
302
  ## WebSocket
172
303
 
173
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).