diff --git a/msp/mgmt/config.go b/common/configtx/handlers/msp/config.go similarity index 58% rename from msp/mgmt/config.go rename to common/configtx/handlers/msp/config.go index a4034e5e590..8d5f689896f 100644 --- a/msp/mgmt/config.go +++ b/common/configtx/handlers/msp/config.go @@ -14,79 +14,22 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mgmt +package msp import ( "fmt" - "os" - "path/filepath" "github.com/golang/protobuf/proto" - "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/protos/common" mspprotos "github.com/hyperledger/fabric/protos/msp" ) -func LoadLocalMsp(dir string) error { - conf, err := msp.GetLocalMspConfig(dir) - if err != nil { - return err - } - - return GetLocalMSP().Setup(conf) -} - -func getConfigPath(dir string) (string, error) { - // Try to read the dir - if _, err := os.Stat(dir); err != nil { - cfg := os.Getenv("PEER_CFG_PATH") - if cfg != "" { - dir = filepath.Join(cfg, dir) - } else { - dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/") - } - if _, err := os.Stat(dir); err != nil { - return "", err - } - } - return dir, nil -} - -// FIXME: this is required for now because we need a local MSP -// and also the MSP mgr for the test chain; as soon as the code -// to setup chains is ready, the chain should be setup using -// the method below and this method should disappear -func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error { - var err error - if dir, err = getConfigPath(dir); err != nil { - return err - } - conf, err := msp.GetLocalMspConfig(dir) - if err != nil { - return err - } - - err = GetLocalMSP().Setup(conf) - if err != nil { - return err - } - - fakeConfig := []*mspprotos.MSPConfig{conf} - - err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig) - if err != nil { - return err - } - - return nil -} - // MSPConfigHandler type MSPConfigHandler struct { - config []*mspprotos.MSPConfig - proposedMgr msp.MSPManager - committedMgr msp.MSPManager + msp.MSPManager + config []*mspprotos.MSPConfig + proposedMgr msp.MSPManager } // BeginConfig called when a config proposal is begun @@ -109,7 +52,7 @@ func (bh *MSPConfigHandler) CommitConfig() { panic("Programming error, called CommitConfig with no proposal in process") } - bh.committedMgr = bh.proposedMgr + bh.MSPManager = bh.proposedMgr bh.config = nil bh.proposedMgr = nil } @@ -131,12 +74,7 @@ func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigItem) error { return bh.proposedMgr.Setup(bh.config) } -// GetMSPManager returns the currently committed MSP manager -func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager { - return bh.committedMgr -} - // DesierializeIdentity allows *MSPConfigHandler to implement the msp.Common interface func (bh *MSPConfigHandler) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) { - return bh.committedMgr.DeserializeIdentity(serializedIdentity) + return bh.DeserializeIdentity(serializedIdentity) } diff --git a/msp/mgmt/mspconfigmgr_test.go b/common/configtx/handlers/msp/config_test.go similarity index 80% rename from msp/mgmt/mspconfigmgr_test.go rename to common/configtx/handlers/msp/config_test.go index fccfba31235..f3bf97d7f80 100644 --- a/msp/mgmt/mspconfigmgr_test.go +++ b/common/configtx/handlers/msp/config_test.go @@ -14,21 +14,19 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mgmt_test +package msp import ( "testing" "github.com/golang/protobuf/proto" - configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/msp" - . "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/protos/common" "github.com/stretchr/testify/assert" ) func TestMSPConfigManager(t *testing.T) { - conf, err := msp.GetLocalMspConfig("../sampleconfig/") + conf, err := msp.GetLocalMspConfig("../../../../msp/sampleconfig/") assert.NoError(t, err) confBytes, err := proto.Marshal(conf) @@ -39,17 +37,13 @@ func TestMSPConfigManager(t *testing.T) { // test success: // begin/propose/commit - var mspCH configtxapi.Handler - mspCH = &MSPConfigHandler{} + mspCH := &MSPConfigHandler{} mspCH.BeginConfig() err = mspCH.ProposeConfig(ci) assert.NoError(t, err) mspCH.CommitConfig() - // get the manager we just created - mgr := mspCH.(*MSPConfigHandler).GetMSPManager() - - msps, err := mgr.GetMSPs() + msps, err := mspCH.GetMSPs() assert.NoError(t, err) if msps == nil || len(msps) == 0 { diff --git a/common/configtx/resources.go b/common/configtx/resources.go index 9279581b6f0..7c3a67a8d3d 100644 --- a/common/configtx/resources.go +++ b/common/configtx/resources.go @@ -20,9 +20,9 @@ import ( "github.com/hyperledger/fabric/common/cauthdsl" "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/configtx/handlers/channel" + configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" - mspmgmt "github.com/hyperledger/fabric/msp/mgmt" cb "github.com/hyperledger/fabric/protos/common" ) @@ -30,7 +30,7 @@ type resources struct { handlers map[cb.ConfigItem_ConfigType]api.Handler policyManager policies.Manager channelConfig api.ChannelConfig - mspConfigHandler *mspmgmt.MSPConfigHandler + mspConfigHandler *configtxmsp.MSPConfigHandler } // PolicyManager returns the policies.Manager for the chain @@ -45,7 +45,7 @@ func (r *resources) ChannelConfig() api.ChannelConfig { // MSPManager returns the msp.MSPManager for the chain func (r *resources) MSPManager() msp.MSPManager { - return r.mspConfigHandler.GetMSPManager() + return r.mspConfigHandler } // Handlers returns the handlers to be used when initializing the configtx.Manager @@ -55,7 +55,7 @@ func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]api.Handler { // NewInitializer creates a chain initializer for the basic set of common chain resources func NewInitializer() api.Initializer { - mspConfigHandler := &mspmgmt.MSPConfigHandler{} + mspConfigHandler := &configtxmsp.MSPConfigHandler{} policyProviderMap := make(map[int32]policies.Provider) for pType := range cb.Policy_PolicyType_name { rtype := cb.Policy_PolicyType(pType) diff --git a/msp/mgmt/mgmt.go b/msp/mgmt/mgmt.go index 771c08d12c3..8d62ae0bcfa 100644 --- a/msp/mgmt/mgmt.go +++ b/msp/mgmt/mgmt.go @@ -17,12 +17,71 @@ limitations under the License. package mgmt import ( + "os" + "path/filepath" "sync" + "github.com/hyperledger/fabric/common/util" + mspprotos "github.com/hyperledger/fabric/protos/msp" + "github.com/hyperledger/fabric/msp" "github.com/op/go-logging" ) +func LoadLocalMsp(dir string) error { + conf, err := msp.GetLocalMspConfig(dir) + if err != nil { + return err + } + + return GetLocalMSP().Setup(conf) +} + +func getConfigPath(dir string) (string, error) { + // Try to read the dir + if _, err := os.Stat(dir); err != nil { + cfg := os.Getenv("PEER_CFG_PATH") + if cfg != "" { + dir = filepath.Join(cfg, dir) + } else { + dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/") + } + if _, err := os.Stat(dir); err != nil { + return "", err + } + } + return dir, nil +} + +// FIXME: this is required for now because we need a local MSP +// and also the MSP mgr for the test chain; as soon as the code +// to setup chains is ready, the chain should be setup using +// the method below and this method should disappear +func LoadFakeSetupWithLocalMspAndTestChainMsp(dir string) error { + var err error + if dir, err = getConfigPath(dir); err != nil { + return err + } + conf, err := msp.GetLocalMspConfig(dir) + if err != nil { + return err + } + + err = GetLocalMSP().Setup(conf) + if err != nil { + return err + } + + fakeConfig := []*mspprotos.MSPConfig{conf} + + err = GetManagerForChain(util.GetTestChainID()).Setup(fakeConfig) + if err != nil { + return err + } + + return nil +} + // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE, // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;