Zookeeper 从入门到入土④:ACL 权限

1. ACL 构成

概述: 针对节点可以设置相关读写权限,保障数据安全

通过 scheme:id:permissions 来构成权限列表

  • scheme:         代表采用的某种权限机制
  • id:                   代表允许访问的用户
  • permissions: 代表允许的操作权限

例: setAcl path world:anyone:d 代表为 path 下的节点设置权限为所有人都只能删除该节点

1.1. scheme 类型

  • world(world:anyone:[permissions]):                                                     默认权限。只有一个用户 —— anyone
  • auth(auth:user:password:[permissions]):                                            代表认证登录,需要注册用户拥有权限
  • digest(digest:username:BASE64(SHA1(password)):[permissions]): 需要对密码加密才能访问
  • ip(ip:192.168.1.1:[permissions]):                                                            限制 ip 进行访问
  • super:                                                                                                             超级管理员,拥有所有权限

1.2. permissions

crdwa 代表的权限含义:

  • CREATE: 创建 子节点
  • READ:     读取节点数据
  • WRITE:    往节点写入数据
  • DELETE: 删除 子节点,对于 delete 权限,要谨慎规划
  • ADMIN:   可以使用 setAcl 命令设置权限

2. 命令

2.1. addauth scheme auth

概述:
添加认证授权信息到 Zookeeper 库中(注册)
并使用该认证作为当前客户端的认证信息(登录),这之后进行的所有操作会以该认证为前提
密码需输入明文,但在 Zookeeper 中密码以加密形式存储

1
[zk: localhost:2181(CONNECTED) 2] addauth digest ccomma:ccomma

2.2. setAcl path schemepwd:permissions

auth:

  • 设置某个节点的 acl 权限信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [zk: localhost:2181(CONNECTED) 6] setAcl /ccomma auth:ccomma:ccomma:crdwa
    cZxid = 0x18
    ctime = Mon May 27 16:36:40 CST 2019
    mZxid = 0x2b
    mtime = Mon May 30 16:10:49 CST 2019
    pZxid = 0x1f
    cversion = 5
    dataVersion = 2
    aclVersion = 1
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 1
  • 当使用 addauth 添加认证后,setAcl 账号和密码可以省略,默认取第一个认证

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [zk: localhost:2181(CONNECTED) 6] setAcl /ccomma auth::crdwa
    cZxid = 0x18
    ctime = Mon May 27 16:36:40 CST 2019
    mZxid = 0x2b
    mtime = Mon May 30 16:10:49 CST 2019
    pZxid = 0x1f
    cversion = 5
    dataVersion = 2
    aclVersion = 1
    ephemeralOwner = 0x0
    dataLength = 6
    numChildren = 1

digest: 密码加密

1
2
3
4
5
6
7
8
9
10
11
12
[zk: localhost:2181(CONNECTED) 8] setAcl /ccomma digest:ccomma:91PXC4WimSDWZikp99kGvvjeVnY=:crdwa
cZxid = 0x60
ctime = Fri May 31 13:33:42 CST 2019
mZxid = 0x60
mtime = Fri May 31 13:33:42 CST 2019
pZxid = 0x60
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0

ip:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[zk: localhost:2181(CONNECTED) 15] setAcl /ccomma/ip ip:192.168.1.7:crdwa
cZxid = 0x62
ctime = Fri May 31 13:36:51 CST 2019
mZxid = 0x62
mtime = Fri May 31 13:36:51 CST 2019
pZxid = 0x62
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: localhost:2181(CONNECTED) 15] getAcl /ccomma/ip
'ip,'192.168.1.7
:crdwa

2.3. getAcl path

获取某个节点的 acl 权限信息
密码以密文形式存储

1
2
3
[zk: localhost:2181(CONNECTED) 8] getAcl /ccomma
'digest,'ccomma:91PXC4WimSDWZikp99kGvvjeVnY=
:crdwa

3. super auth

添加超级用户:
在 nohup “JAVA” “-Dzookeeper.log.dir={ZOO_LOG_DIR}” “-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}”  后面加上 “-Dzookeeper.DigestAuthenticationProvider.superDigest=admin:9iPCX4WimSDWZikp99kGvvjeVnY=”

代表添加超级用户 admin:9iPCX4WimSDWZikp99kGvvjeVnY= (明文:ccomma)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f "$ZOOPIDFILE" ]; then
if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
echo $command already running as process `cat "$ZOOPIDFILE"`.
exit 0
fi
fi
nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}"
"-Dzookeeper.DigestAuthenticationProvider.superDigest=admin:9iPCX4WimSDWZikp99kGvvjeVnY=" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
if [ $? -eq 0 ]
then
case "$OSTYPE" in
*solaris*)
/bin/echo "${!}\\c" > "$ZOOPIDFILE"
;;
*)
/bin/echo -n $! > "$ZOOPIDFILE"
;;
esac
if [ $? -eq 0 ];
then
sleep 1
echo STARTED
else
echo FAILED TO WRITE PID
exit 1
fi
else
echo SERVER DID NOT START
exit 1
fi
;;