github.com/defanghe/fabric@v2.1.1+incompatible/msp/mspmgrimpl.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 msp
    18  
    19  import (
    20  	"github.com/golang/protobuf/proto"
    21  	"github.com/hyperledger/fabric-protos-go/msp"
    22  	"github.com/hyperledger/fabric/common/flogging"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  var mspLogger = flogging.MustGetLogger("msp")
    27  
    28  type mspManagerImpl struct {
    29  	// map that contains all MSPs that we have setup or otherwise added
    30  	mspsMap map[string]MSP
    31  
    32  	// map that maps MSPs by their provider types
    33  	mspsByProviders map[ProviderType][]MSP
    34  
    35  	// error that might have occurred at startup
    36  	up bool
    37  }
    38  
    39  // NewMSPManager returns a new MSP manager instance;
    40  // note that this instance is not initialized until
    41  // the Setup method is called
    42  func NewMSPManager() MSPManager {
    43  	return &mspManagerImpl{}
    44  }
    45  
    46  // Setup initializes the internal data structures of this manager and creates MSPs
    47  func (mgr *mspManagerImpl) Setup(msps []MSP) error {
    48  	if mgr.up {
    49  		mspLogger.Infof("MSP manager already up")
    50  		return nil
    51  	}
    52  
    53  	mspLogger.Debugf("Setting up the MSP manager (%d msps)", len(msps))
    54  
    55  	// create the map that assigns MSP IDs to their manager instance - once
    56  	mgr.mspsMap = make(map[string]MSP)
    57  
    58  	// create the map that sorts MSPs by their provider types
    59  	mgr.mspsByProviders = make(map[ProviderType][]MSP)
    60  
    61  	for _, msp := range msps {
    62  		// add the MSP to the map of active MSPs
    63  		mspID, err := msp.GetIdentifier()
    64  		if err != nil {
    65  			return errors.WithMessage(err, "could not extract msp identifier")
    66  		}
    67  		mgr.mspsMap[mspID] = msp
    68  		providerType := msp.GetType()
    69  		mgr.mspsByProviders[providerType] = append(mgr.mspsByProviders[providerType], msp)
    70  	}
    71  
    72  	mgr.up = true
    73  
    74  	mspLogger.Debugf("MSP manager setup complete, setup %d msps", len(msps))
    75  
    76  	return nil
    77  }
    78  
    79  // GetMSPs returns the MSPs that are managed by this manager
    80  func (mgr *mspManagerImpl) GetMSPs() (map[string]MSP, error) {
    81  	return mgr.mspsMap, nil
    82  }
    83  
    84  // DeserializeIdentity returns an identity given its serialized version supplied as argument
    85  func (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, error) {
    86  	// We first deserialize to a SerializedIdentity to get the MSP ID
    87  	sId := &msp.SerializedIdentity{}
    88  	err := proto.Unmarshal(serializedID, sId)
    89  	if err != nil {
    90  		return nil, errors.Wrap(err, "could not deserialize a SerializedIdentity")
    91  	}
    92  
    93  	// we can now attempt to obtain the MSP
    94  	msp := mgr.mspsMap[sId.Mspid]
    95  	if msp == nil {
    96  		return nil, errors.Errorf("MSP %s is unknown", sId.Mspid)
    97  	}
    98  
    99  	switch t := msp.(type) {
   100  	case *bccspmsp:
   101  		return t.deserializeIdentityInternal(sId.IdBytes)
   102  	case *idemixmsp:
   103  		return t.deserializeIdentityInternal(sId.IdBytes)
   104  	default:
   105  		return t.DeserializeIdentity(serializedID)
   106  	}
   107  }
   108  
   109  func (mgr *mspManagerImpl) IsWellFormed(identity *msp.SerializedIdentity) error {
   110  	// Iterate over all the MSPs by their providers, and find at least 1 MSP that can attest
   111  	// that this identity is well formed
   112  	for _, mspList := range mgr.mspsByProviders {
   113  		// We are guaranteed to have at least 1 MSP in each list from the initialization at Setup()
   114  		msp := mspList[0]
   115  		if err := msp.IsWellFormed(identity); err == nil {
   116  			return nil
   117  		}
   118  	}
   119  	return errors.New("no MSP provider recognizes the identity")
   120  }