credentials/alts: Assume not running on GCP if DMI not present (#2996)

fixes #2995
This commit is contained in:
Michael Hudson-Doyle
2019-08-27 12:06:13 +12:00
committed by Cesar Ghali
parent 45bd2846a3
commit d5a36f00e6
2 changed files with 37 additions and 13 deletions

View File

@ -83,6 +83,9 @@ var (
// running on GCP. // running on GCP.
func isRunningOnGCP() bool { func isRunningOnGCP() bool {
manufacturer, err := readManufacturer() manufacturer, err := readManufacturer()
if os.IsNotExist(err) {
return false
}
if err != nil { if err != nil {
log.Fatalf("failure to read manufacturer information: %v", err) log.Fatalf("failure to read manufacturer information: %v", err)
} }

View File

@ -21,6 +21,7 @@ package alts
import ( import (
"context" "context"
"io" "io"
"os"
"strings" "strings"
"testing" "testing"
@ -28,6 +29,34 @@ import (
"google.golang.org/grpc/peer" "google.golang.org/grpc/peer"
) )
func setupManufacturerReader(testOS string, reader func() (io.Reader, error)) func() {
tmpOS := runningOS
tmpReader := manufacturerReader
// Set test OS and reader function.
runningOS = testOS
manufacturerReader = reader
return func() {
runningOS = tmpOS
manufacturerReader = tmpReader
}
}
func setup(testOS string, testReader io.Reader) func() {
reader := func() (io.Reader, error) {
return testReader, nil
}
return setupManufacturerReader(testOS, reader)
}
func setupError(testOS string, err error) func() {
reader := func() (io.Reader, error) {
return nil, err
}
return setupManufacturerReader(testOS, reader)
}
func TestIsRunningOnGCP(t *testing.T) { func TestIsRunningOnGCP(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
description string description string
@ -53,20 +82,12 @@ func TestIsRunningOnGCP(t *testing.T) {
} }
} }
func setup(testOS string, testReader io.Reader) func() { func TestIsRunningOnGCPNoProductNameFile(t *testing.T) {
tmpOS := runningOS reverseFunc := setupError("linux", os.ErrNotExist)
tmpReader := manufacturerReader if isRunningOnGCP() {
t.Errorf("ErrNotExist: isRunningOnGCP()=true, want false")
// Set test OS and reader function.
runningOS = testOS
manufacturerReader = func() (io.Reader, error) {
return testReader, nil
}
return func() {
runningOS = tmpOS
manufacturerReader = tmpReader
} }
reverseFunc()
} }
func TestAuthInfoFromContext(t *testing.T) { func TestAuthInfoFromContext(t *testing.T) {