Rework: A Companion Guide to Fabric CA Operation Guides for Fabric v2.2

Introduction

This is another rework on my recent article. The source is a tutorial of Fabric CA in Hyperledger Fabric documentation. The tutorial was done with version 1.4, and my first work done early this July was also made on the same version. Since then I keep receiving queries to make it running in v2.0, as there are many changes from v1.4 to v2.0 and the commands are not used directly in v2.0. As a result I rewrite this companion guide to make it workable in v2.2.

We follow the same network design and setup. Unlike the previous work, we consolidate the four scripts in our previous work into one, bringing up all the CA and registering all the entities. No CLI containers are needed as we are using binary directly in the localhost. Meanwhile, all material is now in this repository.

Here in this article I just highlight the difference from the previous one, and most of the explanation is omitted. You can always refer to my previous work for more detailed explanation.

Network Design and Setup

Here is a quick summary of the network design and setup in this tutorial.

Fabric network looks like this

Image for post
Image for post
The fabric network of this tutorial

Each organization comes with an identity CA (organization root CA), which issues the certificates for every component and user in that organization. In addition, one TLS CA is responsible for issuing TLS server certificates for network components (orderer and peer) of ALL organizations. As a result, there are four CAs in this tutorial.

Image for post
Image for post
Four CAs in this setup and those entities each CA is issuing certificate

Demonstration

Step 1: Preparation

Clone the repository if not done before

All material is now in the repository. We will refer to these files later. Meanwhile, for sake of consistency, change the directory name to .

cd fabric-samples
git clone https://github.com/kctam/fabric-ca-tutorial-v2.git
// without changing the document we rename the folder
mv fabric-ca-tutorial-v2 guide

Set path for fabric binaries if not done before

In the script we will directly use , which is inside . Set the environment variable if not done yet

cd fabric-samples
export PATH=$PATH:${PWD}/bin/
// use this to see if we can reach fabric-ca-client
which fabric-ca-client
Image for post
Image for post

Tear down everything

We just make sure we have a clean environment. Also, as all crypto material generated is kept in , we empty this directory as well.

cd fabric-samples/guide/
docker-compose down
docker rm $(docker ps -aq)
docker rmi $(docker images dev-* -q)
// remove any material of previous deployment
mkdir -p /tmp/hyperledger
cd /tmp/hyperledger
rm -rf *

Bring up two terminals, Org1 and Org2

As we will use two terminals accessing each peer, two files are prepared for all the environment variables needed. Open two terminals and include those variables.

// terminal for Org1 (blue background colour)
source terminalorg1
// terminal for Org2 (green background colour)
source terminalorg2
Image for post
Image for post
Terminal for Org1
Image for post
Image for post
Terminal for Org2

Step 2: Bring Up the four CAs

In the main terminal,

cd fabric-samples/guide/docker-compose up -d ca-tls rca-org0 rca-org1 rca-org2
Image for post
Image for post

Note: in case you have permission problems, change owner of the . For example,

sudo chown -R ubuntu:ubuntu /tmp/hyperledger/*

Step 3: Execute Registration and Enrollment scripts

As mentioned before, we have prepared two scripts to process all the registration and enrollment for the network.

Enrol registrar of each CA and register all entities

For each CA, we first enrol a registrar. After that, let this registrar register each entity into the CA database. Note the of each entity. Besides, we have put an attribute to admin-org2 as we are going to test attribute-based access control (ABAC) later in Step 7 when we deploy chaincode.

./allCAnReg.sh

Enrol entities one by one for each organization

For each organization, we now enrol each entity (network component and user) from the CAs. In the repository we have prepared three configuration files () for node OU, one for each organization. They are copied to the organizations respectively.

./enrollAllOrg.sh

Step 4: Prepare channel artifacts

We are preparing the three artifacts: one consortium genesis block, one channel transaction and one anchor peer update. We keep them in directory.

In the main terminal,

configtxgen -profile OrgsOrdererGenesis -outputBlock /tmp/hyperledger/org0/orderer/genesis.block -channelID syschannelconfigtxgen -profile OrgsChannel -outputCreateChannelTx /tmp/hyperledger/org0/orderer/channel.tx -channelID mychannelconfigtxgen -profile OrgsChannel -outputAnchorPeersUpdate /tmp/hyperledger/org0/orderer/org1MSPanchors.tx -channelID mychannel -asOrg org1MSP
Image for post
Image for post

Step 5: Bring up the whole network

In the main terminal,

docker-compose up -d

all the components (four peers and one orderer) are up and running

Image for post
Image for post

Step 6: Create and Join Channel

Create channel genesis block

Terminal for Org1

peer channel create -c mychannel -f /tmp/hyperledger/org0/orderer/channel.tx -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --outputBlock /tmp/hyperledger/org1/peer1/assets/mychannel.block --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem

Join peers of Org1 to mychannel

Note that the default environment variable is set for peer1-org1. For peer2-org1, we specify the correct .

Terminal for Org1

peer channel join -b /tmp/hyperledger/org1/peer1/assets/mychannel.blockCORE_PEER_ADDRESS=localhost:8051 peer channel join -b /tmp/hyperledger/org1/peer1/assets/mychannel.block

Join peers of Org2 to mychannel

Similar, the default environment variable is set for peer1-org2. For peer2-org2, we specify the correct .

Terminal for Org2

peer channel join -b /tmp/hyperledger/org1/peer1/assets/mychannel.blockCORE_PEER_ADDRESS=localhost:10051 peer channel join -b /tmp/hyperledger/org1/peer1/assets/mychannel.block

Update anchor peer for Org1

Terminal for Org1

peer channel update -c mychannel -f /tmp/hyperledger/org0/orderer/org1MSPanchors.tx -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem

Check if all the four peers having the same ledger

After all peers have joined the same channel, they will have the same ledger. We use to check both the blockchain height and current block hash are identical for all peers.

Terminal for Org1

peer channel getinfo -c mychannelCORE_PEER_ADDRESS=localhost:8051 peer channel getinfo -c mychannel
Image for post
Image for post
Blockchain information of peer1-org1 and peer2-org1

Terminal for Org2

peer channel getinfo -c mychannelCORE_PEER_ADDRESS=localhost:10051 peer channel getinfo -c mychannel
Image for post
Image for post
Blockchain information of peer1-org2 and peer2-org2

Step 7: Deploy chaincode abac

Note since Fabric v2.0, chaincode is deployed through lifecycle chaincode. You can get more ideas about the difference between v1.4 and v2.0 here.

Package chaincode abac

Terminal for Org1

// if not done before
cd fabric-samples/chaincode/abac/go/
GO111MODULE=on go mod vendor
cd fabric-samples/guide
peer lifecycle chaincode package abac.tar.gz --path ../chaincode/abac/go/ --label abac_1
Image for post
Image for post

Install chaincode to peer1-org1 and peer1-org2

Terminal for Org1 and Org2

peer lifecycle chaincode install abac.tar.gz
Image for post
Image for post
Chaincode installation: Terminal for Org1
Image for post
Image for post
Chaincode installation: Terminal for Org2

Approve chaincode for Org1 and Org2

Use the package ID obtained from installation.

Terminal for Org1 and Org2

peer lifecycle chaincode approveformyorg --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --channelID mychannel --name mycc --version 1 --sequence 1 --waitForEvent --init-required --package-id abac_1:fd3168223d23448531a02d6f52e33c5e8ea3a63cbf6e53a0093aa3a60bd70a87
Image for post
Image for post
Chaincode approval: Terminal for Org1
Image for post
Image for post
Chaincode approval: Terminal for Org2

Commit chaincode

Terminal for Org1

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --peerAddresses localhost:7051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --peerAddresses localhost:9051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --channelID mychannel --name mycc --init-required --version 1 --sequence 1
Image for post
Image for post
Chaincode commit

Step 8: Test chaincode abac

Invoke chaincode function Init()

The chaincode abac is the same as abstore. The major difference is that the init() can only be invoked by a user with abac.init attribute set. In our setup, only admin-org2 has this attribute (see Step 3). Environment variable for terminal 1 is set to admin-org1, for terminal 2 is set to admin-org2.

We will invoke this init() in both terminal 1 and terminal 2, and we can see invocation in terminal 1 fails while that in terminal 2 is successful.

On both terminals,

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --tls true --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem -C mychannel -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --peerAddresses localhost:9051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --isInit -c '{"Args":["init","a","100","b","200"]}'
Image for post
Image for post
Terminal for Org1: failure to invoke init(), as abac.init is not in admin-org1
Image for post
Image for post
Terminal for Org2: successful as abac.init is in admin-org2

Query values from both peers

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Image for post
Image for post
Terminal for Org1: peer1-org1
Image for post
Image for post
Terminal for Org2: peer1-org2

Invoke chaincode function invoke()

The “restriction” on admin-org2 is only applicable in init(). We will invoke invoke() in the code using terminal 1. And the invocation is successful.

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer1-org0 --tls true --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem -C mychannel -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem --peerAddresses localhost:9051 --tlsRootCertFiles /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem -c '{"Args":["invoke","a","b","10"]}'
Image for post
Image for post

Query values from both peers

The ledger is updated correctly.

Image for post
Image for post
Image for post
Image for post

This is the end of demonstration.

Written by

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store