Skip to content

Commit 83e423a

Browse files
committed
chore: disallow unsupported parent methods, fix (new) linter issues
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 29b5dc5 commit 83e423a

File tree

7 files changed

+29
-14
lines changed

7 files changed

+29
-14
lines changed

ibm_cloud_sdk_core/authenticators/container_authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class ContainerAuthenticator(IAMRequestBasedAuthenticator):
6666

6767
def __init__(
6868
self,
69+
*,
6970
cr_token_filename: Optional[str] = None,
7071
iam_profile_name: Optional[str] = None,
7172
iam_profile_id: Optional[str] = None,

ibm_cloud_sdk_core/authenticators/iam_assume_authenticator.py

Lines changed: 12 additions & 4 deletions
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_assume_token_manager import IAMAssumeTokenManager
2020

@@ -60,9 +60,9 @@ class IAMAssumeAuthenticator(IAMRequestBasedAuthenticator):
6060
6161
Raises:
6262
TypeError: The `disable_ssl_verification` is not a bool.
63-
ValueError: The `apikey` is not valid for IAM token requests or the following keyword arguments are incorrectly specified:
64-
`iam_profile_id`, `iam_profile_crn`, `iam_profile_name`, `iam_account_id`,
65-
ValueError: The apikey, client_id, and/or client_secret are not valid for IAM token requests.
63+
ValueError: The `apikey`, `client_id`, and/or `client_secret` are not valid for IAM token requests or the
64+
following keyword arguments are incorrectly specified:
65+
`iam_profile_id`, `iam_profile_crn`, `iam_profile_name`, `iam_account_id`.
6666
"""
6767

6868
def __init__(
@@ -102,6 +102,14 @@ def __init__(
102102

103103
self.validate()
104104

105+
# Disable a few methods, inherited from the parent class.
106+
def __getattribute__(self, name: str) -> Any:
107+
disallowed_attrs = ['set_scope', 'set_client_id_and_secret']
108+
if name in disallowed_attrs:
109+
raise AttributeError(f"'IAMAssumeAuthenticator' has no attribute '{name}'")
110+
111+
return super().__getattribute__(name)
112+
105113
def authentication_type(self) -> str:
106114
"""Returns this authenticator's type ('iamAssume')."""
107115
return Authenticator.AUTHTYPE_IAM_ASSUME

ibm_cloud_sdk_core/token_managers/container_token_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class ContainerTokenManager(IAMRequestBasedTokenManager):
8585

8686
def __init__(
8787
self,
88+
*,
8889
cr_token_filename: Optional[str] = None,
8990
iam_profile_name: Optional[str] = None,
9091
iam_profile_id: Optional[str] = None,

ibm_cloud_sdk_core/token_managers/iam_assume_token_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class IAMAssumeTokenManager(IAMRequestBasedTokenManager):
3232
iam_profile_crn (str): the CRN of the trusted profile
3333
iam_profile_name (str): the name of the trusted profile (must be used together with `iam_account_id`)
3434
iam_account_id (str): the ID of the trusted profile (must be used together with `iam_profile_name`)
35-
iam_deletgate (IAMTokenManager): an IAMTokenManager instance used to obtain the user's IAM access token from the `apikey`.
35+
iam_deletgate (IAMTokenManager): an IAMTokenManager instance used to obtain the user's IAM access token
36+
from the `apikey`.
3637
url (str): The IAM endpoint to token requests.
3738
headers (dict): Default headers to be sent with every IAM token request.
3839
proxies (dict): Proxies to use for communicating with IAM.
@@ -62,7 +63,7 @@ class IAMAssumeTokenManager(IAMRequestBasedTokenManager):
6263
proxies.http: The proxy endpoint to use for HTTP requests.
6364
proxies.https: The proxy endpoint to use for HTTPS requests.
6465
scope: The "scope" to use when fetching the bearer token from the IAM token server.
65-
This can be used to obtain an access token with a specific scope.
66+
This can be used to obtain an access token with a specific scope.
6667
"""
6768

6869
def __init__(

ibm_cloud_sdk_core/token_managers/iam_request_based_token_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class IAMRequestBasedTokenManager(JWTTokenManager):
7070

7171
def __init__(
7272
self,
73+
*,
7374
url: Optional[str] = None,
7475
client_id: Optional[str] = None,
7576
client_secret: Optional[str] = None,

test/test_container_authenticator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_container_authenticator_with_scope():
8484

8585
def test_authenticator_validate_failed():
8686
with pytest.raises(ValueError) as err:
87-
ContainerAuthenticator(None)
87+
ContainerAuthenticator()
8888
assert str(err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
8989

9090
with pytest.raises(ValueError) as err:

test/test_iam_assume_authenticator.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ def test_iam_assume_authenticator():
4343
assert authenticator.token_manager.iam_profile_name == 'my-profile-name'
4444
assert authenticator.token_manager.iam_account_id == 'my-acc-id'
4545

46-
authenticator.set_client_id_and_secret('tom', 'jerry')
47-
assert authenticator.token_manager.client_id == 'tom'
48-
assert authenticator.token_manager.client_secret == 'jerry'
49-
50-
authenticator.set_scope('scope1 scope2 scope3')
51-
assert authenticator.token_manager.scope == 'scope1 scope2 scope3'
52-
5346
with pytest.raises(TypeError) as err:
5447
authenticator.set_headers('dummy')
5548
assert str(err.value) == 'headers must be a dictionary'
@@ -218,3 +211,13 @@ def test_multiple_iam_assume_authenticators():
218211
authenticator_2 = IAMAssumeAuthenticator('my_other_apikey', iam_profile_id='my_profile_id_2')
219212
assert authenticator_2.token_manager.iam_delegate.request_payload['apikey'] == 'my_other_apikey'
220213
assert authenticator_1.token_manager.iam_delegate.request_payload['apikey'] == 'my_apikey'
214+
215+
216+
def test_iam_assume_authenticator_unsupported_methods():
217+
authenticator = IAMAssumeAuthenticator('my_apikey', iam_profile_id='my_profile_id')
218+
with pytest.raises(AttributeError) as err:
219+
authenticator.set_scope('my_scope')
220+
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_scope'"
221+
with pytest.raises(AttributeError) as err:
222+
authenticator.set_client_id_and_secret('my_client_id', 'my_client_secret')
223+
assert str(err.value) == "'IAMAssumeAuthenticator' has no attribute 'set_client_id_and_secret'"

0 commit comments

Comments
 (0)