Skip to content

Commit

Permalink
pr comments resolved, remove fileExist, rename validate azure arc
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOHart committed Oct 18, 2024
1 parent 78d6cd0 commit eff9860
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions apps/managedidentity/managedidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -364,15 +364,15 @@ 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
}

himdsFilePath := getAzureArcFilePath(platform)

if himdsFilePath != "" {
if _, err := os.Stat(himdsFilePath); !os.IsNotExist(err) {
if _, err := os.Stat(himdsFilePath); err == nil {
return true
}
}
Expand Down
9 changes: 6 additions & 3 deletions apps/managedidentity/managedidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit eff9860

Please sign in to comment.