Files
seaweedfs/weed/s3api/s3tables/vector_namespace.go
T
Chris LuandClaude Opus 5.5 e2826c48bd s3tables: make the vector namespace of a vector bucket read-only (#12292)
* s3tables: make the vector namespace of a vector bucket read-only

S3 Vectors authorizes index operations against the vector bucket policy,
but an index is also a catalog table in the bucket's "vector" namespace,
and the S3 Tables API only checks s3tables grants. A caller the vector
policy denies DeleteIndex could still delete the index, and its data,
with DeleteTable.

Untrusted writes that name the vector namespace of a LANCE bucket now run
behind a fence that passes filer reads and refuses filer writes. The
operation's own checks still answer everything short of a write, so a
caller without the grant gets its usual denial and learns nothing about
the bucket, and a CreateTable that finds the index already there, which
is what the vector engine's declare does, still succeeds. A refused write
answers 403 AccessDenied. Trusted callers (the gateway, the shell) are
unaffected, and so is a namespace named vector in an Iceberg bucket.

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

* lance: refuse register and deregister in a vector bucket's vector namespace

Every catalog write the Lance namespace makes goes through the S3 Tables
handler and its vector namespace fence, but register over an existing
table at the same location and deregister only add or remove a marker
file in the dataset directory. Check the vector namespace before those
writes, after the table read that already gates them.

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

* s3tables: refuse deferred creates in a vector bucket's vector namespace

Manager.AuthorizeCreateTable gates the Iceberg create paths that write
metadata into the table bucket before the catalog registers the table.
Those writes never pass the handler's fence, so the gate applies the same
read-only rule once the caller's own create permission is confirmed.

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

* s3tables: let a declare of an existing index past the vector namespace fence

The vector engine declares every index the gateway has just created, and
that CreateTable only reaches its idempotent path. Upstream that path
writes nothing, so the fence never trips, but a build whose idempotent
path repairs table state would refuse the engine. Decide the exception
up front instead: a CreateTable naming a finished index in the vector
namespace runs unfenced, while a new name or an unfinished create stays
fenced.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 15:36:48 -07:00

209 lines
7.6 KiB
Go

package s3tables
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"sync/atomic"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"google.golang.org/grpc"
)
// VectorIndexNamespace holds a vector bucket's indexes. Only the S3 Vectors
// gateway, through a trusted Manager, may change it in a LANCE bucket.
const VectorIndexNamespace = "vector"
const vectorNamespaceReadOnly = "the vector namespace of a vector bucket is read-only; manage its indexes through S3 Vectors"
var errVectorNamespaceReadOnly = errors.New(vectorNamespaceReadOnly)
func isVectorIndexNamespace(namespace []string) bool {
name, err := validateNamespace(namespace)
return err == nil && name == VectorIndexNamespace
}
func (h *S3TablesHandler) checkVectorNamespaceWrite(ctx context.Context, filerClient FilerClient, bucketName string, namespace []string) error {
if h.trusted || !isVectorIndexNamespace(namespace) {
return nil
}
var metadata tableBucketMetadata
err := filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
data, err := h.getExtendedAttribute(ctx, client, GetTableBucketPath(bucketName), ExtendedKeyMetadata)
if err != nil {
return err
}
return json.Unmarshal(data, &metadata)
})
if errors.Is(err, filer_pb.ErrNotFound) || errors.Is(err, ErrAttributeNotFound) {
return nil
}
if err != nil {
return err
}
if metadata.Format == FormatLance {
return &S3TablesError{Type: ErrCodeAccessDenied, Message: vectorNamespaceReadOnly}
}
return nil
}
// fenceVectorNamespace returns a fence for an untrusted write naming the
// vector namespace of a LANCE bucket. The request still runs, so the
// operation's own checks answer everything short of a write and an ungranted
// caller gets its usual denial. A CreateTable of an index that already exists
// is the vector engine's declare and passes unfenced.
func (h *S3TablesHandler) fenceVectorNamespace(w http.ResponseWriter, r *http.Request, filerClient FilerClient, operation string) *vectorNamespaceFence {
if h.trusted || strings.HasPrefix(operation, "Get") || strings.HasPrefix(operation, "List") {
return nil
}
body, _ := io.ReadAll(io.LimitReader(r.Body, MaxRequestBodySize+1))
r.Body = io.NopCloser(bytes.NewReader(body))
var req struct {
TableBucketARN string `json:"tableBucketARN"`
Namespace []string `json:"namespace"`
SourceNamespace []string `json:"sourceNamespace"`
DestNamespace []string `json:"destNamespace"`
ResourceARN string `json:"resourceArn"`
Name string `json:"name"`
}
// Like the handler's own decode, this keeps going past a mistyped field.
_ = json.Unmarshal(body, &req)
type target struct {
bucket string
namespace []string
}
bucketName, _ := parseBucketNameFromARN(req.TableBucketARN)
targets := []target{{bucketName, req.Namespace}, {bucketName, req.SourceNamespace}, {bucketName, req.DestNamespace}}
if bucket, namespace, _, err := parseTableFromARN(req.ResourceARN); err == nil {
targets = append(targets, target{bucket, []string{namespace}})
}
for _, t := range targets {
if t.bucket == "" || h.checkVectorNamespaceWrite(r.Context(), filerClient, t.bucket, t.namespace) == nil {
continue
}
if operation == "CreateTable" && isVectorIndexNamespace(req.Namespace) && h.vectorIndexExists(r.Context(), filerClient, bucketName, req.Name) {
return nil
}
return &vectorNamespaceFence{ResponseWriter: w, h: h, filer: filerClient}
}
return nil
}
func (h *S3TablesHandler) vectorIndexExists(ctx context.Context, filerClient FilerClient, bucketName, name string) bool {
if _, err := validateTableName(name); err != nil {
return false
}
exists := false
_ = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
entry, err := h.lookupEntry(ctx, client, GetTablePath(bucketName, VectorIndexNamespace, name))
if err != nil {
return err
}
_, hasMetadata := entry.Extended[ExtendedKeyMetadata]
exists = hasMetadata && !isUnfinishedCreate(entry.Extended) && EntryType(entry.Extended) == EntryTypeTable
return nil
})
return exists
}
// vectorNamespaceFence passes reads, refuses filer writes, and answers the
// request as read-only once a write was refused. Handlers write only after
// their permission check, so an ungranted caller never sees that answer.
type vectorNamespaceFence struct {
http.ResponseWriter
h *S3TablesHandler
filer FilerClient
tripped atomic.Bool
decided bool
refusing bool
}
func (f *vectorNamespaceFence) WithFilerClient(streamingMode bool, fn func(client filer_pb.SeaweedFilerClient) error) error {
return f.filer.WithFilerClient(streamingMode, func(client filer_pb.SeaweedFilerClient) error {
return fn(readOnlyFilerClient{SeaweedFilerClient: client, tripped: &f.tripped})
})
}
func (f *vectorNamespaceFence) WriteHeader(status int) {
if !f.settle() {
f.ResponseWriter.WriteHeader(status)
}
}
func (f *vectorNamespaceFence) Write(b []byte) (int, error) {
if f.settle() {
return len(b), nil
}
return f.ResponseWriter.Write(b)
}
// settle decides, at the first response write, whether the handler's answer
// is replaced by the read-only refusal.
func (f *vectorNamespaceFence) settle() bool {
if !f.decided {
f.decided = true
f.refusing = f.tripped.Load()
if f.refusing {
f.h.writeError(f.ResponseWriter, http.StatusForbidden, ErrCodeAccessDenied, vectorNamespaceReadOnly)
}
}
return f.refusing
}
type readOnlyFilerClient struct {
filer_pb.SeaweedFilerClient
tripped *atomic.Bool
}
func (c readOnlyFilerClient) refuse() error {
c.tripped.Store(true)
return errVectorNamespaceReadOnly
}
func (c readOnlyFilerClient) CreateEntry(context.Context, *filer_pb.CreateEntryRequest, ...grpc.CallOption) (*filer_pb.CreateEntryResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) UpdateEntry(context.Context, *filer_pb.UpdateEntryRequest, ...grpc.CallOption) (*filer_pb.UpdateEntryResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) AppendToEntry(context.Context, *filer_pb.AppendToEntryRequest, ...grpc.CallOption) (*filer_pb.AppendToEntryResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) DeleteEntry(context.Context, *filer_pb.DeleteEntryRequest, ...grpc.CallOption) (*filer_pb.DeleteEntryResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) AtomicRenameEntry(context.Context, *filer_pb.AtomicRenameEntryRequest, ...grpc.CallOption) (*filer_pb.AtomicRenameEntryResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) StreamRenameEntry(context.Context, *filer_pb.StreamRenameEntryRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[filer_pb.StreamRenameEntryResponse], error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) StreamMutateEntry(context.Context, ...grpc.CallOption) (grpc.BidiStreamingClient[filer_pb.StreamMutateEntryRequest, filer_pb.StreamMutateEntryResponse], error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) ObjectTransaction(context.Context, *filer_pb.ObjectTransactionRequest, ...grpc.CallOption) (*filer_pb.ObjectTransactionResponse, error) {
return nil, c.refuse()
}
func (c readOnlyFilerClient) ObjectTransactionBatch(context.Context, *filer_pb.ObjectTransactionBatchRequest, ...grpc.CallOption) (*filer_pb.ObjectTransactionBatchResponse, error) {
return nil, c.refuse()
}
// CheckVectorNamespaceWrite refuses an untrusted write into the vector
// namespace of a LANCE bucket, for callers that write the filer directly.
func (m *Manager) CheckVectorNamespaceWrite(ctx context.Context, filerClient FilerClient, bucketName string, namespace []string) error {
return m.handler.checkVectorNamespaceWrite(ctx, filerClient, bucketName, namespace)
}