Refactor Set-DbaNetworkCertificate#10232
Conversation
|
@claude please perform a thorough code review. |
|
Claude finished @andreasjordan's task in 2m 55s —— View job Code Review: Refactor Set-DbaNetworkCertificateTasks:
This is a solid refactor overall. The new approach—delegating to Critical / Bugs1.
The parameter variable
Because these mutations persist across loop iterations, when processing multiple instances: # No -Thumbprint specified, no -UnsetCertificate
Set-DbaNetworkCertificate -SqlInstance instance1, instance2
Fix: use a local variable inside the loop for the effective thumbprint, and do not mutate the parameter: foreach ($instance in $SqlInstance) {
$effectiveThumbprint = $Thumbprint # local copy per-iteration
...
if ($UnsetCertificate) {
$effectiveThumbprint = $null
} elseif ($effectiveThumbprint) {
...
} else {
$effectiveThumbprint = $certTest.SuitableCertificates.Thumbprint
}
# Use $effectiveThumbprint from here on
}2. Tests capture warnings incorrectly —
Multiple
The fix is to add $result = Set-DbaNetworkCertificate -SqlInstance $TestConfig.InstanceRestart -RestartService `
-WarningAction SilentlyContinue -WarningVariable WarnVarModerate3. Error message doesn't distinguish set vs. unset on failure
Stop-Function -Message "Failed to configure certificate $Thumbprint for instance $instance." ...When
The message should branch based on if ($UnsetCertificate) {
Stop-Function -Message "Failed to unset certificate for instance $instance." ...
} else {
Stop-Function -Message "Failed to configure certificate $Thumbprint for instance $instance." ...
}4. No validation when
If a user passes if ($UnsetCertificate -and ($Thumbprint -or $Certificate)) {
Stop-Function -Message "-UnsetCertificate cannot be used with -Thumbprint or -Certificate."
return
}Minor5. Typo: "Failes" appears twice in the description
Should be 6.
7. Original author removed entirely
The original author (Chrissy LeMaire) was replaced rather than credited alongside the refactor. The dbatools convention for significant rewrites is typically to keep the original credit and add the refactor author: 8. Inconsistency between
Positive Highlights
| Branch |
|
@claude based on your feedback I made some changes. Please:
|
|
Claude finished @andreasjordan's task in 2m 47s —— View job Claude finished @andreasjordan's task —— Follow-up Review: Refactor Set-DbaNetworkCertificateTasks:
On the style.md ChangesYes, the addition is genuinely helpful. Knowing that One small nit: the example sentence at the end of the new section reads:
The Status of Previous Feedback
Critical Bug (Regression from Fix #1)
$result = Invoke-Command2 -ScriptBlock $scriptBlock -ArgumentList $instance, $Thumbprint ...The fix for item #1 correctly introduced
The result: auto-select silently removes any configured certificate instead of setting the new one. This is the worst-case scenario — it appears to succeed (no error), returns a Fix: $result = Invoke-Command2 -ScriptBlock $scriptBlock -ArgumentList $instance, $newThumbprint -ComputerName $($certTest.ComputerName) -Credential $Credential -ErrorAction StopMinor IssuesWarning message wording is misleading during unset
Write-Message -Level Warning -Message "New certificate will not take effect until SQL Server services are restarted for $instance"This message fires in all non-restart paths, including unset. When removing a certificate, saying "New certificate will not take effect" is inaccurate — there is no new certificate. A simple fix: use a more generic phrasing for both paths, or branch on if ($UnsetCertificate) {
Write-Message -Level Warning -Message "Certificate removal will not take effect until SQL Server services are restarted for $instance"
} else {
Write-Message -Level Warning -Message "New certificate will not take effect until SQL Server services are restarted for $instance"
}Similarly the Test
$WarnVar | Should -BeNullOrEmptySince Positive HighlightsAll significant feedback from the first pass has been addressed correctly, and the structure is much cleaner now. The | Branch |
|
@claude I added more changes, so please look at the code again. Any open issues? |
|
Claude finished @andreasjordan's task in 2m 14s —— View job Third-Pass Review: Refactor Set-DbaNetworkCertificateTasks:
Status of Previous Issues
The critical regression is fully resolved. The code is in very good shape now. One Remaining BugString interpolation typo in warning message
Write-Message -Level Warning -Message "$notes for instance$instance"The word
Fix — drop the redundant word "instance" since Write-Message -Level Warning -Message "$notes for $instance"Known Fragility (unchanged from previous review, low severity)
Because SummaryOne minor string interpolation bug remains on line 358 — everything else from the previous reviews has been addressed correctly. Once that one-liner is fixed, this PR is clean. | Branch |
|
I added the missing space and will not change the test. |
Complete refactoring of the command, closes #10125.
The code now uses
Test-DbaNetworkCertificatewhich usesGet-DbaNetworkConfigurationand uses a similar structure like those commands.New is the functionality to unset a certificate. This is useful if the currently configured certificate is no longer valid and sql server does not start anymore.