Skip to content

Commit f0f7487

Browse files
author
Jason Yellick
committed
[FAB-5806] Create channel config policy bundle
The channel configuration is currently processed via the configtx package. This is actually a conflation of responsibilities, as the configtx package should be concerned about permission to modify a configuration, not the actual contents of the change. This CR creates a new channel config bundle, which will ultimately hold an immutable copy of the channel configuration. For the time being, it implements a poor man's parsing of the policy tree. This temporary parsing will be removed in the future in favor of more robust parsing once the mutability notion has been removed from the policy manager. Change-Id: Ia3f4dc4d247afcc9c4e7895fff93cc2d007fae5a Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 47d0e3a commit f0f7487

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

common/channelconfig/channelconfig.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package channelconfig
8+
9+
import (
10+
"github.com/hyperledger/fabric/common/cauthdsl"
11+
"github.com/hyperledger/fabric/common/flogging"
12+
"github.com/hyperledger/fabric/common/policies"
13+
"github.com/hyperledger/fabric/msp"
14+
cb "github.com/hyperledger/fabric/protos/common"
15+
16+
"github.com/pkg/errors"
17+
)
18+
19+
var logger = flogging.MustGetLogger("common/channelconfig")
20+
21+
// RootGroupKey is the key for namespacing the channel config, especially for
22+
// policy evaluation.
23+
const RootGroupKey = "Channel"
24+
25+
// Bundle is a collection of resources which will always have a consistent
26+
// view of the channel configuration. In particular, for a given bundle reference,
27+
// the config sequence, the policy manager etc. will always return exactly the
28+
// same value. The Bundle structure is immutable and will always be replaced in its
29+
// entirety, with new backing memory.
30+
type Bundle struct {
31+
policyManager policies.Manager
32+
mspManager msp.MSPManager
33+
}
34+
35+
// PolicyManager returns the policy manager constructed for this config
36+
func (b *Bundle) PolicyManager() policies.Manager {
37+
return b.policyManager
38+
}
39+
40+
// MSPManager returns the MSP manager constructed for this config
41+
func (b *Bundle) MSPManager() msp.MSPManager {
42+
return b.mspManager
43+
}
44+
45+
// NewBundle creates a new immutable bundle of configuration
46+
func NewBundle(config *cb.Config) (*Bundle, error) {
47+
mspManager := msp.NewMSPManager()
48+
// XXX Truly initialize the MSP manager
49+
err := mspManager.Setup([]msp.MSP{})
50+
if err != nil {
51+
logger.Panicf("Error creating MSP manager")
52+
}
53+
54+
policyProviderMap := make(map[int32]policies.Provider)
55+
for pType := range cb.Policy_PolicyType_name {
56+
rtype := cb.Policy_PolicyType(pType)
57+
switch rtype {
58+
case cb.Policy_UNKNOWN:
59+
// Do not register a handler
60+
case cb.Policy_SIGNATURE:
61+
policyProviderMap[pType] = cauthdsl.NewPolicyProvider(mspManager)
62+
case cb.Policy_MSP:
63+
// Add hook for MSP Handler here
64+
}
65+
}
66+
67+
policyManager := policies.NewManagerImpl(RootGroupKey, policyProviderMap)
68+
err = InitializePolicyManager(policyManager, config.ChannelGroup)
69+
if err != nil {
70+
return nil, errors.Wrap(err, "initializing policymanager failed")
71+
}
72+
73+
return &Bundle{
74+
mspManager: mspManager,
75+
policyManager: policyManager,
76+
}, nil
77+
}

common/channelconfig/util.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package channelconfig
8+
9+
import (
10+
"github.com/hyperledger/fabric/common/policies"
11+
cb "github.com/hyperledger/fabric/protos/common"
12+
13+
"github.com/pkg/errors"
14+
)
15+
16+
// InitializePolicyManager takes a config group and uses it to initialize a PolicyManager
17+
// XXX This goes away by the end of the CR series so no test logic
18+
func InitializePolicyManager(pm policies.Proposer, group *cb.ConfigGroup) error {
19+
subGroups := make([]string, len(group.Groups))
20+
i := 0
21+
for subGroup := range group.Groups {
22+
subGroups[i] = subGroup
23+
i++
24+
}
25+
26+
subPolicyHandlers, err := pm.BeginPolicyProposals("", subGroups)
27+
if err != nil {
28+
return err
29+
}
30+
31+
for key, policy := range group.Policies {
32+
_, err := pm.ProposePolicy("", key, policy)
33+
if err != nil {
34+
return err
35+
}
36+
}
37+
38+
for i := range subGroups {
39+
if err := InitializePolicyManager(subPolicyHandlers[i], group.Groups[subGroups[i]]); err != nil {
40+
return errors.Wrapf(err, "failed initializing subgroup %s", subGroups[i])
41+
}
42+
}
43+
44+
pm.CommitProposals("")
45+
return nil
46+
}

0 commit comments

Comments
 (0)