「etcd」- RBAC

Role-Based Access Control

常用的权限控制方法有 ACL(Access Control List)、ABAC(Attribute-based access control)、RBAC(Role-based access control),etcd 实现的是 RBAC 机制;

它由下图中的三部分组成,User、Role、Permission。User 表示用户,如 alice。Role 表示角色,它是权限的赋予对象。Permission 表示具体权限明细,比如赋予 Role 对 key 范围在[key,KeyEnd]数据拥有什么权限。目前支持三种权限,分别是 READ、WRITE、READWRITE;

场景:授予用户读写权限

给 alice 用户赋予一个可读写[hello,helly]数据范围的读写权限

按照 RBAC 原理:
1)首先,需要创建一个 Role,这里我们命名为 admin,
2)然后,新增可读写 [hello,helly] 数据范围的权限,并赋予给 admin 角色,
3)并将 admin 的角色的权限授予 alice 用户;

# 创建一个 admin role

etcdctl role add admin  --user root:root
Role admin created

# 分配一个可读写[hello,helly]范围数据的权限给 admin role

$ etcdctl role grant-permission admin readwrite hello helly --user root:root
Role admin updated

# 将用户 alice 和 admin role 关联起来,赋予 admin 权限给 user

$ etcdctl user grant-role alice admin --user root:root
Role admin is granted to user alice

然后当你再次使用 etcdctl 执行 put hello 命令时,鉴权模块会从 boltdb 查询 alice 用户对应的权限列表;

因为有可能一个用户拥有成百上千个权限列表,etcd 为了提升权限检查的性能,引入了区间树,检查用户操作的 key 是否在已授权的区间,时间复杂度仅为 O(logN);

在我们的这个案例中,很明显 hello 在 admin 角色可读写的[hello,helly) 数据范围内,因此它有权限更新 key hello,执行成功。你也可以尝试更新 key hey,因为此 key 未在鉴权的数据区间内,因此 etcd server 会返回”etcdserver: permission denied”错误给 client,如下所示;

$ etcdctl put hello world --user alice:alice
OK

$ etcdctl put hey hey --user alice:alice
Error: etcdserver: permission denied