Story 04 · The auth plane
Let a team declare its own users and streams
Contents
This guide shows you how to let a team declare NATS users and streams in its own namespace, in an account that the platform team owns in nats-system.
The team needs no access to nats-system.
The manifests give the payments team the account payments and the namespace payments.
Before you begin#
You need:
- The NATS operator
demo, its NATS clusterdemoand the auth controller’s connection from Put a NATS cluster under a NATS operator. - The namespaces
paymentsandorders.
Declare the account and the grant#
As the platform team, apply the account payments and a NatsReferenceGrant in nats-system.
The grant lets a NatsUser in the namespace payments refer to the account payments, and to nothing else in nats-system.
apiVersion: auth.nats-operator.io/v1beta1
kind: NatsAccount
metadata:
name: payments
namespace: nats-system
spec:
operatorRef:
name: demo
limits:
connections: 200
jetstream:
memoryStorage: 512Mi
diskStorage: 100Gi
streams: 10
consumers: 100
---
apiVersion: nats-operator.io/v1beta1
kind: NatsReferenceGrant
metadata:
name: payments-users
namespace: nats-system
spec:
from:
- group: auth.nats-operator.io
kind: NatsUser
namespace: payments
to:
- group: auth.nats-operator.io
kind: NatsAccount
name: payments
kubectl apply -f https://nats-operator.io/docs/stories/04-team-self-service/01-platform.yamlTo admit every NatsAccount in nats-system, leave out name under to.
NatsReferenceGrantSpec in the API reference lists the fields.
Before you grant a namespace access, know what the grant gives it:
- A grant that admits
NatsUsers to an account lets that namespace claim any user key of the account, including keys that the auth controller did not issue, and revoke it by deleting theNatsUser. - Keep
NatsAccounts innats-system. A grant that admitsNatsAccounts from another namespace to theNatsOperatorlets that namespace set the limits of its own accounts and take any account key that noNatsAccountrecords yet.
Tenancy in the design gives the reasons.
Declare the team’s users and streams#
As the payments team, apply the users, the connection and the stream in payments:
payments-apiis the client of the team.payments-jetstreamis the user that the JetStream controller acts as in the account.- The
NatsConnectionand theNatsStreamstay inpayments, so they need no grant.
apiVersion: auth.nats-operator.io/v1beta1
kind: NatsUser
metadata:
name: payments-api
namespace: payments
spec:
accountRef:
kind: NatsAccount
namespace: nats-system
name: payments
permissions:
publish:
allow: ["payments.>", "$JS.API.>"]
subscribe:
allow: ["payments.>", "_INBOX.>"]
credentials:
secretKeyRef:
name: payments-api-creds
---
apiVersion: auth.nats-operator.io/v1beta1
kind: NatsUser
metadata:
name: payments-jetstream
namespace: payments
spec:
accountRef:
kind: NatsAccount
namespace: nats-system
name: payments
permissions:
publish:
allow: ["$JS.API.>"]
subscribe:
allow: ["_INBOX.>"]
credentials:
secretKeyRef:
name: payments-jetstream-creds
---
apiVersion: nats-operator.io/v1beta1
kind: NatsConnection
metadata:
name: demo
namespace: payments
spec:
servers: ["nats://demo.nats-system.svc:4222"]
credentials:
secretKeyRef:
name: payments-jetstream-creds
---
apiVersion: jetstream.nats-operator.io/v1beta1
kind: NatsStream
metadata:
name: payments
namespace: payments
spec:
connectionRef:
name: demo
name: PAYMENTS
subjects: ["payments.>"]
storage: File
replicas: 3
maxAge: 720h
kubectl apply -f https://nats-operator.io/docs/stories/04-team-self-service/01-team.yamlEach accountRef sets the namespace nats-system, and the grant payments-users allows the reference.
The creds Secrets are written beside the users, in payments.
Check a user that no grant admits#
A NatsUser in a namespace that no grant covers is refused.
Apply one in orders:
apiVersion: auth.nats-operator.io/v1beta1
kind: NatsUser
metadata:
name: payments-reader
namespace: orders
spec:
accountRef:
kind: NatsAccount
namespace: nats-system
name: payments
preset: readonly
credentials:
secretKeyRef:
name: payments-reader-creds
kubectl apply -f https://nats-operator.io/docs/stories/04-team-self-service/01-natsuser-payments-reader.yamlRead its status:
kubectl -n orders get natsuser payments-reader -o yamlThe status in the output is similar to this:
status:
observedGeneration: 1
conditions:
- type: Ready
status: "False"
reason: ReferenceNotPermitted
- type: ReferencesResolved
status: "False"
reason: NoGrant
message: >-
no NatsReferenceGrant in nats-system admits NatsUser from namespace
orders to NatsAccount payments
If you delete a grant, the auth controller revokes the users that it admitted and deletes their creds Secrets.