Skip to content

Commit ba52db5

Browse files
Backup-DbaDbCertificate - Handle DMK-encrypted certs when DecryptionPassword is also provided
Restores dbatoolsci_AGCert to DMK encryption (required for AG endpoint auth). Fixes Backup-DbaDbCertificate to check PrivateKeyEncryptionType: when a cert's private key is encrypted by the database master key, skip DecryptionPassword and use the 3-param export overload instead of the 4-param one that SQL Server rejects. Updates the "all instance" backup test to use containment checks instead of an exact count, since the DMK cert will now be backed up successfully. Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent e04c176 commit ba52db5

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

public/Backup-DbaDbCertificate.ps1

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,24 @@ function Backup-DbaDbCertificate {
225225

226226
# because the password shouldn't go to memory...
227227
if ($EncryptionPassword.Length -gt 0 -and $DecryptionPassword.Length -gt 0) {
228-
229-
Write-Message -Level Verbose -Message "Both passwords passed in. Will export both cer and pvk."
230-
231-
$cert.export(
232-
$exportPathCert,
233-
$exportPathKey,
234-
($EncryptionPassword | ConvertFrom-SecurePass),
235-
($DecryptionPassword | ConvertFrom-SecurePass)
236-
)
228+
if ($cert.PrivateKeyEncryptionType -eq [Microsoft.SqlServer.Management.Smo.PrivateKeyEncryptionType]::MasterKey) {
229+
Write-Message -Level Verbose -Message "Both passwords passed in but private key of $certName is encrypted by the database master key. DecryptionPassword will be ignored."
230+
231+
$cert.export(
232+
$exportPathCert,
233+
$exportPathKey,
234+
($EncryptionPassword | ConvertFrom-SecurePass)
235+
)
236+
} else {
237+
Write-Message -Level Verbose -Message "Both passwords passed in. Will export both cer and pvk."
238+
239+
$cert.export(
240+
$exportPathCert,
241+
$exportPathKey,
242+
($EncryptionPassword | ConvertFrom-SecurePass),
243+
($DecryptionPassword | ConvertFrom-SecurePass)
244+
)
245+
}
237246
} elseif ($EncryptionPassword.Length -gt 0 -and $DecryptionPassword.Length -eq 0) {
238247
Write-Message -Level Verbose -Message "Only encryption password passed in. Will export both cer and pvk."
239248

tests/Backup-DbaDbCertificate.Tests.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ Describe $CommandName -Tag IntegrationTests {
149149
}
150150
$results = Backup-DbaDbCertificate @splatBackupAllCerts
151151

152-
$results | Should -HaveCount 3
153-
$results.Certificate | Should -Be $cert1.Name, $cert2.Name, $cert3.Name
152+
$results.Certificate | Should -Contain $cert1.Name
153+
$results.Certificate | Should -Contain $cert2.Name
154+
$results.Certificate | Should -Contain $cert3.Name
154155
}
155156
}
156157
}

tests/appveyor.SQL2019.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $null = Set-DbaSpConfigure -SqlInstance $sqlinstance -Name ExtensibleKeyManageme
1919
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CRYPTOGRAPHIC PROVIDER dbatoolsci_AKV FROM FILE = 'C:\github\appveyor-lab\keytests\ekm\Microsoft.AzureKeyVaultService.EKM.dll'" -EnableException
2020
$null = Enable-DbaAgHadr -SqlInstance $sqlinstance -Force -EnableException -Confirm:$false
2121
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<StrongPassword>'" -EnableException
22-
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CERTIFICATE dbatoolsci_AGCert ENCRYPTION BY PASSWORD = '<StrongPassword>' WITH SUBJECT = 'AG Certificate'" -EnableException
22+
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CERTIFICATE dbatoolsci_AGCert WITH SUBJECT = 'AG Certificate'" -EnableException
2323

2424
$loginName = "$env:COMPUTERNAME\$env:USERNAME"
2525
$login = Get-DbaLogin -SqlInstance $sqlinstance -Login $loginName

tests/appveyor.SQL2022.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $null = Set-DbaSpConfigure -SqlInstance $sqlinstance -Name ExtensibleKeyManageme
1919
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CRYPTOGRAPHIC PROVIDER dbatoolsci_AKV FROM FILE = 'C:\github\appveyor-lab\keytests\ekm\Microsoft.AzureKeyVaultService.EKM.dll'" -EnableException
2020
$null = Enable-DbaAgHadr -SqlInstance $sqlinstance -Force -EnableException -Confirm:$false
2121
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<StrongPassword>'" -EnableException
22-
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CERTIFICATE dbatoolsci_AGCert ENCRYPTION BY PASSWORD = '<StrongPassword>' WITH SUBJECT = 'AG Certificate'" -EnableException
22+
Invoke-DbaQuery -SqlInstance $sqlinstance -Query "CREATE CERTIFICATE dbatoolsci_AGCert WITH SUBJECT = 'AG Certificate'" -EnableException
2323

2424
$loginName = "$env:COMPUTERNAME\$env:USERNAME"
2525
$login = Get-DbaLogin -SqlInstance $sqlinstance -Login $loginName

0 commit comments

Comments
 (0)