CentOS7 - Hyperledger Fabric1.1多機部署中切換預設生成的證書為Fabric-CA
根據蒐集的資料安裝測試並在安裝測試過程中整理的文件,如有不足希望不吝賜教。
目錄
一、前提
- 本文件是在《CentOS7中hyperledger-fabric1.1 - 2+6多機安裝部署、部分異常處理以及使用configtxlator對區塊基本資訊查詢(kafka共識,手動非docker方式)》的基礎上進行修改,更理想的情況是在配置多節點fabric網路時就使用fabric-ca。
- 安裝go1.9+,並設定正確的GOPATN環境變數
- 安裝libtool包:yum -y install libtool
參考:fabric-ca 部署環境
https://blog.csdn.net/xxzjwdnlwx/article/details/80736670
二、安裝部署Fabric-CA
1.下載安裝
執行如下程式碼(以下使用fabric-ca 1.1.0版本,請自行選擇版本):
go get -u github.com/hyperledger/fabric-ca/cmd/...
或者使用:
go get -u github.com/hyperledger/fabric-ca/cmd/fabric-ca-server
go get -u github.com/hyperledger/fabric-ca/cmd/fabric-ca-client
注意:如果已經克隆了fabric-ca倉庫,請確保在執行“go get”命令之前已經在主分支上了。否則,可能會報錯。
執行完畢後會在$GOPATH/bin目錄下看到生成的2個檔案:fabric-ca-client、fabric-ca-server
【異常及解決辦法】
如果有error: RPC failed; result=18, HTTP code = 200錯誤,可以試試如下命令:
git config --global http.postBuffer 24288000
完成後可以通過如下命令檢視。git config --list
如果go get或者git clone命令速度很慢或者為0,可以嘗試修改hosts檔案:vim /etc/hosts新增如下內容並重試:
192.30.253.112 github.com
使用git clone命令也可以新增引數--depth=1只拉取最近一個revision,類似:
git clone --depth=1 https://github.com/hyperledger/fabric-ca.git
2.部署
這裡將fabric-ca部署在/opt/app/fabric-ca/server目錄中,分別執行如下三行命令:
mkdir -p /opt/app/fabric-ca/server
cp -rf /opt/gopath/bin/* /opt/app/fabric-ca/server
ln -s /opt/app/fabric-ca/server/fabric-ca-client /usr/bin/fabric-ca-client
使用下面兩行命令直接啟動ca,fabric-ca admin的名稱為admin,密碼為pass。(這裡只是演示,生產中使用,你需要根據實際的情況配置)
cd /opt/app/fabric-ca/server
./fabric-ca-server start -b admin:pass --cfg.affiliations.allowremove --cfg.identities.allowremove &
注意:這裡只是演示用法,直接用sqlite儲存使用者資訊,生產中,請根據情況配置ldap或者mysql等資料庫:HyperLedger FabricCA Config Database and LDAP。
【異常及解決辦法】
如果啟動報錯:Version is not set for fabric-ca library,這是因為在啟動時呼叫了lib/metadata包中version.go的Version變數,但是在初始化啟動時預設沒有賦值。可通過如下兩種辦法解決:
1.直接在變數後賦值
修改:/opt/gopath/src/github.com/hyperledger/fabric-ca/lib/metadata/version.go
vim /opt/gopath/src/github.com/hyperledger/fabric-ca/lib/metadata/version.go
在文中找到var Version string修改為:var Version string = "1.1.0"重新編譯(go get …)即可;
2.可在編譯時賦值
go get -v -ldflags "-X github.com/hyperledger/fabric-ca/lib/metadata.Version=1.1.0" github.com/hyperledger/fabric-ca/cmd/...
三。生成證書
1.生成fabric-ca admin證書
建立所需資料夾,分別執行下面三條命令:
cd ~
mkdir fabriccert
mkdir fabric-ca-files
使用下面兩行命令生成fabric-ca admin的憑證,用-H引數指定client目錄:
mkdir -p `pwd`/fabric-ca-files/admin
fabric-ca-client enroll -u http://admin:[email protected]:7054 -H `pwd`/fabric-ca-files/admin
完成後目錄結構如圖(tree可能需要安裝):
也可以用環境變數FABRIC_CA_CLIENT_HOME指定了client的工作目錄,生成的使用者憑證將存放在這個目錄中。使用如下三行命令:
export FABRIC_CA_CLIENT_HOME=`pwd`/fabric-ca-files/admin mkdir -p $FABRIC_CA_CLIENT_HOME fabric-ca-client enroll -u http://admin:[email protected]:7054
為了防止混亂,後面的演示操作中,都直接用-H指定目錄。
2建立聯盟.
上面啟動方式會預設建立兩個組織,如下:
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation list
如圖
為了檢視資訊的時候,看到的輸出比較簡潔,用下面的命令將其刪除:
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation remove --force org1;
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation remove --force org2;
如圖:
然後執行下面命令建立聯盟:
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation add com
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation add com.example
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation add com.example.org1
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation add com.example.org2
fabric-ca-client -H `pwd`/fabric-ca-files/admin affiliation add com.example.org3
建立聯盟如下:
3.為每個組織準備msp
為example.com準備msp,將ca證書等存放example.com組織的目錄中:
mkdir -p ./fabric-ca-files/example.com/msp
fabric-ca-client getcacert -M `pwd`/fabric-ca-files/example.com/msp
完成後會得到證書檔案:
注意通過getcacert得到msp目錄中只有CA證書。
同樣的方式為org1.example.com、org2.example.com和org3.example.com獲取msp(每行一條命令):
mkdir -p fabric-ca-files/org1.example.com/msp
mkdir -p fabric-ca-files/org2.example.com/msp
mkdir -p fabric-ca-files/org3.example.com/msp
fabric-ca-client getcacert -M `pwd`/fabric-ca-files/org1.example.com/msp
fabric-ca-client getcacert -M `pwd`/fabric-ca-files/org2.example.com/msp
fabric-ca-client getcacert -M `pwd`/fabric-ca-files/org3.example.com/msp
這裡是用getcacert為每個組織準備需要的ca檔案,在生成創始塊的時候會用到。
在1.1.0版本的fabric-ca中,只會生成元件或使用者在操作區塊鏈的時候用到的證書和金鑰,不會生成用來加密grpc通訊的證書(tls證書)。這裡繼續沿用之前cryptogen生成的tls證書,在最後的重新部署操作,只會替換msp目錄。
但是需要將驗證tls證書的ca新增到msp目錄中,如下(路徑要根據自己實際情況修改):
cp -rf /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/msp/tlscacerts fabric-ca-files/example.com/msp/
cp -rf /etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/msp/tlscacerts fabric-ca-files/org1.example.com/msp/
cp -rf /etc/hyperledger/fabric/crypto-config/peerOrganizations/org2.example.com/msp/tlscacerts fabric-ca-files/org2.example.com/msp/
cp -rf /etc/hyperledger/fabric/crypto-config/peerOrganizations/org3.example.com/msp/tlscacerts fabric-ca-files/org3.example.com/msp/
如果在你的環境中,各個元件域名的證書,是由第三方CA簽署的,就將第三方CA的根證書新增到tlscacerts目錄中。
4.註冊example.com的管理員
註冊example.com的管理員[email protected]
將fabric-ca-files/admin/fabric-ca-client-config.yaml其中的id部分修改為:
id:
name: [email protected]
type: client
affiliation: com.example
maxenrollments: 0
attributes:
- name: hf.Registrar.Roles
value: client,orderer,peer,user
- name: hf.Registrar.DelegateRoles
value: client,orderer,peer,user
- name: hf.Registrar.Attributes
value: "*"
- name: hf.GenCRL
value: true
- name: hf.Revoker
value: true
- name: hf.AffiliationMgr
value: true
- name: hf.IntermediateCA
value: true
- name: role
value: admin
ecert: true
注意最後一行role屬性,是我們自定義的屬性,在配置檔案中是單獨設定ecert屬性為true或者false,如果在命令列中,新增字尾:ecert表示true。
直接執行下面的命令,即可完成使用者[email protected]註冊,注意這時候的註冊使用fabricCA的admin賬號完成的:
fabric-ca-client register -H `pwd`/fabric-ca-files/admin --id.secret=password
如果不用--id.secret指定密碼,會自動生成密碼。
其它配置的含義是使用者名稱為[email protected],型別是client,它能夠管理com.example.*下的使用者,如下:
--id.name [email protected] //使用者名稱
--id.type client //型別為client
--id.affiliation "com.example" //權利訪問
hf.Registrar.Roles=client,orderer,peer,user //能夠管理的使用者型別
hf.Registrar.DelegateRoles=client,orderer,peer,user //可以授權給子使用者管理的使用者型別
hf.Registrar.Attributes=* //可以為子使用者設定所有屬性
hf.GenCRL=true //可以生成撤銷證書列表
hf.Revoker=true //可以撤銷使用者
hf.AffiliationMgr=true //能夠管理聯盟
hf.IntermediateCA=true //可以作為中間CA
role=admin:ecert //自定義屬性
生成[email protected]憑證:
mkdir -p ./fabric-ca-files/example.com/admin
fabric-ca-client enroll -u http://[email protected]:[email protected]:7054 -H `pwd`/fabric-ca-files/example.com/admin
ls ./fabric-ca-files/example.com/admin
這時候可以用[email protected]的身份檢視聯盟:
fabric-ca-client affiliation list -H `pwd`/fabric-ca-files/example.com/admin
最後需要將[email protected]的證書複製到example.com/msp/admincerts/
mkdir fabric-ca-files/example.com/msp/admincerts/
cp fabric-ca-files/example.com/admin/msp/signcerts/cert.pem fabric-ca-files/example.com/msp/admincerts/
只有這樣,才能具備管理員許可權。
5.註冊org1.example.com的管理員
註冊org1.example.com的管理員[email protected]
mkdir -p ./fabric-ca-files/org1.example.com/admin
修改fabric-ca-files/admin/fabric-ca-client-config.yaml中的id部分:
vim fabric-ca-files/admin/fabric-ca-client-config.yaml
修改為:
id:
name: [email protected]
type: client
affiliation: com.example.org1
maxenrollments: 0
attributes:
- name: hf.Registrar.Roles
value: client,orderer,peer,user
- name: hf.Registrar.DelegateRoles
value: client,orderer,peer,user
- name: hf.Registrar.Attributes
value: "*"
- name: hf.GenCRL
value: true
- name: hf.Revoker
value: true
- name: hf.AffiliationMgr
value: true
- name: hf.IntermediateCA
value: true
- name: role
value: admin
ecert: true
註冊:
fabric-ca-client register -H `pwd`/fabric-ca-files/admin --id.secret=password
生成憑證:
fabric-ca-client enroll -u http://[email protected]:[email protected]:7054 -H `pwd`/fabric-ca-files/org1.example.com/admin
檢視聯盟:
fabric-ca-client affiliation list -H `pwd`/fabric-ca-files/org1.example.com/admin
注意與[email protected]的區別,這裡只能看到組織com.example.org1。
將[email protected]的證書複製到org1.example.com的msp/admincerts中:
mkdir fabric-ca-files/org1.example.com/msp/admincerts/
cp fabric-ca-files/org1.example.com/admin/msp/signcerts/cert.pem fabric-ca-files/org1.example.com/msp/admincerts/
在[email protected]中也需要建立msp/admincerts目錄,通過peer命令操作fabric的時候會要求admincerts存在:
mkdir fabric-ca-files/org1.example.com/admin/msp/admincerts/
cp fabric-ca-files/org1.example.com/admin/msp/signcerts/cert.pem fabric-ca-files/org1.example.com/admin/msp/admincerts/
如下:
另外,這裡沒有使用中間CA,將intermediatecerts中的空檔案刪除,否則peer會提示Warning(注:在實際操作中發現,文中指出的並沒有刪除intermediatecerts目錄,因此在最後需要自行檢查一次以確認全部刪除):
rm fabric-ca-files/org1.example.com/admin/msp/intermediatecerts/*
6.註冊org2.example.com的管理員
6、7與5類似,以下只簡單記錄。
註冊org2.example.com的管理員[email protected]
mkdir -p ./fabric-ca-files/org2.example.com/admin
修改fabric-ca-files/admin/fabric-ca-client-config.yaml中的id部分
vim fabric-ca-files/admin/fabric-ca-client-config.yaml
修改為:
id:
name: [email protected]
type: client
affiliation: com.example.org2
maxenrollments: 0
attributes:
- name: hf.Registrar.Roles
value: client,orderer,peer,user
- name: hf.Registrar.DelegateRoles
value: client,orderer,peer,user
- name: hf.Registrar.Attributes
value: "*"
- name: hf.GenCRL
value: true
- name: hf.Revoker
value: true
- name: hf.AffiliationMgr
value: true
- name: hf.IntermediateCA
value: true
- name: role
value: admin
ecert: true
註冊:
fabric-ca-client register -H `pwd`/fabric-ca-files/admin --id.secret=password
生成憑證:
fabric-ca-client enroll -u http://[email protected]:[email protected]:7054 -H `pwd`/fabric-ca-files/org2.example.com/admin
檢視聯盟:
fabric-ca-client affiliation list -H `pwd`/fabric-ca-files/org2.example.com/admin
注意與[email protected]的區別,這裡只能看到組織com.example.org2。
將[email protected]的證書複製到org2.example.com的msp/admincerts中:
mkdir fabric-ca-files/org2.example.com/msp/admincerts/
cp fabric-ca-files/org2.example.com/admin/msp/signcerts/cert.pem fabric-ca-files/org2.example.com/msp/admincerts/
在[email protected]中也需要建立msp/admincerts目錄,通過peer命令操作fabric的時候會要求admincerts存在:
mkdir fabric-ca-files/org2.example.com/admin/msp/admincerts/
cp fabric-ca-files/org2.example.com/admin/msp/signcerts/cert.pem fabric-ca-files/org2.example.com/admin/msp/admincerts/
另外,這裡沒有使用中間CA,將intermediatecerts中的空檔案刪除,否則peer會提示Warning(注:在實際操作中發現,文中指出的並沒有刪除intermediatecerts目錄,因此在最後需要自行檢查一次以確認全部刪除):
rm fabric-ca-files/org2.example.com/admin/msp/intermediatecerts/*
7.註冊org3.example.com的管理員
略。參考5、6
四、各個組織分別使用自己的Admin賬戶建立其它賬號
example.com、org1.example.com、org2.example.com、org3.example.com四個組織這時候可以分別使用自己的Admin賬號建立子賬號。
1.orderer0.example.com
使用[email protected]註冊賬號orderer0.example.com。注意這時候指定的目錄是fabric-ca-files/example.com/admin/。
修改fabric-ca-files/example.com/admin/fabric-ca-client-config.yaml:
vim fabric-ca-files/example.com/admin/fabric-ca-client-config.yaml
內容如下:
id:
name: orderer0.example.com
type: orderer
affiliation: com.example
maxenrollments: 0
attributes:
- name: role
value: orderer
ecert: true
註冊以及生成憑證:
fabric-ca-client register -H `pwd`/fabric-ca-files/example.com/admin --id.secret=password
mkdir ./fabric-ca-files/example.com/orderer0
fabric-ca-client enroll -u http://orderer0.example.com:[email protected]:7054 -H `pwd`/fabric-ca-files/example.com/orderer0
將[email protected]的證書複製到fabric-ca-files/example.com/orderer0/msp/admincerts:
mkdir fabric-ca-files/example.com/orderer0/msp/admincerts
cp fabric-ca-files/example.com/admin/msp/signcerts/cert.pem fabric-ca-files/example.com/orderer0/msp/admincerts/
2.orderer1.example.com
略。參考1、orderer0.example.com
3.peer0.org1.example.com
使用[email protected]註冊賬號peer0.org1.example.com。這時候指定的目錄是fabric-ca-files/org1.example.com/admin/。
修改fabric-ca-files/org1.example.com/admin/fabric-ca-client-config.yaml:
vim fabric-ca-files/org1.example.com/admin/fabric-ca-client-config.yaml
內容如下:
id:
name: peer0.org1.example.com
type: peer
affiliation: com.example.org1
maxenrollments: 0
attributes:
- name: role
value: peer
ecert: true
註冊以及生成憑證:
fabric-ca-client register -H `pwd`/fabric-ca-files/org1.example.com/admin --id.secret=password
mkdir ./fabric-ca-files/org1.example.com/peer0
fabric-ca-client enroll -u http://peer0.org1.example.com:[email protected]:7054 -H `pwd`/fabric-ca-files/org1.example.com/peer0
將[email protected]的證書複製到fabric-ca-files/org1.example.com/peer0/msp/admincerts:
mkdir fabric-ca-files/org1.example.com/peer0/msp/admincerts
cp fabric-ca-files/org1.example.com/admin/msp/signcerts/cert.pem fabric-ca-files/org1.example.com/peer0/msp/admincerts/
4.peer1.org1.example.com
略。參考:
1.orderer0.example.com
3.peer0.org1.example.com
5.peer0.org2.example.com
略。參考:
1.orderer0.example.com
3.peer0.org1.example.com
6.peer1.org2.example.com
略。參考:
1.orderer0.example.com
3.peer0.org1.example.com
7.peer0.org3.example.com
略。參考:
1.orderer0.example.com
3.peer0.org1.example.com
8.peer1.org3.example.com
略。參考:
1.orderer0.example.com
3.peer0.org1.example.com
五、重新部署啟動fabric網路
1.修改configtx.yaml
因為我們不能保證一次成功,因此在重新部署之前需要對之前的配置做一個簡單的備份。在編輯本文件的時候備份過程如下:
- 首先關閉fabric網路,關閉方法參考《CentOS7 - fabric1.1多機部署時切換預設goleaveldb為CouchDB》;
- 除了orderer0機器以外(留用crypto-config目錄修改,其餘檔案與其他機器類似),刪除其他機器上/etc/hyperledger/fabric目錄下除*.yaml全部檔案(如非必要*.pak檔案也可以保留);
- 備份fabric目錄,執行以下命令:cp -r /etc/hyperledger/fabric /etc/hyperledger/fabric-bak
- 刪除kafka所有topic,參考《centos - 環境構建-centOS安裝jdk、zk和kafka》
- 刪除所有couchdb中資料(如果沒有替換預設leavedb為couchdb則忽略)
- 刪除所有機器上/var/hyperledger/production目錄下檔案
- 刪除所有/data/hyperledger/production目錄下檔案(如果有)
執行完上面的操作後進入/etc/hyperledger/fabric目錄,修改configtx.yaml,將其中的msp路徑修改為通過fabric-ca建立的msp目錄,修改部分如下:
2.替換msp
替換example.com中的msp
rm -rf crypto-config/ordererOrganizations/example.com/msp
cp -rf /root/fabric-ca-files/example.com/msp crypto-config/ordererOrganizations/example.com/
替換orderer0.example.com中的msp
rm -rf crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp
cp -rf /root/fabric-ca-files/example.com/orderer0/msp crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/
替換orderer1.example.com中的msp
rm -rf crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp
cp -rf /root/fabric-ca-files/example.com/orderer1/msp crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/
替換org1.example.com中的msp
rm -rf crypto-config/peerOrganizations/org1.example.com/msp
cp -rf /root/fabric-ca-files/org1.example.com/msp crypto-config/peerOrganizations/org1.example.com/
替換peer0.org1.example.com中的msp
rm -rf crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/
cp -rf /root/fabric-ca-files/org1.example.com/peer0/msp crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/
替換peer1.org1.example.com中的msp
rm -rf crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp/
cp -rf /root/fabric-ca-files/org1.example.com/peer1/msp crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/
其餘org2和org3類似org1,參考上文即可,
因為替換目錄比較繁瑣,因此,可以將替換的命令寫到一個簡單的sh指令碼中執行(上面生成證書的步驟以及下面第3步中大量重複的的命令都可以,有興趣的可以自己搞搞),檢查無誤後使用指令碼替換檔案,以下是一個簡單的指令碼檔案示例,自己才學疏淺,只能保證基本能用,但這一定不是最優,如果有更好的希望不吝賜教:
#!/bin/bash
echo "start"
echo "example.com"
rm -rf crypto-config/ordererOrganizations/example.com/msp
cp -rf /root/fabric-ca-files/example.com/msp crypto-config/ordererOrganizations/example.com/
echo "orderer0.example.com"
rm -rf crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp
cp -rf /root/fabric-ca-files/example.com/orderer0/msp crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/
echo "end"
注意:以上替換完成後並沒有替換徹底,經過觀察發現部分目錄下沒有tlscacerts目錄,如/etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/下,需要從別的位置將其拷貝一份即可。
3.異常及解決辦法
如果只執行了以上操作,那麼在執行建立channel過程中可能會報錯:
Error: got unexpected status: BAD_REQUEST -- error authorizing update: error validating DeltaSet: policy for [Group] /Channel/Application not satisfied: Failed to reach implicit threshold of 1 sub-policies, required 1 remaining
錯誤原因是[email protected] 等本地admin沒有設定成ca生成的證書,還是使用之前的證書,替換之後不再報錯。
具體需要根據自己的配置進行替換或者自行對比原始目錄與新目錄,以下貼出大致的替換規則可作為參考:
替換orderer
將/etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/下檔案替換為/root/fabric-ca-files/example.com/admin/msp/下相應檔案(好像是/root/fabric-ca-files/admin/msp/記不清了)
替換org1(org2和org3類似)
將/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/下檔案替換為/root/fabric-ca-files/org1.example.com/admin/msp/下相應檔案
在執行替換證書的過程中,一定要清楚要替換的證書的目錄,否則很容易出現替換不完全,報錯的情況。
4.重新啟動專案
之前的操作如果沒有特殊情況都是在同一臺機器上執行,本例使用orderer0機器,因此,需要將修改完成的crypto-config目錄拷貝到其他機器/etc/hyperledger/fabric目錄中。
之後的操作可以參考《CentOS7中hyperledger-fabric1.1 - 2+6多機安裝部署》文中第四、五部分,此處不再贅述。
六、其他
1.檢視某個端口占用(以7054為例)
netstat -lnp | grep 7054
2.檢視某個程式程序(以fabric為例)
ps -ef|grep fabric|grep -v grep
3.git檢視&切換程式碼版本
檢視當前使用分支版本:
git branch -a
切換版本,以切換到 release-1.1為例:
git checkout -b release-1.1 origin/release-1.1
再次再次檢視當前分支版本:
可見當前已經切換到release-1.1,切換完成。
END