ACL
机制:权限模式(
Schema
)、授权对象(
ID
)、权限(
Permission
)
权限模式(Schema):
world: 它下面只有一个id, 叫anyone, world:anyone代表任何人,zookeeper中对所有人有权限的结点就是属于world:anyone的
auth: 它不需要id, 只要是通过authentication的user都有权限(zookeeper支持通过kerberos来进行authencation, 也支持username/password形式的authentication)
digest: 它对应的id为username:BASE64(SHA1(password)),它需要先通过username:password形式的authentication
ip: 它对应的id为客户机的IP地址,设置的时候可以设置一个ip段,比如ip:192.168.1.0/16, 表示匹配前16个bit的IP段
super: 在这种scheme情况下,对应的id拥有超级权限,可以做任何事情(cdrwa)
auth: 它不需要id, 只要是通过authentication的user都有权限(zookeeper支持通过kerberos来进行authencation, 也支持username/password形式的authentication)
digest: 它对应的id为username:BASE64(SHA1(password)),它需要先通过username:password形式的authentication
ip: 它对应的id为客户机的IP地址,设置的时候可以设置一个ip段,比如ip:192.168.1.0/16, 表示匹配前16个bit的IP段
super: 在这种scheme情况下,对应的id拥有超级权限,可以做任何事情(cdrwa)
授权对象(ID):
根据不同scheme,有不同写法
权限(Permission):
参照:org.apache.zookeeper.ZooDefs.Perms
在源码org.apache.zookeeper.ZooDefs 中,设置ACL
在org.apache.zookeeper.server.auth.DigestAuthenticationProvider中,对密码加密
在ZooKeeperMain类中,parseACLs方法是解析acl表达式的,
其中以:为分隔符,拿第一个之前的为:scheme,最后一个之后的为:perms,中间的为:id
下面演示一个通过digest(用户名密码的方式)为创建的节点设置ACL的例子
package com.dongnao.demo;
import org.apache.zookeeper.*;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
import org.apache.zookeeper.data.*;
import java.util.*;
public class NewDigest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//new一个acl
List<ACL> acls = new ArrayList<ACL>();
//添加第一个id,采用用户名密码形式
Id id1 = new Id("digest",
DigestAuthenticationProvider.generateDigest("admin:admin"));
ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);
acls.add(acl1);
//添加第二个id,所有用户可读权限
Id id2 = new Id("world", "anyone");
ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);
acls.add(acl2);
// zk用admin认证,创建/test ZNode。
ZooKeeper zk = new ZooKeeper(
"host1:2181,host2:2181,host3:2181",
2000, null);
zk.addAuthInfo("digest", "admin:admin".getBytes());
zk.create("/test", "data".getBytes(), acls, CreateMode.PERSISTENT);
}
}
import org.apache.zookeeper.*;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
import org.apache.zookeeper.data.*;
import java.util.*;
public class NewDigest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//new一个acl
List<ACL> acls = new ArrayList<ACL>();
//添加第一个id,采用用户名密码形式
Id id1 = new Id("digest",
DigestAuthenticationProvider.generateDigest("admin:admin"));
ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);
acls.add(acl1);
//添加第二个id,所有用户可读权限
Id id2 = new Id("world", "anyone");
ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);
acls.add(acl2);
// zk用admin认证,创建/test ZNode。
ZooKeeper zk = new ZooKeeper(
"host1:2181,host2:2181,host3:2181",
2000, null);
zk.addAuthInfo("digest", "admin:admin".getBytes());
zk.create("/test", "data".getBytes(), acls, CreateMode.PERSISTENT);
}
}