To secure an application or an API, it is often necessary to define roles that client applications must have permissions for.
In Entra ID, this involves:
Setting up these elements can become time-consuming depending on the number of applications/APIs or clients. This article demonstrates how to address this issue by automating the creation of these components using Azure CLI.
Here, we first check if the Server App Registration already exists; if not, we create it and retrieve its application ID (aka client ID).
Next, we check if the desired role already exists; if not, it is created within the Server App Registration.
Next, the Client App Registration is created if it does not already exist:
To use the Client App Registration, a valid client secret is required:
Here, the secret is stored in an Azure Key Vault for safekeeping or for client access, though this step is optional.
Finally, the Client App Registration is granted permissions on the Server App Registration role:
Adding the permission alone is insufficient; an Entra ID admin must grant the permission to make it effective.
If you already have the necessary rights, you can simply run the following command:
By aggregating these Azure CLI commands with slight customization, you can provision App Registrations on the fly as needed. These commands can be executed during the deployment of an application or API, or as part of a workflow to onboard new clients. In many contexts, this can save a lot of manual configurations !