etcd开启账号认证和账号授权
当etcd同时共多个程序使用时,这时我们一般会根据业务来分为不同的账号,并授予相应的权限。这里使用集群的方式来开启账号认证。
这里举个例子,比如有2个项目使用etcd集群:
一个是项目1,账号test1,键以/mx1/开头,角色为mx1_role。
一个是项目2,账号test2,键以/mx2/开头,角色为mx2_role。
现在的需求是用test1的账号只能操作/mx1/开头的键,test2的账号只能操作/mx2/开头的键。
操作过程:
1、使用etcdctl命令设置好root用户的账号和密码
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ user add root:root
2、开启账号认证。
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ auth enable
3、根据不同项目创建用户和密码设置。
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ user add test1:test1 etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ user add test2:test2
4、创建角色
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ role add mx1_role etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ role add mx2_role
5、赋予角色权限
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ role grant-permission mx1_role --prefix=true readwrite /mx1/ etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ role grant-permission mx2_role --prefix=true readwrite /mx2/
6、授权用户
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ user grant-role test1 mx1_role etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=root:root \ user grant-role test2 mx2_role
7、现在2个项目就可以分别使用test1和test2账户分别操作/mx1/和/mx2/的key了
etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=test1:test1\ put /mx1/test1 "this is test1" etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \ --cacert=/data/etcd/ssl/ca.pem \ --cert=/data/etcd/ssl/server.pem \ --key=/data/etcd/ssl/server-key.pem \ --user=test2:test2\ put /mx2/test2 "this is test2"