Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions kubernetes_asyncio/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,12 @@ async def auth_settings(self):
:return: The Auth Settings information dict.
"""
auth = {}
if 'BearerToken' in self.api_key:
if 'authorization' in self.api_key:
auth['BearerToken'] = {
'type': 'api_key',
'in': 'header',
'key': 'authorization',
'value': await self.get_api_key_with_prefix(
'BearerToken',
),
'value': await self.get_api_key_with_prefix('authorization'),
}
return auth

Expand Down
6 changes: 3 additions & 3 deletions kubernetes_asyncio/config/incluster_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _set_config(self, configuration):
configuration.host = self.host
configuration.ssl_ca_cert = self.ssl_ca_cert
if self.token is not None:
configuration.api_key['BearerToken'] = self.token
configuration.api_key['authorization'] = self.token
if not self._try_refresh_token:
return

Expand All @@ -99,8 +99,8 @@ def load_token_from_file(configuration, *args):

# expiration time is stored InClusterConfigLoader,
# thus some copies of Configuration can be outdated.
if configuration.api_key['BearerToken'] != self.token:
configuration.api_key['BearerToken'] = self.token
if configuration.api_key['authorization'] != self.token:
configuration.api_key['authorization'] = self.token

configuration.refresh_api_key_hook = load_token_from_file

Expand Down
14 changes: 7 additions & 7 deletions kubernetes_asyncio/config/incluster_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ async def test_refresh_token(self):
loader.load_and_set(config)

self.assertEqual('Bearer ' + _TEST_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
self.assertIsNotNone(loader.token_expires_at)

old_token_expires_at = loader.token_expires_at
loader._token_filename = self._create_file_with_temp_content(
_TEST_NEW_TOKEN)
self.assertEqual('Bearer ' + _TEST_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))

loader.token_expires_at = datetime.datetime.now()
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
self.assertGreater(loader.token_expires_at, old_token_expires_at)

Expand All @@ -132,7 +132,7 @@ async def test_refresh_token_default_config_with_copies(self):

for config in configs:
self.assertEqual('Bearer ' + _TEST_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
self.assertIsNotNone(loader.token_expires_at)

Expand All @@ -142,13 +142,13 @@ async def test_refresh_token_default_config_with_copies(self):

for config in configs:
self.assertEqual('Bearer ' + _TEST_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))

loader.token_expires_at = datetime.datetime.now()

for config in configs:
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
await config.get_api_key_with_prefix('BearerToken'))
await config.get_api_key_with_prefix('authorization'))

self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
self.assertGreater(loader.token_expires_at, old_token_expires_at)
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_client_config(self):
loader._set_config(client_config)
self.assertEqual("https://" + _TEST_HOST_PORT, client_config.host)
self.assertEqual(cert_filename, client_config.ssl_ca_cert)
self.assertEqual("Bearer " + _TEST_TOKEN, client_config.api_key['BearerToken'])
self.assertEqual("Bearer " + _TEST_TOKEN, client_config.api_key['authorization'])

async def test_load_config_helper(self):
token_filename = self._create_file_with_temp_content(_TEST_TOKEN)
Expand Down
7 changes: 3 additions & 4 deletions kubernetes_asyncio/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def _load_cluster_info(self):
def _set_config(self, client_configuration):

if 'token' in self.__dict__:
client_configuration.api_key['BearerToken'] = self.token
client_configuration.api_key['authorization'] = self.token

# copy these keys directly from self to configuration object
keys = ['host', 'ssl_ca_cert', 'cert_file', 'key_file', 'verify_ssl', 'tls_server_name']
Expand Down Expand Up @@ -657,17 +657,16 @@ async def refresh_token(loader, client_configuration=None, interval=60):

if client_configuration is None:
raise NotImplementedError

if loader.provider == 'gcp':
while 1:
await asyncio.sleep(interval)
await loader.load_gcp_token()
client_configuration.api_key['BearerToken'] = loader.token
client_configuration.api_key['authorization'] = loader.token
elif 'exec' in loader._user:
while 1:
await asyncio.sleep(interval)
await loader.load_from_exec_plugin()
client_configuration.api_key['BearerToken'] = loader.token
client_configuration.api_key['authorization'] = loader.token


async def new_client_from_config(config_file=None, context=None, persist_config=True,
Expand Down
14 changes: 7 additions & 7 deletions kubernetes_asyncio/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class FakeConfig:
def __init__(self, token=None, **kwargs):
self.api_key = {}
if token:
self.api_key['BearerToken'] = token
self.api_key['authorization'] = token

self.__dict__.update(kwargs)

Expand Down Expand Up @@ -788,7 +788,7 @@ async def test_user_exec_auth(self, mock):
"token": token
}
expected = FakeConfig(host=TEST_HOST, api_key={
"BearerToken": BEARER_TOKEN_FORMAT % token})
"authorization": BEARER_TOKEN_FORMAT % token})
actual = FakeConfig()
await KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
Expand Down Expand Up @@ -1008,14 +1008,14 @@ async def test_new_client_from_config(self):
config_file=config_file, context="simple_token")
self.assertEqual(TEST_HOST, client.configuration.host)
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['BearerToken'])
client.configuration.api_key['authorization'])

async def test_new_client_from_config_dict(self):
client = await new_client_from_config_dict(
config_dict=self.TEST_KUBE_CONFIG, context="simple_token")
self.assertEqual(TEST_HOST, client.configuration.host)
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['BearerToken'])
client.configuration.api_key['authorization'])

async def test_no_users_section(self):
expected = FakeConfig(host=TEST_HOST)
Expand Down Expand Up @@ -1072,7 +1072,7 @@ async def load_from_exec_plugin(self):
with self.assertRaises(AssertionError):
await refresh_token(loader, mock_config)

self.assertEqual(TEST_ANOTHER_DATA_BASE64, mock_config.api_key["BearerToken"])
self.assertEqual(TEST_ANOTHER_DATA_BASE64, mock_config.api_key["authorization"])

async def test_load_config_helper(self):
expected = FakeConfig(host=TEST_HOST,
Expand Down Expand Up @@ -1253,7 +1253,7 @@ async def test_new_client_from_config(self):
config_file=kubeconfigs, context="simple_token")
self.assertEqual(TEST_HOST, client.configuration.host)
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['BearerToken'])
client.configuration.api_key['authorization'])

async def test_merge_with_context_in_different_file(self):
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET2)
Expand All @@ -1269,7 +1269,7 @@ async def test_merge_with_context_in_different_file(self):
self.assertEqual(active_context, expected_contexts[0])
self.assertEqual(TEST_HOST, client.configuration.host)
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['BearerToken'])
client.configuration.api_key['authorization'])

def test_save_changes(self):
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)
Expand Down