github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 in the 36 hyperledger fabric. 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**: Tihs 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 OrdereringOrganization1: 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, whlie 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 Prinicipals ``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 ``mspP0``, 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 MSP Principals 225 -------------- 226 227 The MSP Principal is a generalized notion of cryptographic identity. 228 Although the MSP framework is designed to work with types of 229 cryptography other than X.509, for the purposes of this document, the 230 discussion will assume that the underlying MSP implementation is the 231 fabric MSP type, based on X.509 cryptography. 232 233 An MSP Principal is defined in ``fabric/protos/msp_principal.proto`` as 234 follows: 235 236 :: 237 238 message MSPPrincipal { 239 240 enum Classification { 241 ROLE = 0; 242 ORGANIZATION_UNIT = 1; 243 IDENTITY = 2; 244 } 245 246 Classification principal_classification = 1; 247 248 bytes principal = 2; 249 } 250 251 The ``principal_classification`` must be set to either ``ROLE`` or 252 ``IDENTITY``. The ``ORGANIZATIONAL_UNIT`` is at the time of this writing 253 not implemented. 254 255 In the case of ``IDENTITY`` the ``principal`` field is set to the bytes 256 of a certificate literal. 257 258 However, more commonly the ``ROLE`` type is used, as it allows the 259 principal to match many different certs issued by the MSP's certificate 260 authority. 261 262 In the case of ``ROLE``, the ``principal`` is a marshaled ``MSPRole`` 263 message defined as follows: 264 265 :: 266 267 message MSPRole { 268 string msp_identifier = 1; 269 270 enum MSPRoleType { 271 MEMBER = 0; // Represents an MSP Member 272 ADMIN = 1; // Represents an MSP Admin 273 } 274 275 MSPRoleType Role = 2; 276 } 277 278 The ``msp_identifier`` is set to the ID of the MSP (as defined by the 279 MSPConfig proto in the channel configuration for an org) which will 280 evaluate the signature, and the ``Role`` is set to either ``MEMBER`` or 281 ``ADMIN``. The ``MEMBER`` role will match any certificate issued by the 282 MSP, while the ``ADMIN`` role will match only certificates which are 283 enumerated as admin certificates in the MSP definition. 284 285 Constructing an ImplicitMetaPolicy 286 ---------------------------------- 287 288 The ``ImplicitMetaPolicy`` is only validly defined in the context of 289 channel configuration. It is ``Implicit`` because it is constructed 290 implicitly based on the current configuration, and it is ``Meta`` 291 because its evaluation is not against MSP principals, but rather against 292 other policies. It is defined in ``fabric/protos/common/policies.proto`` 293 as follows: 294 295 :: 296 297 message ImplicitMetaPolicy { 298 enum Rule { 299 ANY = 0; // Requires any of the sub-policies be satisfied, if no sub-policies exist, always returns true 300 ALL = 1; // Requires all of the sub-policies be satisfied 301 MAJORITY = 2; // Requires a strict majority (greater than half) of the sub-policies be satisfied 302 } 303 string sub_policy = 1; 304 Rule rule = 2; 305 } 306 307 For example, consider a policy defined at ``/Channel/Readers`` as 308 309 :: 310 311 ImplicitMetaPolicy{ 312 rule: ANY, 313 sub_policy: "foo", 314 } 315 316 This policy will implicity select the sub-groups of ``/Channel``, in 317 this case, ``Application`` and ``Orderer``, and retrieve the policy of 318 name ``foo``, to give the policies ``/Channel/Application/foo`` and 319 ``/Channel/Orderer/foo``. Then, when the policy is evaluated, it will 320 check to see if ``ANY`` of those two policies evaluate without error. 321 Had the rule been ``ALL`` it would require both. 322 323 Consider another policy defined at ``/Channel/Application/Writers`` 324 where there are 3 application orgs defined, ``OrgA``, ``OrgB``, and 325 ``OrgC``. 326 327 :: 328 329 ImplicitMetaPolicy{ 330 rule: MAJORITY, 331 sub_policy: "bar", 332 } 333 334 In this case, the policies collected would be 335 ``/Channel/Application/OrgA/bar``, ``/Channel/Application/OrgB/bar``, 336 and ``/Channel/Application/OrgC/bar``. Because the rule requires a 337 ``MAJORITY``, this policy will require that 2 of the three 338 organization's ``bar`` policies are satisfied. 339 340 Policy Defaults 341 --------------- 342 343 The ``configtxgen`` tool creates default policies as follows: 344 345 :: 346 347 /Channel/Readers : ImplicitMetaPolicy for ANY of /Channel/*/Readers 348 /Channel/Writers : ImplicitMetaPolicy for ANY of /Channel/*/Writers 349 /Channel/Admins : ImplicitMetaPolicy for MAJORITY of /Channel/*/Admins 350 351 /Channel/Application/Readers : ImplicitMetaPolicy for ANY of /Channel/Application/*/Readers 352 /Channel/Application/Writers : ImplicitMetaPolicy for ANY of /Channel/Application/*/Writers 353 /Channel/Application/Admins : ImplicitMetaPolicy for MAJORITY of /Channel/Application/*/Admins 354 355 /Channel/Orderer/Readers : ImplicitMetaPolicy for ANY of /Channel/Orderer/*/Readers 356 /Channel/Orderer/Writers : ImplicitMetaPolicy for ANY of /Channel/Orderer/*/Writers 357 /Channel/Orderer/Admins : ImplicitMetaPolicy for MAJORITY of /Channel/Orderer/*/Admins 358 359 # Here * represents either Orderer, or Application, and this is repeated for each org 360 /Channel/*/Org/Readers : SignaturePolicy for 1 of MSP Principal Org Member 361 /Channel/*/Org/Writers : SignaturePolicy for 1 of MSP Principal Org Member 362 /Channel/*/Org/Admins : SignaturePolicy for 1 of MSP Principal Org Admin 363 364 Note that policies higher in the hierarchy are all defined as 365 ``ImplicitMetaPolicy``\ s while leaf nodes necessarily are defined as 366 ``SignaturePolicy``\ s. This set of defaults works nicely because the 367 ``ImplicitMetaPolicies`` do not need to be redefined as the number of 368 organizations change, and the individual organizations may pick their 369 own rules and thresholds for what is means to be a a Reader, Writer, and 370 Admin.