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:

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.

01-platform.yaml yaml ·
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.yaml

To 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 the NatsUser.
  • Keep NatsAccounts in nats-system. A grant that admits NatsAccounts from another namespace to the NatsOperator lets that namespace set the limits of its own accounts and take any account key that no NatsAccount records 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-api is the client of the team.
  • payments-jetstream is the user that the JetStream controller acts as in the account.
  • The NatsConnection and the NatsStream stay in payments, so they need no grant.
01-team.yaml yaml ·
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.yaml

Each 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:

01-natsuser-payments-reader.yaml yaml ·
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.yaml

Read its status:

kubectl -n orders get natsuser payments-reader -o yaml

The status in the output is similar to this:

01-status-natsuser-payments-reader.yaml yaml ·
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.

Esc
↓ ↑ Navigate Enter Select Esc Close