ElasticSearch web端口安全访问问题,测试了以下方案:
防火墙限制访问ip | 生产上可用,开发环境需要不停修改ip,不可行 |
---|---|
nginx反向代理 | 最终使用了该方案 |
x-pack插件 | 1个月试用期,license过期后免费续费但鉴权功能不可用【巨坑】 |
http_basic插件 | 仅支持到es1.5.1版本,实测es5.x版本不可用 |
search_guard插件 | 安装后9300端口也需要基于SSL协议访问了,由于我们有应用用到该端口,需要修改应用,不可行 |
使用x-pack插件
安装插件
elasitcsearch目录下
bin/elasticsearch-plugin install x-pack
添加配置
elasitcsearch目录下,vi config/elasticsearch.yml
cluster.routing.allocation.disk.threshold_enabled: false
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
xpack.security.enabled: true
重启
重启es后生效,xpack默认账户为elastic,默认密码为changeme。
卸载过期的x-pack并使用nginx反向代理做basic auth
卸载过期的x-pack
elasitcsearch目录下
bin/elasticsearch-plugin remove x-pack
修改web端口配置
elasitcsearch目录下, vi config/elasticsearch.yml
#network.host: 172.31.197.24
# tcp(9300)端口绑定的ip为对外的ip,不做改变
transport.bind_host: 172.31.197.24
# web端口绑定的ip为localhost,只允许本机访问
network.bind_host: 127.0.0.1
# web端口修改为9201,9200由nginx转发
#http.port: 9200
http.port: 9201
# 删除掉x-pack的相关配置
##cluster.routing.allocation.disk.threshold_enabled: false
##http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
##xpack.security.enabled: true
配置nginx服务器
本机nginx配置中添加:
server {
listen 9200;
# 服务器对外的ip
server_name 172.31.197.24;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
auth_basic "login";
# 存放账号密码的文件
auth_basic_user_file /etc/nginx/pass/es;
autoindex on;
proxy_pass http://localhost:9201/;
}
}
其中,存放账号密码的文件/etc/nginx/pass/es
需要手动创建。
访问https://www.sojson.com/htpasswd.html,输入用户名和密码,将加密算法修改为Crypt,点击生成按钮,将结果内容填入该文件即可。可分行填写多个用户。
重启
依次重启es和ngxin,可通过wget localhost:9201/_cat
和访问页面http://ip:9200/_cat
对两个组件分别进行测试。
使用Search Guard插件
安装插件
elasitcsearch目录下
bin/elasticsearch-plugin install com.floragunn:search-guard-5:5.6.3-19.1
下载完成后,进入plugin/search_guard5/bin
目录,执行install_demo.sh。
添加jar包
如果运行环境JRE为java8以上,则需要找一个javax.xml.bind.jar放到es的lib目录下,否则会报错java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
启动es并初始化
重启es,并执行plugin/search_guard5/bin
目录下的sg_admin_demo.sh,如果报错访问不到172.0.0.1:9300端口,则需要编辑sg_admin_demo.sh在最后加上参数 -h 本机ip
。执行完成后9200和9300都需要鉴权后访问。
防火墙的设定方法:
修改配置
CentOS默认防火墙是firewalld ,修改配置文件vi /etc/firewalld/zones/public.xml
:
<!--
开放除9200外所有端口,对9200只允许指定ip访问
-->
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<port protocol="tcp" port="1-9199"/>
<port protocol="tcp" port="9201-65535"/>
<port protocol="udp" port="1-9199"/>
<port protocol="udp" port="9201-65535"/>
<rule family="ipv4">
<source address="10.5.119.16"/>
<port protocol="tcp" port="9200"/>
<accept/>
</rule>
</zone>
启动防火墙
启动firewall并设置开机启动:
systemctl start firewalld
systemctl enable firewalld