Skip to content

Fix incorrect expiry check in ConfigureCrmServerSideSync.ps1 - #816

Open
Anwarul Haq (Anwarluck) wants to merge 1 commit into
microsoft:masterfrom
Anwarluck:fix/servicesideprincipal-cert-expiry-comparison
Open

Fix incorrect expiry check in ConfigureCrmServerSideSync.ps1#816
Anwarul Haq (Anwarluck) wants to merge 1 commit into
microsoft:masterfrom
Anwarluck:fix/servicesideprincipal-cert-expiry-comparison

Conversation

@Anwarluck

Copy link
Copy Markdown

Fixes #797

endDateTime on the service principal credential comes back from the Graph API response as a string (e.g. 2024-05-01T00:00:00Z), but $currentDateTime is a DateTime object from Get-Date. When you compare a string to a DateTime with -lt, PowerShell converts the right-hand side to match the type of the left-hand side, so this ends up doing a lexicographic string comparison instead of an actual date comparison. Depending on the string formats involved, that can miss certificates that are genuinely expired, or flag ones that aren't.

The fix casts endDateTime to [DateTime] before the comparison, same as $certificateInfo.NotAfter a few lines above it, which is already a proper DateTime and compares correctly.

Left the Write-Output line below it using the raw string value on purpose — that's just for the log message and reads better as the original ISO timestamp.

endDateTime comes back from the Graph API response as a string, while
currentDateTime is a DateTime object. Comparing them with -lt coerces
the DateTime to a string and does a lexicographic comparison instead
of an actual date comparison, so expired certificates can be missed
(or valid ones flagged as expired) depending on how the two string
representations happen to sort.

Cast endDateTime to DateTime before comparing.

Fixes microsoft#797
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConfigureCrmServerSideSync - Date comparison for checking for expired certs is not correct

1 participant