Skip to content

Commit

Permalink
fixing setAssumeRoleSource to return source collision error
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Oct 11, 2018
1 parent 4b39ef4 commit 44e0c89
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
9 changes: 3 additions & 6 deletions aws/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/internal/container"
"github.com/aws/aws-sdk-go/internal/shareddefaults"
)

// A Defaults provides a collection of default values for SDK clients.
Expand Down Expand Up @@ -115,9 +115,6 @@ func CredProviders(cfg *aws.Config, handlers request.Handlers) []credentials.Pro
const (
httpProviderAuthorizationEnvVar = "AWS_CONTAINER_AUTHORIZATION_TOKEN"
httpProviderEnvVar = "AWS_CONTAINER_CREDENTIALS_FULL_URI"
// EcsCredsProviderEnvVar is an environmental variable key used to
// determine which path needs to be hit.
EcsCredsProviderEnvVar = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
)

// RemoteCredProvider returns a credentials provider for the default remote
Expand All @@ -127,8 +124,8 @@ func RemoteCredProvider(cfg aws.Config, handlers request.Handlers) credentials.P
return localHTTPCredProvider(cfg, handlers, u)
}

if uri := os.Getenv(EcsCredsProviderEnvVar); len(uri) > 0 {
u := fmt.Sprintf("%s%s", container.URI, uri)
if uri := os.Getenv(shareddefaults.ECSCredsProviderEnvVar); len(uri) > 0 {
u := fmt.Sprintf("%s%s", shareddefaults.ECSContainerURI, uri)
return httpCredProvider(cfg, handlers, u)
}

Expand Down
3 changes: 2 additions & 1 deletion aws/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/internal/shareddefaults"
)

func TestHTTPCredProvider(t *testing.T) {
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestHTTPCredProvider(t *testing.T) {

func TestECSCredProvider(t *testing.T) {
defer os.Clearenv()
os.Setenv(EcsCredsProviderEnvVar, "/abc/123")
os.Setenv(shareddefaults.ECSCredsProviderEnvVar, "/abc/123")

provider := RemoteCredProvider(aws.Config{}, request.Handlers{})
if provider == nil {
Expand Down
3 changes: 2 additions & 1 deletion aws/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/internal/shareddefaults"
)

const (
Expand Down Expand Up @@ -488,7 +489,7 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
envCfg.Creds,
)
case credSourceECSContainer:
if len(os.Getenv(defaults.EcsCredsProviderEnvVar)) == 0 {
if len(os.Getenv(shareddefaults.ECSCredsProviderEnvVar)) == 0 {
return ErrSharedConfigECSContainerEnvVarEmpty
}

Expand Down
4 changes: 2 additions & 2 deletions aws/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/internal/container"
"github.com/aws/aws-sdk-go/internal/shareddefaults"
"github.com/aws/aws-sdk-go/service/s3"
)

Expand Down Expand Up @@ -592,7 +592,7 @@ func TestSharedConfigCredentialSource(t *testing.T) {
}
}))

container.URI = ecsMetadataServer.URL
shareddefaults.ECSContainerURI = ecsMetadataServer.URL

stsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(assumeRoleRespMsg, time.Now().Add(15*time.Minute).Format("2006-01-02T15:04:05Z"))))
Expand Down
8 changes: 5 additions & 3 deletions aws/session/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func loadSharedConfig(profile string, filenames []string) (sharedConfig, error)
return sharedConfig{}, err
}

if len(cfg.AssumeRole.SourceProfile) > 0 || len(cfg.AssumeRole.CredentialSource) > 0 {
if len(cfg.AssumeRole.SourceProfile) > 0 {
if err := cfg.setAssumeRoleSource(profile, files); err != nil {
return sharedConfig{}, err
}
Expand Down Expand Up @@ -130,8 +130,10 @@ func (cfg *sharedConfig) setAssumeRoleSource(origProfile string, files []sharedC
var assumeRoleSrc sharedConfig

if len(cfg.AssumeRole.CredentialSource) > 0 {
cfg.AssumeRoleSource = &sharedConfig{}
return nil
// setAssumeRoleSource is only called when source_profile is found.
// If both source_profile and credential_source are set, then
// ErrSharedConfigSourceCollision will be returned
return ErrSharedConfigSourceCollision
}

// Multiple level assume role chains are not support
Expand Down
6 changes: 0 additions & 6 deletions internal/container/uri.go

This file was deleted.

12 changes: 12 additions & 0 deletions internal/shareddefaults/ecs_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package shareddefaults

const (
// ECSCredsProviderEnvVar is an environmental variable key used to
// determine which path needs to be hit.
ECSCredsProviderEnvVar = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
)

// ECSContainerURI is the endpoint to retrieve container credentials.
// This can be overriden to test to ensure the credential
// process is behaving correctly.
var ECSContainerURI = "http://169.254.170.2"

0 comments on commit 44e0c89

Please sign in to comment.