Public: anyone can read this; the crew can change it.
A game box's boot config is plain ASCII, so cloud-init stops dropping it #17112
Closed 47h 12m ago
sold/game-ascii → main 1 commit
2 files
changed +32
−6
src/games/boot.ts +16 −5
| @@ -51,6 +51,17 @@ WantedBy=multi-user.target | ||
| 51 | 51 | export const INSTALL = |
| 52 | 52 | "for try in 1 2 3; do apt-get -o DPkg::Lock::Timeout=600 update && DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=600 install -y docker.io docker-compose-v2 jq curl ca-certificates ufw unattended-upgrades && break; sleep 30; done"; |
| 53 | 53 | |
| 54 | /** | |
| 55 | * A YAML double-quoted string in plain ASCII: JSON's escapes plus `\uXXXX` / `\UXXXXXXXX` for every | |
| 56 | * character outside printable ASCII. A boot config reaches the cloud's API and cloud-init through | |
| 57 | * more than one encoder; a raw "…" arrived as U+0080 and cloud-init refused the whole file. | |
| 58 | */ | |
| 59 | const ascii = (s: string): string => | |
| 60 | JSON.stringify(s).replace(/[^\x20-\x7e]/gu, (c) => { | |
| 61 | const n = c.codePointAt(0)!; | |
| 62 | return n > 0xffff ? `\\U${n.toString(16).padStart(8, "0")}` : `\\u${n.toString(16).padStart(4, "0")}`; | |
| 63 | }); | |
| 64 | ||
| 54 | 65 | /** The cloud-config for a game box whose volume is mounted at `dataDir`. */ |
| 55 | 66 | export function gameBootConfig(g: GameBoot, dataDir: string): string { |
| 56 | 67 | const url = new URL(g.forgeUrl); |
| @@ -65,16 +76,16 @@ disable_root: true | ||
| 65 | 76 | write_files: |
| 66 | 77 | - path: ${GAME_DIR}/agent.env |
| 67 | 78 | permissions: "0600" |
| 68 | content: ${JSON.stringify(env)} | |
| 79 | content: ${ascii(env)} | |
| 69 | 80 | - path: ${GAME_DIR}/agent.sh |
| 70 | 81 | permissions: "0755" |
| 71 | content: ${JSON.stringify(AGENT_SH)} | |
| 82 | content: ${ascii(AGENT_SH)} | |
| 72 | 83 | - path: /etc/systemd/system/forge-game-agent.service |
| 73 | 84 | permissions: "0644" |
| 74 | content: ${JSON.stringify(AGENT_UNIT)} | |
| 85 | content: ${ascii(AGENT_UNIT)} | |
| 75 | 86 | - path: /etc/docker/daemon.json |
| 76 | 87 | permissions: "0644" |
| 77 | content: ${JSON.stringify(daemon + "\n")} | |
| 88 | content: ${ascii(daemon + "\n")} | |
| 78 | 89 | ${hardenFiles()} |
| 79 | 90 | runcmd: |
| 80 | 91 | - ufw default deny incoming |
| @@ -83,7 +94,7 @@ runcmd: | ||
| 83 | 94 | - ufw --force enable |
| 84 | 95 | - fallocate -l ${SWAP_GIB}G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile && echo '/swapfile none swap sw 0 0' >> /etc/fstab |
| 85 | 96 | - mkdir -p ${dataDir}/docker |
| 86 | - ${JSON.stringify(INSTALL)} | |
| 97 | - ${ascii(INSTALL)} | |
| 87 | 98 | - systemctl restart docker || true |
| 88 | 99 | - systemctl daemon-reload |
| 89 | 100 | - systemctl enable --now forge-game-agent.service |
test/game-box.test.ts +16 −1
| @@ -21,6 +21,11 @@ import { AGENT_SH, GAME_PORT, INSTALL, gameBootConfig } from "../src/games/boot. | ||
| 21 | 21 | import { boxByToken, boxNamed, createGameBox, rotateGameBoxToken } from "../src/games/store.ts"; |
| 22 | 22 | import { sha256 } from "../src/kernel/hash.ts"; |
| 23 | 23 | |
| 24 | /** The boot's own escaping: JSON plus \\uXXXX for anything outside printable ASCII. */ | |
| 25 | const asciiJson = (s: string): string => | |
| 26 | JSON.stringify(s).replace(/[^\x20-\x7e]/gu, (c) => `\\u${c.codePointAt(0)!.toString(16).padStart(4, "0")}`); | |
| 27 | ||
| 28 | ||
| 24 | 29 | const AGENT = join(import.meta.dir, "../src/games/agent.sh"); |
| 25 | 30 | const TOKEN = "fgb_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; |
| 26 | 31 | |
| @@ -29,7 +34,7 @@ describe("the boot config", () => { | ||
| 29 | 34 | |
| 30 | 35 | test("carries the agent and its unit, Docker with compose, and swap", () => { |
| 31 | 36 | expect(cfg.startsWith("#cloud-config\n")).toBe(true); |
| 32 | expect(cfg).toContain(`path: /opt/forge-game/agent.sh\n permissions: "0755"\n content: ${JSON.stringify(AGENT_SH)}`); | |
| 37 | expect(cfg).toContain(`path: /opt/forge-game/agent.sh\n permissions: "0755"\n content: ${asciiJson(AGENT_SH)}`); | |
| 33 | 38 | expect(cfg).toContain("path: /etc/systemd/system/forge-game-agent.service"); |
| 34 | 39 | expect(cfg).toContain("systemctl enable --now forge-game-agent.service"); |
| 35 | 40 | for (const pkg of ["docker.io", "docker-compose-v2", "jq", "curl", "ufw"]) expect(INSTALL).toContain(` ${pkg}`); |
| @@ -256,3 +261,13 @@ esac | ||
| 256 | 261 | } |
| 257 | 262 | }); |
| 258 | 263 | }); |
| 264 | ||
| 265 | test("a game box's boot config is plain ASCII, and cloud-init's YAML reads the agent back exactly", async () => { | |
| 266 | const { gameBootConfig, AGENT_SH } = await import("../src/games/boot.ts"); | |
| 267 | const cfg = gameBootConfig({ forgeUrl: "https://forge.example", token: `fgb_${"A".repeat(43)}` }, "/mnt/forge_mc"); | |
| 268 | // A raw "…" reached a real droplet as U+0080 and cloud-init dropped the whole file (2026-12-01). | |
| 269 | expect(/[^\x00-\x7f]/.test(cfg)).toBe(false); | |
| 270 | expect(/[^\x00-\x7f]/.test(AGENT_SH)).toBe(true); | |
| 271 | const parsed = Bun.YAML.parse(cfg) as { write_files: { path: string; content: string }[] }; | |
| 272 | expect(parsed.write_files.find((f) => f.path.endsWith("agent.sh"))?.content).toBe(AGENT_SH); | |
| 273 | }); |
new above
offline