Skip to content

Commit

Permalink
Allow callers to set log output
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <jasonhall@redhat.com>
  • Loading branch information
imjasonh committed Feb 14, 2022
1 parent 69c85dc commit 276ba67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
3 changes: 1 addition & 2 deletions ecr-login/cli/docker-credential-ecr-login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"

ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/config"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/version"
"github.com/docker/docker-credential-helpers/credentials"
Expand All @@ -42,5 +41,5 @@ func main() {
}

config.SetupLogger()
credentials.Serve(ecr.ECRHelper{ClientFactory: api.DefaultClientFactory{}})
credentials.Serve(ecr.NewECRHelper())
}
46 changes: 37 additions & 9 deletions ecr-login/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ecr
import (
"errors"
"fmt"
"io"

"github.com/sirupsen/logrus"

Expand All @@ -26,7 +27,34 @@ import (
var notImplemented = errors.New("not implemented")

type ECRHelper struct {
ClientFactory api.ClientFactory
clientFactory api.ClientFactory
logger *logrus.Logger
}

type Option func(*ECRHelper)

func WithClientFactory(clientFactory api.ClientFactory) Option {
return func(e *ECRHelper) {
e.clientFactory = clientFactory
}
}

func WithLogOutput(w io.Writer) Option {
return func(e *ECRHelper) {
e.logger.Out = w
}
}

func NewECRHelper(opts ...Option) *ECRHelper {
e := &ECRHelper{
clientFactory: api.DefaultClientFactory{},
logger: logrus.StandardLogger(),
}
for _, o := range opts {
o(e)
}

return e
}

// ensure ECRHelper adheres to the credentials.Helper interface
Expand All @@ -45,7 +73,7 @@ func (ECRHelper) Delete(serverURL string) error {
func (self ECRHelper) Get(serverURL string) (string, string, error) {
registry, err := api.ExtractRegistry(serverURL)
if err != nil {
logrus.
self.logger.
WithError(err).
WithField("serverURL", serverURL).
Error("Error parsing the serverURL")
Expand All @@ -54,30 +82,30 @@ func (self ECRHelper) Get(serverURL string) (string, string, error) {

var client api.Client
if registry.FIPS {
client, err = self.ClientFactory.NewClientWithFipsEndpoint(registry.Region)
client, err = self.clientFactory.NewClientWithFipsEndpoint(registry.Region)
if err != nil {
logrus.WithError(err).Error("Error resolving FIPS endpoint")
self.logger.WithError(err).Error("Error resolving FIPS endpoint")
return "", "", credentials.NewErrCredentialsNotFound()
}
} else {
client = self.ClientFactory.NewClientFromRegion(registry.Region)
client = self.clientFactory.NewClientFromRegion(registry.Region)
}

auth, err := client.GetCredentials(serverURL)
if err != nil {
logrus.WithError(err).Error("Error retrieving credentials")
self.logger.WithError(err).Error("Error retrieving credentials")
return "", "", credentials.NewErrCredentialsNotFound()
}
return auth.Username, auth.Password, nil
}

func (self ECRHelper) List() (map[string]string, error) {
logrus.Debug("Listing credentials")
client := self.ClientFactory.NewClientWithDefaults()
self.logger.Debug("Listing credentials")
client := self.clientFactory.NewClientWithDefaults()

auths, err := client.ListCredentials()
if err != nil {
logrus.WithError(err).Error("Error listing credentials")
self.logger.WithError(err).Error("Error listing credentials")
return nil, fmt.Errorf("ecr: could not list credentials: %v", err)
}

Expand Down
20 changes: 6 additions & 14 deletions ecr-login/ecr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"

ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/mocks"
mock_api "github.com/awslabs/amazon-ecr-credential-helper/ecr-login/mocks"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/stretchr/testify/assert"
)
Expand All @@ -36,9 +36,7 @@ func TestGetSuccess(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := &ECRHelper{
ClientFactory: factory,
}
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
Expand All @@ -62,9 +60,7 @@ func TestGetError(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := &ECRHelper{
ClientFactory: factory,
}
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
Expand All @@ -78,7 +74,7 @@ func TestGetError(t *testing.T) {
}

func TestGetNoMatch(t *testing.T) {
helper := &ECRHelper{}
helper := NewECRHelper(WithClientFactory(nil))

username, password, err := helper.Get("not-ecr-server-url")
assert.True(t, credentials.IsErrCredentialsNotFound(err))
Expand All @@ -90,9 +86,7 @@ func TestListSuccess(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := &ECRHelper{
ClientFactory: factory,
}
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
Expand All @@ -113,9 +107,7 @@ func TestListFailure(t *testing.T) {
factory := &mock_api.MockClientFactory{}
client := &mock_api.MockClient{}

helper := &ECRHelper{
ClientFactory: factory,
}
helper := NewECRHelper(WithClientFactory(factory))

factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
Expand Down

0 comments on commit 276ba67

Please sign in to comment.