Skip to content

Commit 5ae4171

Browse files
committed
fix: prevent cert provision loop and skip KV 404 retries
Add SAMCertProvisionAttempted guard to break recursion when Update-CIPPSAMCertificate calls Get-GraphToken, which re-enters Get-CIPPAuthentication before the cert env var is set. Also short-circuit Key Vault retry loop on 404 responses since the secret is definitively absent and retrying with backoff only adds latency.
1 parent fb8f193 commit 5ae4171

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ function Get-CIPPAuthentication {
4949
}
5050
}
5151

52-
if (-not $env:SAMCertificate) {
53-
# First run on this instance: provision the certificate now.
52+
if (-not $env:SAMCertificate -and $env:SAMCertProvisionAttempted -ne 'true') {
53+
# First run on this instance: provision the certificate now, at most once per
54+
# process. The guard also breaks a recursion loop: Update-CIPPSAMCertificate
55+
# calls Get-GraphToken, which re-enters this function when the AppCache
56+
# ApplicationId does not match the environment.
5457
# Set-CIPPSAMCertificate refreshes $env:SAMCertificate on success.
58+
$env:SAMCertProvisionAttempted = 'true'
5559
Write-Information 'No SAM certificate found, provisioning one now'
5660
$CertResult = Update-CIPPSAMCertificate -ErrorAction Stop
5761
Write-LogMessage -message "Provisioned SAM certificate during authentication load. Thumbprint: $($CertResult.Thumbprint), storage mode: $($CertResult.StorageMode)" -Sev 'Info' -API 'CIPP Authentication'

Modules/CIPPCore/Public/Get-CippKeyVaultSecret.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ function Get-CippKeyVaultSecret {
6060
break
6161
} catch {
6262
$lastError = $_
63+
# 404 is definitive - the secret does not exist and retrying cannot change that
64+
if ($_.Exception.Message -match '404|SecretNotFound') {
65+
throw "Failed to retrieve secret '$Name' from vault '$VaultName': $($_.Exception.Message)"
66+
}
6367
if ($i -lt ($maxRetries - 1)) {
6468
Start-Sleep -Seconds $retryDelay
6569
$retryDelay *= 2 # Exponential backoff

0 commit comments

Comments
 (0)