Sha256: 1dda80730bd803f686fa66e91b975709ccb50c07a6cf300d89c8cd08bdb6afd3

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'aws-sdk-ecr'

module RakeDocker
  module Authentication
    class ECR
      def initialize(&block)
        @config =
          Struct.new(:region, :registry_id)
                .new(nil, nil)
        block.call(@config)

        @ecr_client = Aws::ECR::Client.new(region: @config.region)
      end

      def arity
        0
      end

      def call
        email = 'none'
        registry_id = resolve_registry_id
        token = get_authorization_token(registry_id)
        proxy_endpoint = extract_proxy_endpoint(token)
        username, password = extract_credentials(token)

        make_credentials_hash(email, password, username, proxy_endpoint)
      end

      private

      def make_credentials_hash(email, password, username, proxy_endpoint)
        {
          username:,
          password:,
          email:,
          serveraddress: proxy_endpoint
        }
      end

      def resolve_registry_id
        if @config.registry_id.respond_to?(:call)
          @config.registry_id.call
        else
          @config.registry_id
        end
      end

      def get_authorization_token(registry_id)
        @ecr_client
          .get_authorization_token(registry_ids: [registry_id])
          .authorization_data[0]
      end

      def extract_proxy_endpoint(token)
        token.proxy_endpoint
      end

      def extract_credentials(token)
        Base64.decode64(token.authorization_token).split(':')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rake_docker-2.22.0.pre.3 lib/rake_docker/authentication/ecr.rb
rake_docker-2.22.0.pre.2 lib/rake_docker/authentication/ecr.rb
rake_docker-2.22.0.pre.1 lib/rake_docker/authentication/ecr.rb
rake_docker-2.21.0 lib/rake_docker/authentication/ecr.rb
rake_docker-2.20.0.pre.8 lib/rake_docker/authentication/ecr.rb