Skip to content

Commit 7559dd9

Browse files
author
Jason Yellick
committed
[FAB-2238] Move Policies Handler to PolicyHander
https://jira.hyperledger.org/browse/FAB-2238 Policy handling is done different from config value handling. This CR addresses TODOs in the code to migrate Policies management onto its own Handler type. Change-Id: I7605b418cf1f498a0503f2a91f87924c6aab8f30 Signed-off-by: Jason Yellick <[email protected]>
1 parent b12c76f commit 7559dd9

File tree

7 files changed

+86
-35
lines changed

7 files changed

+86
-35
lines changed

common/cauthdsl/policy_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"github.com/golang/protobuf/proto"
2727
)
2828

29-
var acceptAllPolicy []byte
30-
var rejectAllPolicy []byte
29+
var acceptAllPolicy *cb.Policy
30+
var rejectAllPolicy *cb.Policy
3131

3232
func init() {
3333
acceptAllPolicy = makePolicySource(true)
@@ -43,24 +43,23 @@ func marshalOrPanic(msg proto.Message) []byte {
4343
return data
4444
}
4545

46-
func makePolicySource(policyResult bool) []byte {
46+
func makePolicySource(policyResult bool) *cb.Policy {
4747
var policyData *cb.SignaturePolicyEnvelope
4848
if policyResult {
4949
policyData = AcceptAllPolicy
5050
} else {
5151
policyData = RejectAllPolicy
5252
}
53-
marshaledPolicy := marshalOrPanic(&cb.Policy{
53+
return &cb.Policy{
5454
Type: int32(cb.Policy_SIGNATURE),
5555
Policy: marshalOrPanic(policyData),
56-
})
57-
return marshaledPolicy
56+
}
5857
}
5958

60-
func addPolicy(manager *policies.ManagerImpl, id string, policy []byte) {
59+
func addPolicy(manager *policies.ManagerImpl, id string, policy *cb.Policy) {
6160
manager.BeginConfig()
62-
err := manager.ProposeConfig(id, &cb.ConfigValue{
63-
Value: policy,
61+
err := manager.ProposePolicy(id, []string{}, &cb.ConfigPolicy{
62+
Policy: policy,
6463
})
6564
if err != nil {
6665
panic(err)

common/configtx/api/api.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type OrdererConfig interface {
8080
}
8181

8282
// Handler provides a hook which allows other pieces of code to participate in config proposals
83+
// TODO, this should probably be renamed to ValueHandler
8384
type Handler interface {
8485
// ProposeConfig called when config is added to a proposal
8586
ProposeConfig(key string, configValue *cb.ConfigValue) error
@@ -125,8 +126,8 @@ type Resources interface {
125126
MSPManager() msp.MSPManager
126127
}
127128

128-
// SubInitializer is used downstream from initializer
129-
type SubInitializer interface {
129+
// Transactional is an interface which allows for an update to be proposed and rolled back
130+
type Transactional interface {
130131
// BeginConfig called when a config proposal is begun
131132
BeginConfig()
132133

@@ -135,18 +136,30 @@ type SubInitializer interface {
135136

136137
// CommitConfig called when a config proposal is committed
137138
CommitConfig()
139+
}
140+
141+
// SubInitializer is used downstream from initializer
142+
type SubInitializer interface {
143+
Transactional
138144

139145
// Handler returns the associated value handler for a given config path
140146
Handler(path []string) (Handler, error)
141147
}
142148

149+
// PolicyHandler is used for config updates to policy
150+
type PolicyHandler interface {
151+
Transactional
152+
153+
ProposePolicy(key string, path []string, policy *cb.ConfigPolicy) error
154+
}
155+
143156
// Initializer is used as indirection between Manager and Handler to allow
144157
// for single Handlers to handle multiple paths
145158
type Initializer interface {
146159
SubInitializer
147160

148161
Resources
149162

150-
// PolicyProposer as a Handler is a temporary hack
151-
PolicyProposer() Handler
163+
// PolicyProposer returns the PolicyHandler to handle updates to policy
164+
PolicyHandler() PolicyHandler
152165
}

common/configtx/initializer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (i *initializer) CommitConfig() {
131131
i.mspConfigHandler.CommitConfig()
132132
}
133133

134-
func (i *initializer) PolicyProposer() api.Handler {
134+
func (i *initializer) PolicyHandler() api.PolicyHandler {
135135
return i.policyManager
136136
}
137137

common/configtx/manager.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ func (cm *configManager) proposeConfig(config map[string]comparable) error {
178178
return err
179179
}
180180
case c.ConfigPolicy != nil:
181-
if err := cm.initializer.PolicyProposer().ProposeConfig(c.key, &cb.ConfigValue{
182-
// TODO, fix policy interface to take the policy directly
183-
Value: utils.MarshalOrPanic(c.ConfigPolicy.Policy),
184-
}); err != nil {
181+
if err := cm.initializer.PolicyHandler().ProposePolicy(c.key, c.path, c.ConfigPolicy); err != nil {
185182
return err
186183
}
187184
}

common/mocks/configtx/configtx.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,55 @@ func (r *Resources) MSPManager() msp.MSPManager {
6666
return r.MSPManagerVal
6767
}
6868

69+
type Transactional struct{}
70+
71+
// BeginConfig calls through to the HandlerVal
72+
func (t *Transactional) BeginConfig() {}
73+
74+
// CommitConfig calls through to the HandlerVal
75+
func (t *Transactional) CommitConfig() {}
76+
77+
// RollbackConfig calls through to the HandlerVal
78+
func (t *Transactional) RollbackConfig() {}
79+
6980
// Initializer mocks the configtxapi.Initializer interface
7081
type Initializer struct {
82+
Transactional
7183
Resources
7284

7385
// HandlersVal is returned as the result of Handlers()
7486
HandlerVal configtxapi.Handler
87+
88+
// PolicyHandlerVal is reutrned at the result of PolicyHandler()
89+
PolicyHandlerVal *PolicyHandler
7590
}
7691

7792
// Returns the HandlersVal
7893
func (i *Initializer) Handler(path []string) (configtxapi.Handler, error) {
7994
return i.HandlerVal, nil
8095
}
8196

82-
func (i *Initializer) PolicyProposer() configtxapi.Handler {
83-
panic("Unimplemented")
97+
// Returns the PolicyHandlerVal
98+
func (i *Initializer) PolicyHandler() configtxapi.PolicyHandler {
99+
return i.PolicyHandlerVal
84100
}
85101

86-
// BeginConfig calls through to the HandlerVal
87-
func (i *Initializer) BeginConfig() {}
88-
89-
// CommitConfig calls through to the HandlerVal
90-
func (i *Initializer) CommitConfig() {}
102+
// PolicyHandler mocks the configtxapi.PolicyHandler interface
103+
type PolicyHandler struct {
104+
Transactional
105+
LastKey string
106+
LastPath []string
107+
LastValue *cb.ConfigPolicy
108+
ErrorForProposePolicy error
109+
}
91110

92-
// RollbackConfig calls through to the HandlerVal
93-
func (i *Initializer) RollbackConfig() {}
111+
// ProposeConfig sets LastKey to key, LastPath to path, and LastPolicy to configPolicy, returning ErrorForProposedConfig
112+
func (ph *PolicyHandler) ProposePolicy(key string, path []string, configPolicy *cb.ConfigPolicy) error {
113+
ph.LastKey = key
114+
ph.LastValue = configPolicy
115+
ph.LastPath = path
116+
return ph.ErrorForProposePolicy
117+
}
94118

95119
// Handler mocks the configtxapi.Handler interface
96120
type Handler struct {

common/mocks/configtx/configtx_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ import (
2222
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2323
)
2424

25+
func TestConfigtxTransactionalInterface(t *testing.T) {
26+
_ = configtxapi.Transactional(&Transactional{})
27+
}
28+
29+
func TestConfigtxPolicyHandlerInterface(t *testing.T) {
30+
_ = configtxapi.PolicyHandler(&PolicyHandler{})
31+
}
32+
2533
func TestConfigtxInitializerInterface(t *testing.T) {
2634
_ = configtxapi.Initializer(&Initializer{})
2735
}

common/policies/policy.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package policies
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
cb "github.com/hyperledger/fabric/protos/common"
2324

24-
"github.com/golang/protobuf/proto"
2525
logging "github.com/op/go-logging"
2626
)
2727

@@ -106,12 +106,11 @@ func (pm *ManagerImpl) CommitConfig() {
106106
pm.pendingPolicies = nil
107107
}
108108

109-
// TODO, move this off of *cb.ConfigValue and onto *cb.ConfigPolicy
110-
func (pm *ManagerImpl) ProposeConfig(key string, configValue *cb.ConfigValue) error {
111-
policy := &cb.Policy{}
112-
err := proto.Unmarshal(configValue.Value, policy)
113-
if err != nil {
114-
return err
109+
// ProposePolicy takes key, path, and ConfigPolicy and registers it in the proposed PolicyManager, or errors
110+
func (pm *ManagerImpl) ProposePolicy(key string, path []string, configPolicy *cb.ConfigPolicy) error {
111+
policy := configPolicy.Policy
112+
if policy == nil {
113+
return fmt.Errorf("Policy cannot be nil")
115114
}
116115

117116
provider, ok := pm.providers[int32(policy.Type)]
@@ -124,7 +123,18 @@ func (pm *ManagerImpl) ProposeConfig(key string, configValue *cb.ConfigValue) er
124123
return err
125124
}
126125

127-
pm.pendingPolicies[key] = cPolicy
126+
prefix := strings.Join(path, "/")
127+
if len(path) == 0 {
128+
prefix = "/"
129+
}
130+
131+
// TODO, once the other components are ready for it, use '_' below as fqKey
132+
_ = prefix + "/" + key
133+
fqKey := key
134+
135+
logger.Debugf("Writing policy with fqKey: %s", fqKey)
136+
137+
pm.pendingPolicies[fqKey] = cPolicy
128138

129139
logger.Debugf("Proposed new policy %s", key)
130140
return nil

0 commit comments

Comments
 (0)