如何使用 python 使用 SSL 连接到 Elasticsearch?

Nik*_*nko 7 python ssl elasticsearch

我正在尝试使用 SSL 从 Python 连接到 Elasticsearch 节点。

我正在使用基本代码:

from elasticsearch import Elasticsearch
from ssl import create_default_context

context = create_default_context(cafile="path/to/cafile.pem")
es = Elasticsearch("https://elasticsearch.url:port", ssl_context=context, http_auth=('elastic','yourpassword'))
Run Code Online (Sandbox Code Playgroud)

来自: https: //github.com/elastic/elasticsearch-py

我需要提供cafile.pem, 和http_auth参数。在运行 Python 的服务器上,已经设置了 SSL 连接,因此我可以对 Elasticsearch 进行基本查询。它是使用目录中的键设置的~/.sshid_rsa, id_rsa.pub

所以,现在我想知道是否应该提供id_rsa.pub密钥来代替path/to/cafile.pem,如果是,那么我需要更改~/.ssh文件夹的权限,从安全角度来看这似乎不是一个好主意。

那么,我不确定这.pub是否与 相同.pem,我需要先将其转换吗?那么,是否应该http_auth省略,因为当我从终端进行简单查询时不使用任何密码?

我应该如何根据最佳实践解决在 Python 中使用 SSL 设置对 ES 的访问的问题?

我尝试了两者.pub并从中生成pemhttps ://serverfault.com/questions/706336/how-to-get-a-pem-file-from-ssh-key-pair

但两者都未能create_default_context使用unknown errorin context.load_verify_locations(cafile, capath, cadata)

Nik*_*nko 7

我的具体案例的答案非常简单。我在这里找到了它:

https://elasticsearch-py.readthedocs.io/en/master/

es = Elasticsearch(['https://user:secret@localhost:443'])
Run Code Online (Sandbox Code Playgroud)

只需在内部指定https url即可立即解决。