diff --git a/docs/using/actions.md b/docs/using/actions.md index b8da339..8110a60 100644 --- a/docs/using/actions.md +++ b/docs/using/actions.md @@ -5,16 +5,19 @@ Installs Nix (pinned installer, pinned `install-nix-action` commit) and writes `nix.conf` with our substituters, trusted keys, and cache credentials. -| Input | Default | Description | -| --------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------- | -| `github_token` | — | authenticates GitHub flake inputs; they are rate-limited without one | -| `cache_pull_token` | `""` | pull token for the GenLayer cache; empty warns and relies on the runner's own netrc, if any | -| `enable_genlayerlabs_cache` | `true` | set `false` to measure a cold build, or to avoid paths a bad push may have poisoned | -| `cache_url` | `https://nix-cache.ygr.ai/genlayer` | substituter URL | -| `cache_public_key` | `genlayer:hMvP8Bk…` | trusted public key | -| `extra_substituters` | `""` | whitespace-separated; each needs its key below | -| `extra_trusted_public_keys` | `""` | whitespace-separated | -| `extra_nix_config` | `""` | lines appended verbatim to `nix.conf`; later lines win | +| Input | Default | Description | +| -------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------- | +| `github_token` | — | authenticates GitHub flake inputs; they are rate-limited without one | +| `cache_pull_token` | `""` | pull token for the GenLayer cache; empty warns and relies on the runner's own netrc, if any | +| `cache_pull_token_ssm_parameter` | `""` | SSM parameter holding the pull token, read in-job with `aws_role_arn`; set this or `cache_pull_token`, not both | +| `aws_role_arn` | `""` | role the job assumes with its OIDC token to read the SSM parameter; needs `id-token: write` | +| `aws_region` | `us-east-1` | region of the SSM parameter | +| `enable_genlayerlabs_cache` | `true` | set `false` to measure a cold build, or to avoid paths a bad push may have poisoned | +| `cache_url` | `https://nix-cache.ygr.ai/genlayer` | substituter URL | +| `cache_public_key` | `genlayer:hMvP8Bk…` | trusted public key | +| `extra_substituters` | `""` | whitespace-separated; each needs its key below | +| `extra_trusted_public_keys` | `""` | whitespace-separated | +| `extra_nix_config` | `""` | lines appended verbatim to `nix.conf`; later lines win | Outputs: @@ -52,19 +55,31 @@ Notes: is what the warning is for. - The last step probes the cache and warns if the token does not work. It never fails the job. +- With `cache_pull_token_ssm_parameter`, the token comes from SSM through + `support/scripts/read-ssm-token.sh`: the job's GitHub OIDC token is exchanged + for `aws_role_arn` (devexp-tf-infra, `github-oidc--nix-cache-tokens`) + and the parameter is read and masked. The cache host rotates it + (devexp-nix-cache, `ciTokens`), so it does not expire unnoticed. A failed + read warns and builds from source, like a bad token. A job without an OIDC + token fails, because it lacks `permissions: id-token: write`, unless it is a + fork's pull request, which never gets one and only warns. Needs the `aws` + CLI, `curl` and `jq` on the runner (GitHub-hosted runners have all three). ## nix-cache-push Uploads paths this job built locally. -| Input | Default | Description | -| ---------- | -------------------------- | -------------------------------------------------------------------------- | -| `token` | `""` | push token; empty skips the step entirely | -| `cache` | `genlayer` | cache name on the server | -| `endpoint` | `https://nix-cache.ygr.ai` | attic server | -| `include` | `genlayer\|genvm` | regex on the store path name, hash stripped | -| `exclude` | `""` | regex on the full store path; empty disables | -| `verify` | `"false"` | `"true"` fails the job unless every pushed path is in the cache afterwards | +| Input | Default | Description | +| --------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------- | +| `token` | `""` | push token; empty skips the step entirely | +| `token_ssm_parameter` | `""` | SSM parameter holding the push token, read in-job with `aws_role_arn`; set this or `token`, not both | +| `aws_role_arn` | `""` | role the job assumes with its OIDC token to read the SSM parameter; needs `id-token: write` | +| `aws_region` | `us-east-1` | region of the SSM parameter | +| `cache` | `genlayer` | cache name on the server | +| `endpoint` | `https://nix-cache.ygr.ai` | attic server | +| `include` | `genlayer\|genvm` | regex on the store path name, hash stripped | +| `exclude` | `""` | regex on the full store path; empty disables | +| `verify` | `"false"` | `"true"` fails the job unless every pushed path is in the cache afterwards | Notes: @@ -83,5 +98,9 @@ Notes: pull): any answer but 200 fails the job and names the path and the HTTP code. This is what makes an expired push token visible — without it a refused push is only a warning in a green job. It needs `curl`. +- `token_ssm_parameter` reads the push token from SSM the same way as + `nix-setup`'s `cache_pull_token_ssm_parameter`. A failed read is a failed + upload: a warning, or an error with `verify`. A fork's pull request skips the + step; any other job without an OIDC token fails. - Requires `jq` and a flake in `$GITHUB_WORKSPACE` (`--inputs-from .` resolves the attic client through your lock file rather than the mutable registry). diff --git a/nix-cache-push/action.yaml b/nix-cache-push/action.yaml index 64fb72c..ead88bd 100644 --- a/nix-cache-push/action.yaml +++ b/nix-cache-push/action.yaml @@ -9,6 +9,27 @@ inputs: from a fork gets no secrets, and such a run must still pass. required: false default: "" + token_ssm_parameter: + description: >- + SSM SecureString parameter holding the push token, read in-job with + `aws_role_arn` instead of passing `token`. The cache host keeps it fresh, + so it never expires unnoticed. Set one of the two, not both. A fork's + pull request gets no OIDC token and skips the step, like an empty `token`; + any other job without one fails, because it lacks `id-token: write`. A + failed read is a failed upload: a warning, or with `verify` an error. + required: false + default: "" + aws_role_arn: + description: >- + IAM role the job assumes with its GitHub OIDC token to read the SSM + parameter above (devexp-tf-infra, github-oidc--nix-cache-tokens). + Required with that input; the job needs `permissions: id-token: write`. + required: false + default: "" + aws_region: + description: region of the SSM parameter + required: false + default: us-east-1 cache: description: cache name on the server required: false @@ -44,9 +65,13 @@ runs: steps: # No `-x`: the token is passed to `attic login` as an argument. - shell: bash -eo pipefail {0} - if: ${{ inputs.token != '' }} + if: ${{ inputs.token != '' || inputs.token_ssm_parameter != '' }} env: NIX_CACHE_PUSH_TOKEN: ${{ inputs.token }} + PUSH_TOKEN_SSM_PARAMETER: ${{ inputs.token_ssm_parameter }} + AWS_ROLE_ARN: ${{ inputs.aws_role_arn }} + AWS_REGION_INPUT: ${{ inputs.aws_region }} + FORK_PR: ${{ github.event.pull_request.head.repo.full_name != '' && github.event.pull_request.head.repo.full_name != github.repository }} CACHE: ${{ inputs.cache }} ENDPOINT: ${{ inputs.endpoint }} INCLUDE: ${{ inputs.include }} @@ -68,6 +93,38 @@ runs: exit 0 } + if [ -n "$PUSH_TOKEN_SSM_PARAMETER" ]; then + if [ -n "$NIX_CACHE_PUSH_TOKEN" ]; then + echo "::error::set token or token_ssm_parameter, not both" + exit 1 + fi + if [ -z "$AWS_ROLE_ARN" ]; then + echo "::error::token_ssm_parameter needs aws_role_arn" + exit 1 + fi + token_file="${RUNNER_TEMP:-/tmp}/attic-push-token" + rc=0 + "$GITHUB_ACTION_PATH/../support/scripts/read-ssm-token.sh" \ + "$AWS_ROLE_ARN" "$AWS_REGION_INPUT" "$PUSH_TOKEN_SSM_PARAMETER" "$token_file" || rc=$? + case "$rc" in + 0) + NIX_CACHE_PUSH_TOKEN="$(cat "$token_file")" + rm -f "$token_file" + ;; + 3) + if [ "$FORK_PR" = "true" ]; then + echo "a fork's pull request gets no OIDC token; nothing pushed" + exit 0 + fi + echo "::error::no OIDC token to read $PUSH_TOKEN_SSM_PARAMETER; add 'permissions: id-token: write' to the job" + exit 1 + ;; + *) + give_up "could not read the push token from $PUSH_TOKEN_SSM_PARAMETER; nothing pushed" + ;; + esac + fi + attic_link="${RUNNER_TEMP:-/tmp}/attic-client" # An out-link rather than `nix run`: it is a gcroot, and a second call # in the same job skips both evaluation and substitution. diff --git a/nix-setup/action.yaml b/nix-setup/action.yaml index 05432c9..27d4ad2 100644 --- a/nix-setup/action.yaml +++ b/nix-setup/action.yaml @@ -13,6 +13,27 @@ inputs: request from a fork gets no secrets. required: false default: "" + cache_pull_token_ssm_parameter: + description: >- + SSM SecureString parameter holding the pull token, read in-job with + `aws_role_arn` instead of passing `cache_pull_token`. The cache host keeps + it fresh, so it never expires unnoticed. Set one of the two, not both. A + fork's pull request gets no OIDC token: it then warns like an empty + `cache_pull_token`. A failed read also only warns: the job builds from + source, as with a bad token. + required: false + default: "" + aws_role_arn: + description: >- + IAM role the job assumes with its GitHub OIDC token to read the SSM + parameter above (devexp-tf-infra, github-oidc--nix-cache-tokens). + Required with that input; the job needs `permissions: id-token: write`. + required: false + default: "" + aws_region: + description: region of the SSM parameter + required: false + default: us-east-1 enable_genlayerlabs_cache: description: >- substitute from our own cache. Turn off to measure a cold build, or to @@ -72,10 +93,46 @@ runs: CACHE_URL: ${{ inputs.cache_url }} CACHE_KEY: ${{ inputs.cache_public_key }} CACHE_PULL_TOKEN: ${{ inputs.cache_pull_token }} + PULL_TOKEN_SSM_PARAMETER: ${{ inputs.cache_pull_token_ssm_parameter }} + AWS_ROLE_ARN: ${{ inputs.aws_role_arn }} + AWS_REGION_INPUT: ${{ inputs.aws_region }} + FORK_PR: ${{ github.event.pull_request.head.repo.full_name != '' && github.event.pull_request.head.repo.full_name != github.repository }} EXTRA_SUBSTITUTERS: ${{ inputs.extra_substituters }} EXTRA_KEYS: ${{ inputs.extra_trusted_public_keys }} EXTRA_CONFIG: ${{ inputs.extra_nix_config }} run: | + if [ -n "$PULL_TOKEN_SSM_PARAMETER" ]; then + if [ -n "$CACHE_PULL_TOKEN" ]; then + echo "::error::set cache_pull_token or cache_pull_token_ssm_parameter, not both" + exit 1 + fi + if [ -z "$AWS_ROLE_ARN" ]; then + echo "::error::cache_pull_token_ssm_parameter needs aws_role_arn" + exit 1 + fi + token_file="${RUNNER_TEMP:-/tmp}/genlayer-nix-pull-token" + rc=0 + "$GITHUB_ACTION_PATH/../support/scripts/read-ssm-token.sh" \ + "$AWS_ROLE_ARN" "$AWS_REGION_INPUT" "$PULL_TOKEN_SSM_PARAMETER" "$token_file" || rc=$? + case "$rc" in + 0) + CACHE_PULL_TOKEN="$(cat "$token_file")" + rm -f "$token_file" + ;; + 3) + # A fork gets no OIDC token by design; any other job without one + # is missing `id-token: write`, a mistake worth failing on. + if [ "$FORK_PR" != "true" ]; then + echo "::error::no OIDC token to read $PULL_TOKEN_SSM_PARAMETER; add 'permissions: id-token: write' to the job" + exit 1 + fi + ;; + *) + echo "::warning::could not read the pull token from $PULL_TOKEN_SSM_PARAMETER; the job builds from source" + ;; + esac + fi + substituters="https://cache.nixos.org/ https://nix-community.cachix.org" keys="cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" keys="$keys nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" @@ -102,7 +159,7 @@ runs: # credentials in /etc/nix/netrc, and overriding netrc-file here would # throw them away. Without any, every lookup 401s, which nix reports # as a miss — hence the warning. - echo "::warning::no cache_pull_token given; the GenLayer cache is private, so unless this runner has its own /etc/nix/netrc the job builds everything from source" + echo "::warning::no pull token; the GenLayer cache is private, so unless this runner has its own /etc/nix/netrc the job builds everything from source" fi fi diff --git a/support/scripts/read-ssm-token.sh b/support/scripts/read-ssm-token.sh new file mode 100755 index 0000000..66a5b7d --- /dev/null +++ b/support/scripts/read-ssm-token.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# Read one SecureString parameter from AWS SSM with the job's GitHub OIDC token, +# for the SSM inputs of nix-setup and nix-cache-push. +# +# read-ssm-token.sh ROLE_ARN REGION PARAMETER OUT_FILE +# +# Writes the value to OUT_FILE (mode 0600) and masks it in the job log. +# Exit 0: read. Exit 3: the job has no OIDC token (a fork's pull request, or a +# job without `permissions: id-token: write`); the caller decides which. Exit 1: +# any other failure, with the reason on stderr. +# +# No `set -x`: the OIDC token, the role credentials and the value all pass +# through this script. None of them reaches argv, which any user can read. +set -euo pipefail + +if [ "$#" -ne 4 ]; then + echo "usage: read-ssm-token.sh ROLE_ARN REGION PARAMETER OUT_FILE" >&2 + exit 1 +fi +role=$1 +region=$2 +parameter=$3 +out=$4 + +if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ]; then + exit 3 +fi + +for tool in aws curl jq; do + if ! command -v "$tool" >/dev/null; then + echo "$tool is not installed on this runner" >&2 + exit 1 + fi +done + +work="$(mktemp -d "${RUNNER_TEMP:-/tmp}/ssm-token.XXXXXX")" +trap 'rm -rf "$work"' EXIT + +# The request token reaches curl through a config on stdin. +if ! printf 'header = "Authorization: bearer %s"\n' "$ACTIONS_ID_TOKEN_REQUEST_TOKEN" | + curl -sSf -K - "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=sts.amazonaws.com" | + jq -er .value >"$work/jwt"; then + echo "could not get an OIDC token from GitHub" >&2 + exit 1 +fi +echo "::add-mask::$(cat "$work/jwt")" + +if ! aws sts assume-role-with-web-identity \ + --region "$region" \ + --role-arn "$role" \ + --role-session-name "nix-cache-${GITHUB_RUN_ID:-local}" \ + --web-identity-token "file://$work/jwt" \ + --duration-seconds 900 \ + --query Credentials --output json >"$work/creds"; then + echo "could not assume $role (is this repository and ref in its trust policy?)" >&2 + exit 1 +fi +key_id="$(jq -r .AccessKeyId "$work/creds")" +secret="$(jq -r .SecretAccessKey "$work/creds")" +session="$(jq -r .SessionToken "$work/creds")" +echo "::add-mask::$secret" +echo "::add-mask::$session" + +if ! value="$(AWS_ACCESS_KEY_ID="$key_id" AWS_SECRET_ACCESS_KEY="$secret" AWS_SESSION_TOKEN="$session" \ + aws ssm get-parameter --region "$region" --name "$parameter" --with-decryption \ + --query Parameter.Value --output text)"; then + echo "could not read $parameter with $role" >&2 + exit 1 +fi +if [ -z "$value" ] || [ "$value" = "CHANGE_ME" ]; then + echo "$parameter still holds its Terraform placeholder; the cache host has not minted it yet" >&2 + exit 1 +fi +echo "::add-mask::$value" + +( + umask 077 + printf '%s' "$value" >"$out" +)