Skip to content

Commit be68782

Browse files
committed
Add code based on awslabs#181
1 parent d883bee commit be68782

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ecr-login/api/factory.go

+18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ClientFactory interface {
3939
NewClientWithOptions(opts Options) Client
4040
NewClientFromRegion(region string) Client
4141
NewClientWithFipsEndpoint(region string) (Client, error)
42+
NewClientWithExplicitProfile(profile string) (Client, error)
4243
NewClientWithDefaults() Client
4344
}
4445

@@ -84,6 +85,23 @@ func (defaultClientFactory DefaultClientFactory) NewClientWithFipsEndpoint(regio
8485
}), nil
8586
}
8687

88+
// NewClientWithExplicitProfile creates a session with a custom profile set
89+
func (defaultClientFactory DefaultClientFactory) NewClientWithExplicitProfile(profile string) (Client, error) {
90+
awsSession, err := session.NewSessionWithOptions(session.Options{
91+
Profile: profile,
92+
SharedConfigState: loadSharedConfigState(),
93+
})
94+
if err != nil {
95+
return nil, err
96+
}
97+
awsSession.Handlers.Build.PushBackNamed(userAgentHandler)
98+
awsConfig := awsSession.Config
99+
return defaultClientFactory.NewClientWithOptions(Options{
100+
Session: awsSession,
101+
Config: awsConfig,
102+
}), nil
103+
}
104+
87105
// NewClientFromRegion uses the region to create the client
88106
func (defaultClientFactory DefaultClientFactory) NewClientFromRegion(region string) Client {
89107
awsSession := session.Must(session.NewSessionWithOptions(session.Options{

ecr-login/ecr.go

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ecr
1616
import (
1717
"errors"
1818
"fmt"
19+
"os"
1920

2021
"github.com/sirupsen/logrus"
2122

@@ -52,13 +53,21 @@ func (self ECRHelper) Get(serverURL string) (string, string, error) {
5253
return "", "", credentials.NewErrCredentialsNotFound()
5354
}
5455

56+
profile, profile_exists := os.LookupEnv("AWS_PROFILE")
57+
5558
var client api.Client
5659
if registry.FIPS {
5760
client, err = self.ClientFactory.NewClientWithFipsEndpoint(registry.Region)
5861
if err != nil {
5962
logrus.WithError(err).Error("Error resolving FIPS endpoint")
6063
return "", "", credentials.NewErrCredentialsNotFound()
6164
}
65+
} else if profile_exists {
66+
client, err = self.ClientFactory.NewClientWithExplicitProfile(profile)
67+
if err != nil {
68+
logrus.WithError(err).Error("Error creating client with explicit profile")
69+
return "", "", credentials.NewErrCredentialsNotFound()
70+
}
6271
} else {
6372
client = self.ClientFactory.NewClientFromRegion(registry.Region)
6473
}

0 commit comments

Comments
 (0)