[英]How to connect to Elasticsearch with python using 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=context, http_auth=('elastic','yourpassword'))
來自: https://github.com/elastic/elasticsearch-py
我需要提供cafile.pem
和http_auth
參數。 在運行我的 Python 的服務器上,已經建立了 SSL 連接,因此我可以對 Elasticsearch 進行基本查詢。它是使用~/.ssh
目錄中的鍵設置的: id_rsa
, id_rsa.pub
。
所以,現在我想知道我是否應該提供id_rsa.pub
密鑰代替path/to/cafile.pem
,如果是,那么我需要更改~/.ssh
文件夾的權限,這似乎不是一個好主意來自安全視角。
然后,我不確定.pub
是否與.pem
相同,是否需要先轉換它? 那么,是否應該省略http_auth
,因為我在從終端進行簡單查詢時不使用任何密碼?
go 在Python 中根據最佳實踐設置對ES 和SSL 的訪問權限問題,我應該如何解決?
我嘗試了.pub
並從中生成了pem
: https://serverfault.com/questions/706336/how-to-get-a-pem-file-from-ssh-key-pair
但是兩者都未能create_default_context
,並在context.load_verify_locations(cafile, capath, cadata)
中出現unknown error
。
我的特殊情況的答案非常簡單。 我在這里找到了它:
https://elasticsearch-py.readthedocs.io/en/master/
es = Elasticsearch(['https://user:secret@localhost:443'])
剛剛在里面指定https
url
,它馬上就解決了。
Elasticsearch Docker 圖像和 Python2.7。 已將 ssl 證書文件復制到項目的根目錄。 確保它是可讀的,所有權和組所有權將允許讀取訪問。 將 pass 和 login 放入常量。
es = Elasticsearch(
hosts=[
"https://localhost:9200"
],
http_auth=(USR_LOGIN, USR_PASS),
use_ssl=True,
verify_certs=True,
ca_certs="./http_ca.crt",
)
對於自簽名證書,使用:
from elastic_transport import NodeConfig
from elasticsearch import AsyncElasticsearch
client = AsyncElasticsearch(
hosts=[
NodeConfig(
scheme= "https",
host="<host URL>",
port=443,
verify_certs=False,
ca_certs=None,
ssl_show_warn=False,
)
],
http_auth=("username", "password"),
verify_certs=False,
ca_certs="/path/to/cafile.pem", # PEM format
client_cert="/path/to/tls.cert" # PEM format
client_key="/path/to/tls.key" # PEM format
)
client.info()
解釋:
verify_certs=False
禁用底層 Python SSL 模塊驗證自簽名證書,但正確地將其向上游發送到服務器。 對於非自簽名證書,您應該嘗試啟用verify_certs=True
。AsyncElasticsearch
,但是如果你需要 sync Elasticsearch
版本,應該直接兼容,因為所有參數都是一樣的。 請參閱: https://elasticsearch-py.readthedocs.io/en/v8.8.1/async.html#getting-started-with-async )所以,現在我想知道我是否應該提供 id_rsa.pub 密鑰來代替 path/to/cafile.pem,如果是,那么我需要更改 ~/.ssh 文件夾的權限,這似乎不是一個好主意來自安全視角。
這些 SSH 密鑰很可能與 Elasticsearch 無關,但允許您連接並驗證運行 Elasticsearch 的服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.