__init__.py 6.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Copyright (C) 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
33 34 35 36 37 38 39 40 41 42 43 44

import logging
import urlparse
import httplib

import simplejson
import objpool.http


logger = logging.getLogger(__name__)


45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# --------------------------------------------------------------------
# Astakos client API

# A simple retry decorator
def retry(howmany):
    def decorator(func):
        def f(*args, **kwargs):
            attemps = 0
            while True:
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    is_last_attempt = attemps == howmany - 1
                    if is_last_attempt:
                        raise e
                    if e.args:
                        status = e[0]
                        # In case of Unauthorized response
                        # or Not Found return immediately
                        if status == 401 or status == 404:
                            raise e
                    attemps += 1
        return f
    return decorator


71 72
# ----------------------------
# Authenticate
73 74 75 76 77 78 79 80 81 82 83 84 85 86
@retry(3)
def authenticate(token, astakos_url, usage=False, use_pool=False):
    """Check if user is an authenticated Astakos user

    Keyword arguments:
    token       -- user's token (string)
    astakos_url -- i.e https://accounts.example.com (string)
    usage       -- return usage information for user (boolean)
    use_pool    -- use objpool for http requests (boolean)

    In case of success return user informations (json parsed format).
    Otherwise raise an Exception.

    """
87
    authentication_url = urlparse.urljoin(astakos_url, "/im/authenticate")
88
    if usage:
89
        authentication_url += "?usage=1,"
90 91 92
    return _callAstakos(token, authentication_url, use_pool=use_pool)


93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
# ----------------------------
# Display Names
@retry(3)
def getDisplayNames(token, uuids, astakos_url, use_pool=False):
    """Return a uuid_catalog dictionary for the given uuids

    Keyword arguments:
    token       -- user's token (string)
    uuids       -- list of user ids (list of strings)
    astakos_url -- i.e https://accounts.example.com (string)
    use_pool    -- use objpool for http requests (boolean)

    The returned uuid_catalog is a dictionary with uuids as
    keys and the corresponding user names as values

    """
    req_headers = {'content-type': 'application/json'}
    req_body = simplejson.dumps({'uuids': uuids})
    req_url = urlparse.urljoin(astakos_url, "/user_catalogs")

    data = _callAstakos(token, req_url, headers=req_headers,
                        body=req_body, method="POST", use_pool=use_pool)
    return data.get("uuid_catalog")


def getDisplayName(token, uuid, astakos_url, use_pool=False):
    """Return the displayname of a uuid (see getDisplayNames)"""
    if not uuid:
121 122 123
        m = "No uuid was given"
        logger.error(m)
        raise ValueError(m)
124 125 126 127
    uuid_dict = getDisplayNames(token, [uuid], astakos_url, use_pool)
    return uuid_dict.get(uuid)


128 129 130 131 132 133 134 135 136 137 138 139
# --------------------------------------------------------------------
# Private functions
def _scheme_to_class(scheme):
    """Return the appropriate httplib class for given scheme"""
    if scheme == "http":
        return httplib.HTTPConnection
    elif scheme == "https":
        return httplib.HTTPSConnection
    else:
        return None


140 141 142 143 144 145 146 147 148 149
def _doRequest(conn, method, url, **kwargs):
    """The actual request. This function can easily be mocked"""
    conn.request(method, url, **kwargs)
    response = conn.getresponse()
    length = response.getheader('content-length', None)
    data = response.read(length)
    status = int(response.status)
    return (status, data)


150 151 152
def _callAstakos(token, url, headers={}, body=None,
                 method='GET', use_pool=False):
    """Make the actual call to astakos service"""
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    logger.debug("Make a %s request to %s with headers %s "
                 "and body %s, %s using the pool" %
                 (method, url, headers, body,
                     "without" if not use_pool else ""))

    # Check input
    if not token:
        m = "Token not given"
        logger.error(m)
        raise ValueError(m)
    if not url:
        m = "Url not given"
        logger.error(m)
        raise ValueError(m)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    # Build request's header and body
    kwargs = {}
    kwargs['headers'] = headers
    kwargs['headers']['X-Auth-Token'] = token
    if body:
        kwargs['body'] = body
        kwargs['headers'].setdefault(
            'content-type', 'application/octet-stream')
    kwargs['headers'].setdefault('content-length', len(body) if body else 0)

    # Check for supported scheme
    p = urlparse.urlparse(url)
    connection_class = _scheme_to_class(p.scheme)
    if connection_class is None:
        m = "Unsupported scheme: %s" % p.scheme
        logger.error(m)
        raise ValueError(m)

    # Get connection object
    if use_pool:
        conn = objpool.http.get_http_connection(p.netloc, p.scheme)
    else:
        conn = connection_class(p.netloc)

    # Send request
    try:
194 195
        request_url = p.path + '?' + p.query
        (status, data) = _doRequest(conn, method, request_url, **kwargs)
196 197 198 199 200 201 202 203 204
    except httplib.HTTPException as err:
        logger.error("Failed to send request: %s" % err)
        raise
    finally:
        conn.close()

    # Return
    logger.debug("Request returned with status %s" % status)
    if status < 200 or status >= 300:
205
        raise Exception(status, data)
206
    return simplejson.loads(unicode(data))