already

etcd集群权限管理和账号密码使用

2022-07-02  本文已影响0人  玄德公笔记

1. 操作实例

1.1 环境说明

节点 IP
etcd1 10.10.239.31:1379
etcd2 10.10.239.31:2379
etcd3 10.10.239.31:3379

1.2 创建root用户

root用户自带所有权限,因此只需创建该用户,开启认证即有所有权限。

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user add root
Password of root:
Type password of root again for confirmation:
User root created

交互需要输入用户密码

1.3 开启身份验证

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 auth enable
I have no name!@555187fb758c:/opt/bitnami/etcd$  etcdctl  --user='root' --password='LiuBei@161'   put name "guanyu"
OK
I have no name!@555187fb758c:/opt/bitnami/etcd$  etcdctl  --user='root' --password='LiuBei@161'   get name
name
guanyu
I have no name!@555187fb758c:/opt/bitnami/etcd$  etcdctl  get name
{"level":"warn","ts":"2022-07-02T08:11:34.599Z","logger":"etcd-client","caller":"v3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0xc00014c000/127.0.0.1:2379","attempt":0,"error":"rpc error: code = InvalidArgument desc = etcdserver: user name is empty"}
Error: etcdserver: user name is empty

  • 如果你决定对外公布root用户,那么到这里就可以了
  • 如果要隐藏高权限的root用户,而公布普通用户,那继续往下操作

1.4 普通用户管理

1.4.1 创建普通用户

因为之前已经启用了密码访问,所以这里要加上用户和密码。没有启用的前当然不用加。

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161'  --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user add myRead
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user add myWrite
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user list
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user add myReadWrite

输出:

myRead
myReadWrite
myWrite
root

1.4.2 创建角色

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl  --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379  role add roleRead 
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl  --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role add roleWrite
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl  --user='root' --password='LiuBei@161'  --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role add roleReadWrite
I have no name!@75cdaac66149:/opt/bitnami/etcd$  etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role list

结果

roleRead
roleReadWrite
roleWrite
root

1.4.3 给角色赋权

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl  --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379  role grant-permission roleRead read /xishu/* 
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role grant-permission roleWrite write /xishu/*
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role grant-permission roleReadWrite readwrite /xishu/*
I have no name!@75cdaac66149:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 role get roleReadWrite

结果显示

Role roleReadWrite
KV Read:
        /xishu/*
KV Write:
        /xishu/*

1.4.4 给用户绑定角色

I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl  --user='root' --password='LiuBei@161'  --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379  user grant-role myRead roleRead 
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161'  --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379  user grant-role myWrite roleWrite 
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379  user grant-role myReadWrite roleReadWrite 
I have no name!@75cdaac66149:/opt/bitnami/etcd$ etcdctl --user='root' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 user get myReadWrite

输出

User: myReadWrite
Roles: roleReadWrite

1.1.5 测试

I have no name!@75cdaac66149:/opt/bitnami/etcd$ etcdctl --user='myWrite' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 put /xishu/name "liubei"
OK
I have no name!@75cdaac66149:/opt/bitnami/etcd$ etcdctl --user='myWrite' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 put /xishu/age "28"
OK
I have no name!@75cdaac66149:/opt/bitnami/etcd$ etcdctl --user='myRead' --password='LiuBei@161' --endpoints http://10.10.239.31:1379,http://10.10.239.31:2379,http://10.10.239.31:3379 get /xishu/age
/xishu/age
28

2. 权限常用命令

2.1 用户管理

创建用户

etcdctl user add 用户名

删除用户

etcdctl user del 用户名

修改密码

etcdctl user passwd 用户名

查看所有用户

etcdctl user list

查看指定用户及绑定角色

etcdctl user get 用户名

2.2 角色

增加角色

etcdctl role add 角色名 

给角色赋权

etcdctl grant-permission 角色名 [read|write|readwrite] 范围

范围如: /xishu/*/liubei

回收角色赋权

etcdctl role revoke-permission  角色名  被回收范围

删除角色

etcdctl role del 角色名 

查询角色列表

etcdctl role list 

查询指定角色的权限

etcdctl role get 角色名 

2.3 用户&角色

用户绑定角色

etcdctl user grant-role 用户名 角色名 

回收用户绑定权限

etcdctl user revoke-role 用户名 角色名 

2.4 权限使用

etcdctl --user='用户名' --password='密码' ……

上一篇下一篇

猜你喜欢

热点阅读