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