-
Notifications
You must be signed in to change notification settings - Fork 22
/
policy.go
executable file
·376 lines (313 loc) · 12.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
Copyright IBM Corp. 2016 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 policies
import (
"fmt"
"strings"
"sync"
cb "github.com/traceabilitychain/tacchain/protos/common"
"github.com/golang/protobuf/proto"
"github.com/traceabilitychain/tacchain/common/flogging"
logging "github.com/op/go-logging"
)
const (
// Path separator is used to separate policy names in paths
PathSeparator = "/"
// ChannelPrefix is used in the path of standard channel policy managers
ChannelPrefix = "Channel"
// ApplicationPrefix is used in the path of standard application policy paths
ApplicationPrefix = "Application"
// OrdererPrefix is used in the path of standard orderer policy paths
OrdererPrefix = "Orderer"
// ChannelReaders is the label for the channel's readers policy (encompassing both orderer and application readers)
ChannelReaders = PathSeparator + ChannelPrefix + PathSeparator + "Readers"
// ChannelWriters is the label for the channel's writers policy (encompassing both orderer and application writers)
ChannelWriters = PathSeparator + ChannelPrefix + PathSeparator + "Writers"
// ChannelApplicationReaders is the label for the channel's application readers policy
ChannelApplicationReaders = PathSeparator + ChannelPrefix + PathSeparator + ApplicationPrefix + PathSeparator + "Readers"
// ChannelApplicationWriters is the label for the channel's application writers policy
ChannelApplicationWriters = PathSeparator + ChannelPrefix + PathSeparator + ApplicationPrefix + PathSeparator + "Writers"
// ChannelApplicationAdmins is the label for the channel's application admin policy
ChannelApplicationAdmins = PathSeparator + ChannelPrefix + PathSeparator + ApplicationPrefix + PathSeparator + "Admins"
// BlockValidation is the label for the policy which should validate the block signatures for the channel
BlockValidation = PathSeparator + ChannelPrefix + PathSeparator + OrdererPrefix + PathSeparator + "BlockValidation"
)
var logger = flogging.MustGetLogger("policies")
// Policy is used to determine if a signature is valid
type Policy interface {
// Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy
Evaluate(signatureSet []*cb.SignedData) error
}
// Manager is a read only subset of the policy ManagerImpl
type Manager interface {
// GetPolicy returns a policy and true if it was the policy requested, or false if it is the default policy
GetPolicy(id string) (Policy, bool)
// Manager returns the sub-policy manager for a given path and whether it exists
Manager(path []string) (Manager, bool)
// Basepath returns the basePath the manager was instantiated with
BasePath() string
// Policies returns all policy names defined in the manager
PolicyNames() []string
}
// Proposer is the interface used by the configtx manager for policy management
type Proposer interface {
// BeginPolicyProposals starts a policy update transaction
BeginPolicyProposals(tx interface{}, groups []string) ([]Proposer, error)
// ProposePolicy createss a pending policy update from a ConfigPolicy and returns the deserialized
// value of the Policy representation
ProposePolicy(tx interface{}, name string, policy *cb.ConfigPolicy) (proto.Message, error)
// RollbackProposals discards the pending policy updates
RollbackProposals(tx interface{})
// CommitProposals commits the pending policy updates
CommitProposals(tx interface{})
// PreCommit tests if a commit will apply
PreCommit(tx interface{}) error
}
// Provider provides the backing implementation of a policy
type Provider interface {
// NewPolicy creates a new policy based on the policy bytes
NewPolicy(data []byte) (Policy, proto.Message, error)
}
// ChannelPolicyManagerGetter is a support interface
// to get access to the policy manager of a given channel
type ChannelPolicyManagerGetter interface {
// Returns the policy manager associated to the passed channel
// and true if it was the manager requested, or false if it is the default manager
Manager(channelID string) (Manager, bool)
}
type policyConfig struct {
policies map[string]Policy
managers map[string]*ManagerImpl
imps []*implicitMetaPolicy
}
// ManagerImpl is an implementation of Manager and configtx.ConfigHandler
// In general, it should only be referenced as an Impl for the configtx.ConfigManager
type ManagerImpl struct {
parent *ManagerImpl
basePath string
fqPrefix string
providers map[int32]Provider
config *policyConfig
pendingConfig map[interface{}]*policyConfig
pendingLock sync.RWMutex
// SuppressSanityLogMessages when set to true will prevent the sanity checking log
// messages. Useful for novel cases like channel templates
SuppressSanityLogMessages bool
}
// NewManagerImpl creates a new ManagerImpl with the given CryptoHelper
func NewManagerImpl(basePath string, providers map[int32]Provider) *ManagerImpl {
_, ok := providers[int32(cb.Policy_IMPLICIT_META)]
if ok {
logger.Panicf("ImplicitMetaPolicy type must be provider by the policy manager")
}
return &ManagerImpl{
basePath: basePath,
fqPrefix: PathSeparator + basePath + PathSeparator,
providers: providers,
config: &policyConfig{
policies: make(map[string]Policy),
managers: make(map[string]*ManagerImpl),
},
pendingConfig: make(map[interface{}]*policyConfig),
}
}
type rejectPolicy string
func (rp rejectPolicy) Evaluate(signedData []*cb.SignedData) error {
return fmt.Errorf("No such policy type: %s", rp)
}
// Basepath returns the basePath the manager was instnatiated with
func (pm *ManagerImpl) BasePath() string {
return pm.basePath
}
func (pm *ManagerImpl) PolicyNames() []string {
policyNames := make([]string, len(pm.config.policies))
i := 0
for policyName := range pm.config.policies {
policyNames[i] = policyName
i++
}
return policyNames
}
// Manager returns the sub-policy manager for a given path and whether it exists
func (pm *ManagerImpl) Manager(path []string) (Manager, bool) {
if len(path) == 0 {
return pm, true
}
m, ok := pm.config.managers[path[0]]
if !ok {
return nil, false
}
return m.Manager(path[1:])
}
// GetPolicy returns a policy and true if it was the policy requested, or false if it is the default reject policy
func (pm *ManagerImpl) GetPolicy(id string) (Policy, bool) {
if id == "" {
logger.Errorf("Returning dummy reject all policy because no policy ID supplied")
return rejectPolicy(id), false
}
var relpath string
if strings.HasPrefix(id, PathSeparator) {
if pm.parent != nil {
return pm.parent.GetPolicy(id)
}
if !strings.HasPrefix(id, pm.fqPrefix) {
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Requested policy from root manager with wrong basePath: %s, returning rejectAll", id)
}
return rejectPolicy(id), false
}
relpath = id[len(pm.fqPrefix):]
} else {
relpath = id
}
policy, ok := pm.config.policies[relpath]
if !ok {
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Returning dummy reject all policy because %s could not be found in /%s/%s", id, pm.basePath, relpath)
}
return rejectPolicy(relpath), false
}
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Returning policy %s for evaluation", relpath)
}
return policy, true
}
// BeginPolicies is used to start a new config proposal
func (pm *ManagerImpl) BeginPolicyProposals(tx interface{}, groups []string) ([]Proposer, error) {
pm.pendingLock.Lock()
defer pm.pendingLock.Unlock()
pendingConfig, ok := pm.pendingConfig[tx]
if ok {
logger.Panicf("Serious Programming error: cannot call begin multiply for the same proposal")
}
pendingConfig = &policyConfig{
policies: make(map[string]Policy),
managers: make(map[string]*ManagerImpl),
}
pm.pendingConfig[tx] = pendingConfig
managers := make([]Proposer, len(groups))
for i, group := range groups {
newManager := NewManagerImpl(group, pm.providers)
newManager.parent = pm
pendingConfig.managers[group] = newManager
managers[i] = newManager
}
return managers, nil
}
// RollbackProposals is used to abandon a new config proposal
func (pm *ManagerImpl) RollbackProposals(tx interface{}) {
pm.pendingLock.Lock()
defer pm.pendingLock.Unlock()
delete(pm.pendingConfig, tx)
}
// PreCommit is currently a no-op for the policy manager and always returns nil
func (pm *ManagerImpl) PreCommit(tx interface{}) error {
return nil
}
// CommitProposals is used to commit a new config proposal
func (pm *ManagerImpl) CommitProposals(tx interface{}) {
pm.pendingLock.Lock()
defer pm.pendingLock.Unlock()
pendingConfig, ok := pm.pendingConfig[tx]
if !ok {
logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
}
if pendingConfig == nil {
logger.Panicf("Programming error, cannot call commit without an existing proposal")
}
for managerPath, m := range pendingConfig.managers {
for _, policyName := range m.PolicyNames() {
fqKey := managerPath + PathSeparator + policyName
pendingConfig.policies[fqKey], _ = m.GetPolicy(policyName)
logger.Debugf("In commit adding relative sub-policy %s to %s", fqKey, pm.basePath)
}
}
// Now that all the policies are present, initialize the meta policies
for _, imp := range pendingConfig.imps {
imp.initialize(pendingConfig)
}
pm.config = pendingConfig
delete(pm.pendingConfig, tx)
if pm.parent == nil && pm.basePath == ChannelPrefix && !pm.SuppressSanityLogMessages {
for _, policyName := range []string{ChannelReaders, ChannelWriters} {
_, ok := pm.GetPolicy(policyName)
if !ok {
logger.Warningf("Current configuration has no policy '%s', this will likely cause problems in production systems", policyName)
} else {
logger.Debugf("As expected, current configuration has policy '%s'", policyName)
}
}
if _, ok := pm.config.managers[ApplicationPrefix]; ok {
// Check for default application policies if the application component is defined
for _, policyName := range []string{
ChannelApplicationReaders,
ChannelApplicationWriters,
ChannelApplicationAdmins} {
_, ok := pm.GetPolicy(policyName)
if !ok {
logger.Warningf("Current configuration has no policy '%s', this will likely cause problems in production systems", policyName)
} else {
logger.Debugf("As expected, current configuration has policy '%s'", policyName)
}
}
}
if _, ok := pm.config.managers[OrdererPrefix]; ok {
for _, policyName := range []string{BlockValidation} {
_, ok := pm.GetPolicy(policyName)
if !ok {
logger.Warningf("Current configuration has no policy '%s', this will likely cause problems in production systems", policyName)
} else {
logger.Debugf("As expected, current configuration has policy '%s'", policyName)
}
}
}
}
}
// ProposePolicy takes key, path, and ConfigPolicy and registers it in the proposed PolicyManager, or errors
// It also returns the deserialized policy value for tracking and inspection at the invocation side.
func (pm *ManagerImpl) ProposePolicy(tx interface{}, key string, configPolicy *cb.ConfigPolicy) (proto.Message, error) {
pm.pendingLock.RLock()
pendingConfig, ok := pm.pendingConfig[tx]
pm.pendingLock.RUnlock()
if !ok {
logger.Panicf("Serious Programming error: called Propose without Begin")
}
policy := configPolicy.Policy
if policy == nil {
return nil, fmt.Errorf("Policy cannot be nil")
}
var cPolicy Policy
var deserialized proto.Message
if policy.Type == int32(cb.Policy_IMPLICIT_META) {
imp, err := newImplicitMetaPolicy(policy.Value)
if err != nil {
return nil, err
}
pendingConfig.imps = append(pendingConfig.imps, imp)
cPolicy = imp
deserialized = imp.conf
} else {
provider, ok := pm.providers[int32(policy.Type)]
if !ok {
return nil, fmt.Errorf("Unknown policy type: %v", policy.Type)
}
var err error
cPolicy, deserialized, err = provider.NewPolicy(policy.Value)
if err != nil {
return nil, err
}
}
pendingConfig.policies[key] = cPolicy
logger.Debugf("Proposed new policy %s for %s", key, pm.basePath)
return deserialized, nil
}