Accounts
Accounts expand on the authentication foundation. With traditional authentication (except for JWT authentication), all clients can publish and subscribe to anything unless explicitly configured otherwise. To protect clients and information, you have to carve the subject space and permission clients carefully.
Accounts allow the grouping of clients, isolating them from clients in other accounts, thus enabling multi-tenancy in the server. With accounts, the subject space is not globally shared, greatly simplifying the messaging environment. Instead of devising complicated subject name carving patterns, clients can use short subjects without explicit authorization rules.
Accounts configuration is done in accounts map. The contents of an account entry includes:
| Property | Description |
|---|---|
users |
a list of user configuration maps |
exports |
a list of export maps |
imports |
a list of import maps |
The accounts list is a map, where the keys on the map are an account name.
accounts: {
A: {
users: [
{user: a, password: a}
]
},
B: {
users: [
{user: b, password: b}
]
},
}
In the most straightforward configuration above you have an account named
Awhich has a single user identified by the usernameaand the passworda, and an account namedBwith a user identified by the usernameband the passwordb.These two accounts are isolated from each other. Messages published by users in
Aare not visible to users inB.The user configuration map is the same as any other NATS user configuration map. You can use:
- username/password
- nkeys
- and add permissions
While the name account implies one or more users, it is much simpler and enlightening to think of one account as a messaging container for one application. Users in the account are simply the minimum number of services that must work together to provide some functionality. In simpler terms, more accounts with few (even one) clients is a better design topology than a large account with many users with complex authorization configuration.
Exporting and Importing
Messaging exchange between different accounts is enabled by exporting streams and services from one account and importing them into another. Each account controls what is exported and imported.
The exports configuration list enable you to define the services and streams that others can import. Services and streams are expressed as an Export configuration map.
Streams
Streams are messages your application publishes. Importing applications won't be able to make requests from your applications but will be able to consume messages you generate.
Services
Services are messages your application can consume and act on, enabling other accounts to make requests that are fulfilled by your account.
Export Configuration Map
The export configuration map binds a subject for use as a service or stream and optionally defines specific accounts that can import the stream or service. Here are the supported configuration properties:
| Property | Description |
|---|---|
stream |
A subject or subject with wildcards that the account will publish. (exclusive of service) |
service |
A subject or subject with wildcards that the account will subscribe to. (exclusive of stream) |
accounts |
A list of account names that can import the stream or service. If not specified, the service or stream is public and any account can import it. |
Here are some example exports:
accounts: {
A: {
users: [
{user: a, password: a}
]
exports: [
{stream: puba.>}
{service: pubq.>}
{stream: b.>, accounts: [B]}
{service: q.b, accounts: [B]}
]
}
...
}
Here's what A is exporting:
- a public stream on the wildcard subject
puba.> - a public service on the wildcard subject
pubq.> - a stream to account
Bon the wildcard subjecta.> - a service to account
Bon the subjectq.b
Source Configuration Map
The source configuration map describes an export from a remote account by specifying the account and subject of the export being imported. This map is embedded in the import configuration map:
| Property | Description |
|---|---|
account |
Account name owning the export. |
subject |
The subject under which the stream or service is made accessible to the importing account |
Import Configuration Map
An import enables an account to consume streams published by another account or make requests to services implemented by another account. All imports require a corresponding export on the exporting account. Accounts cannot do self-imports.
| Property | Description |
|---|---|
stream |
Stream import source configuration. (exclusive of service) |
service |
Service import source configuration (exclusive of stream) |
prefix |
A local subject prefix mapping for the imported stream. |
to |
A local subject mapping for imported service. |
The prefix and to options allow you to remap the subject that is used locally to receive stream messages or publish service requests.
accounts: {
A: {
users: [
{user: a, password: a}
]
exports: [
{stream: puba.>}
{service: pubq.>}
{stream: b.>, accounts: [B]}
{service: q.b, accounts: [B]}
]
},
B: {
users: [
{user: b, password: b}
]
imports: [
{stream: {account: A, subject: b.>}}
{service: {account: A, subject: q.b}}
]
}
C: {
users: [
{user: c, password: c}
]
imports: [
{stream: {account: A, subject: puba.>}, prefix: from_a}
{service: {account: A, subject: pubq.C}, to: Q}
]
}
}
Account B imports:
- the private stream from
Athat onlyBcan receive onb.> - the private service from
Athat onlyBcan send requests onq.b
Account C imports the public service and stream from A, but also:
- remaps the
puba.>stream to be locally available underfrom_a.puba.>. The messages will have their original subjects prefixed byfrom_a. - remaps the
pubq.Cservice to be locally available underQ. AccountConly needs to publish toQlocally.
It is important to reiterate that:
- stream
puba.>fromAis visible to all external accounts that imports the stream. - service
pubq.>fromAis available to all external accounts so long as they know the full subject of where to send the request. Typically an account will export a wildcard service but then coordinate with a client account on specific subjects where requests will be answered. On our example, accountCaccess the service onpubq.C(but has mapped it for simplicity toQ). - stream
b.>is private, only accountBcan receive messages from the stream. - service
q.bis private; only accountBcan send requests to the service. - When
Cpublishes a request toQ, localCclients will seeQmessages. However, the server will remapQtopubq.Cand forward the requests to accountA.