Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

fix(client): remove bravado libs restrictions #706

Merged
merged 4 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 28 additions & 20 deletions aether-client-library/aether/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@
from time import sleep
from urllib.parse import urlparse

import bravado_core
from .exceptions import AetherAPIException
from . import basic_auth
from . import oidc
from .logger import LOG

# monkey patch so that bulk insertion works
from . import patches
bravado_core.marshal.marshal_model = patches.marshal_model # noqa
bravado_core.marshal.marshal_object = patches.marshal_object # noqa
bravado_core.unmarshal.unmarshal_model = patches.unmarshal_model # noqa
from .patches import patched__marshal_object, patched__unmarshal_object
import bravado_core
bravado_core.marshal._marshal_object = patched__marshal_object # noqa
bravado_core.unmarshal._unmarshal_object = patched__unmarshal_object # noqa

import bravado

from bravado.client import (
SwaggerClient,
ResourceDecorator,
Expand All @@ -44,6 +37,13 @@
from bravado.config import bravado_config_from_config_dict
from bravado.swagger_model import Loader

from .exceptions import AetherAPIException
from .basic_auth import BasicRealmClient
from .oidc import OauthClient
from .logger import LOG

_SPEC_URL = '{}/v1/schema/?format=openapi'


class Client(SwaggerClient):
AUTH_METHODS = ['oauth', 'basic']
Expand All @@ -61,7 +61,7 @@ def __init__(
keycloak_url=None,
auth_type='oauth',
# used to specify gateway endpoint ({realm}/{endpoint_name})
endpoint_name='kernel'
endpoint_name='kernel',
):
if auth_type not in Client.AUTH_METHODS:
raise ValueError(f'allowed auth_types are {Client.AUTH_METHODS}')
Expand All @@ -76,14 +76,19 @@ def __init__(
}
url_info = urlparse(url)
server = f'{url_info.scheme}://{url_info.netloc}'
spec_url = '%s/v1/schema/?format=openapi' % url

if auth_type == 'basic':
LOG.debug(f'Using basic auth on {server}')
auth = basic_auth.BasicRealmAuthenticator(server, realm, user, pw)
http_client = basic_auth.BasicRealmClient(auth)
http_client = BasicRealmClient()
http_client.set_realm_basic_auth(
host=url_info.netloc,
username=user,
password=pw,
realm=realm,
)
loader = Loader(http_client, request_headers=None)
try:
spec_url = _SPEC_URL.format(url)
LOG.debug(f'Loading schema from: {spec_url}')
spec_dict = loader.load_spec(spec_url)
except bravado.exception.HTTPForbidden as forb:
Expand All @@ -95,13 +100,16 @@ def __init__(
) as bgwe:
LOG.error('Server Unavailable')
raise bgwe

else:
LOG.debug(f'getting OIDC session on realm {realm}')
auth = oidc.OauthAuthenticator(
server, realm, user, pw,
keycloak_url, offline_token, endpoint_name)
spec_dict = auth.get_spec(spec_url)
http_client = oidc.OauthClient(auth)
http_client = OauthClient()
http_client.set_oauth(
url_info.netloc,
keycloak_url or f'{server}/auth', realm,
user, pw, offline_token, endpoint_name)
spec_url = _SPEC_URL.format(f'{server}/{realm}/{endpoint_name}')
spec_dict = http_client.authenticator.get_spec(spec_url)

# We take this from the from_url class method of SwaggerClient
# Apply bravado config defaults
Expand Down
70 changes: 34 additions & 36 deletions aether-client-library/aether/client/basic_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,48 @@
# under the License.

import os
import requests

from bravado.requests_client import Authenticator, RequestsClient
from requests.auth import HTTPBasicAuth
from urllib.parse import urlparse
from bravado.requests_client import BasicAuthenticator, RequestsClient


class BasicRealmClient(RequestsClient):
"""Synchronous HTTP client implementation.
"""

def __init__(self, authenticator, ssl_verify=True, ssl_cert=None):
"""
:param ssl_verify: Set to False to disable SSL certificate validation.
Provide the path to a CA bundle if you need to use a custom one.
:param ssl_cert: Provide a client-side certificate to use. Either a
sequence of strings pointing to the certificate (1) and the private
key (2), or a string pointing to the combined certificate and key.
"""
self.session = requests.Session()
self.authenticator = authenticator
self.ssl_verify = ssl_verify
self.ssl_cert = ssl_cert

def apply_authentication(self, request):
return self.authenticator.apply(request)


class BasicRealmAuthenticator(Authenticator):
class BasicRealmAuthenticator(BasicAuthenticator):

def __init__(
self,
server,
realm,
user=None,
pw=None
host,
username,
password,
realm=None,
):
super(BasicRealmAuthenticator, self).__init__(host, username, password)
self.realm = realm
self.auth = HTTPBasicAuth(user, pw)
self.host = urlparse(server).netloc

def apply(self, req):
req.auth = self.auth
def apply(self, request):
request = super(BasicRealmAuthenticator, self).apply(request)

if self.realm:
header = os.environ['KERNEL_REALM_HEADER']
req.headers[header] = self.realm
return req
request.headers[header] = self.realm
return request


class BasicRealmClient(RequestsClient):
"""Synchronous HTTP client implementation.
"""

def set_realm_basic_auth(
self,
host,
username,
password,
realm=None,
):
self.authenticator = BasicRealmAuthenticator(
host=host,
username=username,
password=password,
realm=realm,
)

def apply_authentication(self, request):
return self.authenticator.apply(request)
Loading