Task containers log in with their run credential on an external broker and reach only their own subjects, decided by the embedded broker's authenticator. The broker token is never put in a container environment.
102 lines
3.3 KiB
Go
102 lines
3.3 KiB
Go
package nats
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/nats-io/jwt/v2"
|
|
natsio "github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nkeys"
|
|
"github.com/synadia-io/callout.go"
|
|
)
|
|
|
|
// CalloutUser is the broker login archied's callout service connects with. The
|
|
// external broker lists it in auth_users, so its own login skips the callout;
|
|
// its password is the instance token.
|
|
const CalloutUser = "archie-callout"
|
|
|
|
// calloutAccount is the account a granted login is placed in: the broker's
|
|
// global account, which JetStream uses.
|
|
const calloutAccount = "$G"
|
|
|
|
// CalloutOptions configures StartCallout.
|
|
type CalloutOptions struct {
|
|
// URL is the external broker.
|
|
URL string
|
|
// Token is the instance token: the callout user's password, and the token
|
|
// that logs in with every permission.
|
|
Token string
|
|
// IssuerSeed is the account nkey seed whose public key the broker names as
|
|
// its auth_callout issuer.
|
|
IssuerSeed string
|
|
// Tasks admits task users to their own task's subjects.
|
|
Tasks TaskAuthority
|
|
}
|
|
|
|
// Callout is the authorization service of an external broker.
|
|
type Callout struct {
|
|
svc *callout.AuthorizationService
|
|
nc *natsio.Conn
|
|
}
|
|
|
|
// StartCallout answers an external broker's login requests with the decisions
|
|
// the embedded broker makes, so a task container holds only its run credential.
|
|
func StartCallout(ctx context.Context, opts CalloutOptions, log *slog.Logger) (*Callout, error) {
|
|
if log == nil {
|
|
log = slog.New(slog.DiscardHandler)
|
|
}
|
|
issuer, err := nkeys.FromSeed([]byte(opts.IssuerSeed))
|
|
if err != nil {
|
|
return nil, errors.New("nats callout: issuer seed is not an nkey seed")
|
|
}
|
|
nc, err := natsio.Connect(opts.URL, natsio.UserInfo(CalloutUser, opts.Token), natsio.Name("archie-callout"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("nats callout connect: %w", err)
|
|
}
|
|
svc, err := callout.NewAuthorizationService(nc,
|
|
callout.Authorizer(authenticator{token: opts.Token, tasks: opts.Tasks}.calloutAuthorizer(context.WithoutCancel(ctx), issuer)),
|
|
callout.ResponseSignerKey(issuer),
|
|
callout.Logger(brokerLogger{log: log.With("service", "nats", "component", "callout")}),
|
|
)
|
|
if err != nil {
|
|
nc.Close()
|
|
return nil, fmt.Errorf("nats callout service: %w", err)
|
|
}
|
|
log.InfoContext(ctx, "nats callout ready", "url", opts.URL)
|
|
return &Callout{svc: svc, nc: nc}, nil
|
|
}
|
|
|
|
// Close stops answering logins.
|
|
func (c *Callout) Close() {
|
|
_ = c.svc.Stop()
|
|
c.nc.Close()
|
|
}
|
|
|
|
// calloutAuthorizer turns an authenticator decision into a user JWT. An error
|
|
// refuses the login.
|
|
func (a authenticator) calloutAuthorizer(ctx context.Context, issuer nkeys.KeyPair) callout.AuthorizerFn {
|
|
return func(req *jwt.AuthorizationRequest) (string, error) {
|
|
g, ok := a.authorize(ctx, req.ConnectOptions.Token, req.ConnectOptions.Username, req.ConnectOptions.Password)
|
|
if !ok {
|
|
return "", errors.New("login refused")
|
|
}
|
|
return g.userJWT(req.UserNkey, issuer)
|
|
}
|
|
}
|
|
|
|
// userJWT is the grant as a broker user's permissions, signed by the issuer the
|
|
// broker trusts.
|
|
func (g grant) userJWT(userNkey string, issuer nkeys.KeyPair) (string, error) {
|
|
uc := jwt.NewUserClaims(userNkey)
|
|
uc.Name = g.Username
|
|
uc.Audience = calloutAccount
|
|
if !g.Full {
|
|
uc.Pub.Allow.Add(g.Publish...)
|
|
uc.Sub.Allow.Add(g.Subscribe...)
|
|
uc.Resp = &jwt.ResponsePermission{MaxMsgs: 1}
|
|
}
|
|
return uc.Encode(issuer)
|
|
}
|