diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 267ff756a..e1e25a4f9 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -5,6 +5,11 @@ defaults. Only the keys below are recognized; unknown keys are ignored with a warning. Inspect the effective, merged configuration at any time with `simlock config`. +`simlock config set` writes `config.json` readable and writable by your user +only (mode `0600`), because it can hold `gateway.token`. A file you create or +edit yourself keeps the mode you give it until the next `config set`; run +`chmod 600 ~/.simlock/config.json` on one that holds a token. + | Property | Description | Default | | ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | | `mode` | Which shape this daemon runs as: `worker` (owns devices on this machine) or `gateway` (owns none, fronts the workers that join it). See [Modes: gateway and worker](#modes-gateway-and-worker). | `worker` | @@ -22,7 +27,7 @@ a warning. Inspect the effective, merged configuration at any time with | `lease.requestRetentionMs` | How long a settled lease request stays stored, counted from when it settled. A client that repeats a request with the same `idempotencyKey` inside this window gets the stored result back; after it, the record is removed and the same key starts a new request. | `10 minutes` | | `lease.maxRequestRecords` | The most lease-request records Simlock keeps. When a new request would go over it, the oldest settled record is removed. A request that is still open is never removed. | `10000` | | `gateway.url` | **Worker side.** Base URL of the gateway this worker joins, e.g. `wss://gw.example:4700`; the worker dials `/v1/uplink` and upgrades it to a WebSocket. Joining grants that gateway the `admin` role on this daemon, and `gateway.token` rides on the upgrade request as a bearer credential, so use `wss://` — or plain `ws://` only over loopback or inside your own tunnel. `http://` and `https://` are rejected at load. Unset means "do not join a fleet": the default, and the only thing that changes about a joined worker. | unset | -| `gateway.token` | **Worker side.** The join token (`simlock token create --role worker`, minted on the gateway) this worker presents when it opens its uplink. Required whenever `gateway.url` is set. `simlock config` shows it; the daemon log and the `daemon.started` event show `[redacted]`. | unset | +| `gateway.token` | **Worker side.** The join token (`simlock token create --role worker`, minted on the gateway) this worker presents when it opens its uplink. Required whenever `gateway.url` is set. `simlock config` shows it; the daemon log and the `daemon.started` event show `[redacted]`. It is stored in plain text in `config.json`, which `simlock config set` writes owner-only. | unset | | `gateway.label` | **Worker side.** Display name for this worker in `simlock worker list`, `status`, the console, and on the lease's `worker` block. Display-only: nothing routes on it and it need not be unique. | the worker's own id | | `exec.timeoutMs` | **Worker side.** How long one `device.exec` command (`simlock simctl` / `simlock adb` against a gateway or over HTTP) may run before the worker kills it and the operation fails with `EXEC_TIMEOUT`. Authoritative: it bounds the process that actually runs. | `10 minutes` | | `gateway.routing` | **Gateway side.** Which routing policy places a queued request on a worker. `warm-then-free` is the only policy in v1: among workers that can serve the request, warm hit first, then the most free running capacity for the platform. | `warm-then-free` | diff --git a/src/cli/index.test.ts b/src/cli/index.test.ts index e9eaff422..cf11fe139 100644 --- a/src/cli/index.test.ts +++ b/src/cli/index.test.ts @@ -1708,6 +1708,66 @@ describe("CLI: config set validates with the real config loader (ADR 0003 §11, }); }); +describe("CLI: config set and the file mode of config.json (#252)", () => { + it("writes the config.json holding gateway.token owner-only, whether it creates the file or replaces an owner-only one", async () => { + // Two homes, one per way `config set` comes to write the join token. `MemoryFilesystem` + // gives a file written without a `mode` Node's default (0644), so the assertion pins what + // the CLI asks for, independent of the umask of whoever runs the suite. + const fresh = new MemoryFilesystem(); + const freshExit = await runCli( + ["config", "set", "gateway", '{"url":"wss://gw.example:4700","token":"slk_fresh"}'], + outputCapture(realCliEnvironmentPorts(fresh)).environmentWith(), + ); + + const tightened = new MemoryFilesystem(); + await tightened.mkdirp("/simlock"); + await tightened.writeFileAtomic( + "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/simlock/config.json", + `${JSON.stringify({ gateway: { url: "wss://gw.example:4700" } })}\n`, + { mode: 0o600 }, + ); + const tightenedExit = await runCli( + ["config", "set", "gateway.token", "slk_replaced"], + outputCapture(realCliEnvironmentPorts(tightened)).environmentWith(), + ); + + expect([freshExit, tightenedExit]).toEqual([0, 0]); + expect([ + JSON.parse(await fresh.readFile("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/simlock/config.json")).gateway.token, + JSON.parse(await tightened.readFile("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/simlock/config.json")).gateway.token, + ]).toEqual(["slk_fresh", "slk_replaced"]); + expect({ + created: (await fresh.stat("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/simlock/config.json")).mode?.toString(8), + replaced: (await tightened.stat("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/simlock/config.json")).mode?.toString(8), + }).toEqual({ created: "600", replaced: "600" }); + }); + + it("makes a config.json that others could read owner-only on the next config set", async () => { + // A file written by hand or by an older simlock: readable by group and others. `config set` + // must not carry that mode over to the file it writes in its place. + const filesystem = new MemoryFilesystem(); + await filesystem.mkdirp("/simlock"); + await filesystem.writeFileAtomic( + "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/simlock/config.json", + `${JSON.stringify({ gateway: { url: "wss://gw.example:4700", token: "slk_old" } })}\n`, + { mode: 0o644 }, + ); + + const exit = await runCli( + ["config", "set", "gateway.label", "build-mac"], + outputCapture(realCliEnvironmentPorts(filesystem)).environmentWith(), + ); + + expect(exit).toBe(0); + expect(JSON.parse(await filesystem.readFile("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/simlock/config.json")).gateway).toEqual({ + url: "wss://gw.example:4700", + token: "slk_old", + label: "build-mac", + }); + expect((await filesystem.stat("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/simlock/config.json")).mode?.toString(8)).toBe("600"); + }); +}); + describe("CLI: --json shape is the contract, as-is (ADR 0003 §11)", () => { it("status --json has no snake_case renderings", async () => { const output = outputCapture(); diff --git a/src/cli/index.ts b/src/cli/index.ts index 415d0c8db..6dd4215f7 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -440,7 +440,11 @@ export function buildCliEnvironment( readStdin: ports.readStdin ?? readPipedStdin, writeConfigFile: async (contents) => { await filesystem.mkdirp(dataDirectory); - await filesystem.writeFileAtomic(configPath, `${JSON.stringify(contents, null, 2)}\n`); + // Owner-only on every write: the file can hold `gateway.token`, and the atomic rename + // replaces the old file, so a mode the user set by hand would not survive otherwise. + await filesystem.writeFileAtomic(configPath, `${JSON.stringify(contents, null, 2)}\n`, { + mode: 0o600, + }); }, // ADR §11 part D: "validates the merged file through the config loader before writing." // B9: two things the pre-fix version got wrong -- diff --git a/src/core/config.ts b/src/core/config.ts index 828b01ad0..9e3255e71 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -248,7 +248,9 @@ export interface Config { /** A join token minted on the gateway with `simlock token create --role worker`. * Worker-side. It is a secret, and it sits in the daemon's config file like any other * value there: `config.get` is admin-role and returns the config as written, so this key - * is exactly as sensitive as `config.json`'s file permissions make it. Everywhere else the + * is exactly as sensitive as `config.json`'s file permissions make it. `simlock config + * set` writes that file owner-only (0600) every time; a file written by hand, or before + * that, keeps whatever mode it has until the next `config set`. Everywhere else the * config is written out -- the startup log line, `daemon.started` -- goes through * `redactConfig`. See docs/CONFIGURATION.md. */ readonly token?: string;