Automated Azure AD Credential Expiration Alerts

Automated Azure AD Credential Expiration Alerts

All secrets and certificates created within Azure App Registrations have expiration dates, making it crucial for administrators to stay informed to take timely action. Microsoft does not provide an automated notification for this. However, by leveraging Microsoft Graph API and Power Automate, you can set up automated alerts to notify when secrets and certificates are nearing expiration—or have already expired.

Below is an example e-mail of expiring and expired secrets and certificates generated by Power Automate:

Azure AD Secrets and Certificates Report email with tables of credentials expiring in under 14 days and already expired, and a CSV attached
The end-result in e-mail showing expiring secrets, expired secrets, as well as an a csv export of all secrets.

Requirements

You’ll need an app registration that at a minimum has Application.Read.All Permissions. I’ve covered how to create an app registration in this prior blog post, so it’s worth reading if you’re not familiar with the basics.

Microsoft Entra app registration API permissions with the Microsoft Graph Application.Read.All permission granted admin consent

Process Flow Diagram

At a high-level, here’s is the process of the Power Automate:

Flowchart of the flow: scheduled trigger, access token, a Do until loop over applications, secrets and certificates, then filter, format and email

Get the Code

You can download the completed Power Automate from my GitHub. Be sure to review the ReadMe file for the installation instructions!

Detailed Explanation of the Flow

Call to Graph API

After the access token is acquired, the /applications endpoint is called. I select only the columns needed to improve query performance:

https://graph.microsoft.com/v1.0/applications?$select=appId,id,createdDateTime,passwordCredentials,displayName
Power Automate varNextLink variable and a Do until loop with an HTTP action calling the Microsoft Graph applications endpoint

The HTTP Request is inside a Do Until loop since the API call will paginate 100 records at a time by default.

Process the Secrets and Certificates

With the body value response data, I filter on the passwordcredentials array where it is not empty. We only want to get those App Registrations that have secrets. Then, time to loop through each application and it’s secrets with a nested apply to each.

Filter array action keeping only applications that have password credentials

We also make another call to the Graph and fetch the Application’s owner(s). It will return as an array since there can be more than one. I join the owner’s email addresses together with a ‘;’ and append them to the output later. For the secrets, importantly, we need to calculate the date difference from the endDateTime of the secret to now.Using dateDifference with split (to get characters before the “.”) returns the difference in days.

The days to expire is a custom property derived from the datediffence formula in the prior step. However, we take it a step further to check if the result is 0 or contains a “:” as will be the case if a time value is present after the prior split function. If that happens, then it’s 0 days, else it’s the split results from the prior step. The result is casted as a float value since we can have negative values (meaning the secret has already expired!) Finally, append the outputs to the application details array variable.

Apply to each Application loop that gets owners, joins their emails and loops over each secret to calculate days until expiration
split(dateDifference(utcNow(),item()?['endDateTime']),'.')
float(if(equals(length(outputs('Date_Diff_Secret')), 0), 0, if(contains(outputs('Date_Diff_Secret')[0], ':'), 0, outputs('Date_Diff_Secret')[0])))
Append to array variable action recording a secret's type, app ID, name, start and expiration dates, days until expiration and owner emails

The same logic above is applied to retrieve the certificates.

For the last action in the do until, don’t forget to set the Next Link value. When this value is empty, it’ll break out of the loop.

Set variable action storing the @odata.nextlink value in varNextLink to page through results

Filter and Email

At this point, I want to filter the results by filtering the expired and expiring secrets in less than x number days as defined in the “Days Until Expire” variable. We construct two html tables from the results set, including a deep link to the Application in Azure, and print the outputs in the email.

Filter array actions for not expired and expiring in x days, and a Select action building linked display names and colored days until expire

And the email the admin.

Send an email (V2) action with an HTML body listing expiring and expired secrets and certificates