Skip to content

Commit ae7ead8

Browse files
committed
chore: disable all setter methods in the assume token manager and authenticator
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 999ce92 commit ae7ead8

File tree

3 files changed

+24
-93
lines changed

3 files changed

+24
-93
lines changed

ibm_cloud_sdk_core/authenticators/iam_assume_authenticator.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
from .authenticator import Authenticator
2323
from .iam_request_based_authenticator import IAMRequestBasedAuthenticator
24-
from ..utils import has_bad_first_or_last_char
2524

2625

2726
class IAMAssumeAuthenticator(IAMRequestBasedAuthenticator):
@@ -103,10 +102,9 @@ def __init__(
103102

104103
self.validate()
105104

106-
# Disable a few methods, inherited from the parent class.
105+
# Disable all setter methods, inherited from the parent class.
107106
def __getattribute__(self, name: str) -> Any:
108-
disallowed_attrs = ['set_scope', 'set_client_id_and_secret']
109-
if name in disallowed_attrs:
107+
if name.startswith("set_"):
110108
raise AttributeError(f"'{self.__class__.__name__}' has no attribute '{name}'")
111109

112110
return super().__getattribute__(name)
@@ -146,34 +144,3 @@ def validate(self) -> None:
146144
# `iam_account_id` must be specified iff `iam_profile_name` is used.
147145
if self.token_manager.iam_profile_name and not self.token_manager.iam_account_id:
148146
raise ValueError('`iam_profile_name` and `iam_account_id` must be provided together, or not at all.')
149-
150-
def set_disable_ssl_verification(self, status: bool = False) -> None:
151-
self.token_manager.iam_delegate.set_disable_ssl_verification(status)
152-
return super().set_disable_ssl_verification(status)
153-
154-
def set_iam_profile_id(self, iam_profile_id: str) -> None:
155-
"""Set the ID of the IAM profile.
156-
157-
Args:
158-
iam_profile_id (str): the ID of the IAM profile.
159-
"""
160-
self.token_manager.iam_profile_id = iam_profile_id
161-
162-
def set_iam_profile_crn(self, iam_profile_crn: str) -> None:
163-
"""Set the CRN of the IAM profile.
164-
165-
Args:
166-
iam_profile_crn (str): the CRN of the IAM profile.
167-
"""
168-
self.token_manager.iam_profile_crn = iam_profile_crn
169-
170-
def set_iam_profile_name_and_account_id(self, iam_profile_name: str, iam_account_id: str) -> None:
171-
"""Set both the name of the IAM profile and the IAM account ID that the profile belongs to.
172-
173-
Args:
174-
iam_profile_name (str): name of the IAM profile.
175-
iam_account_id (str): ID of the IAM account.
176-
"""
177-
self.token_manager.iam_profile_name = iam_profile_name
178-
self.token_manager.iam_account_id = iam_account_id
179-
self.validate()

ibm_cloud_sdk_core/token_managers/iam_assume_token_manager.py

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

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

1919
from ibm_cloud_sdk_core.token_managers.iam_token_manager import IAMTokenManager
2020

@@ -112,6 +112,13 @@ def __init__(
112112
self.request_payload['grant_type'] = 'urn:ibm:params:oauth:grant-type:assume'
113113
self._set_user_agent(_build_user_agent('iam-assume-authenticator'))
114114

115+
# Disable all setter methods, inherited from the parent class.
116+
def __getattribute__(self, name: str) -> Any:
117+
if name.startswith("set_"):
118+
raise AttributeError(f"'{self.__class__.__name__}' has no attribute '{name}'")
119+
120+
return super().__getattribute__(name)
121+
115122
def request_token(self) -> Dict:
116123
"""Retrieves a standard IAM access token by using the IAM token manager
117124
then obtains another access token for the assumed identity.

test/test_iam_assume_authenticator.py

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,63 +30,6 @@ def test_iam_assume_authenticator():
3030
assert authenticator.token_manager.iam_account_id is None
3131
assert authenticator.token_manager.scope is None
3232

33-
authenticator.set_iam_profile_id('my-id-123')
34-
assert authenticator.token_manager.iam_profile_id == 'my-id-123'
35-
36-
authenticator.set_iam_profile_crn('crn:iam-profile:456')
37-
assert authenticator.token_manager.iam_profile_crn == 'crn:iam-profile:456'
38-
39-
# We need to unset the other IAM related attributes to avoid validation error.
40-
authenticator.set_iam_profile_id(None)
41-
authenticator.set_iam_profile_crn(None)
42-
authenticator.set_iam_profile_name_and_account_id('my-profile-name', 'my-acc-id')
43-
assert authenticator.token_manager.iam_profile_name == 'my-profile-name'
44-
assert authenticator.token_manager.iam_account_id == 'my-acc-id'
45-
46-
with pytest.raises(TypeError) as err:
47-
authenticator.set_headers('dummy')
48-
assert str(err.value) == 'headers must be a dictionary'
49-
50-
authenticator.set_headers({'dummy': 'headers'})
51-
assert authenticator.token_manager.headers == {'dummy': 'headers'}
52-
53-
with pytest.raises(TypeError) as err:
54-
authenticator.set_proxies('dummy')
55-
assert str(err.value) == 'proxies must be a dictionary'
56-
57-
authenticator.set_proxies({'dummy': 'proxies'})
58-
assert authenticator.token_manager.proxies == {'dummy': 'proxies'}
59-
60-
authenticator.set_disable_ssl_verification(True)
61-
assert authenticator.token_manager.disable_ssl_verification
62-
63-
64-
def test_disable_ssl_verification():
65-
authenticator = IAMAssumeAuthenticator(
66-
apikey='my_apikey', iam_profile_id='my-profile-id', disable_ssl_verification=True
67-
)
68-
assert authenticator.token_manager.disable_ssl_verification is True
69-
assert authenticator.token_manager.iam_delegate.disable_ssl_verification is True
70-
71-
authenticator.set_disable_ssl_verification(False)
72-
assert authenticator.token_manager.disable_ssl_verification is False
73-
assert authenticator.token_manager.iam_delegate.disable_ssl_verification is False
74-
75-
76-
def test_invalid_disable_ssl_verification_type():
77-
with pytest.raises(TypeError) as err:
78-
authenticator = IAMAssumeAuthenticator(
79-
apikey='my_apikey', iam_profile_id='my-profile-id', disable_ssl_verification='True'
80-
)
81-
assert str(err.value) == 'disable_ssl_verification must be a bool'
82-
83-
authenticator = IAMAssumeAuthenticator(apikey='my_apikey', iam_profile_id='my-profile-id')
84-
assert authenticator.token_manager.disable_ssl_verification is False
85-
86-
with pytest.raises(TypeError) as err:
87-
authenticator.set_disable_ssl_verification('True')
88-
assert str(err.value) == 'status must be a bool'
89-
9033

9134
def test_iam_assume_authenticator_validate_failed():
9235
with pytest.raises(ValueError) as err:
@@ -217,9 +160,23 @@ def test_multiple_iam_assume_authenticators():
217160

218161
def test_iam_assume_authenticator_unsupported_methods():
219162
authenticator = IAMAssumeAuthenticator('my_apikey', iam_profile_id='my_profile_id')
163+
220164
with pytest.raises(AttributeError) as err:
221165
authenticator.set_scope('my_scope')
222166
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_scope'"
167+
223168
with pytest.raises(AttributeError) as err:
224169
authenticator.set_client_id_and_secret('my_client_id', 'my_client_secret')
225170
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_client_id_and_secret'"
171+
172+
with pytest.raises(AttributeError) as err:
173+
authenticator.set_headers({})
174+
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_headers'"
175+
176+
with pytest.raises(AttributeError) as err:
177+
authenticator.set_proxies({})
178+
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_proxies'"
179+
180+
with pytest.raises(AttributeError) as err:
181+
authenticator.set_disable_ssl_verification(True)
182+
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_disable_ssl_verification'"

0 commit comments

Comments
 (0)