Elasticsearch Cannot Connect with Python Client, Ssl Error

Elasticsearch Cannot Connect with Python Client, Ssl Error

I am hosting standalone elasticsearch on external server. Using certificate and login-password authentication. I am able to connect to it using browser or postman.

However when I try to do it, using python client it doesn't work. My connection code:

        self.es = Elasticsearch(
            address,
            ca_certs=cert_path,
            basic_auth=(user, password),
        )

Error message:

elastic_transport.TlsError: TLS error caused by: TlsError(TLS error caused by: SSLError(hostname '34.116.***.***' doesn't match either of 'localhost', '172.19.0.2', '127.0.0.1', 'bde723133f75'))

2 Answers

Please try creating an ssl_context object and set the verification mode on the context.

import ssl
from elasticsearch.connection import create_ssl_context

ssl_context = create_ssl_context(<use `cafile`, or `cadata` or `capath` to set your CA or CAs)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
es = Elasticsearch('localhost', ssl_context=context, timeout=60)

or you can try with this:

from ssl import create_default_context
from elasticsearch import Elasticsearch
es = Elasticsearch([""], verify_certs=False)
es.cat.nodes()

Option 2 will be warning, because it disable SSL with link url using HTTPS, but it work.

2

Using some help from Luc answer: Just before i tried something like this:

from ssl import create_default_context
import ssl
        context = create_default_context(capath=cert_path)
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        self.es = Elasticsearch(
            address,
            ssl_context=context,
            basic_auth=(user, password),
            verify_certs=False,
        )

I am getting some warnings but other then that it works

Your Answer

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

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.