Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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 `<url>/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` |
Expand Down
60 changes: 60 additions & 0 deletions src/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"/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("/simlock/config.json")).gateway.token,
JSON.parse(await tightened.readFile("/simlock/config.json")).gateway.token,
]).toEqual(["slk_fresh", "slk_replaced"]);
expect({
created: (await fresh.stat("/simlock/config.json")).mode?.toString(8),
replaced: (await tightened.stat("/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(
"/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("/simlock/config.json")).gateway).toEqual({
url: "wss://gw.example:4700",
token: "slk_old",
label: "build-mac",
});
expect((await filesystem.stat("/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();
Expand Down
6 changes: 5 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
4 changes: 3 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading