Skip to content

Commit 29b5dc5

Browse files
committed
chore: add back removed properties
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 99ea907 commit 29b5dc5

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

ibm_cloud_sdk_core/authenticators/iam_assume_authenticator.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ class IAMAssumeAuthenticator(IAMRequestBasedAuthenticator):
4242
iam_profile_name: the name of the trusted profile (must be used together with `iam_account_id`)
4343
iam_account_id: the ID of the trusted profile (must be used together with `iam_profile_name`)
4444
url: The URL representing the IAM token service endpoint. If not specified, a suitable default value is used.
45+
client_id: The client_id and client_secret fields are used to form
46+
a "basic" authorization header for IAM token requests. Defaults to None.
47+
client_secret: The client_id and client_secret fields are used to form
48+
a "basic" authorization header for IAM token requests. Defaults to None.
4549
disable_ssl_verification: A flag that indicates whether verification of
4650
the server's SSL certificate should be disabled or not. Defaults to False.
4751
headers: Default headers to be sent with every IAM token request. Defaults to None.
4852
proxies: Dictionary for mapping request protocol to proxy URL. Defaults to None.
4953
proxies.http (optional): The proxy endpoint to use for HTTP requests.
5054
proxies.https (optional): The proxy endpoint to use for HTTPS requests.
55+
scope: The "scope" to use when fetching the bearer token from the IAM token server.
56+
This can be used to obtain an access token with a specific scope.
5157
5258
Attributes:
5359
token_manager (IAMTokenManager): Retrieves and manages IAM tokens from the endpoint specified by the url.
@@ -56,6 +62,7 @@ class IAMAssumeAuthenticator(IAMRequestBasedAuthenticator):
5662
TypeError: The `disable_ssl_verification` is not a bool.
5763
ValueError: The `apikey` is not valid for IAM token requests or the following keyword arguments are incorrectly specified:
5864
`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.
5966
"""
6067

6168
def __init__(
@@ -67,9 +74,12 @@ def __init__(
6774
iam_profile_name: Optional[str] = None,
6875
iam_account_id: Optional[str] = None,
6976
url: Optional[str] = None,
77+
client_id: Optional[str] = None,
78+
client_secret: Optional[str] = None,
7079
disable_ssl_verification: bool = False,
7180
headers: Optional[Dict[str, str]] = None,
7281
proxies: Optional[Dict[str, str]] = None,
82+
scope: Optional[str] = None,
7383
) -> None:
7484
# Check the type of `disable_ssl_verification`. Must be a bool.
7585
if not isinstance(disable_ssl_verification, bool):
@@ -82,9 +92,12 @@ def __init__(
8292
iam_profile_name=iam_profile_name,
8393
iam_account_id=iam_account_id,
8494
url=url,
95+
client_id=client_id,
96+
client_secret=client_secret,
8597
disable_ssl_verification=disable_ssl_verification,
8698
headers=headers,
8799
proxies=proxies,
100+
scope=scope,
88101
)
89102

90103
self.validate()

ibm_cloud_sdk_core/get_authenticator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ def __construct_authenticator(config: dict) -> Authenticator:
114114
iam_profile_name=config.get('IAM_PROFILE_NAME'),
115115
iam_account_id=config.get('IAM_ACCOUNT_ID'),
116116
url=config.get('AUTH_URL'),
117+
client_id=config.get('CLIENT_ID'),
118+
client_secret=config.get('CLIENT_SECRET'),
117119
disable_ssl_verification=config.get('AUTH_DISABLE_SSL', 'false').lower() == 'true',
120+
scope=config.get('SCOPE'),
118121
)
119122
elif auth_type == Authenticator.AUTHTYPE_VPC.lower():
120123
authenticator = VPCInstanceAuthenticator(

ibm_cloud_sdk_core/token_managers/iam_assume_token_manager.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ class IAMAssumeTokenManager(IAMRequestBasedTokenManager):
4949
iam_profile_name: the name of the trusted profile (must be used together with `iam_account_id`)
5050
iam_account_id: the ID of the trusted profile (must be used together with `iam_profile_name`)
5151
url: The IAM endpoint to token requests. Defaults to None.
52+
client_id: The client_id and client_secret fields are used to form
53+
a "basic auth" Authorization header for interactions with the IAM token server.
54+
Defaults to None.
55+
client_secret: The client_id and client_secret fields are used to form
56+
a "basic auth" Authorization header for interactions with the IAM token server.
57+
Defaults to None.
5258
disable_ssl_verification: A flag that indicates whether verification of
5359
the server's SSL certificate should be disabled or not. Defaults to False.
5460
headers: Default headers to be sent with every IAM token request. Defaults to None.
5561
proxies: Proxies to use for communicating with IAM. Defaults to None.
5662
proxies.http: The proxy endpoint to use for HTTP requests.
5763
proxies.https: The proxy endpoint to use for HTTPS requests.
64+
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.
5866
"""
5967

6068
def __init__(
@@ -66,15 +74,21 @@ def __init__(
6674
iam_profile_name: Optional[str] = None,
6775
iam_account_id: Optional[str] = None,
6876
url: Optional[str] = None,
77+
client_id: Optional[str] = None,
78+
client_secret: Optional[str] = None,
6979
disable_ssl_verification: bool = False,
7080
headers: Optional[Dict[str, str]] = None,
7181
proxies: Optional[Dict[str, str]] = None,
82+
scope: Optional[str] = None,
7283
) -> None:
7384
super().__init__(
7485
url=url,
86+
client_id=client_id,
87+
client_secret=client_secret,
7588
disable_ssl_verification=disable_ssl_verification,
7689
headers=headers,
7790
proxies=proxies,
91+
scope=scope,
7892
)
7993

8094
self.iam_profile_id = iam_profile_id
@@ -88,9 +102,12 @@ def __init__(
88102
self.iam_delegate = IAMTokenManager(
89103
apikey=apikey,
90104
url=url,
105+
client_id=client_id,
106+
client_secret=client_secret,
91107
disable_ssl_verification=disable_ssl_verification,
92108
headers=headers,
93109
proxies=proxies,
110+
scope=scope,
94111
)
95112

96113
self.request_payload['grant_type'] = 'urn:ibm:params:oauth:grant-type:assume'

test/test_iam_assume_authenticator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def test_iam_assume_authenticator():
1818
assert authenticator is not None
1919
assert authenticator.authentication_type() == Authenticator.AUTHTYPE_IAM_ASSUME
2020
assert authenticator.token_manager.url == 'https://iam.cloud.ibm.com'
21+
assert authenticator.token_manager.client_id is None
22+
assert authenticator.token_manager.client_secret is None
2123
assert authenticator.token_manager.disable_ssl_verification is False
2224
assert authenticator.token_manager.headers is None
2325
assert authenticator.token_manager.proxies is None
@@ -26,6 +28,7 @@ def test_iam_assume_authenticator():
2628
assert authenticator.token_manager.iam_profile_crn == 'crn:iam-profile:123'
2729
assert authenticator.token_manager.iam_profile_name is None
2830
assert authenticator.token_manager.iam_account_id is None
31+
assert authenticator.token_manager.scope is None
2932

3033
authenticator.set_iam_profile_id('my-id-123')
3134
assert authenticator.token_manager.iam_profile_id == 'my-id-123'
@@ -40,6 +43,13 @@ def test_iam_assume_authenticator():
4043
assert authenticator.token_manager.iam_profile_name == 'my-profile-name'
4144
assert authenticator.token_manager.iam_account_id == 'my-acc-id'
4245

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+
4353
with pytest.raises(TypeError) as err:
4454
authenticator.set_headers('dummy')
4555
assert str(err.value) == 'headers must be a dictionary'
@@ -146,6 +156,14 @@ def test_iam_assume_authenticator_validate_failed():
146156
str(err.value) == 'Exactly one of `iam_profile_id`, `iam_profile_crn`, or `iam_profile_name` must be specified.'
147157
)
148158

159+
with pytest.raises(ValueError) as err:
160+
IAMAssumeAuthenticator('my_apikey', client_id='my_client_id')
161+
assert str(err.value) == 'Both client_id and client_secret should be initialized.'
162+
163+
with pytest.raises(ValueError) as err:
164+
IAMAssumeAuthenticator('my_apikey', client_secret='my_client_secret')
165+
assert str(err.value) == 'Both client_id and client_secret should be initialized.'
166+
149167

150168
@responses.activate
151169
def test_get_token():

test/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ def test_get_authenticator_from_credential_file():
294294
assert authenticator.token_manager.iam_delegate.apikey == 'my-api-key'
295295
assert authenticator.token_manager.iam_profile_id == 'iam-profile-1'
296296
assert authenticator.token_manager.url == 'https://iam.cloud.ibm.com'
297+
assert authenticator.token_manager.client_id is None
298+
assert authenticator.token_manager.client_secret is None
297299
assert authenticator.token_manager.disable_ssl_verification is False
300+
assert authenticator.token_manager.scope is None
298301
del os.environ['IBM_CREDENTIALS_FILE']
299302

300303
file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-basic.env')

0 commit comments

Comments
 (0)