s3vectors: support TagResource, UntagResource and ListTagsForResource (#12335)

* s3vectors: tag vector buckets and indexes

TagResource, UntagResource and ListTagsForResource answer on
/tags/{resourceArn} (POST, DELETE, GET) and by name. Each authorizes its
own s3vectors action on the bucket or index ARN, then reads or writes the
catalog record's tags. A resource holds at most 50 tags.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: apply the tags passed to CreateVectorBucket and CreateIndex

The tags field was accepted and dropped. It is now stored on the new
bucket or index, and creating with tags needs s3vectors:TagResource in
addition to the create action.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test/s3vectors: cover resource tagging end to end

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3tables: keep tag operations to a bound table bucket

TagResource, UntagResource and ListTagsForResource run through Execute
with WithTableBucketCreatedAt now answer not found when the bucket name
holds another bucket, as the bound creates and deletes already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: bind tagging to the authorized bucket and to vector indexes

The catalog tag calls carry the creation time of the bucket authorizeVector
approved, so a bucket created again under the name is not touched. An index
ARN must name a Lance table: another table in the vector namespace of a
format-less bucket is refused, as DeleteIndex does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: match a TagResource grant scoped to the bucket being created

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3tables: merge tag writes into the tags the record holds at the write

TagResource and UntagResource merged into the tags they read before the
compare-and-swap and wrote that map on every attempt, so a tag write that
landed in between was undone. The merge now runs inside the mutation.
WithMaxTags lets an in-process caller cap the merged set there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: check the 50-tag limit inside the catalog's tag write

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3tables: check a tag operation's bound bucket after its reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: validate each tag key UntagResource removes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: validate untag keys one at a time

UntagResource sized a map by the number of submitted tag keys before
validating them, so a large body of repeated keys allocated far more than
the request itself, before authorization ran.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3vectors: authorize UntagResource before validating its keys

Validating a near-32 MiB body of tag keys costs about 13x its JSON
decode, so an unauthorized caller could make the gateway spend that
work on a request it then refuses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-02 10:33:26 -08:00
1 parent 690aabda0e
commit ec968aa016
9 files changed
+835 -39

No files matched your search

+25 -1
View File
@@ -39,6 +39,9 @@ var handlers = map[string]func(*Server, http.ResponseWriter, *http.Request){
"ListVectors": (*Server).handleListVectors,
"DeleteVectors": (*Server).handleDeleteVectors,
"QueryVectors": (*Server).handleQueryVectors,
"TagResource": (*Server).handleTagResource,
"UntagResource": (*Server).handleUntagResource,
"ListTagsForResource": (*Server).handleListTagsForResource,
}
// lance table ids put indexes under a "vector" namespace inside their bucket.
@@ -275,6 +278,10 @@ func (s *Server) handleCreateVectorBucket(w http.ResponseWriter, r *http.Request
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
if err := s3tables.ValidateTagsWithLimit(req.Tags, maxResourceTags); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
// The catalog keeps the s3tables field name; GetVectorBucket renames it back.
enc := req.EncryptionConfiguration
if len(enc) != 0 {
@@ -283,7 +290,12 @@ func (s *Server) handleCreateVectorBucket(w http.ResponseWriter, r *http.Request
// No bucket exists yet, so check against the handler account as the
// catalog does for CreateTableBucket.
principal := s.tables.PrincipalFor(r)
if !s3tables.CheckPermissionWithContext("s3vectors:CreateVectorBucket", principal, s.tables.PrincipalFor(nil), "", "", s.tables.PolicyContext(r, "")) {
allowed := func(action, bucket string) bool {
return s3tables.CheckPermissionWithContext(action, principal, s.tables.PrincipalFor(nil), "", "", s.tables.PolicyContext(r, bucket))
}
// Tagging names the new bucket, so a grant scoped to it applies.
if !allowed("s3vectors:CreateVectorBucket", "") ||
(len(req.Tags) > 0 && !allowed("s3vectors:TagResource", req.VectorBucketName)) {
catalogErr(w, errVectorDenied)
return
}
@@ -293,6 +305,7 @@ func (s *Server) handleCreateVectorBucket(w http.ResponseWriter, r *http.Request
Name: req.VectorBucketName,
Format: s3tables.FormatLance,
EncryptionConfiguration: enc,
Tags: req.Tags,
}, &resp, identityOf(r))
if err != nil {
catalogErr(w, err)
@@ -963,6 +976,10 @@ func (s *Server) handleCreateIndex(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "ValidationException", "dimension must be 1..=4096")
return
}
if err := s3tables.ValidateTagsWithLimit(req.Tags, maxResourceTags); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
// The create grant is exercised before any write: a denied caller must not
// leave even the implicit namespace behind.
bucketRecord, err := s.authorizeVector(r.Context(), r, "s3vectors:CreateIndex", arns.bucket, arns.index)
@@ -970,6 +987,12 @@ func (s *Server) handleCreateIndex(w http.ResponseWriter, r *http.Request) {
catalogErr(w, err)
return
}
if len(req.Tags) > 0 {
if _, err := s.authorizeVector(r.Context(), r, "s3vectors:TagResource", arns.bucket, arns.index); err != nil {
catalogErr(w, err)
return
}
}
// The catalog writes land only in the bucket authorized above.
bound := s3tables.WithTableBucketCreatedAt(r.Context(), bucketRecord.CreatedAt)
// The namespace is implicit on AWS and internal bookkeeping here.
@@ -1014,6 +1037,7 @@ func (s *Server) handleCreateIndex(w http.ResponseWriter, r *http.Request) {
Name: arns.index,
Format: s3tables.FormatLance,
Metadata: &s3tables.TableMetadata{FullMetadata: metadataJSON},
Tags: req.Tags,
}, nil)
if err != nil {
catalogErr(w, err)
+14 -7
View File
@@ -69,17 +69,24 @@ func (s *Server) SetAccountID(id string) { s.accountID = id }
// apply the AES256 a bucket declares.
func (s *Server) SetEncryptionChecker(fn func() bool) { s.internal.SetEncryptionChecker(fn) }
// RegisterRoutes mounts POST / dispatching on the X-Amz-Target header and
// POST /{Operation} for clients that route per-path. Metrics wrap Auth like the
// S3 track middleware so rejected requests still count toward the operation.
// RegisterRoutes mounts POST / dispatching on the X-Amz-Target header,
// POST /{Operation} for clients that route per-path, and the tagging calls on
// /tags/{resourceArn}. Metrics wrap Auth like the S3 track middleware so
// rejected requests still count toward the operation.
func (s *Server) RegisterRoutes(router *mux.Router) {
router.HandleFunc("/", s.instrument(s.Auth(s.dispatch))).Methods(http.MethodPost)
router.HandleFunc("/{op}", s.instrument(s.Auth(s.dispatch))).Methods(http.MethodPost)
handler := s.instrument(s.Auth(s.dispatch))
router.HandleFunc("/", handler).Methods(http.MethodPost)
router.HandleFunc(tagsPathPrefix+"{resourceArn:.+}", handler).Methods(http.MethodGet, http.MethodPost, http.MethodDelete)
router.HandleFunc("/{op}", handler).Methods(http.MethodPost)
}
// operationFromRequest resolves the operation the way dispatch does: the last
// path segment, or the X-Amz-Target suffix when the path is bare.
// operationFromRequest resolves the operation the way dispatch does: the
// method on a /tags/ path, else the last path segment, or the X-Amz-Target
// suffix when the path is bare.
func operationFromRequest(r *http.Request) string {
if strings.HasPrefix(r.URL.Path, tagsPathPrefix) {
return tagOperation(r.Method)
}
op := strings.Trim(r.URL.Path, "/")
if i := strings.LastIndex(op, "/"); i >= 0 {
op = op[i+1:]
+191
View File
@@ -0,0 +1,191 @@
package s3vectors
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
)
// maxResourceTags is how many tags a vector bucket or index may carry.
const maxResourceTags = 50
const tagsPathPrefix = "/tags/"
// tagOperation names the tagging call a /tags/{resourceArn} request makes.
func tagOperation(method string) string {
switch method {
case http.MethodGet:
return "ListTagsForResource"
case http.MethodDelete:
return "UntagResource"
}
return "TagResource"
}
// readTagRequest decodes the body of a POST; GET and DELETE carry everything
// in the path and query.
func readTagRequest(r *http.Request, req interface{}) error {
if r.Method != http.MethodPost {
return nil
}
return readJSON(r, req)
}
// tagTarget resolves a tagging call's resourceArn, taken from the path when
// the request was routed there, to the vector bucket or index and the ARN the
// catalog keys that record by.
func (s *Server) tagTarget(r *http.Request, bodyArn string) (vectorBucketARNs, string, error) {
arn := bodyArn
if v := mux.Vars(r)["resourceArn"]; v != "" {
arn = v
}
var v vectorBucketARNs
if arn == "" {
return v, "", fmt.Errorf("resourceArn is required")
}
if !strings.HasPrefix(arn, "arn:") {
return v, "", fmt.Errorf("resourceArn must be an ARN, got %q", arn)
}
if err := s.arnParts(&v, "", arn); err != nil {
return v, "", err
}
catalogArn := s.s3tablesArn(v.bucket)
if v.index != "" {
catalogArn = fmt.Sprintf("%s/table/%s/%s", catalogArn, indexNamespace, v.index)
}
return v, catalogArn, nil
}
// authorizeTagging grants action on the tagged bucket or index and returns a
// context binding the catalog calls to the bucket it authorized. An index
// target must be a vector index, not another table in its namespace.
func (s *Server) authorizeTagging(r *http.Request, action string, arns vectorBucketARNs) (context.Context, error) {
record, err := s.authorizeVector(r.Context(), r, action, arns.bucket, arns.index)
if err != nil {
return nil, err
}
ctx := s3tables.WithTableBucketCreatedAt(r.Context(), record.CreatedAt)
if arns.index != "" {
index, err := s.indexRecord(ctx, arns.bucket, arns.index)
if err != nil {
return nil, err
}
if format, _ := index["format"].(string); format != s3tables.FormatLance {
return nil, &s3tables.S3TablesError{Type: s3tables.ErrCodeInvalidRequest,
Message: fmt.Sprintf("index %q is not a vector index", arns.index)}
}
}
return ctx, nil
}
func (s *Server) resourceTags(ctx context.Context, catalogArn string) (map[string]string, error) {
var resp s3tables.ListTagsForResourceResponse
if err := s.internalCatalog(ctx, "ListTagsForResource",
&s3tables.ListTagsForResourceRequest{ResourceARN: catalogArn}, &resp); err != nil {
return nil, err
}
if resp.Tags == nil {
resp.Tags = map[string]string{}
}
return resp.Tags, nil
}
func (s *Server) handleTagResource(w http.ResponseWriter, r *http.Request) {
var req TagResourceRequest
if err := readTagRequest(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
arns, catalogArn, err := s.tagTarget(r, req.ResourceArn)
if err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
if len(req.Tags) == 0 {
writeError(w, http.StatusBadRequest, "ValidationException", "tags are required")
return
}
if err := s3tables.ValidateTagsWithLimit(req.Tags, maxResourceTags); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
ctx, err := s.authorizeTagging(r, "s3vectors:TagResource", arns)
if err != nil {
catalogErr(w, err)
return
}
if err := s.internalCatalog(s3tables.WithMaxTags(ctx, maxResourceTags), "TagResource",
&s3tables.TagResourceRequest{ResourceARN: catalogArn, Tags: req.Tags}, nil); err != nil {
catalogErr(w, err)
return
}
writeJSON(w, http.StatusOK, &TagResourceResponse{})
}
func (s *Server) handleUntagResource(w http.ResponseWriter, r *http.Request) {
var req UntagResourceRequest
if err := readTagRequest(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
if keys, ok := r.URL.Query()["tagKeys"]; ok {
req.TagKeys = keys
}
arns, catalogArn, err := s.tagTarget(r, req.ResourceArn)
if err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
if len(req.TagKeys) == 0 {
writeError(w, http.StatusBadRequest, "ValidationException", "tagKeys are required")
return
}
ctx, err := s.authorizeTagging(r, "s3vectors:UntagResource", arns)
if err != nil {
catalogErr(w, err)
return
}
key := make(map[string]string, 1)
for _, k := range req.TagKeys {
clear(key)
key[k] = ""
if err := s3tables.ValidateTagsWithLimit(key, 1); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
}
if err := s.internalCatalog(ctx, "UntagResource",
&s3tables.UntagResourceRequest{ResourceARN: catalogArn, TagKeys: req.TagKeys}, nil); err != nil {
catalogErr(w, err)
return
}
writeJSON(w, http.StatusOK, &UntagResourceResponse{})
}
func (s *Server) handleListTagsForResource(w http.ResponseWriter, r *http.Request) {
var req ListTagsForResourceRequest
if err := readTagRequest(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
arns, catalogArn, err := s.tagTarget(r, req.ResourceArn)
if err != nil {
writeError(w, http.StatusBadRequest, "ValidationException", err.Error())
return
}
ctx, err := s.authorizeTagging(r, "s3vectors:ListTagsForResource", arns)
if err != nil {
catalogErr(w, err)
return
}
tags, err := s.resourceTags(ctx, catalogArn)
if err != nil {
catalogErr(w, err)
return
}
writeJSON(w, http.StatusOK, &ListTagsForResourceResponse{Tags: tags})
}
+355
View File
@@ -0,0 +1,355 @@
package s3vectors
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"runtime"
"strings"
"testing"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
)
type tagClient struct {
t *testing.T
ts *httptest.Server
fs *s3tablestest.MemFiler
}
func newTagClient(t *testing.T) (*Server, *tagClient) {
fs := s3tablestest.Start(t)
s := NewServer(s3tables.NewManagerClient(fs.Client), headerAuth{}, newFakeVectors(0))
router := mux.NewRouter().SkipClean(true)
s.RegisterRoutes(router)
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)
return s, &tagClient{t: t, ts: ts, fs: fs}
}
func (c *tagClient) do(identity, method, path string, header http.Header, payload interface{}) (int, string) {
c.t.Helper()
var body io.Reader
if payload != nil {
raw, err := json.Marshal(payload)
if err != nil {
c.t.Fatalf("marshal: %v", err)
}
body = bytes.NewReader(raw)
}
req, err := http.NewRequest(method, c.ts.URL+path, body)
if err != nil {
c.t.Fatalf("build %s %s: %v", method, path, err)
}
for k, v := range header {
req.Header[k] = v
}
if identity != "" {
req.Header.Set("X-Test-Identity", identity)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.t.Fatalf("%s %s: %v", method, path, err)
}
defer resp.Body.Close()
out, _ := io.ReadAll(resp.Body)
return resp.StatusCode, string(out)
}
func (c *tagClient) op(identity, op string, payload interface{}) (int, string) {
return c.do(identity, http.MethodPost, "/"+op, nil, payload)
}
func (c *tagClient) tag(identity, arn string, tags map[string]string) (int, string) {
return c.do(identity, http.MethodPost, tagsPathPrefix+url.PathEscape(arn), nil, map[string]interface{}{"tags": tags})
}
func (c *tagClient) untag(identity, arn string, keys ...string) (int, string) {
q := url.Values{"tagKeys": keys}
return c.do(identity, http.MethodDelete, tagsPathPrefix+url.PathEscape(arn)+"?"+q.Encode(), nil, nil)
}
func (c *tagClient) list(identity, arn string) (int, map[string]string) {
c.t.Helper()
code, body := c.do(identity, http.MethodGet, tagsPathPrefix+url.PathEscape(arn), nil, nil)
if code != http.StatusOK {
return code, nil
}
var resp struct {
Tags map[string]string `json:"tags"`
}
if err := json.Unmarshal([]byte(body), &resp); err != nil || resp.Tags == nil {
c.t.Fatalf("decode ListTagsForResource %q: %v", body, err)
}
return code, resp.Tags
}
func mustStatus(t *testing.T, what string, code int, body string, want int) {
t.Helper()
if code != want {
t.Fatalf("%s = %d %s, want %d", what, code, body, want)
}
}
func TestTagVectorBucketsAndIndexes(t *testing.T) {
s, c := newTagClient(t)
code, body := c.op("", "CreateVectorBucket", &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"})
mustStatus(t, "create bucket", code, body, http.StatusOK)
code, body = c.op("", "CreateIndex", &CreateIndexRequest{
VectorBucketName: "vec-bucket", IndexName: "idx", DataType: "float32", Dimension: 3,
})
mustStatus(t, "create index", code, body, http.StatusOK)
bucketArn, indexArn := s.vectorBucketArn("vec-bucket"), s.indexArn("vec-bucket", "idx")
if _, tags := c.list("", bucketArn); len(tags) != 0 {
t.Fatalf("new bucket tags = %v, want none", tags)
}
code, body = c.tag("", bucketArn, map[string]string{"team": "search", "env": "dev"})
mustStatus(t, "tag bucket", code, body, http.StatusOK)
code, body = c.tag("", indexArn, map[string]string{"team": "ranking"})
mustStatus(t, "tag index", code, body, http.StatusOK)
code, body = c.tag("", bucketArn, map[string]string{"env": "prod"})
mustStatus(t, "retag bucket", code, body, http.StatusOK)
if _, tags := c.list("", bucketArn); !reflect.DeepEqual(tags, map[string]string{"team": "search", "env": "prod"}) {
t.Fatalf("bucket tags = %v", tags)
}
if _, tags := c.list("", indexArn); !reflect.DeepEqual(tags, map[string]string{"team": "ranking"}) {
t.Fatalf("index tags = %v", tags)
}
code, body = c.untag("", bucketArn, "env", "absent")
mustStatus(t, "untag bucket", code, body, http.StatusOK)
if _, tags := c.list("", bucketArn); !reflect.DeepEqual(tags, map[string]string{"team": "search"}) {
t.Fatalf("bucket tags after untag = %v", tags)
}
// Clients that post the operation by name carry resourceArn in the body.
code, body = c.do("", http.MethodPost, "/", http.Header{"X-Amz-Target": {"AmazonS3Vectors.ListTagsForResource"}},
&ListTagsForResourceRequest{ResourceArn: indexArn})
mustStatus(t, "list by target header", code, body, http.StatusOK)
var resp ListTagsForResourceResponse
if err := json.Unmarshal([]byte(body), &resp); err != nil || resp.Tags["team"] != "ranking" {
t.Fatalf("list by target header = %s (%v)", body, err)
}
}
func TestTagResourceValidation(t *testing.T) {
s, c := newTagClient(t)
code, body := c.op("", "CreateVectorBucket", &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"})
mustStatus(t, "create bucket", code, body, http.StatusOK)
bucketArn := s.vectorBucketArn("vec-bucket")
code, body = c.tag("", s.indexArn("vec-bucket", "missing"), map[string]string{"a": "b"})
mustStatus(t, "tag missing index", code, body, http.StatusNotFound)
code, _ = c.list("", s.vectorBucketArn("no-such-bucket"))
mustStatus(t, "list missing bucket", code, "", http.StatusNotFound)
code, body = c.tag("", "vec-bucket", map[string]string{"a": "b"})
mustStatus(t, "tag by name", code, body, http.StatusBadRequest)
code, body = c.tag("", "arn:aws:s3vectors:elsewhere:1:bucket/vec-bucket", map[string]string{"a": "b"})
mustStatus(t, "tag foreign ARN", code, body, http.StatusBadRequest)
code, body = c.tag("", bucketArn, map[string]string{})
mustStatus(t, "tag with no tags", code, body, http.StatusBadRequest)
code, body = c.tag("", bucketArn, map[string]string{"bad key!": "v"})
mustStatus(t, "tag with bad key", code, body, http.StatusBadRequest)
code, body = c.untag("", bucketArn)
mustStatus(t, "untag with no keys", code, body, http.StatusBadRequest)
code, body = c.untag("", bucketArn, "")
mustStatus(t, "untag an empty key", code, body, http.StatusBadRequest)
code, body = c.untag("", bucketArn, strings.Repeat("k", 129))
mustStatus(t, "untag a key past 128 characters", code, body, http.StatusBadRequest)
tags := map[string]string{}
for i := 0; i < maxResourceTags; i++ {
tags[fmt.Sprintf("k%d", i)] = "v"
}
code, body = c.tag("", bucketArn, tags)
mustStatus(t, "tag up to the limit", code, body, http.StatusOK)
code, body = c.tag("", bucketArn, map[string]string{"k0": "changed"})
mustStatus(t, "overwrite at the limit", code, body, http.StatusOK)
code, body = c.tag("", bucketArn, map[string]string{"one-more": "v"})
mustStatus(t, "tag past the limit", code, body, http.StatusBadRequest)
tags["one-more"] = "v"
code, body = c.tag("", s.vectorBucketArn("vec-bucket"), tags)
mustStatus(t, "request past the limit", code, body, http.StatusBadRequest)
if _, got := c.list("", bucketArn); len(got) != maxResourceTags || got["k0"] != "changed" {
t.Fatalf("tags after refused requests: %d tags, k0=%q", len(got), got["k0"])
}
}
func TestTaggingNeedsItsOwnGrant(t *testing.T) {
s, c := newTagClient(t)
code, body := c.op("", "CreateVectorBucket", &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"})
mustStatus(t, "create bucket", code, body, http.StatusOK)
bucketArn := s.vectorBucketArn("vec-bucket")
code, body = c.tag("", bucketArn, map[string]string{"team": "search"})
mustStatus(t, "owner tags", code, body, http.StatusOK)
code, _ = c.list("reader", bucketArn)
mustStatus(t, "list without a grant", code, "", http.StatusForbidden)
code, body = c.op("", "PutVectorBucketPolicy", &PutVectorBucketPolicyRequest{
VectorBucketName: "vec-bucket",
Policy: `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*",` +
`"Action":["s3vectors:ListTagsForResource","s3vectors:GetVectorBucket"],"Resource":"` + bucketArn + `"}]}`,
})
mustStatus(t, "put policy", code, body, http.StatusOK)
if code, tags := c.list("reader", bucketArn); code != http.StatusOK || tags["team"] != "search" {
t.Fatalf("list under grant = %d %v", code, tags)
}
code, body = c.tag("reader", bucketArn, map[string]string{"team": "other"})
mustStatus(t, "tag under list grant", code, body, http.StatusForbidden)
code, body = c.untag("reader", bucketArn, "team")
mustStatus(t, "untag under list grant", code, body, http.StatusForbidden)
code, body = c.untag("reader", bucketArn, "")
mustStatus(t, "untag an empty key under list grant", code, body, http.StatusForbidden)
if _, tags := c.list("", bucketArn); tags["team"] != "search" {
t.Fatalf("refused writes changed tags: %v", tags)
}
}
func TestCreateWithTagsNeedsTagResource(t *testing.T) {
e := newAuthTestEnv(t)
tags := map[string]string{"team": "search"}
listed := func(arn string) map[string]string {
t.Helper()
w := e.vectorResp(adminIdentity(), e.s.handleListTagsForResource, &ListTagsForResourceRequest{ResourceArn: arn})
var resp ListTagsForResourceResponse
if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &resp) != nil {
t.Fatalf("list tags of %s: %d %s", arn, w.Code, w.Body.String())
}
return resp.Tags
}
creator := func(actions ...s3api.Action) *s3api.Identity {
return &s3api.Identity{Name: "creator", Account: &s3api.Account{Id: "creator"}, Actions: actions}
}
createOnly := creator("s3vectors:CreateVectorBucket", "s3vectors:CreateIndex")
withTag := creator("s3vectors:CreateVectorBucket", "s3vectors:CreateIndex", "s3vectors:TagResource")
if code := e.vector(createOnly, e.s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "denied", Tags: tags}); code != http.StatusForbidden {
t.Fatalf("tagged bucket create without TagResource: %d, want 403", code)
}
if code := e.vector(adminIdentity(), e.s.handleGetVectorBucket, &GetVectorBucketRequest{VectorBucketName: "denied"}); code != http.StatusNotFound {
t.Fatalf("refused create left a bucket: %d", code)
}
if code := e.vector(withTag, e.s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "vec-bucket", Tags: tags}); code != http.StatusOK {
t.Fatalf("tagged bucket create: %d", code)
}
if got := listed(e.s.vectorBucketArn("vec-bucket")); !reflect.DeepEqual(got, tags) {
t.Fatalf("bucket tags = %v, want %v", got, tags)
}
e.createBuckets(adminIdentity(), "shared")
index := func(ident *s3api.Identity, name string, tags map[string]string) int {
return e.vector(ident, e.s.handleCreateIndex, &CreateIndexRequest{
VectorBucketName: "v-shared", IndexName: name, DataType: "float32", Dimension: 3, Tags: tags,
})
}
if code := index(createOnly, "denied", tags); code != http.StatusForbidden {
t.Fatalf("tagged index create without TagResource: %d, want 403", code)
}
if code := index(createOnly, "plain", nil); code != http.StatusOK {
t.Fatalf("untagged index create: %d", code)
}
if code := index(withTag, "tagged", tags); code != http.StatusOK {
t.Fatalf("tagged index create: %d", code)
}
if got := listed(e.s.indexArn("v-shared", "tagged")); !reflect.DeepEqual(got, tags) {
t.Fatalf("index tags = %v, want %v", got, tags)
}
if got := listed(e.s.indexArn("v-shared", "plain")); len(got) != 0 {
t.Fatalf("untagged index tags = %v", got)
}
scoped := creator("s3vectors:CreateVectorBucket", "s3vectors:TagResource:team-bucket")
if code := e.vector(scoped, e.s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "other-bucket", Tags: tags}); code != http.StatusForbidden {
t.Fatalf("tagged create outside the scoped TagResource grant: %d, want 403", code)
}
if code := e.vector(scoped, e.s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "team-bucket", Tags: tags}); code != http.StatusOK {
t.Fatalf("tagged create under a TagResource grant scoped to the bucket: %d", code)
}
tooMany := map[string]string{}
for i := 0; i <= maxResourceTags; i++ {
tooMany[fmt.Sprintf("k%d", i)] = "v"
}
if code := e.vector(withTag, e.s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "too-many", Tags: tooMany}); code != http.StatusBadRequest {
t.Fatalf("bucket create past the tag limit: %d, want 400", code)
}
if code := index(withTag, "too-many", tooMany); code != http.StatusBadRequest {
t.Fatalf("index create past the tag limit: %d, want 400", code)
}
}
func TestTaggingRefusesTablesThatAreNotIndexes(t *testing.T) {
s, c := newTagClient(t)
code, body := c.op("", "CreateVectorBucket", &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"})
mustStatus(t, "create bucket", code, body, http.StatusOK)
code, body = c.op("", "CreateIndex", &CreateIndexRequest{
VectorBucketName: "vec-bucket", IndexName: "idx", DataType: "float32", Dimension: 3,
})
mustStatus(t, "create index", code, body, http.StatusOK)
indexArn := s.indexArn("vec-bucket", "idx")
code, body = c.tag("", indexArn, map[string]string{"team": "search"})
mustStatus(t, "tag index", code, body, http.StatusOK)
dir, name := splitEntryPath(s3tables.GetTablePath("vec-bucket", indexNamespace, "idx"))
entry := c.fs.Get(dir, name)
var meta map[string]interface{}
if err := json.Unmarshal(entry.Extended[s3tables.ExtendedKeyMetadata], &meta); err != nil {
t.Fatalf("decode index metadata: %v", err)
}
meta["format"] = s3tables.FormatIceberg
data, _ := json.Marshal(meta)
entry.Extended[s3tables.ExtendedKeyMetadata] = data
c.fs.Put(dir, name, entry.Extended)
code, _ = c.list("", indexArn)
mustStatus(t, "list tags of an Iceberg table", code, "", http.StatusBadRequest)
code, body = c.tag("", indexArn, map[string]string{"team": "other"})
mustStatus(t, "tag an Iceberg table", code, body, http.StatusBadRequest)
code, body = c.untag("", indexArn, "team")
mustStatus(t, "untag an Iceberg table", code, body, http.StatusBadRequest)
if got := string(c.fs.Get(dir, name).Extended[s3tables.ExtendedKeyTags]); got != `{"team":"search"}` {
t.Fatalf("refused calls changed the table's tags: %s", got)
}
}
func TestUntagManyKeysAllocatesLikeDecoding(t *testing.T) {
s, c := newTagClient(t)
code, out := c.op("", "CreateVectorBucket", &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"})
mustStatus(t, "create bucket", code, out, http.StatusOK)
body, err := json.Marshal(&UntagResourceRequest{ResourceArn: s.vectorBucketArn("vec-bucket"), TagKeys: make([]string, 1<<18)})
if err != nil {
t.Fatal(err)
}
allocated := func(f func()) uint64 {
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
f()
runtime.ReadMemStats(&after)
return after.TotalAlloc - before.TotalAlloc
}
decode := allocated(func() {
var req UntagResourceRequest
if err := json.Unmarshal(body, &req); err != nil {
t.Fatal(err)
}
})
w := httptest.NewRecorder()
handle := allocated(func() {
s.handleUntagResource(w, httptest.NewRequest(http.MethodPost, "/UntagResource", bytes.NewReader(body)))
})
mustStatus(t, "untag empty keys", w.Code, w.Body.String(), http.StatusBadRequest)
if handle > decode*3/2 {
t.Fatalf("untag allocated %d bytes, decoding the request takes %d", handle, decode)
}
}
+24 -1
View File
@@ -62,7 +62,7 @@ type vectorBucketARNs struct {
type CreateVectorBucketRequest struct {
VectorBucketName string `json:"vectorBucketName"`
EncryptionConfiguration map[string]interface{} `json:"encryptionConfiguration,omitempty"`
MetadataResourceTags map[string]string `json:"-"`
Tags map[string]string `json:"tags,omitempty"`
}
type CreateVectorBucketResponse struct {
@@ -136,6 +136,7 @@ type CreateIndexRequest struct {
MetadataConfiguration *MetadataConfiguration `json:"metadataConfiguration,omitempty"`
VectorBucketName string `json:"vectorBucketName"`
VectorBucketArn string `json:"vectorBucketArn"`
Tags map[string]string `json:"tags,omitempty"`
}
type MetadataConfiguration struct {
@@ -295,3 +296,25 @@ type QueryVectorsResponse struct {
DistanceMetric string `json:"distanceMetric"`
Vectors []*QueryOutputVector `json:"vectors"`
}
type TagResourceRequest struct {
ResourceArn string `json:"resourceArn"`
Tags map[string]string `json:"tags"`
}
type TagResourceResponse struct{}
type UntagResourceRequest struct {
ResourceArn string `json:"resourceArn"`
TagKeys []string `json:"tagKeys"`
}
type UntagResourceResponse struct{}
type ListTagsForResourceRequest struct {
ResourceArn string `json:"resourceArn"`
}
type ListTagsForResourceResponse struct {
Tags map[string]string `json:"tags"`
}