github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/policies/util.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package policies 8 9 import ( 10 cb "github.com/hyperledger/fabric-protos-go/common" 11 "github.com/hyperledger/fabric/protoutil" 12 ) 13 14 // ConfigPolicy defines a common representation for different *cb.ConfigPolicy values. 15 type ConfigPolicy interface { 16 // Key is the key this value should be stored in the *cb.ConfigGroup.Policies map. 17 Key() string 18 19 // Value is the backing policy implementation for this ConfigPolicy 20 Value() *cb.Policy 21 } 22 23 // StandardConfigValue implements the ConfigValue interface. 24 type StandardConfigPolicy struct { 25 key string 26 value *cb.Policy 27 } 28 29 // Key is the key this value should be stored in the *cb.ConfigGroup.Values map. 30 func (scv *StandardConfigPolicy) Key() string { 31 return scv.key 32 } 33 34 // Value is the *cb.Policy which should be stored as the *cb.ConfigPolicy.Policy. 35 func (scv *StandardConfigPolicy) Value() *cb.Policy { 36 return scv.value 37 } 38 39 func makeImplicitMetaPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.Policy { 40 return &cb.Policy{ 41 Type: int32(cb.Policy_IMPLICIT_META), 42 Value: protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{ 43 Rule: rule, 44 SubPolicy: subPolicyName, 45 }), 46 } 47 } 48 49 // ImplicitMetaAllPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ALL. 50 func ImplicitMetaAllPolicy(policyName string) *StandardConfigPolicy { 51 return &StandardConfigPolicy{ 52 key: policyName, 53 value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ALL), 54 } 55 } 56 57 // ImplicitMetaAnyPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ANY. 58 func ImplicitMetaAnyPolicy(policyName string) *StandardConfigPolicy { 59 return &StandardConfigPolicy{ 60 key: policyName, 61 value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ANY), 62 } 63 } 64 65 // ImplicitMetaMajorityPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule MAJORITY. 66 func ImplicitMetaMajorityPolicy(policyName string) *StandardConfigPolicy { 67 return &StandardConfigPolicy{ 68 key: policyName, 69 value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_MAJORITY), 70 } 71 } 72 73 // ImplicitMetaMajorityPolicy defines a policy with key policyName and the given signature policy. 74 func SignaturePolicy(policyName string, sigPolicy *cb.SignaturePolicyEnvelope) *StandardConfigPolicy { 75 return &StandardConfigPolicy{ 76 key: policyName, 77 value: &cb.Policy{ 78 Type: int32(cb.Policy_SIGNATURE), 79 Value: protoutil.MarshalOrPanic(sigPolicy), 80 }, 81 } 82 }