@@ -54,62 +54,71 @@ def string_to_datetime(string):
54
54
"""
55
55
return date_parser .parse (string )
56
56
57
- def get_authenticator_from_environment (service_name ):
57
+ def read_external_sources (service_name ):
58
58
"""
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)
60
63
:param service_name: The service name
61
- :return: the authenticator
64
+ :return: dict
62
65
"""
63
- authenticator = None
64
- # 1. Credentials from credential file
66
+ config = {}
67
+
65
68
config = read_from_credential_file (service_name )
66
- if config :
67
- authenticator = contruct_authenticator (config )
68
69
69
- # 2. From env variables
70
- if not authenticator :
70
+ if not config :
71
71
config = read_from_env_variables (service_name )
72
- if config :
73
- authenticator = contruct_authenticator (config )
74
72
75
- # 3. Credentials from VCAP
76
- if not authenticator :
73
+ if not config :
77
74
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 = _construct_authenticator (config )
80
91
return authenticator
81
92
82
93
def read_from_env_variables (service_name ):
83
94
"""
84
95
:return dict config: parsed env variables
85
96
"""
86
- service_name = service_name .replace (' ' , '_' ).lower ()
87
97
config = {}
88
98
for key , value in environ .items ():
89
- _parse_key_and_update_config (config , service_name . lower () , key . lower () , value )
99
+ _parse_key_and_update_config (config , service_name , key , value )
90
100
return config
91
101
92
102
def read_from_credential_file (service_name , separator = '=' ):
93
103
"""
94
104
:param str service_name: The service name
95
105
:return dict config: parsed key values pairs
96
106
"""
97
- service_name = service_name .replace (' ' , '_' ).lower ()
98
107
DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env'
99
108
100
109
# File path specified by an env variable
101
110
credential_file_path = getenv ('IBM_CREDENTIALS_FILE' )
102
111
103
- # Home directory
112
+ # Current working directory
104
113
if credential_file_path is None :
105
- file_path = join (expanduser ('~' ), DEFAULT_CREDENTIALS_FILE_NAME )
114
+ file_path = join (
115
+ dirname (dirname (abspath (__file__ ))), DEFAULT_CREDENTIALS_FILE_NAME )
106
116
if isfile (file_path ):
107
117
credential_file_path = file_path
108
118
109
- # Top-level of the project directory
119
+ # Home directory
110
120
if credential_file_path is None :
111
- file_path = join (
112
- dirname (dirname (abspath (__file__ ))), DEFAULT_CREDENTIALS_FILE_NAME )
121
+ file_path = join (expanduser ('~' ), DEFAULT_CREDENTIALS_FILE_NAME )
113
122
if isfile (file_path ):
114
123
credential_file_path = file_path
115
124
@@ -119,62 +128,62 @@ def read_from_credential_file(service_name, separator='='):
119
128
for line in fp :
120
129
key_val = line .strip ().split (separator )
121
130
if len (key_val ) == 2 :
122
- key = key_val [0 ]. lower ()
131
+ key = key_val [0 ]
123
132
value = key_val [1 ]
124
133
_parse_key_and_update_config (config , service_name , key , value )
125
134
return config
126
135
127
136
def _parse_key_and_update_config (config , service_name , key , value ):
128
- if service_name in key :
129
- index = key .find ('_' )
130
- if index != - 1 :
131
- config [key [index + 1 :]] = value
137
+ service_name = service_name .replace (' ' , '_' ).replace ('-' , '_' ).upper ()
138
+ if key .startswith (service_name ):
139
+ config [key [len (service_name ) + 1 :]] = value
132
140
133
141
def read_from_vcap_services (service_name ):
134
- service_name = service_name .replace (' ' , '_' ).lower ()
135
142
vcap_services = getenv ('VCAP_SERVICES' )
136
- vcap_service_credentials = None
143
+ vcap_service_credentials = {}
137
144
if vcap_services :
138
145
services = json_import .loads (vcap_services )
139
146
140
147
for key in services .keys ():
141
- name = key .replace ('-' , '_' )
142
- if name == service_name :
148
+ if key == service_name :
143
149
vcap_service_credentials = services [key ][0 ]['credentials' ]
144
150
if vcap_service_credentials is not None and isinstance (vcap_service_credentials , dict ):
145
151
if vcap_service_credentials .get ('username' ) and vcap_service_credentials .get ('password' ): # cf
146
- vcap_service_credentials ['auth_type' ] = 'basic'
152
+ vcap_service_credentials ['AUTH_TYPE' ] = 'basic'
153
+ vcap_service_credentials ['USERNAME' ] = vcap_service_credentials .get ('username' )
154
+ vcap_service_credentials ['PASSWORD' ] = vcap_service_credentials .get ('password' )
147
155
elif vcap_service_credentials .get ('apikey' ): # rc
148
- vcap_service_credentials ['auth_type' ] = 'iam'
156
+ vcap_service_credentials ['AUTH_TYPE' ] = 'iam'
157
+ vcap_service_credentials ['APIKEY' ] = vcap_service_credentials .get ('apikey' )
149
158
else : # no other auth mechanism is supported
150
- vcap_service_credentials = None
159
+ vcap_service_credentials = {}
151
160
return vcap_service_credentials
152
161
153
- def contruct_authenticator (config ):
154
- auth_type = config .get ('auth_type ' ).lower () if config .get ('auth_type ' ) else 'iam'
162
+ def _construct_authenticator (config ):
163
+ auth_type = config .get ('AUTH_TYPE ' ).lower () if config .get ('AUTH_TYPE ' ) else 'iam'
155
164
authenticator = None
156
165
from .authenticators import BasicAuthenticator , BearerTokenAuthenticator , CloudPakForDataAuthenticator , IAMAuthenticator , NoAuthAuthenticator
157
166
158
167
if auth_type == 'basic' :
159
168
authenticator = BasicAuthenticator (
160
- username = config .get ('username ' ),
161
- password = config .get ('password ' ))
169
+ username = config .get ('USERNAME ' ),
170
+ password = config .get ('PASSWORD ' ))
162
171
elif auth_type == 'bearertoken' :
163
172
authenticator = BearerTokenAuthenticator (
164
- bearer_token = config .get ('bearer_token ' ))
173
+ bearer_token = config .get ('BEARER_TOKEN ' ))
165
174
elif auth_type == 'cp4d' :
166
175
authenticator = CloudPakForDataAuthenticator (
167
- username = config .get ('username ' ),
168
- password = config .get ('password ' ),
169
- url = config .get ('auth_url ' ),
170
- disable_ssl_verification = config .get ('auth_disable_ssl ' ))
171
- elif auth_type == 'iam' and config .get ('apikey ' ):
176
+ username = config .get ('USERNAME ' ),
177
+ password = config .get ('PASSWORD ' ),
178
+ url = config .get ('AUTH_URL ' ),
179
+ disable_ssl_verification = config .get ('AUTH_DISABLE_SSL ' ))
180
+ elif auth_type == 'iam' and config .get ('APIKEY ' ):
172
181
authenticator = IAMAuthenticator (
173
- apikey = config .get ('apikey ' ),
174
- url = config .get ('auth_url ' ),
175
- client_id = config .get ('client_id ' ),
176
- client_secret = config .get ('client_secret ' ),
177
- disable_ssl_verification = config .get ('auth_disable_ssl ' ))
182
+ apikey = config .get ('APIKEY ' ),
183
+ url = config .get ('AUTH_URL ' ),
184
+ client_id = config .get ('CLIENT_ID ' ),
185
+ client_secret = config .get ('CLIENT_SECRET ' ),
186
+ disable_ssl_verification = config .get ('AUTH_DISABLE_SSL ' ))
178
187
elif auth_type == 'noauth' :
179
188
authenticator = NoAuthAuthenticator ()
180
189
0 commit comments