Skip to content

Commit

Permalink
only check availability for IMDS
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-the-programmer committed Apr 22, 2022
1 parent b3899c1 commit 7ca18ac
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
Expand Up @@ -1317,12 +1317,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
Expand Up @@ -1343,6 +1343,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 7ca18ac

Please sign in to comment.