`scripts/Dockerfile.base` and `install.sh --with-terraform` download the
bundled Terraform zip from `releases.hashicorp.com` and use it without
checking it. This verifies that zip against a checksum file kept in the
Coder repo. The only thing trusted is the Coder repo, which is where the
`Dockerfile` and `install.sh` themselves come from. Everything fetched
from HashiCorp is checked against it.
The other download paths are already covered:
`provisioner/terraform/install.go` uses `hc-install`, which checks the
GPG-signed SHA256SUMS, and `mise.lock` / `flake.nix` already carry
per-platform hashes.
## Source of truth
`provisioner/terraform/terraform_SHA256SUMS` is a verbatim copy of
HashiCorp's `terraform_1.17.2_SHA256SUMS`, the version `main` ships,
verified against their signing key (`C874 011F 0AB4 0511 0D02 1055 3436
5D94 72D7 468F`). Being verbatim, you can diff it against the published
file or check it with the published `.sig`. Its hashes match the
Terraform entries in `mise.lock` on `main`. Both the Terraform version
and the checksums are read from it, so neither consumer pins a version.
## Consumers
* `scripts/Dockerfile.base` copies the file in and reads the version and
its architecture's checksum from it. It checks the zip with `sha256sum
-c` before unzipping. The build fails if there's no entry for the
architecture or the download doesn't match. All three build contexts
include the file: `docker-base.yaml`, `release.yaml` and
`scripts/build_docker.sh`. `docker-base.yaml` also runs when the file
changes.
* `install.sh` no longer has a `TERRAFORM_VERSION` or any hashes.
`--with-terraform` downloads `terraform_SHA256SUMS` from the Coder repo
at `refs/tags/v${VERSION}`, the tag of the release being installed. Only
if that tag doesn't have the file (a 404, for releases from before it
existed) does it fall back to `refs/heads/main`, with a note in the
output. Any other error fails. Fully qualified refs mean a branch named
like the tag, or a tag named `main`, can't be read instead. It then:
* only accepts lines with exactly two fields: 64 lowercase hex
characters and `terraform_X.Y.Z_<os>_<arch>.zip`. It picks the line for
`${OS}_${TERRAFORM_ARCH}`, validates the Terraform version and builds
the zip name from validated parts rather than using the one in the file.
It refuses to install if there's no such line;
* downloads the zip from HashiCorp and checks it with `sha256sum`,
`shasum -a 256` or `sha256`, including a zip reused from the cache. On a
mismatch it deletes the zip so a rerun downloads it again;
* skips the check under `--dry-run`, since nothing is downloaded. The
checksum file is still fetched so the version can be printed.
* `install.sh` **command handling** (security review): `sh_c` runs its
arguments directly instead of re-parsing them with `sh -c`, and
`sudo_sh_c` passes them to `sudo` or `doas` as-is. The `su` fallback
single-quotes every argument (`quote_args`). The resolved Coder version
must be `X.Y.Z` or `X.Y.Z-rc.N` before it's used in URLs, file names or
commands. As a side effect, install prefixes and cache paths with spaces
now work.
* `provisioner/terraform/checksums_test.go` fails if the file isn't in
SHA256SUMS format, is for a different version than `TerraformVersion`,
or lacks the `linux_{amd64,arm64,arm}` entries that `Dockerfile.base`
needs.
Also fixes `install.sh` unzipping `${ARCH}` instead of
`${TERRAFORM_ARCH}`, which broke `--with-terraform` on armv7 (`armv7` vs
`arm`).
## Bumping after this
Replace `terraform_SHA256SUMS` with the new verified file and update
`TerraformVersion` (plus `mise.toml`, `mise.lock`, `flake.nix` and the
airgap doc, as today). `Dockerfile.base` and `install.sh` need no edits.
`scripts/update_terraform_version.sh` (coder/coder#31463) does all of
this, and the `TerraformVersion` note in `install.go` points at it.
`main` is merged into this branch, including coder/coder#31438
(Terraform 1.17.1) and coder/coder#32110 (Terraform 1.17.2), so the
checksum file and `TerraformVersion` are at 1.17.2.
## Testing
After merging `main` (Terraform 1.17.2), I re-ran the Go test, `go vet`,
`gofmt`, `shellcheck`, `shfmt` and `actionlint`, and checked that the
`Dockerfile.base` extraction resolves 1.17.2 for amd64, arm64 and arm.
The `docker-base` build and the `install.sh` and `Dockerfile.base` runs
below were done before that merge, against 1.16.7 and 1.17.1, and I
haven't repeated them at 1.17.2. The `docker-base` workflow rebuilds the
image from the 1.17.2 file in CI.
* The `docker-base` workflow built all three platforms from the checksum
file: `terraform_1.16.7_linux_{amd64,arm64,arm}.zip: OK`, then Terraform
v1.16.7 on each.
* Ran the `Dockerfile.base` Terraform step under BusyBox 1.35 `sh`. It
fails with a changed hash or no entry for the architecture. With the
verified 1.17.1 file it installs 1.17.1 with no Dockerfile edit.
* `install.sh` `with_terraform` under `dash` on linux/amd64:
* fetched the real file over `raw.githubusercontent.com` from this
branch (standing in for `main`), then verified and installed 1.16.7;
* with the 1.17.1 file it installs 1.17.1;
* a changed hash is rejected and the cached zip is deleted;
* an unsupported platform and a garbage response are both refused;
* `--dry-run` prints the commands.
* Today neither the release tags nor `main` have the file, so
`--with-terraform` fails with "Could not download the Terraform
checksums" until this merges. After that, older releases use `main`'s
file.
* Security review fixes, under `dash` and `bash --posix`:
* checksum lines with `$(...)`, backticks, `;`, `../`, an extra field,
an uppercase or 65-character hash, or a malformed Terraform version are
refused before any command runs. The previous version of the script ran
the `$(...)` one. A file with an injected line followed by the real ones
installs only the real zip;
* with a `curl` stub, a tag hit reads `refs/tags/v2.39.0`, a 404 falls
back to `refs/heads/main`, and a 500 or a network error fails without
trying `main`;
* `sh_c`, and `sudo_sh_c` through `sudo`, `doas` and `su` stubs, pass
arguments with spaces, quotes, `;` and `$(...)` through unchanged and
run nothing else. `--dry-run` runs nothing;
* `--version v2.28.0`, `2.28.0;id` and `../../x` are refused. `--version
2.39.0`, `--version 2.33.0-rc.1`, `--stable` and the default mainline
still work;
* real installs of Terraform into a prefix with a space (no sudo) and
into a root-owned prefix (through `sudo`), and of Coder 2.39.0 with
`--method standalone` into a prefix with a space.
* `go test ./provisioner/terraform -run
TestTerraformChecksumFileMatchesVersion` passes with the 1.17.2 file,
and fails with the 1.17.1 file in place (`expected: "1.17.2", actual:
"1.17.1"`). `go vet` and `gofmt` are clean.
* `shellcheck --external-sources` (v0.10.0) and `shfmt -d` (v3.12.0) are
clean on `install.sh` and `scripts/build_docker.sh`, and `actionlint`
(v1.7.7) is clean on the workflows. Local git hooks were skipped.
## How coder/coder#31600, coder/coder#31463 and coder/coder#31461 fit
together
* coder/coder#31600 makes `provisioner/terraform/terraform_SHA256SUMS`
(a verbatim copy of HashiCorp's signed file) the one place the bundled
Terraform checksums live. `Dockerfile.base` reads it, and `install.sh`
downloads it from the Coder repo at install time.
* coder/coder#31463 adds `scripts/update_terraform_version.sh`. On
branches with the checksum file it just replaces that file. On older
branches it edits `install.sh` and `Dockerfile.base` directly.
* coder/coder#31461 (stacked on coder/coder#31463) runs the script
weekly for `main` and the supported release branches. It needed no
changes for the checksum file.
None of the three touch the same files, and merging all three onto
`main` is conflict-free. On that combined tree, the bump script (run
before `main` moved to 1.17.2) took 1.16.7 to 1.17.1 by replacing the
checksum file, the new Go test passes, and `actionlint` and `shellcheck`
pass. They can merge in any order. Merge coder/coder#31461 after
coder/coder#31463, retargeted to `main`.
Opened by Superblink on behalf of @droopy4096.
---------
Co-authored-by: coder[bot] <137810501+coder[bot]@users.noreply.github.com>
Co-authored-by: Dmytro Makovey <dmitry@coder.com>
Co-authored-by: Lisa Fiander <lisa@coder.com>
Co-authored-by: Dmitry Makovey <dmakovey@yahoo.com>
160 lines
3.8 KiB
Bash
Executable File
160 lines
3.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# This script builds a Docker image of Coder containing the given binary, for
|
|
# the given architecture. Only linux binaries are supported at this time.
|
|
#
|
|
# Usage: ./build_docker.sh --arch amd64 [--version 1.2.3] [--target image_tag] [--build-base image_tag] [--push] path/to/coder
|
|
#
|
|
# The --arch parameter is required and accepts a Golang arch specification. It
|
|
# will be automatically mapped to a suitable architecture that Docker accepts
|
|
# before being passed to `docker buildx build`.
|
|
#
|
|
# The --build-base parameter is optional and specifies to build the base image
|
|
# in Dockerfile.base instead of pulling a copy from the registry. The string
|
|
# value is the tag to use for the built image (not pushed). This also consumes
|
|
# $CODER_IMAGE_BUILD_BASE_TAG for easily forcing a fresh build in CI.
|
|
#
|
|
# The default base image can be controlled via $CODER_BASE_IMAGE_TAG.
|
|
#
|
|
# The image will be built and tagged against the image tag returned by
|
|
# ./image_tag.sh unless a --target parameter is supplied.
|
|
#
|
|
# If no version is specified, defaults to the version from ./version.sh.
|
|
#
|
|
# If the --push parameter is supplied, the image will be pushed.
|
|
#
|
|
# Prints the image tag on success.
|
|
|
|
set -euo pipefail
|
|
# shellcheck source=scripts/lib.sh
|
|
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
|
|
|
|
DEFAULT_BASE="${CODER_BASE_IMAGE_TAG:-ghcr.io/coder/coder-base:latest}"
|
|
|
|
arch=""
|
|
image_tag=""
|
|
build_base="${CODER_IMAGE_BUILD_BASE_TAG:-}"
|
|
version=""
|
|
push=0
|
|
|
|
args="$(getopt -o "" -l arch:,target:,build-base:,version:,push -- "$@")"
|
|
eval set -- "$args"
|
|
while true; do
|
|
case "$1" in
|
|
--arch)
|
|
arch="$2"
|
|
shift 2
|
|
;;
|
|
--target)
|
|
image_tag="$2"
|
|
shift 2
|
|
;;
|
|
--version)
|
|
version="$2"
|
|
shift 2
|
|
;;
|
|
--build-base)
|
|
build_base="$2"
|
|
shift 2
|
|
;;
|
|
--push)
|
|
push=1
|
|
shift
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
*)
|
|
error "Unrecognized option: $1"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ "$arch" == "" ]]; then
|
|
error "The --arch parameter is required"
|
|
fi
|
|
|
|
# Check dependencies
|
|
dependencies docker
|
|
|
|
# Remove the "v" prefix.
|
|
version="${version#v}"
|
|
if [[ "$version" == "" ]]; then
|
|
version="$(execrelative ./version.sh)"
|
|
fi
|
|
|
|
if [[ "$image_tag" == "" ]]; then
|
|
image_tag="$(execrelative ./image_tag.sh --arch "$arch" --version="$version")"
|
|
fi
|
|
|
|
if [[ "$#" != 1 ]]; then
|
|
error "Exactly one argument must be provided to this script, $# were supplied"
|
|
fi
|
|
if [[ ! -f "$1" ]]; then
|
|
error "File '$1' does not exist or is not a regular file"
|
|
fi
|
|
input_file="$(realpath "$1")"
|
|
|
|
# Remap the arch from Golang to Docker.
|
|
declare -A arch_map=(
|
|
[amd64]="linux/amd64"
|
|
[arm64]="linux/arm64"
|
|
[arm]="linux/arm/v7"
|
|
[armv7]="linux/arm/v7"
|
|
)
|
|
if [[ "${arch_map[$arch]+exists}" != "" ]]; then
|
|
arch="${arch_map[$arch]}"
|
|
fi
|
|
|
|
# Make temporary dir where all source files intended to be in the image will be
|
|
# hardlinked from.
|
|
cdroot
|
|
temp_dir="$(TMPDIR="$(dirname "$input_file")" mktemp -d)"
|
|
ln "$input_file" "$temp_dir/coder"
|
|
ln ./scripts/Dockerfile.base "$temp_dir/"
|
|
ln ./provisioner/terraform/terraform_SHA256SUMS "$temp_dir/"
|
|
ln ./scripts/Dockerfile "$temp_dir/"
|
|
|
|
cd "$temp_dir"
|
|
|
|
export DOCKER_BUILDKIT=1
|
|
|
|
base_image="$DEFAULT_BASE"
|
|
if [[ "$build_base" != "" ]]; then
|
|
log "--- Building base Docker image for $arch ($build_base)"
|
|
docker build \
|
|
--platform "$arch" \
|
|
--tag "$build_base" \
|
|
--no-cache \
|
|
-f Dockerfile.base \
|
|
. 1>&2
|
|
|
|
base_image="$build_base"
|
|
else
|
|
docker pull --platform "$arch" "$base_image" 1>&2
|
|
fi
|
|
|
|
log "--- Building Docker image for $arch ($image_tag)"
|
|
|
|
docker build \
|
|
--platform "$arch" \
|
|
--build-arg "BASE_IMAGE=$base_image" \
|
|
--build-arg "CODER_VERSION=$version" \
|
|
--no-cache \
|
|
--tag "$image_tag" \
|
|
-f Dockerfile \
|
|
. 1>&2
|
|
|
|
cdroot
|
|
rm -rf "$temp_dir"
|
|
|
|
if [[ "$push" == 1 ]]; then
|
|
log "--- Pushing Docker image for $arch ($image_tag)"
|
|
docker push "$image_tag" 1>&2
|
|
fi
|
|
|
|
# SBOM generation and attestation moved to the GitHub workflow
|
|
|
|
echo "$image_tag"
|