Skip to content

Commit 866f279

Browse files
committed
feat(env): Load from env variables
1 parent e67e35a commit 866f279

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import requests
2222
from requests.structures import CaseInsensitiveDict
2323
from .version import __version__
24-
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_external_sources
24+
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_env_variables
2525
from .detailed_response import DetailedResponse
2626
from .api_exception import ApiException
2727
from .authenticators import Authenticator
@@ -63,7 +63,7 @@ def __init__(self,
6363

6464
if display_name:
6565
service_name = display_name.replace(' ', '_').lower()
66-
config = read_from_external_sources(service_name)
66+
config = read_from_env_variables(service_name)
6767
if config.get('url'):
6868
self.url = config.get('url')
6969
if config.get('disable_ssl'):

ibm_cloud_sdk_core/utils.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import dateutil.parser as date_parser
1818
from os.path import dirname, isfile, join, expanduser, abspath
19-
from os import getenv
19+
from os import getenv, environ
2020
import json as json_import
2121

2222
def has_bad_first_or_last_char(str):
@@ -62,19 +62,34 @@ def get_authenticator_from_environment(service_name):
6262
"""
6363
authenticator = None
6464
# 1. Credentials from credential file
65-
config = read_from_external_sources(service_name)
65+
config = read_from_credential_file(service_name)
6666
if config:
6767
authenticator = contruct_authenticator(config)
6868

69-
# 2. Credentials from VCAP
69+
# 3. From env variables
7070
if not authenticator:
71-
config = read_from_vcap_services(service_name)
71+
config = read_from_env_variables(service_name)
7272
if config:
7373
authenticator = contruct_authenticator(config)
7474

75+
# 3. Credentials from VCAP
76+
if not authenticator:
77+
config = read_from_vcap_services(service_name)
78+
if config:
79+
authenticator = contruct_authenticator(config)
7580
return authenticator
7681

77-
def read_from_external_sources(service_name, separator='='):
82+
def read_from_env_variables(service_name):
83+
"""
84+
:return dict config: parsed env variables
85+
"""
86+
service_name = service_name.lower()
87+
config = {}
88+
for key, value in environ.items():
89+
_parse_key_and_update_config(config, service_name.lower(), key.lower(), value)
90+
return config
91+
92+
def read_from_credential_file(service_name, separator='='):
7893
"""
7994
:param str service_name: The service name
8095
:return dict config: parsed key values pairs
@@ -106,13 +121,15 @@ def read_from_external_sources(service_name, separator='='):
106121
if len(key_val) == 2:
107122
key = key_val[0].lower()
108123
value = key_val[1]
109-
if service_name in key:
110-
index = key.find('_')
111-
if index != -1:
112-
config[key[index + 1:]] = value
113-
124+
_parse_key_and_update_config(config, service_name, key, value)
114125
return config
115126

127+
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
132+
116133
def read_from_vcap_services(service_name):
117134
vcap_services = getenv('VCAP_SERVICES')
118135
vcap_service_credentials = None
@@ -148,7 +165,7 @@ def contruct_authenticator(config):
148165
password=config.get('password'),
149166
url=config.get('auth_url'),
150167
disable_ssl_verification=config.get('auth_disable_ssl'))
151-
elif auth_type == 'iam':
168+
elif auth_type == 'iam' and config.get('apikey'):
152169
authenticator = IamAuthenticator(
153170
apikey=config.get('apikey'),
154171
url=config.get('auth_url'),

resources/ibm-credentials-iam.env

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
WATSON_APIKEY=5678efgh
2-
WATSON_URL=https://gateway-s.watsonplatform.net/watson/api
3-
WATSON_AUTH_TYPE=iam
4-
WATSON_DISABLE_SSL=False
2+
WATSON_AUTH_TYPE=iam

test/test_base_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,15 @@ def test_fail_http_config():
137137
def test_iam():
138138
iam_authenticator = IamAuthenticator('my_apikey', 'https://iam-test.cloud.ibm.com/identity/token')
139139
file_path = os.path.join(
140-
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
140+
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
141141
os.environ['IBM_CREDENTIALS_FILE'] = file_path
142+
os.environ['WATSON_URL'] = 'https://gateway-s.watsonplatform.net/watson/api'
143+
os.environ['WATSON_DISABLE_SSL'] = 'False'
142144
service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
143145
assert service.url == 'https://gateway-s.watsonplatform.net/watson/api'
144146
del os.environ['IBM_CREDENTIALS_FILE']
147+
del os.environ['WATSON_URL']
148+
del os.environ['WATSON_DISABLE_SSL']
145149
assert service.authenticator is not None
146150

147151
response = {
@@ -166,7 +170,6 @@ def test_iam():
166170
service.any_service_call()
167171
assert "grant-type%3Aapikey" in responses.calls[0].request.body
168172

169-
170173
def test_no_auth():
171174
class MadeUp(object):
172175
def __init__(self):

test/test_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_datetime_conversion():
77
res = datetime_to_string(date)
88
assert res == '2017-03-06T16:00:04.159338'
99

10-
def test_get_authenticator_from_environment_from_credential_file():
10+
def test_get_authenticator_from_credential_file():
1111
file_path = os.path.join(
1212
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
1313
os.environ['IBM_CREDENTIALS_FILE'] = file_path
@@ -48,6 +48,13 @@ def test_get_authenticator_from_environment_from_credential_file():
4848
assert authenticator.bearer_token is not None
4949
del os.environ['IBM_CREDENTIALS_FILE']
5050

51+
def test_get_authenticator_from_env_variabled():
52+
os.environ['TEST_APIKEY'] = '5678efgh'
53+
authenticator = get_authenticator_from_environment('test')
54+
assert authenticator is not None
55+
assert authenticator.token_manager.apikey == '5678efgh'
56+
del os.environ['TEST_APIKEY']
57+
5158
def test_vcap_credentials():
5259
vcap_services = '{"test":[{"credentials":{ \
5360
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
@@ -70,4 +77,3 @@ def test_vcap_credentials():
7077
assert authenticator is not None
7178
assert authenticator.token_manager.apikey == 'bogus apikey'
7279
del os.environ['VCAP_SERVICES']
73-

0 commit comments

Comments
 (0)