Repository navigation
fix(sbx): install-host.sh installs and wires moca-egress (#623) - #627
Conversation
0e1aad7 to
caf45b1
Compare
|
Live acceptance: PASS on the SBX0 KVM host (the metal rig serving Migration. The host had a hand-installed What the install produced:
Removing the drop-in. After the install, I removed the drop-in, ran From a sandbox (
Kept on the host:
The manifest is A0 (the GitHub auth-shape check) is still open. It doesn't block this PR. |
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Reviewed at caf45b1 (commits 43abe54 + caf45b1 on top of main 52cf778).
The egress install is careful: it validates before touching systemd, skips the restart when nothing changed, does reset-failed before start, keeps the seeded allowlist across re-runs, and leaves a foreign unit alone. The tests cover each of those paths, and the live acceptance comment matches the design. No blockers. One suggestion and two nits are inline. The suggestion: ownership of moca-egress.service should also check the unit's content, so this script and setup-microvm.sh can't overwrite or remove each other's unit on a dual-install host.
Also:
- CI has not run on this PR. Only DCO is reported at caf45b1, and there are no workflow runs for this head. The PR was opened and last pushed while its base was #624's branch, and
ci.ymlfilterspull_requestonbranches: [main]. The base change tomaindoesn't triggersynchronize. A push, or close/reopen, is needed sodeploy-scripts(make test-deploy, which runsinstall-host.test.sh),lintand the rest run before merge. - nit: the PR body still says to review
0fdfffd5/0e1aad73afterec21394a, but those SHAs were rebased away. "Still to do" also ticks A0, which the acceptance comment says is still open.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Shell, systemd units, Docs, Tests, Security
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed-off: yes
CI status: not run (DCO only)
| local unit="$SBX_UNIT_DIR/moca-egress.service" env="$SBX_ENV_DIR/moca-egress.env" tmp changed=0 hint | ||
| [[ -x "$SBX_BIN_DIR/moca-egress" ]] || | ||
| die "no $SBX_BIN_DIR/moca-egress: build it with CGO_ENABLED=0 go build -o /usr/local/bin/moca-egress ./remote-worker/cmd/moca-egress (or pass --no-egress to install the worker without guest egress)" | ||
| if [[ -e "$unit" ]] && ! grep -qxF "egress unit $unit" "$SBX_ENV_DIR/manifest" 2>/dev/null; then |
There was a problem hiding this comment.
suggestion — Ownership is decided by the manifest line alone, so it doesn't survive the other installer. Take a host where this script installs moca-egress.service first, and later deploy/microvm/setup-microvm.sh (P6) runs. install_if_changed overwrites the unit with the P6 copy, whose EnvironmentFile=/etc/serverless-harness/moca-egress.env, while the manifest still says egress unit …. After that:
- a re-run of this script finds a recorded unit,
cmpdiffers, so it reinstalls the/etc/sbxvariant and restarts. The two installers flip-flop the allowlist file on every run. - the last
--uninstallhere doesdisable --nowandrmon a unit that P6'smicrovm-workernow depends on.
That breaks the header's "never another install on the same host" claim. Suggest treating a recorded unit as ours only while its content still equals what we generate (the $tmp from sed, or a checksum recorded in the manifest). Otherwise, log it and take the reuse path. Apply the same check in remove_egress before disable --now/rm.
There was a problem hiding this comment.
Fixed in 86d8691. The manifest now records egress unit <path> <sha256>, and a recorded unit counts as ours only while its content hashes to that digest.
On a mismatch, for example setup-microvm.sh has replaced it:
- the install logs
moca-egress.service was changed by another install; reusing it, drops its claim and takes the reuse path; remove_egressmakes the same check beforedisable --noworrm, and otherwise logskept … (changed by another install).
Two-field manifests from the earlier commit fall back to comparing with the regenerated unit. The new test replaces the unit after install, re-runs, and uninstalls. It asserts the unit is reused and never disabled or removed. A "Ownership is by content" bullet is added to the RUNBOOK.
| die "no $SBX_BIN_DIR/moca-egress: build it with CGO_ENABLED=0 go build -o /usr/local/bin/moca-egress ./remote-worker/cmd/moca-egress (or pass --no-egress to install the worker without guest egress)" | ||
| if [[ -e "$unit" ]] && ! grep -qxF "egress unit $unit" "$SBX_ENV_DIR/manifest" 2>/dev/null; then | ||
| log "reusing the existing moca-egress.service (not installed by this script)" | ||
| wait_egress_ready |
There was a problem hiding this comment.
nit — In the reuse path, nothing makes sure the foreign unit is enabled or running. An inactive P6 moca-egress only shows up as the 30 s socket timeout, and wait_egress_ready's hint then names $SBX_ENV_DIR/moca-egress.env, which this unit doesn't read (its allowlist is /etc/serverless-harness/moca-egress.env, per the RUNBOOK). Consider an is-active check with its own message ("the existing moca-egress.service is not running; start it or fix its install"), or passing the env path into the hint.
There was a problem hiding this comment.
Fixed in 86d8691. The reuse path now runs is-active before any worker restart. If the unit isn't active, it fails with the existing moca-egress.service (not installed by this script) is not running: start it or fix the install that owns it.
wait_egress_ready takes the env path as an argument. On the reuse path the script reads it from the foreign unit's EnvironmentFile=, so the timeout hint names the file that unit actually uses. A test asserts that no restart sbx-worker@… happens and checks the hint.
| case "$kind" in | ||
| unit) rm -f "$path"; log "removed $path (no SBX0 worker left on this host)" ;; | ||
| file) | ||
| if [[ -f "$path" && "$(cat "$path")" != "$(egress_seed)" ]]; then |
There was a problem hiding this comment.
nit — "Unedited" is judged against egress_seed at uninstall time, so it depends on the uninstaller's MOCA_EGRESS_ALLOW. Suppose the file was seeded from a custom MOCA_EGRESS_ALLOW, as in the live acceptance run, where the found allowlist was passed in. An uninstall without that env reports kept … (edited) and leaves the file, even though nobody edited it. That fails safe, but it contradicts the RUNBOOK and the log line. Recording the seeded content's hash in the manifest (e.g. egress file <path> <sha256>) would make the check exact. The sed -n 's/^egress //p' + read -r kind path loop would need a third field for that.
There was a problem hiding this comment.
Fixed in 86d8691. When the seed is written, its sha256 is recorded as egress file <path> <sha256>. Uninstall removes the file only when its current hash matches, so the check no longer depends on the uninstaller's MOCA_EGRESS_ALLOW. A two-field manifest line still uses the old comparison.
The reading loop takes the third field. An unknown egress <kind> line now does continue and keeps both its line and its file, which matters for the egress credential kind a later chunk adds.
Tests cover:
- a custom seed, removed without the variable set;
- an edited file, kept;
- a two-field manifest, which uninstalls cleanly;
- an
egress fooline, which survives.
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
caf45b1 to
86d8691
Compare
|
All three inline points are fixed in 86d8691, with replies on each thread. On the two general points:
|
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Re-reviewed at 86d8691. The head was rebased onto main fd97fbd after #624 merged. Running range-diff shows 43abe54/caf45b1 replayed unchanged (=) as 8d24644/d5ec433, so I reviewed only the new commit, 86d8691.
I checked all three items from the caf45b1 review against the code. All are fixed:
- Ownership by content. The manifest now records
egress unit <path> <sha256>. A recorded unit that no longer hashes to its digest getschanged by another install; reusing it, and its claim is dropped.remove_egressdoes the same check beforedisable --now/rm. A test overwrites the unit with a P6 copy, then checks that the re-run reuses it and the last uninstall neither disables nor removes it. - Reuse path.
is-activeruns before any worker restart and has its own message. The socket-timeout hint names the foreign unit'sEnvironmentFile=. Both are tested. - Env-file digest. "Unedited" is judged by the recorded sha256, so it no longer depends on the uninstaller's
MOCA_EGRESS_ALLOW. Two-field manifest lines fall back to the old comparison. An unknownegress <kind>line, and its file, are left alone. Each case is tested.
CI is now running on this head, and every check passes: check, deploy-scripts, lint, shellcheck, trivy-scan, codeql, and the rest. The PR-body nit is also fixed.
FYI, not blocking: a two-field egress unit line (only from this PR's earlier head, i.e. the acceptance host) is judged "ours" by comparing against the current egress_unit_text. If a later release changes deploy/microvm/moca-egress.service before that host re-runs, the old unit is treated as foreign and left in place. That fails safe, and the next re-run here on the acceptance host upgrades the line to the digest form.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Shell, systemd units, Docs, Tests, Security
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits (8d24644, d5ec433, 86d8691), all signed-off: yes
CI status: passing (all checks green at 86d8691)
cwiklik
left a comment
There was a problem hiding this comment.
Reviewed at 86d86910, after the rebase and force-push. CI is fully green (14/14, including deploy-scripts, which is what actually runs these shell tests, and shellcheck), and I confirmed the three points from @ingpaolodettori-dev's review are genuinely fixed rather than just claimed: ownership is now content-based (egress unit <path> <sha256> in the manifest, with egress_unit_is_ours / egress_claimed / egress_recorded_hash / egress_forget), the reuse path gates on is-active before any worker restart, and wait_egress_ready takes the env path so a reused unit's hint names the file that unit actually reads. The five new test blocks cover the P6-overwrite, inactive-reuse, custom-seed-digest and two-field-manifest cases.
Approving. Nothing blocking. Two of the eight inline notes are small regressions the fix commit introduced, both worth a follow-up push:
read -r kind path sha(install-host.sh:356) narrows parsing the two-field version got right — the third variable stopspathabsorbing the remainder, so a$SBX_UNIT_DIRcontaining a space now splits acrosspath/sha. Theoretical today, but it is a real narrowing.sha_ofin the test (:314) has a fallback that cannot fire:||binds to the pipeline, whose status comes fromcut. The productionsha256_ofgets this right withcommand -v.
The rest are nits plus one loose end: #623's proposed fix asks the installer to refuse a pre-P4.1 snapshot "or at least warn", and that is still prose-only in the RUNBOOK. Worth either a best-effort warning or a note in #623 saying why documentation is the right call, so the item does not read as unaddressed.
One housekeeping note on the corrected body: it now ends 🤖 Generated with [Claude Code], which contradicts CLAUDE.md:104 (Assisted-By, not Generated with). The commit trailers themselves are correct.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Shell (install-host.sh, tests/install-host.test.sh), systemd unit ([email protected]), Docs (RUNBOOK.md, README.md)
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed-off: yes
CI status: passing (14/14 at 86d86910)
| remove_egress() { | ||
| local kind path sha | ||
| [[ -f "$SBX_ENV_DIR/manifest" ]] || return 0 | ||
| while read -r kind path sha; do |
There was a problem hiding this comment.
suggestion — the third read variable narrows path handling that the two-field version got right.
With read -r kind path, path absorbed everything after the kind, so a $SBX_UNIT_DIR or $SBX_ENV_DIR containing a space still parsed correctly. With read -r kind path sha, path takes only the first whitespace-delimited field. Checked empirically:
$ printf 'unit /etc/sbx dir/moca-egress.service abc123\n' | while read -r kind path sha; do echo "path=[$path] sha=[$sha]"; done
path=[/etc/sbx] sha=[dir/moca-egress.service abc123]
So rm -f "$path" would target a directory, sha256_of "$path" would fail, and egress_forget would never match the line — it would be left in the manifest. The same split affects egress_claimed and egress_recorded_hash, which compare $3 == p.
This is theoretical today (nothing sets a spaced SBX_UNIT_DIR), hence a suggestion rather than a must-fix — but the previous code was immune and this is not. Two ways to close it: extend the PLAIN_PATH_RE guard that install_janitor already applies (:386, :394) to the install path as well, so a spaced directory is refused up front; or put the digest in field 2 (egress unit <sha256> <path>) so the path stays last and keeps absorbing the remainder.
| # Egress (#623): one moca-egress per host, a seeded allowlist, the worker wired to its socket. The | ||
| # state is clean here (no worker, no janitor, an empty manifest), so the first install installs the unit. | ||
| EM="$SBX_ENV_DIR/manifest" | ||
| sha_of() { shasum -a 256 "$1" 2>/dev/null | cut -d' ' -f1 || sha256sum "$1" | cut -d' ' -f1; } |
There was a problem hiding this comment.
nit — this fallback cannot fire.
shasum -a 256 "$1" 2>/dev/null | cut -d' ' -f1 || sha256sum "$1" | cut -d' ' -f1 — the || binds to the whole pipeline, and a pipeline's exit status is its last command's. With shasum missing, cut still succeeds on empty input, so the pipeline exits 0 and sha256sum is never reached:
$ nosuchcmd -a 256 f 2>/dev/null | cut -d' ' -f1 || echo FALLBACK_FIRED
(prints nothing)
On a host with only sha256sum (most Linux boxes — this is the GNU coreutils name, shasum is the perl script) sha_of returns the empty string, and the manifest assertion at line 332 fails with a confusing message instead of falling back. CI passes because ubuntu-latest happens to have both.
The production sha256_of (install-host.sh:87) already does this correctly with command -v sha256sum; mirroring that shape here would make the test fixture agree with the code it tests.
| else | ||
| # An env file already there is recorded only if nothing is: with no digest, uninstall falls back to | ||
| # comparing it with the seed. | ||
| egress_claimed file "$env" || record "egress file $env" |
There was a problem hiding this comment.
suggestion — a pre-existing env file is recorded without a digest, so this install's own file is judged by the env-dependent comparison the digest was added to replace.
That is the common re-run path: the second and every later install-host.sh for a host that already has moca-egress.env takes this branch, records egress file <path> with no third field, and remove_egress then falls back to "$(cat "$path")" != "$(egress_seed)" — reintroducing the dependency on the uninstaller's MOCA_EGRESS_ALLOW for exactly the file this script seeded on the first run.
The comment acknowledges the fallback, but there is no reason to take it when the file is right there to hash. Recording egress file $env $(sha256_of "$env") in both arms would make the digest universal and leave the two-field fallback for its intended case only — manifests written before digests existed.
The new test at 441-450 covers seed-then-uninstall in one run; it does not cover install, re-install, then uninstall.
| # The moca-egress.service this script installs: the P4 unit with this host's env file and without its | ||
| # comments (they name the P6 paths). | ||
| egress_unit_text() { | ||
| sed -e '/^#/d' -e "s#^EnvironmentFile=.*#EnvironmentFile=$SBX_ENV_DIR/moca-egress.env#" "$SCRIPT_DIR/../microvm/moca-egress.service" |
There was a problem hiding this comment.
nit — the generated unit carries no provenance header.
The janitor unit gets one (:437: # Installed by deploy/sbx/install-host.sh --janitor from deploy/sbx/sbx-janitor.service.). Here sed '/^#/d' correctly drops the source comments — they name the P6 paths — but puts nothing back, so an operator reading /etc/systemd/system/moca-egress.service on an sbx host sees a bare unit with no indication of where it came from or what owns it.
Now that ownership is content-hashed, a header earns its keep in the other direction too: it is the natural place to say "do not edit; install-host.sh owns this file and a hand edit makes it stop recognising it" — which is exactly what egress_unit_is_ours will do silently. Appending one line after egress_unit_text's sed would cover both.
| [[ "$SBX_ATTACH_SETTLE" =~ ^[1-9][0-9]*$ ]] || | ||
| die "SBX_ATTACH_SETTLE='$SBX_ATTACH_SETTLE' must be a whole number of seconds, at least 1, to outlast the worker's first reconnect backoff (at most 750ms)" | ||
| [[ -n "$BUNDLE" ]] || die 'usage: install-host.sh BUNDLE_DIR --snapshot IMAGE [--budget-mb N] | --uninstall ID | --janitor DIR | --uninstall-janitor' | ||
| [[ "$MOCA_EGRESS_READY_TIMEOUT" =~ ^[1-9][0-9]*$ ]] || |
There was a problem hiding this comment.
suggestion — this validation has no test, while both sibling rules do.
SBX_ATTACH_SETTLE=0 and the attach-timeout rule are each pinned in tests/install-host.test.sh with the "refuses before touching systemd" assertion pattern, but grep -c 'MOCA_EGRESS_READY_TIMEOUT=0' deploy/sbx/tests/install-host.test.sh returns 0. One block in the same shape as the existing ones would pin it.
| # override exists for tests only. The wait is as setup-microvm.sh's. | ||
| : "${MOCA_EGRESS_SOCKET:=/run/moca-egress/control.sock}" | ||
| : "${MOCA_EGRESS_READY_TIMEOUT:=30}" | ||
| : "${DEFAULT_EGRESS_ALLOW:=api.github.com,github.com,codeload.github.com,objects.githubusercontent.com}" |
There was a problem hiding this comment.
nit — this is now the third copy of the allowlist literal: deploy/microvm/setup-microvm.sh:49 has the identical default, deploy/microvm/tests/setup-microvm.test.sh:200 asserts it, and P4-ON-P6.md:591 documents it. Not actionable in this PR — there is no file both tiers already source — but worth a note so the next change to the default knows to update all of them.
| export SBX_BIN_DIR="$TMP/usrbin" MOCA_EGRESS_SOCKET="$TMP/run/control.sock" MOCA_EGRESS_READY_TIMEOUT=2 | ||
| mkdir -p "$SBX_BIN_DIR" "$TMP/run" | ||
| fake_egress() { : >"$SBX_BIN_DIR/moca-egress"; chmod +x "$SBX_BIN_DIR/moca-egress"; } | ||
| fake_socket() { rm -f "$MOCA_EGRESS_SOCKET"; python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); s.bind(sys.argv[1])' "$MOCA_EGRESS_SOCKET"; } |
There was a problem hiding this comment.
nit — python3 is unguarded here, unlike the sibling suite.
deploy/sbx/tests/smoke.test.sh:17 does REAL_PY="$(command -v python3)" || { echo 'SKIP: smoke.test.sh needs python3'; exit 0; }. CI's ubuntu-latest always has python3, so this only bites a local dev on a minimal host — but there the symptom is install_egress timing out on the control socket, which points at the wrong thing entirely rather than at a missing interpreter.
| `/etc/serverless-harness/moca-egress.env`. | ||
| - **`--no-egress`** installs a worker without `MOCA_EGRESS_CONTROL_SOCK` and installs no egress unit: | ||
| the worker behaves as before egress existed, and the guest has no network. | ||
| - **Snapshot:** a golden snapshot built before P4.1 has no guest egress listener. Rebuild it with |
There was a problem hiding this comment.
suggestion — #623's proposed fix asks that the installer "refuses a snapshot built before P4.1 when egress is on, or at least warns"; this documents the caveat but implements neither arm.
A check looks mechanically feasible: snapshots ship a manifest.json whose capabilities array is written by write_manifest (deploy/microvm/build-snapshot.sh:1691-1724) from probe_capabilities, and check_egress_listener (:907) already gates the build on the guest listener — so the capability is both recorded and readable at install time.
The real constraint is that jq is only required for --janitor today, not for a worker install, so a hard refusal would add a dependency to the common path. A best-effort warning — skipped silently when the manifest or jq is absent — gets the issue's weaker arm without that cost.
Either way worth resolving explicitly: implement the warning, or note in #623 why documentation-only is the right call, so that proposed-fix item does not read as unaddressed when the issue is closed.
#624 has merged, and this branch is rebased onto
main.What
Closes #623.
deploy/sbx/install-host.shnow installs and wiresmoca-egress, so an SBX KVM host gets guest egress without a manual drop-in. This is chunk 1 of the SBX0.2b plan and carries no credentials.moca-egressper host serves every template worker. The unit is generated fromdeploy/microvm/moca-egress.service, withEnvironmentFile=pointed at$SBX_ENV_DIR/moca-egress.envand comments dropped. It is started and its control socket waited for before any worker restarts. A socket timeout or a failed Type=notify start fails withjournalctl -u moca-egressnamed.MOCA_EGRESS_CONTROL_SOCK=/run/moca-egress/control.sock.[email protected]is orderedAfter=/Wants=moca-egress.service.MOCA_EGRESS_ALLOWor the v0.8.0 default. An operator's edit survives re-runs, and survives the last uninstall too.moca-egress.servicethat this script did not install already exists (a host also runningdeploy/microvm's P6), it is reused: not overwritten, not recorded, not removed./usr/local/bin/moca-egressis missing, the install is refused before any systemd call.--no-egressopts a worker out.Tests
deploy/sbx/tests/install-host.test.shpasses, including the new egress block: ordering, seeding, an edited allowlist kept, reuse of an existing unit, the missing-binary refusal,--no-egress, the socket timeout, a second worker that does not restart the shared proxy, and an inactive proxy getting reset-failed then start.setup.test.shpasses. shellcheck-x -S warningis clean, and Prettier is clean.Status
🤖 Generated with Claude Code