Skip to content

Commit e7ac783

Browse files
committed
Add retry loop for randomizing buildkite-agent password
Sometimes the random password generated doesn't match the password policy on Windows. This Try/Catch loop gives it a few extra chances to get a good password before failing. Signed-off-by: Jeremiah Snapp <[email protected]>
1 parent bb46665 commit e7ac783

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

packer/windows/conf/bin/bk-install-elastic-stack.ps1

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,32 @@ Set-PSDebug -Trace 0
119119

120120
Write-Output "Creating buildkite-agent user account in Administrators group"
121121

122-
$Count = Get-Random -min 24 -max 32
123-
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})
124122
$UserName = "buildkite-agent"
125123

126-
New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
124+
$StopLoop = $false
125+
[int]$RetryCount = "0"
126+
127+
# a Try/Catch block is used in a loop to make a few extra attempts at creating the user account before finally giving up and failing
128+
# because sometimes the generated random password does not satisfy the system's password policy
129+
Do {
130+
Try {
131+
$Count = Get-Random -min 24 -max 32
132+
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})
133+
134+
New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
135+
$StopLoop = $true
136+
}
137+
Catch {
138+
If ($RetryCount -gt 10){
139+
Write-Output "Could not create $UserName user after 10 retries."
140+
exit 1
141+
}
142+
Else {
143+
Write-Output "Could not create $UserName user, retrying..."
144+
$RetryCount = $RetryCount + 1
145+
}
146+
}
147+
} While ($StopLoop -eq $false)
127148

128149
If ($Env:BUILDKITE_WINDOWS_ADMINISTRATOR -eq "true") {
129150
Add-LocalGroupMember -Group "Administrators" -Member $UserName | out-null

0 commit comments

Comments
 (0)