使用SSL通过python连接到Elasticsearch
我正在尝试使用使用SSL通过python连接到Elasticsearch,python,
elasticsearch,ssl,Python,
elasticsearch,Ssl,我正在尝试使用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=
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'))
发件人:
我需要提供cafile.pem
和http\u auth
参数。在运行我的python
的服务器上,SSL连接已经建立,因此我可以对Elasticsearch
进行基本查询。它是使用~/.ssh
目录中的密钥设置的:id\u rsa
,id\u rsa.pub
。因此,现在我想知道是否应该提供id\u rsa.pub
密钥来代替path/to/cafile.pem
,如果是,那么我需要更改~/.ssh
文件夹的权限,从安全角度看,这似乎不是一个好主意。那么,我不确定.pub
是否与.pem
相同,是否需要先转换它?那么,也应该省略http_auth
,因为我在终端中进行简单查询时不使用任何密码?我应该如何根据最佳实践解决这个问题(使用SSL
在python中设置对ES
的访问),这就是问题所在
更新
我尝试了.pub
,并从中生成了pem
:
但两者都未能创建默认上下文,在
上下文中出现了一个未知错误
。加载\u验证位置(cafile、capath、cadata)
对于我的特殊情况,答案非常简单。我在这里找到的:
刚刚在内部指定了
https
url
,它马上就解决了。您缺少了一些东西,id\u rsa
,id\u rsa.pub
是您的ssh密钥,您的用户可以使用它连接到您的服务器,而无需提供密码,要使用SSL与elasticsearch建立安全连接,您需要在群集中对其进行配置,并使用在中生成的证书。您可以使用在群集中配置SSL,这很简单,但在elasticsearch中从HTTP切换到HTTPS时会有所帮助。
es = Elasticsearch(['https://user:secret@localhost:443'])