Skip to content

Commit b331a48

Browse files
authored
alts: re-add vmOnGCP and once globals for easier testing (#4284)
1 parent d26af8e commit b331a48

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

credentials/alts/alts.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5657
var (
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

149152
func 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.
165171
func (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.
220226
func (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

Comments
 (0)