def hil_client_connect(endpoint_ip, name, pw):
    '''
    '''
    hil_http_client = RequestsHTTPClient()
    hil_http_client.auth = (name, pw)

    return Client(endpoint_ip, hil_http_client)
Esempio n. 2
0
def hil_client_connect(endpoint_ip, name, pw):
    """Returns a HIL client object"""

    hil_http_client = RequestsHTTPClient()
    hil_http_client.auth = (name, pw)

    return Client(endpoint_ip, hil_http_client)
Esempio n. 3
0
def hil_client_connect(endpoint_ip, name, pw):
    """Returns a HIL client object"""

    hil_http_client = RequestsHTTPClient()
    hil_http_client.auth = (name, pw)

    return Client(endpoint_ip, hil_http_client)
Esempio n. 4
0
 def __init__(self, endpoint, username=None, password=None):
     if (username is None) != (password is None):
         assert False, (
             "You must either specify both username and password, "
             "or neither.")
     self._flask_client = app.test_client()
     self._requests_client = RequestsHTTPClient()
     self._endpoint = endpoint
     self._username = username
     self._password = password
Esempio n. 5
0
 def __init__(self, endpoint, username=None, password=None):
     if (username is None) != (password is None):
         assert False, (
             "You must either specify both username and password, "
             "or neither."
         )
     self._flask_client = app.test_client()
     self._requests_client = RequestsHTTPClient()
     self._endpoint = endpoint
     self._username = username
     self._password = password
Esempio n. 6
0
def setup_http_client():
    """Set `http_client` to a valid instance of `HTTPClient`

    and pass it as parameter to initialize the client library.

    Sets http_client to an object which makes HTTP requests with
    authentication. It chooses an authentication backend as follows:

    1. If the environment variables HIL_USERNAME and HIL_PASSWORD
       are defined, it will use HTTP basic auth, with the corresponding
       user name and password.
    2. If the `python-keystoneclient` library is installed, and the
       environment variables:

           * OS_AUTH_URL
           * OS_USERNAME
           * OS_PASSWORD
           * OS_PROJECT_NAME

       are defined, Keystone is used.
    3. Oterwise, do not supply authentication information.

    This may be extended with other backends in the future.

    `http_client` is also passed as a parameter to the client library.
    Until all calls are moved to client library, this will support
    both ways of intereacting with HIL.
    """
    global http_client
    global C  # initiating the client library
    # First try basic auth:
    ep = (os.environ.get('HIL_ENDPOINT')
          or sys.stdout.write("Error: HIL_ENDPOINT not set \n"))
    basic_username = os.getenv('HIL_USERNAME')
    basic_password = os.getenv('HIL_PASSWORD')
    if basic_username is not None and basic_password is not None:
        # For calls with no client library support yet.
        # Includes all headnode calls; registration of nodes and switches.
        http_client = RequestsHTTPClient()
        http_client.auth = (basic_username, basic_password)
        # For calls using the client library
        C = Client(ep, http_client)
        return
    # Next try keystone:
    try:
        from keystoneauth1.identity import v3
        from keystoneauth1 import session
        os_auth_url = os.getenv('OS_AUTH_URL')
        os_password = os.getenv('OS_PASSWORD')
        os_username = os.getenv('OS_USERNAME')
        os_user_domain_id = os.getenv('OS_USER_DOMAIN_ID') or 'default'
        os_project_name = os.getenv('OS_PROJECT_NAME')
        os_project_domain_id = os.getenv('OS_PROJECT_DOMAIN_ID') or 'default'
        if None in (os_auth_url, os_username, os_password, os_project_name):
            raise KeyError("Required openstack environment variable not set.")
        auth = v3.Password(auth_url=os_auth_url,
                           username=os_username,
                           password=os_password,
                           project_name=os_project_name,
                           user_domain_id=os_user_domain_id,
                           project_domain_id=os_project_domain_id)
        sess = session.Session(auth=auth)
        http_client = KeystoneHTTPClient(sess)
        # For calls using the client library
        C = Client(ep, http_client)
        return
    except (ImportError, KeyError):
        pass
    # Finally, fall back to no authentication:
    http_client = requests.Session()
    C = Client(ep, http_client)
Esempio n. 7
0
def setup_http_client():
    """Set `http_client` to a valid instance of `HTTPClient`

    and pass it as parameter to initialize the client library.

    Sets http_client to an object which makes HTTP requests with
    authentication. It chooses an authentication backend as follows:

    1. If the environment variables HIL_USERNAME and HIL_PASSWORD
       are defined, it will use HTTP basic auth, with the corresponding
       user name and password.
    2. If the `python-keystoneclient` library is installed, and the
       environment variables:

           * OS_AUTH_URL
           * OS_USERNAME
           * OS_PASSWORD
           * OS_PROJECT_NAME

       are defined, Keystone is used.
    3. Otherwise, do not supply authentication information.

    This may be extended with other backends in the future.

    `http_client` is also passed as a parameter to the client library.
    Until all calls are moved to client library, this will support
    both ways of intereacting with HIL.
    """
    # First try basic auth:
    ep = os.environ.get('HIL_ENDPOINT')

    if ep is None:
        sys.exit("Error: HIL_ENDPOINT not set \n")

    basic_username = os.getenv('HIL_USERNAME')
    basic_password = os.getenv('HIL_PASSWORD')
    if basic_username is not None and basic_password is not None:
        # For calls with no client library support yet.
        # Includes all headnode calls; registration of nodes and switches.
        http_client = RequestsHTTPClient()
        http_client.auth = (basic_username, basic_password)
        # For calls using the client library
        return Client(ep, http_client), http_client
    # Next try keystone:
    try:
        from keystoneauth1.identity import v3
        from keystoneauth1 import session
        os_auth_url = os.getenv('OS_AUTH_URL')
        os_password = os.getenv('OS_PASSWORD')
        os_username = os.getenv('OS_USERNAME')
        os_user_domain_id = os.getenv('OS_USER_DOMAIN_ID') or 'default'
        os_project_name = os.getenv('OS_PROJECT_NAME')
        os_project_domain_id = os.getenv('OS_PROJECT_DOMAIN_ID') or 'default'
        if None in (os_auth_url, os_username, os_password, os_project_name):
            raise KeyError("Required openstack environment variable not set.")
        auth = v3.Password(auth_url=os_auth_url,
                           username=os_username,
                           password=os_password,
                           project_name=os_project_name,
                           user_domain_id=os_user_domain_id,
                           project_domain_id=os_project_domain_id)
        sess = session.Session(auth=auth)
        http_client = KeystoneHTTPClient(sess)
        return Client(ep, http_client), http_client
    except (ImportError, KeyError):
        pass
    # Finally, fall back to no authentication:
    http_client = RequestsHTTPClient()
    return Client(ep, http_client), http_client
Esempio n. 8
0
class HybridHTTPClient(HTTPClient):
    """Implementation of HTTPClient for use in testing.

    Depending on the URL, this uses either Flask's test client,
    or a RequestsHTTPClient. This allows us to do testing without
    launching a separate process for the API server, while still
    being able to talk to external services (like OBMd and keystone).

    Parameters
    ----------

    endpoint : str
        The endpoint for the HIL server
    username : str or None
        The username to authenticate as
    password : str or None
        The password to authenticate with

    If username and password are not specified, not authentication is used,
    otherwise they are used for HTTP basic auth. It is an error to specify
    one and not the other.
    """
    def __init__(self, endpoint, username=None, password=None):
        if (username is None) != (password is None):
            assert False, (
                "You must either specify both username and password, "
                "or neither.")
        self._flask_client = app.test_client()
        self._requests_client = RequestsHTTPClient()
        self._endpoint = endpoint
        self._username = username
        self._password = password

    def request(self, method, url, data=None, params=None):
        # Manually follow redirects. We can't just rely on flask to do it,
        # since if we get redirected to something outside of the app, we
        # want to switch to a different client.
        resp = self._make_request(method, url, data=data, params=params)
        while self._is_redirect(resp):
            url = resp.headers['Location']
            resp = self._make_request(method, url, data=data, params=params)
        return resp

    @staticmethod
    def _is_redirect(resp):
        """Return whether `resp` is a redirect response."""
        return resp.status_code == 307

    def _make_request(self, method, url, data, params):
        """Make an HTTP Request.

        This is a helper method for `request`. It handles making a request,
        but does not itself follow redirects.
        """
        if urlparse(url).netloc == urlparse(self._endpoint).netloc:
            # Url is within the Flask app; use Flask's test client.

            if self._username is None:
                # Username and password not specified; don't set an
                # Authorization header.
                headers = {}
            else:
                headers = {
                    # Flask doesn't provide a straightforward way to do basic
                    # auth, but it's not actually that complicated:
                    'Authorization':
                    'Basic ' +
                    urlsafe_b64encode(self._username + ':' + self._password),
                }

            resp = self._flask_client.open(
                method=method,
                headers=headers,
                # flask expects just a path, and assumes
                # the host & scheme:
                path=urlparse(url).path,
                data=data,
                query_string=params,
            )
            return HTTPResponse(status_code=resp.status_code,
                                headers=resp.headers,
                                content=resp.get_data())
        else:
            # URL is outside of our flask app; use the real http client.
            return self._requests_client.request(method,
                                                 url,
                                                 data=data,
                                                 params=params)
Esempio n. 9
0
import json
import os
import pytest
import requests
import sys
import tempfile
import time

from subprocess import check_call, Popen

ep = "http://127.0.0.1:8000" or os.environ.get('HIL_ENDPOINT')
username = "******" or os.environ.get('HIL_USERNAME')
password = "******" or os.environ.get('HIL_PASSWORD')

http_client = RequestsHTTPClient()
http_client.auth = (username, password)
C = Client(ep, http_client)  # Initializing client library
MOCK_SWITCH_TYPE = 'http://schema.massopencloud.org/haas/v0/switches/mock'
OBM_TYPE_MOCK = 'http://schema.massopencloud.org/haas/v0/obm/mock'
OBM_TYPE_IPMI = 'http://schema.massopencloud.org/haas/v0/obm/ipmi'


class Test_ClientBase:
    """Tests client initialization and object_url creation. """
    def test_init_error(self):
        with pytest.raises(TypeError):
            x = ClientBase()

    def test_object_url(self):
        x = ClientBase(ep, 'some_base64_string')
Esempio n. 10
0
class HybridHTTPClient(HTTPClient):
    """Implementation of HTTPClient for use in testing.

    Depending on the URL, this uses either Flask's test client,
    or a RequestsHTTPClient. This allows us to do testing without
    launching a separate process for the API server, while still
    being able to talk to external services (like OBMd and keystone).

    Parameters
    ----------

    endpoint : str
        The endpoint for the HIL server
    username : str or None
        The username to authenticate as
    password : str or None
        The password to authenticate with

    If username and password are not specified, not authentication is used,
    otherwise they are used for HTTP basic auth. It is an error to specify
    one and not the other.
    """

    def __init__(self, endpoint, username=None, password=None):
        if (username is None) != (password is None):
            assert False, (
                "You must either specify both username and password, "
                "or neither."
            )
        self._flask_client = app.test_client()
        self._requests_client = RequestsHTTPClient()
        self._endpoint = endpoint
        self._username = username
        self._password = password

    def request(self, method, url, data=None, params=None):
        # Manually follow redirects. We can't just rely on flask to do it,
        # since if we get redirected to something outside of the app, we
        # want to switch to a different client.
        resp = self._make_request(method, url, data=data, params=params)
        while self._is_redirect(resp):
            url = resp.headers['Location']
            resp = self._make_request(method, url, data=data, params=params)
        return resp

    @staticmethod
    def _is_redirect(resp):
        """Return whether `resp` is a redirect response."""
        return resp.status_code == 307

    def _make_request(self, method, url, data, params):
        """Make an HTTP Request.

        This is a helper method for `request`. It handles making a request,
        but does not itself follow redirects.
        """
        if urlparse(url).netloc == urlparse(self._endpoint).netloc:
            # Url is within the Flask app; use Flask's test client.

            if self._username is None:
                # Username and password not specified; don't set an
                # Authorization header.
                headers = {}
            else:
                headers = {
                    # Flask doesn't provide a straightforward way to do basic
                    # auth, but it's not actually that complicated:
                    'Authorization': 'Basic ' + urlsafe_b64encode(
                        self._username + ':' + self._password
                    ),
                }

            resp = self._flask_client.open(
                method=method,
                headers=headers,
                # flask expects just a path, and assumes
                # the host & scheme:
                path=urlparse(url).path,
                data=data,
                query_string=params,
            )
            return HTTPResponse(status_code=resp.status_code,
                                headers=resp.headers,
                                # As far as I(zenhack) can tell, flask's test
                                # client doesn't support streaming, so we just
                                # return a one-shot iterator. This is
                                # conceptually not ideal, but fine in practice
                                # because the HIL api server doesn't do any
                                # streaming anyway; we only need that
                                # functionality with requests that go to OBMd.
                                body=iter([resp.get_data()]))
        else:
            # URL is outside of our flask app; use the real http client.
            return self._requests_client.request(method,
                                                 url,
                                                 data=data,
                                                 params=params)
Esempio n. 11
0
import json
import os
import pytest
import requests
import sys
import tempfile
import time

from subprocess import check_call, Popen

ep = "http://127.0.0.1:8000" or os.environ.get('HIL_ENDPOINT')
username = "******" or os.environ.get('HIL_USERNAME')
password = "******" or os.environ.get('HIL_PASSWORD')

http_client = RequestsHTTPClient()
http_client.auth = (username, password)
C = Client(ep, http_client)  # Initializing client library
MOCK_SWITCH_TYPE = 'http://schema.massopencloud.org/haas/v0/switches/mock'
OBM_TYPE_MOCK = 'http://schema.massopencloud.org/haas/v0/obm/mock'
OBM_TYPE_IPMI = 'http://schema.massopencloud.org/haas/v0/obm/ipmi'


class Test_ClientBase:
    """Tests client initialization and object_url creation. """

    def test_init_error(self):
        with pytest.raises(TypeError):
            x = ClientBase()

    def test_object_url(self):