爲ElasticSearch添加HTTP基本認證

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也是一樣可用的。node

插件的安裝步驟以下:python

  • 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中統一配置:git

配置名 默認值 說明
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

ES官方的Python客戶端能夠經過http_auth配置帳號密碼:github

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

來自:建造者說網絡

相關文章
相關標籤/搜索