Skip to content

Commit

Permalink
br: fix getting region failure with other s3 compatible storage (#39653)
Browse files Browse the repository at this point in the history
close #39648
  • Loading branch information
WangLe1321 authored Dec 7, 2022
1 parent be09b67 commit 032e6fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
9 changes: 7 additions & 2 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,18 @@ func NewS3Storage(backend *backuppb.S3, opts *ExternalStorageOptions) (obj *S3St
)
}
c := s3.New(ses, s3CliConfigs...)
// s3manager.GetBucketRegionWithClient will set credential anonymous, which works with s3.
// we need reassign credential to be compatible with minio authentication.
confCred := ses.Config.Credentials
setCredOpt := func(req *request.Request) {
// s3manager.GetBucketRegionWithClient will set credential anonymous, which works with s3.
// we need reassign credential to be compatible with minio authentication.
if confCred != nil {
req.Config.Credentials = confCred
}
// s3manager.GetBucketRegionWithClient use path style addressing default.
// we need set S3ForcePathStyle by our config if we set endpoint.
if qs.Endpoint != "" {
req.Config.S3ForcePathStyle = ses.Config.S3ForcePathStyle
}
}
region, err := s3manager.GetBucketRegionWithClient(context.Background(), c, qs.Bucket, setCredOpt)
if err != nil {
Expand Down
51 changes: 29 additions & 22 deletions br/pkg/storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,35 @@ func TestS3Storage(t *testing.T) {
{
name: "no region",
s3: &backuppb.S3{
Region: "",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
Region: "",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: true,
},
{
name: "wrong region",
s3: &backuppb.S3{
Region: "us-east-2",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
Region: "us-east-2",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: true,
sendCredential: true,
},
{
name: "right region",
s3: &backuppb.S3{
Region: "us-west-2",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
Region: "us-west-2",
Endpoint: s.URL,
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: true,
Expand All @@ -353,6 +356,7 @@ func TestS3Storage(t *testing.T) {
SecretAccessKey: "cd",
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: true,
Expand All @@ -365,30 +369,33 @@ func TestS3Storage(t *testing.T) {
SecretAccessKey: "cd",
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: true,
},
{
name: "no secret access key",
s3: &backuppb.S3{
Region: "us-west-2",
Endpoint: s.URL,
AccessKey: "ab",
Bucket: "bucket",
Prefix: "prefix",
Region: "us-west-2",
Endpoint: s.URL,
AccessKey: "ab",
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: true,
},
{
name: "no secret access key",
s3: &backuppb.S3{
Region: "us-west-2",
Endpoint: s.URL,
AccessKey: "ab",
Bucket: "bucket",
Prefix: "prefix",
Region: "us-west-2",
Endpoint: s.URL,
AccessKey: "ab",
Bucket: "bucket",
Prefix: "prefix",
ForcePathStyle: true,
},
errReturn: false,
sendCredential: false,
Expand Down

0 comments on commit 032e6fd

Please sign in to comment.