github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/docs/source/policies.rst (about) 1 Policies in Hechain 2 ============================== 3 4 Configuration for a Hechain blockchain network is managed by 5 policies. These policies generally reside in the channel configuration. 6 The primary purpose of this document is to explain how policies are 7 defined in and interact with the channel configuration. However, 8 policies may also be specified in some other places, such as chaincodes, 9 so this information may be of interest outside the scope of channel 10 configuration. 11 12 What is a Policy? 13 ----------------- 14 15 At its most basic level, a policy is a function which accepts as input a 16 set of signed data and evaluates successfully, or returns an error 17 because some aspect of the signed data did not satisfy the policy. 18 19 More concretely, policies test whether the signer or signers of some 20 data meet some condition required for those signatures to be considered 21 'valid'. This is useful for determining that the correct parties have 22 agreed to a transaction, or change. 23 24 For example a policy may define any of the following: 25 26 * Administrators from 2 out 5 possible different organizations must sign. 27 * Any member from any organization must sign. 28 * Two specific certificates must both sign. 29 30 Of course these are only examples, and other more powerful rules can be 31 constructed. 32 33 Policy Types 34 ------------ 35 36 There are presently two different types of policies implemented: 37 38 1. **SignaturePolicy**: This policy type is the most powerful, and 39 specifies the policy as a combination of evaluation rules for MSP 40 Principals. It supports arbitrary combinations of *AND*, *OR*, and 41 *NOutOf*, allowing the construction of extremely powerful rules like: 42 "An admin of org A and 2 other admins, or 11 of 20 org admins". 43 2. **ImplicitMetaPolicy**: This policy type is less flexible than 44 SignaturePolicy, and is only valid in the context of configuration. 45 It aggregates the result of evaluating policies deeper in the 46 configuration hierarchy, which are ultimately defined by 47 SignaturePolicies. It supports good default rules like "A majority of 48 the organization admin policies". 49 50 Policies are encoded in a ``common.Policy`` message as defined in 51 ``fabric-protos/common/policies.proto``. They are defined by the 52 following message: 53 54 :: 55 56 message Policy { 57 enum PolicyType { 58 UNKNOWN = 0; // Reserved to check for proper initialization 59 SIGNATURE = 1; 60 MSP = 2; 61 IMPLICIT_META = 3; 62 } 63 int32 type = 1; // For outside implementors, consider the first 1000 types reserved, otherwise one of PolicyType 64 bytes policy = 2; 65 } 66 67 To encode the policy, simply pick the policy type of either 68 ``SIGNATURE`` or ``IMPLICIT_META``, set it to the ``type`` field, and 69 marshal the corresponding policy implementation proto to ``policy``. 70 71 Configuration and Policies 72 -------------------------- 73 74 The channel configuration is expressed as a hierarchy of configuration 75 groups, each of which has a set of values and policies associated with 76 them. For a validly configured application channel with two application 77 organizations and one ordering organization, the configuration looks 78 minimally as follows: 79 80 :: 81 82 Channel: 83 Policies: 84 Readers 85 Writers 86 Admins 87 Groups: 88 Orderer: 89 Policies: 90 Readers 91 Writers 92 Admins 93 Groups: 94 OrderingOrganization1: 95 Policies: 96 Readers 97 Writers 98 Admins 99 Application: 100 Policies: 101 Readers 102 -----------> Writers 103 Admins 104 Groups: 105 ApplicationOrganization1: 106 Policies: 107 Readers 108 Writers 109 Admins 110 ApplicationOrganization2: 111 Policies: 112 Readers 113 Writers 114 Admins 115 116 Consider the Writers policy referred to with the ``------->`` mark in 117 the above example. This policy may be referred to by the shorthand 118 notation ``/Channel/Application/Writers``. Note that the elements 119 resembling directory components are group names, while the last 120 component resembling a file basename is the policy name. 121 122 Different components of the system will refer to these policy names. For 123 instance, to call ``Deliver`` on the orderer, the signature on the 124 request must satisfy the ``/Channel/Readers`` policy. However, to gossip 125 a block to a peer will require that the ``/Channel/Application/Readers`` 126 policy be satisfied. 127 128 By setting these different policies, the system can be configured with 129 rich access controls. 130 131 Constructing a SignaturePolicy 132 ------------------------------ 133 134 As with all policies, the SignaturePolicy is expressed as protobuf. 135 136 :: 137 138 message SignaturePolicyEnvelope { 139 int32 version = 1; 140 SignaturePolicy policy = 2; 141 repeated MSPPrincipal identities = 3; 142 } 143 144 message SignaturePolicy { 145 message NOutOf { 146 int32 N = 1; 147 repeated SignaturePolicy policies = 2; 148 } 149 oneof Type { 150 int32 signed_by = 1; 151 NOutOf n_out_of = 2; 152 } 153 } 154 155 The outer ``SignaturePolicyEnvelope`` defines a version (currently only 156 ``0`` is supported), a set of identities expressed as 157 ``MSPPrincipal``\ s , and a ``policy`` which defines the policy rule, 158 referencing the ``identities`` by index. For more details on how to 159 specify MSP Principals, see the MSP Principals section. 160 161 The ``SignaturePolicy`` is a recursive data structure which either 162 represents a single signature requirement from a specific 163 ``MSPPrincipal``, or a collection of ``SignaturePolicy``\ s, requiring 164 that ``N`` of them are satisfied. 165 166 For example: 167 168 :: 169 170 SignaturePolicyEnvelope{ 171 version: 0, 172 policy: SignaturePolicy{ 173 n_out_of: NOutOf{ 174 N: 2, 175 policies: [ 176 SignaturePolicy{ signed_by: 0 }, 177 SignaturePolicy{ signed_by: 1 }, 178 ], 179 }, 180 }, 181 identities: [mspP1, mspP2], 182 } 183 184 This defines a signature policy over MSP Principals ``mspP1`` and 185 ``mspP2``. It requires both that there is a signature satisfying 186 ``mspP1`` and a signature satisfying ``mspP2``. 187 188 As another more complex example: 189 190 :: 191 192 SignaturePolicyEnvelope{ 193 version: 0, 194 policy: SignaturePolicy{ 195 n_out_of: NOutOf{ 196 N: 2, 197 policies: [ 198 SignaturePolicy{ signed_by: 0 }, 199 SignaturePolicy{ 200 n_out_of: NOutOf{ 201 N: 1, 202 policies: [ 203 SignaturePolicy{ signed_by: 1 }, 204 SignaturePolicy{ signed_by: 2 }, 205 ], 206 }, 207 }, 208 ], 209 }, 210 }, 211 identities: [mspP1, mspP2, mspP3], 212 } 213 214 This defines a signature policy over MSP Principals ``mspP1``, 215 ``mspP2``, and ``mspP3``. It requires one signature which satisfies 216 ``mspP1``, and another signature which either satisfies ``mspP2`` or 217 ``mspP3``. 218 219 Hopefully it is clear that complicated and relatively arbitrary logic 220 may be expressed using the SignaturePolicy policy type. For code which 221 constructs signature policies, consult 222 ``fabric/common/cauthdsl/cauthdsl_builder.go``. 223 224 --------- 225 226 **Limitations**: When evaluating a signature policy against a signature set, 227 signatures are 'consumed', in the order in which they appear, regardless of 228 whether they satisfy multiple policy principals. 229 230 For example. Consider a policy which requires 231 232 :: 233 234 2 of [org1.Member, org1.Admin] 235 236 The naive intent of this policy is to require that both an admin, and a member 237 sign. For the signature set 238 239 :: 240 241 [org1.MemberSignature, org1.AdminSignature] 242 243 the policy evaluates to true, just as expected. However, consider the 244 signature set 245 246 :: 247 248 [org1.AdminSignature, org1.MemberSignature] 249 250 This signature set does not satisfy the policy. This failure is because when 251 ``org1.AdminSignature`` satisfies the ``org1.Member`` role it is considered 252 'consumed' by the ``org1.Member`` requirement. Because the ``org1.Admin`` 253 principal cannot be satisfied by the ``org1.MemberSignature``, the policy 254 evaluates to false. 255 256 To avoid this pitfall, identities should be specified from most privileged to 257 least privileged in the policy identities specification, and signatures should 258 be ordered from least privileged to most privileged in the signature set. 259 260 MSP Principals 261 -------------- 262 263 The MSP Principal is a generalized notion of cryptographic identity. 264 Although the MSP framework is designed to work with types of 265 cryptography other than X.509, for the purposes of this document, the 266 discussion will assume that the underlying MSP implementation is the 267 default MSP type, based on X.509 cryptography. 268 269 An MSP Principal is defined in ``fabric-protos/msp_principal.proto`` as 270 follows: 271 272 :: 273 274 message MSPPrincipal { 275 276 enum Classification { 277 ROLE = 0; 278 ORGANIZATION_UNIT = 1; 279 IDENTITY = 2; 280 } 281 282 Classification principal_classification = 1; 283 284 bytes principal = 2; 285 } 286 287 The ``principal_classification`` must be set to either ``ROLE`` or 288 ``IDENTITY``. The ``ORGANIZATIONAL_UNIT`` is at the time of this writing 289 not implemented. 290 291 In the case of ``IDENTITY`` the ``principal`` field is set to the bytes 292 of a certificate literal. 293 294 However, more commonly the ``ROLE`` type is used, as it allows the 295 principal to match many different certs issued by the MSP's certificate 296 authority. 297 298 In the case of ``ROLE``, the ``principal`` is a marshaled ``MSPRole`` 299 message defined as follows: 300 301 :: 302 303 message MSPRole { 304 string msp_identifier = 1; 305 306 enum MSPRoleType { 307 MEMBER = 0; // Represents an MSP Member 308 ADMIN = 1; // Represents an MSP Admin 309 CLIENT = 2; // Represents an MSP Client 310 PEER = 3; // Represents an MSP Peer 311 } 312 313 MSPRoleType role = 2; 314 } 315 316 The ``msp_identifier`` is set to the ID of the MSP (as defined by the 317 ``MSPConfig`` proto in the channel configuration for an org) which will 318 evaluate the signature, and the ``Role`` is set to either ``MEMBER``, 319 ``ADMIN``, ``CLIENT`` or ``PEER``. In particular: 320 321 1. ``MEMBER`` matches any certificate issued by the MSP. 322 2. ``ADMIN`` matches certificates enumerated as admin in the MSP definition. 323 3. ``CLIENT`` (``PEER``) matches certificates that carry the client (peer) Organizational unit. 324 325 (see `MSP Documentation <http://hyperledger-fabric.readthedocs.io/en/{BRANCH_DOC}/msp.html>`_) 326 327 Constructing an ImplicitMetaPolicy 328 ---------------------------------- 329 330 The ``ImplicitMetaPolicy`` is only validly defined in the context of 331 channel configuration. It is ``Implicit`` because it is constructed 332 implicitly based on the current configuration, and it is ``Meta`` 333 because its evaluation is not against MSP principals, but rather against 334 other policies. It is defined in ``fabric-protos/common/policies.proto`` 335 as follows: 336 337 :: 338 339 message ImplicitMetaPolicy { 340 enum Rule { 341 ANY = 0; // Requires any of the sub-policies be satisfied, if no sub-policies exist, always returns true 342 ALL = 1; // Requires all of the sub-policies be satisfied 343 MAJORITY = 2; // Requires a strict majority (greater than half) of the sub-policies be satisfied 344 } 345 string sub_policy = 1; 346 Rule rule = 2; 347 } 348 349 For example, consider a policy defined at ``/Channel/Readers`` as 350 351 :: 352 353 ImplicitMetaPolicy{ 354 rule: ANY, 355 sub_policy: "foo", 356 } 357 358 This policy will implicitly select the sub-groups of ``/Channel``, in 359 this case, ``Application`` and ``Orderer``, and retrieve the policy of 360 name ``foo``, to give the policies ``/Channel/Application/foo`` and 361 ``/Channel/Orderer/foo``. Then, when the policy is evaluated, it will 362 check to see if ``ANY`` of those two policies evaluate without error. 363 Had the rule been ``ALL`` it would require both. 364 365 Consider another policy defined at ``/Channel/Application/Writers`` 366 where there are 3 application orgs defined, ``OrgA``, ``OrgB``, and 367 ``OrgC``. 368 369 :: 370 371 ImplicitMetaPolicy{ 372 rule: MAJORITY, 373 sub_policy: "bar", 374 } 375 376 In this case, the policies collected would be 377 ``/Channel/Application/OrgA/bar``, ``/Channel/Application/OrgB/bar``, 378 and ``/Channel/Application/OrgC/bar``. Because the rule requires a 379 ``MAJORITY``, this policy will require that 2 of the three 380 organization's ``bar`` policies are satisfied. 381 382 Policy Defaults 383 --------------- 384 385 The ``configtxgen`` tool uses policies which must be specified explicitly in configtx.yaml. 386 387 Note that policies higher in the hierarchy are all defined as 388 ``ImplicitMetaPolicy``\ s while leaf nodes necessarily are defined as 389 ``SignaturePolicy``\ s. This set of defaults works nicely because the 390 ``ImplicitMetaPolicies`` do not need to be redefined as the number of 391 organizations change, and the individual organizations may pick their 392 own rules and thresholds for what is means to be a Reader, Writer, and 393 Admin. 394 395 .. Licensed under Creative Commons Attribution 4.0 International License 396 https://creativecommons.org/licenses/by/4.0/ 397