Skip to content

Commit 5fba59b

Browse files
committed
fix: address review comments; support AUTHTYPE property
1 parent fce4dd1 commit 5fba59b

File tree

6 files changed

+41
-20
lines changed

6 files changed

+41
-20
lines changed

ibm_cloud_sdk_core/authenticators/container_authenticator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ class ContainerAuthenticator(IAMRequestBasedAuthenticator):
3232
cr_token_filename: The name of the file containing the injected CR token value
3333
(applies to IKS-managed compute resources). Defaults to "/var/run/secrets/tokens/vault-token".
3434
iam_profile_name: The name of the linked trusted IAM profile to be used when obtaining the IAM access token
35-
(a CR token might map to multiple IAM profiles). One of IAMProfileName or IAMProfileID must be specified.
35+
(a CR token might map to multiple IAM profiles).
36+
One of iam_profile_name or iam_profile_id must be specified.
3637
Defaults to None.
3738
iam_profile_id: The id of the linked trusted IAM profile to be used when obtaining the IAM access token
38-
(a CR token might map to multiple IAM profiles). One of IAMProfileName or IAMProfileID must be specified.
39+
(a CR token might map to multiple IAM profiles).
40+
One of iam_profile_name or iam_profile_id must be specified.
3941
Defaults to None.
4042
url: The URL representing the IAM token service endpoint. If not specified, a suitable default value is used.
4143
client_id: The client_id and client_secret fields are used to form
@@ -97,7 +99,8 @@ def validate(self) -> None:
9799
super().validate()
98100

99101
if not self.token_manager.iam_profile_name and not self.token_manager.iam_profile_id:
100-
raise ValueError('At least one of iam_profile_name or iam_profile_id must be specified.')
102+
raise ValueError(
103+
'At least one of iam_profile_name or iam_profile_id must be specified.')
101104

102105
def set_cr_token_filename(self, cr_token_filename: str) -> None:
103106
"""Set the location of the compute resource token on the local filesystem.

ibm_cloud_sdk_core/get_authenticator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def __construct_authenticator(config: dict) -> Authenticator:
4444
# Determine the authentication type if not specified explicitly.
4545
if config.get('AUTH_TYPE'):
4646
auth_type = config.get('AUTH_TYPE').lower()
47+
elif config.get('AUTHTYPE'):
48+
auth_type = config.get('AUTHTYPE').lower()
4749
else:
4850
# If the APIKEY property is specified, then it should be IAM, otherwise Container Auth.
4951
auth_type = 'iam' if config.get('APIKEY') else 'container'
@@ -65,7 +67,8 @@ def __construct_authenticator(config: dict) -> Authenticator:
6567
url=config.get('AUTH_URL'),
6668
client_id=config.get('CLIENT_ID'),
6769
client_secret=config.get('CLIENT_SECRET'),
68-
disable_ssl_verification=config.get('AUTH_DISABLE_SSL', 'false').lower() == 'true',
70+
disable_ssl_verification=config.get(
71+
'AUTH_DISABLE_SSL', 'false').lower() == 'true',
6972
scope=config.get('SCOPE'))
7073
elif auth_type == 'cp4d':
7174
authenticator = CloudPakForDataAuthenticator(
@@ -80,7 +83,8 @@ def __construct_authenticator(config: dict) -> Authenticator:
8083
url=config.get('AUTH_URL'),
8184
client_id=config.get('CLIENT_ID'),
8285
client_secret=config.get('CLIENT_SECRET'),
83-
disable_ssl_verification=config.get('AUTH_DISABLE_SSL', 'false').lower() == 'true',
86+
disable_ssl_verification=config.get(
87+
'AUTH_DISABLE_SSL', 'false').lower() == 'true',
8488
scope=config.get('SCOPE'))
8589
elif auth_type == 'noauth':
8690
authenticator = NoAuthAuthenticator()

ibm_cloud_sdk_core/token_managers/container_token_manager.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class ContainerTokenManager(IAMRequestBasedTokenManager):
3232
(applies to IKS-managed compute resources).
3333
iam_profile_name (str): The name of the linked trusted IAM profile to be used when obtaining the
3434
IAM access token (a CR token might map to multiple IAM profiles).
35-
One of IAMProfileName or IAMProfileID must be specified.
35+
One of iam_profile_name or iam_profile_id must be specified.
3636
iam_profile_id (str): The id of the linked trusted IAM profile to be used when obtaining the IAM access token
37-
(a CR token might map to multiple IAM profiles). One of IAMProfileName or IAMProfileID must be specified.
37+
(a CR token might map to multiple IAM profiles).
38+
One of iam_profile_name or iam_profile_id must be specified.
3839
url (str): The IAM endpoint to token requests.
3940
client_id (str): The client_id and client_secret fields are used to form
4041
a "basic auth" Authorization header for interactions with the IAM token server.
@@ -52,10 +53,12 @@ class ContainerTokenManager(IAMRequestBasedTokenManager):
5253
cr_token_filename: The name of the file containing the injected CR token value
5354
(applies to IKS-managed compute resources). Defaults to "/var/run/secrets/tokens/vault-token".
5455
iam_profile_name: The name of the linked trusted IAM profile to be used when obtaining the IAM access token
55-
(a CR token might map to multiple IAM profiles). One of IAMProfileName or IAMProfileID must be specified.
56-
sDefaults to None.
56+
(a CR token might map to multiple IAM profiles).
57+
One of iam_profile_name or iam_profile_id must be specified.
58+
Defaults to None.
5759
iam_profile_id: The id of the linked trusted IAM profile to be used when obtaining the IAM access token
58-
(a CR token might map to multiple IAM profiles). One of IAMProfileName or IAMProfileID must be specified.
60+
(a CR token might map to multiple IAM profiles).
61+
One of iam_profile_name or iam_prfoile_id must be specified.
5962
Defaults to None.
6063
url: The IAM endpoint to token requests. Defaults to None.
6164
client_id: The client_id and client_secret fields are used to form
@@ -107,7 +110,8 @@ def retrieve_cr_token(self) -> str:
107110
"""
108111
cr_token_filename = self.cr_token_filename if self.cr_token_filename else self.DEFAULT_CR_TOKEN_FILENAME
109112

110-
logging.debug('Attempting to read CR token from file: %s', cr_token_filename)
113+
logging.debug('Attempting to read CR token from file: %s',
114+
cr_token_filename)
111115

112116
try:
113117
with open(cr_token_filename, 'r') as file:

resources/ibm-credentials-basic.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
WATSON_USERNAME=my_username
22
WATSON_PASSWORD=my_password
3-
WATSON_AUTH_TYPE=basic
3+
WATSON_AUTHTYPE=basic

resources/ibm-credentials-bearer.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
WATSON_BEARER_TOKEN=my_token
2-
WATSON_AUTH_TYPE=bearerToken
2+
WATSON_AUTHTYPE=bearerToken

test/test_container_authenticator.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
def test_container_authenticator():
88
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123')
99
assert authenticator is not None
10+
assert authenticator.token_manager.cr_token_filename is None
11+
assert authenticator.token_manager.iam_profile_name == 'iam-user-123'
12+
assert authenticator.token_manager.iam_profile_id is None
1013
assert authenticator.token_manager.client_id is None
1114
assert authenticator.token_manager.client_secret is None
1215
assert authenticator.token_manager.disable_ssl_verification is False
@@ -21,7 +24,8 @@ def test_container_authenticator():
2124
# because both of the profile and ID are None.
2225
with pytest.raises(ValueError) as err:
2326
authenticator.set_iam_profile_name(None)
24-
assert str(err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
27+
assert str(
28+
err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
2529

2630
authenticator.set_iam_profile_id('iam-id-123')
2731
assert authenticator.token_manager.iam_profile_id == 'iam-id-123'
@@ -52,7 +56,8 @@ def test_container_authenticator():
5256

5357

5458
def test_disable_ssl_verification():
55-
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', disable_ssl_verification=True)
59+
authenticator = ContainerAuthenticator(
60+
iam_profile_name='iam-user-123', disable_ssl_verification=True)
5661
assert authenticator.token_manager.disable_ssl_verification is True
5762

5863
authenticator.set_disable_ssl_verification(False)
@@ -61,7 +66,8 @@ def test_disable_ssl_verification():
6166

6267
def test_invalid_disable_ssl_verification_type():
6368
with pytest.raises(TypeError) as err:
64-
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', disable_ssl_verification='True')
69+
authenticator = ContainerAuthenticator(
70+
iam_profile_name='iam-user-123', disable_ssl_verification='True')
6571
assert str(err.value) == 'disable_ssl_verification must be a bool'
6672

6773
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123')
@@ -73,22 +79,26 @@ def test_invalid_disable_ssl_verification_type():
7379

7480

7581
def test_container_authenticator_with_scope():
76-
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', scope='scope1 scope2')
82+
authenticator = ContainerAuthenticator(
83+
iam_profile_name='iam-user-123', scope='scope1 scope2')
7784
assert authenticator is not None
7885
assert authenticator.token_manager.scope == 'scope1 scope2'
7986

8087

8188
def test_authenticator_validate_failed():
8289
with pytest.raises(ValueError) as err:
8390
ContainerAuthenticator(None)
84-
assert str(err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
91+
assert str(
92+
err.value) == 'At least one of iam_profile_name or iam_profile_id must be specified.'
8593

8694
with pytest.raises(ValueError) as err:
87-
ContainerAuthenticator(iam_profile_name='iam-user-123', client_id='my_client_id')
95+
ContainerAuthenticator(
96+
iam_profile_name='iam-user-123', client_id='my_client_id')
8897
assert str(
8998
err.value) == 'Both client_id and client_secret should be initialized.'
9099

91100
with pytest.raises(ValueError) as err:
92-
ContainerAuthenticator(iam_profile_name='iam-user-123', client_secret='my_client_secret')
101+
ContainerAuthenticator(
102+
iam_profile_name='iam-user-123', client_secret='my_client_secret')
93103
assert str(
94104
err.value) == 'Both client_id and client_secret should be initialized.'

0 commit comments

Comments
 (0)