github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"sync"
    21  
    22  	"errors"
    23  
    24  	"github.com/hyperledger/fabric/bccsp/factory"
    25  	"github.com/hyperledger/fabric/msp"
    26  	"github.com/op/go-logging"
    27  )
    28  
    29  // LoadLocalMsp loads the local MSP from the specified directory
    30  func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error {
    31  	if mspID == "" {
    32  		return errors.New("The local MSP must have an ID")
    33  	}
    34  
    35  	conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	return GetLocalMSP().Setup(conf)
    41  }
    42  
    43  // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
    44  // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE
    45  // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;
    46  // HOWEVER IN THE INTERIM, THESE HELPER FUNCTIONS ARE REQUIRED
    47  
    48  var m sync.Mutex
    49  var localMsp msp.MSP
    50  var mspMap map[string]msp.MSPManager = make(map[string]msp.MSPManager)
    51  var mspLogger = logging.MustGetLogger("msp")
    52  
    53  // GetManagerForChain returns the msp manager for the supplied
    54  // chain; if no such manager exists, one is created
    55  func GetManagerForChain(chainID string) msp.MSPManager {
    56  	m.Lock()
    57  	defer m.Unlock()
    58  
    59  	mspMgr, ok := mspMap[chainID]
    60  	if !ok {
    61  		mspLogger.Debugf("Created new msp manager for chain %s", chainID)
    62  		mspMgr = msp.NewMSPManager()
    63  		mspMap[chainID] = mspMgr
    64  	} else {
    65  		mspLogger.Debugf("Returning existing manager for chain %s", chainID)
    66  	}
    67  
    68  	return mspMgr
    69  }
    70  
    71  // GetManagers returns all the managers registered
    72  func GetManagers() map[string]msp.MSPManager {
    73  	m.Lock()
    74  	defer m.Unlock()
    75  
    76  	clone := make(map[string]msp.MSPManager)
    77  
    78  	for key, mspManager := range mspMap {
    79  		clone[key] = mspManager
    80  	}
    81  
    82  	return clone
    83  }
    84  
    85  // GetManagerForChainIfExists returns the MSPManager associated to ChainID
    86  // it it exists
    87  func GetManagerForChainIfExists(ChainID string) msp.MSPManager {
    88  	m.Lock()
    89  	defer m.Unlock()
    90  
    91  	return mspMap[ChainID]
    92  }
    93  
    94  // XXXSetMSPManager is a stopgap solution to transition from the custom MSP config block
    95  // parsing to the configtx.Manager interface, while preserving the problematic singleton
    96  // nature of the MSP manager
    97  func XXXSetMSPManager(chainID string, manager msp.MSPManager) {
    98  	m.Lock()
    99  	defer m.Unlock()
   100  
   101  	mspMap[chainID] = manager
   102  }
   103  
   104  // GetLocalMSP returns the local msp (and creates it if it doesn't exist)
   105  func GetLocalMSP() msp.MSP {
   106  	var lclMsp msp.MSP
   107  	var created bool = false
   108  	{
   109  		m.Lock()
   110  		defer m.Unlock()
   111  
   112  		lclMsp = localMsp
   113  		if lclMsp == nil {
   114  			var err error
   115  			created = true
   116  			lclMsp, err = msp.NewBccspMsp()
   117  			if err != nil {
   118  				mspLogger.Fatalf("Failed to initialize local MSP, received err %s", err)
   119  			}
   120  			localMsp = lclMsp
   121  		}
   122  	}
   123  
   124  	if created {
   125  		mspLogger.Debugf("Created new local MSP")
   126  	} else {
   127  		mspLogger.Debugf("Returning existing local MSP")
   128  	}
   129  
   130  	return lclMsp
   131  }
   132  
   133  // GetIdentityDeserializer returns the IdentityDeserializer for the given chain
   134  func GetIdentityDeserializer(chainID string) msp.IdentityDeserializer {
   135  	if chainID == "" {
   136  		return GetLocalMSP()
   137  	}
   138  
   139  	return GetManagerForChain(chainID)
   140  }
   141  
   142  // GetLocalSigningIdentityOrPanic returns the local signing identity or panic in case
   143  // or error
   144  func GetLocalSigningIdentityOrPanic() msp.SigningIdentity {
   145  	id, err := GetLocalMSP().GetDefaultSigningIdentity()
   146  	if err != nil {
   147  		mspLogger.Panicf("Failed getting local signing identity [%s]", err)
   148  	}
   149  	return id
   150  }