forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
210 lines (169 loc) · 7.81 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package policy
import (
"fmt"
"errors"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
)
// PolicyChecker offers methods to check a signed proposal against a specific policy
// defined in a channel or not.
type PolicyChecker interface {
// CheckPolicy checks that the passed signed proposal is valid with the respect to
// passed policy on the passed channel.
// If no channel is passed, CheckPolicyNoChannel is invoked directly.
CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error
// CheckPolicyBySignedData checks that the passed signed data is valid with the respect to
// passed policy on the passed channel.
// If no channel is passed, the method will fail.
CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) error
// CheckPolicyNoChannel checks that the passed signed proposal is valid with the respect to
// passed policy on the local MSP.
CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error
}
type policyChecker struct {
channelPolicyManagerGetter policies.ChannelPolicyManagerGetter
localMSP msp.IdentityDeserializer
principalGetter mgmt.MSPPrincipalGetter
}
// NewPolicyChecker creates a new instance of PolicyChecker
func NewPolicyChecker(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localMSP msp.IdentityDeserializer, principalGetter mgmt.MSPPrincipalGetter) PolicyChecker {
return &policyChecker{channelPolicyManagerGetter, localMSP, principalGetter}
}
// CheckPolicy checks that the passed signed proposal is valid with the respect to
// passed policy on the passed channel.
func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error {
if channelID == "" {
return p.CheckPolicyNoChannel(policyName, signedProp)
}
if policyName == "" {
return fmt.Errorf("Invalid policy name during check policy on channel [%s]. Name must be different from nil.", channelID)
}
if signedProp == nil {
return fmt.Errorf("Invalid signed proposal during check policy on channel [%s] with policy [%s]", channelID, policyName)
}
// Get Policy
policyManager, _ := p.channelPolicyManagerGetter.Manager(channelID)
if policyManager == nil {
return fmt.Errorf("Failed to get policy manager for channel [%s]", channelID)
}
// Prepare SignedData
proposal, err := utils.GetProposal(signedProp.ProposalBytes)
if err != nil {
return fmt.Errorf("Failing extracting proposal during check policy on channel [%s] with policy [%s]: [%s]", channelID, policyName, err)
}
header, err := utils.GetHeader(proposal.Header)
if err != nil {
return fmt.Errorf("Failing extracting header during check policy on channel [%s] with policy [%s]: [%s]", channelID, policyName, err)
}
shdr, err := utils.GetSignatureHeader(header.SignatureHeader)
if err != nil {
return fmt.Errorf("Invalid Proposal's SignatureHeader during check policy on channel [%s] with policy [%s]: [%s]", channelID, policyName, err)
}
sd := []*common.SignedData{{
Data: signedProp.ProposalBytes,
Identity: shdr.Creator,
Signature: signedProp.Signature,
}}
return p.CheckPolicyBySignedData(channelID, policyName, sd)
}
// CheckPolicyNoChannel checks that the passed signed proposal is valid with the respect to
// passed policy on the local MSP.
func (p *policyChecker) CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error {
if policyName == "" {
return errors.New("Invalid policy name during channelless check policy. Name must be different from nil.")
}
if signedProp == nil {
return fmt.Errorf("Invalid signed proposal during channelless check policy with policy [%s]", policyName)
}
proposal, err := utils.GetProposal(signedProp.ProposalBytes)
if err != nil {
return fmt.Errorf("Failing extracting proposal during channelless check policy with policy [%s]: [%s]", policyName, err)
}
header, err := utils.GetHeader(proposal.Header)
if err != nil {
return fmt.Errorf("Failing extracting header during channelless check policy with policy [%s]: [%s]", policyName, err)
}
shdr, err := utils.GetSignatureHeader(header.SignatureHeader)
if err != nil {
return fmt.Errorf("Invalid Proposal's SignatureHeader during channelless check policy with policy [%s]: [%s]", policyName, err)
}
// Deserialize proposal's creator with the local MSP
id, err := p.localMSP.DeserializeIdentity(shdr.Creator)
if err != nil {
return fmt.Errorf("Failed deserializing proposal creator during channelless check policy with policy [%s]: [%s]", policyName, err)
}
// Load MSPPrincipal for policy
principal, err := p.principalGetter.Get(policyName)
if err != nil {
return fmt.Errorf("Failed getting local MSP principal during channelless check policy with policy [%s]: [%s]", policyName, err)
}
// Verify that proposal's creator satisfies the principal
err = id.SatisfiesPrincipal(principal)
if err != nil {
return fmt.Errorf("Failed verifying that proposal's creator satisfies local MSP principal during channelless check policy with policy [%s]: [%s]", policyName, err)
}
// Verify the signature
return id.Verify(signedProp.ProposalBytes, signedProp.Signature)
}
// CheckPolicyBySignedData checks that the passed signed data is valid with the respect to
// passed policy on the passed channel.
func (p *policyChecker) CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) error {
if channelID == "" {
return errors.New("Invalid channel ID name during check policy on signed data. Name must be different from nil.")
}
if policyName == "" {
return fmt.Errorf("Invalid policy name during check policy on signed data on channel [%s]. Name must be different from nil.", channelID)
}
if sd == nil {
return fmt.Errorf("Invalid signed data during check policy on channel [%s] with policy [%s]", channelID, policyName)
}
// Get Policy
policyManager, _ := p.channelPolicyManagerGetter.Manager(channelID)
if policyManager == nil {
return fmt.Errorf("Failed to get policy manager for channel [%s]", channelID)
}
// Recall that get policy always returns a policy object
policy, _ := policyManager.GetPolicy(policyName)
// Evaluate the policy
err := policy.Evaluate(sd)
if err != nil {
return fmt.Errorf("Failed evaluating policy on signed data during check policy on channel [%s] with policy [%s]: [%s]", channelID, policyName, err)
}
return nil
}
var pcFactory PolicyCheckerFactory
// PolicyCheckerFactory defines a factory interface so
// that the actual implementation can be injected
type PolicyCheckerFactory interface {
NewPolicyChecker() PolicyChecker
}
// RegisterPolicyCheckerFactory is to be called once to set
// the factory that will be used to obtain instances of PolicyChecker
func RegisterPolicyCheckerFactory(f PolicyCheckerFactory) {
pcFactory = f
}
// GetPolicyChecker returns instances of PolicyChecker;
// the actual implementation is controlled by the factory that
// is registered via RegisterPolicyCheckerFactory
func GetPolicyChecker() PolicyChecker {
if pcFactory == nil {
panic("The factory must be set first via RegisterPolicyCheckerFactory")
}
return pcFactory.NewPolicyChecker()
}