23
23
from .. import errors
24
24
25
25
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'
27
28
28
29
29
30
def expand_registry_url (hostname , insecure = False ):
@@ -107,6 +108,29 @@ def encode_full_header(auth):
107
108
return encode_header ({'configs' : auth })
108
109
109
110
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
+
110
134
def load_config (config_path = None ):
111
135
"""
112
136
Loads authentication data from a Docker configuration file in the given
@@ -115,26 +139,34 @@ def load_config(config_path=None):
115
139
conf = {}
116
140
data = None
117
141
142
+ # Prefer ~/.docker/config.json.
118
143
config_file = config_path or os .path .join (os .path .expanduser ('~' ),
119
144
DOCKER_CONFIG_FILENAME )
120
145
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
+
121
162
# if config path doesn't exist return empty config
122
163
if not os .path .exists (config_file ):
123
164
return {}
124
165
125
- # First try as JSON
166
+ # Try reading legacy location as JSON.
126
167
try :
127
168
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 ))
138
170
except :
139
171
pass
140
172
0 commit comments