Esempio n. 1
0
    def __init__(self, host='localhost', port=9200, http_auth=None,
        use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None,
        client_key=None, headers=None, **kwargs):
        if not URLFETCH_AVAILABLE:
            raise ImproperlyConfigured("Please install appengine SDK to use URLFetchConnection.")

        super(URLFetchConnection, self).__init__(host=host, port=port, **kwargs)

        self.headers = headers.copy() if headers else dict()

        if http_auth is not None:
            if isinstance(http_auth, (tuple, list)):
                http_auth = tuple(http_auth)
            elif isinstance(http_auth, string_types):
                http_auth = tuple(http_auth.split(':', 1))

            self.headers['Authorization'] = "Basic %s" % base64.b64encode("%s:%s" % http_auth)

        self.base_url = 'http%s://%s:%d%s' % (
            's' if use_ssl else '',
            host, port, self.url_prefix
        )
        self.verify_certs = verify_certs

        assert not ca_certs, "ca_certs not supported by URLFetchConnection."
        assert not client_key, "client_key not supported by URLFetchConnection."
        assert not client_cert, "client_cert not supported by URLFetchConnection."

        if use_ssl and not verify_certs:
            warnings.warn(
                'Connecting to %s using SSL with verify_certs=False is insecure.' % self.base_url)
Esempio n. 2
0
    def __init__(self, connections, **kwargs):
        if len(connections) != 1:
            raise ImproperlyConfigured(
                'DummyConnectionPool needs exactly one connection defined.', )

        self.connection_opts = connections
        self.connection = connections[0][0]
        self.connections = (self.connection, )
    def __init__(self,
                 host='localhost',
                 port=9200,
                 http_auth=None,
                 use_ssl=False,
                 verify_certs=False,
                 ca_certs=None,
                 client_cert=None,
                 maxsize=10,
                 block=False,
                 **kwargs):

        super(Urllib3HttpConnection, self).__init__(host=host,
                                                    port=port,
                                                    **kwargs)
        self.headers = {}
        if http_auth is not None:
            if isinstance(http_auth, (tuple, list)):
                http_auth = ':'.join(http_auth)
            self.headers = urllib3.make_headers(basic_auth=http_auth)

        pool_class = urllib3.HTTPConnectionPool
        kw = {}
        if use_ssl:
            pool_class = urllib3.HTTPSConnectionPool

            if verify_certs:
                kw['cert_reqs'] = 'CERT_REQUIRED'
                kw['ca_certs'] = ca_certs
                kw['cert_file'] = client_cert
            elif ca_certs:
                raise ImproperlyConfigured(
                    "You cannot pass CA certificates when verify SSL is off.")

        self.pool = pool_class(host,
                               port=port,
                               timeout=self.timeout,
                               maxsize=maxsize,
                               block=block,
                               **kw)
        def _create_connection(host):
            # if this is not the initial setup look at the existing connection
            # options and identify connections that haven't changed and can be
            # kept around.
            if hasattr(self, 'connection_pool'):
                for (connection,
                     old_host) in self.connection_pool.connection_opts:
                    if old_host == host:
                        return connection

            # previously unseen params, create new connection
            kwargs = self.kwargs.copy()
            kwargs.update(host)

            if 'scheme' in host and host[
                    'scheme'] != self.connection_class.transport_schema:
                raise ImproperlyConfigured(
                    'Scheme specified in connection (%s) is not the same as the connection class (%s) specifies (%s).'
                    % (host['scheme'], self.connection_class.__name__,
                       self.connection_class.transport_schema))
            ntlm_auth = HttpNtlmAuth(kwargs.get('ntlm_user', ''),
                                     kwargs.get('ntlm_pass', ''))
            return self.connection_class(http_auth=ntlm_auth, **kwargs)
Esempio n. 5
0
        def _create_connection(host):
            # if this is not the initial setup look at the existing connection
            # options and identify connections that haven't changed and can be
            # kept around.
            if hasattr(self, 'connection_pool'):
                for (connection,
                     old_host) in self.connection_pool.connection_opts:  # noqa
                    if old_host == host:
                        return connection

            kwargs = self.kwargs.copy()
            kwargs.update(host)
            kwargs['loop'] = self.loop

            if 'scheme' in host and host[
                    'scheme'] != self.connection_class.transport_schema:  # noqa
                raise ImproperlyConfigured(
                    'Scheme specified in connection (%s) is not the same as the connection class (%s) specifies (%s).'
                    % (  # noqa
                        host['scheme'],
                        self.connection_class.__name__,
                        self.connection_class.transport_schema,  # noqa
                    ), )
            return self.connection_class(**kwargs)
Esempio n. 6
0
    def __init__(self,
                 host='localhost',
                 port=9200,
                 http_auth=None,
                 use_ssl=False,
                 verify_certs=False,
                 ca_certs=None,
                 client_cert=None,
                 client_key=None,
                 loop=None,
                 use_dns_cache=True,
                 headers=None,
                 ssl_context=None,
                 trace_config=None,
                 **kwargs):
        super().__init__(host=host, port=port, **kwargs)

        self.loop = asyncio.get_event_loop() if loop is None else loop

        if http_auth is not None:
            if isinstance(http_auth, str):
                http_auth = tuple(http_auth.split(':', 1))

            if isinstance(http_auth, (tuple, list)):
                http_auth = aiohttp.BasicAuth(*http_auth)

        headers = headers or {}
        headers.setdefault('content-type', 'application/json')

        # if providing an SSL context, raise error if any other SSL related flag is used
        if ssl_context and (verify_certs or ca_certs):
            raise ImproperlyConfigured(
                "When using `ssl_context`, `use_ssl`, `verify_certs`, `ca_certs` are not permitted"
            )

        if use_ssl or ssl_context:
            cafile = ca_certs
            if not cafile and not ssl_context and verify_certs:
                # If no ca_certs and no sslcontext passed and asking to verify certs
                # raise error
                raise ImproperlyConfigured(
                    "Root certificates are missing for certificate "
                    "validation. Either pass them in using the ca_certs parameter or "
                    "install certifi to use it automatically.")
            if verify_certs or ca_certs:
                warnings.warn(
                    'Use of `verify_certs`, `ca_certs` have been deprecated in favor of using SSLContext`',
                    DeprecationWarning)

            if not ssl_context:
                # if SSLContext hasn't been passed in, create one.
                # need to skip if sslContext isn't avail
                try:
                    ssl_context = create_ssl_context(cafile=cafile)
                except AttributeError:
                    ssl_context = None

                if not verify_certs and ssl_context is not None:
                    ssl_context.check_hostname = False
                    ssl_context.verify_mode = ssl.CERT_NONE
                    warnings.warn(
                        'Connecting to %s using SSL with verify_certs=False is insecure.'
                        % host)
            if ssl_context:
                verify_certs = True
                use_ssl = True

        trace_configs = [trace_config] if trace_config else None
        max_connections = max(256, kwargs.get("max_connections", 0))
        enable_cleanup_closed = kwargs.get("enable_cleanup_closed", False)
        self.session = aiohttp.ClientSession(
            auth=http_auth,
            timeout=self.timeout,
            connector=aiohttp.TCPConnector(
                loop=self.loop,
                verify_ssl=verify_certs,
                use_dns_cache=use_dns_cache,
                ssl_context=ssl_context,
                limit=max_connections,
                enable_cleanup_closed=enable_cleanup_closed),
            headers=headers,
            trace_configs=trace_configs,
            response_class=RawClientResponse)
        self.scheme = "https" if use_ssl else "http"