Search code examples
pythonelasticsearchherokuhttpsbonsai-elasticsearch

Accesing elasticsearch on Heroku Bonsai from my computer


I'm trying to ping my Elasticsearch instance (Deployed through Bonsai an Heroku add on). I have followed their guidelines and try to execute the following code on my computer:

from elasticsearch import Elasticsearch
from settings import BONSAI_URL
import re, logging

# Log transport details (optional):
logging.basicConfig(level=logging.INFO)

# Parse the auth and host from env:
bonsai = BONSAI_URL
print(bonsai)
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':')
host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '')

# Connect to cluster over SSL using auth for best security:
es_header = [{
  'host': host,
  'port': 443,
  'use_ssl': True,
  'http_auth': (auth[0],auth[1])
}]

# Instantiate the new Elasticsearch connection:
es = Elasticsearch(es_header)

# Verify that Python can talk to Bonsai (optional):
es.ping()

I've got the following error message:

elasticsearch.exceptions.ImproperlyConfigured: Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to use it automatically.

I believe this error is coming from the fact that I don't have a https certificates so I used HTTP, by removing the s in the URL and the regex and switching the use_ssl to False but I got the following error:

urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

How can I insert data from my computer into elasticsearch on Heroku?


Solution

  • You are probably using Python3. The problem is about your python version and the way urlib behave.

    A quick fix could be:

    es_header = [{
    'host': host,
    'port': 443,
    'use_ssl': True,
    'http_auth': (auth[0],auth[1]),
    'verify_certs': False
    }]
    

    But this way is not secure. A more definitive fix could be to write down in your requirements.txt:

    certifi
    

    Type in your terminal:

    pip install -r requirements.txt
    

    In your file where you are instanciating elasticsearch:

    import certifi
    

    Then launch the exact same code that you launched before and it should work and will be secure.