Skip to content

release v1.0.0 #34

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
merged 65 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
b878a49
feat(authentication): Revanp to new authentication mechanism
ehdsouza Aug 16, 2019
7c969c8
chore(travis): remove older versions of python
ehdsouza Aug 16, 2019
bf7da48
feat(authenticator): Refactor autethnticators
ehdsouza Aug 20, 2019
8a95b89
fix(base service): Separate out request into prepare_request and send
ehdsouza Aug 20, 2019
52d6108
chore(token test): Handle tokens for all python versions
ehdsouza Aug 20, 2019
c50d2bb
feat(config): get_authenticator_from_environment for loading from env
ehdsouza Aug 20, 2019
b12274e
doc(readme): Update readme supporting auth methods
ehdsouza Aug 20, 2019
c7fcb3e
chore(auth): Changes as per Dustin's comments
ehdsouza Aug 21, 2019
e67e35a
chore(imports): remove unused imports
ehdsouza Aug 21, 2019
866f279
feat(env): Load from env variables
ehdsouza Aug 21, 2019
795c630
fix(value error): Throw error if authenticator is not set
ehdsouza Aug 22, 2019
e89e1a1
doc(typos): Correct typos by silly me
ehdsouza Aug 22, 2019
9054fa9
chore(rename): rename noath and iam authenticators to camel case
ehdsouza Aug 22, 2019
c488ace
chore(noAuth): changes as per new naming
ehdsouza Aug 22, 2019
cec3609
chore(noAuth): Consistent naming of noAuth value
ehdsouza Aug 22, 2019
7eb7e88
Merge pull request #21 from IBM/auth
ehdsouza Aug 27, 2019
a037815
fix(json): convert json to string for dict types
ehdsouza Aug 29, 2019
159d3f2
test(vcap): Changes for vcap services
ehdsouza Aug 29, 2019
8ef48a1
test(unit): Add more unit tests
ehdsouza Aug 29, 2019
5e6879a
Merge pull request #22 from IBM/rc-changes
ehdsouza Aug 30, 2019
561c1de
update version
ehdsouza Aug 30, 2019
65a32c2
update to rc2
ehdsouza Aug 30, 2019
3527b84
update to rc3
ehdsouza Aug 30, 2019
fc40656
update to find_packages()
ehdsouza Aug 30, 2019
a76903e
chore(version): update cersion
ehdsouza Aug 30, 2019
baba44d
fix(model): remove _from_dict step in _convert_model
ehdsouza Aug 30, 2019
a7f10ac
chore(model): Add json loads when string
ehdsouza Aug 30, 2019
fbdf3d4
fix(classname): Classname is no longer needed param
ehdsouza Sep 3, 2019
771c8fe
Merge pull request #23 from IBM/model-conversion
ehdsouza Sep 4, 2019
98046ed
Update version to rc5
ehdsouza Sep 4, 2019
f1f4986
doc(logging): Add logging information
ehdsouza Sep 11, 2019
b274cc6
chore(auth type): Case insensitive for auth type
ehdsouza Sep 11, 2019
0689985
fix(url): Change url to service_url
ehdsouza Sep 11, 2019
459513e
chore(service): Validate that service_url is set in the prepare_request
ehdsouza Sep 11, 2019
edd3bf3
Merge pull request #26 from IBM/rc-changes-2
ehdsouza Sep 12, 2019
f7f4647
chore(service_url): Update url to service url
ehdsouza Sep 12, 2019
010e923
Revert "chore(service_url): Update url to service url"
ehdsouza Sep 12, 2019
7ecc329
chore(service_url): Update url to service url
ehdsouza Sep 12, 2019
8fee5bf
feat(base): Add method read_external_sources
ehdsouza Sep 16, 2019
5c3d545
fix(credential): Check for credential file in working dir before home…
ehdsouza Sep 16, 2019
01912cd
chore(replace): service name replace - to _
ehdsouza Sep 17, 2019
fe36514
chore(doc): Update comment
ehdsouza Sep 17, 2019
50fad59
fix(config): Handle multi word service name
ehdsouza Sep 18, 2019
4148034
fix(client props): Setting of the external client config happens in i…
ehdsouza Sep 18, 2019
7981d92
chore(service_url): Make service url as None
ehdsouza Sep 18, 2019
41a3a51
test(service name): Mullti word service name
ehdsouza Sep 18, 2019
d21243c
Update version.py
ehdsouza Sep 18, 2019
a3b5305
chore(external_sources): Export externall sources
ehdsouza Sep 18, 2019
09fa9b5
Merge branch 'order' of github.com:IBM/python-sdk-core into order
ehdsouza Sep 18, 2019
b7b3723
chore(index): remove additional index checking
ehdsouza Sep 18, 2019
c4ba31d
chore(key): can't replace key but only convert to lower
ehdsouza Sep 18, 2019
4742b8e
fix(displlay_name): display_name unused in constructor
ehdsouza Sep 18, 2019
164d02e
chore(upper): Use upper() instead of lower()
ehdsouza Sep 18, 2019
a304b1a
Merge pull request #28 from IBM/order
ehdsouza Sep 19, 2019
cbaab0f
chore(version): Update versions
ehdsouza Sep 19, 2019
33a40d6
feat(disable-ssl-verification): log error for ssl error and others
ehdsouza Sep 19, 2019
4fcaa66
Merge pull request #29 from IBM/handle-ssl-error
ehdsouza Sep 23, 2019
70cf579
fix: support duplicate part names in 'files' parameter of prepare_req…
padamstx Sep 27, 2019
1243a70
chore(version): Update RC version
ehdsouza Sep 27, 2019
0fe9482
fix(client config): merge from master client config to JWT token manager
ehdsouza Oct 2, 2019
3e8b1d8
chore(ssl): update ssl verification error for icp4d
ehdsouza Oct 2, 2019
f51a456
Merge pull request #33 from IBM/rc
ehdsouza Oct 2, 2019
a1bae3f
chore(version): update version
ehdsouza Oct 2, 2019
7fe8d3c
refactor(version): Update version to merge to master
ehdsouza Oct 3, 2019
606a105
Merge branch 'master' into major-release
ehdsouza Oct 3, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: python
matrix:
include:
- python: 2.7
- python: 3.4
- python: 3.5
- python: 3.6
- python: 3.7
Expand Down
78 changes: 71 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,87 @@ easy_install --upgrade ibm-cloud-sdk-core
```

## Authentication Types
There are several flavors of authentication supported in this package. To specify the intended authentication pattern to use, the user can pass in the parameter `authentication_type`. This parameter is optional, but it may become required in a future major release. The options for this parameter are `basic`, `iam`, and `icp4d`.
There are several flavors of authentication supported in this package. To specify the intended authentication pattern to use, simply instantiate the `Authenticator` of your choice.

### basic
### Basic
This indicates Basic Auth is to be used. Users will pass in a `username` and `password` and the SDK will generate a Basic Auth header to send with requests to the service.

### iam
This indicates that IAM token authentication is to be used. Users can pass in an `iam_apikey` or an `iam_access_token`. If an API key is used, the SDK will manage the token for the user. In either case, the SDK will generate a Bearer Auth header to send with requests to the service.
```py
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator

### icp4d
This indicates that the service is an instance of ICP4D, which has its own version of token authentication. Users can pass in a `username` and `password`, or an `icp4d_access_token`. If a username and password is given, the SDK will manage the token for the user.
A `icp4d_url` is **required** for this type. In order to use an SDK-managed token with ICP4D authentication, this option **must** be passed in.
authenticator = BasicAuthenticator(<your_username>, <your_password>)
```

### IAM
This indicates that IAM token authentication is to be used. Users can pass in a `apikey` and the SDK will generate a Bearer Auth header to send with requests to the service.

```py
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator(<your_apikey>)
```

### Cloud Pak for Data
This indicates that the service is an instance of CP4D, which has its own version of token authentication. Users can pass in a `username`, `password` and `url`, and the SDK will generate a Bearer Auth header to send with requests to the service.

```py
from ibm_cloud_sdk_core.authenticators import CloudPakForDataAuthenticator

authenticator = CloudPakForDataAuthenticator(<your_username>, <your_password>, <your_url>)
```

### Bearer Token
This indicates bearer token authentication is to be used. The system would prepend the `bearer` name to your token and add it to the authorization header.

```py
from ibm_cloud_sdk_core.authenticators import BearerTokenAuthenticator

authenticator = BearerTokenAuthenticator(<your_bearer_token>)
```

### No Authentication
This indicates that no authentication is needed when sending requests to the service

```py
from ibm_cloud_sdk_core.authenticators import NoAuthAuthenticator

authenticator = NoAuthAuthenticator()
```

## Issues

If you encounter an issue with this project, you are welcome to submit a [bug report](https://github.com/IBM/python-sdk-core/issues).
Before opening a new issue, please search for similar issues. It's possible that someone has already reported it.

## Logging

### Enable logging

```python
import logging
logging.basicConfig(level=logging.DEBUG)
```

This would show output of the form:
```
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): iam.cloud.ibm.com:443
DEBUG:urllib3.connectionpool:https://iam.cloud.ibm.com:443 "POST /identity/token HTTP/1.1" 200 1809
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "POST /assistant/api/v1/workspaces?version=2018-07-10 HTTP/1.1" 201 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "GET /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10&export=true HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "DELETE /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10 HTTP/1.1" 200 28
```

### Low level request and response dump
To get low level information of the requests/ responses:

```python
from http.client import HTTPConnection
HTTPConnection.debuglevel = 1
```

## Open source @ IBM

Find more open source projects on the [IBM Github Page](http://github.com/IBM)
Expand Down
4 changes: 2 additions & 2 deletions ibm_cloud_sdk_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
from .detailed_response import DetailedResponse
from .iam_token_manager import IAMTokenManager
from .jwt_token_manager import JWTTokenManager
from .icp4d_token_manager import ICP4DTokenManager
from .cp4d_token_manager import CP4DTokenManager
from .api_exception import ApiException
from .utils import datetime_to_string, string_to_datetime
from .utils import datetime_to_string, string_to_datetime, get_authenticator_from_environment, read_external_sources
22 changes: 22 additions & 0 deletions ibm_cloud_sdk_core/authenticators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from .basic_authenticator import BasicAuthenticator
from .bearer_token_authenticator import BearerTokenAuthenticator
from .cp4d_authenticator import CloudPakForDataAuthenticator
from .iam_authenticator import IAMAuthenticator
from .no_auth_authenticator import NoAuthAuthenticator
32 changes: 32 additions & 0 deletions ibm_cloud_sdk_core/authenticators/authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABC, abstractmethod

class Authenticator(ABC):
@abstractmethod
def authenticate(self, req):
"""
Adds the Authorization header, if applicable
"""
pass

@abstractmethod
def validate(self):
"""
Checks if all the inputs needed are present
"""
pass
55 changes: 55 additions & 0 deletions ibm_cloud_sdk_core/authenticators/basic_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from ..utils import has_bad_first_or_last_char
import base64


class BasicAuthenticator(Authenticator):
authentication_type = 'basic'

def __init__(self, username, password):
"""
:attr str username: The username
:attr str password: The password
"""
self.username = username
self.password = password
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.username is None or self.password is None:
raise ValueError('The username and password shouldn\'t be None.')

if has_bad_first_or_last_char(
self.username) or has_bad_first_or_last_char(self.password):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

def authenticate(self, req):
"""
Adds the Authorization header, if applicable
"""
authstring = "{0}:{1}".format(self.username, self.password)
base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8')

headers = req.get('headers')
headers['Authorization'] = 'Basic {0}'.format(base64_authorization)
48 changes: 48 additions & 0 deletions ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator


class BearerTokenAuthenticator(Authenticator):
authentication_type = 'bearerToken'

def __init__(self, bearer_token):
"""
:attr str bearer_token: User managed bearer token
"""
self.bearer_token = bearer_token
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.bearer_token is None:
raise ValueError('The bearer token shouldn\'t be None.')

def authenticate(self, req):
"""
Adds the Authorization header, if applicable
"""
headers = req.get('headers')
headers['Authorization'] = 'Bearer {0}'.format(self.bearer_token)

def set_bearer_token(self, bearer_token):
"""
Sets the bearer token
"""
self.bearer_token = bearer_token
89 changes: 89 additions & 0 deletions ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from ..cp4d_token_manager import CP4DTokenManager
from ..utils import has_bad_first_or_last_char


class CloudPakForDataAuthenticator(Authenticator):
authentication_type = 'cp4d'

def __init__(self,
username,
password,
url,
disable_ssl_verification=False,
headers=None,
proxies=None):
"""
:attr str username: The username
:attr str password: The password
:attr str url: The url for authentication
:attr bool disable_ssl_verification: enables/ disabled ssl verification
:attr dict headers: user-defined headers
:attr dict proxies: user-defined proxies
"""
self.token_manager = CP4DTokenManager(
username, password, url, disable_ssl_verification, headers, proxies)
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.token_manager.username is None or self.token_manager.password is None:
raise ValueError('The username and password shouldn\'t be None.')

if self.token_manager.url is None:
raise ValueError('The url shouldn\'t be None.')

if has_bad_first_or_last_char(
self.token_manager.username) or has_bad_first_or_last_char(self.token_manager.password):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

if has_bad_first_or_last_char(self.token_manager.url):
raise ValueError(
'The url shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

def authenticate(self, req):
"""
Adds the Authorization header, if applicable
"""
headers = req.get('headers')
bearer_token = self.token_manager.get_token()
headers['Authorization'] = 'Bearer {0}'.format(bearer_token)

def set_disable_ssl_verification(self, status=False):
"""
Sets the ssl verification to enabled or disabled
"""
self.token_manager.set_disable_ssl_verification(status)

def set_headers(self, headers):
"""
Sets user-defined headers
"""
self.token_manager.set_headers(headers)

def set_proxies(self, proxies):
"""
Sets the proxies
"""
self.token_manager.set_proxies(proxies)
Loading