diff --git a/apps/managedidentity/managedidentity.go b/apps/managedidentity/managedidentity.go index ad2e81ca..44076fc2 100644 --- a/apps/managedidentity/managedidentity.go +++ b/apps/managedidentity/managedidentity.go @@ -203,7 +203,7 @@ func GetSource(id ID) (Source, error) { return AppService, nil } else if msiEndpoint != "" { return CloudShell, nil - } else if validateAzureArcEnvironment(identityEndpoint, imdsEndpoint, runtime.GOOS) { + } else if isAzureArcEnvironment(identityEndpoint, imdsEndpoint, runtime.GOOS) { return AzureArc, nil } @@ -342,7 +342,7 @@ func createAzureArcAuthRequest(ctx context.Context, id ID, resource string) (*ht var msiEndpoint *url.URL if _, ok := id.(systemAssignedValue); !ok { - return nil, fmt.Errorf("Azure Arc doesn't support user assigned managed identities") + return nil, fmt.Errorf("azure Arc doesn't support user assigned managed identities") } msiEndpoint, parseErr := url.Parse(identityEndpoint) @@ -364,7 +364,7 @@ func createAzureArcAuthRequest(ctx context.Context, id ID, resource string) (*ht return req, nil } -func validateAzureArcEnvironment(identityEndpoint, imdsEndpoint string, platform string) bool { +func isAzureArcEnvironment(identityEndpoint, imdsEndpoint string, platform string) bool { if identityEndpoint != "" && imdsEndpoint != "" { return true } @@ -372,7 +372,7 @@ func validateAzureArcEnvironment(identityEndpoint, imdsEndpoint string, platform himdsFilePath := getAzureArcFilePath(platform) if himdsFilePath != "" { - if _, err := os.Stat(himdsFilePath); !os.IsNotExist(err) { + if _, err := os.Stat(himdsFilePath); err == nil { return true } } diff --git a/apps/managedidentity/managedidentity_test.go b/apps/managedidentity/managedidentity_test.go index c5bd4800..1a8bd165 100644 --- a/apps/managedidentity/managedidentity_test.go +++ b/apps/managedidentity/managedidentity_test.go @@ -217,20 +217,23 @@ func Test_AcquireToken_Returns_Token_Success(t *testing.T) { if err != nil { t.Fatalf("error while forming json response : %s", err.Error()) } + mockClient.AppendResponse(mock.WithHTTPStatusCode(http.StatusOK), mock.WithBody(responseBody), mock.WithCallback(func(r *http.Request) { localUrl = r.URL })) - client, err := New(testCase.miType, WithHTTPClient(&mockClient)) + client, err := New(testCase.miType, WithHTTPClient(&mockClient)) if err != nil { t.Fatal(err) } + result, err := client.AcquireToken(context.Background(), testCase.resource) if err != nil { - if testCase.source == AzureArc && err.Error() == "Azure Arc doesn't support user assigned managed identities" { + if testCase.source == AzureArc && err.Error() == "azure Arc doesn't support user assigned managed identities" { return } } + if !strings.HasPrefix(localUrl.String(), testCase.endpoint) { t.Fatalf("url request is not on %s got %s", testCase.endpoint, localUrl) } @@ -444,7 +447,7 @@ func Test_validateAzureArcEnvironment(t *testing.T) { defer restoreFunc() } - result := validateAzureArcEnvironment(tc.identityEndpoint, tc.imdsEndpoint, tc.platform) + result := isAzureArcEnvironment(tc.identityEndpoint, tc.imdsEndpoint, tc.platform) if result != tc.expectedResult { t.Fatalf("expected %v, got %v", tc.expectedResult, result) }