djangoelasticsearchdjango-haystackhttp-authenticationpyelasticsearch

Django Haystack connection error using http authentication


I'm using Haystack to connect and interact with an installation of elasticsearch. Elasticsearch is installed on a different box to the main webserver.

I have set up HTTP authentication on the elasticsearch box using nginx. This is to stop unauthorised access to elasticsearch.

The Haystack config looks like this:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://USERNAME:PASSWORD@DOMAIN:PORT/',
        'INDEX_NAME': 'haystack',
    },
}

With this set up I get a connection error:

elasticsearch.exceptions.ConnectionError: ConnectionError(('Connection aborted.',
gaierror(-2, 'Name or service not known'))) caused by: ProtocolError(('Connection
aborted.', gaierror(-2, 'Name or service not known')))

If I turn off HTTP authentication and update the URL correspondingly to http://DOMAIN:PORT/ it connects without a problem.

Could it be that Haystack (or elasticsearch-py (http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/) doesn't allow HTTP authentication to be used in the URL? I notice this is a problem with Solr - Solr authentication (using Django Haystack)


Solution

  • For others with the same problem, add kwargs to config:

    'KWARGS': {
        'port': parsed.port,
        'http_auth': (parsed.username, parsed.password),
    }
    

    See: https://github.com/django-haystack/django-haystack/issues/1046