原创

Elasticsearch 通过 jetty 实现权限控制

Elastic Search 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。支持通过HTTP使用JSON进行数据索引。默认elasticsearch是使用netty作为http的容器,由于netty并没有权限模块,所以默认es没有任何的权限控制,直接通过http就可以进行任何操作,除非把http禁用。但如果你使用elasticsearch-jetty插件,就可以使用jetty自带的权限管理进行权限控制,同时也支持通过https协议来访问es以及支持gzip压缩响应信息。本人参考了很多网络资料总结如下。

插件参数,具体如下:

参数名描述默认那里用到
config设置jetty的配置文件,可以设置多个jetty的配置文件jetty.xmlplugin
server_idJetty服务的IDESServerplugin
portJetty http请求监听的端口http.port或9200-9300jetty.xml
bind_host

Jetty绑定的地址

http.bind_host 或http.hostjetty.xml
publish_host

Jetty对外发布的地址

http.publish_host或http.hostjetty.xml
ssl_portJetty SSL连接器监听的端口
jetty-ssl.xml 和jetty-strong-ssl.xml
ssl_bind_hostJetty SSL连接器绑定的地址
jetty-ssl.xml 和jetty-strong-ssl.xml
keystore_passwordSSL连接器的keystore的密码. 纯文本类型的密码可以使用。经过哈希加密过的密码不支持。
jetty-ssl.xml 和jetty-stro

1.安装

改插件地址:https://github.com/sonian/elasticsearch-jetty

使用0.90.0BETA1版本的es,这个插件目前是支持0.20.2版本的es,新版本的es有些方法名变了,把源码下下来,把变了名的方法改过来从新编译打包就行。

打包完后把project home/target/release里面的压缩文件放到es的plugins目录解压,

修改es配置文件elasticsearch.yml,添加

 http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule

把项目根目录下的config文件夹里面的文件(除了elasticsearch.yml和logging.yml)复制到es的config目录下。

启动es。

 

2.验证插件是否安装成功

es控制台出现一下字样:

[2015-05-11 09:35:16,658][INFO ][org.eclipse.jetty.server.Server] [Songbird] jet
ty-8.1.4.v20120524
[2015-05-11 09:35:16,845][INFO ][org.eclipse.jetty.server.AbstractConnector] [So
ngbird] Started SelectChannelConnector@0.0.0.0:9200

随便发送一个请求,如$ curl -I "http://localhost:9200/"

查看响应头,如果包含Server: Jetty(8.1.4.v20120524)就表示安装成功。

HTTP/1.1 200 OK
Content-Type: text/plain;charset=UTF-8
Access-Control-Allow-Origin: *
Content-Length: 0
Server: Jetty(8.1.4.v20120524)

 

3.配置ssl使其支持https请求

配置在原有基础上加上

sonian.elasticsearch.http.jetty:
    config: jetty.xml,jetty-ssl.xml
    ssl_port: 9443
    keystore_password: "OBF:1nc01vuz1w8f1w1c1rbu1rac1w261w9b1vub1ndq"

 

4.添加基本权限控制

修改es配置文件,添加jetty-hash-auth.xml,jetty-restrict-writes.xml两个文件到config中

http.type: com.sonian.elasticsearch.http.jetty.JettyHttpServerTransportModule
sonian.elasticsearch.http.jetty:
    config: jetty.xml,jetty-hash-auth.xml,jetty-restrict-writes.xml

其中jetty-hash-auth.xml文件里面引用到realm.properties这个文件配置的权限信息。

格式如下:

username: password[,rolename ...]

即:用户名:密码[,角色1,角色2...]

可以看看例子:

superuser: Adm1n,admin,readwrite

user: Passw0rd,readwrite

例如第二个就是用户名为user,密码为Passw0rd,角色为readwrite,即有读写的权限。这个权限角色是在jetty-restrict-writes.xml里面设置的。

jetty-restrict-writes.xml这个文件对es集群的写操作进行了限制,即要通过鉴权才能进行写操作。具体设置可以查看这个文件。

 

这时重新启动es,对es进行写入或删除删除操作,如果发现报403错误,就表示配置成功。

这时就必须在请求头加上权限验证,格式如下:

参数名:Authorization 

值:Basic + Base64(用户名:密码)    即对用户名和密码进行Base64加密

如加密后的值为:Basic dXNlcjpQYXNzdzByZA==

curl方式调用

curl -v --user username:password http://localhost:9200

5.日志记录请求

设置es,把http.type设置成FilterHttpServerTransportModule

http.type: com.sonian.elasticsearch.http.filter.FilterHttpServerTransportModule

添加详细设置,该插件可以自由控制不同请求url的日志级别,如一些不是很重要的的请求(如查询集群健康和节点状态),我们把它设置成trace状态,如果是搜索或统计这些重要的查询,我们可以设置记录它的请求的body信息,配置如下。

sonian.elasticsearch.http.filter:
    http_filter_chain: ["logging"]
    http_filter:
# Request logging filter
logging:
    logger: request
    format: text
    type: com.sonian.elasticsearch.http.filter.logging.LoggingFilterHttpServerAdapter
    level: INFO
    log_body: false
    loggers:
stats:
    path: ["/_cluster/health", "/_cluster/nodes", "/_cluster/state", "/_cluster/nodes/{node}/stats"]
    method: GET
    level: TRACE
searches:
    path: ["/_search", "/_search/scroll", "/_search/scroll/{scroll_id}", "/{index}/_search",
    "/{index}/{type}/_search", "/{index}/{type}/{id}/_mlt"]
    method: GET, POST
    log_body: true
count:
    path: ["/_count", "/{index}/_count", "/{index}/{type}/_count"]
    method: GET, POST
    log_body: true

这样设置的话日志都是写进一个文件里,如果想每天对文件进行拆分,可以修改config里面的logging.yml文件,添加如下内容:

logger:
  ........
  request: INFO, request_log_file
 
additivity:
  request: false
 
appender:
  .........
  request_log_file:
      type: dailyRollingFile
      file: ${path.logs}/${cluster.name}_requests.log
      datePattern: "'.'yyyy-MM-dd"
      layout:
type: pattern
conversionPattern: "[%d{ABSOLUTE}] %m%n"

 

6.开启GZip压缩响应信息,把jetty-gzip.xml加到config参数即可。

~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

分享:

支付宝

微信