Etcd安全配置之Basic Auth认证

《中小团队落地配置中心详解》文章中我们介绍了如何基于Etcd+Confd构建配置中心,最后提到Etcd的安全问题时说了可以使用账号密码认证以达到安全访问的目的,究竟该如何开启认证以及怎么设计权限访问呢?本文将为你详细解读 认证说明 Etcd v2以上的版本才支持权限认证,且仅支持Basic Auth Etcd通过用户(user)-角色(role)-权限的方式来控制访问,用户关联角色,角色拥有权限,从而用户也就拥有了相应的权限 Etcd默认没有启用认证机制,只要能连接etcd服务就拥有所有的权限,还是非常危险的,另一种安全的访问方式是开启ssl,只有使用受信任的证书才能访问数据 Etcd开启Basic Auth之后,默认会启用两个角色root和guest,root角色拥有所有权限,guest拥有只读权限,这两个角色都不要删除,否则你可能会遇到意想不到的Bug Etcd的权限分为只读、只写、可读写,可以对etcd的详细key进行授权,例如:/conf/project/dev/nginx.conf,也可以授权key前缀(目录),例如:/conf/project/,授权规则应以最小满足需求为准则 权限设计 权限设计应先考虑我们对权限的需求,从需求出发设计权限 需求 为了方便后续管理,规定配置中心所有key都应已/conf/开头 需要两个账号,一个账号用在Kerrigan(WebUI)拥有读取、写入、修改、删除key的权限,一个账号用在confd,只有只读的权限,能够读取配置就可以了 设计 需求很简单,我们需要建立两个账号,分别对应两个角色,两个角色都是对/conf/开头的Key进行控制,一个读写权限,一个只读权限 定义只读账号名为readx,只读角色名为readConf,定义读写权限账号名为authz,读写权限为rootConf,可操作的key都为/conf/开头 详细步骤 1.添加root用户 # etcdctl user add root New password: 12345 User root created 2.创建root账号后,root默认有root角色,对所有KV有读写权限 # etcdctl user get root User: root Roles: root # etcdctl role get root Role: root KV Read: /* KV Write: /* 3.开启auth认证 # etcdctl auth enable Authentication Enabled 开启权限认证后默认会多一个guest的角色 # etcdctl --username root:12345 role list guest root 4.添加非root账号,一个authz的账号,一个readx的账号 # etcdctl --username root:12345 user add authz New password: User authz created # etcdctl --username root:12345 user add readx New password: User readx created 5.添加角色,一个rootConf的角色,一个readConf的角色 # etcdctl --username root:12345 role add rootConf Role rootConf created # etcdctl --username root:12345 role add readConf Role readConf created 6.为角色授权,readConf角色对/conf有只读权限,rootConf角色对/conf有读写权限 # etcdctl --username root:12345 role grant --read --path /conf/* readConf Role readConf updated # etcdctl --username root:12345 role grant --readwrite --path /conf/* rootConf Role rootConf updated 7.给用户分配角色,authz账号分配rootConf角色,readx账号分配readConf角色 # etcdctl --username root:12345 user grant --roles rootConf authz User authz updated # etcdctl --username root:12345 user grant --roles readConf readx User readx updated 8.查看用户所拥有的角色 # etcdctl --username root:12345 user get authz User: authz Roles: rootConf # etcdctl --username root:12345 user get readx User: readx Roles: readConf 这样readx账号就对/conf下的所有文件有了只读权限,authz对/conf下的所有文件有了读写权限 常用命令 有一些命令上边没有介绍到,会用得到的如下: 1.关闭认证 # etcdctl --username root:12345 auth disable 2.删除用户 # etcdctl --username root:12345 user remove userx 3.用户撤销角色 # etcdctl --username root:12345 user revoke rolex 4.修改用户密码 # etcdctl --username root:12345 user passwd 同时还有删除角色、撤销角色权限可参看上边用户相关操作 踩坑记录 在开启认证后发现系统默认给添加了guest角色,觉得guest角色没用就给删除了,于是再连接etcd集群时就报如下错误: 报错:The request requires user authentication (Insufficient credentials) 解决:重新添加guest角色 长按关注公众号查看更多原创文章 如果你觉得文章对你有帮助,请转发分享给更多的人。如果你觉得读的不尽兴,推荐阅读以下文章: 中小团队落地配置中心详解https://www.cnblogs.com/37Y37/p/9632844.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信