Skip to content

Commit 431ef03

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
Fix test password generation rule to meet Windows complexity requirements (PowerShell#10143)
1 parent f645cb8 commit 431ef03

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

test/powershell/Modules/Microsoft.PowerShell.Management/Set-Service.Tests.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
1010
}
1111
if ($IsWindows) {
1212
$userName = "testuserservices"
13-
$Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | Sort-Object {Get-Random})[0..12] -join ''
14-
$testPass = (New-Object -TypeName Net.NetworkCredential("", $Password)).SecurePassword
13+
$testPass = [Net.NetworkCredential]::new("", (New-ComplexPassword)).SecurePassword
1514
$creds = [pscredential]::new(".\$userName", $testPass)
1615
$SecurityDescriptorSddl = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;SU)'
1716
$WrongSecurityDescriptorSddl = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BB)(A;;CCLCSWLOCRRC;;;SU)'

test/tools/Modules/HelpersCommon/HelpersCommon.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ FunctionsToExport = @(
2323
'Enable-Testhook'
2424
'Get-RandomFileName'
2525
'New-RandomHexString'
26+
'New-ComplexPassword'
2627
'Send-VstsLogFile'
2728
'Set-TesthookResult'
2829
'Start-NativeExecution'

test/tools/Modules/HelpersCommon/HelpersCommon.psm1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,22 @@ function Test-CanWriteToPsHome
344344

345345
$script:CanWriteToPsHome
346346
}
347+
348+
# Creates a password meeting Windows complexity rules
349+
function New-ComplexPassword
350+
{
351+
$numbers = "0123456789"
352+
$lowercase = "abcdefghijklmnopqrstuvwxyz"
353+
$uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
354+
$symbols = "~!@#$%^&*_-+=``|\(){}[]:;`"'<>,.?/"
355+
$password = [string]::Empty
356+
# Windows password complexity rule requires minimum 8 characters and using at least 3 of the
357+
# buckets above, so we just pick one from each bucket twice.
358+
# https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements
359+
1..2 | ForEach-Object {
360+
$Password += $numbers[(Get-Random $numbers.Length)] + $lowercase[(Get-Random $lowercase.Length)] +
361+
$uppercase[(Get-Random $uppercase.Length)] + $symbols[(Get-Random $symbols.Length)]
362+
}
363+
364+
$password
365+
}

0 commit comments

Comments
 (0)