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

opened this

Closed 47h 12m ago

sold/game-ascii → main 1 commit

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +32 −6
src/games/boot.ts +16 −5
@@ -51,6 +51,17 @@ WantedBy=multi-user.target
5151 export const INSTALL =
5252 "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";
5353
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
5465 /** The cloud-config for a game box whose volume is mounted at `dataDir`. */
5566 export function gameBootConfig(g: GameBoot, dataDir: string): string {
5667 const url = new URL(g.forgeUrl);
@@ -65,16 +76,16 @@ disable_root: true
6576 write_files:
6677 - path: ${GAME_DIR}/agent.env
6778 permissions: "0600"
68 content: ${JSON.stringify(env)}
79 content: ${ascii(env)}
6980 - path: ${GAME_DIR}/agent.sh
7081 permissions: "0755"
71 content: ${JSON.stringify(AGENT_SH)}
82 content: ${ascii(AGENT_SH)}
7283 - path: /etc/systemd/system/forge-game-agent.service
7384 permissions: "0644"
74 content: ${JSON.stringify(AGENT_UNIT)}
85 content: ${ascii(AGENT_UNIT)}
7586 - path: /etc/docker/daemon.json
7687 permissions: "0644"
77 content: ${JSON.stringify(daemon + "\n")}
88 content: ${ascii(daemon + "\n")}
7889 ${hardenFiles()}
7990 runcmd:
8091 - ufw default deny incoming
@@ -83,7 +94,7 @@ runcmd:
8394 - ufw --force enable
8495 - fallocate -l ${SWAP_GIB}G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile && echo '/swapfile none swap sw 0 0' >> /etc/fstab
8596 - mkdir -p ${dataDir}/docker
86 - ${JSON.stringify(INSTALL)}
97 - ${ascii(INSTALL)}
8798 - systemctl restart docker || true
8899 - systemctl daemon-reload
89100 - 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.
2121 import { boxByToken, boxNamed, createGameBox, rotateGameBoxToken } from "../src/games/store.ts";
2222 import { sha256 } from "../src/kernel/hash.ts";
2323
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
2429 const AGENT = join(import.meta.dir, "../src/games/agent.sh");
2530 const TOKEN = "fgb_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
2631
@@ -29,7 +34,7 @@ describe("the boot config", () => {
2934
3035 test("carries the agent and its unit, Docker with compose, and swap", () => {
3136 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)}`);
3338 expect(cfg).toContain("path: /etc/systemd/system/forge-game-agent.service");
3439 expect(cfg).toContain("systemctl enable --now forge-game-agent.service");
3540 for (const pkg of ["docker.io", "docker-compose-v2", "jq", "curl", "ufw"]) expect(INSTALL).toContain(` ${pkg}`);
@@ -256,3 +261,13 @@ esac
256261 }
257262 });
258263 });
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 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs