Sha256: 18939d4085fc7b54f2de2f865e92ef5de3f30a4ec39757a56cdcdcd6808ed6c8

Contents?: true

Size: 1.88 KB

Versions: 48

Compression:

Stored size: 1.88 KB

Contents

=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 AwsService

    def initialize(full_image_name)
      @full_image_name = full_image_name
      @repo_domain = "#{full_image_name.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
      if File.exist?(docker_config)
        data = JSON.load(IO.read(docker_config))
        data["auths"][@repo_domain] = {auth: auth_token}
      else
        data = {"auths" => {@repo_domain => {auth: auth_token}}}
      end

      # Handle legacy docker clients that still have old format with https://
      legacy_entry = "https://#{@repo_domain}"
      data["auths"][legacy_entry] = {auth: auth_token}

      ensure_dotdocker_exists
      IO.write(docker_config, JSON.pretty_generate(data))
    end

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

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

    def docker_config
      "#{ENV['HOME']}/.docker/config.json"
    end

    def ensure_dotdocker_exists
      dirname = File.dirname(docker_config)
      FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
    end

  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
ufo-5.0.1 lib/ufo/ecr/auth.rb
ufo-5.0.0 lib/ufo/ecr/auth.rb
ufo-4.6.3 lib/ufo/ecr/auth.rb
ufo-4.6.2 lib/ufo/ecr/auth.rb
ufo-4.6.1 lib/ufo/ecr/auth.rb
ufo-4.6.0 lib/ufo/ecr/auth.rb
ufo-4.5.11 lib/ufo/ecr/auth.rb
ufo-4.5.10 lib/ufo/ecr/auth.rb
ufo-4.5.9 lib/ufo/ecr/auth.rb
ufo-4.5.8 lib/ufo/ecr/auth.rb
ufo-4.5.7 lib/ufo/ecr/auth.rb
ufo-4.5.6 lib/ufo/ecr/auth.rb
ufo-4.5.5 lib/ufo/ecr/auth.rb
ufo-4.5.4 lib/ufo/ecr/auth.rb
ufo-4.5.3 lib/ufo/ecr/auth.rb
ufo-4.5.2 lib/ufo/ecr/auth.rb
ufo-4.5.1 lib/ufo/ecr/auth.rb
ufo-4.5.0 lib/ufo/ecr/auth.rb
ufo-4.4.3 lib/ufo/ecr/auth.rb
ufo-4.4.2 lib/ufo/ecr/auth.rb