Skip to content

[Bug] ManagedIdentity broken in 1.18.0 and up #915

Closed
@lilgreenbird

Description

@lilgreenbird

Library version used

1.18.0, 1.19.0

Java version

23

Scenario

ManagedIdentityClient - managed identity

Is this a new or an existing app?

None

Issue description and reproduction steps

We (Microsoft JDBC Driver for SQL Server) are trying to update to the latest msal4j version that fixed the vulnerability in earlier versions however Managed Identity is broken in this version.

The following repro works for 1.17.3 and older versions but throws error ""Managed Identity authentication is not available." for 1.18.0 and 1.19.0

Relevant code snippets

package org.example;

import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.microsoft.aad.msal4j.*;
import java.net.URI;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import java.util.Arrays;

public class Main {
	// get token using azure-identity
    public static String getManagedIdentityToken1(String scope, String clientId) {
            ManagedIdentityCredential mic = new ManagedIdentityCredentialBuilder()
                    .clientId(clientId) // only required for user-assigned
                    .build();
            TokenRequestContext tokenRequestContext = new TokenRequestContext();
            tokenRequestContext.setScopes(Arrays.asList(scope+"/.default"));

            AccessToken accessTokenOptional = mic.getToken(tokenRequestContext).block();
            return accessTokenOptional.getToken();
    }

	// get token using msal
    public static String getManagedIdentityToken2(String scope, String clientId) throws Exception {
        ManagedIdentityApplication miApp = ManagedIdentityApplication
                .builder(ManagedIdentityId.userAssignedClientId(clientId))
                .build();

        ManagedIdentityParameters parameters = ManagedIdentityParameters.builder(scope).build();

        IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
                ManagedIdentityParameters.builder(scope)
                        .build()).get();

        return result.accessToken();

    }

    public static void main(String[] args) {

        try {
            System.out.println("Starting...");

            String scope = "https://database.windows.net";
            String clientId = "a0bc0c34-a024-4037-9368-9f904a008c36";
            System.out.println("Access Token1: " + getManagedIdentityToken1(scope, clientId));
            System.out.println("Access Token2: " + getManagedIdentityToken2(scope, clientId));

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

Expected behavior

No response

Identity provider

Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)

Regression

1.17.3

Solution and workarounds

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working, needs an investigation and a fixRequires more infoMore information is needed, from either the person who opened the issue or another team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions