@astrofoundry/astrogate 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,8 +7,8 @@ Isolated agent workspaces on one host, each with its own specialists, a foreman,
7
7
  The owner runs this once per host, in a terminal on that host. The trust anchor is this repo's `allowed_signers` with `cli/verify-release.sh`: run it in a checkout, or in a folder holding copies of both. It checks the npm tarball before anything from it runs as root.
8
8
 
9
9
  ```sh
10
- v=0.1.2
10
+ v=0.1.3
11
11
  d=$(mktemp -d) && curl -fsSL -o "$d/core.tgz" "https://registry.npmjs.org/@astrofoundry/astrogate/-/astrogate-$v.tgz" && mkdir "$d/check" && sh cli/verify-release.sh "$d/core.tgz" allowed_signers "$v" "$d/check" && sudo sh "$d/check/package/cli/astrogate.sh" core update "$v" --signers allowed_signers
12
12
  ```
13
13
 
14
- After that, `sudo astrogate core update` installs the newest release, `sudo astrogate core rollback` goes back one version, and `astrogate doctor` checks the installed core against its signature.
14
+ After that, `sudo astrogate core update` installs the newest release with the tools it pins in `tools.lock`, `sudo astrogate core rollback` goes back one version, `astrogate tools` lists the tools, and `astrogate doctor` checks the core against its signature and runs each tool.
package/cli/astrogate.sh CHANGED
@@ -1,29 +1,36 @@
1
1
  #!/bin/sh
2
- # astrogate: the host command for the Astrogate core. Tools and workspaces follow.
2
+ # astrogate: the host command for the Astrogate core and the tools it pins. Workspaces follow.
3
3
  #
4
4
  # astrogate core [status] the current core, the kept previous one, and the newest on npm
5
- # sudo astrogate core update [<x.y.z>] the newest core on npm, or that version; the running core's
6
- # verify-release.sh checks its signature and every file first
7
- # sudo astrogate core rollback swaps the current core and the previous one
5
+ # sudo astrogate core update [<x.y.z>] the newest core on npm, or that version, and the tools it pins;
6
+ # the running core's verify-release.sh checks the core first, and
7
+ # each tool download must match the sha256 in the core's tools.lock
8
+ # sudo astrogate core rollback swaps the current core and the previous one, with their tools
9
+ # (sudo astrogate core activate <x.y.z> the second half of update: the new version's own code installs
10
+ # its tools and switches to it; update runs it, nobody else needs to)
11
+ # astrogate tools each tool's installed version and the version the core pins
8
12
  # astrogate doctor read-only checks; exits 1 when one fails
9
13
  #
10
14
  # The first install runs this script from a tarball the owner checked with the repo's
11
15
  # verify-release.sh, and adds `--signers <the repo's allowed_signers>`; root keeps that file
12
- # in /usr/local/etc/astrogate. Root runs only system programs and files in root-owned folders.
13
- # macOS only for now.
16
+ # in /usr/local/etc/astrogate. Root runs only system programs and files in root-owned folders;
17
+ # it runs a new tool only as nobody. macOS on Apple silicon only for now.
14
18
  set -eu
15
19
  PATH=/usr/bin:/bin:/usr/sbin:/sbin
16
20
  export PATH
17
21
  umask 022
18
22
 
19
23
  BASE=/usr/local/libexec/astrogate
24
+ TOOLS=$BASE/tools
20
25
  SIGNERS_DIR=/usr/local/etc/astrogate
21
26
  SIGNERS=$SIGNERS_DIR/allowed_signers
22
27
  COMMAND=/usr/local/bin/astrogate
23
28
  REGISTRY=https://registry.npmjs.org
29
+ # Installed in this order: gcloud's check runs with the pinned Python.
30
+ TOOL_NAMES="node jq bw python gcloud"
24
31
 
25
32
  usage() {
26
- echo "usage: astrogate core [status] | core update [<x.y.z>] [--signers <allowed_signers>] | core rollback | doctor" >&2
33
+ echo "usage: astrogate core [status] | core update [<x.y.z>] [--signers <allowed_signers>] | core rollback | tools | doctor" >&2
27
34
  exit 2
28
35
  }
29
36
 
@@ -38,6 +45,12 @@ valid_version() {
38
45
  printf '%s\n' "$1" | /usr/bin/grep -Eq '^(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})$'
39
46
  }
40
47
 
48
+ # A python-build-standalone release: CPython x.y.z and the build date that names the release.
49
+ valid_python_version() {
50
+ case "$1" in '' | *[!0-9.+]*) return 1 ;; esac
51
+ printf '%s\n' "$1" | /usr/bin/grep -Eq '^(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\+[0-9]{8}$'
52
+ }
53
+
41
54
  # True when version $1 is older than version $2.
42
55
  version_lt() {
43
56
  old_ifs=$IFS
@@ -59,7 +72,7 @@ case "${1:-}" in
59
72
  action=${2:-status}
60
73
  case "$action" in
61
74
  status) [ $# -le 2 ] || usage ;;
62
- update)
75
+ update | activate)
63
76
  shift 2
64
77
  while [ $# -gt 0 ]; do
65
78
  case "$1" in
@@ -75,14 +88,15 @@ case "${1:-}" in
75
88
  ;;
76
89
  esac
77
90
  done
91
+ [ "$action" = update ] || [ -n "$version" ] || usage
78
92
  ;;
79
93
  rollback) [ $# -eq 2 ] || usage ;;
80
94
  *) usage ;;
81
95
  esac
82
96
  ;;
83
- doctor)
97
+ tools | doctor)
84
98
  [ $# -eq 1 ] || usage
85
- action=doctor
99
+ action=$1
86
100
  ;;
87
101
  *) usage ;;
88
102
  esac
@@ -116,7 +130,7 @@ latest_version() {
116
130
  }
117
131
 
118
132
  fetch() {
119
- /usr/bin/curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 2 --max-time 300 -o "$2" "$1" || fail "download failed: $1"
133
+ /usr/bin/curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 2 --max-time 900 -o "$2" "$1" || fail "download failed: $1"
120
134
  }
121
135
 
122
136
  # A directory only root can change: owned by uid 0, not a link, writable by nobody else.
@@ -143,6 +157,11 @@ ensure_root_dir() {
143
157
  check_root_dir "$1"
144
158
  }
145
159
 
160
+ # Files in folder $1 that are not root's or that group or others can write, at most three.
161
+ unsafe_files() {
162
+ /usr/bin/find "$1" \( ! -user root -o -perm -0002 -o -perm -0020 \) -print 2>/dev/null | /usr/bin/head -n 3 | /usr/bin/tr '\n' ' '
163
+ }
164
+
146
165
  # Points current at versions/$1, and previous at the version current named before.
147
166
  switch_to() {
148
167
  before=$(link_version current)
@@ -169,8 +188,168 @@ prune() {
169
188
 
170
189
  require_root() {
171
190
  [ "$(/usr/bin/id -u)" = 0 ] || fail "run it as root: sudo astrogate core $action"
191
+ [ "$(/usr/bin/uname -m)" = arm64 ] || fail "only Apple silicon is supported yet"
192
+ }
193
+
194
+ # Root's work folder: only root can list it; nobody gets its own folder inside for tool checks.
195
+ make_work() {
196
+ work=$(/usr/bin/mktemp -d /private/var/tmp/astrogate.XXXXXX)
197
+ trap '/bin/rm -rf "$work"' EXIT
198
+ trap 'exit 1' HUP INT TERM
199
+ /bin/chmod 0711 "$work"
200
+ /bin/mkdir -m 0700 "$work/nobody"
201
+ /usr/sbin/chown nobody "$work/nobody"
202
+ }
203
+
204
+ # --- tools ------------------------------------------------------------------
205
+
206
+ tool_version_ok() {
207
+ case "$1" in python) valid_python_version "$2" ;; *) valid_version "$2" ;; esac
208
+ }
209
+
210
+ # The version core folder $1 pins for tool $2, or nothing.
211
+ pinned_version() {
212
+ [ -f "$1/tools.lock" ] || return 0
213
+ /usr/bin/awk -v t="$2" '$1 == t && NF == 4 { print $2; exit }' "$1/tools.lock"
214
+ }
215
+
216
+ # Fails unless core folder $1 has no tools.lock, or one whose lines are all
217
+ # "<tool> <version> <sha256> <https url>" with one valid version per tool.
218
+ check_lock() {
219
+ [ -f "$1/tools.lock" ] || return 0
220
+ bad_line=$(/usr/bin/awk -v names=" $TOOL_NAMES " '
221
+ /^#/ || NF == 0 { next }
222
+ NF != 4 || index(names, " " $1 " ") == 0 || length($3) != 64 || $3 !~ /^[0-9a-f]+$/ { print NR; exit }
223
+ $4 !~ /^https:\/\/(nodejs\.org|github\.com|dl\.google\.com)\/[A-Za-z0-9.\/_+-]+$/ { print NR; exit }
224
+ ($1 in seen) && seen[$1] != $2 { print NR; exit }
225
+ { seen[$1] = $2 }' "$1/tools.lock")
226
+ [ -z "$bad_line" ] || fail "$1/tools.lock line $bad_line is no <tool> <version> <sha256> <https url>, or pins a second version"
227
+ for tool in $TOOL_NAMES; do
228
+ pin=$(pinned_version "$1" "$tool")
229
+ [ -z "$pin" ] || tool_version_ok "$tool" "$pin" || fail "$1/tools.lock pins $tool $pin, which is no valid version"
230
+ done
231
+ }
232
+
233
+ # Runs a tool from / with a clean environment; as nobody when root runs this script, so root never runs a download.
234
+ run_tool() {
235
+ if [ "$(/usr/bin/id -u)" = 0 ]; then
236
+ (cd / && exec /usr/bin/sudo -n -u nobody /usr/bin/env -i PATH=/usr/bin:/bin:/usr/sbin:/sbin HOME=/var/empty "$@")
237
+ else
238
+ (cd / && exec /usr/bin/env -i PATH=/usr/bin:/bin:/usr/sbin:/sbin HOME="$HOME" "$@")
239
+ fi
240
+ }
241
+
242
+ # True when tool $1 in folder $3 answers with version $2. $4 is a folder the runner can write;
243
+ # gcloud runs with the Python folder $5.
244
+ tool_answers() {
245
+ case "$1" in
246
+ node) [ "$(run_tool "$3/bin/node" --version 2>/dev/null)" = "v$2" ] ;;
247
+ jq) [ "$(run_tool "$3/jq" --version 2>/dev/null)" = "jq-$2" ] ;;
248
+ bw) [ "$(run_tool BITWARDENCLI_APPDATA_DIR="$4" "$3/bw" --version 2>/dev/null)" = "$2" ] ;;
249
+ python) [ "$(run_tool "$3/bin/python3" -c 'import ssl, sqlite3, sys; print("%d.%d.%d" % sys.version_info[:3])' 2>/dev/null)" = "${2%+*}" ] ;;
250
+ gcloud) [ "$(run_tool CLOUDSDK_PYTHON="$5/bin/python3" CLOUDSDK_CONFIG="$4" CLOUDSDK_CORE_DISABLE_PROMPTS=1 "$3/bin/gcloud" version 2>/dev/null | /usr/bin/head -n 1)" = "Google Cloud SDK $2" ] ;;
251
+ *) return 1 ;;
252
+ esac
253
+ }
254
+
255
+ unpack_tool() {
256
+ case "$1" in
257
+ node | python) /usr/bin/tar -xzf "$2" --no-same-owner --strip-components 1 -C "$3" ;;
258
+ gcloud) /usr/bin/tar -xzf "$2" --no-same-owner -C "$3" ;;
259
+ bw) /usr/bin/unzip -q "$2" bw -d "$3" && /bin/chmod 0755 "$3/bw" ;;
260
+ jq) /usr/bin/install -m 0755 "$2" "$3/jq" ;;
261
+ esac
262
+ }
263
+
264
+ # Installs tool $2 at the version core folder $1 pins: every download must match its sha256,
265
+ # and the tool must answer with its version, run as nobody, before it moves into place.
266
+ install_tool() {
267
+ core=$1 tool=$2
268
+ pin=$(pinned_version "$core" "$tool")
269
+ dest=$TOOLS/$tool/$pin
270
+ /bin/rm -rf "$work/$tool" "$dest.partial"
271
+ /bin/mkdir "$work/$tool"
272
+ /bin/mkdir -m 0755 "$dest.partial"
273
+ downloads=$(/usr/bin/awk -v t="$tool" '$1 == t && NF == 4 { print $3, $4 }' "$core/tools.lock")
274
+ while read -r sum url; do
275
+ file=$work/$tool/${url##*/}
276
+ fetch "$url" "$file"
277
+ [ "$(/usr/bin/shasum -a 256 "$file" | /usr/bin/cut -d' ' -f1)" = "$sum" ] || fail "checksum mismatch for $url"
278
+ unpack_tool "$tool" "$file" "$dest.partial" || fail "cannot unpack $url"
279
+ done <<EOF
280
+ $downloads
281
+ EOF
282
+ /usr/sbin/chown -R root:wheel "$dest.partial"
283
+ /bin/chmod -R u=rwX,go=rX "$dest.partial"
284
+ tool_answers "$tool" "$pin" "$dest.partial" "$work/nobody" "$TOOLS/python/$(pinned_version "$core" python)" ||
285
+ fail "$tool $pin does not answer with its version; nothing changed"
286
+ /bin/mv "$dest.partial" "$dest"
287
+ echo "installed $tool $pin"
288
+ }
289
+
290
+ # Installs every tool version core folder $1 pins that is not there yet; switches nothing.
291
+ stage_tools() {
292
+ check_lock "$1"
293
+ [ -f "$1/tools.lock" ] || return 0
294
+ ensure_root_dir "$TOOLS"
295
+ for tool in $TOOL_NAMES; do
296
+ pin=$(pinned_version "$1" "$tool")
297
+ [ -n "$pin" ] || continue
298
+ ensure_root_dir "$TOOLS/$tool"
299
+ [ -d "$TOOLS/$tool/$pin" ] || install_tool "$1" "$tool"
300
+ done
301
+ }
302
+
303
+ # Points each tool's current link at the version core folder $1 pins.
304
+ link_tools() {
305
+ [ -f "$1/tools.lock" ] || return 0
306
+ for tool in $TOOL_NAMES; do
307
+ pin=$(pinned_version "$1" "$tool")
308
+ [ -n "$pin" ] && [ -d "$TOOLS/$tool/$pin" ] || continue
309
+ [ "$(/usr/bin/readlink "$TOOLS/$tool/current" 2>/dev/null || true)" != "$pin" ] || continue
310
+ /bin/rm -f "$TOOLS/$tool/current.new"
311
+ /bin/ln -s "$pin" "$TOOLS/$tool/current.new"
312
+ /bin/mv -h "$TOOLS/$tool/current.new" "$TOOLS/$tool/current"
313
+ echo "$tool $pin is current"
314
+ done
315
+ }
316
+
317
+ # Removes the tool versions, and the tools, that neither the current nor the previous core pins.
318
+ prune_tools() {
319
+ for tool_dir in "$TOOLS"/*; do
320
+ [ -d "$tool_dir" ] && [ ! -L "$tool_dir" ] || continue
321
+ tool=${tool_dir##*/}
322
+ keep_current=$(pinned_version "$BASE/current" "$tool")
323
+ keep_previous=$(pinned_version "$BASE/previous" "$tool")
324
+ if [ -z "$keep_current" ] && [ -z "$keep_previous" ]; then
325
+ /bin/rm -rf "$tool_dir"
326
+ continue
327
+ fi
328
+ for dir in "$tool_dir"/*; do
329
+ [ -e "$dir" ] || [ -L "$dir" ] || continue
330
+ name=${dir##*/}
331
+ [ "$name" = current ] || [ "$name" = "$keep_current" ] || [ "$name" = "$keep_previous" ] || /bin/rm -rf "$dir"
332
+ done
333
+ done
172
334
  }
173
335
 
336
+ tools_status() {
337
+ current=$(link_version current)
338
+ [ -n "$current" ] || fail "no core is installed"
339
+ if [ ! -f "$BASE/current/tools.lock" ]; then
340
+ echo "core $current pins no tools"
341
+ return 0
342
+ fi
343
+ for tool in $TOOL_NAMES; do
344
+ pin=$(pinned_version "$BASE/current" "$tool")
345
+ [ -n "$pin" ] || continue
346
+ have=$(/usr/bin/readlink "$TOOLS/$tool/current" 2>/dev/null || true)
347
+ if [ "$have" = "$pin" ]; then echo "$tool $pin"; else echo "$tool ${have:-missing}; core $current pins $pin (sudo astrogate core update)"; fi
348
+ done
349
+ }
350
+
351
+ # --- core -------------------------------------------------------------------
352
+
174
353
  core_status() {
175
354
  current=$(link_version current)
176
355
  previous=$(link_version previous)
@@ -178,6 +357,17 @@ core_status() {
178
357
  echo "core ${current:-not installed}${previous:+, previous $previous}, newest on npm ${latest:-unknown}"
179
358
  }
180
359
 
360
+ # Picks the signers file: root's copy, or on the first install the one named with --signers.
361
+ pick_signers() {
362
+ if [ -f "$SIGNERS" ]; then
363
+ [ -z "$signers_arg" ] || /usr/bin/cmp -s "$signers_arg" "$SIGNERS" || fail "$SIGNERS exists and differs from $signers_arg; replace it by hand when the signing key changes"
364
+ signers=$SIGNERS
365
+ else
366
+ [ -n "$signers_arg" ] && [ -f "$signers_arg" ] || fail "no $SIGNERS yet: the first install needs --signers <the repo's allowed_signers>"
367
+ signers=$signers_arg
368
+ fi
369
+ }
370
+
181
371
  core_update() {
182
372
  require_root
183
373
  check_root_dir /
@@ -186,13 +376,7 @@ core_update() {
186
376
  ensure_root_dir /usr/local/libexec
187
377
  ensure_root_dir "$BASE"
188
378
  ensure_root_dir "$BASE/versions"
189
- if [ -f "$SIGNERS" ]; then
190
- [ -z "$signers_arg" ] || /usr/bin/cmp -s "$signers_arg" "$SIGNERS" || fail "$SIGNERS exists and differs from $signers_arg; replace it by hand when the signing key changes"
191
- signers=$SIGNERS
192
- else
193
- [ -n "$signers_arg" ] && [ -f "$signers_arg" ] || fail "no $SIGNERS yet: the first install needs --signers <the repo's allowed_signers>"
194
- signers=$signers_arg
195
- fi
379
+ pick_signers
196
380
  current=$(link_version current)
197
381
  if [ -z "$version" ]; then
198
382
  version=$(latest_version)
@@ -201,17 +385,17 @@ core_update() {
201
385
  fail "npm names $version as newest, older than the installed $current; name a version to go back"
202
386
  fi
203
387
  fi
388
+ make_work
204
389
  if [ "$version" = "$current" ]; then
205
- echo "core $current is installed"
390
+ stage_tools "$BASE/current"
391
+ link_tools "$BASE/current"
392
+ echo "core $current is installed, with the tools it pins"
206
393
  return 0
207
394
  fi
208
395
  # The installed core checks a new one; only the first install uses the copy next to this script.
209
396
  if [ -n "$current" ]; then verifier=$BASE/current/cli/verify-release.sh; else verifier=$HERE/verify-release.sh; fi
210
397
  [ -f "$verifier" ] || fail "$verifier is missing"
211
398
 
212
- work=$(/usr/bin/mktemp -d /private/var/tmp/astrogate.XXXXXX)
213
- trap '/bin/rm -rf "$work"' EXIT
214
- trap 'exit 1' HUP INT TERM
215
399
  fetch "$REGISTRY/@astrofoundry/astrogate/-/astrogate-$version.tgz" "$work/core.tgz"
216
400
  /bin/mkdir "$work/check"
217
401
  /bin/sh "$verifier" "$work/core.tgz" "$signers" "$version" "$work/check" >/dev/null
@@ -224,8 +408,28 @@ core_update() {
224
408
  /bin/chmod 0755 "$target.new/cli/astrogate.sh" "$target.new/cli/verify-release.sh"
225
409
  /bin/rm -rf "$target"
226
410
  /bin/mv "$target.new" "$target"
411
+ # The checked version's own code finishes, so a release's new install steps apply to itself.
412
+ /bin/rm -rf "$work"
413
+ trap - EXIT HUP INT TERM
414
+ exec /bin/sh "$target/cli/astrogate.sh" core activate "$version" ${signers_arg:+--signers "$signers_arg"}
415
+ }
416
+
417
+ # The second half of core update, run from the checked version it activates: every tool that
418
+ # version pins is in place before anything switches.
419
+ core_activate() {
420
+ require_root
421
+ pick_signers
422
+ target=$BASE/versions/$version
423
+ [ "$HERE" = "$target/cli" ] || fail "core activate runs only from the version it activates"
424
+ check_root_dir "$target"
425
+ current=$(link_version current)
426
+ [ "$version" != "$current" ] || fail "core $version is current already"
427
+ make_work
428
+ stage_tools "$target"
227
429
  switch_to "$version"
430
+ link_tools "$BASE/current"
228
431
  prune
432
+ prune_tools
229
433
 
230
434
  if [ ! -f "$SIGNERS" ]; then
231
435
  ensure_root_dir /usr/local/etc
@@ -246,7 +450,10 @@ core_rollback() {
246
450
  current=$(link_version current)
247
451
  previous=$(link_version previous)
248
452
  [ -n "$previous" ] && [ -d "$BASE/versions/$previous" ] || fail "no previous core to roll back to"
453
+ make_work
454
+ stage_tools "$BASE/versions/$previous"
249
455
  switch_to "$previous"
456
+ link_tools "$BASE/current"
250
457
  echo "core $current -> $previous; astrogate core rollback again goes forward"
251
458
  }
252
459
 
@@ -260,20 +467,47 @@ bad() {
260
467
  failed=1
261
468
  }
262
469
 
470
+ doctor_tools() {
471
+ if [ ! -f "$BASE/current/tools.lock" ]; then
472
+ warn "core $current pins no tools"
473
+ return 0
474
+ fi
475
+ scratch=$(/usr/bin/mktemp -d)
476
+ for tool in $TOOL_NAMES; do
477
+ pin=$(pinned_version "$BASE/current" "$tool")
478
+ [ -n "$pin" ] || continue
479
+ have=$(/usr/bin/readlink "$TOOLS/$tool/current" 2>/dev/null || true)
480
+ if [ "$have" != "$pin" ]; then
481
+ bad "$tool is ${have:-missing}; core $current pins $pin (sudo astrogate core update)"
482
+ continue
483
+ fi
484
+ problem=$(root_dir_problem "$TOOLS/$tool/$pin")
485
+ [ -n "$problem" ] || problem=$(unsafe_files "$TOOLS/$tool/$pin")
486
+ if [ -n "$problem" ]; then
487
+ bad "$tool $pin is not root-only: $problem"
488
+ elif tool_answers "$tool" "$pin" "$TOOLS/$tool/current" "$scratch" "$TOOLS/python/current"; then
489
+ ok "$tool $pin"
490
+ else
491
+ bad "$tool $pin does not answer with its version"
492
+ fi
493
+ done
494
+ /bin/rm -rf "$scratch"
495
+ }
496
+
263
497
  doctor() {
264
498
  printf 'astrogate doctor on %s, macOS %s on %s\n' "$(/bin/hostname -s)" "$(/usr/bin/sw_vers -productVersion)" "$(/usr/bin/uname -m)"
265
499
  current=$(link_version current)
266
500
  if [ -z "$current" ] || [ ! -d "$BASE/versions/$current" ]; then
267
501
  bad "no core in $BASE/current: install it (README, First install)"
268
- [ "$failed" = 0 ] || exit 1
502
+ exit 1
269
503
  fi
270
504
  problems=""
271
505
  for dir in / /usr /usr/local /usr/local/libexec "$BASE" "$BASE/versions" "$BASE/versions/$current"; do
272
506
  problem=$(root_dir_problem "$dir")
273
507
  [ -z "$problem" ] || problems="$problems; $problem"
274
508
  done
275
- unsafe=$(/usr/bin/find "$BASE/versions/$current" \( ! -user root -o -perm -0002 -o -perm -0020 \) -print 2>/dev/null | /usr/bin/head -n 3)
276
- [ -z "$unsafe" ] || problems="$problems; not root-only: $(printf '%s' "$unsafe" | /usr/bin/tr '\n' ' ')"
509
+ unsafe=$(unsafe_files "$BASE/versions/$current")
510
+ [ -z "$unsafe" ] || problems="$problems; not root-only: $unsafe"
277
511
  if [ -z "$problems" ]; then ok "core folders are root-only"; else bad "core folders${problems}"; fi
278
512
 
279
513
  if [ -f "$SIGNERS" ] && [ "$(/usr/bin/stat -f '%u %Lp' "$SIGNERS")" = "0 644" ]; then
@@ -298,12 +532,15 @@ doctor() {
298
532
  previous=$(link_version previous)
299
533
  if [ -n "$previous" ] && [ -d "$BASE/versions/$previous" ]; then ok "previous core $previous kept for rollback"; else warn "no previous core to roll back to"; fi
300
534
  if [ "$(/usr/bin/readlink "$COMMAND" 2>/dev/null || true)" = "$BASE/current/cli/astrogate.sh" ]; then ok "$COMMAND"; else bad "$COMMAND does not link to $BASE/current/cli/astrogate.sh"; fi
535
+ doctor_tools
301
536
  [ "$failed" = 0 ] || exit 1
302
537
  }
303
538
 
304
539
  case "$action" in
305
540
  status) core_status ;;
306
541
  update) core_update ;;
542
+ activate) core_activate ;;
307
543
  rollback) core_rollback ;;
544
+ tools) tools_status ;;
308
545
  doctor) doctor ;;
309
546
  esac
@@ -1,5 +1,5 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import { mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, rmSync } from "node:fs";
2
+ import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { describe, expect, it } from "vitest";
@@ -7,6 +7,9 @@ import { describe, expect, it } from "vitest";
7
7
  const SCRIPT = join(import.meta.dirname, "astrogate.sh");
8
8
  const isRoot = process.getuid?.() === 0;
9
9
  const isMac = process.platform === "darwin";
10
+ const REPO = join(import.meta.dirname, "..");
11
+ const SHA = "a".repeat(64);
12
+ const TOOL_FUNCTIONS = ["fail", "valid_version", "valid_python_version", "tool_version_ok", "pinned_version", "check_lock"];
10
13
 
11
14
  function astrogate(...args: string[]) {
12
15
  return spawnSync("/bin/sh", [SCRIPT, ...args], { encoding: "utf-8" });
@@ -41,7 +44,11 @@ describe("astrogate", () => {
41
44
  [["core", "update", "--signers", ""]],
42
45
  [["core", "update", "--signers", "a", "--signers", "b"]],
43
46
  [["core", "rollback", "1.2.3"]],
47
+ [["core", "activate"]],
48
+ [["core", "activate", "--signers", "x"]],
49
+ [["core", "activate", "1.2"]],
44
50
  [["doctor", "now"]],
51
+ [["tools", "now"]],
45
52
  [["update"]],
46
53
  ])("refuses %j with a usage error before anything else", (args) => {
47
54
  const result = astrogate(...args);
@@ -49,7 +56,7 @@ describe("astrogate", () => {
49
56
  expect(result.stderr).toMatch(/^usage: astrogate/);
50
57
  });
51
58
 
52
- it.runIf(isMac && !isRoot).each([["update"], ["update", "1.2.3", "--signers", "x"], ["rollback"]])("refuses core %s without root", (...args) => {
59
+ it.runIf(isMac && !isRoot).each([["update"], ["update", "1.2.3", "--signers", "x"], ["rollback"], ["activate", "1.2.3"]])("refuses core %s without root", (...args) => {
53
60
  const result = astrogate("core", ...args);
54
61
  expect(result.status).toBe(1);
55
62
  expect(result.stderr).toContain("run it as root: sudo astrogate core");
@@ -110,4 +117,73 @@ describe("astrogate", () => {
110
117
  rmSync(base, { recursive: true, force: true });
111
118
  }
112
119
  });
120
+
121
+ describe("tools.lock", () => {
122
+ const withLock = (lock: string, call: (dir: string) => string) => {
123
+ const dir = mkdtempSync(join(tmpdir(), "astrogate-lock-"));
124
+ try {
125
+ writeFileSync(join(dir, "tools.lock"), lock);
126
+ return shellFunctions(TOOL_FUNCTIONS, `TOOL_NAMES="node jq bw python gcloud"\n${call(dir)}`);
127
+ } finally {
128
+ rmSync(dir, { recursive: true, force: true });
129
+ }
130
+ };
131
+
132
+ it("accepts the committed tools.lock and reads its pins", () => {
133
+ const result = shellFunctions(TOOL_FUNCTIONS, `TOOL_NAMES="node jq bw python gcloud"\ncheck_lock '${REPO}'\nfor t in $TOOL_NAMES; do printf '%s=%s\\n' "$t" "$(pinned_version '${REPO}' "$t")"; done`);
134
+ expect(result.stderr).toBe("");
135
+ expect(result.stdout).toMatch(/^node=\d+\.\d+\.\d+\njq=\d+\.\d+\.\d+\nbw=\d+\.\d+\.\d+\npython=\d+\.\d+\.\d+\+\d{8}\ngcloud=\d+\.\d+\.\d+\n$/);
136
+ });
137
+
138
+ it("accepts a core without tools.lock", () => {
139
+ expect(shellFunctions(TOOL_FUNCTIONS, `TOOL_NAMES=node\ncheck_lock '${tmpdir()}/astrogate-no-such-core'`).status).toBe(0);
140
+ });
141
+
142
+ it.each([
143
+ ["three fields", `jq 1.8.2 ${SHA}\n`],
144
+ ["an unknown tool", `curl 8.0.0 ${SHA} https://github.com/x/curl\n`],
145
+ ["a short sha256", `jq 1.8.2 ${SHA.slice(1)} https://github.com/x/jq\n`],
146
+ ["an upper-case sha256", `jq 1.8.2 ${SHA.toUpperCase()} https://github.com/x/jq\n`],
147
+ ["plain http", `jq 1.8.2 ${SHA} http://github.com/x/jq\n`],
148
+ ["another host", `jq 1.8.2 ${SHA} https://example.com/x/jq\n`],
149
+ ["a host that only starts like an allowed one", `jq 1.8.2 ${SHA} https://github.com.example.com/x/jq\n`],
150
+ ["a query string", `jq 1.8.2 ${SHA} https://github.com/x/jq?y=1\n`],
151
+ ["two versions of one tool", `gcloud 1.0.0 ${SHA} https://dl.google.com/a.tgz\ngcloud 1.0.1 ${SHA} https://dl.google.com/b.tgz\n`],
152
+ ["a bad version", `node 26.14 ${SHA} https://nodejs.org/x.tgz\n`],
153
+ ["a python version without its build date", `python 3.14.10 ${SHA} https://github.com/x/py.tgz\n`],
154
+ ])("refuses %s", (_, lock) => {
155
+ const result = withLock(lock, (dir) => `check_lock '${dir}'`);
156
+ expect(result.status).toBe(1);
157
+ expect(result.stderr).toMatch(/^astrogate: .*tools\.lock (line \d+|pins)/);
158
+ });
159
+ });
160
+
161
+ it.runIf(isMac)("links the pinned tool versions and prunes the ones no kept core pins", () => {
162
+ const base = mkdtempSync(join(tmpdir(), "astrogate-tools-"));
163
+ try {
164
+ const tools = join(base, "tools");
165
+ const core = (v: string, lock: string) => {
166
+ mkdirSync(join(base, "versions", v), { recursive: true });
167
+ writeFileSync(join(base, "versions", v, "tools.lock"), lock);
168
+ };
169
+ core("1.0.0", `jq 1.8.1 ${SHA} https://github.com/x/jq\nbw 2026.1.0 ${SHA} https://github.com/x/bw\n`);
170
+ core("1.1.0", `jq 1.8.2 ${SHA} https://github.com/x/jq\n`);
171
+ for (const d of ["jq/1.8.0", "jq/1.8.1", "jq/1.8.2", "jq/1.8.2.partial", "bw/2026.1.0", "node/26.0.0"]) mkdirSync(join(tools, d), { recursive: true });
172
+ symlinkSync("1.8.0", join(tools, "jq", "current"));
173
+ const run = (call: string) =>
174
+ shellFunctions([...TOOL_FUNCTIONS, "link_version", "switch_to", "link_tools", "prune_tools"], `BASE='${base}'\nTOOLS='${tools}'\nTOOL_NAMES="node jq bw python gcloud"\n${call}`);
175
+
176
+ expect(run("switch_to 1.0.0 && switch_to 1.1.0 && link_tools \"$BASE/current\"").stdout).toBe("jq 1.8.2 is current\n");
177
+ expect(readlinkSync(join(tools, "jq", "current"))).toBe("1.8.2");
178
+ expect(run("prune_tools").status).toBe(0);
179
+ expect(readdirSync(join(tools, "jq")).sort()).toEqual(["1.8.1", "1.8.2", "current"]);
180
+ expect(readdirSync(join(tools, "bw"))).toEqual(["2026.1.0"]);
181
+ expect(existsSync(join(tools, "node"))).toBe(false);
182
+
183
+ expect(run('switch_to 1.0.0 && link_tools "$BASE/current"').stdout).toBe("jq 1.8.1 is current\nbw 2026.1.0 is current\n");
184
+ expect(readlinkSync(join(tools, "jq", "current"))).toBe("1.8.1");
185
+ } finally {
186
+ rmSync(base, { recursive: true, force: true });
187
+ }
188
+ });
113
189
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrofoundry/astrogate",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Astrogate core: isolated agent workspaces with specialists, a foreman, and chat bridges, for the pi coding agent and Claude Code.",
5
5
  "packageManager": "pnpm@12.23.0",
6
6
  "scripts": {
@@ -8,6 +8,7 @@
8
8
  "lint": "eslint .",
9
9
  "build": "node scripts/build.ts",
10
10
  "prepack": "node scripts/build.ts",
11
+ "pin-tools": "node scripts/pin-tools.ts",
11
12
  "typecheck": "tsc --noEmit",
12
13
  "check": "tsc --noEmit && eslint . && node scripts/build.ts && vitest run --coverage",
13
14
  "release:patch": "node scripts/release.ts patch",
@@ -36,7 +37,8 @@
36
37
  "harness",
37
38
  "release.sha256",
38
39
  "release.sha256.sig",
39
- "specialists"
40
+ "specialists",
41
+ "tools.lock"
40
42
  ],
41
43
  "repository": {
42
44
  "type": "git",
package/release.sha256 CHANGED
@@ -1,10 +1,10 @@
1
- 18e5d7d16e71779794629880e19fd14d28d6fb7b5db9ccdc64511e8b6c69a4a9 README.md
1
+ aa08819cc3d490adab1ed75efd822e30d808b029935a2ecb24ee8d37c9e21d8f README.md
2
2
  b9b0f9fa90b9a230157de1fefef1aa5a624bfd7b50527b67ad86ecc62414aadb agents/foreman.md
3
3
  4091f50a5a9ff72e2f1571c6de054d9c891dbe294aa58e6a55ad9a1bff2f766a cli/astro-install.sh
4
4
  9f371247fdaea09e1551f940fd91eacb7d242baea343fce66f12fc8e22ba064b cli/astro.sh
5
5
  8e054f8286876b77e0887d0b0e2a62d4a048fd41bfa0048d126e0c9171869cf6 cli/astro.test.ts
6
- a41c87a4a7ed07382a58de6e8f382e2dba9123de2aca6aa8275bd31df8985a48 cli/astrogate.sh
7
- 08d772b724401f59988b99ad3cadd381c2280975fda38f76a7dfc7853188e117 cli/astrogate.test.ts
6
+ 01882995b53a4f528f39668c50256533c8b6aef1f882dbf7d4cd1ec82ff9969f cli/astrogate.sh
7
+ 1f8a0d0914b30582793399cf16cebcf6266d3c716f5925919073d2c7e85c4c6e cli/astrogate.test.ts
8
8
  07b9aef7e5d0ee43959e0c16e00ec3a202769c94faea4167257513bf15b9383c cli/verify-release.sh
9
9
  29614fd0eb1ad08215baf827b00ee2b98ccb73ca0ccf770bc5a0084c71da5926 cli/verify-release.test.ts
10
10
  369bc39629e3997f35ea22a4ba4d37f84da52c60ca3bf6fa5c623861a1a58329 dist/bridge.js
@@ -69,7 +69,7 @@ e76423d9c09263e064e48e6608c6eb07f70bc350841153f7c251376b8b424dc6 extensions/sub
69
69
  484c52d2e9d93dbe4aec878b5afc494f0076f5b79b5f4d24423933b39913bc3b harness/claude/main.ts
70
70
  e1d07bd981bd4ff6385246b6c1ad41349718c1e490b2bff675291dc7b568cd69 harness/claude/mcp.ts
71
71
  194b623038d56a2565ad42d30d1db72ca453e654bfc84996800362e8a972282b harness/claude/settings.ts
72
- 916cc870de3aa2881387ef4a95c25f7d9145b0f97ea2b940326eb85ea29f4868 package.json
72
+ b8421028591e11d11d3d96c368259d248bbe73afebdcdc81ae17ac87a9a77cc8 package.json
73
73
  642383b14db5dbfb7caa6a185591263e4294a73323bf4c78606f7b85f0817fd0 specialists/AGENTS.md
74
74
  a385cf5e3b59de30b5ed9b0840d6cf6198b577668f18dfd5de13a714fb047c3e specialists/README.md
75
75
  729f8c29d7cf716be5b01d5df869eeda6d4683f86a4915a52fb8adfdebe1f9d2 specialists/entry/specialist-cli.sh
@@ -97,3 +97,4 @@ c1abde1e550c0f4703fd0b2536cc4002c7edf6468c18a03ebaa516fd1ce093c3 specialists/li
97
97
  31959f525a0b0aff070b4a19a9677d446d3425481e936dcd9ca2d957f4e51142 specialists/lib/tunnel.test.ts
98
98
  ed93bfcef355a0a3aa7d679395b2f6ce9ce4c5709253fda7f63c1f3927f5d324 specialists/lib/tunnel.ts
99
99
  7a36850a90c3a5e8c2a02fe386f93a0b6e664431799154f548e7ac9f33e84a90 specialists/provision/provision.sh
100
+ d182f87697c383c29075dc8b5415fc6fa3832ab33ccf14d5df8a941d9435b5c9 tools.lock
@@ -1,6 +1,6 @@
1
1
  -----BEGIN SSH SIGNATURE-----
2
2
  U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgUteIwPdKqPwfNc4vHQPUE+JWMC
3
3
  nmuMUwttLeX5wtyIoAAAAOYXN0cm9nYXRlLWNvcmUAAAAAAAAABnNoYTUxMgAAAFMAAAAL
4
- c3NoLWVkMjU1MTkAAABA1taMKjBlPVP2TGVWp2cWTZZg0JKYd8T3E72x2KrpeX9nZazV3p
5
- TvxaRwQhZeJlqy0k1VRXRZiO5y6W1+M9e6Dw==
4
+ c3NoLWVkMjU1MTkAAABADRbtpiE2hBqARrEoR/Oq9VAmfIegWxcu1WKsiaZzhkkZkP7n72
5
+ XnZmHDgxR+wZjWHd9zdzsbs8afnXj4JLVkDw==
6
6
  -----END SSH SIGNATURE-----
package/tools.lock ADDED
@@ -0,0 +1,12 @@
1
+ # Tools the core installs on every host, pinned by version and sha256.
2
+ # Written by `pnpm pin-tools <tool> <version>`; `astrogate core update` installs exactly these.
3
+ # tool version sha256 url
4
+ node 26.14.0 1c65aaa7d4f1a86e3448f09c536ea45f6e23e2ef472390eee080e9a8deb9a1fe https://nodejs.org/dist/v26.14.0/node-v26.14.0-darwin-arm64.tar.gz
5
+ jq 1.8.2 2d75340ba57a4b4b4c8708a21c2dc8e958a48aaa8bba13b27f77f6e4c0eca07e https://github.com/jqlang/jq/releases/download/jq-1.8.2/jq-macos-arm64
6
+ bw 2026.11.1 0447a5c94b5893deb3fbe4f4ac02e8d811e2f9815c27d567d08b4fc70486cff1 https://github.com/bitwarden/clients/releases/download/cli-v2026.11.1/bw-bit-macos-arm64-2026.11.1.zip
7
+ python 3.14.10+20261201 3106ec6408d5b9ebbb759ca09a1206e5191223d571af57de5fb34fe8c30fa703 https://github.com/astral-sh/python-build-standalone/releases/download/20261201/cpython-3.14.10+20261201-aarch64-apple-darwin-install_only_stripped.tar.gz
8
+ gcloud 594.0.1 f3d680f1d2bcf89b575bbba309305f2d897236d19b82e7d78462cf632ee2252d https://dl.google.com/dl/cloudsdk/channels/rapid/components/google-cloud-sdk-core-20261121205138.tar.gz
9
+ gcloud 594.0.1 0e7d184def218336a3850e51d64b3f4d680fcbd8e075e63205ae78296d6884cc https://dl.google.com/dl/cloudsdk/channels/rapid/components/google-cloud-sdk-core-nix-20261022194556.tar.gz
10
+ gcloud 594.0.1 da085036b39328aedcf43423e29676f40a7743c593f7165d1e5d7bc0e56d2b69 https://dl.google.com/dl/cloudsdk/channels/rapid/components/google-cloud-sdk-gcloud-deps-20261106082013.tar.gz
11
+ gcloud 594.0.1 3bbd67e1be71d5c67c78561c00cb5694668cab238161ff6a6092c0fab1c3068b https://dl.google.com/dl/cloudsdk/channels/rapid/components/google-cloud-sdk-gcloud-deps-darwin-arm-20260622153051.tar.gz
12
+ gcloud 594.0.1 e26abbb067a73858fd55d2d04fd961663118ddf07d849cd86aefea9a0844025a https://dl.google.com/dl/cloudsdk/channels/rapid/components/google-cloud-sdk-gcloud-crc32c-darwin-arm-20261016073856.tar.gz