github.com/kaituanwang/hyperledger@v2.0.1+incompatible/docs/source/endorsement-policies.rst (about) 1 Endorsement policies 2 ==================== 3 4 Every chaincode has an endorsement policy which specifies the set of peers on 5 a channel that must execute chaincode and endorse the execution results in 6 order for the transaction to be considered valid. These endorsement policies 7 define the organizations (through their peers) who must "endorse" (i.e., approve 8 of) the execution of a proposal. 9 10 .. note :: Recall that **state**, represented by key-value pairs, is separate 11 from blockchain data. For more on this, check out our :doc:`ledger/ledger` 12 documentation. 13 14 As part of the transaction validation step performed by the peers, each validating 15 peer checks to make sure that the transaction contains the appropriate **number** 16 of endorsements and that they are from the expected sources (both of these are 17 specified in the endorsement policy). The endorsements are also checked to make 18 sure they're valid (i.e., that they are valid signatures from valid certificates). 19 20 Multiple ways to require endorsement 21 ------------------------------------ 22 23 By default, endorsement policies are specified in the chaincode definition, 24 which is agreed to by channel members and then committed to a channel (that is, 25 one endorsement policy covers all of the state associated with a chaincode). 26 27 For private data collections, you can also specify an endorsement policy 28 at the private data collection level, which would override the chaincode 29 level endorsement policy for any keys in the private data collection, thereby 30 further restricting which organizations can write to a private data collection. 31 32 Finally, there are cases where it may be necessary for a particular public 33 channel state or private data collection state (a particular key-value pair, 34 in other words) to have a different endorsement policy. 35 This **state-based endorsement** allows the chaincode-level or collection-level 36 endorsement policies to be overridden by a different policy for the specified keys. 37 38 To illustrate the circumstances in which the various types of endorsement policies 39 might be used, consider a channel on which cars are being exchanged. The "creation" 40 --- also known as "issuance" -- of a car as an asset that can be traded (putting 41 the key-value pair that represents it into the world state, in other words) would 42 have to satisfy the chaincode-level endorsement policy. To see how to set a 43 chaincode-level endorsement policy, check out the section below. 44 45 If the key representing the car requires a specific endorsement policy, it can be 46 defined either when the car is created or afterwards. There are a number of reasons 47 why it might be necessary or preferable to set a state-specific endorsement policy. The 48 car might have historical importance or value that makes it necessary to have the 49 endorsement of a licensed appraiser. Also, the owner of the car (if they're a 50 member of the channel) might also want to ensure that their peer signs off on a 51 transaction. In both cases, **an endorsement policy is required for a particular 52 asset that is different from the default endorsement policies for the other 53 assets associated with that chaincode.** 54 55 We'll show you how to define a state-based endorsement policy in a subsequent 56 section. But first, let's see how we set a chaincode-level endorsement policy. 57 58 Setting chaincode-level endorsement policies 59 -------------------------------------------- 60 61 Chaincode-level endorsement policies are agreed to by channel members when they 62 approve a chaincode definition for their organization. A sufficient number of 63 channel members need to approve a chaincode definition to meet the 64 ``Channel/Application/LifecycleEndorsement`` policy, which by default is set to 65 a majority of channel members, before the definition can be committed to the 66 channel. Once the definition has been committed, the chaincode is ready to use. 67 Any invoke of the chaincode that writes data to the ledger will need to be 68 validated by enough channel members to meet the endorsement policy. 69 70 You can specify an endorsement policy for a chainocode using the Fabric SDKs. 71 For an example, visit the `How to install and start your chaincode <https://hyperledger.github.io/fabric-sdk-node/master/tutorial-chaincode-lifecycle.html>`_ 72 in the Node.js SDK documentation. You can also create an endorsement policy from 73 your CLI when you approve and commit a chaincode definition with the Fabric peer 74 binaries by using the ``—-signature-policy`` flag. 75 76 .. note:: Don't worry about the policy syntax (``'Org1.member'``, et all) right 77 now. We'll talk more about the syntax in the next section. 78 79 For example: 80 81 :: 82 83 peer lifecycle chaincode approveformyorg --channelID mychannel —-signature-policy "AND('Org1.member', 'Org2.member')" --name mycc --version 1.0 --package-id mycc_1:3a8c52d70c36313cfebbaf09d8616e7a6318ababa01c7cbe40603c373bcfe173 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent 84 85 The above command approves the chaincode definition of ``mycc`` with the policy 86 ``AND('Org1.member', 'Org2.member')`` which would require that a member of both 87 Org1 and Org2 sign the transaction. After a sufficient number of channel members 88 approve a chaincode definition for ``mycc``, the definition and endorsement 89 policy can be committed to the channel using the command below: 90 91 :: 92 93 peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel —-signature-policy "AND('Org1.member', 'Org2.member')" --name mycc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 94 95 Notice that, if the identity classification is enabled (see :doc:`msp`), one can 96 use the ``PEER`` role to restrict endorsement to only peers. 97 98 For example: 99 100 101 :: 102 103 peer lifecycle chaincode approveformyorg --channelID mychannel —-signature-policy "AND('Org1.peer', 'Org2.peer')" --name mycc --version 1.0 --package-id mycc_1:3a8c52d70c36313cfebbaf09d8616e7a6318ababa01c7cbe40603c373bcfe173 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent 104 105 In addition to the specifying an endorsement policy from the CLI or SDK, a 106 chaincode can also use policies in the channel configuration as endorsement 107 policies. You can use the ``--channel-config-policy``flag to select a channel policy with 108 format used by the channel configuration and by ACLs. 109 110 For example: 111 112 :: 113 114 peer lifecycle chaincode approveformyorg --channelID mychannel --channel-config-policy Channel/Application/Admins --name mycc --version 1.0 --package-id mycc_1:3a8c52d70c36313cfebbaf09d8616e7a6318ababa01c7cbe40603c373bcfe173 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent 115 116 If you do not specify a policy, the chaincode definition will use the 117 ``Channel/Application/Endorsement`` policy by default, which requires that a 118 transaction be validated by a majority of channel members. This policy depends on 119 the membership of the channel, so it will be updated automatically when organizations 120 are added or removed from a channel. One advantage of using channel policies is 121 that they can be written to be updated automatically with channel membership. 122 123 If you specify an endorsement policy using the ``—-signature-policy`` flag or 124 the SDK, you will need to update the policy when organizations join or leave the 125 channel. A new organization added to the channel after the chaincode has been defined 126 will be able to query a chaincode (provided the query has appropriate authorization as 127 defined by channel policies and any application level checks enforced by the 128 chaincode) but will not be able to execute or endorse the chaincode. Only 129 organizations listed in the endorsement policy syntax will be able sign 130 transactions. 131 132 Endorsement policy syntax 133 ~~~~~~~~~~~~~~~~~~~~~~~~~ 134 135 As you can see above, policies are expressed in terms of principals 136 ("principals" are identities matched to a role). Principals are described as 137 ``'MSP.ROLE'``, where ``MSP`` represents the required MSP ID and ``ROLE`` 138 represents one of the four accepted roles: ``member``, ``admin``, ``client``, and 139 ``peer``. 140 141 Here are a few examples of valid principals: 142 143 - ``'Org0.admin'``: any administrator of the ``Org0`` MSP 144 - ``'Org1.member'``: any member of the ``Org1`` MSP 145 - ``'Org1.client'``: any client of the ``Org1`` MSP 146 - ``'Org1.peer'``: any peer of the ``Org1`` MSP 147 148 The syntax of the language is: 149 150 ``EXPR(E[, E...])`` 151 152 Where ``EXPR`` is either ``AND``, ``OR``, or ``OutOf``, and ``E`` is either a 153 principal (with the syntax described above) or another nested call to ``EXPR``. 154 155 For example: 156 - ``AND('Org1.member', 'Org2.member', 'Org3.member')`` requests one signature 157 from each of the three principals. 158 - ``OR('Org1.member', 'Org2.member')`` requests one signature from either one 159 of the two principals. 160 - ``OR('Org1.member', AND('Org2.member', 'Org3.member'))`` requests either one 161 signature from a member of the ``Org1`` MSP or one signature from a member 162 of the ``Org2`` MSP and one signature from a member of the ``Org3`` MSP. 163 - ``OutOf(1, 'Org1.member', 'Org2.member')``, which resolves to the same thing 164 as ``OR('Org1.member', 'Org2.member')``. 165 - Similarly, ``OutOf(2, 'Org1.member', 'Org2.member')`` is equivalent to 166 ``AND('Org1.member', 'Org2.member')``, and ``OutOf(2, 'Org1.member', 167 'Org2.member', 'Org3.member')`` is equivalent to ``OR(AND('Org1.member', 168 'Org2.member'), AND('Org1.member', 'Org3.member'), AND('Org2.member', 169 'Org3.member'))``. 170 171 Setting collection-level endorsement policies 172 --------------------------------------------- 173 Similar to chaincode-level endorsement policies, when you approve and commit 174 a chaincode definition, you can also specify the chaincode's private data collections 175 and corresponding collection-level endorsement policies. If a collection-level 176 endorsement policy is set, transactions that write to a private data collection 177 key will require that the specified organization peers have endorsed the transaction. 178 179 You can use collection-level endorsement policies to restrict which organization 180 peers can write to the private data collection key namespace, for example to 181 ensure that non-authorized organizations cannot write to a collection, and to 182 have confidence that any state in a private data collection has been endorsed 183 by the required collection organization(s). 184 185 The collection-level endorsement policy may be less restrictive or more restrictive 186 than the chaincode-level endorsement policy and the collection's private data 187 distribution policy. For example a majority of organizations may be required 188 to endorse a chaincode transaction, but a specific organization may be required 189 to endorse a transaction that includes a key in a specific collection. 190 191 The syntax for collection-level endorsement policies exactly matches the syntax 192 for chaincode-level endorsement policies --- in the collection configuration 193 you can specify an ``endorsementPolicy`` with either a ``signaturePolicy`` or 194 ``channelConfigPolicy``. For more details see :doc:`private-data-arch`. 195 196 .. _key-level-endorsement: 197 198 Setting key-level endorsement policies 199 -------------------------------------- 200 201 Setting regular chaincode-level or collection-level endorsement policies is tied to 202 the lifecycle of the corresponding chaincode. They can only be set or modified when 203 defining the chaincode on a channel. 204 205 In contrast, key-level endorsement policies can be set and modified in a more 206 granular fashion from within a chaincode. The modification is part of the 207 read-write set of a regular transaction. 208 209 The shim API provides the following functions to set and retrieve an endorsement 210 policy for/from a regular key. 211 212 .. note:: ``ep`` below stands for the "endorsement policy", which can be expressed 213 either by using the same syntax described above or by using the 214 convenience function described below. Either method will generate a 215 binary version of the endorsement policy that can be consumed by the 216 basic shim API. 217 218 .. code-block:: Go 219 220 SetStateValidationParameter(key string, ep []byte) error 221 GetStateValidationParameter(key string) ([]byte, error) 222 223 For keys that are part of :doc:`private-data/private-data` in a collection the 224 following functions apply: 225 226 .. code-block:: Go 227 228 SetPrivateDataValidationParameter(collection, key string, ep []byte) error 229 GetPrivateDataValidationParameter(collection, key string) ([]byte, error) 230 231 To help set endorsement policies and marshal them into validation 232 parameter byte arrays, the Go shim provides an extension with convenience 233 functions that allow the chaincode developer to deal with endorsement policies 234 in terms of the MSP identifiers of organizations, see `KeyEndorsementPolicy <https://godoc.org/github.com/hyperledger/fabric-chaincode-go/pkg/statebased#KeyEndorsementPolicy>`_: 235 236 .. code-block:: Go 237 238 type KeyEndorsementPolicy interface { 239 // Policy returns the endorsement policy as bytes 240 Policy() ([]byte, error) 241 242 // AddOrgs adds the specified orgs to the list of orgs that are required 243 // to endorse 244 AddOrgs(roleType RoleType, organizations ...string) error 245 246 // DelOrgs delete the specified channel orgs from the existing key-level endorsement 247 // policy for this KVS key. If any org is not present, an error will be returned. 248 DelOrgs(organizations ...string) error 249 250 // ListOrgs returns an array of channel orgs that are required to endorse changes 251 ListOrgs() ([]string) 252 } 253 254 For example, to set an endorsement policy for a key where two specific orgs are 255 required to endorse the key change, pass both org ``MSPIDs`` to ``AddOrgs()``, 256 and then call ``Policy()`` to construct the endorsement policy byte array that 257 can be passed to ``SetStateValidationParameter()``. 258 259 To add the shim extension to your chaincode as a dependency, see :ref:`vendoring`. 260 261 Validation 262 ---------- 263 264 At commit time, setting a value of a key is no different from setting the 265 endorsement policy of a key --- both update the state of the key and are 266 validated based on the same rules. 267 268 +---------------------+------------------------------------+--------------------------+ 269 | Validation | no validation parameter set | validation parameter set | 270 +=====================+====================================+==========================+ 271 | modify value | check chaincode or collection ep | check key-level ep | 272 +---------------------+------------------------------------+--------------------------+ 273 | modify key-level ep | check chaincode or collection ep | check key-level ep | 274 +---------------------+------------------------------------+--------------------------+ 275 276 As we discussed above, if a key is modified and no key-level endorsement policy 277 is present, the chaincode-level or collection-level endorsement policy applies by default. 278 This is also true when a key-level endorsement policy is set for a key for the first time 279 --- the new key-level endorsement policy must first be endorsed according to the 280 pre-existing chaincode-level or collection-level endorsement policy. 281 282 If a key is modified and a key-level endorsement policy is present, the key-level 283 endorsement policy overrides the chaincode-level or collection-level endorsement policy. 284 In practice, this means that the key-level endorsement policy can be either less restrictive 285 or more restrictive than the chaincode-level or collection-level endorsement policies. 286 Because the chaincode-level or collection-level endorsement policy must be satisfied in order 287 to set a key-level endorsement policy for the first time, no trust assumptions have been violated. 288 289 If a key's endorsement policy is removed (set to nil), the chaincode-level 290 or collection-level endorsement policy becomes the default again. 291 292 If a transaction modifies multiple keys with different associated key-level 293 endorsement policies, all of these policies need to be satisfied in order 294 for the transaction to be valid. 295 296 .. Licensed under Creative Commons Attribution 4.0 International License 297 https://creativecommons.org/licenses/by/4.0/