Skip to content

Commit

Permalink
feat(env): Load from env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdsouza committed Aug 21, 2019
1 parent e67e35a commit 866f279
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import requests
from requests.structures import CaseInsensitiveDict
from .version import __version__
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_external_sources
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_from_env_variables
from .detailed_response import DetailedResponse
from .api_exception import ApiException
from .authenticators import Authenticator
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(self,

if display_name:
service_name = display_name.replace(' ', '_').lower()
config = read_from_external_sources(service_name)
config = read_from_env_variables(service_name)
if config.get('url'):
self.url = config.get('url')
if config.get('disable_ssl'):
Expand Down
39 changes: 28 additions & 11 deletions ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import dateutil.parser as date_parser
from os.path import dirname, isfile, join, expanduser, abspath
from os import getenv
from os import getenv, environ
import json as json_import

def has_bad_first_or_last_char(str):
Expand Down Expand Up @@ -62,19 +62,34 @@ def get_authenticator_from_environment(service_name):
"""
authenticator = None
# 1. Credentials from credential file
config = read_from_external_sources(service_name)
config = read_from_credential_file(service_name)
if config:
authenticator = contruct_authenticator(config)

# 2. Credentials from VCAP
# 3. From env variables
if not authenticator:
config = read_from_vcap_services(service_name)
config = read_from_env_variables(service_name)
if config:
authenticator = contruct_authenticator(config)

# 3. Credentials from VCAP
if not authenticator:
config = read_from_vcap_services(service_name)
if config:
authenticator = contruct_authenticator(config)
return authenticator

def read_from_external_sources(service_name, separator='='):
def read_from_env_variables(service_name):
"""
:return dict config: parsed env variables
"""
service_name = service_name.lower()
config = {}
for key, value in environ.items():
_parse_key_and_update_config(config, service_name.lower(), key.lower(), value)
return config

def read_from_credential_file(service_name, separator='='):
"""
:param str service_name: The service name
:return dict config: parsed key values pairs
Expand Down Expand Up @@ -106,13 +121,15 @@ def read_from_external_sources(service_name, separator='='):
if len(key_val) == 2:
key = key_val[0].lower()
value = key_val[1]
if service_name in key:
index = key.find('_')
if index != -1:
config[key[index + 1:]] = value

_parse_key_and_update_config(config, service_name, key, value)
return config

def _parse_key_and_update_config(config, service_name, key, value):
if service_name in key:
index = key.find('_')
if index != -1:
config[key[index + 1:]] = value

def read_from_vcap_services(service_name):
vcap_services = getenv('VCAP_SERVICES')
vcap_service_credentials = None
Expand Down Expand Up @@ -148,7 +165,7 @@ def contruct_authenticator(config):
password=config.get('password'),
url=config.get('auth_url'),
disable_ssl_verification=config.get('auth_disable_ssl'))
elif auth_type == 'iam':
elif auth_type == 'iam' and config.get('apikey'):
authenticator = IamAuthenticator(
apikey=config.get('apikey'),
url=config.get('auth_url'),
Expand Down
4 changes: 1 addition & 3 deletions resources/ibm-credentials-iam.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
WATSON_APIKEY=5678efgh
WATSON_URL=https://gateway-s.watsonplatform.net/watson/api
WATSON_AUTH_TYPE=iam
WATSON_DISABLE_SSL=False
WATSON_AUTH_TYPE=iam
7 changes: 5 additions & 2 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ def test_fail_http_config():
def test_iam():
iam_authenticator = IamAuthenticator('my_apikey', 'https://iam-test.cloud.ibm.com/identity/token')
file_path = os.path.join(
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
os.environ['IBM_CREDENTIALS_FILE'] = file_path
os.environ['WATSON_URL'] = 'https://gateway-s.watsonplatform.net/watson/api'
os.environ['WATSON_DISABLE_SSL'] = 'False'
service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
assert service.url == 'https://gateway-s.watsonplatform.net/watson/api'
del os.environ['IBM_CREDENTIALS_FILE']
del os.environ['WATSON_URL']
del os.environ['WATSON_DISABLE_SSL']
assert service.authenticator is not None

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


def test_no_auth():
class MadeUp(object):
def __init__(self):
Expand Down
10 changes: 8 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_datetime_conversion():
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338'

def test_get_authenticator_from_environment_from_credential_file():
def test_get_authenticator_from_credential_file():
file_path = os.path.join(
os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
os.environ['IBM_CREDENTIALS_FILE'] = file_path
Expand Down Expand Up @@ -48,6 +48,13 @@ def test_get_authenticator_from_environment_from_credential_file():
assert authenticator.bearer_token is not None
del os.environ['IBM_CREDENTIALS_FILE']

def test_get_authenticator_from_env_variabled():
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']

def test_vcap_credentials():
vcap_services = '{"test":[{"credentials":{ \
"url":"https://gateway.watsonplatform.net/compare-comply/api",\
Expand All @@ -70,4 +77,3 @@ def test_vcap_credentials():
assert authenticator is not None
assert authenticator.token_manager.apikey == 'bogus apikey'
del os.environ['VCAP_SERVICES']

0 comments on commit 866f279

Please sign in to comment.