Skip to content

Commit 1b236e5

Browse files
Merge pull request #49 from cloudbase/add-user-idenpotency-windows
Idempotent user creation
2 parents 4163ff0 + a2056d4 commit 1b236e5

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

cloudconfig/templates.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,19 @@ function Install-Runner() {
604604
# Create user with administrator rights to run service as
605605
$userPasswd = Get-RandomString -Length 10
606606
$secPasswd = ConvertTo-SecureString "$userPasswd" -AsPlainText -Force
607-
New-LocalUser -Name "runner" -Password $secPasswd -PasswordNeverExpires -UserMayNotChangePassword
608-
$pscreds = New-Object System.Management.Automation.PSCredential (".\runner", $secPasswd)
609-
$adminGrpName = (Get-CimInstance win32_group -Filter 'SID = "S-1-5-32-544"').Name
610-
if (!$adminGrpName) {
611-
Throw "Could not find administrators group name"
607+
$userName = "runner"
608+
$user = Get-LocalUser -Name $userName -ErrorAction SilentlyContinue
609+
if (-not $user) {
610+
New-LocalUser -Name $userName -Password $secPasswd -PasswordNeverExpires -UserMayNotChangePassword
611+
} else {
612+
Set-LocalUser -PasswordNeverExpires $true -Name $userName -Password $secPasswd
613+
}
614+
$pscreds = New-Object System.Management.Automation.PSCredential (".\$userName", $secPasswd)
615+
$hasUser = Get-LocalGroupMember -SID S-1-5-32-544 -Member $userName -ErrorAction SilentlyContinue
616+
if (-not $hasUser){
617+
Add-LocalGroupMember -SID S-1-5-32-544 -Member $userName
612618
}
613-
Add-LocalGroupMember -Group $adminGrpName -Member runner
614-
$ntAcct = New-Object System.Security.Principal.NTAccount("runner")
619+
$ntAcct = New-Object System.Security.Principal.NTAccount($userName)
615620
$sid = $ntAcct.Translate([System.Security.Principal.SecurityIdentifier])
616621
$sidBytes = New-Object byte[] ($sid.BinaryLength)
617622
$sid.GetBinaryForm($sidBytes, 0)
@@ -658,7 +663,7 @@ function Install-Runner() {
658663
# Ensure runner has full access to actions-runner folder
659664
$runnerACL = Get-Acl $runnerDir
660665
$runnerACL.SetAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule(
661-
"runner", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"
666+
$userName, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"
662667
)))
663668
Set-Acl -Path $runnerDir -AclObject $runnerAcl
664669
@@ -691,9 +696,9 @@ function Install-Runner() {
691696
Invoke-WebRequest -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/runner-registration-token/
692697
} -MaxRetryCount 5 -RetryInterval 5 -RetryMessage "Retrying download of GitHub registration token..."
693698
{{- if .GitHubRunnerGroup }}
694-
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken --runnergroup {{.GitHubRunnerGroup}} --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --no-default-labels --ephemeral --runasservice --windowslogonaccount runner --windowslogonpassword "$userPasswd"
699+
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken --runnergroup {{.GitHubRunnerGroup}} --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --no-default-labels --ephemeral --runasservice --windowslogonaccount "$userName" --windowslogonpassword "$userPasswd"
695700
{{- else}}
696-
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --no-default-labels --ephemeral --runasservice --windowslogonaccount runner --windowslogonpassword "$userPasswd"
701+
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --no-default-labels --ephemeral --runasservice --windowslogonaccount "$userName" --windowslogonpassword "$userPasswd"
697702
{{- end}}
698703
if ($LASTEXITCODE) {
699704
Throw "Failed to configure runner. Err code $LASTEXITCODE"

0 commit comments

Comments
 (0)