...

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

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

     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 api
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/aws/aws-sdk-go-v2/aws"
    20  	"github.com/aws/aws-sdk-go-v2/config"
    21  	"github.com/aws/aws-sdk-go-v2/service/ecr"
    22  	"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
    23  	"github.com/aws/smithy-go/middleware"
    24  	"github.com/aws/smithy-go/transport/http"
    25  	"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache"
    26  	"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/version"
    27  )
    28  
    29  // Options makes the constructors more configurable
    30  type Options struct {
    31  	Config   aws.Config
    32  	CacheDir string
    33  }
    34  
    35  // ClientFactory is a factory for creating clients to interact with ECR
    36  type ClientFactory interface {
    37  	NewClient(awsConfig aws.Config) Client
    38  	NewClientWithOptions(opts Options) Client
    39  	NewClientFromRegion(region string) Client
    40  	NewClientWithFipsEndpoint(region string) (Client, error)
    41  	NewClientWithDefaults() Client
    42  }
    43  
    44  // DefaultClientFactory is a default implementation of the ClientFactory
    45  type DefaultClientFactory struct{}
    46  
    47  var userAgentLoadOption = config.WithAPIOptions([]func(*middleware.Stack) error{
    48  	http.AddHeaderValue("User-Agent", "amazon-ecr-credential-helper/"+version.Version),
    49  })
    50  
    51  // NewClientWithDefaults creates the client and defaults region
    52  func (defaultClientFactory DefaultClientFactory) NewClientWithDefaults() Client {
    53  	awsConfig, err := config.LoadDefaultConfig(context.TODO(), userAgentLoadOption)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  
    58  	return defaultClientFactory.NewClientWithOptions(Options{Config: awsConfig})
    59  }
    60  
    61  // NewClientWithFipsEndpoint overrides the default ECR service endpoint in a given region to use the FIPS endpoint
    62  func (defaultClientFactory DefaultClientFactory) NewClientWithFipsEndpoint(region string) (Client, error) {
    63  	awsConfig, err := config.LoadDefaultConfig(
    64  		context.TODO(),
    65  		userAgentLoadOption,
    66  		config.WithRegion(region),
    67  		config.WithEndpointDiscovery(aws.EndpointDiscoveryEnabled),
    68  	)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return defaultClientFactory.NewClientWithOptions(Options{Config: awsConfig}), nil
    74  }
    75  
    76  // NewClientFromRegion uses the region to create the client
    77  func (defaultClientFactory DefaultClientFactory) NewClientFromRegion(region string) Client {
    78  	awsConfig, err := config.LoadDefaultConfig(
    79  		context.TODO(),
    80  		userAgentLoadOption,
    81  		config.WithRegion(region),
    82  	)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  
    87  	return defaultClientFactory.NewClientWithOptions(Options{
    88  		Config: awsConfig,
    89  	})
    90  }
    91  
    92  // NewClient Create new client with AWS Config
    93  func (defaultClientFactory DefaultClientFactory) NewClient(awsConfig aws.Config) Client {
    94  	return defaultClientFactory.NewClientWithOptions(Options{Config: awsConfig})
    95  }
    96  
    97  // NewClientWithOptions Create new client with Options
    98  func (defaultClientFactory DefaultClientFactory) NewClientWithOptions(opts Options) Client {
    99  	// The ECR Public API is only available in us-east-1 today
   100  	publicConfig := opts.Config.Copy()
   101  	publicConfig.Region = "us-east-1"
   102  	return &defaultClient{
   103  		ecrClient:       ecr.NewFromConfig(opts.Config),
   104  		ecrPublicClient: ecrpublic.NewFromConfig(publicConfig),
   105  		credentialCache: cache.BuildCredentialsCache(opts.Config, opts.CacheDir),
   106  	}
   107  }
   108  

View as plain text