Skip to content

Remove credential config of GetBucketRegion by default #3081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelog/096161d8acea4d53afe332ec60aac3db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "096161d8-acea-4d53-afe3-32ec60aac3db",
"type": "bugfix",
"description": "Enable user to call GetBucketRegion without credential config",
"modules": [
"feature/s3/manager"
]
}
9 changes: 5 additions & 4 deletions feature/s3/manager/bucket_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ const bucketRegionHeader = "X-Amz-Bucket-Region"
//
// manager.GetBucketRegion(ctx, s3.NewFromConfig(cfg), bucket, func(o *s3.Options) {
// o.Credentials = nil
// // Or
// o.Credentials = aws.AnonymousCredentials{}
// })
//
// The request with anonymous credentials will not be signed.
// Otherwise credentials would be required for private buckets.
func GetBucketRegion(ctx context.Context, client HeadBucketAPIClient, bucket string, optFns ...func(*s3.Options)) (string, error) {
var captureBucketRegion deserializeBucketRegion

clientOptionFns := make([]func(*s3.Options), len(optFns)+1)
clientOptionFns := make([]func(*s3.Options), len(optFns)+2)
clientOptionFns[0] = func(options *s3.Options) {
options.APIOptions = append(options.APIOptions, captureBucketRegion.RegisterMiddleware)
}
copy(clientOptionFns[1:], optFns)
clientOptionFns[1] = func(options *s3.Options) {
options.Credentials = nil
}
copy(clientOptionFns[2:], optFns)
Comment on lines 74 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the bug!

Out of curiosity, is there a reason why we don't just set options.Credentials = nil directly in clientOptionFns[0]?

Suggested change
clientOptionFns[0] = func(options *s3.Options) {
options.APIOptions = append(options.APIOptions, captureBucketRegion.RegisterMiddleware)
}
copy(clientOptionFns[1:], optFns)
clientOptionFns[1] = func(options *s3.Options) {
options.Credentials = nil
}
copy(clientOptionFns[2:], optFns)
clientOptionFns[0] = func(options *s3.Options) {
options.APIOptions = append(options.APIOptions, captureBucketRegion.RegisterMiddleware)
options.Credentials = nil
}
copy(clientOptionFns[1:], optFns)

Copy link
Contributor Author

@wty-Bryant wty-Bryant May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a idioms when I want to add a new functional config


_, err := client.HeadBucket(ctx, &s3.HeadBucketInput{
Bucket: aws.String(bucket),
Expand Down
10 changes: 4 additions & 6 deletions feature/s3/manager/bucket_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ func testSetupGetBucketRegionServer(region string, statusCode int, incHeader boo
}

var testGetBucketRegionCases = []struct {
RespRegion string
StatusCode int
ExpectReqRegion string
RespRegion string
StatusCode int
}{
{
RespRegion: "bucket-region",
Expand All @@ -56,9 +55,8 @@ var testGetBucketRegionCases = []struct {
StatusCode: 200,
},
{
RespRegion: "bucket-region",
StatusCode: 200,
ExpectReqRegion: "default-region",
RespRegion: "bucket-region",
StatusCode: 200,
},
}

Expand Down
27 changes: 21 additions & 6 deletions feature/s3/manager/integ_bucket_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,34 @@ import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)

func TestInteg_GetBucketRegion(t *testing.T) {
expectRegion := integConfig.Region

region, err := manager.GetBucketRegion(context.Background(), s3.NewFromConfig(integConfig), aws.ToString(bucketName))
if err != nil {
t.Fatalf("expect no error, got %v", err)
cases := map[string]struct {
optFns []func(*s3.Options)
}{
"normal credentials": {[]func(o *s3.Options){}},
"invalid credentials": {[]func(o *s3.Options){
func(o *s3.Options) {
o.Credentials = credentials.NewStaticCredentialsProvider("fakeDummy", "fakeDummy", "")
},
}},
}

if e, a := expectRegion, region; e != a {
t.Errorf("expect %s bucket region, got %s", e, a)
for name, c := range cases {
t.Run(name, func(t *testing.T) {
region, err := manager.GetBucketRegion(context.Background(), s3.NewFromConfig(integConfig, c.optFns...), aws.ToString(bucketName))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}

if e, a := expectRegion, region; e != a {
t.Errorf("expect %s bucket region, got %s", e, a)
}
})
}
}