Skip to content

Commit 7cc2c57

Browse files
committed
config: drop dual-write, replace 'authorization' with 'BearerToken'
Address review feedback: the openapi-generator v6.6.0 upgrade declared this rename as a breaking change in the project CHANGELOG, so the loaders should follow the rename rather than preserve the v35 key for backward compatibility. Removes the api_key['authorization'] write in all three affected loaders (sync incluster, sync kube_config, async incluster) and updates the corresponding test assertions to read 'BearerToken'.
1 parent c3608a4 commit 7cc2c57

6 files changed

Lines changed: 13 additions & 21 deletions

File tree

kubernetes/base/config/incluster_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def _set_config(self, client_configuration):
8888
client_configuration.host = self.host
8989
client_configuration.ssl_ca_cert = self.ssl_ca_cert
9090
if self.token is not None:
91-
client_configuration.api_key['authorization'] = self.token
9291
client_configuration.api_key['BearerToken'] = self.token
9392
if not self._try_refresh_token:
9493
return

kubernetes/base/config/incluster_config_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_refresh_token(self):
9191
loader.load_and_set(config)
9292

9393
self.assertEqual('bearer ' + _TEST_TOKEN,
94-
config.get_api_key_with_prefix('authorization'))
94+
config.get_api_key_with_prefix('BearerToken'))
9595
self.assertEqual('bearer ' + _TEST_TOKEN, loader.token)
9696
self.assertIsNotNone(loader.token_expires_at)
9797

@@ -100,11 +100,11 @@ def test_refresh_token(self):
100100
loader._token_filename = self._create_file_with_temp_content(
101101
_TEST_NEW_TOKEN)
102102
self.assertEqual('bearer ' + _TEST_TOKEN,
103-
config.get_api_key_with_prefix('authorization'))
103+
config.get_api_key_with_prefix('BearerToken'))
104104

105105
loader.token_expires_at = datetime.datetime.now()
106106
self.assertEqual('bearer ' + _TEST_NEW_TOKEN,
107-
config.get_api_key_with_prefix('authorization'))
107+
config.get_api_key_with_prefix('BearerToken'))
108108
self.assertEqual('bearer ' + _TEST_NEW_TOKEN, loader.token)
109109
self.assertGreater(loader.token_expires_at, old_token_expires_at)
110110

kubernetes/base/config/kube_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ def _load_cluster_info(self):
527527

528528
def _set_config(self, client_configuration):
529529
if 'token' in self.__dict__:
530-
client_configuration.api_key['authorization'] = self.token
531530
client_configuration.api_key['BearerToken'] = self.token
532531

533532
def _refresh_api_key(client_configuration):

kubernetes/base/config/kube_config_test.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ def __init__(self, token=None, **kwargs):
369369
# Provided by the OpenAPI-generated Configuration class
370370
self.refresh_api_key_hook = None
371371
if token:
372-
self.api_key['authorization'] = token
373372
self.api_key['BearerToken'] = token
374373

375374
self.__dict__.update(kwargs)
@@ -906,7 +905,7 @@ def test_gcp_no_refresh(self):
906905
self.assertIsNotNone(fake_config.refresh_api_key_hook)
907906
self.assertEqual(TEST_HOST, fake_config.host)
908907
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
909-
fake_config.api_key['authorization'])
908+
fake_config.api_key['BearerToken'])
910909

911910
def test_load_gcp_token_no_refresh(self):
912911
loader = KubeConfigLoader(
@@ -1284,14 +1283,14 @@ def test_new_client_from_config(self):
12841283
config_file=config_file, context="simple_token")
12851284
self.assertEqual(TEST_HOST, client.configuration.host)
12861285
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1287-
client.configuration.api_key['authorization'])
1286+
client.configuration.api_key['BearerToken'])
12881287

12891288
def test_new_client_from_config_dict(self):
12901289
client = new_client_from_config_dict(
12911290
config_dict=self.TEST_KUBE_CONFIG, context="simple_token")
12921291
self.assertEqual(TEST_HOST, client.configuration.host)
12931292
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1294-
client.configuration.api_key['authorization'])
1293+
client.configuration.api_key['BearerToken'])
12951294

12961295
def test_no_users_section(self):
12971296
expected = FakeConfig(host=TEST_HOST)
@@ -1318,7 +1317,6 @@ def test_user_exec_auth(self, mock):
13181317
"token": token
13191318
}
13201319
expected = FakeConfig(host=TEST_HOST, api_key={
1321-
"authorization": BEARER_TOKEN_FORMAT % token,
13221320
"BearerToken": BEARER_TOKEN_FORMAT % token})
13231321
actual = FakeConfig()
13241322
KubeConfigLoader(
@@ -1349,13 +1347,13 @@ def test_user_exec_auth_with_expiry(self, mock):
13491347
active_context="exec_cred_user").load_and_set(fake_config)
13501348
# The kube config should use the first token returned from the
13511349
# exec provider.
1352-
self.assertEqual(fake_config.api_key["authorization"],
1350+
self.assertEqual(fake_config.api_key["BearerToken"],
13531351
BEARER_TOKEN_FORMAT % expired_token)
13541352
# Should now be populated with a method to refresh expired tokens.
13551353
self.assertIsNotNone(fake_config.refresh_api_key_hook)
13561354
# Refresh the token; the kube config should be updated.
13571355
fake_config.refresh_api_key_hook(fake_config)
1358-
self.assertEqual(fake_config.api_key["authorization"],
1356+
self.assertEqual(fake_config.api_key["BearerToken"],
13591357
BEARER_TOKEN_FORMAT % current_token)
13601358

13611359
@mock.patch('kubernetes.config.kube_config.ExecProvider.run')
@@ -1397,7 +1395,6 @@ def test_user_cmd_path(self):
13971395
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
13981396
CommandTokenSource.token = mock.Mock(return_value=return_value)
13991397
expected = FakeConfig(api_key={
1400-
"authorization": BEARER_TOKEN_FORMAT % token,
14011398
"BearerToken": BEARER_TOKEN_FORMAT % token})
14021399
actual = FakeConfig()
14031400
KubeConfigLoader(
@@ -1411,7 +1408,6 @@ def test_user_cmd_path_empty(self):
14111408
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
14121409
CommandTokenSource.token = mock.Mock(return_value=return_value)
14131410
expected = FakeConfig(api_key={
1414-
"authorization": BEARER_TOKEN_FORMAT % token,
14151411
"BearerToken": BEARER_TOKEN_FORMAT % token})
14161412
actual = FakeConfig()
14171413
self.expect_exception(lambda: KubeConfigLoader(
@@ -1426,7 +1422,6 @@ def test_user_cmd_path_with_scope(self):
14261422
return_value = A(token, parse_rfc3339(datetime.datetime.now()))
14271423
CommandTokenSource.token = mock.Mock(return_value=return_value)
14281424
expected = FakeConfig(api_key={
1429-
"authorization": BEARER_TOKEN_FORMAT % token,
14301425
"BearerToken": BEARER_TOKEN_FORMAT % token})
14311426
actual = FakeConfig()
14321427
self.expect_exception(lambda: KubeConfigLoader(
@@ -1728,7 +1723,7 @@ def test_new_client_from_config(self):
17281723
config_file=kubeconfigs, context="simple_token")
17291724
self.assertEqual(TEST_HOST, client.configuration.host)
17301725
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1731-
client.configuration.api_key['authorization'])
1726+
client.configuration.api_key['BearerToken'])
17321727

17331728
def test_merge_with_context_in_different_file(self):
17341729
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET2)
@@ -1744,7 +1739,7 @@ def test_merge_with_context_in_different_file(self):
17441739
self.assertEqual(active_context, expected_contexts[0])
17451740
self.assertEqual(TEST_HOST, client.configuration.host)
17461741
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1747-
client.configuration.api_key['authorization'])
1742+
client.configuration.api_key['BearerToken'])
17481743

17491744
def test_save_changes(self):
17501745
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)

kubernetes_asyncio/config/incluster_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def _set_config(self, client_configuration):
8888
client_configuration.host = self.host
8989
client_configuration.ssl_ca_cert = self.ssl_ca_cert
9090
if self.token is not None:
91-
client_configuration.api_key['authorization'] = self.token
9291
client_configuration.api_key['BearerToken'] = self.token
9392
if not self._try_refresh_token:
9493
return

kubernetes_asyncio/config/incluster_config_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async def test_refresh_token(self):
9191
loader.load_and_set(config)
9292

9393
self.assertEqual('bearer ' + _TEST_TOKEN,
94-
await config.get_api_key_with_prefix('authorization'))
94+
await config.get_api_key_with_prefix('BearerToken'))
9595
self.assertEqual('bearer ' + _TEST_TOKEN, loader.token)
9696
self.assertIsNotNone(loader.token_expires_at)
9797

@@ -100,11 +100,11 @@ async def test_refresh_token(self):
100100
loader._token_filename = self._create_file_with_temp_content(
101101
_TEST_NEW_TOKEN)
102102
self.assertEqual('bearer ' + _TEST_TOKEN,
103-
await config.get_api_key_with_prefix('authorization'))
103+
await config.get_api_key_with_prefix('BearerToken'))
104104

105105
loader.token_expires_at = datetime.datetime.now()
106106
self.assertEqual('bearer ' + _TEST_NEW_TOKEN,
107-
await config.get_api_key_with_prefix('authorization'))
107+
await config.get_api_key_with_prefix('BearerToken'))
108108
self.assertEqual('bearer ' + _TEST_NEW_TOKEN, loader.token)
109109
self.assertGreater(loader.token_expires_at, old_token_expires_at)
110110

0 commit comments

Comments
 (0)