Elasticsearch:基于文件的用户认证

你可以使用内置文件域(file realm)管理和验证用户。 使用文件域,用户在集群中每个节点上的本地文件中定义。

重要:作为集群的管理员,你有责任确保在集群中的每个节点上定义相同的用户。 Elastic Stack 安全功能不提供任何机制来保证这一点。 你还应该知道,你不能通过 user APIs 在文件域中添加或管理用户,也不能在 Management/Security/Users 页面上的 Kibana 中添加或管理它们。

文件域作为回退(fallback)或恢复(recovery)域非常有用。 例如,在集群无响应或安全索引不可用的情况下,或者当你忘记管理用户的密码时。 在这种情况下,文件域是一种方便的出路 — 你可以在文件领域中定义一个新的管理员用户,并使用它来登录和重置所有其他用户的凭据。

重要:当你在 elasticsearch.yml 中配置域时,只有指定的域用于身份验证。 要使用文件域,你必须明确地将其包含在域链中。 虽然可以定义一些其他域的多个实例,但你只能为每个节点定义一个文件领域。

文件域(file realm)默认已经添加到域链中。 你不需要显式配置文件域

文件领域的所有用户数据都存储在集群中每个节点上的两个文件中:users 和 users_roles。 这两个文件都位于 ES_PATH_CONF 中,并在启动时读取。

$ pwd
/Users/liuxg/elastic/elasticsearch-8.5.2
$ ls config/
certs                             log4j2.properties
elasticsearch-plugins.example.yml role_mapping.yml
elasticsearch.keystore            roles.yml
elasticsearch.yml                 users
jvm.options                       users_roles
jvm.options.d

配置

我们在 config 下的 user_roles 里配置所需要的 roles,比如:

config/roles.yml

admins:
  cluster:
    - all
  indices:
    - names:
        - "*"
      privileges:
        - all


devs:
  cluster:
    - manage
  indices:
    - names:
        - "*"
      privileges:
        - write
        - delete
        - create_index
$ pwd
/Users/liuxg/elastic/elasticsearch-8.5.2
$ cat config/roles.yml 
# The default roles file is empty as the preferred method of defining roles is
# through the API/UI. File based roles are useful in error scenarios when the
# API based roles may not be available.
admins:
  cluster:
    - all
  indices:
    - names:
        - "*"
      privileges:
        - all


devs:
  cluster:
    - manage
  indices:
    - names:
        - "*"
      privileges:
        - write
        - delete
        - create_index

如上所示,我们在 roles.yml 里创建了两个 roles: admins 及 devs。这两个 roles 有不同的权限。admins 是超级用户的权限,而 devs role 具有 write, delete 及 create_index 的权限。

配置完后,我们重新 Elasticsearch。就像文章开头说的那样,我们通过这样的方法创建的 roles 并不能在 Management/Security/Role 中看到:

创建用户

接下来,我们就要使用 elasticsearch-users 这个工具来创建用户。我们使用如下的命令来创建一个用户 liuxg 及其密码 password。它所定义的 roles 为 networking 及 monitoring:

bin/elasticsearch-users useradd liuxg -p password -r network,monitoring
$ pwd
/Users/liuxg/elastic/elasticsearch-8.5.2
$ bin/elasticsearch-users useradd liuxg -p password -r network,monitoring
Warning: The following roles [monitoring,network] are not in the [/Users/liuxg/elastic/elasticsearch-8.5.2/config/roles.yml] file. Make sure the names are correct. If the names are correct and the roles were created using the API please disregard this message. Nonetheless the user will still be associated with all specified roles
Known roles: [apm_system, watcher_admin, viewer, logstash_system, rollup_user, kibana_user, beats_admin, remote_monitoring_agent, rollup_admin, snapshot_user, data_frame_transforms_admin, devs, monitoring_user, enrich_user, kibana_admin, logstash_admin, editor, data_frame_transforms_user, machine_learning_user, machine_learning_admin, watcher_user, apm_user, beats_system, transform_user, reporting_user, kibana_system, transform_admin, remote_monitoring_collector, transport_client, admins, superuser, ingest_admin]

如上所示,它给出了一些警告:monitoring 及 network 并没有在 roles.yml 里被定义。它还列出了可以被使用的 roles 的名称:

尽管如此,它只是一个警告。我接着查看如下的文件:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.5.2
$ cat config/users
liuxg:$2a$10$VJQzQftnxSwvdaxfuLGLx.lX4VGuIfLHV.R38HBySUUr1KJL2hrgW
$ cat config/users
liuxg:$2a$10$VJQzQftnxSwvdaxfuLGLx.lX4VGuIfLHV.R38HBySUUr1KJL2hrgW

我们可以看到上面的内容。liuxg 就是用户名,而后面是用掩码表示的密码。我们再查看 users_roles 这个文件:

cat config/users_roles 
$ cat config/users_roles 
monitoring:liuxg
network:liuxg

它显示了各个角色(role)对应的用户。

我们现在用刚创建的用户 liuxg:password 来访问 Elasticsearch:

curl -k -u liuxg:password https://localhost:9200
$ curl -k -u liuxg:password https://localhost:9200 | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   541  100   541    0     0   5517      0 --:--:-- --:--:-- --:--:--  5755
{
  "error": {
    "root_cause": [
      {
        "type": "security_exception",
        "reason": "action [cluster:monitor/main] is unauthorized for user [liuxg] with effective roles [] (assigned roles [monitoring,network] were not found), this action is granted by the cluster privileges [monitor,manage,all]"
      }
    ],
    "type": "security_exception",
    "reason": "action [cluster:monitor/main] is unauthorized for user [liuxg] with effective roles [] (assigned roles [monitoring,network] were not found), this action is granted by the cluster privileges [monitor,manage,all]"
  },
  "status": 403
}

显然,我们不能进行登录。这是因为用户 liuxg 还没有相应的权限。

我们接下来使用编辑器来编辑 config/users_roles 文件,使其成为:

config/users_roles

$ pwd
/Users/liuxg/elastic/elasticsearch-8.5.2
$ cat config/users_roles 
monitoring:liuxg
network:liuxg
admins:liuxg

在上面,我们为 liuxg 这个用户添加了之前我们在 roles.yml 文件中定义的 admins 权限。这个 admins 是超级用户的权限。我们再次发送请求:

$ curl -k -u liuxg:password https://localhost:9200 | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   535  100   535    0     0  27225      0 --:--:-- --:--:-- --:--:-- 35666
{
  "name": "liuxgm.local",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "NvSlRkrSTaO33lAKdzNcqQ",
  "version": {
    "number": "8.5.2",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "a846182fa16b4ebfcc89aa3c11a11fd5adf3de04",
    "build_date": "2022-11-17T18:56:17.538630285Z",
    "build_snapshot": false,
    "lucene_version": "9.4.1",
    "minimum_wire_compatibility_version": "7.17.0",
    "minimum_index_compatibility_version": "7.0.0"
  },
  "tagline": "You Know, for Search"
}

这次显然我们的访问是成功的。我们的集群有救了。我们为它设置了一个崭新的账号。

我们还可以使用如下的命令来列出来在当前节点里的文件域中的用户:

bin/elasticsearch-users list
$ bin/elasticsearch-users list
liuxg          : monitoring*,network*,admins

 [*]   Role is not in the [/Users/liuxg/elastic/elasticsearch-8.5.2/config/roles.yml] file. If the role has been created using the API, please disregard this message.

我们可以使用如下的命令来重新设置用户的密码:

bin/elasticsearch-users passwd liuxg

上面的命令将为 liuxg 用户重置密码:

$ bin/elasticsearch-users passwd liuxg
Enter new password: 
Retype new password: 

在上面,我们为 liuxg 用户的密码重置为 123456。我们再次使用如下的命令来进行验证:

$ curl -k -u liuxg:123456 https://localhost:9200 | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   535  100   535    0     0   5533      0 --:--:-- --:--:-- --:--:--  5815
{
  "name": "liuxgm.local",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "NvSlRkrSTaO33lAKdzNcqQ",
  "version": {
    "number": "8.5.2",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "a846182fa16b4ebfcc89aa3c11a11fd5adf3de04",
    "build_date": "2022-11-17T18:56:17.538630285Z",
    "build_snapshot": false,
    "lucene_version": "9.4.1",
    "minimum_wire_compatibility_version": "7.17.0",
    "minimum_index_compatibility_version": "7.0.0"
  },
  "tagline": "You Know, for Search"
}

很显然,密码的修改是成功的。

我们甚至可以使用如下的命令来移除不需要的 roles:

bin/elasticsearch-users roles liuxg -r network,monitoring -a user

上面的命令为 liuxg 用户移除 network 及 monitoring 角色,并添加 user 角色。

执行上面的命令后,我们重新检查用户的角色:

$ bin/elasticsearch-users list
liuxg          : user*,admins

显然之前的 network 及 monitoring 已经被移除了。

我们还可以使用如下的命令来删除一个用户:

bin/elasticsearch-users userdel liuxg

上面的命令将删除 liuxg 用户。我们再次使用 list 命令来查看:

$ bin/elasticsearch-users list
No users found

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>