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