[!WARNING]
This repository has been archived and is no longer maintained. No further issues, pull requests, or updates will be accepted.
NATS REST Configuration Proxy
The NATS Server ACL configuration proxy provides a secure REST interface
for modifying access control lists (ACLs), identities (users), and
passwords. This proxy is designed to facilitate the development of command
line tools and/or user interfaces to remotely update a NATS server
configuration.
Only identities and permissions are supported at this time.
Getting started
go get -u github.com/nats-io/nats-rest-config-proxy
Usage
Usage: nats-rest-config-proxy [options...]
Server Options:
-a, --addr <host> Bind to host address (default: 0.0.0.0)
-p, --port <port> Use port for clients (default: 4567)
-d, --dir <directory> Directory for storing data
-c, --config <file> Configuration file
-f, --publish-script <file> Path to an optional script to execute on publish
Logging Options:
-l, --log <file> File to redirect log output
-D, --debug Enable debugging output
-V, --trace Enable trace logging
-DV Debug and trace
TLS Options:
--cert <file> Server certificate file
--key <file> Private key for server certificate
--cacert <file> Client certificate CA for verification
Common Options:
-h, --help Show this message
-v, --version Show version
Configuration file
The NATS REST ACL Proxy supports a configuration file. Authorization based
on the subject attributes of a client certificate is also supported.
The NATS REST Configuration proxy operates using a data directory a
configuration file, and a publish script.
The process is straightforward:
Launch the NATS REST Configuration proxy and specify the Authorization
configuration file you’d like to modify.
Use the REST API to modify users and permissions.
Take a snapshot. This saves the current work in the data directory.
Invoke the publish command to copy a snapshot into the configuration
file and invoke the optional publish script.
Why a script
A script is used for versatility. For some, this could be used as
a step in a github devops flow and the script creates a PR with the new configuration
for human eyes to review. For others, the updated file is copied to remote nodes and
then NATS servers are reloaded with remote commands, e.g. ssh -t gnatsd -sl reload.
One could even work on an included NATS server file directly, with changes to be picked
up nightly. There are many options.
Developing
# Build locally using Go modules
$ GO111MODULE=on go run main.go
[41405] 2019/02/11 16:18:52.713366 [INF] Starting nats-rest-config-proxy v0.0.1
[41405] 2019/02/11 16:18:52.713804 [INF] Listening on 0.0.0.0:4567
# To run the tests
$ go test ./... -v
Note: To test locally, you’ll need to add a hostname into your /etc/hosts file:
127.0.0.1 nats-cluster.default.svc.cluster.local
REST API
The NATS configuration proxy will return the following error codes:
200 OK - success
400 Bad Request - Invalid API request
404 Not Found - resource was not found
405 Method Not Allowed - unsupported operation
409 Conflict - the operation cannot be completed as a dependency will
create an invalid configuration.
This configuration can now be included by a nats-server. Note that in order to enable checking permissions based on a TLS certificate, it is needed to set verify_and_map= to true in the tls config:
nats-server -c nats.conf -DV
[6342] 2019/06/18 18:04:38.899054 [INF] Starting nats-server version 2.0.0
[6342] 2019/06/18 18:04:38.899177 [DBG] Go build version go1.12
[6342] 2019/06/18 18:04:38.899557 [INF] Listening for client connections on 0.0.0.0:4222
[6342] 2019/06/18 18:04:38.899570 [INF] TLS required for client connections
[6342] 2019/06/18 18:04:38.899578 [INF] Server id is NCFA6C5OC45PKJOISSDCWBEDQ4YMKOH57WHCWLL6EZ2Y723WAAIUHPJI
[6342] 2019/06/18 18:04:38.899584 [INF] Server is ready
Now if the following app tries to connect and publish to a subject without permissions it won’t be able to:
Let’s create some accounts. In this example, the account Foo will export a stream and a service that account Bar will be able to import using a different prefix and subject:
Messages published to foo will be mapped to foo.mapped and messages
published to bar will be distributed to bar.mapped.1 and bar.mapped.2
roughly equally.
Account subject mappings are maintained alongside import and exports, so
be sure to include existing imports and exports as you add, change, or remove
subject mappings.
Creating Snapshots
We now can create a named snapshot for this setup. Let’s create one named v1:
curl -X POST http://127.0.0.1:4567/v2/auth/snapshot?name=v1
Then publish the configuration:
curl -X POST http://127.0.0.1:4567/v2/auth/publish?name=v1
At this point, we will have the following directory structure in the config directory:
And the published auth configuration will look like:
$ cat config/current/accounts/auth.conf
accounts {
Bar { include 'Bar.json' }
Foo { include 'Foo.json' }
}
This configuration can now be included by a nats-server in order to define a
couple of variables that can be used as follow:
include "config/current/accounts/auth.conf"
Validation tool
Release v0.4.0
also now includes a nats-rest-config-validator tool
which can be used to verify whether the resources are in a valid state
and otherwise report the error.
nats-rest-config-validator -h
Usage: nats-rest-config-validator [options...]
Options:
-d, --dir <directory> Directory for storing data (default is the current directory.)
-h, --help Show this message
-v, --version Show version
For example given the following directory structure:
Running the tool would build the config and show on which account the error exists:
$ nats-rest-config-validator -d data
Error: On /bar.json : {
"users": [
{
"username": "user2",
"password": "user2",
"permissions": {
"publish": {
"allow": [
"foo.*"
]
},
"subscribe": {
"deny": [
"",
^^^ subject "" is not a valid subject
Snapshot/Publishing tool
Release v0.5.0
includes a couple of tools to create and publish snapshots without having to start the server,
the nats-rest-config-snapshot and nats-rest-config-publish tools.
For example, first we can create a snapshot:
$ nats-rest-config-snapshot -d data --snapshot my-snapshot
Taking "my-snapshot" snapshot...
OK
And then publish it as well:
$ nats-rest-config-publish -d data --snapshot my-snapshot
Publishing "my-snapshot" snapshot
OK
By default in case no snapshot name was given, the tool will publish the latest configuration:
$ nats-rest-config-publish -d data
Taking "latest" snapshot...
Publishing "latest" snapshot
OK
Our sponsor for this project
Many thanks to MasterCard for sponsoring this project.
We appreciate MasterCard’s support of NATS, CNCF, and the OSS community.
License
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.
NATS REST Configuration Proxy
The NATS Server ACL configuration proxy provides a secure REST interface for modifying access control lists (ACLs), identities (users), and passwords. This proxy is designed to facilitate the development of command line tools and/or user interfaces to remotely update a NATS server configuration.
Only identities and permissions are supported at this time.
Getting started
Usage
Configuration file
The NATS REST ACL Proxy supports a configuration file. Authorization based on the subject attributes of a client certificate is also supported.
How it works
The NATS REST Configuration proxy operates using a data directory a configuration file, and a publish script.
The process is straightforward:
Why a script
A script is used for versatility. For some, this could be used as a step in a github devops flow and the script creates a PR with the new configuration for human eyes to review. For others, the updated file is copied to remote nodes and then NATS servers are reloaded with remote commands, e.g.
ssh -t gnatsd -sl reload. One could even work on an included NATS server file directly, with changes to be picked up nightly. There are many options.Developing
Note: To test locally, you’ll need to add a hostname into your
/etc/hostsfile:127.0.0.1 nats-cluster.default.svc.cluster.localREST API
The NATS configuration proxy will return the following error codes:
Identity Add/Update Payload
NKEY:
Certificate subject attributes with permissions:
Permission add/update payload
Commands
In addition to
/v1/auth/snapshot, there is also/v2/auth/snapshotwhich is documented below in the v2.0 Accounts section.Examples
Create/update a permission
Plain permissions.
Queue group permissions are supported as well. Here
bar.>is the subject andfizzgroupis the queue group.Get a permission
Create/update a user
Get a user
Create/update an account
Create/update the global jetstream configuration
Create/update an account with jetstream support
Create an account with JetStream support enabled with 10GB file storage and 1GB memory, as well as infinite streams and consumers.
Note that in order to use JetStream you need enable it outside of the auth configuration, for example after publishing.
Get an account
Delete an account
Build snapshot
Publish snapshot
Usage Walkthrough
In this example, we will create a couple of users with different permissions:
First we will start the server, and use the
-dflag to setup the directory that will contain the users that were created via the proxy:Next, let’s create the permissions for both
guestandadminusers:Now that we have created the permissions, let’s bind some users to these permissions:
We now can create a named snapshot for this setup. Let’s create one named
v1:Then publish the configuration:
At this point, we will have the following directory structure in the config directory:
And the published auth configuration will look like:
This configuration can now be included by a
nats-server. Note that in order to enable checking permissions based on a TLS certificate, it is needed to setverify_and_map=totruein thetlsconfig:Starting the NATS Server with the configuration:
Now if the following app tries to connect and publish to a subject without permissions it won’t be able to:
Example logs from the server:
Using NATS v2.0 Accounts
In this example, we will create a couple of users on different accounts.
Start the server using its own data directory:
Next, let’s create the permissions for both
guestandadminusers:Let’s create some accounts. In this example, the account
Foowill export a stream and a service that accountBarwill be able to import using a different prefix and subject:Now that we have created the permissions, let’s bind some users to these permissions:
Account based Subject Mapping
You can also apply subject mappings to an account.
Add a user:
Messages published to
foowill be mapped tofoo.mappedand messages published tobarwill be distributed tobar.mapped.1andbar.mapped.2roughly equally.Account subject mappings are maintained alongside import and exports, so be sure to include existing imports and exports as you add, change, or remove subject mappings.
Creating Snapshots
We now can create a named snapshot for this setup. Let’s create one named
v1:Then publish the configuration:
At this point, we will have the following directory structure in the config directory:
And the published auth configuration will look like:
This configuration can now be included by a
nats-serverin order to define a couple of variables that can be used as follow:Validation tool
Release v0.4.0 also now includes a
nats-rest-config-validatortool which can be used to verify whether theresourcesare in a valid state and otherwise report the error.For example given the following directory structure:
Where one of the defined permissions has an invalid subject:
Running the tool would build the config and show on which account the error exists:
Snapshot/Publishing tool
Release v0.5.0 includes a couple of tools to create and publish snapshots without having to start the server, the
nats-rest-config-snapshotandnats-rest-config-publishtools.For example, first we can create a snapshot:
And then publish it as well:
By default in case no snapshot name was given, the tool will publish the latest configuration:
Our sponsor for this project
Many thanks to MasterCard for sponsoring this project. We appreciate MasterCard’s support of NATS, CNCF, and the OSS community.
License
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.