Repository navigation
Expand file tree
/
Copy pathaction.yaml
More file actions
382 lines (335 loc) · 14.2 KB
/
Copy pathaction.yaml
File metadata and controls
382 lines (335 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright 2022 Chainguard, Inc.
# SPDX-License-Identifier: Apache-2.0
name: "Setup chainctl"
description: |
This action sets up the Chainguard chainctl CLI and authenticates
it against the target environment.
branding:
icon: "terminal"
color: "purple"
inputs:
environment:
description: |
Determines the environment from which to download the chainctl
binary from.
required: true
default: enforce.dev
version:
description: |
The version of chainctl to install (e.g. 0.2.313). Defaults to
the latest release.
required: false
default: latest
env-auth:
description: |
Determines if we export HTTP_AUTH env variable with chainctl auth
token.
required: false
default: "false"
identity:
description: |
The id of the identity that this workflow should assume for
performing actions with chainctl.
required: false
audience:
description: |
Specifies the identity token audience to use when creating an
identity token to authenticate with Chainguard.
Defaults to issuer.${environment}
This field is DEPRECATED, use identity instead.
required: false
invite-code:
description: |
Optionally specifies an invite code that allows this workflow
register itself when run for the first time.
Use of invite codes is DEPRECATED, use identity instead.
required: false
verbosity:
description: |
Set the logging verbosity for chainctl. A value of 0 disables
logging output. Valid values are 1-5, increasing in verbosity.
required: false
default: "0"
config-path:
description: |
The location of the chainctl config file to use. These values
override the default configuration values in the binary.
required: false
default: ""
retry-all-errors:
description: |
Whether to retry on errors downloading the chainctl binary.
required: false
default: "true"
apk-host:
description: |
The hostname to authenticate against for APK-related authentication.
Must be a bare hostname, optionally with a port (apk.cgr.dev,
apk.example.com:8443). Schemes, paths and shell metacharacters are
rejected.
required: false
default: apk.cgr.dev
libraries-host:
description: |
The hostname to authenticate against for Chainguard Libraries-related
authentication.
Must be a bare hostname, optionally with a port. Schemes, paths and
shell metacharacters are rejected.
required: false
default: libraries.cgr.dev
setup-python-keyring:
description: |
Whether to install the Chainguard Libraries pip keyring package.
required: false
default: "false"
runs:
using: "composite"
steps:
# apk-host and libraries-host reach two sinks: chainctl arguments, and
# the HTTP_AUTH line appended to $GITHUB_ENV. Both are quoted, but a
# value carrying a newline would still inject extra variables into the
# step environment. Reject anything that is not a bare host[:port] up
# front so neither sink is reachable with a crafted value.
#
# environment and version are interpolated into the download URL, so a
# crafted value could point the download (and its checksum) at another
# host or path. verbosity and identity are passed to chainctl and echoed
# into workflow commands. Validate all of them here too.
- name: Validate inputs
shell: bash
env:
APK_HOST: ${{ inputs.apk-host }}
LIBRARIES_HOST: ${{ inputs.libraries-host }}
ENVIRONMENT: ${{ inputs.environment }}
VERSION: ${{ inputs.version }}
VERBOSITY: ${{ inputs.verbosity }}
IDENTITY: ${{ inputs.identity }}
run: |
# Values are never echoed back: they are caller-supplied and could
# carry workflow commands that the runner would interpret.
host_re='^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?(:[0-9]+)?$'
for var in APK_HOST LIBRARIES_HOST; do
if [[ ! "${!var}" =~ ${host_re} ]]; then
echo "::error::Invalid ${var}: expected a bare hostname such as apk.cgr.dev, optionally with a :port"
exit 1
fi
done
env_re='^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$'
if [[ ! "${ENVIRONMENT}" =~ ${env_re} ]]; then
echo "::error::Invalid environment: expected a bare domain such as enforce.dev"
exit 1
fi
# Empty means latest; otherwise "latest" or a release such as
# 0.2.313 / v0.2.313, optionally with a pre-release suffix.
version_re='^(latest|v?[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?)$'
if [[ -n "${VERSION}" && ! "${VERSION}" =~ ${version_re} ]]; then
echo "::error::Invalid version: expected latest or a release such as 0.2.313"
exit 1
fi
if [[ ! "${VERBOSITY}" =~ ^[0-5]$ ]]; then
echo "::error::Invalid verbosity: expected an integer from 0 to 5"
exit 1
fi
if [[ "${IDENTITY}" =~ [[:space:][:cntrl:]] ]]; then
echo "::error::Invalid identity: must not contain whitespace or control characters"
exit 1
fi
- name: Install chainctl
shell: bash
env:
CURL_RETRY_ALL_ERRORS: ${{ inputs.retry-all-errors }}
ENVIRONMENT: ${{ inputs.environment }}
VERSION: ${{ inputs.version }}
run: |
cd "$(mktemp -d)" || exit
# Massage GitHub's values to the ones we expect.
# https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context
# RUNNER_OS and RUNNER_ARCH are set automatically by the GitHub Actions runner.
os=$(echo "$RUNNER_OS" | tr '[:upper:]' '[:lower:]')
if [[ "${os}" == "macos" ]]; then
os="darwin"
fi
arch="$RUNNER_ARCH"
if [[ "${arch}" == "X64" ]]; then
arch="x86_64"
elif [[ "${arch}" == "ARM64" ]]; then
arch="arm64"
fi
file="chainctl_${os}_${arch}"
out="chainctl"
if [[ "${os}" == "windows" ]]; then
file="${file}.exe"
out="${out}.exe"
fi
# An empty version means the latest release; accept an optional
# leading "v" (dl.* paths use bare version numbers).
VERSION="${VERSION:-latest}"
VERSION="${VERSION#v}"
base_url="https://dl.${ENVIRONMENT}/chainctl/${VERSION}"
checksums_url="${base_url}/chainctl_checksums.txt"
url="${base_url}/${file}"
# Refuse plain HTTP, including on redirects, and old TLS versions.
curl_flags=(-fsSL --proto '=https' --proto-redir '=https' --tlsv1.2 --retry 5 --retry-delay 1)
if [[ "${CURL_RETRY_ALL_ERRORS}" == "true" ]]; then
curl_flags+=(--retry-all-errors)
fi
# Sets ${expected} to the published checksum for ${file}. Re-invoked
# on mismatch: a release promoted between the checksum and binary
# fetches makes the pair inconsistent, and one refresh re-syncs it.
fetch_expected() {
if ! curl -o checksums.txt "${curl_flags[@]}" "${checksums_url}"; then
echo "::error::Failed to download ${checksums_url}; cannot verify chainctl"
exit 1
fi
expected=$(awk -v file="${file}" '$2 == file {print $1}' checksums.txt)
if [[ -z "${expected}" ]]; then
echo "::error::No checksum found for ${file} in checksums.txt"
exit 1
fi
}
# Fetch the expected checksum first so a missing entry fails fast,
# before the binary download.
echo "Downloading checksums from ${checksums_url}"
fetch_expected
echo "Downloading chainctl from ${url}"
curl -o ./${out} "${curl_flags[@]}" "${url}"
# Compute the binary's checksum. PowerShell emits CRLF line endings;
# bash's $() strips only the LF, so drop the CR too or the comparison
# below can never match.
if command -v sha256sum &>/dev/null; then
actual=$(sha256sum "./${out}" | awk '{print $1}')
elif command -v shasum &>/dev/null; then
actual=$(shasum -a 256 "./${out}" | awk '{print $1}')
elif command -v pwsh &>/dev/null; then
actual=$(pwsh -NoProfile -NonInteractive -Command "(Get-FileHash -Algorithm SHA256 -LiteralPath './${out}').Hash.ToLower()" | tr -d '\r')
elif command -v powershell &>/dev/null; then
actual=$(powershell -NoProfile -NonInteractive -Command "(Get-FileHash -Algorithm SHA256 -LiteralPath './${out}').Hash.ToLower()" | tr -d '\r')
else
echo "::error::No sha256 tool available (tried sha256sum, shasum, pwsh, powershell)"
exit 1
fi
if [[ -z "${actual}" ]]; then
echo "::error::Failed to compute the sha256 checksum of ${out}"
exit 1
fi
if [[ "${expected}" != "${actual}" ]]; then
echo "Checksum mismatch for ${file}; refreshing checksums and retrying once"
fetch_expected
fi
if [[ "${expected}" != "${actual}" ]]; then
echo "::error::Checksum mismatch for ${file}: expected ${expected}, got ${actual}"
exit 1
fi
echo "Checksum verified for ${file}"
rm -f checksums.txt
chmod +x ./${out}
echo "$(pwd)" >> "$GITHUB_PATH"
# Print the version of chainctl installed
./${out} version
- name: Authenticate with Chainguard (assumed identity)
shell: bash
if: ${{ inputs.identity != '' }}
env:
CHAINCTL_DEBUG: "true"
VERBOSITY: ${{ inputs.verbosity }}
IDENTITY: ${{ inputs.identity }}
CHAINCTL_CONFIG: ${{ inputs.config-path }}
EXPORT_AUTH: ${{ inputs.env-auth }}
LIBRARIES_HOST: ${{ inputs.libraries-host }}
APK_HOST: ${{ inputs.apk-host }}
run: |
echo "::group::Authenticating with Chainguard as ${IDENTITY}"
if chainctl auth login --identity "${IDENTITY}" -v="${VERBOSITY}"; then
echo "Logged in as ${IDENTITY}!"
else
echo "::error::Unable to assume the identity ${IDENTITY}."
exit 1
fi
echo "::endgroup::"
echo "::group::Authenticating with ${LIBRARIES_HOST} as ${IDENTITY}"
if ! chainctl auth login --identity "${IDENTITY}" --audience "${LIBRARIES_HOST}" -v="${VERBOSITY}"; then
echo "::error::Unable to assume the identity ${IDENTITY} for ${LIBRARIES_HOST}."
exit 1
fi
echo "::endgroup::"
echo "::group::Authenticating with ${APK_HOST} as ${IDENTITY}"
if ! chainctl auth login --identity "${IDENTITY}" --audience "${APK_HOST}" -v="${VERBOSITY}"; then
echo "::error::Unable to assume the identity ${IDENTITY} for ${APK_HOST}."
exit 1
fi
echo "::endgroup::"
echo "::group::Registering docker credential helper"
if ! chainctl auth configure-docker --identity "${IDENTITY}" -v="${VERBOSITY}"; then
echo "::error::Unable to register credential helper as ${IDENTITY}."
exit 1
fi
echo "::endgroup::"
if [ "${EXPORT_AUTH}" == "true" ]; then
echo "::group::Exporting HTTP_AUTH environment variable for ${APK_HOST}"
# Capture the token so it can be masked before it lands in
# $GITHUB_ENV; otherwise anything that later prints HTTP_AUTH
# leaks it into the logs unredacted.
token=$(chainctl auth token --audience "${APK_HOST}")
if [[ -z "${token}" ]]; then
echo "::error::Unable to mint an APK token for ${APK_HOST}."
exit 1
fi
echo "::add-mask::${token}"
echo "HTTP_AUTH=basic:${APK_HOST}:user:${token}" >> "$GITHUB_ENV"
echo "::endgroup::"
fi
- name: Authenticate with Chainguard (DEPRECATED invite-code)
shell: bash
if: ${{ inputs.invite-code != '' }}
env:
CHAINGUARD_INVITE_CODE: ${{ inputs.invite-code }}
CHAINCTL_DEBUG: "true"
VERBOSITY: ${{ inputs.verbosity }}
ENVIRONMENT: ${{ inputs.environment }}
AUDIENCE: ${{ inputs.audience }}
EXPORT_AUTH: ${{ inputs.env-auth }}
APK_HOST: ${{ inputs.apk-host }}
run: |
echo "::warning::The use of invite codes with Github actions is deprecated, use assumed identities instead."
if [[ -z "${AUDIENCE}" ]]; then
AUDIENCE="issuer.${ENVIRONMENT}"
fi
if [[ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ]]; then
echo "::error::No OIDC token available; grant the job 'id-token: write' permission."
exit 1
fi
# URL-encode the audience so it cannot add extra query parameters.
encoded_audience=$(jq -rn --arg a "${AUDIENCE}" '$a | @uri')
IDTOKEN=$(curl -fsS --proto '=https' --tlsv1.2 \
-H "Authorization: bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" \
"${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=${encoded_audience}" | jq -r '.value // empty')
if [[ -z "${IDTOKEN}" ]]; then
echo "::error::Unable to fetch a GitHub OIDC token."
exit 1
fi
# The runner does not mask OIDC tokens fetched by hand.
echo "::add-mask::${IDTOKEN}"
# This will start failing once the invite code expires, which is why we have the login guard.
if chainctl auth login --create-group=false --identity-token "${IDTOKEN}" --invite-code="${CHAINGUARD_INVITE_CODE}" -v="${VERBOSITY}"; then
echo Logged in!
else
echo Failed to log in with invite code
exit 1
fi
if [ "${EXPORT_AUTH}" == "true" ]; then
token=$(chainctl auth token --audience "${APK_HOST}")
if [[ -z "${token}" ]]; then
echo "::error::Unable to mint an APK token for ${APK_HOST}."
exit 1
fi
echo "::add-mask::${token}"
echo "HTTP_AUTH=basic:${APK_HOST}:user:${token}" >> "$GITHUB_ENV"
fi
- if: ${{ inputs.setup-python-keyring == 'true' }}
name: Install Python keyring package
shell: bash
run: |
# Install the published wheel only, so no sdist build code runs.
python -m pip install --disable-pip-version-check --no-input \
--only-binary=keyrings-chainguard-libraries \
"keyrings-chainguard-libraries==0.2.0"