在 DataEase 中使用 Elasticsearch 类型的数据源时,有的用户不希望给查询 Elasticsearch 数据的账号开放过高的权限,希望开放最小的权限满足 DataEase 的访问需求即可,如果你有这样的需求,那么你可以参考下面的步骤进行操作,在 ES 中创建一个只读角色,然后创建一个用户并将此用户设置为只读角色。
1 ES 创建只读角色 readonly_role (对所有的 index 可读) {#1-es-%E5%88%9B%E5%BB%BA%E5%8F%AA%E8%AF%BB%E8%A7%92%E8%89%B2-readonly_role-(%E5%AF%B9%E6%89%80%E6%9C%89%E7%9A%84-index-%E5%8F%AF%E8%AF%BB)}
curl -XPOST -u elastic:Password123@elastic -s http://@10.1.13.102:9200/_xpack/security/role/readonly_role -H "Content-Type: application/json" -d'
{
"cluster": ["all"],
"indices": [{
"names": ["*"],
"privileges": ["read", "view_index_metadata", "monitor"],
"allow_restricted_indices": false
}]
}'
2 查看刚才创建的只读角色 readonly_role {#2-%E6%9F%A5%E7%9C%8B%E5%88%9A%E6%89%8D%E5%88%9B%E5%BB%BA%E7%9A%84%E5%8F%AA%E8%AF%BB%E8%A7%92%E8%89%B2-readonly_role}
curl -XGET -u elastic:Password123@elastic http://@10.1.13.102:9200/_xpack/security/role/readonly_role?pretty
3 ES 创建只读角色下的只读用户 readonly_user {#3-es-%E5%88%9B%E5%BB%BA%E5%8F%AA%E8%AF%BB%E8%A7%92%E8%89%B2%E4%B8%8B%E7%9A%84%E5%8F%AA%E8%AF%BB%E7%94%A8%E6%88%B7-readonly_user}
curl -XPOST -u elastic:Password123@elastic -s http://@10.1.13.102:9200/_xpack/security/user/readonly_user -H "Content-Type: application/json" -d'
{
"password" : "Password@readonly",
"roles" : [ "readonly_role" ],
"full_name" : "Readonly Role",
"email" : "readonlt@fit2cloud.com",
"metadata" : {}
}'
4 下面提供一些ES常用的查询命令 {#4-%E4%B8%8B%E9%9D%A2%E6%8F%90%E4%BE%9B%E4%B8%80%E4%BA%9Bes%E5%B8%B8%E7%94%A8%E7%9A%84%E6%9F%A5%E8%AF%A2%E5%91%BD%E4%BB%A4}
4.1 ES 查看所有索引 {#4.1-es-%E6%9F%A5%E7%9C%8B%E6%89%80%E6%9C%89%E7%B4%A2%E5%BC%95}
curl -XGET -u elastic:Password123@elastic http://@10.1.13.102:9200/_cat/indices?v
4.2 ES查看指定角色的所有权限信息 {#4.2-es%E6%9F%A5%E7%9C%8B%E6%8C%87%E5%AE%9A%E8%A7%92%E8%89%B2%E7%9A%84%E6%89%80%E6%9C%89%E6%9D%83%E9%99%90%E4%BF%A1%E6%81%AF}
curl -XGET -u elastic:Password123@elastic http://@10.1.13.102:9200/_xpack/security/role/superuser?pretty
4.3 ES查看所有用户及用户所属的角色 {#4.3-es%E6%9F%A5%E7%9C%8B%E6%89%80%E6%9C%89%E7%94%A8%E6%88%B7%E5%8F%8A%E7%94%A8%E6%88%B7%E6%89%80%E5%B1%9E%E7%9A%84%E8%A7%92%E8%89%B2}
curl -XGET -u elastic:Password123@elastic http://@10.1.13.102:9200/_xpack/security/user?pretty