5

My Django application uses elasticsearch to index several ressources. Now I wanted to protect my elasticsearch instance with as password which is working fine if I use "curl -u" or so. Anyways from the elasticsearch_dsl documentation, found here: https://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html, I do not understand what I have to do in order to setup elasticsearch that way that it uses a password for authentication and where do I have to place this code?! is smb. maybe able to pass show me some snippets of his configuration?

My current state looks like this:

settingy.py

ELASTICSEARCH_DSL = {
    'default': {
        'hosts': env.str('ELASTICSEARCH_HOST') + str(':') + env.str('ELASTICSEARCH_PORT'),
    },
}

ELASTICSEARCH_DSL_SIGNAL_PROCESSOR = 'django_elasticsearch_dsl.signals.RealTimeSignalProcessor'

documents.py

from django_elasticsearch_dsl import Document, Index, fields
from elasticsearch_dsl import analyzer
from App.models import Post


# elasticsearch index
posts = Index('posts')

html_strip = analyzer(
    'html_strip',
    tokenizer="standard",
    filter=["lowercase", "stop", "snowball"],
    char_filter=["html_strip"]
)


@posts.document
class PostDocument(Document):

... more index stuff

According to the docs, I have to manually setup a default client connection where I can also pass the password and username for authentication which to me seems not to be possible at settings.py at moment.

Kind regards

1 Answer 1

8
+50

You can pass the elasticsearch URL as

from urllib.parse import quote_plus as urlquote

elk_base_url = 'elasticsearch://{user_name}:{password}@{host_ip}:{host_port}'
elastic_search_url = elk_base_url.format(user_name='my_username',
                                         password=urlquote('mysecret_password'),
                                         # password may contain special characters
                                         host_ip='my-elastic-host-ip',
                                         host_port=9200)
ELASTICSEARCH_DSL = {
    'default': {
        'hosts': [elastic_search_url]
    },
}

This solution has been tested under the following circumstances

  1. Django==3.0.4
  2. django-elasticsearch-dsl==7.1.1
  3. logstash == kibana == elasticsearch == 7.6.0

If you are experiencing AuthenticationException(401, '') exception, it means you were provide the wrong credentials. Please do check the value of elastic_search_url and make sure the values are correct.

Sign up to request clarification or add additional context in comments.

10 Comments

Sadly this also results in: File "/venv/lib/python3.8/site-packages/elasticsearch/connection/base.py", line 243, in _raise_error raise HTTP_EXCEPTIONS.get(status_code, TransportError)( elasticsearch.exceptions.AuthenticationException: AuthenticationException(401, '')
Currently im doing: elastic_connection = 'http://{user_name}:{password}@{host_ip}:{host_port}'.format(user_name=env.str('ELASTICSEARCH_USERNAME'), password=env.str('ELASTICSEARCH_PASSWORD'), host_ip=env.str('ELASTICSEARCH_HOST'), host_port=env.str('ELASTICSEARCH_PORT')), also tried elasticsearch:// with no effect
Can you specify the versions of Django, elasticsearch_dsl and django_elasticsearch_dsl ??
It shouldn't be http or https
After a lot of debugging the github comminuty and I have found a working solution: github.com/elastic/elasticsearch-dsl-py/issues/1338 Again, thx everybody.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.