mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* s3tables: let DirHoldsFiles count only files older than a cutoff A non-zero cutoff skips files created at or after it, and Iceberg metadata files directly under the walked directory's metadata/ folder, which a create stages before its record exists. Existing callers pass the zero time and keep counting every file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: keep files a deleted table or view did not write DeleteTable purged the table's directory (or its decoupled data location) recursively, and DeleteView purged the view's directory, so files that were already there when the record was created, or that were written under a view, went with it. Those files now stay: the record's attributes are cleared and the directory is left as a leftover. A table still purges the files it wrote after its create, and its staged metadata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: skip the drop purge of a location holding older files DropTable and DropView purge the dropped entry's location after the catalog delete. A location that holds files older than the table, or any file other than metadata for a view, is left alone, matching DeleteTable and DeleteView. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: count a file from the record's own second as older File creation times have second granularity, so an upload made in the same second as the table's create compared equal and was purged. An equal second now counts as older, and the view cutoff no longer needs the extra second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: count S3 objects stored on directories in DirHoldsFiles A trailing-slash key, or a file promoted to a directory by a child write, is an S3 object on a directory entry. The walk only counted plain files, so a delete purged such objects, including one on the purged path itself. They now count like files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: keep every non-metadata file under a deleted view A view writes nothing but its metadata files, so a file under it counts whatever its creation time, including one stamped ahead of this clock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: count a file from the record's own second as the record's File times are whole seconds, and a writer's first file (an insert right after a create, or a CTAS writing in the second it staged) often shares the record's second. Counting it as older kept the table's own files on drop, so a re-create at the location found it non-empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: date a staged create's table from its stage The engine writes a staged create's first files before the commit registers the table, so they predate its record and a drop kept them. The commit now passes the time the stage marker recorded, in process only, and the table's CreatedAt starts there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: say a view name holds plain files instead of a table A view deleted over files it did not write leaves its directory without a catalog record. CreateView reported that directory as a table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: keep a view location's metadata files older than the view A view writes its metadata only after its record, so a metadata file from an earlier second at the location is not the view's and keeps the directory on a drop, like any other file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: test that a view drop keeps older metadata at its location Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: date both view drop test files from one record time Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: drop only the table or view record the lookup read Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: test a drop whose record is replaced after the lookup Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: check the location claim again after the drop scan Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iceberg: test a location claimed while the drop scans it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
263 lines
9.4 KiB
Go
263 lines
9.4 KiB
Go
package s3tables
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
)
|
|
|
|
// Manager provides reusable S3 Tables operations for shell/admin without HTTP routing.
|
|
type Manager struct {
|
|
handler *S3TablesHandler
|
|
}
|
|
|
|
// NewManager creates a new Manager.
|
|
func NewManager() *Manager {
|
|
m := &Manager{handler: NewS3TablesHandler()}
|
|
// Default to allowing access when IAM is not configured
|
|
m.handler.SetDefaultAllow(true)
|
|
return m
|
|
}
|
|
|
|
// SetRegion sets the AWS region for ARN generation.
|
|
func (m *Manager) SetRegion(region string) {
|
|
m.handler.SetRegion(region)
|
|
}
|
|
|
|
// SetAccountID sets the AWS account ID for ARN generation.
|
|
func (m *Manager) SetAccountID(accountID string) {
|
|
m.handler.SetAccountID(accountID)
|
|
}
|
|
|
|
// SetDefaultAllow sets whether to allow access by default.
|
|
func (m *Manager) SetDefaultAllow(allow bool) {
|
|
m.handler.SetDefaultAllow(allow)
|
|
}
|
|
|
|
// SetTrusted lets trusted local tooling (shell, admin console) bypass authorization.
|
|
func (m *Manager) SetTrusted(trusted bool) {
|
|
m.handler.SetTrusted(trusted)
|
|
}
|
|
|
|
// SetLocalAccountChecker sets the handler's configured local account predicate.
|
|
func (m *Manager) SetLocalAccountChecker(fn func(string) bool) {
|
|
m.handler.SetLocalAccountChecker(fn)
|
|
}
|
|
|
|
// SetTrustedIssuerSource sets the handler's configured OIDC issuer source.
|
|
func (m *Manager) SetTrustedIssuerSource(fn func(context.Context) []string) {
|
|
m.handler.SetTrustedIssuerSource(fn)
|
|
}
|
|
|
|
// SetEncryptionChecker sets the handler's SSE-S3 availability predicate.
|
|
func (m *Manager) SetEncryptionChecker(fn func() bool) {
|
|
m.handler.SetEncryptionChecker(fn)
|
|
}
|
|
|
|
// PrincipalFor resolves the caller of r the way the catalog's handlers do,
|
|
// so it matches the owner CreateTableBucket records. A nil request is the
|
|
// handler's account ID.
|
|
func (m *Manager) PrincipalFor(r *http.Request) string {
|
|
if r == nil {
|
|
return m.handler.accountID
|
|
}
|
|
return m.handler.getAccountID(r)
|
|
}
|
|
|
|
// PolicyContext builds the evaluation context a Manager call's synthesized
|
|
// request produces: the configured local-account predicate, the caller's own
|
|
// identity actions so admin and action grants resolve on privileges rather
|
|
// than the forwarded name, plus the open-by-default fallback only for a
|
|
// trusted caller or no resolved identity. A non-empty bucket lets an identity
|
|
// action scoped to that bucket apply, as the handlers do.
|
|
func (m *Manager) PolicyContext(r *http.Request, bucket string) *PolicyContext {
|
|
ctx := &PolicyContext{IsLocalAccountID: m.handler.localAccountIDs, IdentityActions: getIdentityActions(r),
|
|
TrustedIssuers: m.handler.trustedIssuers(r), Subject: callerSubject(r), TableBucketName: bucket}
|
|
if m.handler.trusted || (m.handler.defaultAllow && s3_constants.GetIdentityNameFromContext(r) == "") {
|
|
ctx.DefaultAllow = true
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
type tableBucketCreatedAtKey struct{}
|
|
|
|
// WithTableBucketCreatedAt binds a DeleteTableBucket, CreateNamespace,
|
|
// DeleteNamespace, CreateTable, DeleteTable or tag operation run through
|
|
// Execute to the bucket created at createdAt, so a caller that authorized the
|
|
// bucket itself never writes into one created again under its name.
|
|
// In-process only: no wire request can carry it.
|
|
func WithTableBucketCreatedAt(ctx context.Context, createdAt time.Time) context.Context {
|
|
return context.WithValue(ctx, tableBucketCreatedAtKey{}, createdAt)
|
|
}
|
|
|
|
type tableStagedAtKey struct{}
|
|
|
|
// WithTableStagedAt dates a CreateTable run through Execute from stagedAt, when
|
|
// a staged create handed out the table's location, so the files the engine
|
|
// wrote there before the commit are the table's. In-process only, like
|
|
// WithTableBucketCreatedAt.
|
|
func WithTableStagedAt(ctx context.Context, stagedAt time.Time) context.Context {
|
|
return context.WithValue(ctx, tableStagedAtKey{}, stagedAt)
|
|
}
|
|
|
|
// boundTableBucket returns the bucket creation time ctx binds the request to.
|
|
func boundTableBucket(ctx context.Context) (time.Time, bool) {
|
|
createdAt, ok := ctx.Value(tableBucketCreatedAtKey{}).(time.Time)
|
|
return createdAt, ok
|
|
}
|
|
|
|
// otherTableBucket reports whether ctx binds the request to a bucket other
|
|
// than the one created at createdAt.
|
|
func otherTableBucket(ctx context.Context, createdAt time.Time) bool {
|
|
want, ok := boundTableBucket(ctx)
|
|
return ok && !createdAt.Equal(want)
|
|
}
|
|
|
|
// Execute runs an S3 Tables operation and decodes the response into resp (if provided).
|
|
func (m *Manager) Execute(ctx context.Context, filerClient FilerClient, operation string, req interface{}, resp interface{}, identity string) error {
|
|
body, err := json.Marshal(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
httpReq, err := newManagerRequest(ctx, operation, body, identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recorder := httptest.NewRecorder()
|
|
m.handler.HandleRequest(recorder, httpReq, filerClient)
|
|
return decodeS3TablesHTTPResponse(recorder, resp)
|
|
}
|
|
|
|
// AuthorizeCreateTable checks that identity may create the table the request
|
|
// describes, without creating anything. A deferred create (Iceberg
|
|
// stage-create) writes into the table bucket long before it registers the
|
|
// table, so it passes this gate first.
|
|
func (m *Manager) AuthorizeCreateTable(ctx context.Context, filerClient FilerClient, req *CreateTableRequest, identity string) error {
|
|
httpReq, err := newManagerRequest(ctx, "CreateTable", nil, identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recorder := httptest.NewRecorder()
|
|
target, authErr := m.handler.authorizeCreateTable(recorder, httpReq, filerClient, req.TableBucketARN, req.Namespace, req.Name, req.Tags, req.AllowMissingNamespace)
|
|
if authErr == nil {
|
|
if err := m.handler.checkVectorNamespaceWrite(ctx, filerClient, target.bucketName, req.Namespace); err != nil {
|
|
return err
|
|
}
|
|
if req.MetadataLocation == "" {
|
|
return nil
|
|
}
|
|
tablePath := GetTablePath(target.bucketName, target.namespaceName, target.tableName)
|
|
if authErr = ValidateMetadataLocation(req.MetadataLocation, target.bucketName, target.namespaceName); authErr != nil {
|
|
m.handler.writeError(recorder, http.StatusBadRequest, ErrCodeInvalidRequest, authErr.Error())
|
|
} else if authErr = m.handler.refuseClaimedLocation(recorder, httpReq, filerClient, target.bucketName, tablePath, CatalogRecordRoot(target.bucketName, tablePath, req.MetadataLocation, "")); authErr == nil {
|
|
return nil
|
|
}
|
|
}
|
|
if decoded := decodeS3TablesHTTPResponse(recorder, nil); decoded != nil {
|
|
return decoded
|
|
}
|
|
return authErr
|
|
}
|
|
|
|
// AuthorizeUpdateTable checks that identity may update the table the request
|
|
// describes, without updating anything. Credential vending gates on the same
|
|
// permission a commit needs.
|
|
func (m *Manager) AuthorizeUpdateTable(ctx context.Context, filerClient FilerClient, req *UpdateTableRequest, identity string) error {
|
|
httpReq, err := newManagerRequest(ctx, "UpdateTable", nil, identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recorder := httptest.NewRecorder()
|
|
_, authErr := m.handler.authorizeUpdateTable(recorder, httpReq, filerClient, req)
|
|
if authErr == nil {
|
|
return nil
|
|
}
|
|
if decoded := decodeS3TablesHTTPResponse(recorder, nil); decoded != nil {
|
|
return decoded
|
|
}
|
|
return authErr
|
|
}
|
|
|
|
// AuthorizeUpdateView checks that identity may update the view the request
|
|
// describes, without updating anything.
|
|
func (m *Manager) AuthorizeUpdateView(ctx context.Context, filerClient FilerClient, req *UpdateViewRequest, identity string) error {
|
|
httpReq, err := newManagerRequest(ctx, "UpdateView", nil, identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recorder := httptest.NewRecorder()
|
|
_, authErr := m.handler.authorizeUpdateView(recorder, httpReq, filerClient, req)
|
|
if authErr == nil {
|
|
return nil
|
|
}
|
|
if decoded := decodeS3TablesHTTPResponse(recorder, nil); decoded != nil {
|
|
return decoded
|
|
}
|
|
return authErr
|
|
}
|
|
|
|
func newManagerRequest(ctx context.Context, operation string, body []byte, identity string) (*http.Request, error) {
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/x-amz-json-1.1")
|
|
httpReq.Header.Set("X-Amz-Target", "S3Tables."+operation)
|
|
if identity != "" {
|
|
httpReq.Header.Set(s3_constants.AmzAccountId, identity)
|
|
httpReq = httpReq.WithContext(s3_constants.SetIdentityNameInContext(httpReq.Context(), identity))
|
|
}
|
|
return httpReq, nil
|
|
}
|
|
|
|
func decodeS3TablesHTTPResponse(recorder *httptest.ResponseRecorder, resp interface{}) error {
|
|
result := recorder.Result()
|
|
defer result.Body.Close()
|
|
data, err := io.ReadAll(result.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.StatusCode >= http.StatusBadRequest {
|
|
var errResp S3TablesError
|
|
if len(data) > 0 {
|
|
if jsonErr := json.Unmarshal(data, &errResp); jsonErr == nil && (errResp.Type != "" || errResp.Message != "") {
|
|
return &errResp
|
|
}
|
|
}
|
|
return &S3TablesError{Type: ErrCodeInternalError, Message: string(bytes.TrimSpace(data))}
|
|
}
|
|
if resp == nil || len(data) == 0 {
|
|
return nil
|
|
}
|
|
if err := json.Unmarshal(data, resp); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ManagerClient adapts a SeaweedFilerClient to the FilerClient interface.
|
|
type ManagerClient struct {
|
|
client filer_pb.SeaweedFilerClient
|
|
}
|
|
|
|
// NewManagerClient wraps a filer client.
|
|
func NewManagerClient(client filer_pb.SeaweedFilerClient) *ManagerClient {
|
|
return &ManagerClient{client: client}
|
|
}
|
|
|
|
// WithFilerClient implements FilerClient.
|
|
func (m *ManagerClient) WithFilerClient(streamingMode bool, fn func(client filer_pb.SeaweedFilerClient) error) error {
|
|
if m.client == nil {
|
|
return errors.New("nil filer client")
|
|
}
|
|
return fn(m.client)
|
|
}
|