|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package policies |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" |
| 23 | + "github.com/hyperledger/fabric/orderer/common/cauthdsl" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | +) |
| 27 | + |
| 28 | +// Policy is used to determine if a signature is valid |
| 29 | +type Policy interface { |
| 30 | + // Evaluate returns nil if a msg is properly signed by sigs, or an error indicating why it failed |
| 31 | + Evaluate(msg []byte, sigs []*ab.SignedData) error |
| 32 | +} |
| 33 | + |
| 34 | +// Manager is intended to be the primary accessor of ManagerImpl |
| 35 | +// It is intended to discourage use of the other exported ManagerImpl methods |
| 36 | +// which are used for updating policy by the ConfigManager |
| 37 | +type Manager interface { |
| 38 | + // GetPolicy returns a policy and true if it was the policy requested, or false if it is the default policy |
| 39 | + GetPolicy(id string) (Policy, bool) |
| 40 | +} |
| 41 | + |
| 42 | +type policy struct { |
| 43 | + source *ab.Policy |
| 44 | + evaluator *cauthdsl.SignaturePolicyEvaluator |
| 45 | +} |
| 46 | + |
| 47 | +func newPolicy(policySource *ab.Policy, ch cauthdsl.CryptoHelper) (*policy, error) { |
| 48 | + envelopeWrapper, ok := policySource.Type.(*ab.Policy_SignaturePolicy) |
| 49 | + |
| 50 | + if !ok { |
| 51 | + return nil, fmt.Errorf("Unknown policy type: %T", policySource.Type) |
| 52 | + } |
| 53 | + |
| 54 | + if envelopeWrapper.SignaturePolicy == nil { |
| 55 | + return nil, fmt.Errorf("Nil signature policy received") |
| 56 | + } |
| 57 | + |
| 58 | + sigPolicy := envelopeWrapper.SignaturePolicy |
| 59 | + |
| 60 | + evaluator, err := cauthdsl.NewSignaturePolicyEvaluator(sigPolicy, ch) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + return &policy{ |
| 66 | + evaluator: evaluator, |
| 67 | + source: policySource, |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +// Evaluate returns nil if a msg is properly signed by sigs, or an error indicating why it failed |
| 72 | +func (p *policy) Evaluate(msg []byte, sigs []*ab.SignedData) error { |
| 73 | + if p == nil { |
| 74 | + return fmt.Errorf("Evaluated default policy, results in reject") |
| 75 | + } |
| 76 | + |
| 77 | + identities := make([][]byte, len(sigs)) |
| 78 | + signatures := make([][]byte, len(sigs)) |
| 79 | + for i, sigpair := range sigs { |
| 80 | + envelope := &ab.PayloadEnvelope{} |
| 81 | + if err := proto.Unmarshal(sigpair.PayloadEnvelope, envelope); err != nil { |
| 82 | + return fmt.Errorf("Failed to unmarshal the payload envelope to extract the signatures") |
| 83 | + } |
| 84 | + identities[i] = envelope.Signer |
| 85 | + signatures[i] = sigpair.Signature |
| 86 | + } |
| 87 | + // XXX This is wrong, as the signatures are over the payload envelope, not the message, fix either here, or in cauthdsl once transaction is finalized |
| 88 | + if !p.evaluator.Authenticate(msg, identities, signatures) { |
| 89 | + return fmt.Errorf("Failed to authenticate policy") |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// ManagerImpl is an implementation of Manager and configtx.ConfigHandler |
| 95 | +// In general, it should only be referenced as an Impl for the configtx.ConfigManager |
| 96 | +type ManagerImpl struct { |
| 97 | + policies map[string]*policy |
| 98 | + pendingPolicies map[string]*policy |
| 99 | + ch cauthdsl.CryptoHelper |
| 100 | +} |
| 101 | + |
| 102 | +// NewManagerImpl creates a new ManagerImpl with the given CryptoHelper |
| 103 | +func NewManagerImpl(ch cauthdsl.CryptoHelper) *ManagerImpl { |
| 104 | + return &ManagerImpl{ |
| 105 | + ch: ch, |
| 106 | + policies: make(map[string]*policy), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// GetPolicy returns a policy and true if it was the policy requested, or false if it is the default policy |
| 111 | +func (pm *ManagerImpl) GetPolicy(id string) (Policy, bool) { |
| 112 | + policy, ok := pm.policies[id] |
| 113 | + // Note the nil policy evaluates fine |
| 114 | + return policy, ok |
| 115 | +} |
| 116 | + |
| 117 | +// BeginConfig is used to start a new configuration proposal |
| 118 | +func (pm *ManagerImpl) BeginConfig() { |
| 119 | + if pm.pendingPolicies != nil { |
| 120 | + panic("Programming error, cannot call begin in the middle of a proposal") |
| 121 | + } |
| 122 | + pm.pendingPolicies = make(map[string]*policy) |
| 123 | +} |
| 124 | + |
| 125 | +// RollbackConfig is used to abandon a new configuration proposal |
| 126 | +func (pm *ManagerImpl) RollbackConfig() { |
| 127 | + pm.pendingPolicies = nil |
| 128 | +} |
| 129 | + |
| 130 | +// CommitConfig is used to commit a new configuration proposal |
| 131 | +func (pm *ManagerImpl) CommitConfig() { |
| 132 | + if pm.pendingPolicies == nil { |
| 133 | + panic("Programming error, cannot call commit without an existing proposal") |
| 134 | + } |
| 135 | + pm.policies = pm.pendingPolicies |
| 136 | + pm.pendingPolicies = nil |
| 137 | +} |
| 138 | + |
| 139 | +// ProposeConfig is used to add new configuration to the configuration proposal |
| 140 | +func (pm *ManagerImpl) ProposeConfig(configItem *ab.Configuration) error { |
| 141 | + if configItem.Type != ab.Configuration_Policy { |
| 142 | + return fmt.Errorf("Expected type of Configuration_Policy, got %v", configItem.Type) |
| 143 | + } |
| 144 | + |
| 145 | + policy := &ab.Policy{} |
| 146 | + err := proto.Unmarshal(configItem.Data, policy) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + cPolicy, err := newPolicy(policy, pm.ch) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + pm.pendingPolicies[configItem.ID] = cPolicy |
| 157 | + return nil |
| 158 | +} |
0 commit comments