Fix Task 11 review: keygen, safer prepare-apps
Add tools/keygen so operators can make BASE_ASSERTION_KEY, with a round trip through config.Load. prepare-apps.sh now stops when the releases or apps lookup fails, checks the account's app list before claiming a name, matches the version field exactly, and refuses an ambiguous release dir (pass a version). Add a fake-carlos script test, run by make ci.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7 files changed,
+249
−42
Makefile+6 −2README.md+8 −6docs/live-check.md+4 −3scripts/prepare-apps.sh+52 −31scripts/prepare-apps_test.sh+104 −0tools/keygen/main.go+34 −0tools/keygen/main_test.go+41 −0
diff --git a/Makefile b/Makefile| index a69a8f1..951a4b0 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -12,7 +12,7 @@ RELEASE_VERSION = $(shell git rev-parse --short HEAD) |
| # One target at a time, even under make -j. |
| .NOTPARALLEL: |
| -.PHONY: fonts build release test browser vet fmt-check staticcheck govulncheck gitleaks budget ci |
| +.PHONY: script-test fonts build release test browser vet fmt-check staticcheck govulncheck gitleaks budget ci |
| RASTRILLO := go run amadan.net/rastrillo/rastrillo/cmd/rastrillo |
| @@ -69,8 +69,12 @@ gitleaks: |
| echo "gitleaks: not the root of a git repository, nothing committed to scan"; \ |
| fi |
| +# Tests the operator script against a fake carlos. |
| +script-test: |
| + scripts/prepare-apps_test.sh |
| + |
| # ci is the one gate: what a runner executes and what you run before pushing. |
| -ci: build vet fmt-check staticcheck govulncheck gitleaks budget test |
| +ci: build vet fmt-check staticcheck govulncheck gitleaks budget script-test test |
| # Fetches Timeless Sans, which the licence keeps out of the repository. |
| fonts: |
diff --git a/README.md b/README.md| index 8114016..ac22778 100644 |
| --- a/README.md |
| +++ b/README.md |
| @@ -41,9 +41,9 @@ Everything is an environment variable. `BASE_ORIGIN`, `BASE_OWNER` and |
| - `BASE_SLUG` names the apps' hostnames: `<slug>-<appid>.<BASE_APPS_DOMAIN>`. |
| Lowercase letters, digits and `-`, at most 19 characters (default `base`). |
| - `BASE_TIMEZONE` is where dates are shown (default `Europe/London`). |
| -- `BASE_ASSERTION_KEY` is Base's signing key, base64 of rastrillo's |
| - `crypto.MarshalKeypair`. Without it, no one can open an installed app. |
| - Keep it stable across restarts. |
| +- `BASE_ASSERTION_KEY` is Base's signing key. `go run ./tools/keygen` prints |
| + a fresh one. Without it, no one can open an installed app. Keep it stable |
| + across restarts: apps check sign-ins against its public half. |
| - `BASE_ALLOW_LOGGED_MAIL=1` prints sign-in links instead of sending them. |
| Local only; Base refuses it on a non-loopback origin. |
| @@ -80,11 +80,13 @@ you add an app to the catalogue. The Platform needs each app to exist, have a |
| release, and have instances enabled before Base is allowed to create one for a |
| team: |
| - scripts/prepare-apps.sh <account> releases |
| + scripts/prepare-apps.sh <account> releases [<version>] |
| -For every app in `internal/catalog/catalog.json` it claims the name, deploys |
| +Pass the version (the commit in the binary names) if `releases/` holds more |
| +than one build; the script refuses to guess. For every app in `internal/catalog/catalog.json` it claims the name, deploys |
| the binary to `stable`, and enables instances. Run it again if something |
| -fails halfway; steps already done are skipped. You need to be logged in with |
| +fails halfway; steps already done are skipped. It stops if the account |
| +doesn't list an app but the name can't be claimed, because someone else owns it. You need to be logged in with |
| `carlos auth login` first. |
| ## Writing an app Base can install |
diff --git a/docs/live-check.md b/docs/live-check.md| index dc0572a..b1d549b 100644 |
| --- a/docs/live-check.md |
| +++ b/docs/live-check.md |
| @@ -10,9 +10,10 @@ Account: ______________________ Date: ______________ Commit: ______________ |
| Result: |
| 2. Run `scripts/prepare-apps.sh <account> releases`. Run it a second time and |
| - confirm it finishes and skips what is done (this also checks the "already |
| - exists" wording `apps create` and `instances enable` give, which the script |
| - matches on). |
| + confirm it finishes and skips what is done. This also checks two guesses |
| + in the script: that the first column of `carlos apps list` is the app |
| + name, and that the first column of `carlos releases` is the version. |
| + Also confirm a second `carlos instances enable` is harmless. |
| Result: |
diff --git a/scripts/prepare-apps.sh b/scripts/prepare-apps.sh| index 14c419e..bd08b3c 100755 |
| --- a/scripts/prepare-apps.sh |
| +++ b/scripts/prepare-apps.sh |
| @@ -2,23 +2,29 @@ |
| # One-time operator setup: put every app in Base's catalogue on a CARLOS |
| # account, so that Base can install it for a team later. |
| # |
| -# scripts/prepare-apps.sh <account> <release-dir> |
| +# scripts/prepare-apps.sh <account> <release-dir> [<version>] |
| # |
| # <release-dir> holds the binaries `make release` built, named |
| -# <app>-<goos>-<goarch>-<version> (releases/ by default). For each app id in |
| -# internal/catalog/catalog.json this claims the app name, deploys the binary |
| -# to the stable channel, and enables instances. It stops at the first failure. |
| -# Run it again after a failure: finished steps are skipped. |
| +# <app>-<goos>-<goarch>-<version> (releases/ by default). Give <version> to |
| +# pick a build. Without it, the directory must hold exactly one build of each |
| +# app, so an old build can't be promoted by accident. |
| +# |
| +# For each app id in internal/catalog/catalog.json this claims the app name |
| +# (unless the account already has it), deploys the binary to the stable |
| +# channel (promotes it if that version is already shipped), and enables |
| +# instances. It stops at the first failure. Run it again after a failure: |
| +# finished steps are skipped. |
| # |
| # You need `carlos auth login` first, against the console that hosts <account>. |
| set -euo pipefail |
| -if [ $# -ne 2 ]; then |
| - echo "usage: $0 <account> <release-dir>" >&2 |
| +if [ $# -lt 2 ] || [ $# -gt 3 ]; then |
| + echo "usage: $0 <account> <release-dir> [<version>]" >&2 |
| exit 2 |
| fi |
| account=$1 |
| releases=$2 |
| +want_version=${3:-} |
| root=$(cd -- "$(dirname -- "$0")/.." && pwd) |
| catalog=$root/internal/catalog/catalog.json |
| @@ -29,35 +35,50 @@ command -v carlos >/dev/null || { echo "carlos is not on PATH" >&2; exit 1; } |
| ids=$(grep -o '"id": *"[^"]*"' "$catalog" | sed 's/.*: *"\(.*\)"/\1/') |
| [ -n "$ids" ] || { echo "no app ids found in $catalog" >&2; exit 1; } |
| -# run_tolerating_exists runs a command and treats "already ..." as done. |
| -run_tolerating_exists() { |
| - local out |
| - if out=$("$@" 2>&1); then |
| - [ -z "$out" ] || printf '%s\n' "$out" |
| - elif printf '%s' "$out" | grep -qiE 'already (exists|claimed|enabled|taken)|is taken'; then |
| - echo " already done, skipping" |
| - else |
| - printf '%s\n' "$out" >&2 |
| - return 1 |
| - fi |
| -} |
| - |
| +# Decide every binary before touching the account, so ambiguous input |
| +# fails with nothing done. |
| +declare -A bin_of version_of |
| for id in $ids; do |
| - echo "== $id" |
| - |
| - # Newest binary for this app: <id>-linux-arm64-<version>, not .COMMIT files. |
| - bin="" |
| + found=() |
| for f in "$releases/$id"-linux-arm64-*; do |
| case $f in *.COMMIT) continue ;; esac |
| - [ -f "$f" ] && bin=$f |
| + [ -f "$f" ] || continue |
| + if [ -z "$want_version" ] || [ "${f##*-linux-arm64-}" = "$want_version" ]; then |
| + found+=("$f") |
| + fi |
| done |
| - [ -n "$bin" ] || { echo "no $id-linux-arm64-* binary in $releases" >&2; exit 1; } |
| - version=${bin##*-linux-arm64-} |
| + if [ ${#found[@]} -eq 0 ]; then |
| + echo "no $id-linux-arm64-${want_version:-*} binary in $releases" >&2 |
| + exit 1 |
| + fi |
| + if [ ${#found[@]} -gt 1 ]; then |
| + echo "$releases holds ${#found[@]} builds of $id; pass the version to use as a third argument:" >&2 |
| + printf ' %s\n' "${found[@]##*-linux-arm64-}" >&2 |
| + exit 1 |
| + fi |
| + bin_of[$id]=${found[0]} |
| + version_of[$id]=${found[0]##*-linux-arm64-} |
| +done |
| + |
| +# The apps this account already has (the Trash included). A failed listing |
| +# stops the script rather than looking like an empty account. |
| +apps_out=$(carlos apps list --account "$account") |
| - echo "claim the app name" |
| - run_tolerating_exists carlos apps create --app "$id" --account "$account" |
| +for id in $ids; do |
| + echo "== $id" |
| + bin=${bin_of[$id]} |
| + version=${version_of[$id]} |
| + |
| + if printf '%s\n' "$apps_out" | awk -v id="$id" '$1 == id { found = 1 } END { exit !found }'; then |
| + echo "app name already claimed in $account, skipping" |
| + else |
| + echo "claim the app name" |
| + carlos apps create --app "$id" --account "$account" |
| + fi |
| - if carlos releases --app "$id" --account "$account" 2>/dev/null | grep -qF -- "$version"; then |
| + # Is this version already shipped? A failed lookup is not "no". |
| + rel_out=$(carlos releases --app "$id" --account "$account") |
| + if printf '%s\n' "$rel_out" | awk -v v="$version" '$1 == v { found = 1 } END { exit !found }'; then |
| echo "promote $version to stable (already shipped)" |
| carlos promote --app "$id" --account "$account" "$version" stable |
| else |
| @@ -67,6 +88,6 @@ for id in $ids; do |
| fi |
| echo "enable instances" |
| - run_tolerating_exists carlos instances enable --app "$id" --account "$account" |
| + carlos instances enable --app "$id" --account "$account" |
| done |
| echo "done: every catalogue app is ready for Base to install" |
diff --git a/scripts/prepare-apps_test.sh b/scripts/prepare-apps_test.sh| new file mode 100755 |
| index 0000000..329dad7 |
| --- /dev/null |
| +++ b/scripts/prepare-apps_test.sh |
| @@ -0,0 +1,104 @@ |
| +#!/usr/bin/env bash |
| +# Tests for prepare-apps.sh, using a fake `carlos` on PATH. Run directly: |
| +# scripts/prepare-apps_test.sh |
| +set -uo pipefail |
| + |
| +here=$(cd -- "$(dirname -- "$0")" && pwd) |
| +script=$here/prepare-apps.sh |
| +work=$(mktemp -d) |
| +trap 'rm -rf "$work"' EXIT |
| +fails=0 |
| + |
| +# The fake logs every call to $CALLS. Its behaviour comes from the environment: |
| +# FAKE_APPS what `apps list` prints (default: nothing) |
| +# FAKE_LIST_FAIL 1: `apps list` fails |
| +# FAKE_CREATE_FAIL 1: `apps create` fails |
| +# FAKE_RELEASES what `releases` prints |
| +# FAKE_RELEASES_FAIL 1: `releases` fails |
| +mkdir "$work/bin" |
| +cat >"$work/bin/carlos" <<'FAKE' |
| +#!/usr/bin/env bash |
| +echo "$*" >>"$CALLS" |
| +case "$1 $2" in |
| +"apps list") |
| + [ "${FAKE_LIST_FAIL:-}" = 1 ] && { echo "list failed" >&2; exit 1; } |
| + printf '%s\n' "${FAKE_APPS:-}" ;; |
| +"apps create") |
| + [ "${FAKE_CREATE_FAIL:-}" = 1 ] && { echo "app name is taken" >&2; exit 1; } ;; |
| +esac |
| +case "$1" in |
| +releases) |
| + [ "${FAKE_RELEASES_FAIL:-}" = 1 ] && { echo "console unreachable" >&2; exit 1; } |
| + printf 'VERSION LABEL KIND BUILT TAG\n%s\n' "${FAKE_RELEASES:-}" ;; |
| +esac |
| +exit 0 |
| +FAKE |
| +chmod +x "$work/bin/carlos" |
| + |
| +# newdir makes a release dir with one build of each catalogue app. |
| +newdir() { |
| + local d |
| + d=$(mktemp -d "$work/rel.XXXXXX") |
| + for a in welcome scratchpad; do |
| + : >"$d/$a-linux-arm64-aaa111" |
| + : >"$d/$a-linux-arm64-aaa111.COMMIT" |
| + done |
| + echo "$d" |
| +} |
| + |
| +# check <name> <want-exit: zero|nonzero> <want-calls-regex or ''> <not-calls-regex or ''> -- script args... |
| +check() { |
| + local name=$1 want=$2 must=$3 mustnot=$4 |
| + shift 5 |
| + : >"$work/calls" |
| + CALLS=$work/calls PATH=$work/bin:$PATH "$script" "$@" >"$work/out" 2>&1 |
| + local rc=$? ok=1 |
| + if [ "$want" = zero ] && [ $rc -ne 0 ]; then ok=0; fi |
| + if [ "$want" = nonzero ] && [ $rc -eq 0 ]; then ok=0; fi |
| + if [ -n "$must" ] && ! grep -qE "$must" "$work/calls"; then ok=0; fi |
| + if [ -n "$mustnot" ] && grep -qE "$mustnot" "$work/calls"; then ok=0; fi |
| + if [ $ok -eq 1 ]; then |
| + echo "ok $name" |
| + else |
| + echo "FAIL $name (exit $rc)"; sed 's/^/ out: /' "$work/out"; sed 's/^/ call: /' "$work/calls" |
| + fails=$((fails + 1)) |
| + fi |
| +} |
| + |
| +d=$(newdir) |
| + |
| +# A failed releases lookup must stop the script before it deploys. |
| +FAKE_APPS=$'welcome x\nscratchpad x' FAKE_RELEASES_FAIL=1 \ |
| + check "releases lookup failure stops before deploy" nonzero '^releases ' '^deploy ' -- acme "$d" |
| + |
| +# An app that is not in this account's listing, and cannot be created, stops it. |
| +FAKE_APPS='' FAKE_CREATE_FAIL=1 \ |
| + check "app owned elsewhere stops the script" nonzero '^apps create ' '^(deploy|instances) ' -- acme "$d" |
| + |
| +# A failed apps listing stops it too, without trying to create. |
| +FAKE_LIST_FAIL=1 \ |
| + check "apps listing failure stops before create" nonzero '^apps list ' '^(apps create|deploy) ' -- acme "$d" |
| + |
| +# Several builds of one app and no explicit version is ambiguous. |
| +d2=$(newdir) |
| +: >"$d2/welcome-linux-arm64-bbb222" |
| +: >"$d2/scratchpad-linux-arm64-bbb222" |
| +check "ambiguous release dir is refused" nonzero '' '^(apps|deploy|instances|releases) ' -- acme "$d2" |
| +grep -qi 'version' "$work/out" || { echo "FAIL ambiguity message should mention version"; fails=$((fails + 1)); } |
| + |
| +# An explicit version picks the right build out of several. |
| +FAKE_APPS=$'welcome x\nscratchpad x' \ |
| + check "explicit version resolves the ambiguity" zero 'deploy .*--version bbb222 .*welcome-linux-arm64-bbb222' '' -- acme "$d2" bbb222 |
| + |
| +# Version match is exact: aaa111 listed as a prefix of the shipped version is not shipped. |
| +FAKE_APPS=$'welcome x\nscratchpad x' FAKE_RELEASES='aaa1112 - binary now -' \ |
| + check "releases match is exact, not substring" zero '^deploy ' '^promote ' -- acme "$d" |
| + |
| +# Already shipped: promote, don't deploy. Existing apps: no create. |
| +FAKE_APPS=$'welcome x\nscratchpad x' FAKE_RELEASES='aaa111 - binary now -' \ |
| + check "shipped version is promoted, existing app not created" zero '^promote ' '^(deploy|apps create) ' -- acme "$d" |
| + |
| +# Fresh account: create, deploy, enable, for each app. |
| +check "fresh account creates, deploys, enables" zero 'apps create .*scratchpad' '' -- acme "$d" |
| + |
| +[ $fails -eq 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; } |
diff --git a/tools/keygen/main.go b/tools/keygen/main.go| new file mode 100644 |
| index 0000000..9f5de81 |
| --- /dev/null |
| +++ b/tools/keygen/main.go |
| @@ -0,0 +1,34 @@ |
| +// Command keygen prints a fresh BASE_ASSERTION_KEY: Base's signing key, as |
| +// base64 of rastrillo's crypto.MarshalKeypair, which is what config.Load reads. |
| +// |
| +// go run ./tools/keygen |
| +package main |
| + |
| +import ( |
| + "encoding/base64" |
| + "fmt" |
| + "io" |
| + "os" |
| + |
| + "amadan.net/rastrillo/rastrillo/crypto" |
| +) |
| + |
| +func main() { |
| + if err := run(os.Stdout); err != nil { |
| + fmt.Fprintln(os.Stderr, "keygen:", err) |
| + os.Exit(1) |
| + } |
| +} |
| + |
| +func run(w io.Writer) error { |
| + kp, err := crypto.Generate() |
| + if err != nil { |
| + return err |
| + } |
| + b, err := crypto.MarshalKeypair(kp) |
| + if err != nil { |
| + return err |
| + } |
| + _, err = fmt.Fprintln(w, base64.StdEncoding.EncodeToString(b)) |
| + return err |
| +} |
diff --git a/tools/keygen/main_test.go b/tools/keygen/main_test.go| new file mode 100644 |
| index 0000000..62d0c4b |
| --- /dev/null |
| +++ b/tools/keygen/main_test.go |
| @@ -0,0 +1,41 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "os" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/rastrillo/rastrillo/budget" |
| + |
| + "amadan.net/carlos/base/internal/config" |
| +) |
| + |
| +func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| + |
| +// The printed value must be exactly what config.Load accepts. |
| +func TestKeyRoundTripsThroughConfig(t *testing.T) { |
| + var out bytes.Buffer |
| + if err := run(&out); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := strings.TrimSpace(out.String()) |
| + env := map[string]string{ |
| + "BASE_ORIGIN": "https://base.example.com", "BASE_OWNER": "o@example.com", |
| + "BASE_ASSERTION_KEY": key, |
| + } |
| + cfg, err := config.Load(func(k string) string { return env[k] }) |
| + if err != nil { |
| + t.Fatalf("config.Load rejected the printed key: %v", err) |
| + } |
| + if cfg.AssertionKey == nil { |
| + t.Fatal("config.Load left AssertionKey nil") |
| + } |
| + var again bytes.Buffer |
| + if err := run(&again); err != nil { |
| + t.Fatal(err) |
| + } |
| + if again.String() == out.String() { |
| + t.Fatal("two runs printed the same key") |
| + } |
| +} |