Skip to content

Commit efdc26e

Browse files
New-DbaDbMailAccount - Fix conflicting SMTP authentication validation (review of #10257)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f72d98a commit efdc26e

5 files changed

Lines changed: 57 additions & 6 deletions

File tree

docs/trackers/features/commit-bug-review-TRACKER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
7373
| 4d1a9d80c | v2.7.27 | DONE | version bump - skip |
7474
| 232395207 | Set-DbaPrivilege, Get-DbaPrivilege - Add CreateGlobalObjects privilege support (#10235) | DONE | Fixed empty privilege-entry updates so Set-DbaPrivilege still grants rights when secedit exports a blank line; added unit regression test. |
7575
| 099624061 | Get-DbaDbRestoreHistory - Add BackupStartDate, StopAt, and LastRestorePoint columns (#10249) | DONE | Fixed LastRestorePoint so StopAt is used whenever specified; added unit regression test. |
76-
| 4f1e56ce4 | New-DbaDbMailAccount, Set-DbaDbMailAccount - Add Port, SSL, and authentication parameters (#10257) | PENDING | |
76+
| 4f1e56ce4 | New-DbaDbMailAccount, Set-DbaDbMailAccount - Add Port, SSL, and authentication parameters (#10257) | DONE | Added validation to reject conflicting SMTP authentication modes and incomplete credential pairs; added unit regression tests. |
7777
| 8218d327e | Restore-DbaDatabase, Invoke-DbaAdvancedRestore - Add ErrorBrokerConversations parameter (#10253) | PENDING | |
7878
| 9a4e4bacb | Connect-DbaInstance - Set NonPooledConnection on ServerConnection (#10260) | PENDING | |
7979
| a0ab78a66 | Get-DbaCmObject - Apply CimOperationTimeout to all CIM connections (#10252) | PENDING | |

public/New-DbaDbMailAccount.ps1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ function New-DbaDbMailAccount {
160160
[string]$EmailAddress,
161161
[string]$ReplyToAddress,
162162
[string]$MailServer,
163+
[ValidateRange(1, 65535)]
163164
[int]$Port,
164165
[switch]$EnableSSL,
165166
[switch]$UseDefaultCredentials,
@@ -169,6 +170,16 @@ function New-DbaDbMailAccount {
169170
[switch]$EnableException
170171
)
171172
process {
173+
if ($UseDefaultCredentials.IsPresent -and (Test-Bound -ParameterName UserName, Password)) {
174+
Stop-Function -Category InvalidArgument -Message "You cannot specify -UseDefaultCredentials with -UserName or -Password."
175+
return
176+
}
177+
178+
if (Test-Bound -ParameterName UserName, Password -Min 1 -Max 1) {
179+
Stop-Function -Category InvalidArgument -Message "You must specify both -UserName and -Password together."
180+
return
181+
}
182+
172183
foreach ($instance in $SqlInstance) {
173184
try {
174185
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
@@ -224,4 +235,4 @@ function New-DbaDbMailAccount {
224235
}
225236
}
226237
}
227-
}
238+
}

public/Set-DbaDbMailAccount.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ function Set-DbaDbMailAccount {
137137
[string]$EmailAddress,
138138
[string]$ReplyToAddress,
139139
[string]$NewMailServerName,
140+
[ValidateRange(1, 65535)]
140141
[int]$Port,
141142
[switch]$EnableSSL,
142143
[switch]$UseDefaultCredentials,
@@ -145,6 +146,11 @@ function Set-DbaDbMailAccount {
145146
[switch]$EnableException
146147
)
147148
process {
149+
if ($UseDefaultCredentials.IsPresent -and (Test-Bound -ParameterName UserName, Password)) {
150+
Stop-Function -Category InvalidArgument -Message "You cannot specify -UseDefaultCredentials with -UserName or -Password."
151+
return
152+
}
153+
148154
foreach ($instance in $SqlInstance) {
149155
$InputObject += Get-DbaDbMailAccount -SqlInstance $instance -SqlCredential $SqlCredential -Account $Account -EnableException:$EnableException
150156
}
@@ -197,4 +203,4 @@ function Set-DbaDbMailAccount {
197203
}
198204
}
199205
}
200-
}
206+
}

tests/New-DbaDbMailAccount.Tests.ps1

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
3+
$ModuleName = "dbatools",
44
$CommandName = "New-DbaDbMailAccount",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -30,6 +30,27 @@ Describe $CommandName -Tag UnitTests {
3030
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
3131
}
3232
}
33+
34+
InModuleScope dbatools {
35+
Context "Input validation" {
36+
It "Should call Stop-Function when default credentials are combined with SMTP credentials" {
37+
Mock Stop-Function { }
38+
$securePassword = ConvertTo-SecureString "P@ssw0rd!" -AsPlainText -Force
39+
40+
New-DbaDbMailAccount -SqlInstance "sql1" -Account "alerts" -EmailAddress "alerts@contoso.com" -UseDefaultCredentials -UserName "alerts@contoso.com" -Password $securePassword | Should -BeNullOrEmpty
41+
42+
Should -Invoke Stop-Function -Times 1 -Exactly
43+
}
44+
45+
It "Should call Stop-Function when only one SMTP credential value is provided" {
46+
Mock Stop-Function { }
47+
48+
New-DbaDbMailAccount -SqlInstance "sql1" -Account "alerts" -EmailAddress "alerts@contoso.com" -UserName "alerts@contoso.com" | Should -BeNullOrEmpty
49+
50+
Should -Invoke Stop-Function -Times 1 -Exactly
51+
}
52+
}
53+
}
3354
}
3455

3556
Describe $CommandName -Tag IntegrationTests {

tests/Set-DbaDbMailAccount.Tests.ps1

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
3+
$ModuleName = "dbatools",
44
$CommandName = "Set-DbaDbMailAccount",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -30,6 +30,19 @@ Describe $CommandName -Tag UnitTests {
3030
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
3131
}
3232
}
33+
34+
InModuleScope dbatools {
35+
Context "Input validation" {
36+
It "Should call Stop-Function when default credentials are combined with SMTP credentials" {
37+
Mock Stop-Function { }
38+
$securePassword = ConvertTo-SecureString "P@ssw0rd!" -AsPlainText -Force
39+
40+
Set-DbaDbMailAccount -SqlInstance "sql1" -Account "alerts" -UseDefaultCredentials -UserName "alerts@contoso.com" -Password $securePassword | Should -BeNullOrEmpty
41+
42+
Should -Invoke Stop-Function -Times 1 -Exactly
43+
}
44+
}
45+
}
3346
}
3447

3548
Describe $CommandName -Tag IntegrationTests {
@@ -126,4 +139,4 @@ Describe $CommandName -Tag IntegrationTests {
126139
$mailServer.EnableSsl | Should -Be $false
127140
}
128141
}
129-
}
142+
}

0 commit comments

Comments
 (0)