Skip to content

Commit 91a316d

Browse files
committed
fix(ssh): mint pairing tokens with the live server's runtime
1 parent d3258a2 commit 91a316d

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ describe("ssh tunnel scripts", () => {
245245
'[ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && kill -0 "$REMOTE_PID" 2>/dev/null',
246246
);
247247
assert.notInclude(launch, "RUNNER_CHANGED");
248+
assert.include(launch, 'STARTED_RUNNER_FILE="$STATE_DIR/run-t3-started.sh"');
249+
assert.include(launch, 'if [ -f "$RUNNER_FILE" ] && [ ! -f "$STARTED_RUNNER_FILE" ]; then');
250+
assert.include(launch, 'cp "$RUNNER_FILE" "$STARTED_RUNNER_FILE"');
248251
assert.include(launch, "ensure_remote_node_path()");
249252
assert.include(launch, "if ! ensure_remote_node_path; then");
250253
assert.include(devLaunch, `T3_NODE_ENGINE_RANGE='${TEST_NODE_ENGINE_RANGE}'`);
@@ -263,6 +266,14 @@ describe("ssh tunnel scripts", () => {
263266
SshTunnel.buildRemotePairingScript(stateKey, ARCHIVE),
264267
'"$RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json',
265268
);
269+
assert.include(
270+
SshTunnel.buildRemotePairingScript(stateKey, ARCHIVE),
271+
'"$STARTED_RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json',
272+
);
273+
assert.include(
274+
SshTunnel.buildRemotePairingScript(stateKey, ARCHIVE),
275+
'kill -0 "$REMOTE_PID" 2>/dev/null; then',
276+
);
266277
assert.include(
267278
SshTunnel.buildRemotePairingScript(stateKey, ARCHIVE),
268279
'PAIRING_BASE_DIR="$DEFAULT_SERVER_HOME"',
@@ -929,6 +940,82 @@ exec "$REAL_NODE" "$@"
929940
if (ready) assert.deepEqual(JSON.parse(stdout), { remotePort: 3773, serverKind: "managed" });
930941
else assert.include(stderr, "server was left running");
931942
assert.equal(yield* fs.readFileString(`${state}/pid`), String(server.pid));
943+
// The pairing handshake needs the runtime that owns the live server.
944+
assert.equal(yield* fs.readFileString(`${state}/run-t3-started.sh`), "old runner");
932945
}).pipe(Effect.provide(NodeServices.layer)),
933946
);
934947
});
948+
949+
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("remote pairing script", () => {
950+
it.effect.each([{ live: true }, { live: false }] as const)(
951+
"uses the started runner while a managed server is live: %j",
952+
({ live }) =>
953+
Effect.gen(function* () {
954+
const fs = yield* FileSystem.FileSystem;
955+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
956+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-pairing-" });
957+
const state = `${root}/.t3/ssh-launch/test`;
958+
yield* fs.makeDirectory(state, { recursive: true });
959+
const startedRunner = `${state}/run-t3-started.sh`;
960+
yield* fs.writeFileString(
961+
startedRunner,
962+
`#!/bin/sh
963+
printf '{"credential":"started"}\\n'
964+
`,
965+
);
966+
yield* fs.chmod(startedRunner, 0o700);
967+
const nodeScript = `${root}/runner.mjs`;
968+
yield* fs.writeFileString(
969+
nodeScript,
970+
`console.log(JSON.stringify({ credential: "new" }));\n`,
971+
);
972+
if (live) {
973+
const server = yield* spawner.spawn(ChildProcess.make("sleep", ["120"]));
974+
yield* fs.writeFileString(`${state}/pid`, String(server.pid));
975+
yield* fs.writeFileString(`${state}/managed`, "managed");
976+
}
977+
const script = `${root}/pair.sh`;
978+
yield* fs.writeFileString(
979+
script,
980+
SshTunnel.buildRemotePairingScript("test", {
981+
nodeScriptPath: nodeScript,
982+
nodeEngineRange: ">=20",
983+
}),
984+
);
985+
const child = yield* spawner.spawn(
986+
ChildProcess.make("sh", [script], {
987+
env: {
988+
HOME: root,
989+
PATH: `${process.execPath.slice(0, process.execPath.lastIndexOf("/"))}:${process.env.PATH ?? ""}`,
990+
},
991+
extendEnv: false,
992+
}),
993+
);
994+
const [stdout, stderr, code] = yield* Effect.all(
995+
[
996+
child.stdout.pipe(
997+
Stream.decodeText(),
998+
Stream.runFold(
999+
() => "",
1000+
(a, b) => a + b,
1001+
),
1002+
),
1003+
child.stderr.pipe(
1004+
Stream.decodeText(),
1005+
Stream.runFold(
1006+
() => "",
1007+
(a, b) => a + b,
1008+
),
1009+
),
1010+
child.exitCode,
1011+
],
1012+
{ concurrency: "unbounded" },
1013+
);
1014+
assert.equal(Number(code), 0, stderr);
1015+
// A live managed server must never run the newer runner: its
1016+
// migrations would run underneath the older running server.
1017+
assert.equal(stdout.trim(), live ? '{"credential":"started"}' : '{"credential":"new"}');
1018+
assert.equal(yield* fs.exists(`${state}/run-t3.sh`), !live);
1019+
}).pipe(Effect.provide(NodeServices.layer)),
1020+
);
1021+
});

‎packages/ssh/src/tunnel.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ PID_FILE="$STATE_DIR/pid"
550550
MANAGED_FILE="$STATE_DIR/managed"
551551
LOG_FILE="$STATE_DIR/server.log"
552552
RUNNER_FILE="$STATE_DIR/run-t3.sh"
553+
STARTED_RUNNER_FILE="$STATE_DIR/run-t3-started.sh"
553554
RUNNER_NEXT="$STATE_DIR/run-t3.next.$$"
554555
mkdir -p "$STATE_DIR"
555556
cleanup_runner_next() {
@@ -559,6 +560,13 @@ trap cleanup_runner_next EXIT
559560
cat >"$RUNNER_NEXT" <<'SH'
560561
@@T3_RUNNER_SCRIPT@@
561562
SH
563+
# The pairing handshake must run under the runtime that owns the live server's
564+
# database. Until this launch replaces the server, the previous runner is the
565+
# best record of that runtime, so keep it for the pairing script.
566+
if [ -f "$RUNNER_FILE" ] && [ ! -f "$STARTED_RUNNER_FILE" ]; then
567+
cp "$RUNNER_FILE" "$STARTED_RUNNER_FILE"
568+
chmod 700 "$STARTED_RUNNER_FILE"
569+
fi
562570
mv "$RUNNER_NEXT" "$RUNNER_FILE"
563571
chmod 700 "$RUNNER_FILE"
564572
T3_ARCHIVE_MODE=@@T3_ARCHIVE_MODE@@
@@ -675,6 +683,8 @@ if [ -z "$REMOTE_PORT" ]; then
675683
printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
676684
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
677685
printf 'managed\\n' >"$MANAGED_FILE"
686+
cp "$RUNNER_FILE" "$STARTED_RUNNER_FILE"
687+
chmod 700 "$STARTED_RUNNER_FILE"
678688
if ! wait_ready "@@T3_READY_TIMEOUT_MS@@"; then
679689
printf 'Remote T3 server did not become ready on 127.0.0.1:%s.\\n' "$REMOTE_PORT" >&2
680690
if [ -s "$LOG_FILE" ]; then
@@ -695,12 +705,24 @@ const REMOTE_PAIRING_SCRIPT = `set -eu
695705
STATE_DIR="$HOME/.t3/ssh-launch/@@T3_STATE_KEY@@"
696706
DEFAULT_SERVER_HOME="$HOME/.t3"
697707
RUNNER_FILE="$STATE_DIR/run-t3.sh"
708+
STARTED_RUNNER_FILE="$STATE_DIR/run-t3-started.sh"
709+
PID_FILE="$STATE_DIR/pid"
710+
MANAGED_FILE="$STATE_DIR/managed"
698711
mkdir -p "$STATE_DIR"
712+
PAIRING_BASE_DIR="$DEFAULT_SERVER_HOME"
713+
REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
714+
REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
715+
# A live managed server keeps the runtime it was started with. Minting the
716+
# token with a newer runner would run that runner's database migrations
717+
# underneath the older running server.
718+
if [ "$REMOTE_MANAGED" != "external" ] && [ -n "$REMOTE_PID" ] && [ -x "$STARTED_RUNNER_FILE" ] && kill -0 "$REMOTE_PID" 2>/dev/null; then
719+
"$STARTED_RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json
720+
exit 0
721+
fi
699722
cat >"$RUNNER_FILE" <<'SH'
700723
@@T3_RUNNER_SCRIPT@@
701724
SH
702725
chmod 700 "$RUNNER_FILE"
703-
PAIRING_BASE_DIR="$DEFAULT_SERVER_HOME"
704726
"$RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json
705727
`;
706728

0 commit comments

Comments
 (0)