google.golang.org/grpc@v1.74.2/credentials/alts/internal/handshaker/service/service.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package service manages connections between the VM application and the ALTS
    20  // handshaker service.
    21  package service
    22  
    23  import (
    24  	"sync"
    25  	"time"
    26  
    27  	grpc "google.golang.org/grpc"
    28  	"google.golang.org/grpc/credentials/insecure"
    29  	"google.golang.org/grpc/internal/envconfig"
    30  	"google.golang.org/grpc/keepalive"
    31  )
    32  
    33  var (
    34  	// mu guards hsConnMap and hsDialer.
    35  	mu sync.Mutex
    36  	// hsConn represents a mapping from a hypervisor handshaker service address
    37  	// to a corresponding connection to a hypervisor handshaker service
    38  	// instance.
    39  	hsConnMap = make(map[string]*grpc.ClientConn)
    40  )
    41  
    42  // Dial dials the handshake service in the hypervisor. If a connection has
    43  // already been established, this function returns it. Otherwise, a new
    44  // connection is created.
    45  func Dial(hsAddress string) (*grpc.ClientConn, error) {
    46  	mu.Lock()
    47  	defer mu.Unlock()
    48  
    49  	hsConn, ok := hsConnMap[hsAddress]
    50  	if !ok {
    51  		// Create a new connection to the handshaker service. Note that
    52  		// this connection stays open until the application is closed.
    53  		// Disable the service config to avoid unnecessary TXT record lookups that
    54  		// cause timeouts with some versions of systemd-resolved.
    55  		var err error
    56  		opts := []grpc.DialOption{
    57  			grpc.WithTransportCredentials(insecure.NewCredentials()),
    58  			grpc.WithDisableServiceConfig(),
    59  		}
    60  		if envconfig.ALTSHandshakerKeepaliveParams {
    61  			opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
    62  				Timeout: 10 * time.Second,
    63  				Time:    10 * time.Minute,
    64  			}))
    65  		}
    66  		hsConn, err = grpc.NewClient(hsAddress, opts...)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		hsConnMap[hsAddress] = hsConn
    71  	}
    72  	return hsConn, nil
    73  }
    74  
    75  // CloseForTesting closes all open connections to the handshaker service.
    76  //
    77  // For testing purposes only.
    78  func CloseForTesting() error {
    79  	for _, hsConn := range hsConnMap {
    80  		if hsConn == nil {
    81  			continue
    82  		}
    83  		if err := hsConn.Close(); err != nil {
    84  			return err
    85  		}
    86  	}
    87  
    88  	// Reset the connection map.
    89  	hsConnMap = make(map[string]*grpc.ClientConn)
    90  	return nil
    91  }