@@ -54,29 +54,40 @@ def string_to_datetime(string):
5454 """
5555 return date_parser .parse (string )
5656
57- def get_authenticator_from_environment (service_name ):
57+ def read_external_sources (service_name ):
5858 """
59- Checks the credentials file and VCAP_SERVICES environment variable
59+ Try to get config from external sources, with the following priority:
60+ 1. Credentials file(ibm-credentials.env)
61+ 2. Environment variables
62+ 3. VCAP Services(Cloud Foundry)
6063 :param service_name: The service name
61- :return: the authenticator
64+ :return: dict
6265 """
63- authenticator = None
64- # 1. Credentials from credential file
66+ config = {}
67+
6568 config = read_from_credential_file (service_name )
66- if config :
67- authenticator = contruct_authenticator (config )
6869
69- # 2. From env variables
70- if not authenticator :
70+ if not config :
7171 config = read_from_env_variables (service_name )
72- if config :
73- authenticator = contruct_authenticator (config )
7472
75- # 3. Credentials from VCAP
76- if not authenticator :
73+ if not config :
7774 config = read_from_vcap_services (service_name )
78- if config :
79- authenticator = contruct_authenticator (config )
75+
76+ return config
77+
78+ def get_authenticator_from_environment (service_name ):
79+ """
80+ Try to get authenticator from external sources, with the following priority:
81+ 1. Credentials file(ibm-credentials.env)
82+ 2. Environment variables
83+ 3. VCAP Services(Cloud Foundry)
84+ :param service_name: The service name
85+ :return: the authenticator
86+ """
87+ authenticator = None
88+ config = read_external_sources (service_name )
89+ if config :
90+ authenticator = contruct_authenticator (config )
8091 return authenticator
8192
8293def read_from_env_variables (service_name ):
@@ -125,15 +136,15 @@ def read_from_credential_file(service_name, separator='='):
125136 return config
126137
127138def _parse_key_and_update_config (config , service_name , key , value ):
128- if service_name in key :
139+ if key . startswith ( service_name ) :
129140 index = key .find ('_' )
130141 if index != - 1 :
131142 config [key [index + 1 :]] = value
132143
133144def read_from_vcap_services (service_name ):
134145 service_name = service_name .replace (' ' , '_' ).lower ()
135146 vcap_services = getenv ('VCAP_SERVICES' )
136- vcap_service_credentials = None
147+ vcap_service_credentials = {}
137148 if vcap_services :
138149 services = json_import .loads (vcap_services )
139150
@@ -147,7 +158,7 @@ def read_from_vcap_services(service_name):
147158 elif vcap_service_credentials .get ('apikey' ): # rc
148159 vcap_service_credentials ['auth_type' ] = 'iam'
149160 else : # no other auth mechanism is supported
150- vcap_service_credentials = None
161+ vcap_service_credentials = {}
151162 return vcap_service_credentials
152163
153164def contruct_authenticator (config ):
0 commit comments