This repo demonstrates how to set up automatic Azure AD app registration client secret rotation using Azure Functions (in Java) and Key Vault (with Event Grid notification when secrets are about to expire).
- Key Vault is configured to send an Event Grid notification when a secret is about to expire (in 30 days by default) when a secret is created.
- Azure Function is triggered by the Event Grid notification.
- Azure Function creates a new client secret for the Azure AD app registration.
- Azure Function updates the secret in Key Vault.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Azure CLI
- Azure Bicep
- Java 11
- Azure subscription & resource group
-
Modify the
./infra/env/dev.parameters.json
file to match your environment. -
Run the following command to deploy the initial infrastructure to Azure.
az deployment group create -g rg-keyVaultJava-ussc-dev --template-file ./infra/init/main.bicep --parameters ./infra/env/dev.parameters.json
-
Update the
./src/java/pom.xml
file to match your environment (specifically thefunctionAppName
,resourceGroup
,appServicePlanName
andregion
keys) -
Build & deploy the Azure Function Java code.
cd src/java
mvn clean package
mvn azure-functions:deploy
- Deploy the Event Grid subscription now that an endpoint exists in Azure Functions.
cd ../..
az deployment group create -g rg-keyVaultJava-ussc-dev --template-file ./infra/subscription/main.bicep --parameters ./infra/env/dev.parameters.json
-
Create a test App Registration to be managed by the Azure Function in Azure Active Directory. Take note of the
objectId
of the App Registration. -
Retrieve the Object Id of the Managed Identity.
az identity show -g rg-keyvaultJava-ussc-dev -n mi-keyVaultJava-ussc-dev --query principalId
- Run the following command to assign the Managed Identity ownership over a test app registration (the id is the objectId of the app registration, the owner-object-id is the objectId of the Managed Identity).
az ad app owner add --id a9425fd4-66c5-43a2-afb2-9135ec474e4a --owner-object-id 51819bcc-7865-4f6d-9f50-48ffbafe79a2
- Run the following command to assign the Managed Identity the
Application.ReadWrite.OwnedBy
permission on the Graph API so it can update the client secrets on any app registration it owns (the spId is the objectId of the Managed Identity). NOTE: It is likely you will need an admin to do this for you.
spId=51819bcc-7865-4f6d-9f50-48ffbafe79a2
graphResourceId=$(az ad sp list --display-name "Microsoft Graph" --query [0].id --out tsv)
appRoleId=$(az ad sp list --display-name "Microsoft Graph" --query "[0].appRoles[?value=='Application.ReadWrite.OwnedBy' && contains(allowedMemberTypes, 'Application')].id" --output tsv)
uri=https://graph.microsoft.com/v1.0/servicePrincipals/$spId/appRoleAssignments
body="{'principalId':'$spId','resourceId':'$graphResourceId','appRoleId':'$appRoleId'}"
az rest --method post --uri $uri --body $body --headers "Content-Type=application/json"
-
Navigate to the test App Registration in the Azure portal. Copy the objectId of the App Registration.
-
Click on Certificates & secrets.
-
Click on New client secret.
-
Enter a description and click Add.
-
Copy the id & value of the secret.
-
Navigate to the Key Vault in the Azure portal.
-
Click on Secrets
-
Click on Generate/Import
-
Set the name of the secret to the objectId of the App Registration.
-
Set the value of the secret to the value of the secret.
-
Set the Content Type to the id of the secret (not of the App Registration, but of the secret itself).
-
Set the Expiration date to a date in the near future (less than 30 days from now).
-
Click Create.
-
Wait a few minutes for Key Vault to send the notification to the Azure Function.
-
Navigate back to the App Registration in the Azure portal.
-
Click on Certificates & secrets.
-
Notice that the secret has been replaced by a new one. Note the first 3 characters of the Value and the Expires value.
-
Navigate back to the Key Vault in the Azure portal.
-
Click on Secrets
-
Click on the secret.
-
Notice that a new secret version has been created. If you open it, you will see the new secret value and expiration date 1 year in the future.