Elasticsearch:使用 IP 过滤器限制连接

你还可以将 IP过滤 应用于应用程序客户端,节点客户端或传输客户端来限制或允许一些 IP 对于 Elasticsearch 的访问。如果节点的IP地址在黑名单中,则 Elasticsearch 安全功能允许连接到 Elasticsearch,但该连接将立即被丢弃,并且不处理任何请求。Elasticsearch 安装并非旨在通过 Internet 公开访问。 IP过滤和 Elasticsearch 安全功能的其他功能不会改变这种情况。在使用这项功能的时候,必须注意的一点是:IP过滤 是金和白金许可的一部分。

在今天的文章中,我们来展示如何使用这些功能。

准备工作

如果你还没有安装自己的 Elasticsearch 及 Kibana 的话,那么请参阅我之前的文章:

整个的安装都非常直接。在这里我就不重复了。整个系统的配置如下:

由于这个功能是 金和白金许可的一部分,那么我们必须在 Kibana 中启动30天试用:

选择 Start my trial:

这样我们就完成了对金和白金许可的试用。

对于那些喜欢使用 API 的用户来说,你也可以参照 _license 终点:

POST /_license/start_trial?acknowledge=true

使用 HTTP filter

为了使用 HTTP filter,我们来修改 Elasticsearch 的配置文件 config/elasticsearch.yml:

config/elasticsearch.yml

xpack.security.http.filter.allow: "192.168.0.4"
xpack.security.http.filter.deny: "192.168.0.0/24"

我们在 Elasticsearch 的配置文件中添加上面的两行配置。重新启动 Elasticsearch。上面的两行表示只允许从 IP 地址 192.168.0.4 来进行访问,但是不支持从任何其它的 IP 地址,比如 192.168.0.0/24 进行访问。

为了验证这个,我们最如下的实验。我们的 Elasticsearch 运行于 IP 地址 192.168.0.3 上。这个可以通过如下的命令来进行查询:

ifconfig | grep 192
$ ifconfig | grep 192
	inet 192.168.0.3 netmask 0xffffff00 broadcast 192.168.0.255

我们在当前 Elasticsearch 运行的电脑上运行:

curl 192.168.0.3:9200 -u elastic:password
$ ifconfig | grep 192
	inet 192.168.0.3 netmask 0xffffff00 broadcast 192.168.0.255
liuxg:~ liuxg$ curl 192.168.0.3:9200 -u elastic:password
curl: (52) Empty reply from server

显然,上面的访问是失败的。我们接下来使用另外一个电脑,它的 IP 地址是:192.168.0.4:

$ ifconfig | grep 192
        inet 192.168.0.4  netmask 255.255.255.0  broadcast 192.168.0.255
liuxg@liuxgu:~$ curl 192.168.0.3:9200 -u elastic:password
{
  "name" : "liuxg",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "PpiZRc6DQyin2reBiEngZg",
  "version" : {
    "number" : "7.8.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65",
    "build_date" : "2020-06-14T19:35:50.234439Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

上面显示在 192.168.0.4 电脑上发送的请求,我们可以得到相应的输出,表明我们的 IP filter 是正确工作的。

在很多的情况下,我们更喜欢通过 API  来进行操作,因为这样更容易维护。否则每次都需要重新编辑 config/elasticsearch.yml 文件,并重新启动 Elasticsearch。

刚才在上面,我们可以看到,在安装 Elasticsearch 的电脑上,我们并不能访问 Elasticsearch,这是因为 192.168.0.3 这个 IP地址被禁止了。我们在下面通过 API 的方式来重新打开这个 IP 地址的访问。到目前为止,我们的 Kibana 也不能访问。

curl -u elastic:password -XPUT 'http://192.168.0.3:9200/_cluster/settings?pretty=true' -H 'Content-Type: application/json' -d '
{
    "persistent": {
    "xpack.security.http.filter.deny": "_all",
    "xpack.security.http.filter.allow": ["localhost"]
  }
}'

在上面,我们允许 localhost 这个 IP 地址的访问,并且屏蔽其他 IP 地址的访问。在上面,我们必须在目前允许的 IP 地址 192.168.0.4 的机器上运行上面的命令,因为只有这台机器是可以被访问的。运行的结果是:

经过上面的执行后,我们重新在 IP 地址为 192.168.0.4 上的机器上重新运行如下的命令:

显然目前的这台机器的访问已经被阻止了。

我们回到 Elasticsearch 运行的机器上,并使用 localhost 来进行访问:

上面显示,我们针对 localhost 的访问是成功的。这正说明了我们的 API 的使用是成功的。它可以动态地为我们做 http filter.

使用 IP filter

按照同样的道理,我们可以针对 transport 来做 IP filter,只不过我们使用如下的一对设置:

xpack.security.transport.filter.allow: "192.168.0.1"
xpack.security.transport.filter.deny: "192.168.0.0/24"

使用这个可以有效地阻止一些不相关的节点加入我们的集群。详细的情况,我这里就不做累述了。请大家自己去尝试。

参考:

【1】https://www.elastic.co/guide/en/elasticsearch/reference/current/ip-filtering.html#_enabling_ip_filtering

猜你喜欢

转载自blog.csdn.net/UbuntuTouch/article/details/107154165