elastic_transport.TlsError: TLS error caused by:TlsError(TLS error caused by: SSLError([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)))
Asked Answered
M

4

9

Getting this error Trying to connect elasticsearch docker container with elasticsearch-python client.

    /home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elasticsearch/_sync/client/__init__.py:379: SecurityWarning: Connecting to 'https://localhost:9200' using TLS with verify_certs=False is insecure
  **transport_kwargs,
<Elasticsearch(['https://localhost:9200'])>
Traceback (most recent call last):
  File "test_all.py", line 29, in <module>
    resp = es.index(index="test-index", id=1, document=doc)
  File "/home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elasticsearch/_sync/client/utils.py", line 404, in wrapped
    return api(*args, **kwargs)
  File "/home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elasticsearch/_sync/client/__init__.py", line 2218, in index
    __method, __path, params=__query, headers=__headers, body=__body
  File "/home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elasticsearch/_sync/client/_base.py", line 295, in perform_request
    client_meta=self._client_meta,
  File "/home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elastic_transport/_transport.py", line 334, in perform_request
    request_timeout=request_timeout,
  File "/home/raihan/dev/aims_lab/ai_receptionist/env/lib/python3.6/site-packages/elastic_transport/_node/_http_urllib3.py", line 199, in perform_request
    raise err from None
elastic_transport.TlsError: TLS error caused by: TlsError(TLS error caused by: SSLError([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)))

contents in elastic.py

    host = "https://localhost:9200"
es = Elasticsearch(host, ca_certs=False, verify_certs=False)
print(es)

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])

resp = es.get(index="test-index", id=1)
print(resp['_source'])

contents in elasticsearch dockerfile

FROM docker.elastic.co/elasticsearch/elasticsearch:7.12.0
RUN elasticsearch-plugin install --batch https://github.com/alexklibisz/elastiknn/releases/download/7.12.0.0/elastiknn-7.12.0.0.zip

urllib3==1.26.9 requests==2.27.1

Manlike answered 9/4, 2022 at 6:57 Comment(1)
Hope you are accessing the Elasticsearch url with authentication. Hence mention it with credentials url in host variable like this "user:pass@localhost:9200"Solvolysis
M
19
#disable certificate
es = Elasticsearch(hosts="https://localhost:9200", basic_auth=(USER, PASS), verify_certs=False)

#if getting an issue relevant to the certificate then:
es = Elasticsearch(hosts="https://localhost:9200", basic_auth=(USER, PASS), ca_certs=CERTIFICATE, verify_certs=False)

# I hope you know where to find certificate, e.g:
$ find /usr/share/elasticsearch -name "certs.pem"

Reference Follow for more

Murvyn answered 8/8, 2022 at 12:36 Comment(1)
Can you explain why it should make a difference to add ca_certs= with the verify_certs=False already present? On my end it does not make a difference.Heterogony
A
10

you should will "https" change "http" ,this is answer

Alaster answered 11/7, 2022 at 6:12 Comment(1)
Recent versions of official elasticsearch docker image force the use of https. Changing to http results in ConnectionErrorSheerlegs
A
4

You must check if the scheme is correctly set, i.e. if https is written instead of http for localhost where elastic is running on latter, then this error is produced.

The code below raises a TLS error if elastic is running only on http:

es = Elasticsearch([{'host': 'localhost', 'port':9200, 'scheme':'https'}])

The correct way to fix this should be to reconfigure TLS to listen at both https as well as http or to simply use the correct scheme name on which the elastic node is currently running.

The correction code should be:

es = Elasticsearch([{'host': 'localhost', 'port':9200, 'scheme':'http'}])
Antiperistalsis answered 19/7, 2022 at 3:44 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fabri
B
0

Before connecting to elasticsearch.

    connection_params["verify_certs"] = False
    es_client = elasticsearch.Elasticsearch(
        **connection_params,
        headers={"user-agent": ElasticsearchStore.get_user_agent()},
    )
Berar answered 20/12, 2023 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.