Skip to content

Commit b6f6e72

Browse files
committed
Add registry auth header to inspect_distribution requests
Update docstring for auth_config parameter in pull, push, and inspect_distribution Signed-off-by: Joffrey F <[email protected]>
1 parent 28c9100 commit b6f6e72

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

docker/api/image.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,15 @@ def inspect_image(self, image):
247247

248248
@utils.minimum_version('1.30')
249249
@utils.check_resource('image')
250-
def inspect_distribution(self, image):
250+
def inspect_distribution(self, image, auth_config=None):
251251
"""
252252
Get image digest and platform information by contacting the registry.
253253
254254
Args:
255255
image (str): The image name to inspect
256+
auth_config (dict): Override the credentials that are found in the
257+
config for this request. ``auth_config`` should contain the
258+
``username`` and ``password`` keys to be valid.
256259
257260
Returns:
258261
(dict): A dict containing distribution data
@@ -261,9 +264,21 @@ def inspect_distribution(self, image):
261264
:py:class:`docker.errors.APIError`
262265
If the server returns an error.
263266
"""
267+
registry, _ = auth.resolve_repository_name(image)
268+
269+
headers = {}
270+
if auth_config is None:
271+
header = auth.get_config_header(self, registry)
272+
if header:
273+
headers['X-Registry-Auth'] = header
274+
else:
275+
log.debug('Sending supplied auth config')
276+
headers['X-Registry-Auth'] = auth.encode_header(auth_config)
277+
278+
url = self._url("/distribution/{0}/json", image)
264279

265280
return self._result(
266-
self._get(self._url("/distribution/{0}/json", image)), True
281+
self._get(url, headers=headers), True
267282
)
268283

269284
def load_image(self, data, quiet=None):
@@ -336,10 +351,9 @@ def pull(self, repository, tag=None, stream=False, auth_config=None,
336351
tag (str): The tag to pull
337352
stream (bool): Stream the output as a generator. Make sure to
338353
consume the generator, otherwise pull might get cancelled.
339-
auth_config (dict): Override the credentials that
340-
:py:meth:`~docker.api.daemon.DaemonApiMixin.login` has set for
341-
this request. ``auth_config`` should contain the ``username``
342-
and ``password`` keys to be valid.
354+
auth_config (dict): Override the credentials that are found in the
355+
config for this request. ``auth_config`` should contain the
356+
``username`` and ``password`` keys to be valid.
343357
decode (bool): Decode the JSON data from the server into dicts.
344358
Only applies with ``stream=True``
345359
platform (str): Platform in the format ``os[/arch[/variant]]``
@@ -414,10 +428,9 @@ def push(self, repository, tag=None, stream=False, auth_config=None,
414428
repository (str): The repository to push to
415429
tag (str): An optional tag to push
416430
stream (bool): Stream the output as a blocking generator
417-
auth_config (dict): Override the credentials that
418-
:py:meth:`~docker.api.daemon.DaemonApiMixin.login` has set for
419-
this request. ``auth_config`` should contain the ``username``
420-
and ``password`` keys to be valid.
431+
auth_config (dict): Override the credentials that are found in the
432+
config for this request. ``auth_config`` should contain the
433+
``username`` and ``password`` keys to be valid.
421434
decode (bool): Decode the JSON data from the server into dicts.
422435
Only applies with ``stream=True``
423436

docker/models/images.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,26 @@ def get(self, name):
315315
"""
316316
return self.prepare_model(self.client.api.inspect_image(name))
317317

318-
def get_registry_data(self, name):
318+
def get_registry_data(self, name, auth_config=None):
319319
"""
320320
Gets the registry data for an image.
321321
322322
Args:
323323
name (str): The name of the image.
324+
auth_config (dict): Override the credentials that are found in the
325+
config for this request. ``auth_config`` should contain the
326+
``username`` and ``password`` keys to be valid.
324327
325328
Returns:
326329
(:py:class:`RegistryData`): The data object.
330+
327331
Raises:
328332
:py:class:`docker.errors.APIError`
329333
If the server returns an error.
330334
"""
331335
return RegistryData(
332336
image_name=name,
333-
attrs=self.client.api.inspect_distribution(name),
337+
attrs=self.client.api.inspect_distribution(name, auth_config),
334338
client=self.client,
335339
collection=self,
336340
)
@@ -404,10 +408,9 @@ def pull(self, repository, tag=None, **kwargs):
404408
Args:
405409
repository (str): The repository to pull
406410
tag (str): The tag to pull
407-
auth_config (dict): Override the credentials that
408-
:py:meth:`~docker.client.DockerClient.login` has set for
409-
this request. ``auth_config`` should contain the ``username``
410-
and ``password`` keys to be valid.
411+
auth_config (dict): Override the credentials that are found in the
412+
config for this request. ``auth_config`` should contain the
413+
``username`` and ``password`` keys to be valid.
411414
platform (str): Platform in the format ``os[/arch[/variant]]``
412415
413416
Returns:

0 commit comments

Comments
 (0)