Skip to content

Commit ad594ec

Browse files
authored
Merge pull request #621 from watson-developer-cloud/url-creds
chore(all services): Check for incorrect url and credentials
2 parents 43183da + d8e32c0 commit ad594ec

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

test/unit/test_watson_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,16 @@ def test_response_with_no_body():
204204
assert response is not None
205205
assert len(responses.calls) == 1
206206
assert response.get_result() is None
207+
208+
def test_has_bad_first_or_last_char():
209+
with pytest.raises(ValueError) as err:
210+
AnyServiceV1('2018-11-20', username='{username}', password='password')
211+
assert str(err.value) == 'The username shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username'
212+
213+
with pytest.raises(ValueError) as err:
214+
AnyServiceV1('2018-11-20', iam_apikey='{apikey}')
215+
assert str(err.value) == 'The credentials shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your credentials'
216+
217+
with pytest.raises(ValueError) as err:
218+
AnyServiceV1('2018-11-20', iam_apikey='apikey', url='"url"')
219+
assert str(err.value) == 'The URL shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your URL'

watson_developer_cloud/watson_service.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def _convert_boolean_values(dictionary):
147147
[(k, _convert_boolean_value(v)) for k, v in dictionary.items()])
148148
return dictionary
149149

150+
def _has_bad_first_or_last_char(str):
151+
return str is not None and (str.startswith('{') or str.startswith('"') or str.endswith('}') or str.endswith('"'))
152+
150153
def get_error_message(response):
151154
"""
152155
Gets the error message from a JSON response.
@@ -236,6 +239,10 @@ def __init__(self, vcap_services_name, url, username=None, password=None,
236239
self.token_manager = None
237240
self.verify = None # Indicates whether to ignore verifying the SSL certification
238241

242+
if _has_bad_first_or_last_char(self.url):
243+
raise ValueError('The URL shouldn\'t start or end with curly brackets or quotes. '
244+
'Be sure to remove any {} and \" characters surrounding your URL')
245+
239246
user_agent_string = 'watson-apis-python-sdk-' + __version__ # SDK version
240247
user_agent_string += ' ' + platform.system() # OS
241248
user_agent_string += ' ' + platform.release() # OS version
@@ -281,6 +288,13 @@ def set_username_and_password(self, username=None, password=None):
281288
if password == 'YOUR SERVICE PASSWORD':
282289
password = None
283290

291+
if _has_bad_first_or_last_char(username):
292+
raise ValueError('The username shouldn\'t start or end with curly brackets or quotes. '
293+
'Be sure to remove any {} and \" characters surrounding your username')
294+
if _has_bad_first_or_last_char(password):
295+
raise ValueError('The password shouldn\'t start or end with curly brackets or quotes. '
296+
'Be sure to remove any {} and \" characters surrounding your password')
297+
284298
self.username = username
285299
self.password = password
286300
self.jar = CookieJar()
@@ -304,7 +318,9 @@ def set_api_key(self, api_key):
304318
def set_token_manager(self, iam_apikey=None, iam_access_token=None, iam_url=None):
305319
if iam_apikey == 'YOUR IAM API KEY':
306320
return
307-
321+
if _has_bad_first_or_last_char(iam_apikey):
322+
raise ValueError('The credentials shouldn\'t start or end with curly brackets or quotes. '
323+
'Be sure to remove any {} and \" characters surrounding your credentials')
308324
self.iam_apikey = iam_apikey
309325
self.iam_access_token = iam_access_token
310326
self.iam_url = iam_url
@@ -320,6 +336,9 @@ def set_iam_access_token(self, iam_access_token):
320336
self.jar = CookieJar()
321337

322338
def set_iam_apikey(self, iam_apikey):
339+
if _has_bad_first_or_last_char(iam_apikey):
340+
raise ValueError('The credentials shouldn\'t start or end with curly brackets or quotes. '
341+
'Be sure to remove any {} and \" characters surrounding your credentials')
323342
if self.token_manager:
324343
self.token_manager.set_iam_apikey(iam_apikey)
325344
else:
@@ -328,6 +347,9 @@ def set_iam_apikey(self, iam_apikey):
328347
self.jar = CookieJar()
329348

330349
def set_url(self, url):
350+
if _has_bad_first_or_last_char(url):
351+
raise ValueError('The URL shouldn\'t start or end with curly brackets or quotes. '
352+
'Be sure to remove any {} and \" characters surrounding your URL')
331353
self.url = url
332354

333355
def set_default_headers(self, headers):

0 commit comments

Comments
 (0)