Skip to content

Commit ffc4443

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

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test: vet envtest ## Run unit tests; run Go linters checks; check if api and bun
141141
@make check-go-dependencies
142142

143143
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
144-
GOLANGCI_LINT_VERSION ?= v1.55.2
144+
GOLANGCI_LINT_VERSION ?= v1.64.8 # go1.24 requires at least v1.64.4 to work on gci
145145
.PHONY: golangci-lint
146146
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
147147
$(GOLANGCI_LINT): $(LOCALBIN)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ require (
2626
)
2727

2828
require (
29-
github.com/aws/aws-sdk-go-v2 v1.30.3
3029
github.com/aws/aws-sdk-go-v2/config v1.26.3
30+
github.com/aws/aws-sdk-go-v2/credentials v1.17.26
3131
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.15.11
3232
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0
3333
github.com/deckarep/golang-set/v2 v2.3.0
@@ -42,8 +42,8 @@ require (
4242

4343
require (
4444
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
45+
github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
4546
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 // indirect
46-
github.com/aws/aws-sdk-go-v2/credentials v1.17.26 // indirect
4747
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect
4848
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
4949
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)