System Events & Decentralized JWT Tutorial

Enabling System Events with Decentralized Authentication/Authorization

To enable and access system events, you'll have to:
  • Create an Operator, Account and User
  • Run a NATS Account Server (or Memory Resolver)

Create an Operator, Account, User

Let's create an operator, system account and system account user:
1
nsc add operator -n SAOP
Copied!
Output
1
Generated operator key - private key stored "~/.nkeys/SAOP/SAOP.nk"
2
Success! - added operator "SAOP"
Copied!
Add the system account
1
nsc add account -n SYS
Copied!
Output
1
Generated account key - private key stored "~/.nkeys/SAOP/accounts/SYS/SYS.nk"
2
Success! - added account "SYS"
Copied!
Add a system account user
1
nsc add user -n SYSU
Copied!
Output
1
Generated user key - private key stored "~/.nkeys/SAOP/accounts/SYS/users/SYSU.nk"
2
Generated user creds file "~/.nkeys/SAOP/accounts/SYS/users/SYSU.creds"
3
Success! - added user "SYSU" to "SYS"
Copied!
By default, the operator JWT can be found in ~/.nsc/nats/<operator_name>/<operator.name>.jwt.

NATS-Account-Server

To vend the credentials to the nats-server, we'll use a nats-account-server. Let's start a nats-account-server to serve the JWT credentials:
1
nats-account-server -nsc ~/.nsc/nats/SAOP
Copied!
The server will by default vend JWT configurations on the an endpoint at: http(s)://<server_url>/jwt/v1/accounts/.

NATS Server Configuration

The server configuration will need:
  • The operator JWT - (~/.nsc/nats/<operator_name>/<operator.name>.jwt)
  • The URL where the server can resolve accounts (http://localhost:9090/jwt/v1/accounts/)
  • The public key of the system_account
The only thing we don't have handy is the public key for the system account. We can get it easy enough:
1
nsc list accounts
Copied!
Output
1
╭─────────────────────────────────────────────────────────────────╮
2
│ Accounts │
3
├──────┬──────────────────────────────────────────────────────────┤
4
│ Name │ Public Key │
5
├──────┼──────────────────────────────────────────────────────────┤
6
│ SYS │ ADWJVSUSEVC2GHL5GRATN2LOEOQOY2E6Z2VXNU3JEIK6BDGPWNIW3AXF │
7
╰──────┴──────────────────────────────────────────────────────────╯
Copied!
Because the server has additional resolver implementations, you need to enclose the server url like: URL(<url>).
Let's create server config with the following contents and save it to server.conf:
1
operator: /Users/synadia/.nsc/nats/SAOP/SAOP.jwt
2
system_account: ADWJVSUSEVC2GHL5GRATN2LOEOQOY2E6Z2VXNU3JEIK6BDGPWNIW3AXF
3
resolver: URL(http://localhost:9090/jwt/v1/accounts/)
Copied!
Let's start the nats-server:
1
nats-server -c server.conf
Copied!

Inspecting Server Events

Let's add a subscriber for all the events published by the system account:
1
nats sub --creds ~/.nkeys/SAOP/accounts/SYS/users/SYSU.creds ">"
Copied!
Very quickly we'll start seeing messages from the server as they are published by the NATS server. As should be expected, the messages are just JSON, so they can easily be inspected even if just using a simple nats sub to read them.
To see an account update:
1
nats pub --creds ~/.nkeys/SAOP/accounts/SYS/users/SYSU.creds foo bar
Copied!
The subscriber will print the connect and disconnect:
1
"server": {
2
"host": "0.0.0.0",
3
"id": "NBTGVY3OKDKEAJPUXRHZLKBCRH3LWCKZ6ZXTAJRS2RMYN3PMDRMUZWPR",
4
"ver": "2.0.0-RC5",
5
"seq": 32,
6
"time": "2019-05-03T14:53:15.455266-05:00"
7
},
8
"acc": "ADWJVSUSEVC2GHL5GRATN2LOEOQOY2E6Z2VXNU3JEIK6BDGPWNIW3AXF",
9
"conns": 1,
10
"total_conns": 1
11
}'
12
"server": {
13
"host": "0.0.0.0",
14
"id": "NBTGVY3OKDKEAJPUXRHZLKBCRH3LWCKZ6ZXTAJRS2RMYN3PMDRMUZWPR",
15
"ver": "2.0.0-RC5",
16
"seq": 33,
17
"time": "2019-05-03T14:53:15.455304-05:00"
18
},
19
"client": {
20
"start": "2019-05-03T14:53:15.453824-05:00",
21
"host": "127.0.0.1",
22
"id": 6,
23
"acc": "ADWJVSUSEVC2GHL5GRATN2LOEOQOY2E6Z2VXNU3JEIK6BDGPWNIW3AXF",
24
"user": "UACPEXCAZEYWZK4O52MEGWGK4BH3OSGYM3P3C3F3LF2NGNZUS24IVG36",
25
"name": "NATS Sample Publisher",
26
"lang": "go",
27
"ver": "1.7.0",
28
"stop": "2019-05-03T14:53:15.45526-05:00"
29
},
30
"sent": {
31
"msgs": 1,
32
"bytes": 3
33
},
34
"received": {
35
"msgs": 0,
36
"bytes": 0
37
},
38
"reason": "Client Closed"
39
}'
Copied!

System Services

$SYS.REQ.SERVER.PING - Discovering Servers

To discover servers in the cluster, and get a small heath summary, publish a request to $SYS.REQ.SERVER.PING. Note that while the example below uses nats-req, only the first answer for the request will be printed. You can easily modify the example to wait until no additional responses are received for a specific amount of time, thus allowing for all responses to be collected.
1
nats request --creds ~/.nkeys/SAOP/accounts/SYS/users/SYSU.creds \$SYS.REQ.SERVER.PING ""
Copied!
Output
1
Published [$SYS.REQ.SERVER.PING] : ''
2
Received [_INBOX.G5mbsf0k7l7nb4eWHa7GTT.omklmvnm] : '{
3
"server": {
4
"host": "0.0.0.0",
5
"id": "NCZQDUX77OSSTGN2ESEOCP4X7GISMARX3H4DBGZBY34VLAI4TQEPK6P6",
6
"ver": "2.0.0-RC9",
7
"seq": 47,
8
"time": "2019-05-02T14:02:46.402166-05:00"
9
},
10
"statsz": {
11
"start": "2019-05-02T13:41:01.113179-05:00",
12
"mem": 12922880,
13
"cores": 20,
14
"cpu": 0,
15
"connections": 2,
16
"total_connections": 2,
17
"active_accounts": 1,
18
"subscriptions": 10,
19
"sent": {
20
"msgs": 7,
21
"bytes": 2761
22
},
23
"received": {
24
"msgs": 0,
25
"bytes": 0
26
},
27
"slow_consumers": 0
28
}
29
}'
Copied!

$SYS.SERVER.<id>.STATSZ - Requesting Server Stats Summary

If you know the server id for a particular server (such as from a response to $SYS.REQ.SERVER.PING), you can query the specific server for its health information:
1
nats request --creds ~/.nkeys/SAOP/accounts/SYS/users/SYSU.creds \$SYS.REQ.SERVER.NC7AKPQRC6CIZGWRJOTVFIGVSL7VW7WXTQCTUJFNG7HTCMCKQTGE5PUL.STATSZ ""
Copied!
Output
1
Published [$SYS.REQ.SERVER.NC7AKPQRC6CIZGWRJOTVFIGVSL7VW7WXTQCTUJFNG7HTCMCKQTGE5PUL.STATSZ] : ''
2
Received [_INBOX.DQD44ugVt0O4Ur3pWIOOD1.WQOBevoq] : '{
3
"server": {
4
"host": "0.0.0.0",
5
"id": "NC7AKPQRC6CIZGWRJOTVFIGVSL7VW7WXTQCTUJFNG7HTCMCKQTGE5PUL",
6
"ver": "2.0.0-RC5",
7
"seq": 25,
8
"time": "2019-05-03T14:34:02.066077-05:00"
9
},
10
"statsz": {
11
"start": "2019-05-03T14:32:19.969037-05:00",
12
"mem": 11874304,
13
"cores": 20,
14
"cpu": 0,
15
"connections": 2,
16
"total_connections": 4,
17
"active_accounts": 1,
18
"subscriptions": 10,
19
"sent": {
20
"msgs": 26,
21
"bytes": 9096
22
},
23
"received": {
24
"msgs": 2,
25
"bytes": 0
26
},
27
"slow_consumers": 0
28
}
29
}'
Copied!
Last modified 1mo ago