Module: Docker::Template::Auth

Defined in:
lib/docker/template/auth.rb

Constant Summary collapse

DEFAULT_SERVER =

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

Class Method Summary collapse

Class Method Details

.auth_from_configObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docker/template/auth.rb', line 45

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

  unless credentials.empty?
    credentials["auths"].each do |server, info|
      username, password = Base64.decode64(info["auth"])
        .split(":", 2)

      Docker.authenticate!({
        "username" => username,
        "serveraddress" => server,
        "email" => info["email"],
        "password" => password
      })
    end
  end
end

.auth_from_envObject



34
35
36
37
38
39
40
41
# File 'lib/docker/template/auth.rb', line 34

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_env?Boolean

Returns:

  • (Boolean)


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

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

.hubObject



24
25
26
27
28
29
30
# File 'lib/docker/template/auth.rb', line 24

def hub
  return auth_from_env if auth_with_env?

  auth_from_config
rescue Docker::Error::AuthenticationError
  raise Error::UnsuccessfulAuth
end