66 "net/http"
77 "net/url"
88
9- "github.com/aws/aws-sdk-go-v2/aws"
109 "github.com/aws/aws-sdk-go-v2/config"
10+ "github.com/aws/aws-sdk-go-v2/credentials"
1111 "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
1212 "github.com/aws/aws-sdk-go-v2/service/s3"
1313 "github.com/aws/aws-sdk-go/aws/request"
@@ -29,15 +29,23 @@ func GetBucketRegion(bucket string) (string, error) {
2929 // Client therefore needs to be configured with region.
3030 // In local dev environments, you might have ~/.aws/config that could be loaded and set with default region.
3131 // In cluster/CI environment, ~/.aws/config may not be configured, so set hinting region server explicitly.
32- // Also set to use anonymous credentials. If the bucket is private, this function would not work unless we modify it to take credentials.
32+ // Also set to use anonymous credentials. This works for both public and private buckets as AWS Security
33+ // confirmed that HeadBucket API (used by GetBucketRegion) doesn't enforce s3:ListBucket permissions
34+ // for region retrieval - this is expected AWS behavior.
3335 cfg , err := config .LoadDefaultConfig (context .Background (),
3436 config .WithRegion ("us-east-1" ), // This is not default region being used, this is to specify a region hinting server that we will use to get region from.
35- config .WithCredentialsProvider (aws.AnonymousCredentials {}),
3637 )
3738 if err != nil {
3839 return "" , err
3940 }
40- region , err = manager .GetBucketRegion (context .Background (), s3 .NewFromConfig (cfg ), bucket )
41+ region , err = manager .GetBucketRegion (context .Background (), s3 .NewFromConfig (cfg ), bucket , func (o * s3.Options ) {
42+ // AWS Security confirmed that anonymous credentials can be used here for GetBucketRegion.
43+ // The HeadBucket API endpoint used internally by GetBucketRegion does not enforce
44+ // s3:ListBucket permissions for retrieving bucket region information.
45+ // Reference: AWS Security response (Engagement ID: CACenGS4Mha_KeJ=e3jBSLD6rPZ2iNtfuJUv9QJViaCOt7GVNDg)
46+ // This is expected AWS behavior, not a security vulnerability.
47+ o .Credentials = credentials .NewStaticCredentialsProvider ("anon-credentials" , "anon-secret" , "" )
48+ })
4149 if region != "" {
4250 return region , nil
4351 }
0 commit comments