Skip to content
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

Correctly aquire information on AWS_REGION #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 28 additions & 5 deletions s3/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -23,7 +24,7 @@ const HTTP = "http"

// TODO : unit tests
// Given an S3 bucket name, attempt to determine its region
func findBucketRegion(bucket string, config *aws.Config) (string, error) {
var findBucketRegion = func(bucket string, config *aws.Config) (string, error) {
input := s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}
Expand All @@ -47,13 +48,35 @@ func findBucketRegion(bucket string, config *aws.Config) (string, error) {
}

// TODO : unit tests
func getAWSRegion(s3Bucket string, config *aws.Config, settings map[string]string) (string, error) {
func GetAWSRegion(s3Bucket string, config *aws.Config, settings map[string]string) (string, error) {
if region, ok := settings[RegionSetting]; ok {
return region, nil
}

if config.Endpoint == nil ||
Copy link
Member

Choose a reason for hiding this comment

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

This new extracting logic deserves extracting into a separate function, just like the findBucketRegion

*config.Endpoint == "" ||
strings.HasSuffix(*config.Endpoint, ".amazonaws.com") {
*config.Endpoint == "" {

if config.Endpoint != nil {
hostAddr, parseErr := url.Parse(*config.Endpoint)

if parseErr != nil {
return "us-east-1", parseErr
}

host, _, err := net.SplitHostPort(hostAddr.Host)

if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

In this logic, if WAL-G failed to parse the endpoint host, it won't attempt to call findBucketRegion, is it intended behavior?
I think that WAL-G should always try both approaches before giving up.

return "us-east-1", err
}

if strings.HasSuffix(host, ".amazonaws.com") {
region, err := findBucketRegion(s3Bucket, config)
return region, errors.Wrapf(err, "%s is not set and s3:GetBucketLocation failed", RegionSetting)
}

return "us-east-1", nil
}

region, err := findBucketRegion(s3Bucket, config)
return region, errors.Wrapf(err, "%s is not set and s3:GetBucketLocation failed", RegionSetting)
} else {
Expand Down Expand Up @@ -122,7 +145,7 @@ func createSession(bucket string, settings map[string]string) (*session.Session,
config.S3ForcePathStyle = aws.Bool(s3ForcePathStyle)
}

region, err := getAWSRegion(bucket, config, settings)
region, err := GetAWSRegion(bucket, config, settings)
if err != nil {
return nil, err
}
Expand Down
49 changes: 49 additions & 0 deletions s3/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package s3

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go/aws/defaults"
"testing"
)

var bucket = "s3://test-bucket/wal-g-test-folder/Sub0"
var settings = map[string]string{
EndpointSetting: "http://s3.mdst.yandex.net/",
}
var config = defaults.Get().Config.WithRegion(settings[RegionSetting])

func TestGetAWSRegionWithEmptyEndpoint(t *testing.T) {
findBucketRegion = func(bucket string, config *aws.Config) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I missed the point, but I think that we just need to provide different aws.Config and compare the result with the expected one? Basically, write unit tests for these functions:

  1. findBucketRegion
  2. The new function which extracts the region from the hostname

return "europe", nil
}

region, err := GetAWSRegion(bucket, config, settings)

assert.Nil(t, err)
assert.Equal(t, region, "europe")
}

func TestGetAWSRegionWithPort(t *testing.T) {
bucket = "s3://test-bucket:8080/wal-g-test-folder/Sub0"
findBucketRegion = func(bucket string, config *aws.Config) (string, error) {
return "europe", nil
}

region, err := GetAWSRegion(bucket, config, settings)

assert.Nil(t, err)
assert.Equal(t, region, "europe")
}

func TestGetAWSRegionWithEndpoint(t *testing.T) {
config.Endpoint = aws.String("s3://test-bucket:8080/wal-g-test-folder/Sub0")
findBucketRegion = func(bucket string, config *aws.Config) (string, error) {
return "europe", nil
}

region, err := GetAWSRegion(bucket, config, settings)

assert.Nil(t, err)
assert.Equal(t, region, "us-east-1")
}