github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/docs/source/policies/policies.md (about) 1 # Policies 2 3 **Audience**: Architects, application and smart contract developers, 4 administrators 5 6 In this topic, we'll cover: 7 8 * [What is a policy](#what-is-a-policy) 9 * [Why are policies needed](#why-are-policies-needed) 10 * [How are policies implemented](#how-are-policies-implemented) 11 * [How do you write a policy in Fabric](#how-do-you-write-a-policy-in-fabric) 12 * [Fabric chaincode lifecycle](#fabric-chaincode-lifecycle) 13 * [Overriding policy definitions](#overriding-policy-definitions) 14 15 Note: this topic describes a network that does not use a "system channel", a channel that the ordering service is bootstrapped with and the ordering service exclusively controls. Since the release of v2.3, using system channel is now considered the legacy process as compared to the process to [Create a channel](../create_channel/create_channel_participation.html) without a system channel. For a version of this topic that includes information about the system channel, check out [Policies](https://hyperledger-fabric.readthedocs.io/en/release-2.2/policies/policies.html) from the v2.2 documentation. 16 17 ## What is a policy 18 19 At its most basic level, a policy is a set of rules that define the structure 20 for how decisions are made and specific outcomes are reached. To that end, 21 policies typically describe a **who** and a **what**, such as the access or 22 rights that an individual has over an **asset**. We can see that policies are 23 used throughout our daily lives to protect assets of value to us, from car 24 rentals, health, our homes, and many more. 25 26 For example, an insurance policy defines the conditions, terms, limits, and 27 expiration under which an insurance payout will be made. The policy is 28 agreed to by the policy holder and the insurance company, and defines the rights 29 and responsibilities of each party. 30 31 Whereas an insurance policy is put in place for risk management, in Hyperledger 32 Fabric, policies are the mechanism for infrastructure management. Fabric policies 33 represent how members come to agreement on accepting or rejecting changes to the 34 network, a channel, or a smart contract. Policies are agreed to by the channel 35 members when the channel is originally configured, but they can also be modified 36 as the channel evolves. For example, they describe the criteria for adding or 37 removing members from a channel, change how blocks are formed, or specify the 38 number of organizations required to endorse a smart contract. All of these 39 actions are described by a policy which defines who can perform the action. 40 Simply put, everything you want to do on a Fabric network is controlled by a 41 policy. 42 43 ## Why are policies needed 44 45 Policies are one of the things that make Hechain different from other 46 blockchains like Ethereum or Bitcoin. In those systems, transactions can be 47 generated and validated by any node in the network. The policies that govern the 48 network are fixed at any point in time and can only be changed using the same 49 process that governs the code. Because Fabric is a permissioned blockchain whose 50 users are recognized by the underlying infrastructure, those users have the 51 ability to decide on the governance of the network before it is launched, and 52 change the governance of a running network. 53 54 Policies allow members to decide which organizations can access or update a Fabric 55 network, and provide the mechanism to enforce those decisions. Policies contain 56 the lists of organizations that have access to a given resource, such as a 57 user or system chaincode. They also specify how many organizations need to agree 58 on a proposal to update a resource, such as a channel or smart contracts. Once 59 they are written, policies evaluate the collection of signatures attached to 60 transactions and proposals and validate if the signatures fulfill the governance 61 agreed to by the network. 62 63 ## How are policies implemented 64 65 Policies are defined within the relevant administrative domain of a particular action defined by the policy. For example, the policy for adding a peer organization to a channel is defined within the administrative domain of the peer organizations (known as the `Application` group). Similarly, adding ordering nodes in the consenter set of the channel is controlled by a policy inside the `Orderer` group. Actions that cross both the peer and orderer organizational domains are contained in the `Channel` group. 66 67 Typically, these policies default to the "majority of admins" of the group they fall under (a majority of peer organization admins for example, or in the case of `Channel` policies, a majority of both peer organizations and orderer organizations), though they can be specified to any rule a user wishes to define. Check out [Signature policies](#signature-policies) for more information. 68 69 ### Access control lists (ACLs) 70 71 Network administrators will be especially interested in the Fabric use of ACLs, 72 which provide the ability to configure access to resources by associating those 73 resources with existing policies. These "resources" could be functions on system 74 chaincode (e.g., "GetBlockByNumber" on the "qscc" system chaincode) or other 75 resources (e.g.,who can receive Block events). ACLs refer to policies 76 defined in an application channel configuration and extends them to control 77 additional resources. The default set of Fabric ACLs is visible in the 78 `configtx.yaml` file under the `Application: &ApplicationDefaults` section but 79 they can and should be overridden in a production environment. The list of 80 resources named in `configtx.yaml` is the complete set of all internal resources 81 currently defined by Fabric. 82 83 In that file, ACLs are expressed using the following format: 84 85 ``` 86 # ACL policy for chaincode to chaincode invocation 87 peer/ChaincodeToChaincode: /Channel/Application/Writers 88 ``` 89 90 Where `peer/ChaincodeToChaincode` represents the resource being secured and 91 `/Channel/Application/Writers` refers to the policy which must be satisfied for 92 the associated transaction to be considered valid. 93 94 For a deeper dive into ACLS, refer to the topic in the Operations Guide on [ACLs](../access_control.html). 95 96 ### Smart contract endorsement policies 97 98 Every smart contract inside a chaincode package has an endorsement policy that 99 specifies how many peers belonging to different channel members need to execute 100 and validate a transaction against a given smart contract in order for the 101 transaction to be considered valid. Hence, the endorsement policies define the 102 organizations (through their peers) who must “endorse” (i.e., approve of) the 103 execution of a proposal. 104 105 ### Modification policies 106 107 There is one last type of policy that is crucial to how policies work in Fabric, 108 the `Modification policy`. Modification policies specify the group of identities 109 required to sign (approve) any configuration _update_. It is the policy that 110 defines how the policy is updated. Thus, each channel configuration element 111 includes a reference to a policy which governs its modification. 112 113 ## How do you write a policy in Fabric 114 115 If you want to change anything in Fabric, the policy associated with the resource 116 describes **who** needs to approve it, either with an explicit sign off from 117 individuals, or an implicit sign off by a group. In the insurance domain, an 118 explicit sign off could be a single member of the homeowners insurance agents 119 group. And an implicit sign off would be analogous to requiring approval from a 120 majority of the managerial members of the homeowners insurance group. This is 121 particularly useful because the members of that group can change over time 122 without requiring that the policy be updated. In Hechain, explicit 123 sign offs in policies are expressed using the `Signature` syntax and implicit 124 sign offs use the `ImplicitMeta` syntax. 125 126 ### Signature policies 127 128 `Signature` policies define specific types of users who must sign in order for a 129 policy to be satisfied such as `OR('Org1.peer', 'Org2.peer')`. These policies are 130 considered the most versatile because they allow for the construction of 131 extremely specific rules like: “An admin of org A and 2 other admins, or 5 of 6 132 organization admins”. The syntax supports arbitrary combinations of `AND`, `OR` 133 and `NOutOf`. For example, a policy can be easily expressed by using 134 `AND('Org1.member', 'Org2.member')` which means that a signature from at least 135 one member in Org1 AND one member in Org2 is required for the policy to be satisfied. 136 137 ### ImplicitMeta policies 138 139 `ImplicitMeta` policies are only valid in the context of channel configuration 140 which is based on a tiered hierarchy of policies in a configuration tree. ImplicitMeta 141 policies aggregate the result of policies deeper in the configuration tree that 142 are ultimately defined by Signature policies. They are `Implicit` because they 143 are constructed implicitly based on the current organizations in the 144 channel configuration, and they are `Meta` because their evaluation is not 145 against specific MSP principals, but rather against other sub-policies below 146 them in the configuration tree. 147 148 The following diagram illustrates the tiered policy structure for an application 149 channel and shows how the `ImplicitMeta` channel configuration admins policy, 150 named `/Channel/Admins`, is resolved when the sub-policies named `Admins` below it 151 in the configuration hierarchy are satisfied where each check mark represents that 152 the conditions of the sub-policy were satisfied. 153 154  155 156 As you can see in the diagram above, `ImplicitMeta` policies, Type = 3, use a 157 different syntax, `"<ANY|ALL|MAJORITY> <SubPolicyName>"`, for example: 158 ``` 159 `MAJORITY sub policy: Admins` 160 ``` 161 The diagram shows a sub-policy `Admins`, which refers to all the `Admins` policy 162 below it in the configuration tree. You can create your own sub-policies 163 and name them whatever you want and then define them in each of your 164 organizations. 165 166 As mentioned above, a key benefit of an `ImplicitMeta` policy such as `MAJORITY 167 Admins` is that when you add a new admin organization to the channel, you do not 168 have to update the channel policy. Therefore `ImplicitMeta` policies are 169 considered to be more flexible as organizations are added. Recall that `ImplicitMeta` policies ultimately resolve the 170 `Signature` sub-policies underneath them in the configuration tree as the 171 diagram shows. 172 173 You can also define an application level implicit policy to operate across 174 organizations, in a channel for example, and either require that ANY of them 175 are satisfied, that ALL are satisfied, or that a MAJORITY are satisfied. This 176 format lends itself to much better, more natural defaults, so that each 177 organization can decide what it means for a valid endorsement. 178 179 Further granularity and control can be achieved if you include [NodeOUs](../msp.html#organizational-units) in your 180 organization definition. Organization Units (OUs) are defined in the Fabric CA 181 client configuration file and can be associated with an identity when it is 182 created. In Fabric, `NodeOUs` provide a way to classify identities in a digital 183 certificate hierarchy. For instance, an organization having specific `NodeOUs` 184 enabled could require that a 'peer' sign for it to be a valid endorsement, 185 whereas an organization without any might simply require that any member can 186 sign. 187 188 ## An example: channel configuration policy 189 190 Understanding policies begins with examining the `configtx.yaml` where the 191 channel policies are defined. We can use the `configtx.yaml` file in the Fabric 192 test network to see examples of both policy syntax types. We are going to examine 193 the `configtx.yaml` file used by the [fabric-samples/test-network](https://github.com/hyperledger/fabric-samples/blob/main/test-network/configtx/configtx.yaml) sample. 194 195 The first section of the file defines the organizations that will be members of the channel. Inside each 196 organization definition are the default policies for that organization, `Readers`, `Writers`, 197 `Admins`, and `Endorsement`, although you can name your policies anything you want. 198 Each policy has a `Type` which describes how the policy is expressed (`Signature` 199 or `ImplicitMeta`) and a `Rule`. 200 201 The test network example below shows the Org1 organization definition in the 202 channel, where the policy `Type` is `Signature` and the `Endorsement:` policy rule 203 is defined as `"OR('Org1MSP.peer')"`. This policy specifies that a peer that is 204 a member of `Org1MSP` is required to sign. It is these signature policies that 205 become the sub-policies that the `ImplicitMeta` policies point to. 206 207 <details> 208 <summary> 209 **Click here to see an example of an organization defined with signature policies** 210 </summary> 211 212 ``` 213 - &Org1 214 # DefaultOrg defines the organization which is used in the sampleconfig 215 # of the fabric.git development environment 216 Name: Org1MSP 217 218 # ID to load the MSP definition as 219 ID: Org1MSP 220 221 MSPDir: ../organizations/peerOrganizations/org1.example.com/msp 222 223 # Policies defines the set of policies at this level of the config tree 224 # For organization policies, their canonical path is usually 225 # /Channel/<Application|Orderer>/<OrgName>/<PolicyName> 226 Policies: 227 Readers: 228 Type: Signature 229 Rule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')" 230 Writers: 231 Type: Signature 232 Rule: "OR('Org1MSP.admin', 'Org1MSP.client')" 233 Admins: 234 Type: Signature 235 Rule: "OR('Org1MSP.admin')" 236 Endorsement: 237 Type: Signature 238 Rule: "OR('Org1MSP.peer')" 239 ``` 240 </details> 241 242 The next example shows the `ImplicitMeta` policy type used in the `Application` 243 section of the `configtx.yaml`. These set of policies lie on the 244 `/Channel/Application/` path. If you use the default set of Fabric ACLs, these 245 policies define the behavior of many important features of application channels, 246 such as who can query the channel ledger, invoke a chaincode, or update a channel 247 config. These policies point to the sub-policies defined for each organization. 248 The Org1 defined in the section above contains `Reader`, `Writer`, and `Admin` 249 sub-policies that are evaluated by the `Reader`, `Writer`, and `Admin` `ImplicitMeta` 250 policies in the `Application` section. Because the test network is built with the 251 default policies, you can use the example Org1 to query the channel ledger, invoke a 252 chaincode, and approve channel updates for any test network channel that you 253 create. 254 255 <details> 256 <summary> 257 **Click here to see an example of ImplicitMeta policies** 258 </summary> 259 ``` 260 ################################################################################ 261 # 262 # SECTION: Application 263 # 264 # - This section defines the values to encode into a config transaction or 265 # genesis block for application related parameters 266 # 267 ################################################################################ 268 Application: &ApplicationDefaults 269 270 # Organizations is the list of orgs which are defined as participants on 271 # the application side of the network 272 Organizations: 273 274 # Policies defines the set of policies at this level of the config tree 275 # For Application policies, their canonical path is 276 # /Channel/Application/<PolicyName> 277 Policies: 278 Readers: 279 Type: ImplicitMeta 280 Rule: "ANY Readers" 281 Writers: 282 Type: ImplicitMeta 283 Rule: "ANY Writers" 284 Admins: 285 Type: ImplicitMeta 286 Rule: "MAJORITY Admins" 287 LifecycleEndorsement: 288 Type: ImplicitMeta 289 Rule: "MAJORITY Endorsement" 290 Endorsement: 291 Type: ImplicitMeta 292 Rule: "MAJORITY Endorsement" 293 ``` 294 </details> 295 296 ## Fabric chaincode lifecycle 297 298 In the Fabric 2.0 release, a new chaincode lifecycle process was introduced, 299 whereby a more democratic process is used to govern chaincode on the network. 300 The new process allows multiple organizations to vote on how a chaincode will 301 be operated before it can be used on a channel. This is significant because it is 302 the combination of this new lifecycle process and the policies that are 303 specified during that process that dictate the security across the network. More details on 304 the flow are available in the [Fabric chaincode lifecycle](../chaincode_lifecycle.html) 305 concept topic, but for purposes of this topic you should understand how policies are 306 used in this flow. The new flow includes two steps where policies are specified: 307 when chaincode is **approved** by organization members, and when it is **committed** 308 to the channel. 309 310 The `Application` section of the `configtx.yaml` file includes the default 311 chaincode lifecycle endorsement policy. In a production environment you would 312 customize this definition for your own use case. 313 314 ``` 315 ################################################################################ 316 # 317 # SECTION: Application 318 # 319 # - This section defines the values to encode into a config transaction or 320 # genesis block for application related parameters 321 # 322 ################################################################################ 323 Application: &ApplicationDefaults 324 325 # Organizations is the list of orgs which are defined as participants on 326 # the application side of the network 327 Organizations: 328 329 # Policies defines the set of policies at this level of the config tree 330 # For Application policies, their canonical path is 331 # /Channel/Application/<PolicyName> 332 Policies: 333 Readers: 334 Type: ImplicitMeta 335 Rule: "ANY Readers" 336 Writers: 337 Type: ImplicitMeta 338 Rule: "ANY Writers" 339 Admins: 340 Type: ImplicitMeta 341 Rule: "MAJORITY Admins" 342 LifecycleEndorsement: 343 Type: ImplicitMeta 344 Rule: "MAJORITY Endorsement" 345 Endorsement: 346 Type: ImplicitMeta 347 Rule: "MAJORITY Endorsement" 348 ``` 349 350 - The `LifecycleEndorsement` policy governs who needs to _approve a chaincode 351 definition_. 352 - The `Endorsement` policy is the _default endorsement policy for 353 a chaincode_. More on this below. 354 355 ## Chaincode endorsement policies 356 357 The endorsement policy is specified for a **chaincode** when it is approved 358 and committed to the channel using the Fabric chaincode lifecycle (that is, one 359 endorsement policy covers all of the state associated with a chaincode). The 360 endorsement policy can be specified either by reference to an endorsement policy 361 defined in the channel configuration or by explicitly specifying a Signature policy. 362 363 If an endorsement policy is not explicitly specified during the approval step, 364 the default `Endorsement` policy `"MAJORITY Endorsement"` is used which means 365 that a majority of the peers belonging to the different channel members 366 (organizations) need to execute and validate a transaction against the chaincode 367 in order for the transaction to be considered valid. This default policy allows 368 organizations that join the channel to become automatically added to the chaincode 369 endorsement policy. If you don't want to use the default endorsement 370 policy, use the Signature policy format to specify a more complex endorsement 371 policy (such as requiring that a chaincode be endorsed by one organization, and 372 then one of the other organizations on the channel). 373 374 Signature policies also allow you to include `principals` which are simply a way 375 of matching an identity to a role. Principals are just like user IDs or group 376 IDs, but they are more versatile because they can include a wide range of 377 properties of an actor’s identity, such as the actor’s organization, 378 organizational unit, role or even the actor’s specific identity. When we talk 379 about principals, they are the properties which determine their permissions. 380 Principals are described as `'MSP.ROLE'`, where `MSP` represents the required MSP 381 ID (the organization), and `ROLE` represents one of the four accepted roles: 382 Member, Admin, Client, and Peer. A role is associated to an identity when a user 383 enrolls with a CA. You can customize the list of roles available on your Fabric 384 CA. 385 386 Some examples of valid principals are: 387 * 'Org0.Admin': an administrator of the Org0 MSP 388 * 'Org1.Member': a member of the Org1 MSP 389 * 'Org1.Client': a client of the Org1 MSP 390 * 'Org1.Peer': a peer of the Org1 MSP 391 * 'OrdererOrg.Orderer': an orderer in the OrdererOrg MSP 392 393 There are cases where it may be necessary for a particular state 394 (a particular key-value pair, in other words) to have a different endorsement 395 policy. This **state-based endorsement** allows the default chaincode-level 396 endorsement policies to be overridden by a different policy for the specified 397 keys. 398 399 For a deeper dive on how to write an endorsement policy refer to the topic on 400 [Endorsement policies](../endorsement-policies.html) in the Operations Guide. 401 402 **Note:** Policies work differently depending on which version of Fabric you are 403 using: 404 - In Fabric releases prior to 2.0, chaincode endorsement policies can be 405 updated during chaincode instantiation or by using the chaincode lifecycle 406 commands. If not specified at instantiation time, the endorsement policy 407 defaults to “any member of the organizations in the channel”. For example, 408 a channel with “Org1” and “Org2” would have a default endorsement policy of 409 “OR(‘Org1.member’, ‘Org2.member’)”. 410 - Starting with Fabric 2.0, Fabric introduced a new chaincode 411 lifecycle process that allows multiple organizations to agree on how a 412 chaincode will be operated before it can be used on a channel. The new process 413 requires that organizations agree to the parameters that define a chaincode, 414 such as name, version, and the chaincode endorsement policy. 415 416 ## Overriding policy definitions 417 418 Hechain includes default policies which are useful for getting started, 419 developing, and testing your blockchain, but they are meant to be customized 420 in a production environment. You should be aware of the default policies 421 in the `configtx.yaml` file. Channel configuration policies can be extended 422 with arbitrary verbs, beyond the default `Readers, Writers, Admins` in 423 `configtx.yaml`. 424 425 For more information on overriding policy definitions when creating a channel, check out [Channel policies](../create_channel/channel_policies.html) and [Creating a channel without a system channel](../create_channel/create_channel_participation.html). 426 427 For information about how to update a channel, check out [Updating a channel configuration](../config_update.html#updating-a-channel-configuration) for more information. 428 429 <!--- Licensed under Creative Commons Attribution 4.0 International License 430 https://creativecommons.org/licenses/by/4.0/) -->