-
Notifications
You must be signed in to change notification settings - Fork 29
Change order of credential path and read_external_sources and more #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8fee5bf
feat(base): Add method read_external_sources
ehdsouza 5c3d545
fix(credential): Check for credential file in working dir before home…
ehdsouza 01912cd
chore(replace): service name replace - to _
ehdsouza fe36514
chore(doc): Update comment
ehdsouza 50fad59
fix(config): Handle multi word service name
ehdsouza 4148034
fix(client props): Setting of the external client config happens in i…
ehdsouza 7981d92
chore(service_url): Make service url as None
ehdsouza 41a3a51
test(service name): Mullti word service name
ehdsouza d21243c
Update version.py
ehdsouza a3b5305
chore(external_sources): Export externall sources
ehdsouza 09fa9b5
Merge branch 'order' of github.com:IBM/python-sdk-core into order
ehdsouza b7b3723
chore(index): remove additional index checking
ehdsouza c4ba31d
chore(key): can't replace key but only convert to lower
ehdsouza 4742b8e
fix(displlay_name): display_name unused in constructor
ehdsouza 164d02e
chore(upper): Use upper() instead of lower()
ehdsouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,62 +54,71 @@ def string_to_datetime(string): | |
""" | ||
return date_parser.parse(string) | ||
|
||
def get_authenticator_from_environment(service_name): | ||
def read_external_sources(service_name): | ||
""" | ||
Checks the credentials file and VCAP_SERVICES environment variable | ||
Try to get config from external sources, with the following priority: | ||
1. Credentials file(ibm-credentials.env) | ||
2. Environment variables | ||
3. VCAP Services(Cloud Foundry) | ||
:param service_name: The service name | ||
:return: the authenticator | ||
:return: dict | ||
""" | ||
authenticator = None | ||
# 1. Credentials from credential file | ||
config = {} | ||
|
||
config = read_from_credential_file(service_name) | ||
if config: | ||
authenticator = contruct_authenticator(config) | ||
|
||
# 2. From env variables | ||
if not authenticator: | ||
if not config: | ||
config = read_from_env_variables(service_name) | ||
if config: | ||
authenticator = contruct_authenticator(config) | ||
|
||
# 3. Credentials from VCAP | ||
if not authenticator: | ||
if not config: | ||
config = read_from_vcap_services(service_name) | ||
if config: | ||
authenticator = contruct_authenticator(config) | ||
|
||
return config | ||
|
||
def get_authenticator_from_environment(service_name): | ||
""" | ||
Try to get authenticator from external sources, with the following priority: | ||
1. Credentials file(ibm-credentials.env) | ||
2. Environment variables | ||
3. VCAP Services(Cloud Foundry) | ||
:param service_name: The service name | ||
:return: the authenticator | ||
""" | ||
authenticator = None | ||
config = read_external_sources(service_name) | ||
if config: | ||
authenticator = _construct_authenticator(config) | ||
return authenticator | ||
|
||
def read_from_env_variables(service_name): | ||
""" | ||
:return dict config: parsed env variables | ||
""" | ||
service_name = service_name.replace(' ', '_').lower() | ||
config = {} | ||
for key, value in environ.items(): | ||
_parse_key_and_update_config(config, service_name.lower(), key.lower(), value) | ||
_parse_key_and_update_config(config, service_name, key, 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 | ||
""" | ||
service_name = service_name.replace(' ', '_').lower() | ||
DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env' | ||
|
||
# File path specified by an env variable | ||
credential_file_path = getenv('IBM_CREDENTIALS_FILE') | ||
|
||
# Home directory | ||
# Current working directory | ||
if credential_file_path is None: | ||
file_path = join(expanduser('~'), DEFAULT_CREDENTIALS_FILE_NAME) | ||
file_path = join( | ||
dirname(dirname(abspath(__file__))), DEFAULT_CREDENTIALS_FILE_NAME) | ||
if isfile(file_path): | ||
credential_file_path = file_path | ||
|
||
# Top-level of the project directory | ||
# Home directory | ||
padamstx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if credential_file_path is None: | ||
file_path = join( | ||
dirname(dirname(abspath(__file__))), DEFAULT_CREDENTIALS_FILE_NAME) | ||
file_path = join(expanduser('~'), DEFAULT_CREDENTIALS_FILE_NAME) | ||
if isfile(file_path): | ||
credential_file_path = file_path | ||
|
||
|
@@ -119,62 +128,62 @@ def read_from_credential_file(service_name, separator='='): | |
for line in fp: | ||
key_val = line.strip().split(separator) | ||
if len(key_val) == 2: | ||
key = key_val[0].lower() | ||
key = key_val[0] | ||
value = key_val[1] | ||
_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 | ||
service_name = service_name.replace(' ', '_').replace('-', '_').upper() | ||
if key.startswith(service_name): | ||
config[key[len(service_name) + 1:]] = value | ||
|
||
def read_from_vcap_services(service_name): | ||
service_name = service_name.replace(' ', '_').lower() | ||
vcap_services = getenv('VCAP_SERVICES') | ||
vcap_service_credentials = None | ||
vcap_service_credentials = {} | ||
if vcap_services: | ||
services = json_import.loads(vcap_services) | ||
|
||
for key in services.keys(): | ||
name = key.replace('-', '_') | ||
if name == service_name: | ||
if key == service_name: | ||
vcap_service_credentials = services[key][0]['credentials'] | ||
if vcap_service_credentials is not None and isinstance(vcap_service_credentials, dict): | ||
if vcap_service_credentials.get('username') and vcap_service_credentials.get('password'): # cf | ||
vcap_service_credentials['auth_type'] = 'basic' | ||
vcap_service_credentials['AUTH_TYPE'] = 'basic' | ||
vcap_service_credentials['USERNAME'] = vcap_service_credentials.get('username') | ||
vcap_service_credentials['PASSWORD'] = vcap_service_credentials.get('password') | ||
elif vcap_service_credentials.get('apikey'): # rc | ||
vcap_service_credentials['auth_type'] = 'iam' | ||
vcap_service_credentials['AUTH_TYPE'] = 'iam' | ||
vcap_service_credentials['APIKEY'] = vcap_service_credentials.get('apikey') | ||
else: # no other auth mechanism is supported | ||
vcap_service_credentials = None | ||
vcap_service_credentials = {} | ||
return vcap_service_credentials | ||
|
||
def contruct_authenticator(config): | ||
auth_type = config.get('auth_type').lower() if config.get('auth_type') else 'iam' | ||
def _construct_authenticator(config): | ||
auth_type = config.get('AUTH_TYPE').lower() if config.get('AUTH_TYPE') else 'iam' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't comment on this before, and I don't think we need to change anything right now, but... when we implement the service config feature, it might be good to define constants for the various config properties rather than use the literal strings like this. |
||
authenticator = None | ||
from .authenticators import BasicAuthenticator, BearerTokenAuthenticator, CloudPakForDataAuthenticator, IAMAuthenticator, NoAuthAuthenticator | ||
|
||
if auth_type == 'basic': | ||
authenticator = BasicAuthenticator( | ||
username=config.get('username'), | ||
password=config.get('password')) | ||
username=config.get('USERNAME'), | ||
password=config.get('PASSWORD')) | ||
elif auth_type == 'bearertoken': | ||
authenticator = BearerTokenAuthenticator( | ||
bearer_token=config.get('bearer_token')) | ||
bearer_token=config.get('BEARER_TOKEN')) | ||
elif auth_type == 'cp4d': | ||
authenticator = CloudPakForDataAuthenticator( | ||
username=config.get('username'), | ||
password=config.get('password'), | ||
url=config.get('auth_url'), | ||
disable_ssl_verification=config.get('auth_disable_ssl')) | ||
elif auth_type == 'iam' and config.get('apikey'): | ||
username=config.get('USERNAME'), | ||
password=config.get('PASSWORD'), | ||
url=config.get('AUTH_URL'), | ||
disable_ssl_verification=config.get('AUTH_DISABLE_SSL')) | ||
elif auth_type == 'iam' and config.get('APIKEY'): | ||
authenticator = IAMAuthenticator( | ||
apikey=config.get('apikey'), | ||
url=config.get('auth_url'), | ||
client_id=config.get('client_id'), | ||
client_secret=config.get('client_secret'), | ||
disable_ssl_verification=config.get('auth_disable_ssl')) | ||
apikey=config.get('APIKEY'), | ||
url=config.get('AUTH_URL'), | ||
client_id=config.get('CLIENT_ID'), | ||
client_secret=config.get('CLIENT_SECRET'), | ||
disable_ssl_verification=config.get('AUTH_DISABLE_SSL')) | ||
elif auth_type == 'noauth': | ||
authenticator = NoAuthAuthenticator() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
WATSON_APIKEY=5678efgh | ||
WATSON_AUTH_TYPE=iam | ||
IBM_WATSON_APIKEY=5678efgh | ||
IBM_WATSON_AUTH_TYPE=iam | ||
IBM_WATSON_URL=https://gateway-s.watsonplatform.net/watson/api | ||
IBM_WATSON_DISABLE_SSL=False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting of client properties go in the generated service classes