Files
archie/internal/infrastructure/eventbus/nats/auth.go
T
sam 3a965e2a4f feat(nats): an external broker defers logins to archie through auth callout (#1866)
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.
2026-11-14 04:04:15 +11:00

111 lines
3.2 KiB
Go

package nats
import (
"context"
"crypto/subtle"
"slices"
"strconv"
"strings"
"time"
"github.com/nats-io/nats-server/v2/server"
)
// TaskUserPrefix names a task's broker user: "task-<id>". Its password is the
// task's run credential.
const TaskUserPrefix = "task-"
// TaskInboxPrefix is the inbox prefix a task's connection must use: the only
// inbox it may subscribe to.
func TaskInboxPrefix(taskID int64) string {
return "_INBOX." + TaskUserPrefix + strconv.FormatInt(taskID, 10)
}
// TaskSubjects is what one task's connection may publish to and subscribe to,
// beside its own inbox and its replies.
type TaskSubjects struct {
Publish []string
Subscribe []string
}
// TaskAuthority answers which subjects a run credential opens for its task.
// An error refuses the connection.
type TaskAuthority interface {
TaskSubjects(ctx context.Context, taskID int64, credential string) (TaskSubjects, error)
}
// taskAuthTimeout bounds the credential lookup a connection waits on.
const taskAuthTimeout = 2 * time.Second
// authenticator admits the instance token with every permission, and a task
// user only to its own task's subjects, so no container holds a credential
// that reaches past its run. The embedded broker and the auth callout of an
// external broker both decide through it.
type authenticator struct {
token string
tasks TaskAuthority
}
// grant is what a login is allowed. Full is the instance token's reach; a task
// login lists its subjects instead.
type grant struct {
Username string
Full bool
Publish []string
Subscribe []string
}
// authorize decides one login: the instance token, or "task-<id>" with that
// task's run credential.
func (a authenticator) authorize(ctx context.Context, token, username, password string) (grant, bool) {
if token != "" {
if subtle.ConstantTimeCompare([]byte(token), []byte(a.token)) != 1 {
return grant{}, false
}
return grant{Username: "archie", Full: true}, true
}
taskID, ok := taskUser(username)
if !ok || password == "" || a.tasks == nil {
return grant{}, false
}
ctx, cancel := context.WithTimeout(ctx, taskAuthTimeout)
defer cancel()
subjects, err := a.tasks.TaskSubjects(ctx, taskID, password)
if err != nil {
return grant{}, false
}
return grant{
Username: username,
Publish: slices.Clone(subjects.Publish),
Subscribe: append(slices.Clone(subjects.Subscribe), TaskInboxPrefix(taskID)+".>"),
}, true
}
func (a authenticator) Check(c server.ClientAuthentication) bool {
opts := c.GetOpts()
g, ok := a.authorize(context.Background(), opts.Token, opts.Username, opts.Password)
if !ok {
return false
}
if g.Full {
c.RegisterUser(&server.User{Username: g.Username})
return true
}
c.RegisterUser(&server.User{Username: g.Username, Permissions: &server.Permissions{
Publish: &server.SubjectPermission{Allow: g.Publish},
Subscribe: &server.SubjectPermission{Allow: g.Subscribe},
// Lets the task answer the one request it is sent, on the requester's
// reply subject.
Response: &server.ResponsePermission{MaxMsgs: 1},
}})
return true
}
func taskUser(name string) (int64, bool) {
id, err := strconv.ParseInt(strings.TrimPrefix(name, TaskUserPrefix), 10, 64)
if !strings.HasPrefix(name, TaskUserPrefix) || err != nil || id <= 0 {
return 0, false
}
return id, true
}