Skip to content

Commit f9617ee

Browse files
committed
chore: change a few things, fix a few things...
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 481f59e commit f9617ee

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

ibm_cloud_sdk_core/authenticators/iam_assume_authenticator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ def validate(self) -> None:
126126
Raises:
127127
ValueError: The apikey, client_id, and/or client_secret are not valid for IAM token requests.
128128
"""
129-
super().validate()
130-
131129
# Create a temporary IAM authenticator that we can use to validat our delegate.
132130
tmp_authenticator = IAMAuthenticator("")
133131
tmp_authenticator.token_manager = self.token_manager.iam_delegate

ibm_cloud_sdk_core/token_managers/iam_assume_token_manager.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ def __init__(
8484
) -> None:
8585
super().__init__(
8686
url=url,
87-
client_id=client_id,
88-
client_secret=client_secret,
8987
disable_ssl_verification=disable_ssl_verification,
9088
headers=headers,
9189
proxies=proxies,
92-
scope=scope,
9390
)
9491

9592
self.iam_profile_id = iam_profile_id
@@ -114,14 +111,6 @@ def __init__(
114111
self.request_payload['grant_type'] = 'urn:ibm:params:oauth:grant-type:assume'
115112
self._set_user_agent(_build_user_agent('iam-assume-authenticator'))
116113

117-
# Remove unsupported attributes, inherited from the parent class.
118-
def __getattribute__(self, name: str) -> Any:
119-
disallowed_attrs = ['refresh_token', 'client_id', 'client_secret']
120-
if name in disallowed_attrs:
121-
raise AttributeError(f"'{self.__class__.__name__}' has no attribute '{name}'")
122-
123-
return super().__getattribute__(name)
124-
125114
def request_token(self) -> Dict:
126115
"""Retrieves a standard IAM access token by using the IAM token manager
127116
then obtains another access token for the assumed identity.
@@ -140,4 +129,14 @@ def request_token(self) -> Dict:
140129
self.request_payload['profile_name'] = self.iam_profile_name
141130
self.request_payload['account'] = self.iam_account_id
142131

132+
# Make sure that the unsupported attributes will never be included in the requests.
133+
self.client_id = None
134+
self.client_secret = None
135+
self.scope = None
136+
143137
return super().request_token()
138+
139+
def _save_token_info(self, token_response: Dict) -> None:
140+
super()._save_token_info(token_response)
141+
# Set refresh token to None unconditionally.
142+
self.refresh_token = None

test/test_iam_assume_token_manager.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def test_request_token_with_profile_name_and_account_id():
148148

149149
@responses.activate
150150
def test_request_token_uses_the_correct_grant_types():
151-
iam_url = "https://iam.cloud.ibm.com/identity/token"
152-
responses.add(responses.POST, url=iam_url, body=BASE_RESPONSE_JSON, status=200)
151+
responses.add(responses.POST, url=IAM_URL, body=BASE_RESPONSE_JSON, status=200)
153152

154153
token_manager = IAMAssumeTokenManager("apikey", iam_profile_id='my_profile_id')
155154
token_manager.request_token()
@@ -192,7 +191,19 @@ def test_get_token():
192191
# The final result should be the other access token, which belong to the "assume" request.
193192
assert access_token == OTHER_ACCESS_TOKEN
194193

195-
# Make sure `refresh_token` is not accessible.
196-
with pytest.raises(AttributeError) as err:
197-
assert token_manager.refresh_token == "not_available"
198-
assert str(err.value) == "'IAMAssumeTokenManager' has no attribute 'refresh_token'"
194+
# Make sure `refresh_token` is None.
195+
assert token_manager.refresh_token is None
196+
197+
@responses.activate
198+
def test_correct_properties_used_in_calls():
199+
responses.add_callback(responses.POST, url=IAM_URL, callback=request_callback)
200+
201+
token_manager = IAMAssumeTokenManager("apikey", iam_profile_id=MY_PROFILE_ID, client_id='my_client_id', client_secret='my_client_secret', scope='my_scope')
202+
token_manager.get_token()
203+
204+
# Make sure the all properties were used in the first request via the IAM delegate,
205+
assert responses.calls[0].request.headers.get('Authorization') == 'Basic bXlfY2xpZW50X2lkOm15X2NsaWVudF9zZWNyZXQ='
206+
assert 'scope=my_scope' in responses.calls[0].request.body
207+
# but those were not included in the second, assume type request.
208+
assert responses.calls[1].request.headers.get('Authorization') is None
209+
assert 'scope=my_scope' not in responses.calls[1].request.body

0 commit comments

Comments
 (0)