Sha256: 7eeeaa00ea2eb6d27a190c68323d4db69539469aca4824fd5e73ea7f11d936b7

Contents?: true

Size: 1.76 KB

Versions: 10

Compression:

Stored size: 1.76 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
      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

10 entries across 10 versions & 1 rubygems

Version Path
ufo-3.4.3 lib/ufo/ecr/auth.rb
ufo-3.4.2 lib/ufo/ecr/auth.rb
ufo-3.4.1 lib/ufo/ecr/auth.rb
ufo-3.4.0 lib/ufo/ecr/auth.rb
ufo-3.3.2 lib/ufo/ecr/auth.rb
ufo-3.3.1 lib/ufo/ecr/auth.rb
ufo-3.3.0 lib/ufo/ecr/auth.rb
ufo-3.2.2 lib/ufo/ecr/auth.rb
ufo-3.2.1 lib/ufo/ecr/auth.rb
ufo-3.2.0 lib/ufo/ecr/auth.rb