在這個教程中,我們将學習Hyperledger Fabric區塊鍊的通路控制清單(ACL)的配置與動态更新方法。教程分為兩個部分:1、了解并配置Hyperledger Fabric的通路控制清單2、動态更新通道配置中的通路控制清單。我們将介紹fabric中的預設ACL内容及格式,以通道管理者的角色進行通道ACL的配置管理。
相關教程: Fabric區塊鍊Java開發詳解 | Fabric區塊鍊Node.JS開發詳解
1、Hyperledger Fabric通路控制清單/ACL的基本概念
在Hyperledger Fabric中有兩種類型的通路控制政策:
- 簽名政策:Signature Policies
- 隐性元政策:Implicit Meta Policies
簽名政策通過檢查請求中的簽名來識别特定的使用者。例如:
Policies:
MyPolicy:
Type: Signature
Rule: “Org1.Peer OR Org2.Peer”
簽名政策支援的關鍵字包括:AND、OR和NOutOf,利用這幾個關鍵字可以組合出強大的通路控制規則,例如:
- A機構的管理者簽名的請求可以放行
- 20個機構中超過半數的管理者簽名的請求可以放行
隐性元政策則通過聚合後代簽名政策來定義通路控制規則,它支援預設的通路規則例如“超過半數的機構管理者簽名的請求可以放行”。隐性元政策的定義方法與簽名政策類似但略有差別,其形式如下:
<ALL|ANY|MAJORITY> <sub_policy>
下面是一個隐性元政策的示例:
Policies:
AnotherPolicy:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
2、Hyperledger Fabric的預設通路控制清單
預設的通路控制規則定義在configtx.yaml中,用來供configtxgen生成通道配置。在官方提供的
configtx.yaml示例中,第35行定義了簽名政策,第194行定義了隐性元政策,而第131行則定義了通路控制清單/ACL。
3、自定義Hyperledger Fabric的通路控制清單
讓我們編輯configtx.yaml中的
Application: ACLs
部分來修改以下内容:
peer/Propose: /Channel/Application/Writers
為:
peer/Propose: /Channel/Application/MyPolicy
其中MyPolicy這個政策定義如下:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
MyPolicy:
Type: Signature
Rule: "OR('Org1MSP.client')"
MyPolicy政策聲明了隻有Client角色可以執行相應的任務。
别忘了生成并更新CA和管理者證書。
現在讓我們嘗試從Org1Client來調用鍊碼:

現在使用Org2Client來調用鍊碼:
可以清楚的看到,peer/propose已經不接受ORG2的Client的調用了。
4、動态更新Hyperledger Fabric通道的ACL配置
有兩種方法可以用來更新通路控制政策:
- 編輯configtx.yaml,僅适用于後續建立的新通道
- 直接更新特定通道中的ACL配置,适用于已有的通道
在下面我們将展示如何更新已有通道中的通路控制清單配置。
在執行以下操作之前,記得先啟動你的Hyperledger Fabric網絡。
4.1 通路指令行接口
Hyperledger Fabric有一個自動建立的cli容器,可以提供操作節點的指令行接口。執行如下指令進入cli界面:
docker exec -it cli bash
然後設定程式需要使用的環境變量:
export CHANNEL_NAME=mychannel
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
4.2 擷取指定Fabric通道的目前配置
執行下面指令擷取通道的目前配置并寫入檔案config_block.pb:
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
4.3 将通道配置轉換為JSON格式
config_block.pb是二進制編碼的區塊配置資料,我們要将其先轉換為
容易檢視、修改的JSON格式:
configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
4.4 建立通道配置的JSON副本以便修改
後續的修改将在副本modified_config.json上進行:
cp config.json modified_config.json
4.5 修改JSON副本的通道配置
可以使用你喜歡的任何編輯器來修改JSON副本,比如用vim:
vim modified_config.json
我們将MyPolicy的描述從Org1MSP修改為Org2MSP:
修改後記得儲存。
4.6 将修改後的通道配置JSON副本轉換為二進制格式
configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb
4.7 将config.json轉換為區塊二進制格式
configtxlator proto_encode --input config.json --type common.Config --output config.pb
4.8 生成修改前後通道配置的差異
configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_config.pb --output diff_config.pb
4.9 将配置的差異部分轉換為JSON格式
configtxlator proto_decode --input diff_config.pb --type common.ConfigUpdate | jq . > diff_config.json
4.10 封裝Fabric配置更新消息
echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat diff_config.json)'}}}' | jq . > diff_config_envelope.json
4.11 将配置更新消息轉換為二進制格式
configtxlator proto_encode --input diff_config_envelope.json --type common.Envelope --output diff_config_envelope.pb
4.12 簽名配置更新消息
首先以Org1的管理者簽名,設定環境變量:
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
然後簽名:
peer channel signconfigtx -f diff_config_envelope.pb
然後以Org2的管理者身份簽名,設定環境變量:
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
簽名:
peer channel signconfigtx -f diff_config_envelope.pb
4.13 送出通道配置更新
執行如下指令向排序節點送出通道更新交易:
peer channel update -f diff_config_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
現在讓我們檢查下效果。首先用Org1的Client調用鍊碼:
果然失敗了。接下來用Org2的Client調用鍊碼:
和預期也一樣,成功了。
原文連結:
Hyperledger Fabric通路控制清單的配置與更新 - 彙智網