Skip to content

Commit ea0c6dd

Browse files
authored
Update Rule “expiring-app-secrets-certificates/rule” (#10017)
* Update Rule “expiring-app-secrets-certificates/rule” * Update Rule “expiring-app-secrets-certificates/rule” * Update Rule “expiring-app-secrets-certificates/rule” * Update rules-to-better-system-administrators.md
1 parent e194464 commit ea0c6dd

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

categories/infrastructure-and-networking/rules-to-better-system-administrators.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ index:
2424
- use-the-distributed-file-system-for-your-file-shares
2525
- how-to-manage-certificates
2626
- do-you-use-free-or-paid-ssl-certificates
27+
- expiring-app-secrets-certificates
2728
- secure-your-wireless-connection
2829
- easy-wifi-access
2930
- keep-your-file-servers-clean

rules/expiring-app-secrets-certificates/rule.md

+58-10
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,86 @@
11
---
2-
seoDescription: Keep track of expiring app registration secrets and certificates in Azure AD to avoid authentication issues.
32
type: rule
43
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.
56
uri: expiring-app-secrets-certificates
67
authors:
78
- title: Chris Schultz
89
url: https://ssw.com.au/people/chris-schultz
10+
- title: Brady Stroud
11+
url: https://ssw.com.au/people/brady-stroud/
912
created: 2023-05-12T00:55:28.532Z
1013
guid: 429dbbef-ea36-4fc6-b358-924330966b4a
1114
---
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.
1416

1517
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.
1618

1719
<!--endintro-->
1820

1921
### Use a PowerShell script to check expiry dates
2022

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+
}
2259
23-
`Get-AzureADApplication`
60+
```
2461

25-
`Get-AzureADApplicationPasswordCredential`
62+
Automate Expiry Notifications
2663

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:
2865

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+
}
3077
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+
```
3280

3381
:::greybox
3482

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.
3684

3785
:::
3886

0 commit comments

Comments
 (0)