In modern microservice architecture it is common to share infrastructure - such as NATS - between services. Accounts are securely isolated communication contexts that allow multi-tenancy in a NATS deployment. They allow users to bifurcate technology from business driven use cases, where data silos are created by design, not software limitations. Furthermore, they facilitate the controlled exchange of information between those data silos/Tenants/Accounts.
Accounts expand on the authorization foundation. With traditional authorization, 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. System Events are an example of this isolation at work.
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.
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.
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.
{% hint style="info" %}
The term stream in the context of import and export account configuration does not refer to and should not be confused with a JetStream stream (unfortunate collision of terms as the import/export between accounts predates JetStream), it is just a 'stream of (Core NATS) messages'
{% endhint %}
The exports configuration list enable you to define the services and streams that others can import. Exported services and streams are expressed as an Export configuration map. The imports configuration lists the services and streams that an Account imports. Imported services and streams are expressed as an Import 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. |
response_type |
Indicates if a response to a service request consists of a single or a stream of messages. Possible values are: single or stream. (Default value is singleton) |
Here are some example exports:
Here's what A is exporting:
puba.>pubq.>B on the wildcard subject b.>B on the subject q.bAn 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. (applicable to stream) |
to |
A local subject mapping for imported service. (applicable to service) |
The prefix and to options are optional and allow you to remap the subject that is used locally to receive stream messages from or publish service requests to. This way the importing account does not depend on naming conventions picked by another. Currently, a service import can not make use of wildcards, which is why the import subject can be rewritten. A stream import may make use of wildcards. To retain information contained in the subject, it can thus only be prefixed with prefix...
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 |
Account B imports:
A that only B can receive on b.>A that only B can send requests on q.bAccount C imports the public service and stream from A, but also:
puba.> stream to be locally available under from_a.puba.>. The messages will have their original subjects prefixed by from_a.pubq.C service to be locally available under Q. Account C only needs to publish to Q locally.It is important to reiterate that:
puba.> from A is visible to all external accounts that imports the stream.pubq.> from A is 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, account C access the service on pubq.C (but has mapped it for simplicity to Q).b.> is private, only account B can receive messages from the stream.q.b is private; only account B can send requests to the service.C publishes a request to Q, local C clients will see Q messages. However, the server will remap Q to pubq.C and forward the requests to account A.Clients connecting without authentication can be associated with a particular user within an account.
The above example shows how clients without authentication can be associated with the user a within account A.
Please note that the
no_auth_userwill not work with nkeys or bcrypted passwords. The user referenced can also be part of the authorization block.Despite
no_auth_userbeing set, clients still need to communicate that they will not be using credentials. The authentication timeout applies to this process as well. When your connection is slow, you may run into this timeout and the resultingAuthentication Timeouterror, despite not providing credentials.
It is possible to import/export messages stored in JetStream streams between accounts. While it is possible to allow a client application in one account to access a stream located in another account, in most use cases people want a setup where a stream in one account is mirrored or sourced in another account (and the applications in that other account simply use that mirrored/sourced stream in their account), as this is a more 'locked-down' way to share messages in streams between accounts, compared to letting the client applications directly use a stream in another account.
There are two resources documenting and giving examples of how to do this: