mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* s3api: refuse a directory marker PUT when the existing marker cannot be read Only a missing marker skips the Object Lock check. Any other lookup error now fails the request instead of letting mkdir replace a marker that may be under retention or legal hold. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: create the Extended map before applying upload ACL metadata A directory extracted from a zip without an owner header reached applyPutObjectACL with a nil Extended map and panicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: refuse extracting over a marker that cannot be read The zip extract mkdir path had the same fail-open lookup as the directory marker PUT; a failed read now reports InternalError for the member. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
255 lines
8.3 KiB
Go
255 lines
8.3 KiB
Go
package s3api
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func makeZipArchive(t *testing.T, members map[string]string) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
for name, content := range members {
|
|
w, err := zw.Create(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write([]byte(content)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func newExtractTestServer(t *testing.T) (*S3ApiServer, *ambiguousPutFiler) {
|
|
t.Helper()
|
|
volume := startFakeVolumeServer(t)
|
|
filerImpl := &ambiguousPutFiler{volume: volume, entries: map[string]*filer_pb.Entry{}, apply: true}
|
|
s3a := newPutTestServer(t, startFakeFiler(t, filerImpl))
|
|
s3a.iam = NewIdentityAccessManagementWithStore(s3a.option, nil, "memory")
|
|
t.Cleanup(s3a.iam.Shutdown)
|
|
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
|
|
s3a.bucketConfigCache.Set("b", &BucketConfig{Name: "b"})
|
|
return s3a, filerImpl
|
|
}
|
|
|
|
func TestIsExtractZip(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
setup func(r *http.Request)
|
|
want bool
|
|
}{
|
|
{"none", func(r *http.Request) {}, false},
|
|
{"query", func(r *http.Request) {
|
|
q := r.URL.Query()
|
|
q.Set("extract", "zip")
|
|
r.URL.RawQuery = q.Encode()
|
|
}, true},
|
|
{"seaweed header", func(r *http.Request) {
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
}, true},
|
|
{"snowball header", func(r *http.Request) {
|
|
r.Header.Set(s3_constants.AmzMetaSnowballAutoExtract, "true")
|
|
}, true},
|
|
{"snowball false", func(r *http.Request) {
|
|
r.Header.Set(s3_constants.AmzMetaSnowballAutoExtract, "false")
|
|
}, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodPut, "/b/o", nil)
|
|
tc.setup(r)
|
|
if got := isExtractZip(r); got != tc.want {
|
|
t.Fatalf("isExtractZip = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractZipPutObject(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
|
|
body := makeZipArchive(t, map[string]string{
|
|
"a.txt": "hello",
|
|
"sub/b.txt": "world!",
|
|
})
|
|
r := httptest.NewRequest(http.MethodPut, "/b/dir/archive.zip", bytes.NewReader(body))
|
|
r.Header.Set(s3_constants.AmzMetaSnowballAutoExtract, "true")
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "dir/archive.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
|
}
|
|
if got := rec.Header().Get(s3_constants.SeaweedFSExtractedCount); got != "2" {
|
|
t.Fatalf("extracted count = %q, want 2", got)
|
|
}
|
|
var result extractZipResult
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(result.Errors) != 0 {
|
|
t.Fatalf("unexpected member errors: %v", result.Errors)
|
|
}
|
|
for _, want := range []string{"/buckets/b/dir/a.txt", "/buckets/b/dir/sub/b.txt"} {
|
|
entry, ok := filerImpl.entries[want]
|
|
if !ok {
|
|
t.Fatalf("member %s was not created; entries: %v", want, filerImpl.entries)
|
|
}
|
|
if entry.IsDirectory {
|
|
t.Fatalf("member %s stored as directory", want)
|
|
}
|
|
}
|
|
if _, ok := filerImpl.entries["/buckets/b/dir/archive.zip"]; ok {
|
|
t.Fatal("archive object itself should not be stored")
|
|
}
|
|
}
|
|
|
|
func TestExtractZipPutObjectArchiveChecksum(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
|
|
body := makeZipArchive(t, map[string]string{"a.txt": "hello"})
|
|
md5sum := md5.Sum(body)
|
|
|
|
r := httptest.NewRequest(http.MethodPut, "/b/dir/archive.zip", bytes.NewReader(body))
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
r.Header.Set("Content-Md5", base64.StdEncoding.EncodeToString(md5sum[:]))
|
|
r.Header.Set(s3_constants.AmzChecksumSHA256, base64.StdEncoding.EncodeToString([]byte("not-the-sha256")))
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "dir/archive.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400", rec.Code)
|
|
}
|
|
if len(filerImpl.entries) != 0 {
|
|
t.Fatalf("entries created despite checksum mismatch: %v", filerImpl.entries)
|
|
}
|
|
}
|
|
|
|
func TestExtractZipPutObjectGoodArchiveMd5(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
|
|
body := makeZipArchive(t, map[string]string{"a.txt": "hello"})
|
|
md5sum := md5.Sum(body)
|
|
|
|
r := httptest.NewRequest(http.MethodPut, "/b/dir/archive.zip", bytes.NewReader(body))
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
r.Header.Set("Content-Md5", base64.StdEncoding.EncodeToString(md5sum[:]))
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "dir/archive.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
|
}
|
|
if _, ok := filerImpl.entries["/buckets/b/dir/a.txt"]; !ok {
|
|
t.Fatalf("member not created; entries: %v", filerImpl.entries)
|
|
}
|
|
}
|
|
|
|
func TestExtractZipPutObjectNotAZip(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
|
|
r := httptest.NewRequest(http.MethodPut, "/b/dir/archive.zip", bytes.NewReader([]byte("not a zip")))
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "dir/archive.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400", rec.Code)
|
|
}
|
|
if len(filerImpl.entries) != 0 {
|
|
t.Fatalf("entries created for invalid zip: %v", filerImpl.entries)
|
|
}
|
|
}
|
|
|
|
func TestExtractZipPutObjectRejectsExplicitSSE(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
|
|
body := makeZipArchive(t, map[string]string{"a.txt": "hello"})
|
|
r := httptest.NewRequest(http.MethodPut, "/b/dir/archive.zip", bytes.NewReader(body))
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
r.Header.Set(s3_constants.AmzServerSideEncryption, "aws:kms")
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "dir/archive.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400", rec.Code)
|
|
}
|
|
if len(filerImpl.entries) != 0 {
|
|
t.Fatalf("entries created despite explicit SSE headers: %v", filerImpl.entries)
|
|
}
|
|
}
|
|
|
|
func TestExtractZipOverLockedMarkerFailsClosedOnLookupError(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
absent bool
|
|
lookupErr error
|
|
wantErr []string
|
|
}{
|
|
{name: "absent", absent: true},
|
|
{name: "locked", wantErr: []string{"zipdir: AccessDenied"}},
|
|
{name: "lookup error", lookupErr: status.Error(codes.Unavailable, "filer unavailable"), wantErr: []string{"zipdir: InternalError"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s3a, filerImpl := newExtractTestServer(t)
|
|
s3a.bucketConfigCache.Set("b", &BucketConfig{
|
|
Name: "b",
|
|
Versioning: s3_constants.VersioningEnabled,
|
|
ObjectLockConfig: &ObjectLockConfiguration{ObjectLockEnabled: s3_constants.ObjectLockEnabled},
|
|
})
|
|
locked := &filer_pb.Entry{
|
|
Name: "zipdir",
|
|
IsDirectory: true,
|
|
Attributes: &filer_pb.FuseAttributes{Mime: "locked"},
|
|
Extended: map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)},
|
|
}
|
|
if !tc.absent {
|
|
filerImpl.entries["/buckets/b/zipdir"] = locked
|
|
}
|
|
filerImpl.lookupErr, filerImpl.lookupFailKey = tc.lookupErr, "/buckets/b/zipdir"
|
|
|
|
body := makeZipArchive(t, map[string]string{"zipdir/": ""})
|
|
r := httptest.NewRequest(http.MethodPut, "/b/upload.zip", bytes.NewReader(body))
|
|
r.Header.Set(s3_constants.SeaweedFSExtract, "zip")
|
|
rec := httptest.NewRecorder()
|
|
s3a.extractZipObject(rec, r, "b", "upload.zip", r.Body)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var result extractZipResult
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(result.Errors, tc.wantErr) {
|
|
t.Fatalf("errors = %v, want %v", result.Errors, tc.wantErr)
|
|
}
|
|
filerImpl.mu.Lock()
|
|
defer filerImpl.mu.Unlock()
|
|
entry := filerImpl.entries["/buckets/b/zipdir"]
|
|
if entry == nil || !entry.IsDirectory {
|
|
t.Fatalf("marker missing: %+v", entry)
|
|
}
|
|
if !tc.absent && (entry.Attributes.Mime != "locked" || string(entry.Extended[s3_constants.ExtLegalHoldKey]) != s3_constants.LegalHoldOn) {
|
|
t.Fatalf("locked marker replaced: %+v", entry)
|
|
}
|
|
})
|
|
}
|
|
}
|