Elasticsearch学习笔记(八) Metricbeat安全配置—通过API连接Elasticsearch

在前面的教程中,我们配置了Metricbeat的数字证书,但是在配置文件中使用了elastic的内置超级用户elastic并且在配置文件中硬编码了用户名和密码,这样做是有一定的安全风险的,为了解决这个问题,需要在kibana中为metricbeat新建一个权限用户,然后利用API的方式实现Metricbeat和kibana之间的通信。

1.新建用户

登录kibana的管理页面,按照下面的步骤新建一个发布用户。

1.1 为新用户创建角色

Stack Management > Roles > Create role

Stack管理->安全->角色

复制代码
Role name: metricbeat-user
Cluster privileges: monitor read_ilm manage_index_templates
Indices: metricbeat-*
Privileges: create_doc

通过上面的操作新建了一个用户角色:metricbeat-user,它的集群权限为monitor、read_ilm和manage_index_templates,对应的索引是metricbeat用户的权限是create_doc。

⚠️ 在创建角色时应当本着最小权限原则,否则就失去了利用角色控制安全的作用了。

⚠️ 集群权限如果缺少manage_index_templates时会报错。

1.2 创建用户

创建用户

👿 用户权限这里除了要为用户赋1.1中新创建的角色,还要再加上editor角色权限。

2.生成API

使用kibana的开发工具,使用下面的命令生成API。

复制代码
# 生成API 
POST /_security/api_key/grant
{
  "grant_type": "password",
  "username": "metric",
  "password": "Yourpassword",
  "api_key": {
    "name": "metric"
  }
}

💁‍♂ 注意不要照抄我的命令,username和password根据实际进行填写。

生成的API先保存起来,后面还会用到。

复制代码
{
  "id": "Hi22rJIBZeCTcJCiOY1O",
  "name": "metric",
  "api_key": "zOvXXaWdQISYa5kvRNoPSw",
  "encoded": "SGkyMnJKSUJaZUNUY0pDaU9ZMU86ek92WFhhV2RRSVNZYTVrdlJOb1BTdw=="
}

3.添加API到keystore

接下来把上面生成的API添加到keystore中。这里需要用到的是:<api_key>。

以我上面的API为例就是:Hi22rJIBZeCTcJCiOY1O:zOvXXaWdQISYa5kvRNoPSw

使用下面的命令将:<api_key>添加到keystore中。

复制代码
/usr/share/metricbeat/bin/metricbeat keystore add ES_API_KEY -c /etc/metricbeat/metricbeat.yml --path.home /usr/share/metricbeat/ --path.data /var/lib/metricbeat

📓 ES_API_KEY是你自定义的key name请根据实际情况命名。

📓 执行上条命令时,输入等待是不显字符的,要小心操作,防止重复输入:<api_key>。如果感觉输入有误可以使用下面的命令删除keystore

复制代码
/usr/share/metricbeat/bin/metricbeat keystore remove ES_API_KEY -c /etc/metricbeat/metricbeat.yml --path.home /usr/share/metricbeat/ --path.data /var/lib/metricbeat

4.修改metricbeat配置文件

复制代码
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["https://192.168.50.7:9200"]
  ssl.certificate_authorities: ["/etc/metricbeat/certs/ca.crt"]
  ssl.certificate: "/etc/metricbeat/certs/elastic.crt"
  ssl.key: "/etc/metricbeat/certs/elastic.key"
  protocol: "https"
  api_key: "${ES_API_KEY}"

这里就使用${ES_API_KEY}代替了前面的硬编码,安全性得到了提升。

⚠️ 我使用了一个新的节点,这里的证书配置请参考我前面的教程。

使用下面的命令检查配置文件是否正确。

复制代码
/usr/share/metricbeat/bin/metricbeat test config -c /etc/metricbeat/metricbeat.yml --path.home /usr/share/metricbeat/ --path.data /var/lib/metricbeat

检查输出是否正常。

复制代码
/usr/share/metricbeat/bin/metricbeat test output -c /etc/metricbeat/metricbeat.yml --path.home /usr/share/metricbeat/ --path.data /var/lib/metricbeat

如果出现上图所示输出说明证书配置正确。

如果出现以下报错信息:

talk to server... ERROR 401 Unauthorized: {"error":{"root_cause":{"type":"security_exception","reason":"unable to authenticate with provided credentials and anonymous access is not allowed for this request","additional_unsuccessful_credentials":"API key: invalid ApiKey value","header":{"WWW-Authenticate":\["Basic realm="security" charset="UTF-8"","Bearer realm="security"","ApiKey"}}],"type":"security_exception","reason":"unable to authenticate with provided credentials and anonymous access is not allowed for this request","additional_unsuccessful_credentials":"API key: invalid ApiKey value","header":{"WWW-Authenticate":"Basic realm="security" charset="UTF-8"","Bearer realm="security"","ApiKey"}},"status":401}

说明ApiKey有问题,请检查你的:<api_key>或者删除再重新添加。

报错信息

Exiting: error loading template: failed to load template: couldn't load template: 403 Forbidden: {"error":{"root_cause":{"type":"security_exception","reason":"action \[indices:admin/index_template/put is unauthorized for API key id py3JrJIBZeCTcJCiiZeI of user metric, this action is granted by the cluster privileges manage_index_templates,manage,all"}],"type":"security_exception","reason":"action indices:admin/index_template/put is unauthorized for API key id py3JrJIBZeCTcJCiiZeI of user metric, this action is granted by the cluster privileges manage_index_templates,manage,all"},"status":403}. Response body: {"error":{"root_cause":{"type":"security_exception","reason":"action \[indices:admin/index_template/put is unauthorized for API key id py3JrJIBZeCTcJCiiZeI of user metric, this action is granted by the cluster privileges manage_index_templates,manage,all"}],"type":"security_exception","reason":"action indices:admin/index_template/put is unauthorized for API key id py3JrJIBZeCTcJCiiZeI of user metric, this action is granted by the cluster privileges manage_index_templates,manage,all"},"status":403}

说明用户角色权限配置有问题,应当按照最小权限的原则配置,这里需要多次尝试,可以试着先给最高权限是否报错会消失,然后再依次降低权限下图是我的权限配置。

5.重启metricbeat验证

重启metricbeat并到kibana中查看监测数据是否正常。

复制代码
systemctl restart metricbeat


相关推荐
a1117761 小时前
2FA 验证码生成器(github登录验证 app)
笔记·学习
我的xiaodoujiao1 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
破碎的南瓜2 小时前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
阿里云大数据AI技术2 小时前
阿里云 ES AI 引擎版:面向 Agent 场景,为亿级租户、千亿规模向量设计的搜索引擎
人工智能·elasticsearch·agent
阿里云大数据AI技术3 小时前
FalconSeek 技术解析:阿里云 Elasticsearch 云原生内核如何让查询性能飙升600%
人工智能·elasticsearch
你想知道什么?3 小时前
线性回归-学习笔记
笔记·学习·线性回归
鱼毓屿御3 小时前
从「只会聊」到「边想边做」的跃迁
前端·学习·react.js
Elastic 中国社区官方博客3 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana
谙弆悕博士3 小时前
系统集成项目管理工程师教程(第3版)笔记——第17章:法律法规和标准规范
笔记·职场和发展·学习方法·业界资讯·软考·法律·法规
AOwhisky4 小时前
Python 学习笔记(第十五期)——运维自动化(下·后篇):堡垒机实战——paramiko高阶篇
运维·python·学习·云原生·自动化·运维开发