github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/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 create an endorsement policy from
    71  your CLI when you approve and commit a chaincode definition with the Fabric peer
    72  binaries by using the ``--signature-policy`` flag.
    73  
    74  .. note:: Don't worry about the policy syntax (``'Org1.member'``, et all) right
    75            now. We'll talk more about the syntax in the next section.
    76  
    77  For example:
    78  
    79  ::
    80  
    81      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 --cafile /opt/gopath/src/github.com/hechain20/hechain/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent
    82  
    83  The above command approves the chaincode definition of ``mycc`` with the policy
    84  ``AND('Org1.member', 'Org2.member')`` which would require that a member of both
    85  Org1 and Org2 sign the transaction. After a sufficient number of channel members
    86  approve a chaincode definition for ``mycc``, the definition and endorsement
    87  policy can be committed to the channel using the command below:
    88  
    89  ::
    90  
    91      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 --cafile /opt/gopath/src/github.com/hechain20/hechain/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/hechain20/hechain/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/hechain20/hechain/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
    92  
    93  Notice that, if the identity classification is enabled (see :doc:`msp`), one can
    94  use the ``PEER`` role to restrict endorsement to only peers.
    95  
    96  For example:
    97  
    98  
    99  ::
   100  
   101      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 --cafile /opt/gopath/src/github.com/hechain20/hechain/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent
   102  
   103  In addition to the specifying an endorsement policy from the CLI or SDK, a
   104  chaincode can also use policies in the channel configuration as endorsement
   105  policies. You can use the ``--channel-config-policy`` flag to select a channel policy with
   106  format used by the channel configuration and by ACLs.
   107  
   108  For example:
   109  
   110  ::
   111  
   112      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 --cafile /opt/gopath/src/github.com/hechain20/hechain/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --waitForEvent
   113  
   114  If you do not specify a policy, the chaincode definition will use the
   115  ``Channel/Application/Endorsement`` policy by default, which requires that a
   116  transaction be validated by a majority of channel members. This policy depends on
   117  the membership of the channel, so it will be updated automatically when organizations
   118  are added or removed from a channel. One advantage of using channel policies is
   119  that they can be written to be updated automatically with channel membership.
   120  
   121  If you specify an endorsement policy using the ``--signature-policy`` flag, you will need to update the policy when organizations join or leave the
   122  channel. A new organization added to the channel after the chaincode has been defined
   123  will be able to query a chaincode (provided the query has appropriate authorization as
   124  defined by channel policies and any application level checks enforced by the
   125  chaincode) but will not be able to execute or endorse the chaincode. Only
   126  organizations listed in the endorsement policy syntax will be able sign
   127  transactions.
   128  
   129  Endorsement policy syntax
   130  ~~~~~~~~~~~~~~~~~~~~~~~~~
   131  
   132  As you can see above, policies are expressed in terms of principals
   133  ("principals" are identities matched to a role). Principals are described as
   134  ``'MSP.ROLE'``, where ``MSP`` represents the required MSP ID and ``ROLE``
   135  represents one of the four accepted roles: ``member``, ``admin``, ``client``, and
   136  ``peer``.
   137  
   138  Here are a few examples of valid principals:
   139  
   140    - ``'Org0.admin'``: any administrator of the ``Org0`` MSP
   141    - ``'Org1.member'``: any member of the ``Org1`` MSP
   142    - ``'Org1.client'``: any client of the ``Org1`` MSP
   143    - ``'Org1.peer'``: any peer of the ``Org1`` MSP
   144  
   145  The syntax of the language is:
   146  
   147  ``EXPR(E[, E...])``
   148  
   149  Where ``EXPR`` is either ``AND``, ``OR``, or ``OutOf``, and ``E`` is either a
   150  principal (with the syntax described above) or another nested call to ``EXPR``.
   151  
   152  For example:
   153    - ``AND('Org1.member', 'Org2.member', 'Org3.member')`` requests one signature
   154      from each of the three principals.
   155    - ``OR('Org1.member', 'Org2.member')`` requests one signature from either one
   156      of the two principals.
   157    - ``OR('Org1.member', AND('Org2.member', 'Org3.member'))`` requests either one
   158      signature from a member of the ``Org1`` MSP or one signature from a member
   159      of the ``Org2`` MSP and one signature from a member of the ``Org3`` MSP.
   160    - ``OutOf(1, 'Org1.member', 'Org2.member')``, which resolves to the same thing
   161      as ``OR('Org1.member', 'Org2.member')``.
   162    - Similarly, ``OutOf(2, 'Org1.member', 'Org2.member')`` is equivalent to
   163      ``AND('Org1.member', 'Org2.member')``, and ``OutOf(2, 'Org1.member',
   164      'Org2.member', 'Org3.member')`` is equivalent to ``OR(AND('Org1.member',
   165      'Org2.member'), AND('Org1.member', 'Org3.member'), AND('Org2.member',
   166      'Org3.member'))``.
   167  
   168  Setting collection-level endorsement policies
   169  ---------------------------------------------
   170  Similar to chaincode-level endorsement policies, when you approve and commit
   171  a chaincode definition, you can also specify the chaincode's private data collections
   172  and corresponding collection-level endorsement policies. If a collection-level
   173  endorsement policy is set, transactions that write to a private data collection
   174  key will require that the specified organization peers have endorsed the transaction.
   175  
   176  You can use collection-level endorsement policies to restrict which organization
   177  peers can write to the private data collection key namespace, for example to
   178  ensure that non-authorized organizations cannot write to a collection, and to
   179  have confidence that any state in a private data collection has been endorsed
   180  by the required collection organization(s).
   181  
   182  The collection-level endorsement policy may be less restrictive or more restrictive
   183  than the chaincode-level endorsement policy and the collection's private data
   184  distribution policy.  For example a majority of organizations may be required
   185  to endorse a chaincode transaction, but a specific organization may be required
   186  to endorse a transaction that includes a key in a specific collection.
   187  
   188  If you do not specify a collection-level endorsement policy, the chaincode-level
   189  endorsement policy will be applied to protect writes to a private data collection
   190  key namespace. This may be desirable if a set of organizations meeting the chaincode-level
   191  endorsement policy are authorized to create data in other organization's private
   192  data collection. For example if those organizations are trusted to process
   193  transactions but are not authorized to store and later query private data due to industry privacy regulations,
   194  or if the private data is being shared or transferred from one set of organizations
   195  to another through the use of private data collections.
   196  In other scenarios, the private data collection members may
   197  need to be in full control of writes to the private data collection, in which case
   198  a collection-level endorsement policy should be provided.
   199  
   200  The syntax for collection-level endorsement policies exactly matches the syntax
   201  for chaincode-level endorsement policies --- in the collection configuration
   202  you can specify an ``endorsementPolicy`` with either a ``signaturePolicy`` or
   203  ``channelConfigPolicy``. For more details see :doc:`private-data-arch`.
   204  
   205  .. _key-level-endorsement:
   206  
   207  Setting key-level endorsement policies
   208  --------------------------------------
   209  
   210  Setting regular chaincode-level or collection-level endorsement policies is tied to
   211  the lifecycle of the corresponding chaincode. They can only be set or modified when
   212  defining the chaincode on a channel.
   213  
   214  In contrast, key-level endorsement policies can be set and modified in a more
   215  granular fashion from within a chaincode. The modification is part of the
   216  read-write set of a regular transaction.
   217  
   218  The shim API provides the following functions to set and retrieve an endorsement
   219  policy for/from a regular key.
   220  
   221  .. note:: ``ep`` below stands for the "endorsement policy", which can be expressed
   222            either by using the same syntax described above or by using the
   223            convenience function described below. Either method will generate a
   224            binary version of the endorsement policy that can be consumed by the
   225            basic shim API.
   226  
   227  .. code-block:: Go
   228  
   229      SetStateValidationParameter(key string, ep []byte) error
   230      GetStateValidationParameter(key string) ([]byte, error)
   231  
   232  For keys that are part of :doc:`private-data/private-data` in a collection the
   233  following functions apply:
   234  
   235  .. code-block:: Go
   236  
   237      SetPrivateDataValidationParameter(collection, key string, ep []byte) error
   238      GetPrivateDataValidationParameter(collection, key string) ([]byte, error)
   239  
   240  To help set endorsement policies and marshal them into validation
   241  parameter byte arrays, the Go shim provides an extension with convenience
   242  functions that allow the chaincode developer to deal with endorsement policies
   243  in terms of the MSP identifiers of organizations, see `KeyEndorsementPolicy <https://godoc.org/github.com/hyperledger/fabric-chaincode-go/pkg/statebased#KeyEndorsementPolicy>`_:
   244  
   245  .. code-block:: Go
   246  
   247      type KeyEndorsementPolicy interface {
   248          // Policy returns the endorsement policy as bytes
   249          Policy() ([]byte, error)
   250  
   251          // AddOrgs adds the specified orgs to the list of orgs that are required
   252          // to endorse
   253          AddOrgs(roleType RoleType, organizations ...string) error
   254  
   255          // DelOrgs delete the specified channel orgs from the existing key-level endorsement
   256          // policy for this KVS key. If any org is not present, an error will be returned.
   257          DelOrgs(organizations ...string) error
   258  
   259          // ListOrgs returns an array of channel orgs that are required to endorse changes
   260          ListOrgs() ([]string)
   261      }
   262  
   263  For example, to set an endorsement policy for a key where two specific orgs are
   264  required to endorse the key change, pass both org ``MSPIDs`` to ``AddOrgs()``,
   265  and then call ``Policy()`` to construct the endorsement policy byte array that
   266  can be passed to ``SetStateValidationParameter()``.
   267  
   268  To add the shim extension to your chaincode as a dependency, see :ref:`vendoring`.
   269  
   270  Validation
   271  ----------
   272  
   273  At commit time, setting a value of a key is no different from setting the
   274  endorsement policy of a key --- both update the state of the key and are
   275  validated based on the same rules.
   276  
   277  +---------------------+------------------------------------+--------------------------+
   278  | Validation          | no validation parameter set        | validation parameter set |
   279  +=====================+====================================+==========================+
   280  | modify value        | check chaincode or collection ep   | check key-level ep       |
   281  +---------------------+------------------------------------+--------------------------+
   282  | modify key-level ep | check chaincode or collection ep   | check key-level ep       |
   283  +---------------------+------------------------------------+--------------------------+
   284  
   285  As we discussed above, if a key is modified and no key-level endorsement policy
   286  is present, the chaincode-level or collection-level endorsement policy applies by default.
   287  This is also true when a key-level endorsement policy is set for a key for the first time
   288  --- the new key-level endorsement policy must first be endorsed according to the
   289  pre-existing chaincode-level or collection-level endorsement policy.
   290  
   291  If a key is modified and a key-level endorsement policy is present, the key-level
   292  endorsement policy overrides the chaincode-level or collection-level endorsement policy.
   293  In practice, this means that the key-level endorsement policy can be either less restrictive
   294  or more restrictive than the chaincode-level or collection-level endorsement policies.
   295  Because the chaincode-level or collection-level endorsement policy must be satisfied in order
   296  to set a key-level endorsement policy for the first time, no trust assumptions have been violated.
   297  
   298  If a key's endorsement policy is removed (set to nil), the chaincode-level
   299  or collection-level endorsement policy becomes the default again.
   300  
   301  If a transaction modifies multiple keys with different associated key-level
   302  endorsement policies, all of these policies need to be satisfied in order
   303  for the transaction to be valid.
   304  
   305  .. Licensed under Creative Commons Attribution 4.0 International License
   306     https://creativecommons.org/licenses/by/4.0/