diff --git a/src/dockerclient/auth.go b/src/dockerclient/auth.go index d94b23f5..9f60606b 100644 --- a/src/dockerclient/auth.go +++ b/src/dockerclient/auth.go @@ -19,10 +19,11 @@ package dockerclient import ( "encoding/base64" "fmt" - "github.com/grammarly/rocker/src/imagename" "strings" "sync" + "github.com/grammarly/rocker/src/imagename" + log "github.com/Sirupsen/logrus" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" @@ -54,7 +55,7 @@ func GetAuthForRegistry(auth *docker.AuthConfigurations, image *imagename.ImageN } // Optionally override auth took via aws-sdk (through ENV vars) if image.IsECR() { - if awsRegAuth, err := GetECRAuth(registry); err != nil && err != credentials.ErrNoValidProvidersFoundInChain { + if awsRegAuth, err := GetECRAuth(registry, image.GetECRRegion()); err != nil && err != credentials.ErrNoValidProvidersFoundInChain { return result, err } else if awsRegAuth.Username != "" { return awsRegAuth, nil @@ -85,7 +86,7 @@ func GetAuthForRegistry(auth *docker.AuthConfigurations, image *imagename.ImageN } // GetECRAuth requests AWS ECR API to get docker.AuthConfiguration token -func GetECRAuth(registry string) (result docker.AuthConfiguration, err error) { +func GetECRAuth(registry, region string) (result docker.AuthConfiguration, err error) { _ecrAuthCache.mu.Lock() defer _ecrAuthCache.mu.Unlock() @@ -97,9 +98,8 @@ func GetECRAuth(registry string) (result docker.AuthConfiguration, err error) { _ecrAuthCache.tokens[registry] = result }() - // TODO: take region from the registry hostname? cfg := &aws.Config{ - Region: aws.String("us-east-1"), + Region: aws.String(region), } if log.StandardLogger().Level >= log.DebugLevel { diff --git a/src/imagename/imagename.go b/src/imagename/imagename.go index 6d5e3127..e5500755 100644 --- a/src/imagename/imagename.go +++ b/src/imagename/imagename.go @@ -51,7 +51,7 @@ const ( ) var ( - ecrRe = regexp.MustCompile("^\\d+\\.dkr\\.ecr\\.[^\\.]+\\.amazonaws\\.com$") + ecrRe = regexp.MustCompile("^(\\d+)\\.dkr\\.ecr\\.([^\\.]+)\\.amazonaws\\.com$") ) // ImageName is the data structure with describes docker image name @@ -250,6 +250,12 @@ func (img ImageName) IsECR() bool { return ecrRe.MatchString(img.Registry) } +// GetECRRegion returns the regoin of the AWS ECR registry +func (img ImageName) GetECRRegion() string { + matches := ecrRe.FindStringSubmatch(img.Registry) + return matches[2] +} + // TagAsVersion return semver.Version of the current image tag in case it's in semver format func (img ImageName) TagAsVersion() (ver *semver.Version) { ver, _ = semver.NewVersion(strings.TrimPrefix(img.Tag, "v"))