为了账号安全,请及时绑定邮箱和手机立即绑定

Etcd安全配置之Basic Auth认证

标签:
安全

《中小团队落地配置中心详解》文章中我们介绍了如何基于Etcd+Confd构建配置中心,最后提到Etcd的安全问题时说了可以使用账号密码认证以达到安全访问的目的,究竟该如何开启认证以及怎么设计权限访问呢?本文将为你详细解读

认证说明

  1. Etcd v2以上的版本才支持权限认证,且仅支持Basic Auth

  2. Etcd通过用户(user)-角色(role)-权限的方式来控制访问,用户关联角色,角色拥有权限,从而用户也就拥有了相应的权限

  3. Etcd默认没有启用认证机制,只要能连接etcd服务就拥有所有的权限,还是非常危险的,另一种安全的访问方式是开启ssl,只有使用受信任的证书才能访问数据

  4. Etcd开启Basic Auth之后,默认会启用两个角色root和guest,root角色拥有所有权限,guest拥有只读权限,这两个角色都不要删除,否则你可能会遇到意想不到的Bug

  5. Etcd的权限分为只读、只写、可读写,可以对etcd的详细key进行授权,例如:/conf/project/dev/nginx.conf,也可以授权key前缀(目录),例如:/conf/project/,授权规则应以最小满足需求为准则

权限设计

权限设计应先考虑我们对权限的需求,从需求出发设计权限

  • 需求

  1. 为了方便后续管理,规定配置中心所有key都应已/conf/开头

  2. 需要两个账号,一个账号用在Kerrigan(WebUI)拥有读取、写入、修改、删除key的权限,一个账号用在confd,只有只读的权限,能够读取配置就可以了

  • 设计

  1. 需求很简单,我们需要建立两个账号,分别对应两个角色,两个角色都是对/conf/开头的Key进行控制,一个读写权限,一个只读权限

  2. 定义只读账号名为readx,只读角色名为readConf,定义读写权限账号名为authz,读写权限为rootConf,可操作的key都为/conf/开头

详细步骤

1.添加root用户

# etcdctl user add rootNew password: 12345User root created

2.创建root账号后,root默认有root角色,对所有KV有读写权限

# etcdctl user get rootUser: rootRoles:  root# etcdctl role get rootRole: rootKV Read:
    /*KV Write:
    /*

3.开启auth认证

# etcdctl auth enableAuthentication Enabled

开启权限认证后默认会多一个guest的角色# etcdctl --username root:12345 role listguest
root

4.添加非root账号,一个authz的账号,一个readx的账号

# etcdctl --username root:12345 user add authzNew password: 
User authz created# etcdctl --username root:12345 user add readxNew password: 
User readx created

5.添加角色,一个rootConf的角色,一个readConf的角色

# etcdctl --username root:12345 role add rootConfRole rootConf created# etcdctl --username root:12345 role add readConfRole readConf created

6.为角色授权,readConf角色对/conf有只读权限,rootConf角色对/conf有读写权限

# etcdctl --username root:12345 role grant --read --path /conf/* readConfRole readConf updated# etcdctl --username root:12345 role grant --readwrite --path /conf/* rootConfRole rootConf updated

7.给用户分配角色,authz账号分配rootConf角色,readx账号分配readConf角色

# etcdctl --username root:12345 user grant --roles rootConf authzUser authz updated# etcdctl --username root:12345 user grant --roles readConf readxUser readx updated

8.查看用户所拥有的角色

# etcdctl --username root:12345 user get authzUser: authzRoles:  rootConf# etcdctl --username root:12345 user get readxUser: readxRoles:  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

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消