Skip to content

Commit fb0bfcd

Browse files
authored
Merge pull request #1082 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 43879bd + 2b3ec21 commit fb0bfcd

6 files changed

Lines changed: 266 additions & 209 deletions

File tree

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,40 @@ function Get-CIPPAuthentication {
2626
Set-Item -Path env:$_ -Value (Get-CippKeyVaultSecret -VaultName $keyvaultname -Name $_ -AsPlainText -ErrorAction Stop) -Force
2727
}
2828
}
29-
# Preload the SAM certificate PFX alongside the other credentials. Non-fatal: the
30-
# certificate does not exist until first bootstrap (setup wizard or weekly timer).
29+
# Set before certificate handling: Update-CIPPSAMCertificate goes through
30+
# Get-GraphToken, which re-enters this function when SetFromProfile is unset
31+
$env:SetFromProfile = $true
32+
33+
# Preload the SAM certificate PFX alongside the other credentials, provisioning it
34+
# when it does not exist yet. Non-fatal: auth must succeed even when certificate
35+
# handling fails; the weekly token update retries provisioning.
3136
try {
3237
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true') {
3338
if ($Secret.SAMCertificate) {
3439
$env:SAMCertificate = $Secret.SAMCertificate
3540
}
3641
} else {
37-
$SAMCertificate = Get-CippKeyVaultSecret -VaultName $keyvaultname -Name 'SAMCertificate' -AsPlainText -ErrorAction Stop
38-
if ($SAMCertificate) {
39-
$env:SAMCertificate = $SAMCertificate
42+
try {
43+
$SAMCertificate = Get-CippKeyVaultSecret -VaultName $keyvaultname -Name 'SAMCertificate' -AsPlainText -ErrorAction Stop
44+
if ($SAMCertificate) {
45+
$env:SAMCertificate = $SAMCertificate
46+
}
47+
} catch {
48+
Write-Information "SAM certificate not found in storage: $($_.Exception.Message)"
4049
}
4150
}
51+
52+
if (-not $env:SAMCertificate) {
53+
# First run on this instance: provision the certificate now.
54+
# Set-CIPPSAMCertificate refreshes $env:SAMCertificate on success.
55+
Write-Information 'No SAM certificate found, provisioning one now'
56+
$CertResult = Update-CIPPSAMCertificate -ErrorAction Stop
57+
Write-LogMessage -message "Provisioned SAM certificate during authentication load. Thumbprint: $($CertResult.Thumbprint), storage mode: $($CertResult.StorageMode)" -Sev 'Info' -API 'CIPP Authentication'
58+
}
4259
} catch {
43-
Write-Information "SAM certificate not preloaded (not created yet?): $($_.Exception.Message)"
60+
Write-LogMessage -message 'Could not preload or provision the SAM certificate. It will be retried by the weekly token update.' -Sev 'Warning' -API 'CIPP Authentication' -LogData (Get-CippException -Exception $_)
4461
}
4562

46-
$env:SetFromProfile = $true
4763
Write-LogMessage -message 'Reloaded authentication data from KeyVault' -Sev 'debug' -API 'CIPP Authentication'
4864

4965
return $true

0 commit comments

Comments
 (0)