Skip to content

Commit 22e1d7b

Browse files
authored
Merge pull request #635 from watson-developer-cloud/load-credentials
Instantiate credentials using `ibm-credentials.env`
2 parents a25669e + 6337f05 commit 22e1d7b

18 files changed

+158
-20
lines changed

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,45 @@ Watson services are migrating to token-based Identity and Access Management (IAM
9090
### Getting credentials
9191
To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
9292

93-
1. Go to the IBM Cloud [Dashboard](https://console.bluemix.net/dashboard/apps?category=ai) page.
94-
1. Either click an existing Watson service instance or click [**Create resource > AI**](https://console.bluemix.net/catalog/?category=ai) and create a service instance.
95-
1. Copy the `url` and either `apikey` or `username` and `password`. Click **Show** if the credentials are masked.
93+
1. Go to the IBM Cloud [Dashboard](https://cloud.ibm.com/) page.
94+
1. Either click an existing Watson service instance in your [resource list](https://cloud.ibm.com/resources) or click [**Create resource > AI**](https://cloud.ibm.com/catalog?category=ai) and create a service instance.
95+
1. Click on the **Manage** item in the left nav bar of your service instance.
96+
97+
On this page, you should be able to see your credentials for accessing your service instance.
98+
99+
### Supplying credentials
100+
101+
There are two ways to supply the credentials you found above to the SDK for authentication.
102+
103+
#### Credential file (easier!)
104+
105+
With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing and authenticating. You can get this file by clicking the **Download** button for the credentials in the **Manage** tab of your service instance.
106+
107+
The file downloaded will be called `ibm-credentials.env`. This is the name the SDK will search for and **must** be preserved unless you want to configure the file path (more on that later). The SDK will look for your `ibm-credentials.env` file in the following places (in order):
108+
109+
- Your system's home directory
110+
- The top-level directory of the project you're using the SDK in
111+
112+
As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following:
113+
114+
```python
115+
discovery = DiscoveryV1(version='2018-08-01')
116+
```
117+
118+
And that's it!
119+
120+
If you're using more than one service at a time in your code and get two different `ibm-credentials.env` files, just put the contents together in one `ibm-credentials.env` file and the SDK will handle assigning credentials to their appropriate services.
121+
122+
If you would like to configure the location/name of your credential file, you can set an environment variable called `IBM_CREDENTIALS_FILE`. **This will take precedence over the locations specified above.** Here's how you can do that:
123+
124+
```bash
125+
export IBM_CREDENTIALS_FILE="<path>"
126+
```
127+
128+
where `<path>` is something like `/home/user/Downloads/<file_name>.env`.
129+
130+
#### Manually
131+
If you'd prefer to set authentication values manually in your code, the SDK supports that as well. The way you'll do this depends on what type of credentials your service instance gives you.
96132

97133
### IAM
98134

resources/ibm-credentials.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VISUAL_RECOGNITION_APIKEY=1234abcd
2+
VISUAL_RECOGNITION_URL=https://stgwat-us-south-mzr-cruiser6.us-south.containers.mybluemix.net/visual-recognition/api
3+
WATSON_APIKEY=5678efgh
4+
WATSON_URL=https://gateway-s.watsonplatform.net/watson/api

test/unit/test_watson_service.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from watson_developer_cloud import WatsonService
55
import time
6+
import os
67

78
import responses
89

@@ -28,7 +29,8 @@ def __init__(self, version, url=default_url, username=None, password=None,
2829
use_vcap_services=True,
2930
iam_apikey=iam_apikey,
3031
iam_access_token=iam_access_token,
31-
iam_url=iam_url)
32+
iam_url=iam_url,
33+
display_name='Watson')
3234
self.version = version
3335

3436
def op_with_path_params(self, path0, path1):
@@ -217,3 +219,14 @@ def test_has_bad_first_or_last_char():
217219
with pytest.raises(ValueError) as err:
218220
AnyServiceV1('2018-11-20', iam_apikey='apikey', url='"url"')
219221
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'
222+
223+
@responses.activate
224+
def test__set_credential_based_on_type():
225+
file_path = os.path.join(os.path.dirname(__file__), '../../resources/ibm-credentials.env')
226+
os.environ['IBM_CREDENTIALS_FILE'] = file_path
227+
service = AnyServiceV1('2018-11-20')
228+
assert service.iam_apikey == '5678efgh'
229+
del os.environ['IBM_CREDENTIALS_FILE']
230+
231+
service = AnyServiceV1('2018-11-20', username='test', password='test')
232+
assert service.username == 'test'

watson_developer_cloud/assistant_v1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def __init__(
9797
iam_apikey=iam_apikey,
9898
iam_access_token=iam_access_token,
9999
iam_url=iam_url,
100-
use_vcap_services=True)
100+
use_vcap_services=True,
101+
display_name='Assistant')
101102
self.version = version
102103

103104
#########################

watson_developer_cloud/assistant_v2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def __init__(
9696
iam_apikey=iam_apikey,
9797
iam_access_token=iam_access_token,
9898
iam_url=iam_url,
99-
use_vcap_services=True)
99+
use_vcap_services=True,
100+
display_name='Assistant')
100101
self.version = version
101102

102103
#########################

watson_developer_cloud/authorization_v1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AuthorizationV1(WatsonService):
3838
def __init__(self, url=default_url,
3939
username=None, password=None, use_vcap_services=True):
4040
WatsonService.__init__(
41-
self, 'authorization', url, username, password, use_vcap_services)
41+
self, 'authorization', url, username, password, use_vcap_services, display_name='authorization')
4242

4343
def get_token(self, url):
4444
"""

watson_developer_cloud/compare_comply_v1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def __init__(
8181
iam_apikey=iam_apikey,
8282
iam_access_token=iam_access_token,
8383
iam_url=iam_url,
84-
use_vcap_services=True)
84+
use_vcap_services=True,
85+
display_name='Compare Comply')
8586
self.version = version
8687

8788
#########################

watson_developer_cloud/discovery_v1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def __init__(
100100
iam_apikey=iam_apikey,
101101
iam_access_token=iam_access_token,
102102
iam_url=iam_url,
103-
use_vcap_services=True)
103+
use_vcap_services=True,
104+
display_name='Discovery')
104105
self.version = version
105106

106107
#########################

watson_developer_cloud/iam_token_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def set_iam_apikey(self, iam_apikey):
111111
"""
112112
self.iam_apikey = iam_apikey
113113

114+
def set_iam_url(self, iam_url):
115+
"""
116+
Set the IAM url
117+
"""
118+
self.iam_url = iam_url
119+
114120
def _is_token_expired(self):
115121
"""
116122
Check if currently stored token is expired.

watson_developer_cloud/language_translator_v3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def __init__(
9999
iam_apikey=iam_apikey,
100100
iam_access_token=iam_access_token,
101101
iam_url=iam_url,
102-
use_vcap_services=True)
102+
use_vcap_services=True,
103+
display_name='Language Translator')
103104
self.version = version
104105

105106
#########################

0 commit comments

Comments
 (0)