為ElasticSearch添加HTTP基本認證

jopen 9年前發布 | 64K 次閱讀 ElasticSearch 搜索引擎

ES的HTTP連接沒有提供任何的權限控制措施,一旦部署在公共網絡就容易有數據泄露的風險,尤其是加上類似 elasticsearch-head 這樣友好的前端界面,簡直讓你的數據瞬間裸奔在黑客的眼皮底下。項目上線前做十萬伏特的防護當然不現實,但至少,我們不要裸奔,穿一套比基尼吧。而做一個簡單的HTTP認證并不需要從頭造輪子, elasticsearch-http-basic 就提供了針對ES HTTP連接的IP白名單、密碼權限和信任代理功能。

安裝

elasticsearch-http-basic還不支持ES標準的 bin/plugin install [github-name]/[repo-name] 的安裝方式,但作者有提供編譯好的jar包,不需要下載源碼重新編譯。GitHub上目前的最新版本是對應ES的1.4.0版本,但驗證過1.5.2也是同樣可用的。

插件的安裝步驟如下:

  • 從 elasticsearch-http-basic的發布版 下載對應版本的jar包
  • mkdir -p plugins/http-basic; mv elasticsearch-http-basic-x.x.x.jar plugins/http-basic ( 注意文件夾的名稱 )
  • 重啟ES進程
  • 驗證插件是否生效: curl localhost:9200/_nodes/[your-node-name]/plugins?pretty=true (如果看到 plugins 列表包含有 http-basic-server-plugin 就說明插件生效了)

配置

elasticsearch-http-basic和其他ES插件一樣,在 config/elasticsearch.yml 中統一配置:

配置名 默認值 說明
http.basic.enabled true 開關,開啟會接管全部HTTP連接
http.basic.user “admin” 賬號
http.basic.password “admin_pw” 密碼
http.basic.ipwhitelist [“localhost”, “127.0.0.1”] 白名單內的ip訪問不需要通過賬號和密碼,支持ip和主機名,不支持ip區間或正則
http.basic.trusted_proxy_chains [] 信任代理列表
http.basic.log false 把無授權的訪問事件添加到ES的日志
http.basic.xforward “” 記載代理路徑的header字段名

測試

  • Shell

# 無賬號密碼,不可訪問

>>> curl http: / /[your-node-name]:[your-port]/ [your-index]/_count?pretty=true

Authentication Required

# 通過user選項帶上賬號密碼,返回正常數據

>>> curl --user [your-admin] : [your-password] http: / /[your-node-name]:[your-port]/ [your-index]/_count?pretty=true

{

"count" : xxx,

"_shards" : {

"total" : xxx,

"successful" : xxx,

"failed" : 0

}

}

  • 添加了HTTP基本認證后, elasticsearch-head 同樣會彈窗要求你先進行權限認證

Python客戶端

# http_auth選項可以配置賬號密碼

from elasticsearch import Elasticsearch

es = Elasticsearch([ 'localhost' ], http_auth=( 'your-admin' , 'your-password' ), port=...)

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!