Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
#38 allow passing Authorization header on HTTPClient.get(, headers=..) (
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Aug 16, 2019
1 parent 33332c9 commit aae1122
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pykube/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def send(self, request, **kwargs):

# setup cluster API authentication

if "token" in config.user and config.user["token"]:
if "Authorization" in request.headers:
# request already has some auth header (e.g. Bearer token)
# don't modify/overwrite it
pass
elif "token" in config.user and config.user["token"]:
request.headers["Authorization"] = "Bearer {}".format(config.user["token"])
elif "auth-provider" in config.user:
auth_provider = config.user["auth-provider"]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,18 @@ def test_http_insecure_skip_tls_verify(monkeypatch):
mock_send.assert_called_once()
# check that SSL is not verified
assert not mock_send.call_args[1]['verify']


def test_http_do_not_overwrite_auth(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg)

mock_send = MagicMock()
mock_send.side_effect = Exception('MOCK HTTP')
monkeypatch.setattr('pykube.http.KubernetesHTTPAdapter._do_send', mock_send)

with pytest.raises(Exception):
api.get(url='test', headers={'Authorization': 'Bearer testtoken'})

mock_send.assert_called_once()
assert mock_send.call_args[0][0].headers['Authorization'] == 'Bearer testtoken'

0 comments on commit aae1122

Please sign in to comment.