github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/gossip/sa/sa.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 sa
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/flogging"
    21  	"github.com/hyperledger/fabric/gossip/api"
    22  	"github.com/hyperledger/fabric/msp/mgmt"
    23  )
    24  
    25  var logger = flogging.MustGetLogger("peer/gossip/sa")
    26  
    27  // mspSecurityAdvisor implements the SecurityAdvisor interface
    28  // using peer's MSPs.
    29  //
    30  // In order for the system to be secure it is vital to have the
    31  // MSPs to be up-to-date. Channels' MSPs are updated via
    32  // configuration transactions distributed by the ordering service.
    33  //
    34  // This implementation assumes that these mechanisms are all in place and working.
    35  type mspSecurityAdvisor struct {
    36  }
    37  
    38  // NewSecurityAdvisor creates a new instance of mspSecurityAdvisor
    39  // that implements MessageCryptoService
    40  func NewSecurityAdvisor() api.SecurityAdvisor {
    41  	return &mspSecurityAdvisor{}
    42  }
    43  
    44  // OrgByPeerIdentity returns the OrgIdentityType
    45  // of a given peer identity.
    46  // If any error occurs, nil is returned.
    47  // This method does not validate peerIdentity.
    48  // This validation is supposed to be done appropriately during the execution flow.
    49  func (advisor *mspSecurityAdvisor) OrgByPeerIdentity(peerIdentity api.PeerIdentityType) api.OrgIdentityType {
    50  	// Validate arguments
    51  	if len(peerIdentity) == 0 {
    52  		logger.Error("Invalid Peer Identity. It must be different from nil.")
    53  
    54  		return nil
    55  	}
    56  
    57  	// Notice that peerIdentity is assumed to be the serialization of an identity.
    58  	// So, first step is the identity deserialization
    59  
    60  	// TODO: This method should return a structure consisting of two fields:
    61  	// one of the MSPidentifier of the MSP the identity belongs to,
    62  	// and then a list of organization units this identity is in possession of.
    63  	// For gossip use, it is the first part that we would need for now,
    64  	// namely the identity's MSP identifier be returned (Identity.GetMSPIdentifier())
    65  
    66  	// First check against the local MSP.
    67  	identity, err := mgmt.GetLocalMSP().DeserializeIdentity([]byte(peerIdentity))
    68  	if err == nil {
    69  		return []byte(identity.GetMSPIdentifier())
    70  	}
    71  
    72  	// Check against managers
    73  	for chainID, mspManager := range mgmt.GetDeserializers() {
    74  		// Deserialize identity
    75  		identity, err := mspManager.DeserializeIdentity([]byte(peerIdentity))
    76  		if err != nil {
    77  			logger.Debug("Failed deserialization identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
    78  			continue
    79  		}
    80  
    81  		return []byte(identity.GetMSPIdentifier())
    82  	}
    83  
    84  	logger.Warning("Peer Identity [% x] cannot be desirialized. No MSP found able to do that.", peerIdentity)
    85  
    86  	return nil
    87  }