Skip to content

Commit

Permalink
Parse .dockercfg for registry login information, fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Feb 11, 2014
1 parent c7926d8 commit 166e24e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,10 @@ email | Registry email | String | nil
password | Registry password | String | nil
username | Registry username | String | nil

Log into public registry:
Log into or register with public registry:

docker_registry 'https://index.docker.io/v1/' do
email '[email protected]'
username 'publicme'
password 'hope_this_is_in_encrypted_databag'
end
Expand All @@ -686,14 +687,6 @@ Log into private registry with optional port:
password 'still_hope_this_is_in_encrypted_databag'
end

Register with registry:

docker_registry 'https://index.docker.io/v1/' do
email '[email protected]'
username 'publicme'
password 'hope_this_is_in_encrypted_databag'
end

## Usage

### Default Installation
Expand Down
19 changes: 19 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ def docker_inspect_id(id)
inspect['id'] if inspect
end

def dockercfg_parse
require 'json'
dockercfg = JSON.parse(::File.read(::File.join(::Dir.home, '.dockercfg')))
dockercfg.each_pair do |k,v|
dockercfg[k].merge!(dockercfg_parse_auth(v['auth']))
end
dockercfg
end

def dockercfg_parse_auth(str)
require 'base64'
decoded_str = Base64.decode64(str)
if decoded_str
auth = {}
auth['username'], auth['password'] = decoded_str.split(':')
auth
end
end

def timeout
node['docker']['docker_daemon_timeout']
end
Expand Down
15 changes: 13 additions & 2 deletions providers/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ class CommandTimeout < RuntimeError; end
def load_current_resource
@current_resource = Chef::Resource::DockerRegistry.new(new_resource)
wait_until_ready!
# TODO: load current resource?
dockercfg = dockercfg_parse
if dockercfg && login_matches(dockercfg[new_resource.server])
Chef::Log.debug("Matched registry login: #{new_resource.server}: #{dockercfg[new_resource.server].to_s}")
@current_resource.server(new_resource.server)
@current_resource.username(dockercfg[new_resource.server]['username'])
@current_resource.password(dockercfg[new_resource.server]['password'])
end
@current_resource
end

Expand All @@ -27,7 +33,7 @@ def command_timeout_error_message
end

def logged_in?
@current_resource || true
@current_resource.server
end

def login
Expand All @@ -38,3 +44,8 @@ def login
)
docker_cmd!("login #{login_args} #{new_resource.server} ")
end

def login_matches(cfg)
return false unless cfg
cfg['username'] == new_resource.username && cfg['password'] == new_resource.password
end

0 comments on commit 166e24e

Please sign in to comment.