mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-02 18:22:40 +01:00
* security: share the filer admin token check CheckGrpcAdminPeer verifies an admin Bearer token inline. Move the check into IsFilerAdminToken so an HTTP endpoint can accept the same token. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: recognize the cluster's own components by a signed token A request whose X-Seaweedfs-Cluster-Token header carries an admin token signed with jwt.filer_signing.key comes from a component that holds the cluster key: the vector engine or the Lance maintenance worker. Mark such a request as trusted. It still authenticates as an identity, which stays the principal for vended credentials. A dedicated header leaves the Authorization header to Lance OAuth clients. A gateway with no key trusts no one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: vend write credentials to a vector bucket's indexes only to trusted callers An index in a vector bucket's vector namespace is also a Lance table, so a caller granted s3tables:UpdateTable could describe it, or declare it again, and get credentials that write the index data without passing the vector bucket policy. In that namespace both now vend read-only credentials unless the caller is trusted, and a declare from anyone else no longer writes the dataset markers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance worker: prove the cluster key to the Lance namespace The gateway now vends write credentials to a vector bucket's indexes only to callers that send a token signed with jwt.filer_signing.key. The worker already takes that key for the admin stream; its namespace client now mints a token from it for every catalog request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * weed-vector: describe the index the gateway created and prove the cluster key CreateIndex declared a table the gateway had just created, which only worked because declare is idempotent. Describe it instead. The Lance namespace vends write credentials to a vector bucket's indexes only to callers that send a token signed with jwt.filer_signing.key, so take the key with --filer-signing-key, the same flag the Lance worker has. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance worker: keep the cluster key out of Debug output Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: answer a declare of an existing table with its own location CreateTable succeeds for a name that already exists without looking at the location requested, and declare kept using that location for the dataset markers and the vended credentials. Declaring an existing table with another table's location, such as a vector index, got credentials to that table's data. Use the location the catalog holds, which CreateTable now also reports for a new table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: sign cluster tokens with a key derived for the catalog A cluster token was a filer admin token signed with jwt.filer_signing.key and, by default, never expired. Sent to the Lance namespace on every request, often over plain HTTP, a captured one would also authorize the filer's IAM service. Sign it with a key derived from the cluster key for this one use, which the admin services do not accept, and mint it with a five minute lifetime. weed-vector drops its token lifetime flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: follow a reloaded jwt.filer_signing.key The Lance namespace copied the cluster key at startup. A SIGHUP reload refreshes the gateway's filer guard but not that copy, so after a key rotation the restarted vector engine and worker were no longer trusted. Read the key from the guard on every check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: keep a vector bucket's indexes read-only An index in a vector bucket is also a catalog table the Iceberg REST catalog loads, and LoadTable vended credentials that could write it to any caller granted s3tables:UpdateTable. A commit staged its metadata file in the index directory on the same grant. Apply the vector namespace rule to both, after the caller's own UpdateTable check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: confine a created table's location to its namespace before writing Stage-create skips the registration that confines a location to the table's namespace, yet vends write credentials for that location. A caller allowed to create a table in any namespace of a bucket could name another namespace's prefix, a vector index included, and get write credentials to it. The non-staged create also wrote its first metadata file there before the registration refused the location. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * lance: a declare that finds an existing table answers like describe Creating a name that already exists is idempotent, so a caller allowed only to create tables could declare someone else's table and get write credentials to it, and clear its deregistered marker. Describe would have vended that caller read-only credentials. A declare now looks the name up first; when it only finds the table, writing needs UpdateTable, as it does for describe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
7.0 KiB
Go
166 lines
7.0 KiB
Go
package lance
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
)
|
|
|
|
// seedVectorIndex creates bucket/vector/idx through the trusted path, the way
|
|
// the S3 Vectors gateway does before it asks the engine to build the index.
|
|
func (h *testHarness) seedVectorIndex(t *testing.T, bucket, format string) {
|
|
t.Helper()
|
|
h.createBucket(t, bucket, format)
|
|
client := s3tables.NewManagerClient(h.filer.Client)
|
|
for _, op := range []struct {
|
|
name string
|
|
req interface{}
|
|
}{
|
|
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: h.bucketARN(t, bucket), Namespace: []string{s3tables.VectorIndexNamespace}}},
|
|
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: h.bucketARN(t, bucket), Namespace: []string{"other"}}},
|
|
{"CreateTable", &s3tables.CreateTableRequest{TableBucketARN: h.bucketARN(t, bucket), Namespace: []string{s3tables.VectorIndexNamespace},
|
|
Name: "idx", Format: s3tables.FormatLance}},
|
|
} {
|
|
if err := h.admin.Execute(t.Context(), client, op.name, op.req, nil, ""); err != nil {
|
|
t.Fatalf("%s: %v", op.name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Through the Lance namespace the vector namespace of a vector bucket is
|
|
// read-only too, while the engine's declare of an index the gateway already
|
|
// created keeps working.
|
|
func TestVectorNamespaceIsReadOnlyOverLance(t *testing.T) {
|
|
h := newTestHarness(t)
|
|
h.seedVectorIndex(t, "vec", s3tables.FormatLance)
|
|
idx := "/v1/table/vec$vector$idx"
|
|
|
|
h.mustDo(t, http.MethodPost, idx+"/declare", `{"id":["vec","vector","idx"]}`, http.StatusOK)
|
|
h.mustDo(t, http.MethodPost, idx+"/describe", `{}`, http.StatusOK)
|
|
|
|
for _, tc := range []struct{ name, target, body string }{
|
|
{"declare a new index", "/v1/table/vec$vector$fresh/declare", `{}`},
|
|
{"register a new index", "/v1/table/vec$vector$fresh/register", `{"location":"s3://vec/vector/fresh"}`},
|
|
{"register over the index", idx + "/register", `{"location":"s3://vec/vector/idx","mode":"overwrite"}`},
|
|
{"repoint the index", idx + "/register", `{"location":"s3://vec/vector/elsewhere","mode":"overwrite"}`},
|
|
{"deregister", idx + "/deregister", `{}`},
|
|
{"drop", idx + "/drop", `{}`},
|
|
{"rename out", idx + "/rename", `{"new_id":["vec","other","moved"]}`},
|
|
{"drop the namespace", "/v1/namespace/vec$vector/drop", `{}`},
|
|
{"overwrite the namespace", "/v1/namespace/vec$vector/create", `{"mode":"overwrite"}`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h.mustDo(t, http.MethodPost, tc.target, tc.body, http.StatusForbidden)
|
|
})
|
|
}
|
|
|
|
if h.filer.Get("/buckets/vec/vector/idx", deregisteredMarker) != nil {
|
|
t.Fatal("a refused deregister left its marker")
|
|
}
|
|
h.mustDo(t, http.MethodPost, idx+"/exists", `{}`, http.StatusOK)
|
|
|
|
h.createBucket(t, "fresh", s3tables.FormatLance)
|
|
h.mustDo(t, http.MethodPost, "/v1/namespace/fresh$vector/create", `{}`, http.StatusForbidden)
|
|
}
|
|
|
|
// A bucket from before formats existed is not a vector bucket here, so its
|
|
// namespace named vector keeps the ordinary Lance lifecycle.
|
|
func TestVectorNamespaceOfUndeclaredBucketIsWritableOverLance(t *testing.T) {
|
|
h := newTestHarness(t)
|
|
h.seedVectorIndex(t, "old", "")
|
|
idx := "/v1/table/old$vector$idx"
|
|
|
|
h.mustDo(t, http.MethodPost, idx+"/deregister", `{}`, http.StatusOK)
|
|
h.mustDo(t, http.MethodPost, idx+"/register", `{"location":"s3://old/vector/idx"}`, http.StatusOK)
|
|
h.mustDo(t, http.MethodPost, idx+"/drop", `{}`, http.StatusOK)
|
|
}
|
|
|
|
// recordingVendor vends a credential whose key says whether it may write and
|
|
// whose secret is the prefix it covers.
|
|
type recordingVendor struct{}
|
|
|
|
func (recordingVendor) VendTableCredentials(_ context.Context, _, _, prefix string, readOnly bool) (*VendedCredentials, error) {
|
|
if readOnly {
|
|
return &VendedCredentials{AccessKeyID: "read-only", SecretAccessKey: prefix}, nil
|
|
}
|
|
return &VendedCredentials{AccessKeyID: "read-write", SecretAccessKey: prefix}, nil
|
|
}
|
|
|
|
func (h *testHarness) vendedKey(t *testing.T, target, token string) string {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(`{"vend_credentials":true}`))
|
|
if token != "" {
|
|
req.Header.Set(clusterTokenHeader, token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.router.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("POST %s = %d (%s)", target, rec.Code, rec.Body.String())
|
|
}
|
|
return decode[DescribeTableResponse](t, rec).StorageOptions["aws_access_key_id"]
|
|
}
|
|
|
|
// A caller holding every s3tables grant still gets read-only credentials to a
|
|
// vector bucket's index data; only the cluster's own components can write it.
|
|
func TestVectorNamespaceVendsWriteCredentialsOnlyToTrustedCallers(t *testing.T) {
|
|
h := newTestHarness(t)
|
|
key := security.SigningKey("cluster-key")
|
|
h.server.SetSigningKey(func() security.SigningKey { return key })
|
|
h.server.SetCredentialVendor(recordingVendor{})
|
|
h.seedVectorIndex(t, "vec", s3tables.FormatLance)
|
|
h.mustDo(t, http.MethodPost, "/v1/table/vec$other$tbl/declare", `{}`, http.StatusOK)
|
|
h.seedVectorIndex(t, "old", "")
|
|
|
|
h.vendedKey(t, "/v1/table/vec$vector$idx/declare", "")
|
|
if h.filer.Get("/buckets/vec/vector/idx", reservedMarker) != nil {
|
|
t.Error("an untrusted declare wrote into the index")
|
|
}
|
|
|
|
trusted := string(security.GenJwtForFilerAdmin(clusterTokenKey(key), 60))
|
|
forged := string(security.GenJwtForFilerAdmin(key, 60))
|
|
for _, tc := range []struct {
|
|
name, target, token, want string
|
|
}{
|
|
{"describe the index", "/v1/table/vec$vector$idx/describe", "", "read-only"},
|
|
{"declare the index", "/v1/table/vec$vector$idx/declare", "", "read-only"},
|
|
{"describe with an admin token", "/v1/table/vec$vector$idx/describe", forged, "read-only"},
|
|
{"trusted describe", "/v1/table/vec$vector$idx/describe", trusted, "read-write"},
|
|
{"trusted declare", "/v1/table/vec$vector$idx/declare", trusted, "read-write"},
|
|
{"another namespace", "/v1/table/vec$other$tbl/describe", "", "read-write"},
|
|
{"bucket from before formats", "/v1/table/old$vector$idx/describe", "", "read-write"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := h.vendedKey(t, tc.target, tc.token); got != tc.want {
|
|
t.Errorf("vended %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Declaring a name that already exists answers for that table, so naming the
|
|
// index's location on another table gets no credentials to the index.
|
|
func TestDeclareOfAnExistingTableKeepsItsLocation(t *testing.T) {
|
|
h := newTestHarness(t)
|
|
h.server.SetCredentialVendor(recordingVendor{})
|
|
h.seedVectorIndex(t, "vec", s3tables.FormatLance)
|
|
h.mustDo(t, http.MethodPost, "/v1/table/vec$other$tbl/declare", `{}`, http.StatusOK)
|
|
|
|
rec := h.mustDo(t, http.MethodPost, "/v1/table/vec$other$tbl/declare",
|
|
`{"location":"s3://vec/vector/idx","vend_credentials":true}`, http.StatusOK)
|
|
resp := decode[DeclareTableResponse](t, rec)
|
|
if resp.Location != "s3://vec/other/tbl" {
|
|
t.Errorf("location = %q, want the table's own s3://vec/other/tbl", resp.Location)
|
|
}
|
|
if got := resp.StorageOptions["aws_secret_access_key"]; got != "other/tbl" {
|
|
t.Errorf("credentials cover %q, want other/tbl", got)
|
|
}
|
|
if h.filer.Get("/buckets/vec/vector/idx", reservedMarker) != nil {
|
|
t.Error("the declare wrote into the index")
|
|
}
|
|
}
|