...

Source file src/github.com/awslabs/amazon-ecr-credential-helper/ecr-login/ecr.go

Documentation: github.com/awslabs/amazon-ecr-credential-helper/ecr-login

     1  // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"). You may
     4  // not use this file except in compliance with the License. A copy of the
     5  // License is located at
     6  //
     7  //	http://aws.amazon.com/apache2.0/
     8  //
     9  // or in the "license" file accompanying this file. This file is distributed
    10  // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    11  // express or implied. See the License for the specific language governing
    12  // permissions and limitations under the License.
    13  
    14  package ecr
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/sirupsen/logrus"
    22  
    23  	"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
    24  	"github.com/docker/docker-credential-helpers/credentials"
    25  )
    26  
    27  var notImplemented = errors.New("not implemented")
    28  
    29  type ECRHelper struct {
    30  	clientFactory api.ClientFactory
    31  	logger        *logrus.Logger
    32  }
    33  
    34  type Option func(*ECRHelper)
    35  
    36  // WithClientFactory sets the ClientFactory used to make API requests.
    37  func WithClientFactory(clientFactory api.ClientFactory) Option {
    38  	return func(e *ECRHelper) {
    39  		e.clientFactory = clientFactory
    40  	}
    41  }
    42  
    43  // WithLogger sets a new logger instance that writes to the given writer,
    44  // instead of the default writer which writes to stderr.
    45  //
    46  // This can be useful if callers want to redirect logging emitted by this tool
    47  // to another location.
    48  func WithLogger(w io.Writer) Option {
    49  	return func(e *ECRHelper) {
    50  		logger := logrus.New()
    51  		logger.Out = w
    52  		e.logger = logger
    53  	}
    54  }
    55  
    56  // NewECRHelper returns a new ECRHelper with the given options to override
    57  // default behavior.
    58  func NewECRHelper(opts ...Option) *ECRHelper {
    59  	e := &ECRHelper{
    60  		clientFactory: api.DefaultClientFactory{},
    61  		logger:        logrus.StandardLogger(),
    62  	}
    63  	for _, o := range opts {
    64  		o(e)
    65  	}
    66  
    67  	return e
    68  }
    69  
    70  // ensure ECRHelper adheres to the credentials.Helper interface
    71  var _ credentials.Helper = (*ECRHelper)(nil)
    72  
    73  func (ECRHelper) Add(creds *credentials.Credentials) error {
    74  	// This does not seem to get called
    75  	return notImplemented
    76  }
    77  
    78  func (ECRHelper) Delete(serverURL string) error {
    79  	// This does not seem to get called
    80  	return notImplemented
    81  }
    82  
    83  func (self ECRHelper) Get(serverURL string) (string, string, error) {
    84  	registry, err := api.ExtractRegistry(serverURL)
    85  	if err != nil {
    86  		self.logger.
    87  			WithError(err).
    88  			WithField("serverURL", serverURL).
    89  			Error("Error parsing the serverURL")
    90  		return "", "", credentials.NewErrCredentialsNotFound()
    91  	}
    92  
    93  	var client api.Client
    94  	if registry.FIPS {
    95  		client, err = self.clientFactory.NewClientWithFipsEndpoint(registry.Region)
    96  		if err != nil {
    97  			self.logger.WithError(err).Error("Error resolving FIPS endpoint")
    98  			return "", "", credentials.NewErrCredentialsNotFound()
    99  		}
   100  	} else {
   101  		client = self.clientFactory.NewClientFromRegion(registry.Region)
   102  	}
   103  
   104  	auth, err := client.GetCredentials(serverURL)
   105  	if err != nil {
   106  		self.logger.WithError(err).Error("Error retrieving credentials")
   107  		return "", "", credentials.NewErrCredentialsNotFound()
   108  	}
   109  	return auth.Username, auth.Password, nil
   110  }
   111  
   112  func (self ECRHelper) List() (map[string]string, error) {
   113  	self.logger.Debug("Listing credentials")
   114  	client := self.clientFactory.NewClientWithDefaults()
   115  
   116  	auths, err := client.ListCredentials()
   117  	if err != nil {
   118  		self.logger.WithError(err).Error("Error listing credentials")
   119  		return nil, fmt.Errorf("ecr: could not list credentials: %v", err)
   120  	}
   121  
   122  	result := map[string]string{}
   123  
   124  	for _, auth := range auths {
   125  		serverURL := auth.ProxyEndpoint
   126  		result[serverURL] = auth.Username
   127  	}
   128  	return result, nil
   129  }
   130  

View as plain text