Skip to content

Commit ba5d42e

Browse files
authored
Merge pull request #2604 from GK-07/fix-auth-settings-authorization-fallback
Restore bearer-token fallback in Configuration.auth_settings()
2 parents 95bc5f3 + 5621a4c commit ba5d42e

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

kubernetes/aio/client/configuration.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,19 @@ async def auth_settings(self):
415415
:return: The Auth Settings information dict.
416416
"""
417417
auth = {}
418-
if 'BearerToken' in self.api_key:
418+
# Backward compatibility: prior to v36.0.0 the bearer token was
419+
# stored under the 'authorization' api_key. Continue to honor it
420+
# so user code that sets ``config.api_key['authorization']``
421+
# directly keeps working with the new 'BearerToken' key the
422+
# generated client now looks for.
423+
# See: https://github.com/kubernetes-client/python/issues/2595
424+
if 'BearerToken' in self.api_key or 'authorization' in self.api_key:
419425
auth['BearerToken'] = {
420426
'type': 'api_key',
421427
'in': 'header',
422428
'key': 'authorization',
423429
'value': await self.get_api_key_with_prefix(
424-
'BearerToken',
430+
'BearerToken', alias='authorization',
425431
),
426432
}
427433
return auth

kubernetes/client/configuration.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,19 @@ def auth_settings(self):
406406
:return: The Auth Settings information dict.
407407
"""
408408
auth = {}
409-
if 'BearerToken' in self.api_key:
409+
# Backward compatibility: prior to v36.0.0 the bearer token was
410+
# stored under the 'authorization' api_key. Continue to honor it
411+
# so user code that sets ``config.api_key['authorization']``
412+
# directly keeps working with the new 'BearerToken' key the
413+
# generated client now looks for.
414+
# See: https://github.com/kubernetes-client/python/issues/2595
415+
if 'BearerToken' in self.api_key or 'authorization' in self.api_key:
410416
auth['BearerToken'] = {
411417
'type': 'api_key',
412418
'in': 'header',
413419
'key': 'authorization',
414420
'value': self.get_api_key_with_prefix(
415-
'BearerToken',
421+
'BearerToken', alias='authorization',
416422
),
417423
}
418424
return auth

kubernetes/test/test_api_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,45 @@ def test_rest_proxycare(self):
7878
# test
7979
client = kubernetes.client.ApiClient(configuration=config)
8080
self.assertEqual( expected_pool, type(client.rest_client.pool_manager) )
81+
82+
83+
class TestConfigurationAuthSettings(unittest.TestCase):
84+
"""Regression tests for Configuration.auth_settings() bearer-token lookup.
85+
86+
Prior to v36.0.0 the generated client stored the bearer token under
87+
``api_key['authorization']`` (e.g. set by ``load_kube_config`` or by
88+
user code directly). v36.0.0 switched the lookup to
89+
``api_key['BearerToken']`` without a fallback, which silently dropped
90+
the Authorization header from every outgoing request and caused 401
91+
Unauthorized against any cluster relying on bearer tokens.
92+
See: https://github.com/kubernetes-client/python/issues/2595
93+
"""
94+
95+
def _bearer_value(self, config):
96+
settings = config.auth_settings()
97+
self.assertIn('BearerToken', settings)
98+
return settings['BearerToken']['value']
99+
100+
def test_auth_settings_with_bearer_token_key(self):
101+
"""The new key 'BearerToken' continues to work."""
102+
config = Configuration()
103+
config.api_key['BearerToken'] = 'Bearer abc123'
104+
self.assertEqual(self._bearer_value(config), 'Bearer abc123')
105+
106+
def test_auth_settings_with_authorization_key(self):
107+
"""Legacy key 'authorization' is honored as a fallback."""
108+
config = Configuration()
109+
config.api_key['authorization'] = 'Bearer abc123'
110+
self.assertEqual(self._bearer_value(config), 'Bearer abc123')
111+
112+
def test_auth_settings_bearer_token_takes_precedence(self):
113+
"""When both keys are set, 'BearerToken' wins."""
114+
config = Configuration()
115+
config.api_key['BearerToken'] = 'Bearer new'
116+
config.api_key['authorization'] = 'Bearer old'
117+
self.assertEqual(self._bearer_value(config), 'Bearer new')
118+
119+
def test_auth_settings_with_no_token(self):
120+
"""No api_key entry yields an empty auth dict."""
121+
config = Configuration()
122+
self.assertEqual(config.auth_settings(), {})

0 commit comments

Comments
 (0)