github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/comm/connection.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 comm
    18  
    19  import (
    20  	"os"
    21  	"time"
    22  
    23  	"google.golang.org/grpc"
    24  	"google.golang.org/grpc/credentials"
    25  	"google.golang.org/grpc/grpclog"
    26  
    27  	"github.com/op/go-logging"
    28  	"github.com/spf13/viper"
    29  )
    30  
    31  const defaultTimeout = time.Second * 3
    32  
    33  var commLogger = logging.MustGetLogger("comm")
    34  
    35  func getEnv(key, def string) string {
    36  	val := os.Getenv(key)
    37  	if len(val) > 0 {
    38  		return val
    39  	} else {
    40  		return def
    41  	}
    42  }
    43  
    44  func GetPeerTestingAddress(port string) string {
    45  	return getEnv("UNIT_TEST_PEER_IP", "localhost") + ":" + port
    46  }
    47  
    48  // NewClientConnectionWithAddress Returns a new grpc.ClientConn to the given address.
    49  func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled bool, creds credentials.TransportCredentials) (*grpc.ClientConn, error) {
    50  	var opts []grpc.DialOption
    51  	if tslEnabled {
    52  		opts = append(opts, grpc.WithTransportCredentials(creds))
    53  	} else {
    54  		opts = append(opts, grpc.WithInsecure())
    55  	}
    56  	opts = append(opts, grpc.WithTimeout(defaultTimeout))
    57  	if block {
    58  		opts = append(opts, grpc.WithBlock())
    59  	}
    60  	conn, err := grpc.Dial(peerAddress, opts...)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return conn, err
    65  }
    66  
    67  // InitTLSForPeer returns TLS credentials for peer
    68  func InitTLSForPeer() credentials.TransportCredentials {
    69  	var sn string
    70  	if viper.GetString("peer.tls.serverhostoverride") != "" {
    71  		sn = viper.GetString("peer.tls.serverhostoverride")
    72  	}
    73  	var creds credentials.TransportCredentials
    74  	if viper.GetString("peer.tls.cert.file") != "" {
    75  		var err error
    76  		creds, err = credentials.NewClientTLSFromFile(viper.GetString("peer.tls.cert.file"), sn)
    77  		if err != nil {
    78  			grpclog.Fatalf("Failed to create TLS credentials %v", err)
    79  		}
    80  	} else {
    81  		creds = credentials.NewClientTLSFromCert(nil, sn)
    82  	}
    83  	return creds
    84  }