Commit Graph

28 Commits

Author SHA1 Message Date
Ivan Kozlovic
1dba6418ed [ADDED] MQTT Support
This PR introduces native support for MQTT clients. It requires use
of accounts with JetStream enabled. Since as of now clustering is
not available, MQTT will be limited to single instance.

Only QoS 0 and 1 are supported at the moment. MQTT clients can
exchange messages with NATS clients and vice-versa.

Since JetStream is required, accounts with JetStream enabled must
exist in order for an MQTT client to connect to the NATS Server.
The administrator can limit the users that can use MQTT with the
allowed_connection_types option in the user section. For instance:
```
accounts {
  mqtt {
    users [
      {user: all, password: pwd, allowed_connection_types: ["STANDARD", "WEBSOCKET", "MQTT"]}
      {user: mqtt_only, password: pwd, allowed_connection_types: "MQTT"}
    ]
    jetstream: enabled
  }
}
```
The "mqtt_only" can only be used for MQTT connections, which the user
"all" accepts standard, websocket and MQTT clients.

Here is what a configuration to enable MQTT looks like:
```
mqtt {
  # Specify a host and port to listen for websocket connections
  #
  # listen: "host:port"

  # It can also be configured with individual parameters,
  # namely host and port.
  #
  # host: "hostname"
  port: 1883

  # TLS configuration section
  #
  # tls {
  #  cert_file: "/path/to/cert.pem"
  #  key_file: "/path/to/key.pem"
  #  ca_file: "/path/to/ca.pem"
  #
  #  # Time allowed for the TLS handshake to complete
  #  timeout: 2.0
  #
  #  # Takes the user name from the certificate
  #  #
  #  # verify_an_map: true
  #}

  # Authentication override. Here are possible options.
  #
  # authorization {
  #   # Simple username/password
  #   #
  #   user: "some_user_name"
  #   password: "some_password"
  #
  #   # Token. The server will check the MQTT's password in the connect
  #   # protocol against this token.
  #   #
  #   # token: "some_token"
  #
  #   # Time allowed for the client to send the MQTT connect protocol
  #   # after the TCP connection is established.
  #   #
  #   timeout: 2.0
  #}

  # If an MQTT client connects and does not provide a username/password and
  # this option is set, the server will use this client (and therefore account).
  #
  # no_auth_user: "some_user_name"

  # This is the time after which the server will redeliver a QoS 1 message
  # sent to a subscription that has not acknowledged (PUBACK) the message.
  # The default is 30 seconds.
  #
  # ack_wait: "1m"

  # This limits the number of QoS1 messages sent to a session without receiving
  # acknowledgement (PUBACK) from that session. MQTT specification defines
  # a packet identifier as an unsigned int 16, which means that the maximum
  # value is 65535. The default value is 1024.
  #
  # max_ack_pending: 100
}
```

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2020-11-30 20:08:44 -07:00
Matthias Hanel
8fd1b66f66 Renaming to verify_cert_and_check_known_urls
Signed-off-by: Matthias Hanel <mh@synadia.com>
2020-11-20 16:56:44 -05:00
Matthias Hanel
eda80ff7b5 changing the option name verify_and_implicit_allow to verify_and_accept_known_urls
This follows the suggestion by phil. I added the and to be similar to verify_and_map.
I fixed a minor issue where the implicit verify could be overwriting an
explicitly configured one.

Signed-off-by: Matthias Hanel <mh@synadia.com>
2020-11-20 14:27:24 -05:00
Matthias Hanel
74642e024e [Added] verify_and_implicit_allow to tie subject alt name to url in cfg
Only works for gateways and routes. When true the subject alt DNS name
must match one url in the corresponding configuration

Signed-off-by: Matthias Hanel <mh@synadia.com>
2020-11-20 13:57:34 -05:00
Derek Collison
511cdaa5ac Do not report bad latency on auto-unsubscribe triggers
Signed-off-by: Derek Collison <derek@nats.io>
2020-10-22 05:41:33 -07:00
Ivan Kozlovic
e84f1cf52c Added test with unknown JWT allowed connection type
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2020-09-17 18:17:19 -06:00
Ivan Kozlovic
648346f857 Add some tests for config parsing
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2020-09-16 19:17:12 -06:00
Ivan Kozlovic
7ccbaca782 Added an allowed connection type filter for users
Users and NKey users will now have the option to specify a list
of allowed connection types.

This will allow for instance a certain user to be allowed to
connect as a standard NATS client, but not as Websocket, or
vice-versa.

This also fixes the websocket auth override. Indeed, with
the original behavior, the websocket users would have been bound
to $G, which would not work when there are accounts defined, since
when that is the case, no app can connect/bind to $G account.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2020-09-16 18:22:44 -06:00
Waldemar Quevedo
de98a04aa2 Update config check tests
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2020-09-09 04:46:52 -07:00
Waldemar Quevedo
3161cb1693 Add more config checks for leafnode remotes
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2020-09-04 00:34:40 -07:00
Ivan Kozlovic
cd6d71deaa [ADDED] lame_duck_grace_period option
The grace period used to be hardcoded at 10 seconds.
This option allows the user to configure the amount of time the
server will wait before initiating the closing of client connections.

Note that the grace period needs to be strictly lower than the overall
lame_duck_duration. The server deducts the grace period from that
overall duration and spreads the closing of connections during
that time.
For instance, if there are 1000 connections and the lame duck
duration is set to 30 seconds and grace period to 10, then
the server will use 30-10 = 20 seconds to spread the closing
of those 1000 connections, so say roughly 50 clients per second.

Resolves #1459.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2020-06-08 11:43:25 -06:00
Ivan Kozlovic
63138509f7 Tune some code/test for Windows
Running test suite on a Windows VM, I notice several failures.
Updated the compute of the RTT to be at least 1ns. I think that
this is just an issue with the VM I am running, but that change
will have no impact for normal situations (since setting the rtt
to the very minimum duration (1ns) instead of 0) and will prevent
some tests from failing.

Because of those same timer granularity issues, I had to add some
delays between some actions in order for time.Sub()/Since() to
actually report something more than 0.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2019-11-21 14:32:46 -07:00
Ivan Kozlovic
07bf4a499e Issue with multiple users in Leafnode authorization
This was introduced in master #1147, not in any public release.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2019-10-29 13:34:30 -06:00
Ivan Kozlovic
18a1702ba2 [ADDED] Basic auth for leafnodes
Added a way to specify which account an accepted leafnode connection
should be bound to when using simple auth (user/password).

Singleton:
```
leafnodes {
  port: ...
  authorization {
    user: leaf
    password: secret
    account: TheAccount
  }
}
```
With above configuration, if a soliciting server creates a LN connection
with url: `nats://leaf:secret@host:port`, then the accepting server
will bind the leafnode connection to the account "TheAccount". This account
need to exist otherwise the connection will be rejected.

Multi:
```
leafnodes {
  port: ...
  authorization {
    users = [
      {user: leaf1, password: secret, account: account1}
      {user: leaf2, password: secret, account: account2}
    ]
  }
}
```
With the above, if a server connects using `leaf1:secret@host:port`, then
the accepting server will bind the connection to account `account1`.

If user/password (either singleton or multi) is defined, then the connecting
server MUST provide the proper credentials otherwise the connection will
be rejected.

If no user/password info is provided, it is still possible to provide the
account the connection should be associated with:
```
leafnodes {
  port: ...
  authorization {
    account: TheAccount
  }
}
```
With the above, a connection without credentials will be bound to the
account "TheAccount".

If credentials are used (jwt, nkey or other), then the server will attempt
to authenticate and if successful associate to the account for that specific
user. If the user authentication fails (wrong password, no such user, etc..)
the connection will be also rejected.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2019-09-30 19:42:11 -06:00
Ivan Kozlovic
3600accb79 [FIXED] Reject duplicate service import "to" subject
Report error from configuration parsing, and also return error
in AddServiceImport() (and its variants).

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2019-09-20 16:30:13 -06:00
Jaime Piña
ab24cddc06 Add latency config
Currently, the config file doesn't recognize the latency config block in
account exports. This change exposes those settings in the config file.

Signed-off-by: Jaime Piña <jaime@synadia.com>
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2019-09-18 13:20:26 -07:00
Waldemar Quevedo
8147adc1b0 Add support to extend leafnodes remote tls timeout
Bump default TLS timeout for leafnode connections

Add checks for when cert_file or key_file are missing in TLS config

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2019-06-14 08:04:44 -07:00
Ivan Kozlovic
7449e9ac53 Replace megacheck with staticcheck
Fixed issues reported by staticcheck

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2019-01-09 14:14:47 -07:00
Derek Collison
7978d8e667 Support for mapping user from TLS client certificate
Signed-off-by: Derek Collison <derek@nats.io>
2018-12-20 07:51:43 -08:00
Waldemar Quevedo
61d1ec53fa Allow conf vars within account block
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-11-02 14:42:29 -07:00
Ivan Kozlovic
0067c3bb04 Added support for lame duck mode
When receiving SIGUSR2 signal (or -sl ldm) the server stops
accepting new clients, closes routes connections and spread the
closing of client connections based on a config lame duck duration
(default is 30sec). This will help preventing a storm of client
reconnect when a server needs to be shutdown.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
2018-10-19 19:07:37 -06:00
Waldemar Quevedo
e819f15ca4 Collecting all config errors then flush before exit
Show warnings on server startup

Removes notions around `pedantic` checks and instead
just reports the warnings in case there were any, or
the collection of errors that may have been found
in the configuration file.

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-10-16 13:29:29 -07:00
Waldemar Quevedo
ed1af40996 Add source to config errors
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-10-16 13:29:29 -07:00
Waldemar Quevedo
fdd1536a8a Always parse config file with checks enabled
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-10-16 13:29:29 -07:00
Waldemar Quevedo
18a62cdb60 Change style of reporting errors with line and pos
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-10-03 14:52:57 -07:00
Waldemar Quevedo
181b07ebc1 Config reporting with line and error position
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-10-02 11:38:15 -07:00
Waldemar Quevedo
255be72df8 Report warnings when using deprecated fields
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-09-11 17:50:02 -07:00
Waldemar Quevedo
df2364af26 Add -t pedantic config check to the server
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
2018-09-11 16:06:53 -07:00