当前位置: 首页 > news >正文

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_role 这个文件:

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

相关文章:

  • 【C】带你复习有趣的函数
  • .NET Framework杂记
  • 4线SPI驱动OLED常规操作
  • ESP32 OTA
  • Linux C编程一站式学习笔记2
  • RK3568平台开发系列讲解(摄像头篇)使用 Camera 的步骤
  • Kerberos的概述和认证原理
  • RocketMQ的TAG过滤和SQL过滤机制
  • 2023年电气,电子与信息工程国际会议(ISEEIE 2023)
  • 【前端开发学习】4.JavaScript
  • 【大数据技术Hadoop+Spark】HBase分布式数据库架构、特点、数据存储方式、寻址机制详解(图文解释)
  • K8s——Service、代理模式演示(二)
  • 哈希表及其与Java类集的关系
  • CSS基础总结(二)
  • 《Python多人游戏项目实战》第三节 在窗口上显示玩家ID以及对话内容
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • canvas绘制圆角头像
  • CentOS6 编译安装 redis-3.2.3
  • CSS实用技巧干货
  • egg(89)--egg之redis的发布和订阅
  • E-HPC支持多队列管理和自动伸缩
  • ES6系统学习----从Apollo Client看解构赋值
  • Java 23种设计模式 之单例模式 7种实现方式
  • Java多线程(4):使用线程池执行定时任务
  • jquery ajax学习笔记
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • learning koa2.x
  • passportjs 源码分析
  • SAP云平台里Global Account和Sub Account的关系
  • 区块链技术特点之去中心化特性
  • 通过npm或yarn自动生成vue组件
  • 责任链模式的两种实现
  • Java总结 - String - 这篇请使劲喷我
  • Spring第一个helloWorld
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (C语言)逆序输出字符串
  • (C语言)球球大作战
  • (pojstep1.3.1)1017(构造法模拟)
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)计算机毕业设计SSM智慧停车系统
  • (算法)Game
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .Net core 6.0 升8.0
  • .NET Core 项目指定SDK版本
  • .NET Micro Framework初体验
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • /bin/rm: 参数列表过长"的解决办法
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [ 网络基础篇 ] MAP 迈普交换机常用命令详解
  • [04] Android逐帧动画(一)
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [android] 天气app布局练习