diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 9722e55..b94deae 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -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] diff --git a/resources/ibm-credentials.env b/resources/ibm-credentials.env index 327a89c..901504f 100644 --- a/resources/ibm-credentials.env +++ b/resources/ibm-credentials.env @@ -1,4 +1,12 @@ IBM_WATSON_APIKEY=5678efgh IBM_WATSON_AUTH_TYPE=iam IBM_WATSON_URL=https://cwdserviceurl -IBM_WATSON_DISABLE_SSL=False \ No newline at end of file +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 \ No newline at end of file diff --git a/test/test_utils.py b/test/test_utils.py index afc1fc1..acb8b51 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -136,6 +136,17 @@ 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') @@ -143,6 +154,12 @@ def test_get_authenticator_from_env_variables(): 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",\ @@ -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":[{\