Skip to content

Commit 887987a

Browse files
Merge pull request #3 from gabriel-samfira/add-jit-config-enablement
Add jit config enablement
2 parents 61409c2 + 7cbb88a commit 887987a

File tree

3 files changed

+90
-17
lines changed

3 files changed

+90
-17
lines changed

Diff for: cloudconfig/templates.go

+83-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ if [ -z "$METADATA_URL" ];then
4040
echo "no token is available and METADATA_URL is not set"
4141
exit 1
4242
fi
43-
GITHUB_TOKEN=$(curl --retry 5 --retry-delay 5 --retry-connrefused --fail -s -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}/runner-registration-token/")
4443
4544
function call() {
4645
PAYLOAD="$1"
@@ -53,11 +52,18 @@ function sendStatus() {
5352
call "{\"status\": \"installing\", \"message\": \"$MSG\"}"
5453
}
5554
55+
{{- if .UseJITConfig }}
56+
function success() {
57+
MSG="$1"
58+
call "{\"status\": \"idle\", \"message\": \"$MSG\"}"
59+
}
60+
{{- else}}
5661
function success() {
5762
MSG="$1"
5863
ID=$2
5964
call "{\"status\": \"idle\", \"message\": \"$MSG\", \"agent_id\": $ID}"
6065
}
66+
{{- end}}
6167
6268
function fail() {
6369
MSG="$1"
@@ -105,15 +111,15 @@ function downloadAndExtractRunner() {
105111
# chown {{ .RunnerUsername }}:{{ .RunnerGroup }} -R /home/{{ .RunnerUsername }}/actions-runner/ || fail "failed to change owner"
106112
}
107113
108-
TEMP_TOKEN=""
114+
{{- if not .UseJITConfig }}
109115
GH_RUNNER_GROUP="{{.GitHubRunnerGroup}}"
110-
111116
# $RUNNER_GROUP_OPT will be added to the config.sh line. If it's empty, nothing happens
112117
# if it holds a value, it will be part of the command.
113118
RUNNER_GROUP_OPT=""
114119
if [ ! -z $GH_RUNNER_GROUP ];then
115120
RUNNER_GROUP_OPT="--runnergroup=$GH_RUNNER_GROUP"
116121
fi
122+
{{- end }}
117123
118124
CACHED_RUNNER=$(getCachedToolsPath)
119125
if [ -z "$CACHED_RUNNER" ];then
@@ -130,6 +136,37 @@ fi
130136
131137
132138
sendStatus "configuring runner"
139+
{{- if .UseJITConfig }}
140+
function getRunnerFile() {
141+
curl --retry 5 --retry-delay 5 \
142+
--retry-connrefused --fail -s \
143+
-X GET -H 'Accept: application/json' \
144+
-H "Authorization: Bearer ${BEARER_TOKEN}" \
145+
"${METADATA_URL}/$1" -o "$2"
146+
}
147+
148+
sendStatus "downloading JIT credentials"
149+
getRunnerFile "credentials/runner" "/home/{{ .RunnerUsername }}/actions-runner/.runner" || fail "failed to get runner file"
150+
getRunnerFile "credentials/credentials" "/home/{{ .RunnerUsername }}/actions-runner/.credentials" || fail "failed to get credentials file"
151+
getRunnerFile "credentials/credentials_rsaparams" "/home/{{ .RunnerUsername }}/actions-runner/.credentials_rsaparams" || fail "failed to get credentials_rsaparams file"
152+
getRunnerFile "system/service-name" "/home/{{ .RunnerUsername }}/actions-runner/.service" || fail "failed to get service name file"
153+
sed -i 's/$/\.service/' /home/{{ .RunnerUsername }}/actions-runner/.service
154+
155+
SVC_NAME=$(cat /home/{{ .RunnerUsername }}/actions-runner/.service)
156+
157+
sendStatus "generating systemd unit file"
158+
getRunnerFile "systemd/unit-file?runAsUser={{ .RunnerUsername }}" "$SVC_NAME" || fail "failed to get service file"
159+
sudo mv $SVC_NAME /etc/systemd/system/ || fail "failed to move service file"
160+
161+
sendStatus "enabling runner service"
162+
cp /home/{{ .RunnerUsername }}/actions-runner/bin/runsvc.sh /home/{{ .RunnerUsername }}/actions-runner/ || fail "failed to copy runsvc.sh"
163+
sudo chown {{ .RunnerUsername }}:{{ .RunnerGroup }} -R /home/{{ .RunnerUsername }} || fail "failed to change owner"
164+
sudo systemctl daemon-reload || fail "failed to reload systemd"
165+
sudo systemctl enable $SVC_NAME
166+
{{- else}}
167+
168+
GITHUB_TOKEN=$(curl --retry 5 --retry-delay 5 --retry-connrefused --fail -s -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}/runner-registration-token/")
169+
133170
set +e
134171
attempt=1
135172
while true; do
@@ -161,12 +198,17 @@ set -e
161198
162199
sendStatus "installing runner service"
163200
sudo ./svc.sh install {{ .RunnerUsername }} || fail "failed to install service"
201+
{{- end}}
164202
165203
if [ -e "/sys/fs/selinux" ];then
166204
sudo chcon -h user_u:object_r:bin_t /home/runner/ || fail "failed to change selinux context"
167205
sudo chcon -R -h {{ .RunnerUsername }}:object_r:bin_t /home/runner/* || fail "failed to change selinux context"
168206
fi
169207
208+
{{- if .UseJITConfig }}
209+
sudo systemctl start $SVC_NAME || fail "failed to start service"
210+
success "runner successfully installed"
211+
{{- else}}
170212
sendStatus "starting service"
171213
sudo ./svc.sh start || fail "failed to start service"
172214
@@ -176,8 +218,8 @@ if [ $? -ne 0 ];then
176218
fail "failed to get agent ID"
177219
fi
178220
set -e
179-
180221
success "runner successfully installed" $AGENT_ID
222+
{{- end}}
181223
`
182224

183225
var WindowsSetupScriptTemplate = `#ps1_sysnative
@@ -298,14 +340,22 @@ function Update-GarmStatus() {
298340
param (
299341
[parameter(Mandatory=$true)]
300342
[string]$Message,
343+
[parameter(Mandatory=$false)]
344+
[int64]$AgentID=0,
345+
[parameter(Mandatory=$false)]
346+
[string]$Status="installing",
301347
[parameter(Mandatory=$true)]
302348
[string]$CallbackURL
303349
)
304350
PROCESS{
305351
$body = @{
306-
"status"="installing"
352+
"status"=$Status
307353
"message"=$Message
308354
}
355+
356+
if ($AgentID -ne 0) {
357+
$body["AgentID"] = $AgentID
358+
}
309359
Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
310360
}
311361
}
@@ -321,12 +371,7 @@ function Invoke-GarmSuccess() {
321371
[string]$CallbackURL
322372
)
323373
PROCESS{
324-
$body = @{
325-
"status"="idle"
326-
"message"=$Message
327-
"agent_id"=$AgentID
328-
}
329-
Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
374+
Update-GarmStatus -Message $Message -AgentID $AgentID -CallbackURL $CallbackURL -Status "idle" | Out-Null
330375
}
331376
}
332377
@@ -339,11 +384,7 @@ function Invoke-GarmFailure() {
339384
[string]$CallbackURL
340385
)
341386
PROCESS{
342-
$body = @{
343-
"status"="failed"
344-
"message"=$Message
345-
}
346-
Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
387+
Update-GarmStatus -Message $Message -CallbackURL $CallbackURL -Status "failed" | Out-Null
347388
Throw $Message
348389
}
349390
}
@@ -363,6 +404,7 @@ function Install-Runner() {
363404
Throw "missing callback authentication token"
364405
}
365406
try {
407+
net user administrator P@ssw0rd /active:yes
366408
$MetadataURL="{{.MetadataURL}}"
367409
$DownloadURL="{{.DownloadURL}}"
368410
if($MetadataURL -eq ""){
@@ -374,7 +416,6 @@ function Install-Runner() {
374416
Import-Certificate -CertificatePath $env:TMP\garm-ca.pem
375417
}
376418
377-
$GithubRegistrationToken = Invoke-WebRequest -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/runner-registration-token/
378419
Update-GarmStatus -CallbackURL $CallbackURL -Message "downloading tools from $DownloadURL"
379420
380421
$downloadToken="{{.TempDownloadToken}}"
@@ -399,11 +440,34 @@ function Install-Runner() {
399440
}
400441
Update-GarmStatus -CallbackURL $CallbackURL -Message "configuring and starting runner"
401442
cd $runnerDir
443+
444+
{{- if .UseJITConfig }}
445+
Update-GarmStatus -CallbackURL $CallbackURL -Message "downloading JIT credentials"
446+
wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/runner -OutFile (Join-Path $runnerDir ".runner")
447+
wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/credentials -OutFile (Join-Path $runnerDir ".credentials")
448+
449+
Add-Type -AssemblyName System.Security
450+
$rsaData = (wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/credentials_rsaparams)
451+
$encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($rsaData)
452+
$protectedBytes = [Security.Cryptography.ProtectedData]::Protect( $encodedBytes, $null, [Security.Cryptography.DataProtectionScope]::LocalMachine )
453+
[System.IO.File]::WriteAllBytes((Join-Path $runnerDir ".credentials_rsaparams"), $protectedBytes)
454+
455+
wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/system/service-name -OutFile "C:\runner\.service"
456+
457+
Update-GarmStatus -CallbackURL $CallbackURL -Message "Creating system service"
458+
$SVC_NAME=(gc -raw "C:\runner\.service")
459+
New-Service -Name "$SVC_NAME" -BinaryPathName "C:\runner\bin\RunnerService.exe" -DisplayName "$SVC_NAME" -Description "GitHub Actions Runner ($SVC_NAME)" -StartupType Automatic
460+
Start-Service "$SVC_NAME"
461+
Update-GarmStatus -Message "runner successfully installed" -CallbackURL $CallbackURL -Status "idle" | Out-Null
462+
463+
{{- else }}
464+
$GithubRegistrationToken = Invoke-WebRequest -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/runner-registration-token/
402465
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken $runnerGroupOpt --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --ephemeral --runasservice
403466
404467
$agentInfoFile = Join-Path $runnerDir ".runner"
405468
$agentInfo = ConvertFrom-Json (gc -raw $agentInfoFile)
406469
Invoke-GarmSuccess -CallbackURL $CallbackURL -Message "runner successfully installed" -AgentID $agentInfo.agentId
470+
{{- end }}
407471
} catch {
408472
Invoke-GarmFailure -CallbackURL $CallbackURL -Message $_
409473
}
@@ -452,6 +516,8 @@ type InstallRunnerParams struct {
452516
// This option is useful for situations in which you're supplying your own template and you need
453517
// to pass in information that is not available in the default template.
454518
ExtraContext map[string]string
519+
// UseJITConfig indicates whether to attempt to configure the runner using JIT or a registration token.
520+
UseJITConfig bool
455521
}
456522

457523
func InstallRunnerScript(installParams InstallRunnerParams, osType params.OSType, tpl string) ([]byte, error) {

Diff for: cloudconfig/util.go

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func GetRunnerInstallScript(bootstrapParams commonParams.BootstrapInstance, tool
105105
GitHubRunnerGroup: bootstrapParams.GitHubRunnerGroup,
106106
ExtraContext: extraSpecs.ExtraContext,
107107
EnableBootDebug: bootstrapParams.UserDataOptions.EnableBootDebug,
108+
UseJITConfig: bootstrapParams.JitConfigEnabled,
108109
}
109110

110111
if bootstrapParams.CACertBundle != nil && len(bootstrapParams.CACertBundle) > 0 {

Diff for: params/params.go

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ type BootstrapInstance struct {
104104

105105
// UserDataOptions are the options for the user data generation.
106106
UserDataOptions UserDataOptions `json:"user_data_options"`
107+
108+
// JitConfigEnabled is a flag that indicates if the runner should be configured to use
109+
// just-in-time configuration. If set to true, providers must attempt to fetch the JIT configuration
110+
// from the metadata service instead of the runner registration token. The runner registration token
111+
// is not available if the runner is configured to use JIT.
112+
JitConfigEnabled bool `json:"jit_config_enabled"`
107113
}
108114

109115
type Address struct {

0 commit comments

Comments
 (0)