Exemplo n.º 1
0
def _prepare_url_request(host, username, password):
    """
    Return a ProxyManager object (as defined in urllib3 [1]) that can be used
    to perform authorized requests to a specific host.

    Authorization header is constructed and set using "username" and "password"
    parameters. Also set the common HTTP headers that we want to be sent with
    each request.

    [1]: http://urllib3.readthedocs.io/en/latest/reference/#urllib3.poolmanager.ProxyManager  # noqa
    """
    # Initialize http and https pool managers
    num_pools = 1
    managers = {}

    if host.lower().startswith("http://"):
        scheme = "http"
        if "http_proxy" in os.environ:
            proxy_url = urllib3.util.url.parse_url(os.environ["http_proxy"])
            managers["http"] = urllib3.ProxyManager(
                proxy_url=proxy_url.url,
                proxy_headers=urllib3.util.make_headers(
                    user_agent=user_agent_identifier(),
                    proxy_basic_auth=proxy_url.auth),
                num_pools=num_pools)
        else:
            managers["http"] = urllib3.PoolManager(num_pools=num_pools)
    elif host.lower().startswith("https://"):
        scheme = "https"
        if "https_proxy" in os.environ:
            proxy_url = urllib3.util.url.parse_url(os.environ["https_proxy"])
            managers["https"] = urllib3.ProxyManager(
                proxy_url=proxy_url.url,
                proxy_headers=urllib3.util.make_headers(
                    user_agent=user_agent_identifier(),
                    proxy_basic_auth=proxy_url.auth),
                num_pools=num_pools,
                cert_reqs=CERT_REQUIRED,
                ca_certs=certs_file())
        else:
            managers["https"] = urllib3.PoolManager(num_pools=num_pools,
                                                    cert_reqs=CERT_REQUIRED,
                                                    ca_certs=certs_file())
    else:
        raise Exception("Unknown scheme")

    headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(
        username, password),
                                        accept_encoding=True,
                                        user_agent=user_agent_identifier(),
                                        keep_alive=True)

    manager = managers[scheme]

    return headers, manager
Exemplo n.º 2
0
def make_request(method, host, url, username, password, fields=None):
    if host.lower().startswith('https://'):
        connection = urllib3.connection_from_url(host,
                                                 cert_reqs=ssl.CERT_REQUIRED,
                                                 ca_certs=certs_file())
    else:
        connection = urllib3.connection_from_url(host)
    headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(
        username, password),
                                        accept_encoding=True,
                                        user_agent=user_agent_identifier(),
                                        keep_alive=True)
    r = None
    try:
        r = connection.request(method, url, headers=headers, fields=fields)
        data = r.data
        charset = determine_charset(r)
        if isinstance(data, bytes):
            data = data.decode(charset)
        if r.status < 200 or r.status >= 400:
            if r.status == 404:
                raise HttpNotFound(data)
            else:
                raise Exception(data)
        return data, charset
    except SSLError:
        logger.error("Invalid SSL certificate")
        raise
    finally:
        if not r is None:
            r.close()
Exemplo n.º 3
0
def make_request(method, host, url, username, password, fields=None):
    if host.lower().startswith('https://'):
        connection = urllib3.connection_from_url(
            host,
            cert_reqs=ssl.CERT_REQUIRED,
            ca_certs=certs_file()
        )
    else:
        connection = urllib3.connection_from_url(host)
    headers = urllib3.util.make_headers(
        basic_auth='{0}:{1}'.format(username, password),
        accept_encoding=True,
        user_agent=user_agent_identifier(),
        keep_alive=True
    )
    r = None
    try:
        r = connection.request(method, url, headers=headers, fields=fields)
        data = r.data
        charset = determine_charset(r)
        if isinstance(data, bytes):
            data = data.decode(charset)
        if r.status < 200 or r.status >= 400:
            if r.status == 404:
                raise HttpNotFound(data)
            else:
                raise Exception(data)
        return data, charset
    except SSLError:
        logger.error("Invalid SSL certificate")
        raise
    finally:
        if not r is None:
            r.close()
Exemplo n.º 4
0
def get_details(api_call, username, password, *args, **kwargs):
    """
    Get the tx project info through the API.

    This function can also be used to check the existence of a project.
    """
    url = (API_URLS[api_call] % (kwargs)).encode('UTF-8')
    conn = urllib3.connection_from_url(kwargs['hostname'])
    headers = urllib3.util.make_headers(
        basic_auth='{0}:{1}'.format(username, password),
        accept_encoding=True,
        user_agent=user_agent_identifier(),
    )
    try:
        r = conn.request('GET', url, headers=headers)
        if r.status < 200 or r.status >= 400:
            raise Exception(r.data)
        remote_project = parse_json(r.data)
        return remote_project
    except ssl.SSLError:
        logger.error("Invalid SSL certificate")
        raise
    except Exception, e:
        logger.debug(unicode(e))
        raise
Exemplo n.º 5
0
def make_request(method,
                 host,
                 url,
                 username,
                 password,
                 fields=None,
                 skip_decode=False):

    # Initialize http and https pool managers
    num_pools = 1
    managers = {}

    if host.lower().startswith("http://"):
        scheme = "http"
        if "http_proxy" in os.environ:
            proxy_url = os.environ["http_proxy"]
            managers["http"] = urllib3.ProxyManager(
                proxy_url=proxy_url,
                proxy_headers={"User-Agent": user_agent_identifier()},
                num_pools=num_pools)
        else:
            managers["http"] = urllib3.PoolManager(num_pools=num_pools)
    elif host.lower().startswith("https://"):
        scheme = "https"
        if "https_proxy" in os.environ:
            proxy_url = os.environ["https_proxy"]
            managers["https"] = urllib3.ProxyManager(
                proxy_url=proxy_url,
                proxy_headers={"User-Agent": user_agent_identifier()},
                num_pools=num_pools,
                cert_reqs=CERT_REQUIRED,
                ca_certs=certs_file())
        else:
            managers["https"] = urllib3.PoolManager(num_pools=num_pools,
                                                    cert_reqs=CERT_REQUIRED,
                                                    ca_certs=certs_file())
    else:
        raise Exception("Unknown scheme")

    charset = None
    headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(
        username, password),
                                        accept_encoding=True,
                                        user_agent=user_agent_identifier(),
                                        keep_alive=True)

    response = None
    try:
        manager = managers[scheme]
        response = manager.request(method,
                                   host + url,
                                   headers=dict(headers),
                                   fields=fields)
        data = response.data
        if not skip_decode:
            charset = determine_charset(response)
            if isinstance(data, bytes):
                data = data.decode(charset)
        if response.status < 200 or response.status >= 400:
            if response.status == 401:
                raise HttpNotAuthorized(data)
            elif response.status == 404:
                raise HttpNotFound(data)
            else:
                raise Exception(data)
        return data, charset
    except SSLError:
        logger.error("Invalid SSL certificate")
        raise
    finally:
        if response is not None:
            response.close()
Exemplo n.º 6
0
def make_request(method, host, url, username, password, fields=None,
                 skip_decode=False):

    # Initialize http and https pool managers
    num_pools = 1
    managers = {}

    if host.lower().startswith("http://"):
        scheme = "http"
        if "http_proxy" in os.environ:
            proxy_url = os.environ["http_proxy"]
            managers["http"] = urllib3.ProxyManager(
                proxy_url=proxy_url,
                proxy_headers={"User-Agent": user_agent_identifier()},
                num_pools=num_pools
            )
        else:
            managers["http"] = urllib3.PoolManager(num_pools=num_pools)
    elif host.lower().startswith("https://"):
        scheme = "https"
        if "https_proxy" in os.environ:
            proxy_url = os.environ["https_proxy"]
            managers["https"] = urllib3.ProxyManager(
                proxy_url=proxy_url,
                proxy_headers={"User-Agent": user_agent_identifier()},
                num_pools=num_pools,
                cert_reqs=CERT_REQUIRED,
                ca_certs=certs_file()
            )
        else:
            managers["https"] = urllib3.PoolManager(
                num_pools=num_pools,
                cert_reqs=CERT_REQUIRED,
                ca_certs=certs_file()
            )
    else:
        raise Exception("Unknown scheme")

    charset = None
    headers = urllib3.util.make_headers(
        basic_auth='{0}:{1}'.format(username, password),
        accept_encoding=True,
        user_agent=user_agent_identifier(),
        keep_alive=True
    )

    response = None
    try:
        manager = managers[scheme]
        # All arguments must be bytes, not unicode
        encoded_request = encode_args(manager.request)
        response = encoded_request(
            method,
            host + url,
            headers=dict(headers),
            fields=fields
        )
        data = response.data
        if not skip_decode:
            charset = determine_charset(response)
            if isinstance(data, bytes):
                data = data.decode(charset)
        if response.status < 200 or response.status >= 400:
            if response.status == 401:
                raise HttpNotAuthorized(data)
            elif response.status == 404:
                raise HttpNotFound(data)
            else:
                raise Exception(data)
        return data, charset
    except SSLError:
        logger.error("Invalid SSL certificate")
        raise
    finally:
        if response is not None:
            response.close()
Exemplo n.º 7
0
def make_request(method,
                 host,
                 url,
                 username,
                 password,
                 fields=None,
                 skip_decode=False,
                 get_params={}):

    # Initialize http and https pool managers
    num_pools = 1
    managers = {}

    if host.lower().startswith("http://"):
        scheme = "http"
        if "http_proxy" in os.environ:
            proxy_url = urllib3.util.url.parse_url(os.environ["http_proxy"])
            managers["http"] = urllib3.ProxyManager(
                proxy_url=proxy_url.url,
                proxy_headers=urllib3.util.make_headers(
                    user_agent=user_agent_identifier(),
                    proxy_basic_auth=proxy_url.auth),
                num_pools=num_pools)
        else:
            managers["http"] = urllib3.PoolManager(num_pools=num_pools)
    elif host.lower().startswith("https://"):
        scheme = "https"
        if "https_proxy" in os.environ:
            proxy_url = urllib3.util.url.parse_url(os.environ["https_proxy"])
            managers["https"] = urllib3.ProxyManager(
                proxy_url=proxy_url.url,
                proxy_headers=urllib3.util.make_headers(
                    user_agent=user_agent_identifier(),
                    proxy_basic_auth=proxy_url.auth),
                num_pools=num_pools,
                cert_reqs=CERT_REQUIRED,
                ca_certs=certs_file())
        else:
            managers["https"] = urllib3.PoolManager(num_pools=num_pools,
                                                    cert_reqs=CERT_REQUIRED,
                                                    ca_certs=certs_file())
    else:
        raise Exception("Unknown scheme")

    charset = None
    headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format(
        username, password),
                                        accept_encoding=True,
                                        user_agent=user_agent_identifier(),
                                        keep_alive=True)

    response = None
    try:
        manager = managers[scheme]
        # All arguments must be bytes, not unicode
        encoded_request = encode_args(manager.request)
        response = encoded_request(method,
                                   urljoin(host, url),
                                   headers=dict(headers),
                                   fields=fields)
        data = response.data
        if not skip_decode:
            charset = determine_charset(response)
            if isinstance(data, bytes):
                data = data.decode(charset)
        if response.status < 200 or response.status >= 400:
            if response.status == 401:
                raise AuthenticationError(data)
            elif response.status == 403:
                raise HttpNotAuthorized(data)
            elif response.status == 404:
                raise HttpNotFound(data)
            elif response.status >= 500:
                msg = "Failed to connect. Server responded with HTTP code {}"
                raise TXConnectionError(msg.format(response.status),
                                        code=response.status)
            else:
                raise Exception("Error received from server: {}".format(data))
        return data, charset
    except SSLError:
        logger.error("Invalid SSL certificate")
        raise
    finally:
        if response is not None:
            response.close()