Skip to content

Commit

Permalink
Allow the AWS backend to get credentials from its environment. (#111)
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
  • Loading branch information
tomwilkie authored and bboreham committed Aug 3, 2018
1 parent d178023 commit 54b7e30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
14 changes: 7 additions & 7 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ import (
// endpoint) or fully valid endpoint with dummy region assumed (e.g
// for URLs to emulated services).
func ConfigFromURL(awsURL *url.URL) (*aws.Config, error) {
if awsURL.User == nil {
return nil, fmt.Errorf("must specify escaped Access Key & Secret Access in URL")
}

password, _ := awsURL.User.Password()
creds := credentials.NewStaticCredentials(awsURL.User.Username(), password, "")
config := aws.NewConfig().
WithCredentials(creds).
// Use a custom http.Client with the golang defaults but also specifying
// MaxIdleConnsPerHost because of a bug in golang https://github.com/golang/go/issues/13801
// where MaxIdleConnsPerHost does not work as expected.
Expand All @@ -44,6 +37,13 @@ func ConfigFromURL(awsURL *url.URL) (*aws.Config, error) {
ExpectContinueTimeout: 1 * time.Second,
},
})

if awsURL.User != nil {
password, _ := awsURL.User.Password()
creds := credentials.NewStaticCredentials(awsURL.User.Username(), password, "")
config = config.WithCredentials(creds)
}

if strings.Contains(awsURL.Host, ".") {
return config.WithEndpoint(fmt.Sprintf("http://%s", awsURL.Host)).WithRegion("dummy"), nil
}
Expand Down
67 changes: 27 additions & 40 deletions aws/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,75 @@ package aws

import (
"net/url"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAWSConfigFromURL(t *testing.T) {
for _, tc := range []struct {
for i, tc := range []struct {
url string
expectedKey string
expectedSecret string
expectedRegion string
expectedEp string

expectedNotSpecifiedUserErr bool
}{
{
"s3://abc:123@s3.default.svc.cluster.local:4569",
"abc",
"123",
"dummy",
"http://s3.default.svc.cluster.local:4569",
false,
},
{
"dynamodb://user:pass@dynamodb.default.svc.cluster.local:8000/cortex",
"user",
"pass",
"dummy",
"http://dynamodb.default.svc.cluster.local:8000",
false,
},
{
// Not escaped password.
"s3://abc:123/@s3.default.svc.cluster.local:4569",
"",
"",
"",
"",
true,
},
{
// Not escaped username.
"s3://abc/:123@s3.default.svc.cluster.local:4569",
"",
"",
// No credentials.
"s3://s3.default.svc.cluster.local:4569",
"",
"",
true,
"dummy",
"http://s3.default.svc.cluster.local:4569",
},
{
"s3://keyWithEscapedSlashAtTheEnd%2F:%24%2C%26%2C%2B%2C%27%2C%2F%2C%3A%2C%3B%2C%3D%2C%3F%2C%40@eu-west-2/bucket1",
"keyWithEscapedSlashAtTheEnd/",
"$,&,+,',/,:,;,=,?,@",
"eu-west-2",
"",
false,
},
} {
parsedURL, err := url.Parse(tc.url)
require.NoError(t, err)

cfg, err := ConfigFromURL(parsedURL)
if tc.expectedNotSpecifiedUserErr {
require.Error(t, err)
continue
}
require.NoError(t, err)
t.Run(strconv.Itoa(i), func(t *testing.T) {
parsedURL, err := url.Parse(tc.url)
require.NoError(t, err)

require.NotNil(t, cfg.Credentials)
val, err := cfg.Credentials.Get()
require.NoError(t, err)
cfg, err := ConfigFromURL(parsedURL)
require.NoError(t, err)

assert.Equal(t, tc.expectedKey, val.AccessKeyID)
assert.Equal(t, tc.expectedSecret, val.SecretAccessKey)
if cfg.Credentials == nil {
assert.Equal(t, "", tc.expectedKey)
assert.Equal(t, "", tc.expectedSecret)
} else {
val, err := cfg.Credentials.Get()
require.NoError(t, err)
assert.Equal(t, tc.expectedKey, val.AccessKeyID)
assert.Equal(t, tc.expectedSecret, val.SecretAccessKey)
}

require.NotNil(t, cfg.Region)
assert.Equal(t, tc.expectedRegion, *cfg.Region)
require.NotNil(t, cfg.Region)
assert.Equal(t, tc.expectedRegion, *cfg.Region)

if tc.expectedEp != "" {
require.NotNil(t, cfg.Endpoint)
assert.Equal(t, tc.expectedEp, *cfg.Endpoint)
}
if tc.expectedEp != "" {
require.NotNil(t, cfg.Endpoint)
assert.Equal(t, tc.expectedEp, *cfg.Endpoint)
}
})
}
}

0 comments on commit 54b7e30

Please sign in to comment.