Lab04 Exploring Attribute-Based Access Control (ABAC)
Lab04 Exploring Attribute-Based Access Control (ABAC)
04
Attribute based access control
1. Introduction...........................................................................................................1
2. Start the network and deploy the smart contract...............................................2
3. Register identities with attributes........................................................................2
4. Create an asset.......................................................................................................3
5. Transfer the asset...................................................................................................4
6. Update the asset.....................................................................................................5
7. Delete the asset.......................................................................................................6
8. Clean up..................................................................................................................7
9. Reference................................................................................................................7
>> Yêu cầu chụp hình ảnh là kết quả thực hành của SV. Không sử dụng lại hình ảnh của
bài lab.
1. Introduction
The asset-transfer-abac sample demonstrates the use of Attribute-based
access control within the context of a simple asset transfer scenario. The
sample also uses authorization based on individual client identities to allow
the users that interact with the network to own assets on the blockchain
ledger.
We will create the identities using the Org1 CA. Set the Fabric CA client
home to the MSP of the Org1 CA admin:
There are two ways to generate certificates with attributes added. We will
use both methods and create two identities in the process. The first method
is to specify that the attribute be added to the certificate by default when
the identity is registered. The following command will register an identity
named creator1 with the attribute of abac.creator=true.
The ecert suffix adds the attribute to the certificate automatically when
the identity is enrolled. As a result, the following enroll command will contain
the attribute that was provided in the registration command.
Now that we have enrolled the identity, run the command below to copy
the Node OU configuration file into the creator1 MSP folder.
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ cp
"${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml"
"${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]
e.com/msp/config.yaml"
The following enroll command will add the attribute to the certificate:
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ fabric-ca-client enroll -u
https://creator2:creator2pw@localhost:7054 --caname ca-org1 --enrollment.attrs
"abac.creator" -M
"${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]
e.com/msp" --tls.certfiles "${PWD}/organizations/fabric-ca/org1/tls-cert.pem"
Run the command below to copy the Node OU configuration file into the
creator2 MSP folder.
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ cp
"${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml"
"${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]
e.com/msp/config.yaml"
3. Create an asset
You can use either identity with the abac.creator=true attribute to create
an asset using the asset-transfer-abac smart contract. We will set the
following environment variables to use the first identity that was generated,
creator1:
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export
CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.co
m/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/
org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051
export TARGET_TLS_OPTIONS=(-o localhost:7050 --ordererTLSHostnameOverride
orderer.example.com --tls --cafile
"${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/
msp/tlscacerts/tlsca.example.com-cert.pem" --peerAddresses localhost:7051 --
tlsRootCertFiles
"${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.co
m/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles
"${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.co
m/tls/ca.crt")
The result will list the creator1 identity as the asset owner. The GetID() API
reads the name and issuer from the certificate of the identity that submitted
the transaction and assigns that identity as the asset owner:
{"ID":"Asset1","color":"blue","size":20,"owner":"x509::CN=creator1,OU=clien
t+OU=org1,O=Hyperledger,ST=North
Carolina,C=US::CN=ca.org1.example.com,O=org1.example.com,L=Durham,ST=Nort
h Carolina,C=US","appraisedValue":100}
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ export
RECIPIENT="x509::CN=user1,OU=client,O=Hyperledger,ST=North
Carolina,C=US::CN=ca.org1.example.com,O=org1.example.com,L=Durham,ST=North
Carolina,C=US"
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ peer chaincode invoke "$
{TARGET_TLS_OPTIONS[@]}" -C mychannel -n abac -c '{"function":"TransferAsset","Args":
["Asset1","'"$RECIPIENT"'"]}'
Query the ledger to verify that the asset has a new owner:
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ peer chaincode query -C
mychannel -n abac -c '{"function":"ReadAsset","Args":["Asset1"]}'
Even though creator1 can create new assets, the smart contract detects
that the transaction was not submitted by the identity that owns the asset,
user1. The command returns the following error:
We can now update the asset. Run the following command to change the
asset color from blue to green. All other aspects of the asset will remain
unchanged.
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ peer chaincode invoke "$
{TARGET_TLS_OPTIONS[@]}" -C mychannel -n abac -c '{"function":"UpdateAsset","Args":
["Asset1","green","20","100"]}'
Run the query command again to verify that the asset has changed color:
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ peer chaincode query -C
mychannel -n abac -c '{"function":"ReadAsset","Args":["Asset1"]}'
If you query the ledger once more, you will see that Asset1 no longer
exists:
ubuntu@vmhyper:~/fabric-samples/asset-transfer-abac$ peer chaincode query -C
mychannel -n abac -c '{"function":"ReadAsset","Args":["Asset1"]}'
7. NOTE
NOTE: Do not execute commands 1®3 when performing remote access on
itfdut.ddns.net server because the test-network has already been started.
Start the network and deploy the smart contract
We can use the Fabric test network to deploy and interact with the asset-
transfer-abac smart contract. Run the following command to change into the
test network directory and bring down any running nodes:
cd fabric-samples/test-network
./network.sh down
Run the following command to deploy the test network using Certificate
Authorities:
./network.sh up createChannel -ca
You can then use the test network script to deploy the asset-transfer-
abac smart contract to a channel on the network:
./network.sh deployCC -ccn abac -ccp ../asset-transfer-abac/chaincode-go/ -ccl go
When you are finished, you can run the following command to bring down
the test network: network.sh down
-----------------------------------------------