github.com/yimialmonte/fabric@v2.1.1+incompatible/discovery/cmd/stub.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package discovery
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/discovery"
    13  	"github.com/hyperledger/fabric/cmd/common"
    14  	"github.com/hyperledger/fabric/cmd/common/comm"
    15  	"github.com/hyperledger/fabric/cmd/common/signer"
    16  	discoveryclient "github.com/hyperledger/fabric/discovery/client"
    17  	"github.com/hyperledger/fabric/protoutil"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  //go:generate mockery -dir . -name LocalResponse -case underscore -output mocks/
    22  
    23  // LocalResponse is the local interface used to generate mocks for foreign interface.
    24  type LocalResponse interface {
    25  	discoveryclient.LocalResponse
    26  }
    27  
    28  //go:generate mockery -dir . -name ChannelResponse -case underscore -output mocks/
    29  
    30  // ChannelResponse is the local interface used to generate mocks for foreign interface.
    31  type ChannelResponse interface {
    32  	discoveryclient.ChannelResponse
    33  }
    34  
    35  //go:generate mockery -dir . -name ServiceResponse -case underscore -output mocks/
    36  
    37  // ServiceResponse represents a response sent from the discovery service
    38  type ServiceResponse interface {
    39  	// ForChannel returns a ChannelResponse in the context of a given channel
    40  	ForChannel(string) discoveryclient.ChannelResponse
    41  
    42  	// ForLocal returns a LocalResponse in the context of no channel
    43  	ForLocal() discoveryclient.LocalResponse
    44  
    45  	// Raw returns the raw response from the server
    46  	Raw() *discovery.Response
    47  }
    48  
    49  type response struct {
    50  	raw *discovery.Response
    51  	discoveryclient.Response
    52  }
    53  
    54  func (r *response) Raw() *discovery.Response {
    55  	return r.raw
    56  }
    57  
    58  // ClientStub is a stub that communicates with the discovery service
    59  // using the discovery client implementation
    60  type ClientStub struct {
    61  }
    62  
    63  // Send sends the request, and receives a response
    64  func (stub *ClientStub) Send(server string, conf common.Config, req *discoveryclient.Request) (ServiceResponse, error) {
    65  	comm, err := comm.NewClient(conf.TLSConfig)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	signer, err := signer.NewSigner(conf.SignerConfig)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
    74  	defer cancel()
    75  
    76  	disc := discoveryclient.NewClient(comm.NewDialer(server), signer.Sign, 0)
    77  
    78  	resp, err := disc.Send(timeout, req, &discovery.AuthInfo{
    79  		ClientIdentity:    signer.Creator,
    80  		ClientTlsCertHash: comm.TLSCertHash,
    81  	})
    82  	if err != nil {
    83  		return nil, errors.Errorf("failed connecting to %s: %v", server, err)
    84  	}
    85  	return &response{
    86  		Response: resp,
    87  	}, nil
    88  }
    89  
    90  // RawStub is a stub that communicates with the discovery service
    91  // without any intermediary.
    92  type RawStub struct {
    93  }
    94  
    95  // Send sends the request, and receives a response
    96  func (stub *RawStub) Send(server string, conf common.Config, req *discoveryclient.Request) (ServiceResponse, error) {
    97  	comm, err := comm.NewClient(conf.TLSConfig)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	signer, err := signer.NewSigner(conf.SignerConfig)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
   106  	defer cancel()
   107  
   108  	req.Authentication = &discovery.AuthInfo{
   109  		ClientIdentity:    signer.Creator,
   110  		ClientTlsCertHash: comm.TLSCertHash,
   111  	}
   112  
   113  	payload := protoutil.MarshalOrPanic(req.Request)
   114  	sig, err := signer.Sign(payload)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	cc, err := comm.NewDialer(server)()
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	resp, err := discovery.NewDiscoveryClient(cc).Discover(timeout, &discovery.SignedRequest{
   124  		Payload:   payload,
   125  		Signature: sig,
   126  	})
   127  
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return &response{
   133  		raw: resp,
   134  	}, nil
   135  }