@@ -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