Skip to content

Commit 8cf4fc9

Browse files
committed
fix: allow '=' character in environment config values
1 parent d995efb commit 8cf4fc9

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

ibm_cloud_sdk_core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def __read_from_credential_file(service_name: str, *, separator: str = '=') -> d
231231
if credential_file_path is not None:
232232
with open(credential_file_path, 'r') as fobj:
233233
for line in fobj:
234-
key_val = line.strip().split(separator)
234+
key_val = line.strip().split(separator, 1)
235235
if len(key_val) == 2:
236236
key = key_val[0]
237237
value = key_val[1]

resources/ibm-credentials.env

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
IBM_WATSON_APIKEY=5678efgh
22
IBM_WATSON_AUTH_TYPE=iam
33
IBM_WATSON_URL=https://cwdserviceurl
4-
IBM_WATSON_DISABLE_SSL=False
4+
IBM_WATSON_DISABLE_SSL=False
5+
6+
# Service1 auth properties configured with IAM and a token containing '='
7+
SERVICE_1_AUTH_TYPE=iam
8+
SERVICE_1_APIKEY=V4HXmoUtMjohnsnow=KotN
9+
SERVICE_1_CLIENT_ID=somefake========id
10+
SERVICE_1_CLIENT_SECRET===my-client-secret==
11+
SERVICE_1_AUTH_URL=https://iamhost/iam/api=
12+
SERVICE_1_URL=service1.com/api

test/test_utils.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,30 @@ def test_get_authenticator_from_credential_file():
136136
assert authenticator.bearer_token is not None
137137
del os.environ['IBM_CREDENTIALS_FILE']
138138

139+
file_path = os.path.join(
140+
os.path.dirname(__file__), '../resources/ibm-credentials.env')
141+
os.environ['IBM_CREDENTIALS_FILE'] = file_path
142+
authenticator = get_authenticator_from_environment('service_1')
143+
assert authenticator is not None
144+
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
145+
assert authenticator.token_manager.client_id == 'somefake========id'
146+
assert authenticator.token_manager.client_secret == '==my-client-secret=='
147+
assert authenticator.token_manager.url == 'https://iamhost/iam/api='
148+
del os.environ['IBM_CREDENTIALS_FILE']
149+
139150
def test_get_authenticator_from_env_variables():
140151
os.environ['TEST_APIKEY'] = '5678efgh'
141152
authenticator = get_authenticator_from_environment('test')
142153
assert authenticator is not None
143154
assert authenticator.token_manager.apikey == '5678efgh'
144155
del os.environ['TEST_APIKEY']
145156

157+
os.environ['SERVICE_1_APIKEY'] = 'V4HXmoUtMjohnsnow=KotN'
158+
authenticator = get_authenticator_from_environment('service_1')
159+
assert authenticator is not None
160+
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
161+
del os.environ['SERVICE_1_APIKEY']
162+
146163
def test_vcap_credentials():
147164
vcap_services = '{"test":[{"credentials":{ \
148165
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
@@ -205,13 +222,26 @@ def test_vcap_credentials_2():
205222
"credentials":{ \
206223
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
207224
"username":"bogus username", \
208-
"password":"bogus password"}}]}'
225+
"password":"bogus password"}}],\
226+
"equals_sign_test":[{"name": "equals_sign_test",\
227+
"credentials":{ \
228+
"iam_apikey": "V4HXmoUtMjohnsnow=KotN",\
229+
"iam_apikey_description": "Auto generated apikey...",\
230+
"iam_apikey_name": "auto-generated-apikey-111-222-333",\
231+
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",\
232+
"iam_serviceid_crn": "crn:v1:staging:public:iam-identity::a/::serviceid:ServiceID-1234",\
233+
"url": "https://gateway.watsonplatform.net/testService",\
234+
"auth_url": "https://iamhost/iam/api="}}]}'
209235

210236
os.environ['VCAP_SERVICES'] = vcap_services
211237
authenticator = get_authenticator_from_environment('testname')
212238
assert isinstance(authenticator, BasicAuthenticator)
213239
assert authenticator.username == 'bogus username2'
214240
assert authenticator.password == 'bogus password2'
241+
242+
authenticator = get_authenticator_from_environment('equals_sign_test')
243+
assert isinstance(authenticator, IAMAuthenticator)
244+
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
215245
del os.environ['VCAP_SERVICES']
216246

217247
vcap_services = '{"test":[{\

0 commit comments

Comments
 (0)