Files
seaweedfs/weed/s3api/s3vectors/authorize_lookups_test.go
T
Nguyễn Lộc PhúcandClaude Opus 5.5 cf3eda7e57 perf(s3vectors): read the bucket once per authorization (#13427)
s3vectors: read the bucket once per authorization

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 23:54:04 -08:00

66 lines
2.5 KiB
Go

package s3vectors
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
)
// Authorizing a vector request needs the bucket's record, its s3vectors policy
// and its tags, all attributes of the one bucket entry. The check reads the
// record through the catalog's GetTableBucket and then the entry again for the
// policy and tags, three lookups of the same entry, and GetVectorBucketPolicy
// reads it a fourth time for the policy it returns. One lookup is enough.
func TestVectorAuthorizationLooksUpTheBucketOnce(t *testing.T) {
fs := s3tablestest.Start(t)
s := NewServer(s3tables.NewManagerClient(fs.Client), allowAllAuth{}, newFakeVectors(0))
if w := postJSONAs(t, "", s.handleCreateVectorBucket, &CreateVectorBucketRequest{VectorBucketName: "vec-bucket"}); w.Code != http.StatusOK {
t.Fatalf("create bucket: %d %s", w.Code, w.Body.String())
}
const policy = `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3vectors:*","Resource":"*"}]}`
if w := postJSONAs(t, "", s.handlePutVectorBucketPolicy, &PutVectorBucketPolicyRequest{VectorBucketName: "vec-bucket", Policy: policy}); w.Code != http.StatusOK {
t.Fatalf("put policy: %d %s", w.Code, w.Body.String())
}
dir, name := splitEntryPath(s3tables.GetTableBucketPath("vec-bucket"))
cases := []struct {
name string
call func() int
}{
{"authorizeVector", func() int {
r := httptest.NewRequest(http.MethodPost, "/", nil)
r = r.WithContext(contextWithIdentity(r.Context(), ""))
if _, err := s.authorizeVector(r.Context(), r, "s3vectors:GetVectorBucket", "vec-bucket", ""); err != nil {
t.Fatalf("authorize: %v", err)
}
return http.StatusOK
}},
{"GetVectorBucket", func() int {
return postJSONAs(t, "", s.handleGetVectorBucket, &GetVectorBucketRequest{VectorBucketName: "vec-bucket"}).Code
}},
{"GetVectorBucketPolicy", func() int {
return postJSONAs(t, "", s.handleGetVectorBucketPolicy, &GetVectorBucketPolicyRequest{VectorBucketName: "vec-bucket"}).Code
}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lookups := 0
fs.OnLookup = func(d, n string) {
if d == dir && n == name {
lookups++
}
}
defer func() { fs.OnLookup = nil }()
if code := tc.call(); code != http.StatusOK {
t.Fatalf("%s = %d", tc.name, code)
}
if lookups != 1 {
t.Errorf("%s looked up the bucket %d times, want 1", tc.name, lookups)
}
})
}
}