Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement using dockercfg from a file-like object #2040

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions docker/api/client.py
Expand Up @@ -441,16 +441,16 @@ def get_adapter(self, url):
def api_version(self):
return self._version

def reload_config(self, dockercfg_path=None):
def reload_config(self, dockercfg=None):
"""
Force a reload of the auth configuration

Args:
dockercfg_path (str): Use a custom path for the Docker config file
(default ``$HOME/.docker/config.json`` if present,
otherwise``$HOME/.dockercfg``)
dockercfg (str or file obj): Use a file path or file object for
the Docker config file (default ``$HOME/.docker/config.json``
if present, otherwise``$HOME/.dockercfg``)

Returns:
None
"""
self._auth_configs = auth.load_config(dockercfg_path)
self._auth_configs = auth.load_config(dockercfg)
15 changes: 8 additions & 7 deletions docker/api/daemon.py
Expand Up @@ -95,7 +95,7 @@ def info(self):
return self._result(self._get(self._url("/info")), True)

def login(self, username, password=None, email=None, registry=None,
reauth=False, dockercfg_path=None):
reauth=False, dockercfg=None):
"""
Authenticate with a registry. Similar to the ``docker login`` command.

Expand All @@ -107,9 +107,9 @@ def login(self, username, password=None, email=None, registry=None,
``https://index.docker.io/v1/``
reauth (bool): Whether or not to refresh existing authentication on
the Docker server.
dockercfg_path (str): Use a custom path for the Docker config file
(default ``$HOME/.docker/config.json`` if present,
otherwise``$HOME/.dockercfg``)
dockercfg (str or file obj): Use a file path or file object for
the Docker config file (default ``$HOME/.docker/config.json``
if present, otherwise``$HOME/.dockercfg``)

Returns:
(dict): The response from the login request
Expand All @@ -121,10 +121,11 @@ def login(self, username, password=None, email=None, registry=None,

# 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 dockercfg_path is passed check to see if the config file exists,
# If dockercfg is passed check to see if the config file exists,
# if so load that config.
if dockercfg_path and os.path.exists(dockercfg_path):
self._auth_configs = auth.load_config(dockercfg_path)
if hasattr(dockercfg, 'read') or \
(dockercfg and os.path.exists(dockercfg)):
self._auth_configs = auth.load_config(dockercfg)
elif not self._auth_configs:
self._auth_configs = auth.load_config()

Expand Down
8 changes: 6 additions & 2 deletions docker/auth.py
Expand Up @@ -224,14 +224,18 @@ def parse_auth(entries, raise_on_error=False):
return conf


def load_config(config_path=None, config_dict=None):
def load_config(cfg=None, config_path=None, config_dict=None):
"""
Loads authentication data from a Docker configuration file in the given
root directory or if config_path is passed use given path.
root directory or if cfg is passed, use it as a fileobj/path.
Lookup priority:
explicit config_path parameter > DOCKER_CONFIG environment variable >
~/.docker/config.json > ~/.dockercfg
"""
if isinstance(cfg, six.string_types):
config_path = cfg
elif hasattr(cfg, 'read'): # file-like object
config_dict = json.load(cfg)

if not config_dict:
config_file = config.find_config_file(config_path)
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/api_test.py
@@ -1,3 +1,4 @@
import base64
import datetime
import json
import io
Expand Down Expand Up @@ -230,6 +231,34 @@ def test_login(self):
}
}

def test_login_with_dockercfg(self):
registry = 'docker.io'
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
config = {
"auths": {
registry: {
'auth': '{0}'.format(auth_),
}
}
}
cfg_file = io.StringIO(six.text_type(json.dumps(config)))
self.client.login('sakuya', dockercfg=cfg_file)
args = fake_request.call_args
assert args[0][0] == 'POST'
assert args[0][1] == url_prefix + 'auth'
assert json.loads(args[1]['data']) == {
'username': 'sakuya', 'password': 'izayoi'
}
assert args[1]['headers'] == {'Content-Type': 'application/json'}
assert self.client._auth_configs['auths'] == {
'docker.io': {
'email': None,
'password': 'izayoi',
'username': 'sakuya',
'serveraddress': 'docker.io',
}
}

def test_events(self):
self.client.events()

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/auth_test.py
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-

import base64
import io
import json
import os
import os.path
import random
import shutil
import six
import tempfile
import unittest

Expand Down Expand Up @@ -462,3 +464,24 @@ def test_load_config_identity_token(self):
cfg = cfg['auths'][registry]
assert 'IdentityToken' in cfg
assert cfg['IdentityToken'] == token

def test_load_config_from_file_obj(self):
registry = 'https://your.private.registry.io'
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
config = {
registry: {
'auth': '{0}'.format(auth_),
'email': 'sakuya@scarlet.net'
}
}

f = io.StringIO(six.text_type(json.dumps(config)))

cfg = auth.load_config(f)
assert registry in cfg
assert cfg[registry] is not None
cfg = cfg[registry]
assert cfg['username'] == 'sakuya'
assert cfg['password'] == 'izayoi'
assert cfg['email'] == 'sakuya@scarlet.net'
assert cfg.get('auth') is None