Skip to content

Commit abd4624

Browse files
iciclespiderPatrick J. McNerthney
and
Patrick J. McNerthney
authored
Allow for async refresh_api_key_hook methods. (#359)
Co-authored-by: Patrick J. McNerthney <patrick.mcnerthney@fortra.com>
1 parent b377772 commit abd4624

File tree

6 files changed

+102
-15
lines changed

6 files changed

+102
-15
lines changed

kubernetes_asyncio/client/api_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ async def __call_api(
165165
post_params.extend(self.files_parameters(files))
166166

167167
# auth setting
168-
self.update_params_for_auth(
168+
await self.update_params_for_auth(
169169
header_params, query_params, auth_settings,
170170
request_auth=_request_auth)
171171

@@ -548,7 +548,7 @@ def select_header_content_type(self, content_types, method=None, body=None):
548548
else:
549549
return content_types[0]
550550

551-
def update_params_for_auth(self, headers, queries, auth_settings,
551+
async def update_params_for_auth(self, headers, queries, auth_settings,
552552
request_auth=None):
553553
"""Updates header and query params based on authentication setting.
554554
@@ -566,7 +566,7 @@ def update_params_for_auth(self, headers, queries, auth_settings,
566566
return
567567

568568
for auth in auth_settings:
569-
auth_setting = self.configuration.auth_settings().get(auth)
569+
auth_setting = (await self.configuration.auth_settings()).get(auth)
570570
if auth_setting:
571571
self._apply_auth_params(headers, queries, auth_setting)
572572

kubernetes_asyncio/client/configuration.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from __future__ import absolute_import
1414

15+
import asyncio
1516
import copy
1617
import logging
1718
import sys
@@ -370,15 +371,17 @@ def logger_format(self, value):
370371
self.__logger_format = value
371372
self.logger_formatter = logging.Formatter(self.__logger_format)
372373

373-
def get_api_key_with_prefix(self, identifier, alias=None):
374+
async def get_api_key_with_prefix(self, identifier, alias=None):
374375
"""Gets API key (with prefix if set).
375376
376377
:param identifier: The identifier of apiKey.
377378
:param alias: The alternative identifier of apiKey.
378379
:return: The token for api key authentication.
379380
"""
380381
if self.refresh_api_key_hook is not None:
381-
self.refresh_api_key_hook(self)
382+
result = self.refresh_api_key_hook(self)
383+
if asyncio.iscoroutine(result):
384+
await result
382385
key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
383386
if key:
384387
prefix = self.api_key_prefix.get(identifier)
@@ -402,7 +405,7 @@ def get_basic_auth_token(self):
402405
basic_auth=username + ':' + password
403406
).get('authorization')
404407

405-
def auth_settings(self):
408+
async def auth_settings(self):
406409
"""Gets Auth Settings dict for api client.
407410
408411
:return: The Auth Settings information dict.
@@ -413,7 +416,7 @@ def auth_settings(self):
413416
'type': 'api_key',
414417
'in': 'header',
415418
'key': 'authorization',
416-
'value': self.get_api_key_with_prefix(
419+
'value': await self.get_api_key_with_prefix(
417420
'BearerToken',
418421
),
419422
}

kubernetes_asyncio/config/incluster_config_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,29 @@ def test_load_config(self):
9999
self.assertEqual(cert_filename, loader.ssl_ca_cert)
100100
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
101101

102-
def test_refresh_token(self):
102+
async def test_refresh_token(self):
103103
loader = self.get_test_loader()
104104
config = Configuration()
105105
loader.load_and_set(config)
106106

107107
self.assertEqual('Bearer ' + _TEST_TOKEN,
108-
config.get_api_key_with_prefix('BearerToken'))
108+
await config.get_api_key_with_prefix('BearerToken'))
109109
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
110110
self.assertIsNotNone(loader.token_expires_at)
111111

112112
old_token_expires_at = loader.token_expires_at
113113
loader._token_filename = self._create_file_with_temp_content(
114114
_TEST_NEW_TOKEN)
115115
self.assertEqual('Bearer ' + _TEST_TOKEN,
116-
config.get_api_key_with_prefix('BearerToken'))
116+
await config.get_api_key_with_prefix('BearerToken'))
117117

118118
loader.token_expires_at = datetime.datetime.now()
119119
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
120-
config.get_api_key_with_prefix('BearerToken'))
120+
await config.get_api_key_with_prefix('BearerToken'))
121121
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
122122
self.assertGreater(loader.token_expires_at, old_token_expires_at)
123123

124-
def test_refresh_token_default_config_with_copies(self):
124+
async def test_refresh_token_default_config_with_copies(self):
125125
loader = self.get_test_loader()
126126
loader.load_and_set()
127127

@@ -132,7 +132,7 @@ def test_refresh_token_default_config_with_copies(self):
132132

133133
for config in configs:
134134
self.assertEqual('Bearer ' + _TEST_TOKEN,
135-
config.get_api_key_with_prefix('BearerToken'))
135+
await config.get_api_key_with_prefix('BearerToken'))
136136
self.assertEqual('Bearer ' + _TEST_TOKEN, loader.token)
137137
self.assertIsNotNone(loader.token_expires_at)
138138

@@ -142,13 +142,13 @@ def test_refresh_token_default_config_with_copies(self):
142142

143143
for config in configs:
144144
self.assertEqual('Bearer ' + _TEST_TOKEN,
145-
config.get_api_key_with_prefix('BearerToken'))
145+
await config.get_api_key_with_prefix('BearerToken'))
146146

147147
loader.token_expires_at = datetime.datetime.now()
148148

149149
for config in configs:
150150
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN,
151-
config.get_api_key_with_prefix('BearerToken'))
151+
await config.get_api_key_with_prefix('BearerToken'))
152152

153153
self.assertEqual('Bearer ' + _TEST_NEW_TOKEN, loader.token)
154154
self.assertGreater(loader.token_expires_at, old_token_expires_at)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
diff --git a/kubernetes_asyncio/client/api_client.py b/kubernetes_asyncio/client/api_client.py
2+
index 81c12e00..02c9d0f2 100644
3+
--- a/kubernetes_asyncio/client/api_client.py
4+
+++ b/kubernetes_asyncio/client/api_client.py
5+
@@ -165,7 +165,7 @@ class ApiClient(object):
6+
post_params.extend(self.files_parameters(files))
7+
8+
# auth setting
9+
- self.update_params_for_auth(
10+
+ await self.update_params_for_auth(
11+
header_params, query_params, auth_settings,
12+
request_auth=_request_auth)
13+
14+
@@ -548,7 +548,7 @@ class ApiClient(object):
15+
else:
16+
return content_types[0]
17+
18+
- def update_params_for_auth(self, headers, queries, auth_settings,
19+
+ async def update_params_for_auth(self, headers, queries, auth_settings,
20+
request_auth=None):
21+
"""Updates header and query params based on authentication setting.
22+
23+
@@ -566,6 +566,6 @@ class ApiClient(object):
24+
return
25+
26+
for auth in auth_settings:
27+
- auth_setting = self.configuration.auth_settings().get(auth)
28+
+ auth_setting = (await self.configuration.auth_settings()).get(auth)
29+
if auth_setting:
30+
self._apply_auth_params(headers, queries, auth_setting)
31+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
diff --git a/kubernetes_asyncio/client/configuration.py b/kubernetes_asyncio/client/configuration.py
2+
index 720bb81f..b2522c23 100644
3+
--- a/kubernetes_asyncio/client/configuration.py
4+
+++ b/kubernetes_asyncio/client/configuration.py
5+
@@ -12,6 +12,7 @@
6+
7+
from __future__ import absolute_import
8+
9+
+import asyncio
10+
import copy
11+
import logging
12+
import sys
13+
@@ -370,7 +371,7 @@ conf = client.Configuration(
14+
self.__logger_format = value
15+
self.logger_formatter = logging.Formatter(self.__logger_format)
16+
17+
- def get_api_key_with_prefix(self, identifier, alias=None):
18+
+ async def get_api_key_with_prefix(self, identifier, alias=None):
19+
"""Gets API key (with prefix if set).
20+
21+
:param identifier: The identifier of apiKey.
22+
@@ -378,7 +379,9 @@ conf = client.Configuration(
23+
:return: The token for api key authentication.
24+
"""
25+
if self.refresh_api_key_hook is not None:
26+
- self.refresh_api_key_hook(self)
27+
+ result = self.refresh_api_key_hook(self)
28+
+ if asyncio.iscoroutine(result):
29+
+ await result
30+
key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
31+
if key:
32+
prefix = self.api_key_prefix.get(identifier)
33+
@@ -402,7 +405,7 @@ conf = client.Configuration(
34+
basic_auth=username + ':' + password
35+
).get('authorization')
36+
37+
- def auth_settings(self):
38+
+ async def auth_settings(self):
39+
"""Gets Auth Settings dict for api client.
40+
41+
:return: The Auth Settings information dict.
42+
@@ -413,7 +416,7 @@ conf = client.Configuration(
43+
'type': 'api_key',
44+
'in': 'header',
45+
'key': 'authorization',
46+
- 'value': self.get_api_key_with_prefix(
47+
+ 'value': await self.get_api_key_with_prefix(
48+
'BearerToken',
49+
),
50+
}

scripts/update-client.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ echo ">>> don't deep-copy configuration for local_vars_configuration in models"
8383
patch "${CLIENT_ROOT}/client/configuration.py" "${SCRIPT_ROOT}/client_configuration_get_default_patch.diff"
8484
find "${CLIENT_ROOT}/client/models/" -type f -print0 | xargs -0 sed -i 's/local_vars_configuration = Configuration.get_default_copy()/local_vars_configuration = Configuration.get_default()/g'
8585

86+
echo ">>> fix generated api client and configuration for async token refreshing..."
87+
patch "${CLIENT_ROOT}/client/api_client.py" "${SCRIPT_ROOT}/api_client_async_refresh_api_key_hook.diff"
88+
patch "${CLIENT_ROOT}/client/configuration.py" "${SCRIPT_ROOT}/client_configuration_async_refresh_api_key_hook.diff"
8689

8790
echo ">>> Remove invalid tests (workaround https://github.com/OpenAPITools/openapi-generator/issues/5377)"
8891
grep -r make_instance "${CLIENT_ROOT}/test/" | awk '{ gsub(":", ""); print $1}' | sort | uniq | xargs rm

0 commit comments

Comments
 (0)