etcd配置支持SSL+ACL

etcd配置支持SSL+ACL

前面的文章已经介绍了etcd如何配置SSL,以及etcd如何配置ACL。

这里介绍如果在SSL配置环境下如何使用ACL。

原理,按照etcd的说法,SSL配置环境下当前用户使用的是证书里面CN值,其他都一样;这样在客户端发起命令的时候不需要提供用户名了,etcd将从命令提供的证书的CN域作为用户名。

If an etcd server is launched with the option --client-cert-auth=true,
the field of Common Name (CN) in the client's TLS cert will be used as an etcd user.
In this case, the common name authenticates the user and the client does not need a password.

几个步骤。

  1. 启动etcd的时候需要配置ETCD_ADVERTISE_CLIENT_URLS为真实域名地址

不能设置成ETCD_ADVERTISE_CLIENT_URLS=https://0.0.0.0:2379,否则验证SSL证书SAN的时候会失败,例如:

ETCD_ADVERTISE_CLIENT_URLS=https://etcd1.example.com:2379
  1. 需要生成一个CN=root的证书。

因为root是内置账户,要启动ACL必须创建root账号,后续的一些基本操作,必须要root账号才能操作。

  1. 激活ACL

假设etcd启动配置的server证书是server.pem (CN=server),当然也可以使用root证书启动。

$ etcdctl --endpoints https://etcd1.example.com:2379 \
          --cert-file /ssl/server.pem --key-file ssl/server.key --ca-file ssl/ca.pem \
          user add root
$ etcdctl --endpoints https://etcd1.example.com:2379 \
          --cert-file /ssl/server.pem --key-file ssl/server.key --ca-file ssl/ca.pem  \
          auth enable

这个第一步,使用server证书创建root用户,并且激活ACL。

  1. 创建访问用户账号和角色

注意这里要使用root证书,不能使用server证书了,因为一旦激活的ACL,那么server证书就是一个普通用户,不具有超级权限。

$ etcdctl --endpoints https://etcd1.example.com:2379 \
          --cert-file ssl/root.pem --key-file ssl/root.key --ca-file ssl/ca.pem \
          role add myrole

$ etcdctl --endpoints https://etcd1.example.com:2379 \
          --cert-file ssl/root.pem --key-file ssl/root.key --ca-file ssl/ca.pem \
          user add client1

$ etcdctl --endpoints https://etcd1.example.com:2379 \
          --cert-file ssl/root.pem --key-file ssl/root.key --ca-file ssl/ca.pem  \
          user grant --roles myrole client1

这一步创建一个角色myrole,并且创建一个用户client1,然后把角色myrole赋给用户client1。
注意虽然使用证书验证客户端命令行不需要再提供用户信息,但是还必须在etcd服务器里面创建账号,否则etcd无法为一个不存在的账号赋予角色权利。

  1. 客户端用户访问
$ etcdctl --endpoints https://etcd1.example.com:2379 \
        --cert-file ssl/root.pem --key-file ssl/root.key --ca-file ssl/ca.pem \
        set /foo 'myvalue'

$ etcdctl --endpoints https://etcd1.example.com:2379 \
        --cert-file ssl/client1.pem --key-file ssl/client1.key --ca-file ssl/ca.pem \
        get /foo
Error:  110: The request requires user authentication (Insufficient credentials) [0]

$ etcdctl --endpoints https://etcd1.example.com:2379 \
        --cert-file ssl/client1.pem --key-file ssl/client1.key --ca-file ssl/ca.pem \
        --username root:root get /foo
myvalue

这个例子我们包含三个命令:
命令1:使用root创建/foo
命令2:使用client1(用户从证书CN域获取)去访问/foo,失败
命令3:使用root(用户从命令行获取,替换证书CN域)去访问/foo,成功

猜你喜欢

转载自blog.csdn.net/weixin_33806300/article/details/86887554
今日推荐