github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/msp/mgmt/mgmt.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mgmt
    18  
    19  import (
    20  	"reflect"
    21  	"sync"
    22  
    23  	"errors"
    24  
    25  	"github.com/hyperledger/fabric/bccsp/factory"
    26  	configvaluesmsp "github.com/hyperledger/fabric/common/config/msp"
    27  	"github.com/hyperledger/fabric/common/flogging"
    28  	"github.com/hyperledger/fabric/core/config"
    29  	"github.com/hyperledger/fabric/msp"
    30  )
    31  
    32  // LoadLocalMsp loads the local MSP from the specified directory
    33  func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error {
    34  	if mspID == "" {
    35  		return errors.New("The local MSP must have an ID")
    36  	}
    37  
    38  	conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	return GetLocalMSP().Setup(conf)
    44  }
    45  
    46  // Loads the development local MSP for use in testing.  Not valid for production/runtime context
    47  func LoadDevMsp() error {
    48  	mspDir, err := config.GetDevMspDir()
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return LoadLocalMsp(mspDir, nil, "DEFAULT")
    54  }
    55  
    56  // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
    57  // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE
    58  // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;
    59  // HOWEVER IN THE INTERIM, THESE HELPER FUNCTIONS ARE REQUIRED
    60  
    61  var m sync.Mutex
    62  var localMsp msp.MSP
    63  var mspMap map[string]msp.MSPManager = make(map[string]msp.MSPManager)
    64  var mspLogger = flogging.MustGetLogger("msp")
    65  
    66  // GetManagerForChain returns the msp manager for the supplied
    67  // chain; if no such manager exists, one is created
    68  func GetManagerForChain(chainID string) msp.MSPManager {
    69  	m.Lock()
    70  	defer m.Unlock()
    71  
    72  	mspMgr, ok := mspMap[chainID]
    73  	if !ok {
    74  		mspLogger.Debugf("Created new msp manager for chain %s", chainID)
    75  		mspMgr = msp.NewMSPManager()
    76  		mspMap[chainID] = mspMgr
    77  	} else {
    78  		switch mgr := mspMgr.(type) {
    79  		case *configvaluesmsp.MSPConfigHandler:
    80  			// check for nil MSPManager interface as it can exist but not be
    81  			// instantiated
    82  			if mgr.MSPManager == nil {
    83  				mspLogger.Debugf("MSPManager is not instantiated; no MSPs are defined for this channel.")
    84  				// return nil so the MSPManager methods cannot be accidentally called,
    85  				// which would result in a panic
    86  				return nil
    87  			}
    88  		default:
    89  			// check for internal mspManagerImpl type. if a different type is found,
    90  			// it's because a developer has added a new type that implements the
    91  			// MSPManager interface and should add a case to the logic above to handle
    92  			// it.
    93  			if reflect.TypeOf(mgr).Elem().Name() != "mspManagerImpl" {
    94  				panic("Found unexpected MSPManager type.")
    95  			}
    96  		}
    97  		mspLogger.Debugf("Returning existing manager for channel '%s'", chainID)
    98  	}
    99  	return mspMgr
   100  }
   101  
   102  // GetManagers returns all the managers registered
   103  func GetDeserializers() map[string]msp.IdentityDeserializer {
   104  	m.Lock()
   105  	defer m.Unlock()
   106  
   107  	clone := make(map[string]msp.IdentityDeserializer)
   108  
   109  	for key, mspManager := range mspMap {
   110  		clone[key] = mspManager
   111  	}
   112  
   113  	return clone
   114  }
   115  
   116  // GetManagerForChainIfExists returns the MSPManager associated to ChainID
   117  // it it exists
   118  func GetManagerForChainIfExists(ChainID string) msp.MSPManager {
   119  	m.Lock()
   120  	defer m.Unlock()
   121  
   122  	return mspMap[ChainID]
   123  }
   124  
   125  // XXXSetMSPManager is a stopgap solution to transition from the custom MSP config block
   126  // parsing to the configtx.Manager interface, while preserving the problematic singleton
   127  // nature of the MSP manager
   128  func XXXSetMSPManager(chainID string, manager msp.MSPManager) {
   129  	m.Lock()
   130  	defer m.Unlock()
   131  
   132  	mspMap[chainID] = manager
   133  }
   134  
   135  // GetLocalMSP returns the local msp (and creates it if it doesn't exist)
   136  func GetLocalMSP() msp.MSP {
   137  	var lclMsp msp.MSP
   138  	var created bool = false
   139  	{
   140  		m.Lock()
   141  		defer m.Unlock()
   142  
   143  		lclMsp = localMsp
   144  		if lclMsp == nil {
   145  			var err error
   146  			created = true
   147  			lclMsp, err = msp.NewBccspMsp()
   148  			if err != nil {
   149  				mspLogger.Fatalf("Failed to initialize local MSP, received err %s", err)
   150  			}
   151  			localMsp = lclMsp
   152  		}
   153  	}
   154  
   155  	if created {
   156  		mspLogger.Debugf("Created new local MSP")
   157  	} else {
   158  		mspLogger.Debugf("Returning existing local MSP")
   159  	}
   160  
   161  	return lclMsp
   162  }
   163  
   164  // GetIdentityDeserializer returns the IdentityDeserializer for the given chain
   165  func GetIdentityDeserializer(chainID string) msp.IdentityDeserializer {
   166  	if chainID == "" {
   167  		return GetLocalMSP()
   168  	}
   169  
   170  	return GetManagerForChain(chainID)
   171  }
   172  
   173  // GetLocalSigningIdentityOrPanic returns the local signing identity or panic in case
   174  // or error
   175  func GetLocalSigningIdentityOrPanic() msp.SigningIdentity {
   176  	id, err := GetLocalMSP().GetDefaultSigningIdentity()
   177  	if err != nil {
   178  		mspLogger.Panicf("Failed getting local signing identity [%s]", err)
   179  	}
   180  	return id
   181  }