Skip to content

fix(email): make the encrypted mail store restore proof runnable #228

fix(email): make the encrypted mail store restore proof runnable

fix(email): make the encrypted mail store restore proof runnable #228

name: 🔐 Check GitHub Token Broker Policies
# Every repository's GitHub token broker trust policies (.github/chainguard/
# <identity>.sts.yaml, devantler-tech/monorepo ADR 0020) must admit only
# reviewed callers and leave the App to the broker. An organization ruleset
# requires this workflow on every repository's default branch
# (deploy/organization-rulesets/require-github-broker-policies.yaml). The rules
# live in this file, read from this repository's main branch, so the pull
# request under review cannot change them; its files are only data.
permissions: {}
on:
# Required by the organization ruleset only; nothing calls it as a reusable workflow.
### Required Workflow Triggers ###
pull_request:
merge_group:
##################################
jobs:
check:
name: Check broker trust policies
timeout-minutes: 5
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: 🛡️ Harden runner
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
with:
egress-policy: audit
- name: 📑 Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: 🔐 Check broker trust policies
shell: bash
run: |
set -euo pipefail
# Callers: GitHub Actions tokens from the devantler-tech organization,
# matched by its numeric ID, which survives renames and cannot be
# re-registered. No other caller is ever admitted, customers included
# (ADR 0020): the brokers serve the maintainer's own systems only.
readonly actions_issuer=https://token.actions.githubusercontent.com
readonly org_id=178524219
# A shared workflow called from another repository runs on that
# repository's main branch, so its policy admits exactly this subject
# pattern: any devantler-tech repository, main only. A repository
# issues one of two subject forms, set by its OIDC subject setting
# (use_immutable_subject): repo:devantler-tech/<name> or the immutable
# repo:devantler-tech@<owner ID>/<name>@<repository ID>. Both are
# admitted, so no caller is refused for its repository's age.
readonly caller_subject_pattern='repo:devantler-tech(/[A-Za-z0-9._-]+|@178524219/[A-Za-z0-9._-]+@[0-9]+):ref:refs/heads/main'
# botantler-1 and botantler-2 are interchangeable: both may create release
# tags (the organization ruleset restrict-release-tag-creation), so a policy
# names no App and the broker mints from whichever has more rate limit left.
# A policy may pin an App only while just that App holds a permission the
# policy needs: listed here by this repository's workflow, with the App and
# that permission, which the policy must request. Only botantler-1 holds
# organization_custom_properties.
readonly app_pins='{"apply-custom-properties.yaml": {"app": "botantler-1", "permission": "organization_custom_properties"}}'
# Privileged permissions (administration, members and any
# organization_* permission) are granted only to this repository's own
# workflows, on main, each with exactly a reviewed set. A policy that
# asks for one must live in devantler-tech/.github, name that exact
# subject and workflow, and request exactly one listed set.
readonly dotgithub_subject='repo:devantler-tech/.github:ref:refs/heads/main'
readonly dotgithub_workflow_re='^devantler-tech/\\\.github/\\\.github/workflows/([a-z0-9-]+)\\\.yaml@refs/heads/main$'
readonly privileged_grants='{
"apply-custom-properties.yaml": [
{"organization_administration": "write", "organization_custom_properties": "admin"},
{"organization_custom_properties": "read"}
],
"apply-workflow-execution-policies.yaml": [{"organization_administration": "write"}],
"governance-audits.yaml": [{"administration": "read", "organization_administration": "read"}],
"organization-settings-audit.yaml": [{"organization_administration": "read"}],
"repository-admin-team-audit.yaml": [{"administration": "read", "organization_administration": "read"}],
"repository-coverage-check.yaml": [{"organization_administration": "read"}]
}'
# A shared workflow that needs a privileged permission in every caller
# is listed here instead. Its policy must live in devantler-tech/.github,
# admit exactly caller_subject_pattern, name the workflow on main or at
# a pinned commit, scope the token to the caller, name no App, and
# request exactly one listed set.
readonly cross_repository_grants='{
"scan-for-todo-comments": [{"organization_projects": "write"}]
}'
# The broker decodes a policy through JSON with Go's case-insensitive
# field matching (sigs.k8s.io/yaml), so only the exact field names it
# defines are allowed: any other spelling could set a field unseen here.
readonly fields='["issuer","issuer_pattern","subject","subject_pattern","audience","audience_pattern","claim_pattern","permissions","app","app_pattern","caller_repository","repositories","repository_ids"]'
for tool in yq jq; do
command -v "${tool}" >/dev/null || { echo "::error::${tool} is not installed"; exit 2; }
done
# The broker reads <identity>.sts.yaml for any identity, including one
# that starts with a dot.
shopt -s nullglob dotglob
policies=(.github/chainguard/*.sts.yaml)
shopt -u nullglob dotglob
failures=0
fail() {
echo "::error file=$1::$2"
failures=$((failures + 1))
}
for policy in "${policies[@]}"; do
if ! documents="$(yq ea '[.] | length' "${policy}" 2>/dev/null)"; then
fail "${policy}" 'is not valid YAML'
continue
fi
if [[ "${documents}" != 1 ]]; then
fail "${policy}" "holds ${documents} YAML documents, not one"
continue
fi
# Anchors, aliases, merge keys and duplicate keys are where this
# check's YAML reader and the broker's can disagree about a value.
ambiguous="$(yq '[.. | select((anchor != "") or (kind == "alias"))] | length' "${policy}")"
ambiguous=$((ambiguous + $(yq '[.. | select(tag == "!!map") | keys[] | select(. == "<<")] | length' "${policy}")))
if [[ "${ambiguous}" != 0 || "$(yq '[.. | select(tag == "!!map") | (keys | length) != (keys | unique | length)] | any' "${policy}")" != false ]]; then
fail "${policy}" 'uses YAML anchors, aliases, merge keys or duplicate keys, which the broker may read differently'
continue
fi
json="$(yq -o=json -I=0 '.' "${policy}")"
if [[ "$(jq -r 'type' <<<"${json}")" != object ]]; then
fail "${policy}" 'is not a mapping'
continue
fi
unknown="$(jq -r --argjson fields "${fields}" '[keys[] | select(. as $k | $fields | index($k) | not)] | join(", ")' <<<"${json}")"
[[ -z "${unknown}" ]] || fail "${policy}" "uses fields the broker does not define in this exact spelling: ${unknown}"
if [[ "$(jq 'has("issuer_pattern") or has("app_pattern")' <<<"${json}")" != false ]]; then
fail "${policy}" 'uses issuer_pattern or app_pattern; name the issuer and App exactly'
fi
if [[ "$(jq -r 'if (.issuer | type) == "string" then .issuer else "" end' <<<"${json}")" != "${actions_issuer}" ]]; then
fail "${policy}" "must accept exactly the GitHub Actions issuer ${actions_issuer}"
fi
if [[ "$(jq '(.claim_pattern // {}) | type == "object" and all(.[]; type == "string")' <<<"${json}")" != true ]]; then
fail "${policy}" 'claim_pattern must map claim names to strings'
continue
fi
owner="$(jq -r '.claim_pattern.repository_owner_id // ""' <<<"${json}")"
if [[ "${owner}" != "${org_id}" ]]; then
fail "${policy}" "must pin claim_pattern.repository_owner_id to the string \"${org_id}\" (devantler-tech); no other caller is admitted"
fi
# The broker passes the policy's permissions to GitHub as given, and
# GitHub can read an empty set as every permission the installation
# holds. So every policy must name its permissions.
if [[ "$(jq '(.permissions | type) == "object" and (.permissions | length) > 0' <<<"${json}")" != true ]]; then
fail "${policy}" 'must name at least one permission; an empty set can mean every permission the App has'
continue
fi
if [[ "$(jq '.permissions | all(to_entries[]; (.key | test("^[a-z_]+$")) and (.value | IN("read", "write", "admin")))' <<<"${json}")" != true ]]; then
fail "${policy}" 'permissions must use the canonical lowercase names, each read, write or admin'
continue
fi
app="$(jq -r 'if (.app | type) == "string" then .app elif has("app") then "?" else "" end' <<<"${json}")"
workflow="$(jq -r '.claim_pattern.job_workflow_ref // ""' <<<"${json}")"
caller_scoped="$(jq '.caller_repository == true and (has("repositories") | not) and (has("repository_ids") | not)' <<<"${json}")"
privileged="$(jq -c '(.permissions // {}) | with_entries(select(.key == "administration" or .key == "members" or (.key | startswith("organization_"))))' <<<"${json}")"
if [[ "${privileged}" != '{}' ]]; then
listed=false
if [[ "${GITHUB_REPOSITORY:-}" == devantler-tech/.github &&
"$(jq -r '.subject // ""' <<<"${json}")" == "${dotgithub_subject}" &&
"$(jq 'has("subject_pattern")' <<<"${json}")" == false &&
"${workflow}" =~ ${dotgithub_workflow_re} ]]; then
listed="$(jq --arg file "${BASH_REMATCH[1]}.yaml" --argjson grants "${privileged_grants}" --argjson asked "${privileged}" \
'($grants[$file] // []) | any(. == $asked)' <<<'null')"
fi
if [[ "${listed}" != true && "${GITHUB_REPOSITORY:-}" == devantler-tech/.github &&
"$(jq -r '.subject_pattern // ""' <<<"${json}")" == "${caller_subject_pattern}" &&
"$(jq 'has("subject")' <<<"${json}")" == false &&
"${caller_scoped}" == true && "${app}" == "" ]]; then
for name in $(jq -r 'keys[]' <<<"${cross_repository_grants}"); do
# The whole permission set must be the listed one, not just its privileged part.
[[ "${workflow}" == "devantler-tech/\\.github/\\.github/workflows/${name}\\.yaml@([0-9a-f]{40}|refs/heads/main)" ]] || continue
listed="$(jq --arg name "${name}" --argjson grants "${cross_repository_grants}" --argjson asked "$(jq -c '.permissions' <<<"${json}")" \
'($grants[$name] // []) | any(. == $asked)' <<<'null')"
done
fi
[[ "${listed}" == true ]] ||
fail "${policy}" "grants privileged permissions $(jq -r 'keys | join(", ")' <<<"${privileged}") outside the reviewed allowlist: only devantler-tech/.github's own policies, for a listed workflow on main or a listed shared workflow's caller policy, with exactly its listed set"
fi
if [[ -n "${app}" ]]; then
pinned=""
if [[ "${GITHUB_REPOSITORY:-}" == devantler-tech/.github &&
"$(jq -r '.subject // ""' <<<"${json}")" == "${dotgithub_subject}" &&
"${workflow}" =~ ${dotgithub_workflow_re} ]]; then
pinned="$(jq -r --arg file "${BASH_REMATCH[1]}.yaml" --argjson pins "${app_pins}" \
'$pins[$file] as $pin | if $pin != null and ((.permissions // {}) | has($pin.permission)) then $pin.app else "" end' <<<"${json}")"
fi
[[ "${app}" == "${pinned}" ]] ||
fail "${policy}" "pins App ${app}; name no App, so the broker mints from botantler-1 or botantler-2. Only a listed workflow that requests its listed permission may pin the one App that holds it"
fi
done
if ((failures > 0)); then
echo "::error::${failures} problem(s) in ${#policies[@]} broker trust policies"
exit 1
fi
echo "${#policies[@]} broker trust policies admit only reviewed callers and leave the App to the broker"
# Callers pin devantler-tech/.github's shared workflows by commit, so their
# broker policies admit job_workflow_ref …@<commit>. GitHub resolves such a
# commit even when it never merged: an unmerged branch, or a commit pushed
# to a fork of this repository (an impostor commit). So every pinned call
# must name a commit on this repository's main branch. Calls are read
# through YAML, so quoting and escapes cannot hide one, and matched without
# regard to case, as GitHub matches repository names.
- name: 🔗 Check shared workflow pins are merged
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
readonly source=devantler-tech/.github
for tool in yq gh; do
command -v "${tool}" >/dev/null || { echo "::error::${tool} is not installed"; exit 2; }
done
# Dot-prefixed workflow files count too: nothing in the workflow contract excludes them.
shopt -s nullglob dotglob
workflows=(.github/workflows/*.yaml .github/workflows/*.yml)
shopt -u nullglob dotglob
pins=()
for workflow in "${workflows[@]}"; do
if ! calls="$(yq -r '(.jobs // {}) | .[] | .uses // ""' "${workflow}" 2>/dev/null)"; then
echo "::error file=${workflow}::is not valid YAML, so its shared workflow pins cannot be checked"
exit 2
fi
while IFS= read -r call; do
shopt -s nocasematch
if [[ "${call}" =~ ^devantler-tech/\.github/\.github/workflows/[^@]+@([0-9a-f]{40})$ ]]; then
pins+=("${workflow}|$(tr '[:upper:]' '[:lower:]' <<<"${BASH_REMATCH[1]}")")
fi
shopt -u nocasematch
done <<<"${calls}"
done
declare -A verdict=()
failures=0
unknown=0
for entry in ${pins[@]+"${pins[@]}"}; do
workflow="${entry%%|*}"
commit="${entry#*|}"
if [[ -z "${verdict[${commit}]:-}" ]]; then
status=unknown
for attempt in 1 2 3; do
# main...<commit> is "behind" or "identical" exactly when the commit
# is in main's history, and then lists no commits or files.
if out="$(gh api "repos/${source}/compare/main...${commit}" --jq .status 2>&1)"; then
status="${out}"
break
fi
if grep -qE 'HTTP 404|HTTP 422' <<<"${out}"; then
status=missing
break
fi
sleep $((attempt * 5))
done
verdict[${commit}]="${status}"
fi
case "${verdict[${commit}]}" in
behind | identical) ;;
unknown)
echo "::error file=${workflow}::could not check whether ${commit} is on ${source} main"
unknown=$((unknown + 1))
;;
*)
echo "::error file=${workflow}::pins ${source} at ${commit}, which is not a commit on its main branch (${verdict[${commit}]})"
failures=$((failures + 1))
;;
esac
done
if ((failures > 0)); then
echo "::error::${failures} shared workflow pin(s) name a commit that is not on ${source} main"
exit 1
fi
if ((unknown > 0)); then
exit 2
fi
echo "${#pins[@]} pinned shared workflow call(s) name commits on ${source} main"