Skip to content

Commit 999ce92

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

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

ibm_cloud_sdk_core/authenticators/iam_assume_authenticator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ 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-
131-
# Create a temporary IAM authenticator that we can use to validat our delegate.
129+
# Create a temporary IAM authenticator that we can use to validate our delegate.
132130
tmp_authenticator = IAMAuthenticator("")
133131
tmp_authenticator.token_manager = self.token_manager.iam_delegate
134132
tmp_authenticator.validate()

ibm_cloud_sdk_core/token_managers/iam_assume_token_manager.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from typing import Any, Dict, Optional
17+
from typing import Dict, Optional
1818

1919
from ibm_cloud_sdk_core.token_managers.iam_token_manager import IAMTokenManager
2020

2121
from .iam_request_based_token_manager import IAMRequestBasedTokenManager
2222
from ..private_helpers import _build_user_agent
2323

2424

25+
# pylint: disable=too-many-instance-attributes
2526
class IAMAssumeTokenManager(IAMRequestBasedTokenManager):
2627
"""The IAMAssumeTokenManager takes an api key and information about a trusted profile then performs the necessary
2728
interactions with the IAM token service to obtain and store a suitable bearer token. This token "assumes" the
@@ -84,12 +85,9 @@ def __init__(
8485
) -> None:
8586
super().__init__(
8687
url=url,
87-
client_id=client_id,
88-
client_secret=client_secret,
8988
disable_ssl_verification=disable_ssl_verification,
9089
headers=headers,
9190
proxies=proxies,
92-
scope=scope,
9391
)
9492

9593
self.iam_profile_id = iam_profile_id
@@ -114,14 +112,6 @@ def __init__(
114112
self.request_payload['grant_type'] = 'urn:ibm:params:oauth:grant-type:assume'
115113
self._set_user_agent(_build_user_agent('iam-assume-authenticator'))
116114

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-
125115
def request_token(self) -> Dict:
126116
"""Retrieves a standard IAM access token by using the IAM token manager
127117
then obtains another access token for the assumed identity.
@@ -140,4 +130,14 @@ def request_token(self) -> Dict:
140130
self.request_payload['profile_name'] = self.iam_profile_name
141131
self.request_payload['account'] = self.iam_account_id
142132

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

test/test_iam_assume_token_manager.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import urllib
2222

2323
import jwt
24-
import pytest
2524
import responses
2625

2726
from ibm_cloud_sdk_core import IAMAssumeTokenManager
@@ -148,8 +147,7 @@ def test_request_token_with_profile_name_and_account_id():
148147

149148
@responses.activate
150149
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)
150+
responses.add(responses.POST, url=IAM_URL, body=BASE_RESPONSE_JSON, status=200)
153151

154152
token_manager = IAMAssumeTokenManager("apikey", iam_profile_id='my_profile_id')
155153
token_manager.request_token()
@@ -192,7 +190,26 @@ def test_get_token():
192190
# The final result should be the other access token, which belong to the "assume" request.
193191
assert access_token == OTHER_ACCESS_TOKEN
194192

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'"
193+
# Make sure `refresh_token` is None.
194+
assert token_manager.refresh_token is None
195+
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(
202+
"apikey",
203+
iam_profile_id=MY_PROFILE_ID,
204+
client_id='my_client_id',
205+
client_secret='my_client_secret',
206+
scope='my_scope',
207+
)
208+
token_manager.get_token()
209+
210+
# Make sure the all properties were used in the first request via the IAM delegate,
211+
assert responses.calls[0].request.headers.get('Authorization') == 'Basic bXlfY2xpZW50X2lkOm15X2NsaWVudF9zZWNyZXQ='
212+
assert 'scope=my_scope' in responses.calls[0].request.body
213+
# but those were not included in the second, assume type request.
214+
assert responses.calls[1].request.headers.get('Authorization') is None
215+
assert 'scope=my_scope' not in responses.calls[1].request.body

0 commit comments

Comments
 (0)