Skip to content

Commit 8c533bf

Browse files
committed
Merge pull request #899 from docker/http-headers-support
Add HttpHeaders support
2 parents 57b79cb + 84a72f4 commit 8c533bf

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

docker/auth/auth.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ def load_config(config_path=None):
158158
explicit config_path parameter > DOCKER_CONFIG environment variable >
159159
~/.docker/config.json > ~/.dockercfg
160160
"""
161-
162161
config_file = find_config_file(config_path)
163162

164163
if not config_file:
@@ -168,11 +167,17 @@ def load_config(config_path=None):
168167
try:
169168
with open(config_file) as f:
170169
data = json.load(f)
170+
res = {}
171171
if data.get('auths'):
172172
log.debug("Found 'auths' section")
173-
return parse_auth(data['auths'])
173+
res.update(parse_auth(data['auths']))
174+
if data.get('HttpHeaders'):
175+
log.debug("Found 'HttpHeaders' section")
176+
res.update({'HttpHeaders': data['HttpHeaders']})
177+
if res:
178+
return res
174179
else:
175-
log.debug("Couldn't find 'auths' section")
180+
log.debug("Couldn't find 'auths' or 'HttpHeaders' sections")
176181
f.seek(0)
177182
return parse_auth(json.load(f))
178183
except (IOError, KeyError, ValueError) as e:

docker/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from .auth import auth
2929
from .unixconn import unixconn
3030
from .ssladapter import ssladapter
31-
from .utils import utils, check_resource
31+
from .utils import utils, check_resource, update_headers
3232
from .tls import TLSConfig
3333

3434

@@ -103,15 +103,19 @@ def _set_request_timeout(self, kwargs):
103103
kwargs.setdefault('timeout', self.timeout)
104104
return kwargs
105105

106+
@update_headers
106107
def _post(self, url, **kwargs):
107108
return self.post(url, **self._set_request_timeout(kwargs))
108109

110+
@update_headers
109111
def _get(self, url, **kwargs):
110112
return self.get(url, **self._set_request_timeout(kwargs))
111113

114+
@update_headers
112115
def _put(self, url, **kwargs):
113116
return self.put(url, **self._set_request_timeout(kwargs))
114117

118+
@update_headers
115119
def _delete(self, url, **kwargs):
116120
return self.delete(url, **self._set_request_timeout(kwargs))
117121

docker/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
) # flake8: noqa
99

1010
from .types import Ulimit, LogConfig # flake8: noqa
11-
from .decorators import check_resource, minimum_version # flake8: noqa
11+
from .decorators import check_resource, minimum_version, update_headers #flake8: noqa

docker/utils/decorators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ def wrapper(self, *args, **kwargs):
3535
return f(self, *args, **kwargs)
3636
return wrapper
3737
return decorator
38+
39+
40+
def update_headers(f):
41+
def inner(self, *args, **kwargs):
42+
if 'HttpHeaders' in self._auth_configs:
43+
if 'headers' not in kwargs:
44+
kwargs['headers'] = self._auth_configs['HttpHeaders']
45+
else:
46+
kwargs['headers'].update(self._auth_configs['HttpHeaders'])
47+
return f(self, *args, **kwargs)
48+
return inner

tests/unit/auth_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,27 @@ def test_load_config_custom_config_env_utf8(self):
409409
self.assertEqual(cfg['password'], b'izayoi\xc3\xa6'.decode('utf8'))
410410
self.assertEqual(cfg['email'], '[email protected]')
411411
self.assertEqual(cfg.get('auth'), None)
412+
413+
def test_load_config_custom_config_env_with_headers(self):
414+
folder = tempfile.mkdtemp()
415+
self.addCleanup(shutil.rmtree, folder)
416+
417+
dockercfg_path = os.path.join(folder, 'config.json')
418+
config = {
419+
'HttpHeaders': {
420+
'Name': 'Spike',
421+
'Surname': 'Spiegel'
422+
},
423+
}
424+
425+
with open(dockercfg_path, 'w') as f:
426+
json.dump(config, f)
427+
428+
with mock.patch.dict(os.environ, {'DOCKER_CONFIG': folder}):
429+
cfg = auth.load_config(None)
430+
assert 'HttpHeaders' in cfg
431+
self.assertNotEqual(cfg['HttpHeaders'], None)
432+
cfg = cfg['HttpHeaders']
433+
434+
self.assertEqual(cfg['Name'], 'Spike')
435+
self.assertEqual(cfg['Surname'], 'Spiegel')

0 commit comments

Comments
 (0)