Etcd安全配置之Basic Auth认证

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

认证说明

  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用户bash

# etcdctl user add root
New password: 12345
User root created
复制代码

2.建立root帐号后,root默认有root角色,对全部KV有读写权限ui

# etcdctl user get root
User: root
Roles:  root

# etcdctl role get root
Role: root
KV Read:
	/*
KV Write:
	/*
复制代码

3.开启auth认证spa

# 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的角色code

# 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有读写权限cdn

# 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角色ssl

# 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.查看用户所拥有的角色ci

# 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角色


长按关注公众号查看更多原创文章

若是你以为文章对你有帮助,请转发分享给更多的人。若是你以为读的不尽兴,推荐阅读如下文章:

相关文章
相关标签/搜索