|
| 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 | +} |
0 commit comments