Skip to content

Commit

Permalink
fix: allow '=' character in environment config values
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ibm committed May 11, 2020
1 parent d995efb commit 8cf4fc9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __read_from_credential_file(service_name: str, *, separator: str = '=') -> d
if credential_file_path is not None:
with open(credential_file_path, 'r') as fobj:
for line in fobj:
key_val = line.strip().split(separator)
key_val = line.strip().split(separator, 1)
if len(key_val) == 2:
key = key_val[0]
value = key_val[1]
Expand Down
10 changes: 9 additions & 1 deletion resources/ibm-credentials.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
IBM_WATSON_APIKEY=5678efgh
IBM_WATSON_AUTH_TYPE=iam
IBM_WATSON_URL=https://cwdserviceurl
IBM_WATSON_DISABLE_SSL=False
IBM_WATSON_DISABLE_SSL=False

# Service1 auth properties configured with IAM and a token containing '='
SERVICE_1_AUTH_TYPE=iam
SERVICE_1_APIKEY=V4HXmoUtMjohnsnow=KotN
SERVICE_1_CLIENT_ID=somefake========id
SERVICE_1_CLIENT_SECRET===my-client-secret==
SERVICE_1_AUTH_URL=https://iamhost/iam/api=
SERVICE_1_URL=service1.com/api
32 changes: 31 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,30 @@ def test_get_authenticator_from_credential_file():
assert authenticator.bearer_token is not None
del os.environ['IBM_CREDENTIALS_FILE']

file_path = os.path.join(
os.path.dirname(__file__), '../resources/ibm-credentials.env')
os.environ['IBM_CREDENTIALS_FILE'] = file_path
authenticator = get_authenticator_from_environment('service_1')
assert authenticator is not None
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
assert authenticator.token_manager.client_id == 'somefake========id'
assert authenticator.token_manager.client_secret == '==my-client-secret=='
assert authenticator.token_manager.url == 'https://iamhost/iam/api='
del os.environ['IBM_CREDENTIALS_FILE']

def test_get_authenticator_from_env_variables():
os.environ['TEST_APIKEY'] = '5678efgh'
authenticator = get_authenticator_from_environment('test')
assert authenticator is not None
assert authenticator.token_manager.apikey == '5678efgh'
del os.environ['TEST_APIKEY']

os.environ['SERVICE_1_APIKEY'] = 'V4HXmoUtMjohnsnow=KotN'
authenticator = get_authenticator_from_environment('service_1')
assert authenticator is not None
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
del os.environ['SERVICE_1_APIKEY']

def test_vcap_credentials():
vcap_services = '{"test":[{"credentials":{ \
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
Expand Down Expand Up @@ -205,13 +222,26 @@ def test_vcap_credentials_2():
"credentials":{ \
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
"username":"bogus username", \
"password":"bogus password"}}]}'
"password":"bogus password"}}],\
"equals_sign_test":[{"name": "equals_sign_test",\
"credentials":{ \
"iam_apikey": "V4HXmoUtMjohnsnow=KotN",\
"iam_apikey_description": "Auto generated apikey...",\
"iam_apikey_name": "auto-generated-apikey-111-222-333",\
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",\
"iam_serviceid_crn": "crn:v1:staging:public:iam-identity::a/::serviceid:ServiceID-1234",\
"url": "https://gateway.watsonplatform.net/testService",\
"auth_url": "https://iamhost/iam/api="}}]}'

os.environ['VCAP_SERVICES'] = vcap_services
authenticator = get_authenticator_from_environment('testname')
assert isinstance(authenticator, BasicAuthenticator)
assert authenticator.username == 'bogus username2'
assert authenticator.password == 'bogus password2'

authenticator = get_authenticator_from_environment('equals_sign_test')
assert isinstance(authenticator, IAMAuthenticator)
assert authenticator.token_manager.apikey == 'V4HXmoUtMjohnsnow=KotN'
del os.environ['VCAP_SERVICES']

vcap_services = '{"test":[{\
Expand Down

0 comments on commit 8cf4fc9

Please sign in to comment.