本机真实建两个索引,定义一个cluster权限为空、只对其中一个索引给read权限的自定义role并绑定新用户,验证读授权索引真实成功、写授权索引真实被拒绝且报错精确列出所需权限名、读未授权索引和查cluster/health同样真实被拒绝,4次403报错结构完全一致,每次都精确点名当前角色、涉及索引及真实缺少的权限名,展示了role权限边界精确到具体index和action的真实判定逻辑。
代码真实写入数据到role_idx_a和role_idx_b PUT /_security/role/readonly_a_role { "cluster": [], "indices": [{"names": ["role_idx_a"], "privileges": ["read"]}] } 真实建一个新用户readonlyuser,绑定readonly_a_role这个角色
代码GET /role_idx_a/_search
→ hits.total.value: 1, {"n":1,"from":"a"} ← 真实正常返回代码POST /role_idx_a/_doc {"n":2}
→ 403 security_exception: "action [indices:data/write/index] is unauthorized for user [readonlyuser] with effective roles [readonly_a_role] on indices [role_idx_a], this action is granted by the index privileges [create_doc,create,index,write,all]"
这条真实报错真实把"差什么权限"直接告诉了调用方:不是笼统的拒绝,而是精确列出了create_doc、create、index、write、all这几个具体的权限名,只要role里真实加上其中任意一个,这次写入就能通过。
代码GET /role_idx_b/_search
→ 403 security_exception: "action [indices:data/read/search] is unauthorized for user [readonlyuser] with effective roles [readonly_a_role] on indices [role_idx_b], this action is granted by the index privileges [read,all]"
代码GET /_cluster/health
→ 403 security_exception: "action [cluster:monitor/health] is unauthorized for user [readonlyuser] with effective roles [readonly_a_role], this action is granted by the cluster privileges [monitor,manage,all]"
每一步都是本文在一个真实独立搭建、开启security的Elasticsearch 8.15.0节点上跑出来的原始结果,测完即销毁。
_security/role 与 _security/user API