github.com/zorawar87/trillian@v1.2.1/client/rpcflags/rpcflags.go (about)

     1  // Copyright 2018 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpcflags
    16  
    17  import (
    18  	"flag"
    19  
    20  	"github.com/golang/glog"
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/grpc/credentials"
    23  )
    24  
    25  var (
    26  	// tlsCertFile is the flag-assigned value for the path to the Trillian server's TLS certificate.
    27  	tlsCertFile = flag.String("tls_cert_file", "", "Path to the file containing the Trillian server's PEM-encoded public TLS certificate. If unset, unsecured connections will be used")
    28  )
    29  
    30  // NewClientDialOptionsFromFlags returns a list of grpc.DialOption values to be
    31  // passed as DialOption arguments to grpc.Dial
    32  func NewClientDialOptionsFromFlags() ([]grpc.DialOption, error) {
    33  	dialOpts := []grpc.DialOption{}
    34  
    35  	if *tlsCertFile == "" {
    36  		glog.Warning("Using an insecure gRPC connection to Trillian")
    37  		dialOpts = append(dialOpts, grpc.WithInsecure())
    38  	} else {
    39  		creds, err := credentials.NewClientTLSFromFile(*tlsCertFile, "")
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  		dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
    44  	}
    45  
    46  	return dialOpts, nil
    47  }