Skip to content

Commit ac1e1e2

Browse files
committed
Add tests for auto bucket region on priv/pubic bucket
Signed-off-by: Tiger Kaovilai <[email protected]>
1 parent 08b9b61 commit ac1e1e2

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ require (
3131
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.9.0
3232
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1
3333
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.1
34-
github.com/aws/aws-sdk-go-v2 v1.30.3
3534
github.com/aws/aws-sdk-go-v2/config v1.26.3
35+
github.com/aws/aws-sdk-go-v2/credentials v1.17.26
3636
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.11
3737
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0
3838
github.com/deckarep/golang-set/v2 v2.3.0
@@ -60,8 +60,8 @@ require (
6060
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
6161
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 // indirect
6262
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
63+
github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
6364
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 // indirect
64-
github.com/aws/aws-sdk-go-v2/credentials v1.17.26 // indirect
6565
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect
6666
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
6767
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect

pkg/storage/aws/s3.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
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"
@@ -32,12 +32,14 @@ func GetBucketRegion(bucket string) (string, error) {
3232
// Also set to use anonymous credentials. If the bucket is private, this function would not work unless we modify it to take credentials.
3333
cfg, err := config.LoadDefaultConfig(context.Background(),
3434
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{}),
3635
)
3736
if err != nil {
3837
return "", err
3938
}
40-
region, err = manager.GetBucketRegion(context.Background(), s3.NewFromConfig(cfg), bucket)
39+
region, err = manager.GetBucketRegion(context.Background(), s3.NewFromConfig(cfg), bucket, func(o *s3.Options) {
40+
// TODO: get creds from bsl
41+
o.Credentials = credentials.NewStaticCredentialsProvider("anon-credentials", "anon-secret", "") // this works with private buckets.. why? supposed to require cred with s3:ListBucket https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html
42+
})
4143
if region != "" {
4244
return region, nil
4345
}

pkg/storage/aws/s3_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,31 @@ func TestGetBucketRegion(t *testing.T) {
1414
wantErr bool
1515
}{
1616
{
17+
// Public bucket should work anonymously, policy below
18+
// {
19+
// "Version": "2012-10-17",
20+
// "Statement": [
21+
// {
22+
// "Sid": "publicList",
23+
// "Effect": "Allow",
24+
// "Principal": "*",
25+
// "Action": "s3:ListBucket",
26+
// "Resource": "arn:aws:s3:::openshift-velero-plugin-s3-auto-region-test-1"
27+
// }
28+
// ]
29+
// }
30+
// ❯ aws s3api head-bucket --bucket openshift-velero-plugin-s3-auto-region-test-1 --no-sign-request
31+
// {
32+
// "BucketRegion": "us-east-1",
33+
// "AccessPointAlias": false
34+
// }
1735
name: "openshift-velero-plugin-s3-auto-region-test-1",
1836
bucket: "openshift-velero-plugin-s3-auto-region-test-1",
1937
region: "us-east-1",
2038
wantErr: false,
2139
},
2240
{
41+
// Private bucket do not require creds per email with AWS, not a concern
2342
name: "openshift-velero-plugin-s3-auto-region-test-2",
2443
bucket: "openshift-velero-plugin-s3-auto-region-test-2",
2544
region: "us-west-1",
@@ -42,6 +61,7 @@ func TestGetBucketRegion(t *testing.T) {
4261
bucket: "velero-6109f5e9711c8c58131acdd2f490f451",
4362
region: "us-east-1",
4463
wantErr: false,
64+
// TODO: add creds usage here.
4565
},
4666
}
4767
for _, tt := range tests {

0 commit comments

Comments
 (0)