Get list of subscriptions using clientSecretCredential method

Nikunj Badjatya 81 Reputation points
2022-03-13T00:37:10.417+00:00

Hello,

We want to find tags for all azure subscriptions in a azure tenant.

Let's say there are N subscriptions (subscription1, subscription2, ...) in an Azure Tenant T1.
Each of these subscriptions has some tags created at the subscription level.

Now in another tenant T2, an azure multitenant app (app1) is created. T2 is the home tenant for this app1 and has got clientid and clientsecret.
app1 is registered in T1 as a serviceprincipal.
app1 is assigned necessary permissions at each of the subscriptions in T1 to be able to get tags at resource level.

Now using java sdk ClientSecretCredential method, we are able to authenticate per subscription and retrieve the tags available at subscription level.
Sample code:

            ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                    .clientId(CLIENT_ID)
                    .clientSecret(CLIENT_SECRET)
                    .tenantId(TENANT_ID_1)
                    .build();
            AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);            

            AzureResourceManager azure = AzureResourceManager
                    .authenticate(clientSecretCredential, profile)
                    .withSubscription(SUBSCRIPTION_ID_1);

            TagsResourceInner tagsResourceInner2 = azure.genericResources().manager().
                                                    serviceClient().getTagOperations().
                                                    getAtScope("/subscriptions/<SUBSCRIPTION_ID_1>/");
            System.out.println(tagsResourceInner2.properties().tags());

My question is, if there are N subscriptions in T1, and the subs list can change over time, how to programmatically using java sdk get list of subscriptions ? So that we can fetch tags for all.

Azure Managed Applications
Azure Managed Applications
An Azure service that enables managed service providers, independent software vendors, and enterprise IT teams to deliver turnkey solutions through the Azure Marketplace or service catalog.
115 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,919 questions
{count} votes

Accepted answer
  1. James Hamil 22,436 Reputation points Microsoft Employee
    2022-03-18T21:25:45.787+00:00

    Hi @ NikunjBadjatya-8851 , sorry for the delay in response. I was awaiting a response from the product team.

    You can use: azure.subscriptions().list() as long as app1 has permission to all required subscriptions. subscription ID is Subscription.subscriptionId()

    Please let me know if you have any questions and I can help you further.

    If this answer helped you please mark it as "Verified" so other users can reference it.

    Thank you,
    James

    0 comments No comments

0 additional answers

Sort by: Most helpful