Files
dualfroz e1d2284906 fix: reject a ListBuckets bucket-region other than the gateway region
S3 never lists an empty result for another region: a valid region
other than the endpoint's own gets 400 InvalidArgument, "Requests with
bucket-region specified must be made to the corresponding regional
endpoint", with no ArgumentValue. The gateway is a single regional
endpoint, so it now does the same instead of clearing the listing.

Match the region case-insensitively like S3, and check an empty
bucket-region= too by looking for the query argument itself, since an
empty value is not a valid AWS region either.
2026-10-05 20:42:30 +02:00

232 lines
5.0 KiB
Go

// Copyright 2023 Versity Software
// This file is licensed under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package controllers
import (
"context"
"testing"
"github.com/versity/versitygw/s3err"
"github.com/versity/versitygw/s3response"
)
func TestS3ApiController_ListBuckets(t *testing.T) {
validRes := s3response.ListAllMyBucketsResult{
Owner: s3response.CanonicalUser{
ID: "root",
},
Buckets: s3response.ListAllMyBucketsList{
Bucket: []s3response.ListAllMyBucketsEntry{
{Name: "test"},
},
},
}
tests := []struct {
name string
input testInput
output testOutput
}{
{
name: "negative max buckets",
input: testInput{
locals: defaultLocals,
queries: map[string]string{
"max-buckets": "-1",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "-1"),
},
},
{
name: "max buckets exceeds upper limit",
input: testInput{
locals: defaultLocals,
queries: map[string]string{
"max-buckets": "10001",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "10001"),
},
},
{
name: "max buckets 0",
input: testInput{
locals: defaultLocals,
queries: map[string]string{
"max-buckets": "0",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "0"),
},
},
{
name: "invalid max buckets",
input: testInput{
locals: defaultLocals,
queries: map[string]string{
"max-buckets": "bla",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgMaxLimiter("max-buckets", "bla"),
},
},
{
name: "invalid bucket region",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"bucket-region": "abc",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgBucketRegion("abc"),
},
},
{
name: "bucket region of another region",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"bucket-region": "us-west-2",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgBucketRegionEndpoint, ""),
},
},
{
name: "empty bucket region",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"bucket-region": "",
},
},
output: testOutput{
response: &Response{
MetaOpts: &MetaOptions{},
},
err: s3err.GetInvalidArgBucketRegion(""),
},
},
{
name: "bucket region of the gateway region in uppercase",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"bucket-region": "US-EAST-1",
},
},
output: testOutput{
response: &Response{
Data: validRes,
},
},
},
{
name: "bucket region of the gateway region",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"bucket-region": "us-east-1",
},
},
output: testOutput{
response: &Response{
Data: validRes,
},
},
},
{
name: "backend returns error",
input: testInput{
locals: defaultLocals,
beErr: s3err.GetAPIError(s3err.ErrNoSuchBucket),
beRes: s3response.ListAllMyBucketsResult{},
},
output: testOutput{
response: &Response{},
err: s3err.GetAPIError(s3err.ErrNoSuchBucket),
},
},
{
name: "successful response",
input: testInput{
locals: defaultLocals,
beRes: validRes,
queries: map[string]string{
"max-buckets": "3",
},
},
output: testOutput{
response: &Response{
Data: validRes,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
be := &BackendMock{
ListBucketsFunc: func(contextMoqParam context.Context, listBucketsInput s3response.ListBucketsInput) (s3response.ListAllMyBucketsResult, error) {
return tt.input.beRes.(s3response.ListAllMyBucketsResult), tt.input.beErr
},
}
ctrl := S3ApiController{
be: be,
}
testController(
t,
ctrl.ListBuckets,
tt.output.response,
tt.output.err,
ctxInputs{
locals: tt.input.locals,
queries: tt.input.queries,
})
})
}
}