Class: Docker::Template::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/template/auth.rb

Constant Summary collapse

DEFAULT_SERVER =
"https://index.docker.io/v1/"

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Auth

Returns a new instance of Auth.



12
13
14
# File 'lib/docker/template/auth.rb', line 12

def initialize(repo)
  @repo = repo
end

Instance Method Details

#auth(skip: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/docker/template/auth.rb', line 27

def auth(skip: nil)
  return auth_from_cmd if auth_with_cmd? && skip != :cmd
  return auth_from_env if auth_with_env? && skip != :env
  auth_from_config

# Wrap around their error to create ours.
rescue Docker::Error::AuthenticationError
  raise Error::UnsuccessfulAuth
  # Something went wrong?
end

#auth_from_cmdObject



39
40
41
42
43
44
45
46
47
# File 'lib/docker/template/auth.rb', line 39

def auth_from_cmd
  case @repo.user
  when %r!^gcr\.io/! then auth_from_gcr
  else
    auth({
      skip: :cmd
    })
  end
end

#auth_from_configObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/docker/template/auth.rb', line 60

def auth_from_config
  cred = Pathutil.new("~/.docker/config.json")
  cred = cred.expand_path.read_json

  unless cred.empty?
    cred["auths"].each do |server, info|
      next if info.empty?

      user, pass = Base64.decode64(info["auth"]).split(":", 2)
      Docker.authenticate!({
        "username" => user,
        "serveraddress" => server,
        "email" => info["email"],
        "password" => pass
      })
    end
  end
end

#auth_from_envObject



50
51
52
53
54
55
56
57
# File 'lib/docker/template/auth.rb', line 50

def auth_from_env
  Docker.authenticate!({
    "username" => ENV["DOCKER_USERNAME"],
    "serveraddress" => ENV["DOCKER_SERVER"] || DEFAULT_SERVER,
    "password" => ENV["DOCKER_PASSWORD"],
    "email" => ENV["DOCKER_EMAIL"]
  })
end

#auth_with_cmd?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/docker/template/auth.rb', line 16

def auth_with_cmd?
  @repo.user =~ %r!/!
end

#auth_with_env?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/docker/template/auth.rb', line 20

def auth_with_env?
  ENV.key?("DOCKER_USERNAME") && \
  ENV.key?("DOCKER_PASSWORD") && \
  ENV.key?("DOCKER_EMAIL")
end