Skip to content

Commit 322667b

Browse files
committed
config: write api_key['BearerToken'] so v36+ SDK auth works
v36 rewrote Configuration.auth_settings() to look up the bearer-token credential under api_key['BearerToken'], matching the OpenAPI security scheme name. The in-cluster and kubeconfig loaders were not updated - they still write api_key['authorization'], the v35 lookup key. As a result, on v36 every call to load_incluster_config() (and load_kube_config() with a static token) produces a Configuration whose auth_settings() yields no bearer credential, so outgoing API requests are sent without an Authorization header and the apiserver treats them as system:anonymous. Write the token under both 'authorization' (v35) and 'BearerToken' (v36+) so requests carry the expected header on either SDK release line. Add a regression test in incluster_config_test that drives a real ApiClient.update_params_for_auth() against a freshly-loaded Configuration and asserts the resulting headers contain an Authorization entry - the end-to-end invariant that v36 quietly broke.
1 parent 26761f3 commit 322667b

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

kubernetes/base/config/incluster_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def _set_config(self, client_configuration):
8989
client_configuration.ssl_ca_cert = self.ssl_ca_cert
9090
if self.token is not None:
9191
client_configuration.api_key['authorization'] = self.token
92+
client_configuration.api_key['BearerToken'] = self.token
9293
if not self._try_refresh_token:
9394
return
9495

kubernetes/base/config/incluster_config_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ def test_refresh_token(self):
108108
self.assertEqual('bearer ' + _TEST_NEW_TOKEN, loader.token)
109109
self.assertGreater(loader.token_expires_at, old_token_expires_at)
110110

111+
def test_load_incluster_sets_request_authorization_header(self):
112+
from kubernetes.client import ApiClient
113+
cert_filename = self._create_file_with_temp_content(_TEST_CERT)
114+
loader = self.get_test_loader(cert_filename=cert_filename)
115+
config = Configuration()
116+
loader.load_and_set(config)
117+
118+
api_client = ApiClient(config)
119+
headers = {}
120+
api_client.update_params_for_auth(headers, [], ['BearerToken'])
121+
122+
self.assertIn('authorization', headers)
123+
self.assertTrue(
124+
headers['authorization'].lower().startswith('bearer '),
125+
"Expected a Bearer authorization header, got: %r"
126+
% headers['authorization'])
127+
111128
def _should_fail_load(self, config_loader, reason):
112129
try:
113130
config_loader.load_and_set()

kubernetes/base/config/kube_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ def _load_cluster_info(self):
528528
def _set_config(self, client_configuration):
529529
if 'token' in self.__dict__:
530530
client_configuration.api_key['authorization'] = self.token
531+
client_configuration.api_key['BearerToken'] = self.token
531532

532533
def _refresh_api_key(client_configuration):
533534
if ('expiry' in self.__dict__ and _is_expired(self.expiry)):

kubernetes/base/config/kube_config_test.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ def __init__(self, token=None, **kwargs):
370370
self.refresh_api_key_hook = None
371371
if token:
372372
self.api_key['authorization'] = token
373+
self.api_key['BearerToken'] = token
373374

374375
self.__dict__.update(kwargs)
375376

@@ -1317,7 +1318,8 @@ def test_user_exec_auth(self, mock):
13171318
"token": token
13181319
}
13191320
expected = FakeConfig(host=TEST_HOST, api_key={
1320-
"authorization": BEARER_TOKEN_FORMAT % token})
1321+
"authorization": BEARER_TOKEN_FORMAT % token,
1322+
"BearerToken": BEARER_TOKEN_FORMAT % token})
13211323
actual = FakeConfig()
13221324
KubeConfigLoader(
13231325
config_dict=self.TEST_KUBE_CONFIG,
@@ -1395,7 +1397,8 @@ def test_user_cmd_path(self):
13951397
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
13961398
CommandTokenSource.token = mock.Mock(return_value=return_value)
13971399
expected = FakeConfig(api_key={
1398-
"authorization": BEARER_TOKEN_FORMAT % token})
1400+
"authorization": BEARER_TOKEN_FORMAT % token,
1401+
"BearerToken": BEARER_TOKEN_FORMAT % token})
13991402
actual = FakeConfig()
14001403
KubeConfigLoader(
14011404
config_dict=self.TEST_KUBE_CONFIG,
@@ -1408,7 +1411,8 @@ def test_user_cmd_path_empty(self):
14081411
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
14091412
CommandTokenSource.token = mock.Mock(return_value=return_value)
14101413
expected = FakeConfig(api_key={
1411-
"authorization": BEARER_TOKEN_FORMAT % token})
1414+
"authorization": BEARER_TOKEN_FORMAT % token,
1415+
"BearerToken": BEARER_TOKEN_FORMAT % token})
14121416
actual = FakeConfig()
14131417
self.expect_exception(lambda: KubeConfigLoader(
14141418
config_dict=self.TEST_KUBE_CONFIG,
@@ -1422,7 +1426,8 @@ def test_user_cmd_path_with_scope(self):
14221426
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
14231427
CommandTokenSource.token = mock.Mock(return_value=return_value)
14241428
expected = FakeConfig(api_key={
1425-
"authorization": BEARER_TOKEN_FORMAT % token})
1429+
"authorization": BEARER_TOKEN_FORMAT % token,
1430+
"BearerToken": BEARER_TOKEN_FORMAT % token})
14261431
actual = FakeConfig()
14271432
self.expect_exception(lambda: KubeConfigLoader(
14281433
config_dict=self.TEST_KUBE_CONFIG,

0 commit comments

Comments
 (0)