ZooKeeper ACL許可權控制
阿新 • • 發佈:2019-01-06
ZK 類似檔案系統,Client 可以在上面建立節點、更新節點、刪除節點等如何做到許可權的控制?查閱文件,zk的ack(Access Control List)能夠保證許可權,但是調研完後發現它不是很好用。
ACL 許可權控制,使用:schema:id:permission 來標識,主要涵蓋 3 個方面:
- 許可權模式(Schema):鑑權的策略
- 授權物件(ID)
- 許可權(Permission)
- ZooKeeper的許可權控制是基於每個znode節點的,需要對每個節點設定許可權
- 每個znode支援設定多種許可權控制方案和多個許可權
- 子節點不會繼承父節點的許可權,客戶端無權訪問某節點,但可能可以訪問它的子節點
方案 | 描述 |
---|---|
world | 只有一個使用者:anyone,代表所有人(預設) |
ip | 使用IP地址認證 |
auth | 使用已新增認證的使用者認證 |
digest | 使用“使用者名稱:密碼”方式認證 |
3、許可權permission:
許可權 | ACL簡寫 | 描述 |
---|---|---|
CREATE | c | 可以建立子節點 |
DELETE | d | 可以刪除子節點(僅下一級節點) |
READ | r | 可以讀取節點資料及顯示子節點列表 |
WRITE | w | 可以設定節點資料 |
ADMIN | a | 可以設定節點訪問控制列表許可權 |
命令 | 使用方式 | 描述 |
---|---|---|
getAcl | getAcl <path> | 讀取ACL許可權 |
setAcl | setAcl <path> <acl> | 設定ACL許可權 |
addauth | addauth <scheme> <auth> | 新增認證使用者 |
2)客戶端例項:setAcl <path> world:anyone:<acl>
[zk: localhost:2181(CONNECTED) 0] create /node1 1
Created /node1
[zk: localhost:2181(CONNECTED) 1] getAcl /node1
'world,'anyone #預設為world方案
: cdrwa #任何人都擁有所有許可權
#可以用以下方式設定:
[zk: localhost:2181(CONNECTED) 2] setAcl /node1 world:anyone:cdrwa
cZxid = 0x19000002a1
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x19000002a1
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x19000002a1
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
2、IP方案: 1)設定方式
setAcl <path> ip:<ip>:<acl>
<ip>:可以是具體IP也可以是IP/bit格式,即IP轉換為二進位制,匹配前bit位,如192.168.0.0/16匹配192.168.*.*
2)客戶端例項
[zk: localhost:2181(CONNECTED) 0] create /node2 1
Created /node2
[zk: localhost:2181(CONNECTED) 1] setAcl /node2 ip:192.168.100.1:cdrwa #設定IP:192.168.100.1 擁有所有許可權
cZxid = 0x1900000239
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x1900000239
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x1900000239
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
[zk: localhost:2181(CONNECTED) 2] getAcl /node2
'ip,'192.168.100.1
: cdrwa
#使用IP非 192.168.100.1 的機器
[zk: localhost:2181(CONNECTED) 0] get /node2
Authentication is not valid : /node2 #沒有許可權
[zk: localhost:2181(CONNECTED) 1] delete /node2 #刪除成功(因為設定DELETE許可權僅對下一級子節點有效,並不包含此節點)
3、Auth方案 1)設定方式
addauth digest <user>:<password> #新增認證使用者
setAcl <path> auth:<user>:<acl>
2)客戶端例項
[zk: localhost:2181(CONNECTED) 0] create /node3 1
Created /node3
[zk: localhost:2181(CONNECTED) 1] addauth digest yoonper:123456 #新增認證使用者
[zk: localhost:2181(CONNECTED) 2] setAcl /node3 auth:yoonper:cdrwa
cZxid = 0x19000002b8
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x19000002b8
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x19000002b8
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
[zk: localhost:2181(CONNECTED) 3] getAcl /node3
'digest,'yoonper:UvJWhBril5yzpEiA2eV7bwwhfLs=
: cdrwa
[zk: localhost:2181(CONNECTED) 4] get /node3
1 #剛才已經新增認證使用者,可以直接讀取資料,斷開會話重連需要重新addauth新增認證使用者
cZxid = 0x1900000418
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x1900000418
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x1900000418
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
4、Digest方案 1)設定方式
setAcl <path> digest:<user>:<password>:<acl>
這裡的密碼是經過SHA1及BASE64處理的密文,在SHELL中可以通過以下命令計算:echo -n <user>:<password> | openssl dgst -binary -sha1 | openssl base64
先來計算一個密文
echo -n yoonper:123456 | openssl dgst -binary -sha1 | openssl base64
UvJWhBril5yzpEiA2eV7bwwhfLs=
2)客戶端例項
[zk: localhost:2181(CONNECTED) 0] create /node4 1
Created /node4
#使用是上面算好的密文密碼新增許可權:
[zk: localhost:2181(CONNECTED) 1] setAcl /node4 digest:yoonper:UvJWhBril5yzpEiA2eV7bwwhfLs=:cdrwa
cZxid = 0x19000002e3
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x19000002e3
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x19000002e3
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
[zk: localhost:2181(CONNECTED) 2] getAcl /node4
'digest,'yoonper:UvJWhBril5yzpEiA2eV7bwwhfLs=
: cdrwa
[zk: localhost:2181(CONNECTED) 3] get /node4
Authentication is not valid : /node4 #沒有許可權
[zk: localhost:2181(CONNECTED) 4] addauth digest yoonper:123456 #新增認證使用者
[zk: localhost:2181(CONNECTED) 5] get /node4
1 #成功讀取資料
cZxid = 0x1900000420
ctime = Thu May 11 22:00:00 CST 2017
mZxid = 0x1900000420
mtime = Thu May 11 22:00:00 CST 2017
pZxid = 0x1900000420
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
5、java客戶單例項:
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import com.zookeeper.utils.CommonParams;
public class Zookeeper_Acl_Create extends CommonParams implements Watcher {
private static CountDownLatch latch = new CountDownLatch(1);
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static ZooKeeper zk = null;
public void syncInit() {
try {
zk = new ZooKeeper(CONNECTION_IP, 5000,
new Zookeeper_Acl_Create());
latch.await();
zk.addAuthInfo("digest", "username:password".getBytes());
zk.create("/act", "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL);
ZooKeeper zk3 = new ZooKeeper(CONNECTION_IP, 5000,
null);
zk3.addAuthInfo("digest", "username:password".getBytes());
String value2 = new String(zk3.getData("/act", false, null));
System.out.println("zk3有許可權進行資料的獲取" + value2);
ZooKeeper zk2 = new ZooKeeper(CONNECTION_IP, 5000,
null);
zk2.addAuthInfo("digest", "super:123".getBytes());
zk2.getData("/act", false, null);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e) {
System.out.println("異常:" + e.getMessage());
System.out.println("zk2沒有許可權進行資料的獲取");
countDownLatch.countDown();
}
}
@Override
public void process(WatchedEvent event) {
if (KeeperState.SyncConnected == event.getState()) {
if (event.getType() == EventType.None && null == event.getPath()) {
latch.countDown();
}
}
}
public static void main(String[] args) throws InterruptedException {
Zookeeper_Acl_Create acl_Create = new Zookeeper_Acl_Create();
acl_Create.syncInit();
countDownLatch.await();
}
}
輸出:
zk3有許可權進行資料的獲取init
異常:KeeperErrorCode = NoAuth for /act
zk2沒有許可權進行資料的獲取