Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docker/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ def load_config(config_path=None):
explicit config_path parameter > DOCKER_CONFIG environment variable >
~/.docker/config.json > ~/.dockercfg
"""

config_file = find_config_file(config_path)

if not config_file:
Expand All @@ -168,11 +167,17 @@ def load_config(config_path=None):
try:
with open(config_file) as f:
data = json.load(f)
res = {}
if data.get('auths'):
log.debug("Found 'auths' section")
return parse_auth(data['auths'])
res.update(parse_auth(data['auths']))
if data.get('HttpHeaders'):
log.debug("Found 'HttpHeaders' section")
res.update({'HttpHeaders': data['HttpHeaders']})
if res:
return res
else:
log.debug("Couldn't find 'auths' section")
log.debug("Couldn't find 'auths' or 'HttpHeaders' sections")
f.seek(0)
return parse_auth(json.load(f))
except (IOError, KeyError, ValueError) as e:
Expand Down
6 changes: 5 additions & 1 deletion docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .auth import auth
from .unixconn import unixconn
from .ssladapter import ssladapter
from .utils import utils, check_resource
from .utils import utils, check_resource, update_headers
from .tls import TLSConfig


Expand Down Expand Up @@ -103,15 +103,19 @@ def _set_request_timeout(self, kwargs):
kwargs.setdefault('timeout', self.timeout)
return kwargs

@update_headers
def _post(self, url, **kwargs):
return self.post(url, **self._set_request_timeout(kwargs))

@update_headers
def _get(self, url, **kwargs):
return self.get(url, **self._set_request_timeout(kwargs))

@update_headers
def _put(self, url, **kwargs):
return self.put(url, **self._set_request_timeout(kwargs))

@update_headers
def _delete(self, url, **kwargs):
return self.delete(url, **self._set_request_timeout(kwargs))

Expand Down
2 changes: 1 addition & 1 deletion docker/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
) # flake8: noqa

from .types import Ulimit, LogConfig # flake8: noqa
from .decorators import check_resource, minimum_version # flake8: noqa
from .decorators import check_resource, minimum_version, update_headers #flake8: noqa
11 changes: 11 additions & 0 deletions docker/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ def wrapper(self, *args, **kwargs):
return f(self, *args, **kwargs)
return wrapper
return decorator


def update_headers(f):
def inner(self, *args, **kwargs):
if 'HttpHeaders' in self._auth_configs:
if 'headers' not in kwargs:
kwargs['headers'] = self._auth_configs['HttpHeaders']
else:
kwargs['headers'].update(self._auth_configs['HttpHeaders'])
return f(self, *args, **kwargs)
return inner
24 changes: 24 additions & 0 deletions tests/unit/auth_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,27 @@ def test_load_config_custom_config_env_utf8(self):
self.assertEqual(cfg['password'], b'izayoi\xc3\xa6'.decode('utf8'))
self.assertEqual(cfg['email'], 'sakuya@scarlet.net')
self.assertEqual(cfg.get('auth'), None)

def test_load_config_custom_config_env_with_headers(self):
folder = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, folder)

dockercfg_path = os.path.join(folder, 'config.json')
config = {
'HttpHeaders': {
'Name': 'Spike',
'Surname': 'Spiegel'
},
}

with open(dockercfg_path, 'w') as f:
json.dump(config, f)

with mock.patch.dict(os.environ, {'DOCKER_CONFIG': folder}):
cfg = auth.load_config(None)
assert 'HttpHeaders' in cfg
self.assertNotEqual(cfg['HttpHeaders'], None)
cfg = cfg['HttpHeaders']

self.assertEqual(cfg['Name'], 'Spike')
self.assertEqual(cfg['Surname'], 'Spiegel')