Skip to content

Commit

Permalink
only check availability for IMDS (#685)
Browse files Browse the repository at this point in the history
Co-authored-by: Joel Hendrix <jhendrix@microsoft.com>
  • Loading branch information
scott-the-programmer and jhendrixMSFT authored May 4, 2022
1 parent 0dbcd58 commit 4990699
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions autorest/adal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,12 +1363,25 @@ func NewMultiTenantServicePrincipalTokenFromCertificate(multiTenantCfg MultiTena

// MSIAvailable returns true if the MSI endpoint is available for authentication.
func MSIAvailable(ctx context.Context, s Sender) bool {
msiType, _, err := getMSIType()

if err != nil {
return false
}

if msiType != msiTypeIMDS {
return true
}

if s == nil {
s = sender()
}

resp, err := getMSIEndpoint(ctx, s)

if err == nil {
resp.Body.Close()
}

return err == nil
}
26 changes: 26 additions & 0 deletions autorest/adal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,32 @@ func TestMSIAvailableSuccess(t *testing.T) {
}
}

func TestMSIAvailableAppService(t *testing.T) {
os.Setenv("MSI_ENDPOINT", "http://localhost")
os.Setenv("MSI_SECRET", "super")
defer func() {
os.Unsetenv("MSI_ENDPOINT")
os.Unsetenv("MSI_SECRET")
}()
c := mocks.NewSender()
c.AppendResponse(mocks.NewResponse())
available := MSIAvailable(context.Background(), c)

if !available {
t.Fatal("expected MSI to be available")
}
}

func TestMSIAvailableIMDS(t *testing.T) {
c := mocks.NewSender()
c.AppendResponse(mocks.NewResponse())
available := MSIAvailable(context.Background(), c)

if !available {
t.Fatal("expected MSI to be available")
}
}

func TestMSIAvailableSlow(t *testing.T) {
c := mocks.NewSender()
// introduce a long response delay to simulate the endpoint not being available
Expand Down

0 comments on commit 4990699

Please sign in to comment.