Files
seaweedfs/weed/s3api/s3api_object_handlers_get_part_test.go
T
Chris LuandClaude Opus 5.5 480a1baa20 s3api: GetPart, reading parts of in-progress and completed multipart uploads (#13174)
* s3api: GetPart reads one part of a multipart upload

GET /key?part&partNumber=N&uploadId=X serves the latest attempt of an
open upload's part, or the bytes that part became in the object a
completed upload assembled, including a version that is no longer
latest. x-amz-multipart-upload-status says which. A Range counts
within the part, and a whole part answers 200 even though a completed
part is served as a range of the object.

The route sits ahead of ListParts and claims any ?part request, so a
missing partNumber is InvalidArgument rather than a listing, and the
request resolves to s3:GetObject, not the ListParts grant.

* s3api: reading an open upload's part needs s3:GetIncompleteUpload

AWS gates GetPart on an upload that has not completed behind
s3:GetIncompleteUpload on top of s3:GetObject, so a plain read grant
does not start exposing data its writer has not committed yet. The
action is its own legacy grant too; Read alone does not carry it, Admin
does. A completed upload's part needs only the read grant, plus
s3:GetObjectVersion when it lives in a version.

* s3api: a GetPart request carrying ?uploads is not a multipart listing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: GetPart answers a completed empty part with an empty 200

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 15:09:19 -08:00

64 lines
2.1 KiB
Go

package s3api
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/stretchr/testify/assert"
)
func getPartRequest() *http.Request {
req := &http.Request{
Method: http.MethodGet,
URL: &url.URL{Path: "/bucket/key", RawQuery: "part&partNumber=1&uploadId=abc"},
}
return mux.SetURLVars(req, map[string]string{"bucket": "bucket", "object": "key"})
}
func TestGetPartIncompleteUploadActionIgnoresRequestShape(t *testing.T) {
assert.Equal(t, s3_constants.S3_ACTION_GET_INCOMPLETE_UPLOAD,
ResolveS3Action(getPartRequest(), s3_constants.ACTION_GET_INCOMPLETE_UPLOAD, "bucket", "key"))
}
func TestGetPartIsNotAPartsListing(t *testing.T) {
assert.Equal(t, Action(""), multipartListingAction(getPartRequest(), s3_constants.ACTION_READ))
withUploads := getPartRequest()
withUploads.URL.RawQuery = "part&partNumber=1&uploadId=abc&uploads"
assert.Equal(t, Action(""), multipartListingAction(withUploads, s3_constants.ACTION_READ))
listing := getPartRequest()
listing.URL.RawQuery = "uploadId=abc"
assert.Equal(t, Action(s3_constants.ACTION_LIST_PARTS), multipartListingAction(listing, s3_constants.ACTION_READ))
}
func TestPartRangeWriter(t *testing.T) {
cases := []struct {
name string
ranged bool
wantStatus int
wantRangeHdr string
}{
{"whole part answers 200", false, http.StatusOK, ""},
{"ranged read counts within the part", true, http.StatusPartialContent, "bytes 2-5/10"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rec := httptest.NewRecorder()
w := &partRangeWriter{ResponseWriter: rec, ranged: c.ranged, contentRange: "bytes 2-5/10"}
w.Header().Set("Content-Range", "bytes 102-105/4096")
w.WriteHeader(http.StatusPartialContent)
assert.Equal(t, c.wantStatus, rec.Code)
assert.Equal(t, c.wantRangeHdr, rec.Header().Get("Content-Range"))
})
}
rec := httptest.NewRecorder()
(&partRangeWriter{ResponseWriter: rec}).WriteHeader(http.StatusInternalServerError)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
}