I recently stumbled across the Using SideDB to Store Private Data SAP Help article and decided to try out the described functionality. It turned out that the example code provided in the article was created for Hyperledger Fabric v1.2 and can actually be simplified for the current version 1.3 available on the SAP Cloud Platform.
Let's have a closer look.
In order to get the right context of the task, please have a look at this document explaining the terminology and use cases for private data. Here in this blog we would just like to create our own implementation of the service deployed on the SAP Cloud Platform, which would save and retrieve the private data passed inside transient fields. We would also like to check how to configure access to the private data.
As you can see in the original article already mentioned above, the access control logic for the private data has to be implemented by developers themselves. This is despite the fact that developers also have to describe access by organisations in the collection-config.json file.
According to the v1.4 release notes, Hyperledger Fabric service is able to automatically enforce access control based on the collection-config.json definition only.
However, the current Hyperledger Fabric service version deployed to the SAP Cloud Platform is 1.3. Still, v1.3 does support private collections separation between different organisations.
Let's check it out by removing the implicit code checks provided in the SAP Help example.
In order to check the functionality, I used a trial Hyperledger Fabric service instance. I've also published the code in my repository. So you can use your SAP Cloud Platform account to clone&deploy the project and play around with the settings.
The files and folders structure has remained same as before:
Here the first collection named "collection1" has the following properties:
"policy" and "memberOnlyRead" define the collection accessibility by only the members of the "devMSP" organisation.
the "requiredPeerCount" property is set to "0" meaning there is no need to disseminate the private data to other peers. This setting is not significant for our scenario.
the "maxPeerCount" is set to "2" to indicate how many peers will keep the data redundantly. It's also not of the great significance for this case.
the "blockToLive" value is set to "3" in order to test that the records are purged after 3 subsequent ledger invocations (we will check this option below).
The second collection "collection2" has similar settings with the most significant one being the "policy". We've assigned the "collection2" to a non-existent organisation "devMSP2" just in order to check that we won't have access to it as members of organisation "devMSP". The "blockToLive" value is set to "0" meaning the collection items will be kept on the private database indefinitely.
We deploy our chaincode as a zip archive consisting of the following files including the src folder:
The collection-config.json file is added to the network separately after installation when the chaincode is being instantiated or upgraded. Please see the screen captures below:
Once initially installed, it can be instantiated:
And the collection config is provided:
So now we've got the instance with its Id highlighted below:
Let's see how it works.
Calling the API
So now we can jump to the SAP API Hub and log in there. Once the API call environment is configured, the service invocations can be performed as per below screen captures (please also see Step 2 in my previous blog for a quick reference).
Let's post some dummy "test value" via the "value" transient field:
The recorded dummy value is returned successfully:
Now if we press the Execute button three more times, the data will become unavailable on the fourth invocation. This is because the "blockToLive" property value was set to "3" in the collection definition. It actually means that the private data will live in the side db for only 3 subsequent blockchain transactions (newly created blocks), after which it gets purged. The Query API calls do not create new transactions on the ledger and, therefore, can be executed unlimited number of times to return the private data values if they were not removed.
Calling the API for the second private collection
As you remember, we have the "collection2" defined in our collection-config.json file. That collection is associated with the non-existent organisation "devMSP2". Unfortunately, we cannot switch our organisation under the trial account. Let's try to write and read a couple of private data samples belonging to the "collection2" while being a member of the "devMSP".
Theoretically speaking, we should not be allowed to perform any operation on the collection. However, the "writeCollection" invocation with the following payload succeeds:
We're only able to see the public hash of the private data belonging to a different organisation, which is exactly what we would like to see:
This actually proves our initial statement that access to the private data can be controlled by the collections configuration without writing explicit access control logic in chaincode.
Storing sensitive data off-chain is a very useful feature for potential customers. Here we have tried out the private data tools provided by the Hyperledger Fabric service. The solution looks usable and secure if implemented properly. Please bear in mind that it's developing rapidly, so a lot of cool features will be introduced in the nearest releases. Stay tuned!