Skip to content

Commit 0c486b9

Browse files
Backup-DbaDbCertificate: Don't use decryption password if certificate is encrypted by the database master key (#10329)
1 parent 9899bd2 commit 0c486b9

2 files changed

Lines changed: 21 additions & 11 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
}

0 commit comments

Comments
 (0)