• 为ElasticSearch添加HTTP基本认证 https://github.com/Asquera/elasticsearch-http-basic/releases


    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

    ES官方的Python客户端可以通过http_auth配置账号密码:

     
    from elasticsearch import Elasticsearch
    es = Elasticsearch(['localhost'], http_auth=('your-admin', 'your-password'), port=...)
  • 相关阅读:
    牛客小白月赛21
    牛客小白月赛21
    CodeForces 1333-C Eugene and an array(子区间和为0、前缀和)
    页面大小、页表项、虚拟地址和物理地址之间的关系(转)
    001-Paint_FreePythonGames项目代码详解(每行都有注释!!!)
    第17讲~第19讲:函数:python的乐高积木
    第16讲:序列!序列!
    第15讲:字符串格式化
    练习23--字符串、字节和编码
    第14讲:字符串--各种奇葩内置方法
  • 原文地址:https://www.cnblogs.com/SZLLQ2000/p/6336709.html
Copyright © 2020-2023  润新知