|
1 | 1 | ---
|
2 |
| -seoDescription: Keep track of expiring app registration secrets and certificates in Azure AD to avoid authentication issues. |
3 | 2 | type: rule
|
4 | 3 | title: Do you keep track of expiring app registration secrets and certificates?
|
| 4 | +seoDescription: Keep track of expiring app registration secrets and certificates |
| 5 | + in Azure AD to avoid authentication issues. |
5 | 6 | uri: expiring-app-secrets-certificates
|
6 | 7 | authors:
|
7 | 8 | - title: Chris Schultz
|
8 | 9 | url: https://ssw.com.au/people/chris-schultz
|
| 10 | + - title: Brady Stroud |
| 11 | + url: https://ssw.com.au/people/brady-stroud/ |
9 | 12 | created: 2023-05-12T00:55:28.532Z
|
10 | 13 | guid: 429dbbef-ea36-4fc6-b358-924330966b4a
|
11 | 14 | ---
|
12 |
| - |
13 |
| -In Azure AD, App Registrations are used to establish a trust relationship between your app and the Microsoft identity platform. This allows you to give your app access to various resources, such as Graph API. |
| 15 | +In Entra ID (formerly Azure AD), App Registrations are used to establish a trust relationship between your app and the Microsoft identity platform. This allows you to give your app access to various resources, such as Graph API. |
14 | 16 |
|
15 | 17 | App Registrations use secrets or certificates for authentication. It is important to keep track of the expiry date of these authentication methods, so you can update them before things break.
|
16 | 18 |
|
17 | 19 | <!--endintro-->
|
18 | 20 |
|
19 | 21 | ### Use a PowerShell script to check expiry dates
|
20 | 22 |
|
21 |
| -An easy way to do this is to run a PowerShell script that checks the expiry date of all app registration secrets or certificates. This requires the AzureAD module; the cmdlets used are: |
| 23 | +An easy way to do this is to run a PowerShell script that checks the expiry date of all app registration secrets or certificates. This requires the Microsoft Graph PowerShell module, as the older AzureAD module is deprecated. The key cmdlets used are: |
| 24 | + |
| 25 | +Get-MgApplication |
| 26 | +Get-MgApplicationPassword |
| 27 | +Get-MgApplicationKeyCredential |
| 28 | +Here’s an updated script using the Microsoft Graph module: |
| 29 | + |
| 30 | +``` |
| 31 | +# Install Microsoft Graph module (if not installed) |
| 32 | +if (-not (Get-Module -ListAvailable -Name Microsoft.Graph)) { |
| 33 | + Install-Module Microsoft.Graph -Scope CurrentUser -Force |
| 34 | +} |
| 35 | +
|
| 36 | +# Connect to Microsoft Graph with appropriate permissions |
| 37 | +Connect-MgGraph -Scopes "Application.Read.All" |
| 38 | +
|
| 39 | +# Get all applications |
| 40 | +$apps = Get-MgApplication -All |
| 41 | +
|
| 42 | +# Check expiry dates of secrets and certificates |
| 43 | +foreach ($app in $apps) { |
| 44 | + $secrets = Get-MgApplicationPassword -ApplicationId $app.Id |
| 45 | + $certs = Get-MgApplicationKeyCredential -ApplicationId $app.Id |
| 46 | +
|
| 47 | + foreach ($secret in $secrets) { |
| 48 | + if ($secret.EndDateTime -lt (Get-Date).AddDays(30)) { |
| 49 | + Write-Host "Secret for App '$($app.DisplayName)' expires on $($secret.EndDateTime)" |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + foreach ($cert in $certs) { |
| 54 | + if ($cert.EndDateTime -lt (Get-Date).AddDays(30)) { |
| 55 | + Write-Host "Certificate for App '$($app.DisplayName)' expires on $($cert.EndDateTime)" |
| 56 | + } |
| 57 | + } |
| 58 | +} |
22 | 59 |
|
23 |
| -`Get-AzureADApplication` |
| 60 | +``` |
24 | 61 |
|
25 |
| -`Get-AzureADApplicationPasswordCredential` |
| 62 | +Automate Expiry Notifications |
26 | 63 |
|
27 |
| -`Get-AzureADApplicationKeyCredential` |
| 64 | +To receive alerts, you can modify the script to send email notifications using Microsoft Graph API (Send-MgUserMail) instead of Send-MailMessage (which is deprecated). For example: |
28 | 65 |
|
29 |
| -There's an example of a working script here: https://github.com/demiliani/PowershellCloudScripts/blob/master/AzureADCheckSecretsToExpire.ps1 |
| 66 | +``` |
| 67 | +$body = @{ |
| 68 | + Message = @{ |
| 69 | + Subject = "Expiring App Secrets" |
| 70 | + Body = @{ |
| 71 | + ContentType = "Text" |
| 72 | + Content = "The following app secrets are expiring soon..." |
| 73 | + } |
| 74 | + ToRecipients = @(@{ EmailAddress = @{ Address = "[email protected]" } }) |
| 75 | + } |
| 76 | +} |
30 | 77 |
|
31 |
| -To extend the example above, you can run the script on a schedule using Task Scheduler or an Azure Automation Runbook, and send an email with [Send-MailMessage](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.3). |
| 78 | +Send-MgUserMail -UserId "[email protected]" -Message $body |
| 79 | +``` |
32 | 80 |
|
33 | 81 | :::greybox
|
34 | 82 |
|
35 |
| -Note: To run this on a schedule, you should create an app registration to authenticate the script. The app registration will need the role **Cloud Application Administrator**. |
| 83 | +Note: To run this on a schedule, you should create an app registration to authenticate the script. The app registration will at least need **Application.Read.All** rights to be able to run this. |
36 | 84 |
|
37 | 85 | :::
|
38 | 86 |
|
|
0 commit comments