@@ -28,6 +28,7 @@ import (
2828 "errors"
2929 "fmt"
3030 "net"
31+ "sync"
3132 "time"
3233
3334 "google.golang.org/grpc/credentials"
@@ -54,6 +55,8 @@ const (
5455)
5556
5657var (
58+ vmOnGCP bool
59+ once sync.Once
5760 maxRPCVersion = & altspb.RpcProtocolVersions_Version {
5861 Major : protocolVersionMaxMajor ,
5962 Minor : protocolVersionMaxMinor ,
@@ -147,6 +150,9 @@ func NewServerCreds(opts *ServerOptions) credentials.TransportCredentials {
147150}
148151
149152func newALTS (side core.Side , accounts []string , hsAddress string ) credentials.TransportCredentials {
153+ once .Do (func () {
154+ vmOnGCP = googlecloud .OnGCE ()
155+ })
150156 if hsAddress == "" {
151157 hsAddress = hypervisorHandshakerServiceAddress
152158 }
@@ -163,7 +169,7 @@ func newALTS(side core.Side, accounts []string, hsAddress string) credentials.Tr
163169
164170// ClientHandshake implements the client side handshake protocol.
165171func (g * altsTC ) ClientHandshake (ctx context.Context , addr string , rawConn net.Conn ) (_ net.Conn , _ credentials.AuthInfo , err error ) {
166- if ! googlecloud . OnGCE () {
172+ if ! vmOnGCP {
167173 return nil , nil , ErrUntrustedPlatform
168174 }
169175
@@ -218,7 +224,7 @@ func (g *altsTC) ClientHandshake(ctx context.Context, addr string, rawConn net.C
218224
219225// ServerHandshake implements the server side ALTS handshaker.
220226func (g * altsTC ) ServerHandshake (rawConn net.Conn ) (_ net.Conn , _ credentials.AuthInfo , err error ) {
221- if ! googlecloud . OnGCE () {
227+ if ! vmOnGCP {
222228 return nil , nil , ErrUntrustedPlatform
223229 }
224230 // Connecting to ALTS handshaker service.
0 commit comments