Skip to content

Commit b142e9e

Browse files
authored
fix: multiple IAM based authenticator overrides (#124)
This commit makes the `request_payload` instance variable. Previously it was a class variable and that could cause issues if there were more than one IAM based authenticator in the same project. There are also some unicode string and utf-8 encoding related changes to make the code compatible with the latest pylint release.
1 parent 4ac1246 commit b142e9e

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
lines changed

ibm_cloud_sdk_core/token_managers/container_token_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def retrieve_cr_token(self) -> str:
114114
cr_token_filename)
115115

116116
try:
117-
with open(cr_token_filename, 'r') as file:
117+
with open(cr_token_filename, 'r', encoding='utf-8') as file:
118118
cr_token = file.read()
119119
return cr_token
120120
# pylint: disable=broad-except

ibm_cloud_sdk_core/token_managers/iam_request_based_token_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .jwt_token_manager import JWTTokenManager
2020

2121

22+
#pylint: disable=too-many-instance-attributes
2223
class IAMRequestBasedTokenManager(JWTTokenManager):
2324
"""The IamRequestBasedTokenManager class contains code relevant to any token manager that
2425
interacts with the IAM service to manage a token. It stores information relevant to all
@@ -62,8 +63,6 @@ class IAMRequestBasedTokenManager(JWTTokenManager):
6263
DEFAULT_IAM_URL = 'https://iam.cloud.ibm.com'
6364
OPERATION_PATH = "/identity/token"
6465

65-
request_payload = {}
66-
6766
def __init__(self,
6867
url: Optional[str] = None,
6968
client_id: Optional[str] = None,
@@ -83,6 +82,7 @@ def __init__(self,
8382
self.refresh_token = None
8483
self.proxies = proxies
8584
self.scope = scope
85+
self.request_payload = {}
8686
super().__init__(
8787
self.url, disable_ssl_verification=disable_ssl_verification, token_name='access_token')
8888

ibm_cloud_sdk_core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def __read_from_credential_file(service_name: str,
314314
config = {}
315315
if credential_file_path is not None:
316316
try:
317-
with open(credential_file_path, 'r') as fobj:
317+
with open(credential_file_path, 'r', encoding='utf-8') as fobj:
318318
for line in fobj:
319319
key_val = line.strip().split(separator, 1)
320320
if len(key_val) == 2:

test/test_base_service.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_url_encoding():
134134
path0 = ' \"<>^`{}|/\\?#%[]'
135135
path0_encoded = '%20%22%3C%3E%5E%60%7B%7D%7C%2F%5C%3F%23%25%5B%5D'
136136
# All non-ASCII chars _must_ be encoded in path segments
137-
path1 = u'比萨浇头'.encode('utf8') # "pizza toppings"
137+
path1 = '比萨浇头'.encode('utf8') # "pizza toppings"
138138
path1_encoded = '%E6%AF%94%E8%90%A8%E6%B5%87%E5%A4%B4'
139139

140140
path_encoded = '/v1/foo/' + path0_encoded + '/bar/' + path1_encoded + '/baz'
@@ -741,7 +741,7 @@ def test_files_dict():
741741
form_data = {}
742742
with open(
743743
os.path.join(
744-
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r') as file:
744+
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8') as file:
745745
form_data['file1'] = (None, file, 'application/octet-stream')
746746
form_data['string1'] = (None, 'hello', 'text/plain')
747747
request = service.prepare_request(
@@ -762,7 +762,7 @@ def test_files_list():
762762
form_data = []
763763
with open(
764764
os.path.join(
765-
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r') as file:
765+
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8') as file:
766766
form_data.append(('file1', (None, file, 'application/octet-stream')))
767767
form_data.append(('string1', (None, 'hello', 'text/plain')))
768768
request = service.prepare_request(
@@ -783,17 +783,17 @@ def test_files_duplicate_parts():
783783
form_data = []
784784
with open(
785785
os.path.join(
786-
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r') as file:
786+
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8') as file:
787787
form_data.append(
788788
('creds_file', (None, file, 'application/octet-stream')))
789789
with open(
790790
os.path.join(
791-
os.path.dirname(__file__), '../resources/ibm-credentials-basic.env'), 'r') as file:
791+
os.path.dirname(__file__), '../resources/ibm-credentials-basic.env'), 'r', encoding='utf-8') as file:
792792
form_data.append(
793793
('creds_file', (None, file, 'application/octet-stream')))
794794
with open(
795795
os.path.join(
796-
os.path.dirname(__file__), '../resources/ibm-credentials-bearer.env'), 'r') as file:
796+
os.path.dirname(__file__), '../resources/ibm-credentials-bearer.env'), 'r', encoding='utf-8') as file:
797797
form_data.append(
798798
('creds_file', (None, file, 'application/octet-stream')))
799799
request = service.prepare_request(

test/test_container_token_manager.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def callback(request):
3737

3838
assert payload['cr_token'][0] == 'cr-token-1'
3939
assert payload['grant_type'][0] == 'urn:ibm:params:oauth:grant-type:cr-token'
40-
assert payload['profile_name'][0] or payload['iam_profile_id']
40+
assert payload.get('profile_name', [None])[0] or payload.get('profile_id', [None])[0]
4141

4242
status_code = 200
4343

@@ -264,12 +264,20 @@ def test_client_id_and_secret():
264264
def test_setter_methods():
265265
token_manager = ContainerTokenManager(
266266
cr_token_filename='bogus-cr-token-file',
267-
iam_profile_id=MOCK_IAM_PROFILE_NAME,
267+
iam_profile_name=MOCK_IAM_PROFILE_NAME,
268268
)
269269

270+
assert token_manager.iam_profile_id is None
271+
assert token_manager.iam_profile_name == MOCK_IAM_PROFILE_NAME
272+
assert token_manager.cr_token_filename == 'bogus-cr-token-file'
273+
270274
token_manager.set_iam_profile_id('iam-id-123')
271275
token_manager.set_iam_profile_name(None)
272276
token_manager.set_cr_token_filename(cr_token_file)
273277

278+
assert token_manager.iam_profile_id == 'iam-id-123'
279+
assert token_manager.iam_profile_name is None
280+
assert token_manager.cr_token_filename == cr_token_file
281+
274282
access_token = token_manager.get_token()
275283
assert access_token == TEST_ACCESS_TOKEN_1

test/test_detailed_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_detailed_response_dict():
2626
status_code=mock_response.status_code)
2727
assert detailed_response is not None
2828
assert detailed_response.get_result() == {'foobar': 'baz'}
29-
assert detailed_response.get_headers() == {u'Content-Type': 'application/json'}
29+
assert detailed_response.get_headers() == {'Content-Type': 'application/json'}
3030
assert detailed_response.get_status_code() == 200
3131

3232
response_str = clean(detailed_response.__str__())
@@ -48,7 +48,7 @@ def test_detailed_response_list():
4848
status_code=mock_response.status_code)
4949
assert detailed_response is not None
5050
assert detailed_response.get_result() == ['foobar', 'baz']
51-
assert detailed_response.get_headers() == {u'Content-Type': 'application/json'}
51+
assert detailed_response.get_headers() == {'Content-Type': 'application/json'}
5252
assert detailed_response.get_status_code() == 200
5353

5454
response_str = clean(detailed_response.__str__())

test/test_iam_authenticator.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,13 @@ def test_get_token():
142142
# Verify that the "get token" call contained the Host header.
143143
assert responses.calls[0].request.headers.get(
144144
'Host') == 'iam.cloud.ibm.com:443'
145+
146+
147+
def test_multiple_iam_authenticators():
148+
authenticator_1 = IAMAuthenticator('my_apikey')
149+
assert authenticator_1.token_manager.request_payload['apikey'] == 'my_apikey'
150+
151+
authenticator_2 = IAMAuthenticator('my_other_apikey')
152+
assert authenticator_2.token_manager.request_payload['apikey'] == 'my_other_apikey'
153+
154+
assert authenticator_1.token_manager.request_payload['apikey'] == 'my_apikey'

0 commit comments

Comments
 (0)