Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit a46566d

Browse files
authored
Remove IMDS probe when determining environment (#669)
* Remove IMDS probe when determining environment Assume IMDS when other env vars aren't set. If the request fails, regular retry logic will kick in. Bump up the health check probe to two seconds. * Add default sender when provided sender is nil
1 parent 4c698b4 commit a46566d

File tree

4 files changed

+9
-24
lines changed

4 files changed

+9
-24
lines changed

autorest/adal/token.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,6 @@ const (
676676

677677
func (m msiType) String() string {
678678
switch m {
679-
case msiTypeUnavailable:
680-
return "unavailable"
681679
case msiTypeAppServiceV20170901:
682680
return "AppServiceV20170901"
683681
case msiTypeCloudShell:
@@ -699,13 +697,9 @@ func getMSIType() (msiType, string, error) {
699697
}
700698
// if ONLY the env var MSI_ENDPOINT is set the msiType is CloudShell
701699
return msiTypeCloudShell, endpointEnvVar, nil
702-
} else if msiAvailableHook(context.Background(), sender()) {
703-
// if MSI_ENDPOINT is NOT set AND the IMDS endpoint is available the msiType is IMDS. This will timeout after 500 milliseconds
704-
return msiTypeIMDS, msiEndpoint, nil
705-
} else {
706-
// if MSI_ENDPOINT is NOT set and IMDS endpoint is not available Managed Identity is not available
707-
return msiTypeUnavailable, "", errors.New("MSI not available")
708700
}
701+
// if MSI_ENDPOINT is NOT set assume the msiType is IMDS
702+
return msiTypeIMDS, msiEndpoint, nil
709703
}
710704

711705
// GetMSIVMEndpoint gets the MSI endpoint on Virtual Machines.
@@ -1322,15 +1316,13 @@ func NewMultiTenantServicePrincipalTokenFromCertificate(multiTenantCfg MultiTena
13221316
}
13231317

13241318
// MSIAvailable returns true if the MSI endpoint is available for authentication.
1325-
func MSIAvailable(ctx context.Context, sender Sender) bool {
1326-
resp, err := getMSIEndpoint(ctx, sender)
1319+
func MSIAvailable(ctx context.Context, s Sender) bool {
1320+
if s == nil {
1321+
s = sender()
1322+
}
1323+
resp, err := getMSIEndpoint(ctx, s)
13271324
if err == nil {
13281325
resp.Body.Close()
13291326
}
13301327
return err == nil
13311328
}
1332-
1333-
// used for testing purposes
1334-
var msiAvailableHook = func(ctx context.Context, sender Sender) bool {
1335-
return MSIAvailable(ctx, sender)
1336-
}

autorest/adal/token_1.13.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func getMSIEndpoint(ctx context.Context, sender Sender) (*http.Response, error) {
28-
tempCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
28+
tempCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
2929
defer cancel()
3030
// http.NewRequestWithContext() was added in Go 1.13
3131
req, _ := http.NewRequestWithContext(tempCtx, http.MethodGet, msiEndpoint, nil)

autorest/adal/token_legacy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
func getMSIEndpoint(ctx context.Context, sender Sender) (*http.Response, error) {
27-
tempCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
27+
tempCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
2828
defer cancel()
2929
req, _ := http.NewRequest(http.MethodGet, msiEndpoint, nil)
3030
req = req.WithContext(tempCtx)

autorest/adal/token_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ const (
4444
defaultManualFormData = "client_id=id&grant_type=refresh_token&refresh_token=refreshtoken&resource=resource"
4545
)
4646

47-
func init() {
48-
// fake that the IMDS endpoint is available
49-
msiAvailableHook = func(ctx context.Context, sender Sender) bool {
50-
return true
51-
}
52-
}
53-
5447
func TestTokenExpires(t *testing.T) {
5548
tt := time.Now().Add(5 * time.Second)
5649
tk := newTokenExpiresAt(tt)

0 commit comments

Comments
 (0)