Sha256: cc74e8ea04238e156217c7678764c525a375b5c6df73aa2a0479e1e08d5b75fa

Contents?: true

Size: 1.59 KB

Versions: 36

Compression:

Stored size: 1.59 KB

Contents

require 'open3'

=begin
Normally, you must authorized to AWS ECR to push to their registry with:

  eval $(aws ecr get-login --no-include-email)

If you haven't ever ran the ecr get-login command before then you'll get this error:

  no basic auth credentials

If you have ran it before but the auto token has expired you'll get this message:

  denied: Your Authorization Token has expired. Please run 'aws ecr get-login' to fetch a new one.

This class manipulates the ~/.docker/config.json file which is an internal docker file to automatically update the auto token for you.  If that format changes, the update will need to be updated.
=end
module Ufo
  class Ecr::Auth
    include Ufo::AwsServices
    include Ufo::Utils::Logging

    def initialize(docker_image)
      @docker_image = docker_image
      @repo_domain = "#{docker_image.split('/').first}"
    end

    def update
      # wont update auth token unless the image being pushed in the ECR image format
      return unless ecr_image?

      auth_token = fetch_auth_token
      username, password = Base64.decode64(auth_token).split(':')

      command = "docker login -u #{username} --password-stdin #{@repo_domain}"
      logger.debug "=> #{command}".color(:green)
      *, status = Open3.capture3(command, stdin_data: password)
      unless status.success?
        logger.error "ERROR: The docker failed to login.".color(:red)
        exit 1
      end
    end

    def ecr_image?
      !!(@docker_image =~ /\.amazonaws\.com/)
    end

    def fetch_auth_token
      ecr.get_authorization_token.authorization_data.first.authorization_token
    end

  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
ufo-6.3.13 lib/ufo/ecr/auth.rb
ufo-6.3.12 lib/ufo/ecr/auth.rb
ufo-6.3.11 lib/ufo/ecr/auth.rb
ufo-6.3.10 lib/ufo/ecr/auth.rb
ufo-6.3.9 lib/ufo/ecr/auth.rb
ufo-6.3.8 lib/ufo/ecr/auth.rb
ufo-6.3.7 lib/ufo/ecr/auth.rb
ufo-6.3.6 lib/ufo/ecr/auth.rb
ufo-6.3.5 lib/ufo/ecr/auth.rb
ufo-6.3.4 lib/ufo/ecr/auth.rb
ufo-6.3.3 lib/ufo/ecr/auth.rb
ufo-6.3.2 lib/ufo/ecr/auth.rb
ufo-6.3.1 lib/ufo/ecr/auth.rb
ufo-6.3.0 lib/ufo/ecr/auth.rb
ufo-6.2.5 lib/ufo/ecr/auth.rb
ufo-6.2.4 lib/ufo/ecr/auth.rb
ufo-6.2.3 lib/ufo/ecr/auth.rb
ufo-6.2.2 lib/ufo/ecr/auth.rb
ufo-6.2.1 lib/ufo/ecr/auth.rb
ufo-6.2.0 lib/ufo/ecr/auth.rb