etcd v2认证(一)用户名密码认证

一、etcd用户认证简介

默认不使用认证的情况下,任一主机都可以通过restful api请求操作修改etcd数据。etcd有两种认证机制:一种是基于用户名密码认证,一种是基于SSL/TSL证书 认证。两种同时使用更好。

  • etcd通过设置用户-角色关联,角色和权限关联,通过设置这些信息,使得指定的用户对某些目录拥有指定的权限;
  • etcd默认不启用权限机制,需要手动启;
  • 拥有一个个特殊的用户root,拥有两个特殊的角色root和guest,root用户必须在启用权限系统前创建。

二、用户、组、权限

权限

etcd权限是通过role来实现的,先给role分配一定的权限,再将对应的用户(user)加入到相应的组(role),则用户也具有了相应的权限。

角色(role)可以设置权限,权限分为读、写、读写,权限的基础是etcd的key,一般普通用户只对某些key的目录拥有写权限,读权限不做限制。

用户 user

即然要先给组(role)赋予权限用户才有权限,为什么还先要说用户呢?因为这里有一个特殊的用户root,必须要先将其启用先能配置其他用户。

  • 默认的root用户拥有所有权限
  • 其他用户可以通过root用户去创建
  • 用户拥有哪些权限需要通过赋予指定的角色去获取权限,一个用户可以有多个角色

角色 role

  • 对于root角色的权限是不能被修改的,root角色拥有所有系统权限,是系统管理员。
  • 而经过测试,guest用户组默认也具有读写,这可能和我们一般的理解是不一样的,具体可以通过etcdctl role get guest确认。
  • 对于不同的用户组,可以通过分配/*、/test/*、/com/361way/*分配不同的子目录权限。

The guest role defines the permissions granted to any request that does not provide an authentication.

$ etcdctl user add root
New password:
$ etcdctl auth enable

# 默认guest用户组具有读写权限
[root@361way ~]# etcdctl -u root:redhat role get guest
Role: guest
KV Read:
        /*
KV Write:
        /*

回收权限

[root@361way ~]# etcdctl -u root:redhat role revoke guest -path '/' -write
auth: Role not updated. Use grant/revoke to update the role.
[root@361way ~]# etcdctl -u root:redhat role revoke guest -path '/*' -write
Role guest updated
[root@361way ~]# etcdctl -u root:redhat role get guest
Role: guest
KV Read:
        /*
KV Write:

不使用用户名密码失败

[root@361way ~]# etcdctl update /skydns/migu/example/a '{"host": "1.2.3.8"}'
Error:  110: The request requires user authentication (Insufficient credentials) [0]
# 使用用户名密码更改数据成功
[root@361way ~]# etcdctl -u root:redhat update /skydns/migu/example/a '{"host": "1.2.3.8"}'
{"host": "1.2.3.8"}
[root@361way ~]# etcdctl get /skydns/migu/example/a
{"host": "1.2.3.8"}

# 正常读取不受影响
[root@361way ~]# etcdctl get /skydns/migu/example/a
{"host": "1.2.3.8"}

给组赋权限

# Give read access to keys under the /foo directory
$ etcdctl role grant myrolename -path '/foo/*' -read

# Give write-only access to the key at /foo/bar
$ etcdctl role grant myrolename -path '/foo/bar' -write

# Give full access to keys under /pub
$ etcdctl role grant myrolename -path '/pub/*' -readwrite

为用户赋权及回收

$ etcdctl user grant myusername -roles foo,bar,baz
$ etcdctl user revoke myusername -roles bar,baz

启用用户认证时,guest用户的/*的只读权限不能回收。回收后coredns会出现无法读取的问题,具体报错如下:

Jun 25 16:07:09 localhost etcd: auth: invalid access for unauthenticated user on resource /skydns/migu/inspur.
Jun 25 16:07:09 localhost coredns: 2018/06/25 16:07:09 [ERROR] 0 inspur.migu. A: 110: The request requires user authentication (Insufficient credentials) [0]

如果觉得只是用户名密码认证还是不安全,可以启用tls认证,coredns是支持该认证的,其配置中有如下配置项:

tls CERT KEY CACERT

Restful api帐号管理

也可以通过restful api的方式进行帐号增删改查操作,具体可以参看etcd auth_api

第三方API认证调用

shell

curl 中使用 -u user:password 

python调用

import base64

def basic_auth(name,passwd):
     return "Basic %s"%(base64.encodestring('%s:%s'%(name,passwd)))

auth = basic_auth('u user','u password')
req.add_header('Authorization',auth)
req.add_header('Content-Length',length)
resp = urllib2.urlopen(req)

go api 使用官方的go sdk

cfg := client.Config{
    Endpoints: []string{"http://XXX.XX.XXX.XX:2379"},
    Transport: client.DefaultTransport,
    // set timeout per request to fail fast when the target endpoint is unavailable
    HeaderTimeoutPerRequest: time.Second,
    Username:                "XXX",
    Password:                "XXX",
}

** 参考页面: **

etcd认证相关命令

etcd auth_api

for下一篇

tls认证

etcd启用https


donation