mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* util: one admission rule for upload limits * operation: mark gateway fan-out holder writes as client traffic * volume: bound upload admission, limit gateway holder writes, 503 on timeout * volume: charge unknown-length uploads half the budget, document client wait cap * s3, filer: admit uploads by inFlight+size and charge unknown lengths a chunk * s3: answer a volume overload as SlowDown with Retry-After * operation: cut an upload whose body crawls below the minimum rate on any host * operation: make the slow-commit deadline test honour the request context * s3: stop chunk uploads when the client goes away * operation: treat a body deadline cut as back-pressure, not a same-holder retry A body that crawls past the body deadline returns from retriedUploadData at once, like an overload, and counts toward the single reassign the chunk loop and the filer retry gate give back-pressure. A watchdog cut keeps its retry on a fresh conn. Both still match ErrUploadStalled and stay reassignable. * operation: apply the body deadline only to uploads that opt in UploadOption.BodyDeadline and ChunkedUploadOption.BodyDeadline turn the body deadline on; S3 chunk uploads (relay and fan-out) and the filer's chunk loop set it. Mount, filer.sync, filer.copy and UploadWithRetry keep the stall watchdog only, as before the deadline existed. * s3, operation: check a chunk cancelled with its client is deleted from every holder A volume commits on a context that ignores the cancel, so a chunk whose caller went away after sending the body may still land. The chunk loop already deletes the fid from every holder after a failed attempt; pin that for the relay and fan-out paths and for an S3 PUT whose client goes away. * s3: count a request's declared bytes in the circuit breaker byte limits The per-bucket and global byte counters go back to the declared length (an unknown length counts one chunk), so a bucket bytes limit still rejects a single PUT larger than it. Only the upload budget uses the capped gateway charge. * s3: answer copy chunk overloads as SlowDown, tidy the client-gone checks CopyObject and UploadPartCopy map a volume overload or an exhausted stall to SlowDown, as PutObject does. The client-gone log check matches the mapper's context.Canceled test, the truncated-body case drops a check the first case already makes, and the Retry-After test reads the headers actually sent. * filer, volume: clamp the filer charge chunk, set the upload gauge under the lock A filer with -maxMB 0 charges its default chunk size instead of nothing. The volume server sets its in-flight upload gauge inside the critical section. The upload limit tests wait until the waiter is parked before releasing, check handleUploadRequest gives its charge back, and use CI-safe bounds. * volume: cap the Rust server's client upload-slot wait at 5s Same cap as the Go volume server's volumeClientUploadWaitCap, so a gateway's body deadline never fires before the 503. * operation: reflow the body deadline comment * volume(rs): count a gateway's src=client holder write against the upload limit Mirrors operation.IsReplicaForward: only a primary-to-replica forward skips the upload budget; a gateway's ?type=replicate&src=client fan-out waits like any client write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): admit uploads by inFlight+size, reserved atomically Mirrors the Go volume server's waitForUploadSlot: an upload is admitted by util.UploadAdmits (idle server, or inFlight + size within the limit) and reserves its bytes in the same step, so concurrent waiters cannot all pass one check. Primary-to-replica forwards and unlimited servers are counted but never refused. UploadLimitCond counts once per waiting upload, and the wake-up is armed before each check so a release in between is not missed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): charge an unknown-length upload against the upload limit Mirrors the Go volume server's uploadCharge: a chunked upload without Content-Length reserves one file's worth of budget, at most half the limit and at least 1 byte, instead of 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): a zero -inflightUploadDataTimeout waits the 5s client cap The Go volume server now reads 0 as the 5s cap (its flag help says so); the Rust server used a 2s wait. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): close a rejected large or unknown-length HTTP/1 upload Mirrors the Go volume server's uploadRejectCloseThreshold: the 503 for a body over 256 KiB, or one without a length, carries Connection: close instead of keeping the connection for a body nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): drop src from a primary's replica forward A client upload carrying src=client kept it on the forward, so replicas treated the forward as gateway fan-out and could refuse it after the primary had already appended. Go's ReplicatedWrite builds a fresh query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): keep the upload admission CAS buildable on rust 1.91.1 Atomic try_update is stable only since 1.95. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume(rs): refuse an upload the in-flight counter cannot hold A replica forward added its charge with fetch_add, so a Content-Length near i64::MAX wrapped the shared counter negative and let later client uploads past admission. The forward now reserves with a checked add and answers 503 when the size does not fit; the client CAS adds only after admission passes. With no upload limit the counter only feeds the gauge, so it keeps wrapping arithmetic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: refuse an upload the in-flight counter cannot hold A replica forward added its charge unchecked, so a Content-Length near math.MaxInt64 wrapped inFlightUploadDataSize negative and let later client uploads past admission. Under the lock the reservation now checks that the counter can hold the charge and answers 503 otherwise, matching the Rust volume server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: keep the request body on a rejected upload Replacing r.Body with http.NoBody hid the server's early close of the unread body from net/http before Go 1.27, so it skipped the FIN-then-wait close and reset the connection under a client still sending. Go 1.26.6, the version go.mod declares, then failed TestUploadLimit503ReachesGoClientForLargeBody with a write error instead of the 503. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: compare upload admission without forming inFlight+size * operation: reassign a chunk after a 429 as after a 503 * s3: do not count a chunk assignment the client cancelled as a filer failure * filer: charge an upload by its request chunk size, and an inline upload by its whole body * s3: hold an aws-chunked body to its X-Amz-Decoded-Content-Length * volume: answer a rejected small-body upload without waiting for its body * s3: fill the upload budget before testing that a release wakes every waiter --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
359 lines
13 KiB
Go
359 lines
13 KiB
Go
package s3api
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"hash"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
func isExtractZip(r *http.Request) bool {
|
|
if r.URL.Query().Get("extract") == "zip" ||
|
|
r.Header.Get(s3_constants.SeaweedFSExtract) == "zip" {
|
|
return true
|
|
}
|
|
// AWS Snowball auto-extract: header presence requests extraction.
|
|
if v := r.Header.Get(s3_constants.AmzMetaSnowballAutoExtract); v != "" && v != "false" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func hasExplicitSseHeaders(r *http.Request) bool {
|
|
return r.Header.Get(s3_constants.AmzServerSideEncryption) != "" ||
|
|
r.Header.Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId) != "" ||
|
|
r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm) != ""
|
|
}
|
|
|
|
type extractMemberResult struct {
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size,omitempty"`
|
|
ETag string `json:"etag,omitempty"`
|
|
}
|
|
|
|
type extractZipResult struct {
|
|
Extracted []*extractMemberResult `json:"extracted"`
|
|
Errors []string `json:"errors,omitempty"`
|
|
}
|
|
|
|
// extractZipObject writes a PutObject zip body as one object per member under
|
|
// the object's directory, like AWS Snowball's x-amz-meta-snowball-auto-extract.
|
|
// The archive object itself is not stored.
|
|
func (s3a *S3ApiServer) extractZipObject(w http.ResponseWriter, r *http.Request, bucket, object string, dataReader io.Reader) {
|
|
if hasExplicitSseHeaders(r) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
|
|
versioningState, err := s3a.getVersioningState(bucket)
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrNotFound) {
|
|
if !s3a.handleAutoCreateBucket(w, r, bucket, "PutObjectHandler") {
|
|
return
|
|
}
|
|
versioningState, err = s3a.getVersioningState(bucket)
|
|
}
|
|
if err != nil {
|
|
glog.Errorf("extractZipObject: versioning state for bucket %s: %v", bucket, err)
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
return
|
|
}
|
|
}
|
|
versioningConfigured := versioningState != ""
|
|
|
|
r, aclCode := s3a.preparePutObjectACL(r, bucket)
|
|
if aclCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, aclCode)
|
|
return
|
|
}
|
|
|
|
objectLockEnabled, lockErr := s3a.isObjectLockEnabled(bucket)
|
|
if lockErr != nil && !errors.Is(lockErr, filer_pb.ErrNotFound) {
|
|
glog.Errorf("extractZipObject: object lock state for bucket %s: %v", bucket, lockErr)
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
return
|
|
}
|
|
// Malformed or unauthorized lock headers fail the request outright, the
|
|
// same as a standalone PutObject; per-member permission re-checks happen in
|
|
// authorizeMember.
|
|
if vErr := s3a.validateObjectLockHeaders(r, bucket, object, objectLockEnabled); vErr != nil {
|
|
s3err.WriteErrorResponse(w, r, mapValidationErrorToS3Error(vErr))
|
|
return
|
|
}
|
|
|
|
// Checksums describing the archive body are verified while it spools, then
|
|
// scrubbed so members are validated against their own bytes instead.
|
|
requestQuery := parseRequestQuery(r)
|
|
archiveMd5 := r.Header.Get("Content-Md5")
|
|
var archiveMd5Hash hash.Hash
|
|
if archiveMd5 != "" && !fips140Enforced() {
|
|
archiveMd5Hash = md5.New()
|
|
dataReader = io.TeeReader(dataReader, archiveMd5Hash)
|
|
}
|
|
archiveChecksumAlgo, archiveChecksumHeader, checksumCode := detectRequestedChecksumAlgorithmQ(r, requestQuery)
|
|
if checksumCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, checksumCode)
|
|
return
|
|
}
|
|
var archiveChecksumHash hash.Hash
|
|
expectedArchiveChecksum := ""
|
|
if archiveChecksumAlgo != ChecksumAlgorithmNone {
|
|
if archiveChecksumHash = getCheckSumWriter(archiveChecksumAlgo); archiveChecksumHash != nil {
|
|
dataReader = io.TeeReader(dataReader, archiveChecksumHash)
|
|
}
|
|
expectedArchiveChecksum = lookupHeaderOrQuery(r, requestQuery, archiveChecksumHeader)
|
|
}
|
|
scrubArchiveChecksums(r)
|
|
// Content-Encoding describes the archive body on the wire; the members it
|
|
// fans out to are stored as-is.
|
|
r.Header.Del("Content-Encoding")
|
|
|
|
ra, size, cleanup, err := filer.SpoolZipReader(dataReader, 0)
|
|
if err != nil {
|
|
if errors.Is(err, filer.ErrZipTooLarge) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrEntityTooLarge)
|
|
} else if strings.Contains(err.Error(), s3err.ErrMsgContentSha256Mismatch) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrContentSHA256Mismatch)
|
|
} else if errors.Is(err, errDecodedLengthExceeded) {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
} else {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
|
|
}
|
|
return
|
|
}
|
|
defer cleanup()
|
|
if errCode := verifyArchiveChecksums(archiveMd5, archiveMd5Hash, expectedArchiveChecksum, archiveChecksumHash); errCode != s3err.ErrNone {
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
|
return
|
|
}
|
|
zr, err := zip.NewReader(ra, size)
|
|
if err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
if err = filer.CheckZipLimits(zr); err != nil {
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
|
|
return
|
|
}
|
|
|
|
root := object
|
|
if !strings.HasSuffix(root, "/") {
|
|
root = path.Dir(root)
|
|
}
|
|
root = strings.TrimSuffix(root, "/")
|
|
if root == "." {
|
|
root = ""
|
|
}
|
|
|
|
identity, _ := s3_constants.GetIdentityFromContext(r).(*Identity)
|
|
result := &extractZipResult{}
|
|
|
|
join := func(rel string) string {
|
|
if root == "" {
|
|
return rel
|
|
}
|
|
return root + "/" + rel
|
|
}
|
|
// authorizeMember applies to a member key the same checks a standalone
|
|
// PutObject of that key would face: IAM, table layout, bucket policy,
|
|
// object-lock header permissions, and existing-entry protections.
|
|
authorizeMember := func(memberKey string) s3err.ErrorCode {
|
|
if code := s3a.iam.AuthorizeObjectWrite(r, identity, bucket, memberKey); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
if err := s3a.validateTableBucketObjectPath(bucket, memberKey); err != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
if code := s3a.checkVersionFileWrite(bucket, memberKey, "extractZipObject"); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
if code, _ := s3a.checkPolicyWithEntry(r, bucket, memberKey, string(s3_constants.ACTION_WRITE), "", nil); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
if code := s3a.recheckTagConditionedPolicyWithLookup(r, bucket, memberKey, "", string(s3_constants.ACTION_WRITE), "extractZipObject"); code != s3err.ErrNone {
|
|
return code
|
|
}
|
|
// Lock headers validated for the request still need the dedicated
|
|
// permissions against the member's own key.
|
|
if r.Header.Get(s3_constants.AmzObjectLockMode) != "" || r.Header.Get(s3_constants.AmzObjectLockRetainUntilDate) != "" {
|
|
if !s3a.checkObjectLockHeaderPermission(r, bucket, memberKey, s3_constants.ACTION_PUT_OBJECT_RETENTION) {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
if r.Header.Get(s3_constants.AmzObjectLockLegalHold) != "" {
|
|
if !s3a.checkObjectLockHeaderPermission(r, bucket, memberKey, s3_constants.ACTION_PUT_OBJECT_LEGAL_HOLD) {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
if !versioningConfigured {
|
|
if lockErr := s3a.enforceObjectLockProtections(r, bucket, memberKey, "", s3a.evaluateGovernanceBypassRequest(r, bucket, memberKey)); lockErr != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
writeMember := func(rel string, memberReader io.Reader, memberSize int64) {
|
|
memberKey := join(rel)
|
|
if authCode := authorizeMember(memberKey); authCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(authCode).Code)
|
|
return
|
|
}
|
|
memberContentType := mime.TypeByExtension(path.Ext(memberKey))
|
|
if memberContentType == "" {
|
|
memberContentType = "application/octet-stream"
|
|
}
|
|
// Write helpers read the object from route vars, e.g. for the SSE-KMS
|
|
// encryption context ARN; point them at the member, not the archive.
|
|
memberReq := mux.SetURLVars(r, map[string]string{
|
|
"bucket": bucket,
|
|
"object": memberKey,
|
|
})
|
|
memberReq.Header.Set("Content-Type", memberContentType)
|
|
memberReq.ContentLength = memberSize
|
|
var etag string
|
|
var errCode s3err.ErrorCode
|
|
switch versioningState {
|
|
case s3_constants.VersioningEnabled:
|
|
_, etag, errCode, _ = s3a.putVersionedObject(memberReq, bucket, memberKey, memberReader, memberContentType)
|
|
case s3_constants.VersioningSuspended:
|
|
etag, errCode, _ = s3a.putSuspendedVersioningObject(memberReq, bucket, memberKey, memberReader, memberContentType)
|
|
default:
|
|
filePath := s3a.toFilerPath(bucket, memberKey)
|
|
lifecycleTTLSec := s3a.lifecycleTTLForObjectWrite(bucket, memberKey, memberSize)
|
|
etag, errCode, _ = s3a.putToFiler(memberReq, filePath, memberReader, bucket, memberKey, 1, lifecycleTTLSec, nil, false, "")
|
|
}
|
|
if errCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(errCode).Code)
|
|
return
|
|
}
|
|
result.Extracted = append(result.Extracted, &extractMemberResult{Name: memberKey, Size: memberSize, ETag: etag})
|
|
}
|
|
mkdirMember := func(memberKey string) {
|
|
if authCode := authorizeMember(memberKey); authCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(authCode).Code)
|
|
return
|
|
}
|
|
dirPath := s3a.toFilerPath(bucket, memberKey)
|
|
dirName, entryName := path.Dir(dirPath), path.Base(dirPath)
|
|
// mkdir replaces an existing marker outright, so a lock recorded on the
|
|
// entry itself is checked under the same lock the replacement takes,
|
|
// mirroring the explicit-directory branch of PutObjectHandler.
|
|
markerCode := s3a.withObjectWriteLock(bucket, memberKey, func() s3err.ErrorCode {
|
|
if !objectLockEnabled {
|
|
return s3err.ErrNone
|
|
}
|
|
// mkdir replaces the entry on the default filer, so the lock check
|
|
// must read there too — a routed read can miss a marker the owner
|
|
// has not received yet.
|
|
existing, existErr := s3a.getEntry(dirName, entryName)
|
|
if existErr != nil {
|
|
return s3err.ErrNone
|
|
}
|
|
if lockErr := s3a.enforceObjectLockOnEntry(existing, bucket, memberKey, "", s3a.evaluateGovernanceBypassRequest(r, bucket, memberKey)); lockErr != nil {
|
|
return s3err.ErrAccessDenied
|
|
}
|
|
return s3err.ErrNone
|
|
}, func() s3err.ErrorCode {
|
|
mkErr := s3a.mkdir(dirName, entryName, func(entry *filer_pb.Entry) {
|
|
entry.Attributes.Mime = s3_constants.FolderMimeType
|
|
s3a.setObjectOwnerFromRequest(r, bucket, entry)
|
|
applyPutObjectACL(r, entry)
|
|
if lockErr := s3a.extractObjectLockMetadataFromRequest(r, entry); lockErr != nil {
|
|
glog.Errorf("extractZipObject: object lock metadata for %s/%s: %v", bucket, memberKey, lockErr)
|
|
}
|
|
})
|
|
return filerErrorToS3Error(mkErr)
|
|
})
|
|
if markerCode != s3err.ErrNone {
|
|
result.Errors = append(result.Errors, memberKey+": "+s3err.GetAPIError(markerCode).Code)
|
|
}
|
|
}
|
|
|
|
for _, zf := range zr.File {
|
|
if ctxErr := r.Context().Err(); ctxErr != nil {
|
|
result.Errors = append(result.Errors, ctxErr.Error())
|
|
break
|
|
}
|
|
rel, ok := filer.SanitizeZipMemberName(zf)
|
|
if !ok {
|
|
result.Errors = append(result.Errors, zf.Name+": unsafe member name")
|
|
continue
|
|
}
|
|
if zf.FileInfo().IsDir() {
|
|
mkdirMember(join(rel))
|
|
continue
|
|
}
|
|
memberReader, openErr := zf.Open()
|
|
if openErr != nil {
|
|
result.Errors = append(result.Errors, rel+": "+openErr.Error())
|
|
continue
|
|
}
|
|
writeMember(rel, memberReader, int64(zf.UncompressedSize64))
|
|
memberReader.Close()
|
|
}
|
|
|
|
w.Header().Set(s3_constants.SeaweedFSExtractedCount, strconv.Itoa(len(result.Extracted)))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(result)
|
|
}
|
|
|
|
// verifyArchiveChecksums compares the archive's supplied Content-Md5 and
|
|
// x-amz-checksum-* values against the bytes consumed while spooling. A nil
|
|
// hash means the client supplied nothing to check.
|
|
func verifyArchiveChecksums(expectedMd5 string, md5Hash hash.Hash, expectedChecksum string, checksumHash hash.Hash) s3err.ErrorCode {
|
|
if md5Hash != nil {
|
|
expected, err := base64.StdEncoding.DecodeString(expectedMd5)
|
|
if err != nil {
|
|
return s3err.ErrInvalidDigest
|
|
}
|
|
if sum := md5Hash.Sum(nil); !bytes.Equal(expected, sum) {
|
|
return s3err.ErrBadDigest
|
|
}
|
|
}
|
|
if checksumHash != nil && expectedChecksum != "" {
|
|
if sum := base64.StdEncoding.EncodeToString(checksumHash.Sum(nil)); expectedChecksum != sum {
|
|
return s3err.ErrBadDigest
|
|
}
|
|
}
|
|
return s3err.ErrNone
|
|
}
|
|
|
|
// scrubArchiveChecksums removes request checksums that describe the archive
|
|
// body; members are validated against their own bytes.
|
|
func scrubArchiveChecksums(r *http.Request) {
|
|
isChecksumHeader := func(k string) bool {
|
|
k = strings.ToLower(k)
|
|
return k == "content-md5" || k == "x-amz-trailer" || k == "x-amz-content-sha256" ||
|
|
strings.HasPrefix(k, "x-amz-checksum") || strings.HasPrefix(k, "x-amz-sdk-checksum")
|
|
}
|
|
for k := range r.Header {
|
|
if isChecksumHeader(k) {
|
|
delete(r.Header, k)
|
|
}
|
|
}
|
|
query := r.URL.Query()
|
|
for k := range query {
|
|
if isChecksumHeader(k) {
|
|
query.Del(k)
|
|
}
|
|
}
|
|
r.URL.RawQuery = query.Encode()
|
|
}
|