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

Commit 3efcdec

Browse files
twz123hjacobs
authored andcommitted
Use ID token if auth type is OIDC (#41)
Simply use the ID token inside the Authentication header for the OIDC case. This helps at least a bit when running locally as long as the ID token is still valid.
1 parent 46f2738 commit 3efcdec

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

pykube/http.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ def send(self, request, **kwargs):
120120
auth_config.get("expiry"),
121121
config,
122122
)
123-
# @@@ support oidc
123+
elif auth_provider.get("name") == "oidc":
124+
auth_config = auth_provider.get("config", {})
125+
# @@@ support token refresh
126+
if "id-token" in auth_config:
127+
request.headers["Authorization"] = "Bearer {}".format(auth_config["id-token"])
124128
elif "client-certificate" in config.user:
125129
kwargs["cert"] = (
126130
config.user["client-certificate"].filename(),

tests/test_config_with_oidc_auth.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TODO: Replace with a more realistic example
2+
current-context: thecluster
3+
clusters:
4+
- name: thecluster
5+
cluster: {}
6+
users:
7+
- name: admin
8+
user:
9+
auth-provider:
10+
config:
11+
client-id: google
12+
client-secret: s3cr3t
13+
id-token: some-id-token
14+
idp-issuer-url: https://accounts.google.com
15+
refresh-token: some-refresh-token
16+
name: oidc
17+
contexts:
18+
- name: thecluster
19+
context:
20+
cluster: thecluster
21+
user: admin
22+
- name: second
23+
context: secondcontext

tests/test_http.py

+16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
GOOD_CONFIG_FILE_PATH = os.path.sep.join(["tests", "test_config_with_context.yaml"])
1515
CONFIG_WITH_INSECURE_SKIP_TLS_VERIFY = os.path.sep.join(["tests", "test_config_with_insecure_skip_tls_verify.yaml"])
16+
CONFIG_WITH_OIDC_AUTH = os.path.sep.join(["tests", "test_config_with_oidc_auth.yaml"])
1617

1718

1819
def test_http(monkeypatch):
@@ -62,3 +63,18 @@ def test_http_do_not_overwrite_auth(monkeypatch):
6263

6364
mock_send.assert_called_once()
6465
assert mock_send.call_args[0][0].headers['Authorization'] == 'Bearer testtoken'
66+
67+
68+
def test_http_with_oidc_auth(monkeypatch):
69+
cfg = KubeConfig.from_file(CONFIG_WITH_OIDC_AUTH)
70+
api = HTTPClient(cfg)
71+
72+
mock_send = MagicMock()
73+
mock_send.side_effect = Exception('MOCK HTTP')
74+
monkeypatch.setattr('pykube.http.KubernetesHTTPAdapter._do_send', mock_send)
75+
76+
with pytest.raises(Exception):
77+
api.get(url='test')
78+
79+
mock_send.assert_called_once()
80+
assert mock_send.call_args[0][0].headers['Authorization'] == 'Bearer some-id-token'

0 commit comments

Comments
 (0)