diff options
author | Aanand Prasad <aanand.prasad@gmail.com> | 2016-07-22 19:43:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-22 19:43:42 +0100 |
commit | 30644d8ae06a1591537e47029ca07a5496172bf7 (patch) | |
tree | aa9e81193c19d1f4e91f1891d2e69149aebe449d | |
parent | 01cb969215fc91fa99642bd9718bfa0ce2a66a0c (diff) | |
parent | 1294d3c4103fc33949edc146be9bc91fd1a05c4d (diff) | |
download | docker-py-30644d8ae06a1591537e47029ca07a5496172bf7.tar.gz |
Merge pull request #1133 from kmala/master
Add optional auth config to docker push
-rw-r--r-- | docker/api/image.py | 27 | ||||
-rw-r--r-- | docs/api.md | 2 | ||||
-rw-r--r-- | tests/unit/image_test.py | 26 |
3 files changed, 45 insertions, 10 deletions
diff --git a/docker/api/image.py b/docker/api/image.py index 3e66347..2bdbce8 100644 --- a/docker/api/image.py +++ b/docker/api/image.py @@ -205,7 +205,7 @@ class ImageApiMixin(object): return self._result(response) def push(self, repository, tag=None, stream=False, - insecure_registry=False, decode=False): + insecure_registry=False, auth_config=None, decode=False): if insecure_registry: warnings.warn( INSECURE_REGISTRY_DEPRECATION_WARNING.format('push()'), @@ -224,15 +224,22 @@ class ImageApiMixin(object): if utils.compare_version('1.5', self._version) >= 0: # If we don't have any auth data so far, try reloading the config # file one more time in case anything showed up in there. - if not self._auth_configs: - self._auth_configs = auth.load_config() - authcfg = auth.resolve_authconfig(self._auth_configs, registry) - - # Do not fail here if no authentication exists for this specific - # registry as we can have a readonly pull. Just put the header if - # we can. - if authcfg: - headers['X-Registry-Auth'] = auth.encode_header(authcfg) + if auth_config is None: + log.debug('Looking for auth config') + if not self._auth_configs: + log.debug( + "No auth config in memory - loading from filesystem" + ) + self._auth_configs = auth.load_config() + authcfg = auth.resolve_authconfig(self._auth_configs, registry) + # Do not fail here if no authentication exists for this + # specific registry as we can have a readonly pull. Just + # put the header if we can. + if authcfg: + headers['X-Registry-Auth'] = auth.encode_header(authcfg) + else: + log.debug('Sending supplied auth config') + headers['X-Registry-Auth'] = auth.encode_header(auth_config) response = self._post_json( u, None, headers=headers, stream=stream, params=params diff --git a/docs/api.md b/docs/api.md index e058deb..9b3a726 100644 --- a/docs/api.md +++ b/docs/api.md @@ -801,6 +801,8 @@ command. * tag (str): An optional tag to push * stream (bool): Stream the output as a blocking generator * insecure_registry (bool): Use `http://` to connect to the registry +* auth_config (dict): Override the credentials that Client.login has set for this request + `auth_config` should contain the `username` and `password` keys to be valid. **Returns** (generator or str): The output of the upload diff --git a/tests/unit/image_test.py b/tests/unit/image_test.py index 8fd894c..b2b1dd6 100644 --- a/tests/unit/image_test.py +++ b/tests/unit/image_test.py @@ -2,6 +2,7 @@ import docker import pytest from . import fake_api +from docker import auth from .api_test import ( DockerClientTest, fake_request, DEFAULT_TIMEOUT_SECONDS, url_prefix, fake_resolve_authconfig @@ -262,6 +263,31 @@ class ImageTest(DockerClientTest): timeout=DEFAULT_TIMEOUT_SECONDS ) + def test_push_image_with_auth(self): + auth_config = { + 'username': "test_user", + 'password': "test_password", + 'serveraddress': "test_server", + } + encoded_auth = auth.encode_header(auth_config) + self.client.push( + fake_api.FAKE_IMAGE_NAME, tag=fake_api.FAKE_TAG_NAME, + auth_config=auth_config + ) + + fake_request.assert_called_with( + 'POST', + url_prefix + 'images/test_image/push', + params={ + 'tag': fake_api.FAKE_TAG_NAME, + }, + data='{}', + headers={'Content-Type': 'application/json', + 'X-Registry-Auth': encoded_auth}, + stream=False, + timeout=DEFAULT_TIMEOUT_SECONDS + ) + def test_push_image_stream(self): with mock.patch('docker.auth.auth.resolve_authconfig', fake_resolve_authconfig): |