python - 使用 SSL 使用 python 连接到 Elasticsearch

标签 python elasticsearch ssl

我正在尝试连接到 Elasticsearch来自 pythonSSL并使用基本代码:

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'))

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

我需要供应cafile.pem , 和 http_auth参数。在我的python所在的服务器上正在运行 SSL 连接已设置,因此我可以对 Elasticsearch 进行基本查询.它是使用 ~/.ssh 中的 key 设置的目录:id_rsa , id_rsa.pub .所以,现在我想知道我是否应该提供 id_rsa.pub代替 path/to/cafile.pem 的 key ,如果是,那么我需要更改 ~/.ssh 的权限从安全角度来看,这似乎不是一个好主意。然后,我不确定 .pub.pem 相同,我需要先转换它吗?那么,也应该http_auth只是被省略,因为我在终端中进行简单查询时不使用任何密码?我应该如何根据最佳实践解决这个问题(在 python 中设置对 ES 的访问权限 SSL ) - 这就是问题所在。

Update



我都试过了 .pub并从中生成 pem :

https://serverfault.com/questions/706336/how-to-get-a-pem-file-from-ssh-key-pair

但两者都失败了create_default_contextunknown errorcontext.load_verify_locations(cafile, capath, cadata) .

最佳答案

我的特定案例的答案非常简单。我在这里找到了:

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

es = Elasticsearch(['https://user:secret@localhost:443'])

刚刚指定 https url在里面,它马上就解决了。

关于python - 使用 SSL 使用 python 连接到 Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61961725/

相关文章:

python - 在 Pandas 中部分拆分字符串列

Python os.path.join Linux 上的绝对路径

c# - 查询Elastic Search时如何添加条件表达式?

elasticsearch - 如何使用 ElasticSearch 在字符串字段中搜索精确短语?

asp.net-mvc - 显示 http 代替 https

python - 如何手动计算ROC的AUC?

python - 为什么这个函数装饰器失败了?

elasticsearch - 使用Joda时间解析ZonedDateTime#toString()时失败

java - Apache 目录 API 拒绝将用户添加到 Active Directory "(UNWILLING_TO_PERFORM)"

ssl - 错误 SSL NET::ERR_CERT_DATE_INVALID 即使 SSL 尚未过期