Skip to content

Commit d300f5f

Browse files
committed
Merge pull request #654 from Melraidin/master
Prefer new Docker config location and format.
2 parents 5375433 + 6609586 commit d300f5f

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

docker/auth/auth.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from .. import errors
2424

2525
INDEX_URL = 'https://index.docker.io/v1/'
26-
DOCKER_CONFIG_FILENAME = '.dockercfg'
26+
DOCKER_CONFIG_FILENAME = os.path.join('.docker', 'config.json')
27+
LEGACY_DOCKER_CONFIG_FILENAME = '.dockercfg'
2728

2829

2930
def expand_registry_url(hostname, insecure=False):
@@ -107,6 +108,29 @@ def encode_full_header(auth):
107108
return encode_header({'configs': auth})
108109

109110

111+
def parse_auth(entries):
112+
"""
113+
Parses authentication entries
114+
115+
Args:
116+
entries: Dict of authentication entries.
117+
118+
Returns:
119+
Authentication registry.
120+
"""
121+
122+
conf = {}
123+
for registry, entry in six.iteritems(entries):
124+
username, password = decode_auth(entry['auth'])
125+
conf[registry] = {
126+
'username': username,
127+
'password': password,
128+
'email': entry['email'],
129+
'serveraddress': registry,
130+
}
131+
return conf
132+
133+
110134
def load_config(config_path=None):
111135
"""
112136
Loads authentication data from a Docker configuration file in the given
@@ -115,26 +139,34 @@ def load_config(config_path=None):
115139
conf = {}
116140
data = None
117141

142+
# Prefer ~/.docker/config.json.
118143
config_file = config_path or os.path.join(os.path.expanduser('~'),
119144
DOCKER_CONFIG_FILENAME)
120145

146+
if os.path.exists(config_file):
147+
try:
148+
with open(config_file) as f:
149+
for section, data in six.iteritems(json.load(f)):
150+
if section != 'auths':
151+
continue
152+
return parse_auth(data)
153+
except (IOError, KeyError, ValueError):
154+
# Likely missing new Docker config file or it's in an
155+
# unknown format, continue to attempt to read old location
156+
# and format.
157+
pass
158+
159+
config_file = config_path or os.path.join(os.path.expanduser('~'),
160+
LEGACY_DOCKER_CONFIG_FILENAME)
161+
121162
# if config path doesn't exist return empty config
122163
if not os.path.exists(config_file):
123164
return {}
124165

125-
# First try as JSON
166+
# Try reading legacy location as JSON.
126167
try:
127168
with open(config_file) as f:
128-
conf = {}
129-
for registry, entry in six.iteritems(json.load(f)):
130-
username, password = decode_auth(entry['auth'])
131-
conf[registry] = {
132-
'username': username,
133-
'password': password,
134-
'email': entry['email'],
135-
'serveraddress': registry,
136-
}
137-
return conf
169+
return parse_auth(json.load(f))
138170
except:
139171
pass
140172

0 commit comments

Comments
 (0)