Files
mtusi2026lab7/backend/internal/adapter/queue/identities_integration_test.go

107 lines
3.6 KiB
Go

package queue
import (
"backend/internal/config"
"backend/internal/domain/model"
"context"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"net/url"
"os"
"regexp"
"strings"
"testing"
"time"
)
func TestRealValkeyRejectsCrossRoleCommands(t *testing.T) {
if os.Getenv("RUN_BROKER_ACL_TESTS") != "1" {
t.Skip("explicit isolated Valkey admin required")
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
raw, e := config.ReadSecret("/run/secrets/cache_url")
if e != nil {
t.Fatal("admin file unavailable")
}
opts, e := redis.ParseURL(raw)
if e != nil {
t.Fatal("admin config invalid")
}
opts.MaxRetries = -1
admin := redis.NewClient(opts)
defer closeRedis(t, admin)
names, e := admin.ACLUsers(ctx).Result()
if e != nil {
t.Fatal("temporary ACL inventory failed")
}
owned := regexp.MustCompile(`^vp7_(pub|con)_[a-f0-9]{32}$`)
for _, name := range names {
if owned.MatchString(name) {
if e = admin.Do(ctx, "ACL", "DELUSER", name).Err(); e != nil {
t.Fatal("prior temporary ACL cleanup failed")
}
}
}
u, e := url.Parse(raw)
if e != nil {
t.Fatal("broker URL invalid")
}
suffix := strings.ReplaceAll(uuid.NewString(), "-", "")
pubName, conName := "vp7_pub_"+suffix, "vp7_con_"+suffix
password := uuid.NewString()
defer func() {
c, stop := context.WithTimeout(context.Background(), 3*time.Second)
defer stop()
if e := admin.Do(c, "ACL", "DELUSER", pubName, conName).Err(); e != nil {
t.Error("temporary ACL cleanup failed")
}
}()
for _, role := range []struct {
name string
commands []string
}{{pubName, []string{"+xadd"}}, {conName, []string{"+xgroup", "+xreadgroup", "+xautoclaim", "+xack", "+xdel"}}} {
args := []any{"ACL", "SETUSER", role.name, "reset", "on", ">" + password, "~attachment-jobs", "+hello", "+ping", "+select", "+client|setinfo"}
for _, command := range role.commands {
args = append(args, command)
}
if e = admin.Do(ctx, args...).Err(); e != nil {
t.Fatal("temporary ACL setup failed")
}
}
u.User = url.UserPassword(pubName, password)
pubURL := u.String()
u.User = url.UserPassword(conName, password)
p, c, e := Clients(pubURL, u.String())
if e != nil {
t.Fatal("separate clients rejected")
}
defer closeRedis(t, p)
defer closeRedis(t, c)
j := model.Job{ID: uuid.New(), AttachmentID: uuid.New(), PipelineVersion: 1, CreatedAt: time.Now()}
s := &publishStore{j: j}
b := New(p, c, s, "synthetic")
if e = b.Publish(ctx); e != nil || !s.published {
t.Fatal("publisher-only native identity could not publish outbox")
}
rows, e := admin.XRevRangeN(ctx, stream, "+", "-", 1).Result()
if e != nil || len(rows) != 1 || !strings.Contains(rows[0].Values["job"].(string), j.ID.String()) {
t.Fatal("owned test publication not found")
}
defer admin.XDel(context.Background(), stream, rows[0].ID)
if e = p.XAck(ctx, stream, "workers", rows[0].ID).Err(); e == nil || !strings.Contains(e.Error(), "NOPERM") {
t.Fatal("publisher identity could acknowledge consumer work")
}
if e = c.XAdd(ctx, &redis.XAddArgs{Stream: stream, Values: map[string]any{"forbidden": "synthetic"}}).Err(); e == nil || !strings.Contains(e.Error(), "NOPERM") {
t.Fatal("consumer identity could publish work")
}
processed := false
b.consume(ctx, rows[0], func(context.Context, model.Job) error { processed = true; return nil }, func(error) { t.Error("consumer failed") })
if !processed {
t.Fatal("native consumer did not accept envelope")
}
if left, e := admin.XRangeN(ctx, stream, rows[0].ID, rows[0].ID, 1).Result(); e != nil || len(left) != 0 {
t.Fatal("native consumer did not delete its acknowledged delivery")
}
}