Skip to content

Commit c344660

Browse files
authored
Merge pull request #2188 from docker/c6374-credhelpers
Modernize auth management
2 parents b72fb1e + cc38efa commit c344660

File tree

9 files changed

+530
-286
lines changed

9 files changed

+530
-286
lines changed

docker/api/build.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -288,46 +288,29 @@ def _set_auth_headers(self, headers):
288288
# file one more time in case anything showed up in there.
289289
if not self._auth_configs:
290290
log.debug("No auth config in memory - loading from filesystem")
291-
self._auth_configs = auth.load_config()
291+
self._auth_configs = auth.load_config(
292+
credsore_env=self.credsore_env
293+
)
292294

293295
# Send the full auth configuration (if any exists), since the build
294296
# could use any (or all) of the registries.
295297
if self._auth_configs:
296-
auth_cfgs = self._auth_configs
297-
auth_data = {}
298-
if auth_cfgs.get('credsStore'):
299-
# Using a credentials store, we need to retrieve the
300-
# credentials for each registry listed in the config.json file
301-
# Matches CLI behavior: https://github.com/docker/docker/blob/
302-
# 67b85f9d26f1b0b2b240f2d794748fac0f45243c/cliconfig/
303-
# credentials/native_store.go#L68-L83
304-
for registry in auth_cfgs.get('auths', {}).keys():
305-
auth_data[registry] = auth.resolve_authconfig(
306-
auth_cfgs, registry,
307-
credstore_env=self.credstore_env,
308-
)
309-
else:
310-
for registry in auth_cfgs.get('credHelpers', {}).keys():
311-
auth_data[registry] = auth.resolve_authconfig(
312-
auth_cfgs, registry,
313-
credstore_env=self.credstore_env
314-
)
315-
for registry, creds in auth_cfgs.get('auths', {}).items():
316-
if registry not in auth_data:
317-
auth_data[registry] = creds
318-
# See https://github.com/docker/docker-py/issues/1683
319-
if auth.INDEX_NAME in auth_data:
320-
auth_data[auth.INDEX_URL] = auth_data[auth.INDEX_NAME]
298+
auth_data = self._auth_configs.get_all_credentials()
299+
300+
# See https://github.com/docker/docker-py/issues/1683
301+
if auth.INDEX_URL not in auth_data and auth.INDEX_URL in auth_data:
302+
auth_data[auth.INDEX_URL] = auth_data.get(auth.INDEX_NAME, {})
321303

322304
log.debug(
323305
'Sending auth config ({0})'.format(
324306
', '.join(repr(k) for k in auth_data.keys())
325307
)
326308
)
327309

328-
headers['X-Registry-Config'] = auth.encode_header(
329-
auth_data
330-
)
310+
if auth_data:
311+
headers['X-Registry-Config'] = auth.encode_header(
312+
auth_data
313+
)
331314
else:
332315
log.debug('No auth config found')
333316

docker/api/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, base_url=None, version=None,
115115

116116
self._general_configs = config.load_general_config()
117117
self._auth_configs = auth.load_config(
118-
config_dict=self._general_configs
118+
config_dict=self._general_configs, credstore_env=credstore_env,
119119
)
120120
self.credstore_env = credstore_env
121121

@@ -480,4 +480,6 @@ def reload_config(self, dockercfg_path=None):
480480
Returns:
481481
None
482482
"""
483-
self._auth_configs = auth.load_config(dockercfg_path)
483+
self._auth_configs = auth.load_config(
484+
dockercfg_path, credstore_env=self.credstore_env
485+
)

docker/api/daemon.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ def login(self, username, password=None, email=None, registry=None,
124124
# If dockercfg_path is passed check to see if the config file exists,
125125
# if so load that config.
126126
if dockercfg_path and os.path.exists(dockercfg_path):
127-
self._auth_configs = auth.load_config(dockercfg_path)
127+
self._auth_configs = auth.load_config(
128+
dockercfg_path, credstore_env=self.credstore_env
129+
)
128130
elif not self._auth_configs:
129-
self._auth_configs = auth.load_config()
131+
self._auth_configs = auth.load_config(
132+
credstore_env=self.credstore_env
133+
)
130134

131-
authcfg = auth.resolve_authconfig(
132-
self._auth_configs, registry, credstore_env=self.credstore_env,
133-
)
135+
authcfg = self._auth_configs.resolve_authconfig(registry)
134136
# If we found an existing auth config for this registry and username
135137
# combination, we can return it immediately unless reauth is requested.
136138
if authcfg and authcfg.get('username', None) == username \
@@ -146,9 +148,7 @@ def login(self, username, password=None, email=None, registry=None,
146148

147149
response = self._post_json(self._url('/auth'), data=req_data)
148150
if response.status_code == 200:
149-
if 'auths' not in self._auth_configs:
150-
self._auth_configs['auths'] = {}
151-
self._auth_configs['auths'][registry or auth.INDEX_NAME] = req_data
151+
self._auth_configs.add_auth(registry or auth.INDEX_NAME, req_data)
152152
return self._result(response, json=True)
153153

154154
def ping(self):

0 commit comments

Comments
 (0)