shadok-ai 0.17.51 → 0.17.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -599,6 +599,24 @@ across OS users (Claude rewrites the credential single-owner on refresh), which
599
599
  is why each user logs in separately. Design:
600
600
  `docs/superpowers/specs/2026-11-04-connectors-via-global-login-design.md`.
601
601
 
602
+ **Git over SSH, per group.** Each group has its own SSH identity: the shared
603
+ *commons* (every public group) reuses the cockpit's historical container key —
604
+ the one already registered on your forge — and every **walled** group (restricted
605
+ or private) gets a key of its own. The keypairs live on the `shadok-data` volume,
606
+ so they survive a recreate even though the agent homes do not, and the private
607
+ half never leaves the server's own 0700 home: a copy is written into each
608
+ agent's `~/.ssh` as that OS user.
609
+
610
+ A key is only an identity once a remote accepts it, so **read a group's public
611
+ key and register it** where its agents must clone or push: `GET /groups/ssh`
612
+ returns one row per group you can see — `{group, name, osUser, publicKey,
613
+ shared}` — and `shared: true` marks the historical key, which you register once
614
+ for every public group. The rows go through the same visibility filter as
615
+ `GET /groups` (a private group stays invisible even to an admin), and only the
616
+ `.pub` half is ever read. A `publicKey: null` means no keypair has been minted
617
+ yet, not an error. Until a group's key is registered, its agents simply cannot
618
+ reach that remote over SSH.
619
+
602
620
  **Persist the group homes (recommended in Docker).** Each dropped agent runs as
603
621
  its own OS user with HOME under `SHADOK_HOME_BASE` (default `/home`), which is
604
622
  **not** on the `shadok-data` volume by default — so a `docker rm` + recreate
@@ -1207,7 +1225,8 @@ client, or immediately on an explicit `stop` (which ends it for everyone).
1207
1225
 
1208
1226
  HTTP endpoints (same auth): `/usage`, `/live`, `/sessions`, `/recover`,
1209
1227
  `/diff`, `/channels` (its GET adds a **derived** `crons` field — never stored),
1210
- `/channel` (DELETE), `/groups`, `/crons`, `/timezone`, `/title` (GET/PUT — the
1228
+ `/channel` (DELETE), `/groups`, `/groups/ssh` (GET — the SSH **public** key to
1229
+ register per group, filtered like `/groups`), `/crons`, `/timezone`, `/title` (GET/PUT — the
1211
1230
  cockpit's name, per launch directory: click the header brand to rename it, so
1212
1231
  several cockpits stay apart in the tab bar; an empty PUT reverts to the default),
1213
1232
  `/theme` (GET/PUT — the cockpit's colour palette, per launch directory: an accent
package/dist/server.js CHANGED
@@ -35,7 +35,7 @@ import { starCount } from "./stars.js";
35
35
  import { announceFirstAgent, countMainChannels, ensureFirstAgent, firstAgentStatus, settleFirstAgent, } from "./first-agent.js";
36
36
  import { readGroundAt } from "./ground.js";
37
37
  import { homeGreetingBrief } from "./greeting.js";
38
- import { ensureSshIdentity } from "./ssh.js";
38
+ import { ensureSshIdentity, groupKeyRows, volumeKeyPaths } from "./ssh.js";
39
39
  import { accountsEnabled, deploymentMode, probeCanProvision } from "./capability.js";
40
40
  import { migrationPlan, runMigration, respawnPlan, commonsOsUser, COMMONS_USER, MIGRATION_MARKER_FILE, rewriteCronCheckHome, strandedCheckDirs, serverOwnedStatePaths, } from "./migrate-groups.js";
41
41
  import { relayOAuthCallback } from "./oauth-relay.js";
@@ -2583,6 +2583,38 @@ app.get("/groups", (req, res) => {
2583
2583
  // Mono-tenant (only a public Main) keeps Main for everyone, unchanged.
2584
2584
  res.json(listableGroups(loadGroups(), viewerFor(currentAccount(req))));
2585
2585
  });
2586
+ /**
2587
+ * The SSH public key to register for each group the caller can see.
2588
+ *
2589
+ * A public key is not a secret, but WHICH GROUPS EXIST is: the rows go through the
2590
+ * very same `listableGroups` filter as `GET /groups`, so a private group stays
2591
+ * invisible even to an admin (operator decision 2026-11-03) and a restricted
2592
+ * group's existence is not leaked to a non-member. Never the private key, which
2593
+ * no endpoint can reach by construction — `groupKeyRows` only ever reads `.pub`.
2594
+ *
2595
+ * `publicKey: null` is an honest "no keypair yet" (mono-tenant before any boot
2596
+ * minted one, or a group provisioned by an older build), not an error.
2597
+ */
2598
+ app.get("/groups/ssh", (req, res) => {
2599
+ const groups = listableGroups(loadGroups(), viewerFor(currentAccount(req)));
2600
+ res.json(groupKeyRows(groups, {
2601
+ serverHome: os.homedir(),
2602
+ commonsUser: commonsOsUser({ migrated: groupsMigrated, mode: DEPLOYMENT_MODE }),
2603
+ // A walled group has an OS user of its OWN only under isolation. On a
2604
+ // mono-tenant cockpit every agent runs as the server's user and shares the
2605
+ // one identity, so naming `sk-<id>` here would point the operator at a key
2606
+ // directory nothing will ever read — a plausible, complete and wrong answer.
2607
+ osUserFor: (g) => (DEPLOYMENT_MODE === "multi-tenant" ? osUserFor(g) : null),
2608
+ readPub: (p) => {
2609
+ try {
2610
+ return fs.readFileSync(p, "utf8");
2611
+ }
2612
+ catch {
2613
+ return null; // not minted yet (or unreadable): the row says so
2614
+ }
2615
+ },
2616
+ }));
2617
+ });
2586
2618
  app.put("/groups", (req, res) => {
2587
2619
  // BROWSER-ONLY (T2/T6 carry): writing the group list is a person's act, and
2588
2620
  // `requestFromBrowser` refuses any request carrying an agent key or a peer
@@ -3087,6 +3119,10 @@ async function ensureGroupsProvisioned() {
3087
3119
  // invite-redeem hook nor the per-account migration loop ever fires for it.
3088
3120
  // Give it a Personal group here; idempotent (reuses an existing private group).
3089
3121
  await ensurePrivateGroupProvisioned(BOOTSTRAP_ADMIN);
3122
+ // Each group's keypair on the volume, BEFORE any HOME is filled: that is what
3123
+ // lets an operator register the public key (GET /groups/ssh) without waiting
3124
+ // for the group's first agent to spawn.
3125
+ ensureGroupVolumeKeys();
3090
3126
  // Before any agent comes back: fill every drop user's HOME with the full set
3091
3127
  // of trusted directories in one write each, so none strands on the trust
3092
3128
  // dialog when the resume storm rebuilds a wiped (ephemeral) group HOME.
@@ -6045,42 +6081,93 @@ function writeAsUserAtomic(user, file, content) {
6045
6081
  throw new Error(`mv ${tmp} → ${file} failed`);
6046
6082
  }
6047
6083
  /** The SSH key store on the shadok-data VOLUME (survives a docker recreate, which
6048
- * wipes `/home`). The container's historical identity lives here (`id_ed25519`);
6049
- * per-group keys go under `groups/<os-user>/`. `/root` is 0700, so a dropped
6050
- * agent cannot read this — the server (root) copies the material into the
6051
- * agent's own HOME at spawn. Mirrors `ensureSshIdentity` (src/ssh.ts). */
6052
- const SHADOK_SSH_DIR = path.join(os.homedir(), ".shadok-ai", "ssh");
6084
+ * wipes `/home`). The container's historical identity lives there (`id_ed25519`);
6085
+ * per-group keys under `groups/<os-user>/`. `/root` is 0700, so a dropped agent
6086
+ * cannot read it — the server (root) copies the material into the agent's own
6087
+ * HOME, as that user. The paths themselves come from `volumeKeyPaths`
6088
+ * (src/ssh.ts), so the provisioner and `GET /groups/ssh` cannot disagree about
6089
+ * where a group's key lives. Mirrors `ensureSshIdentity`. */
6053
6090
  const SSH_AGENT_CONFIG = "# Managed by shadok-ai — SSH identity for this agent.\n" +
6054
6091
  "Host *\n StrictHostKeyChecking accept-new\n UserKnownHostsFile ~/.ssh/known_hosts\n IdentityFile ~/.ssh/id_ed25519\n IdentitiesOnly yes\n";
6092
+ /**
6093
+ * Mint-or-find a drop user's keypair ON THE VOLUME (it must survive a recreate,
6094
+ * which wipes `/home`). The COMMONS reuses the container's HISTORICAL key — the
6095
+ * identity already registered on the forge (operator: "récupère la clef
6096
+ * historique") — while each ISOLATED group gets its own under `groups/<os-user>/`
6097
+ * (operator decision: one key per group); `volumeKeyPaths` is where that split
6098
+ * lives, so this and `GET /groups/ssh` always look in the same directory.
6099
+ * Idempotent: an existing keypair is returned untouched, which is what keeps a
6100
+ * group's identity STABLE across recreates and respawns.
6101
+ */
6102
+ function ensureVolumeKeypair(user) {
6103
+ const p = volumeKeyPaths(os.homedir(), user, COMMONS_USER);
6104
+ if (!fs.existsSync(p.key)) {
6105
+ // No historical key for the commons (a fresh instance), or a new group: mint
6106
+ // one on the VOLUME, as root. `accept-new` means the remote is trusted on
6107
+ // first use; registering the PUBLIC line on the forge is the operator's step,
6108
+ // which is why it is served by `GET /groups/ssh` and logged here.
6109
+ fs.mkdirSync(p.dir, { recursive: true, mode: 0o700 });
6110
+ execFileSync("ssh-keygen", ["-t", "ed25519", "-N", "", "-q", "-f", p.key, "-C", `shadok-${user}`]);
6111
+ const line = (() => {
6112
+ try {
6113
+ return fs.readFileSync(p.pub, "utf8").trim();
6114
+ }
6115
+ catch {
6116
+ return "(unreadable)";
6117
+ }
6118
+ })();
6119
+ console.log(`ssh: minted a keypair for ${user} on the volume — register it where its agents must push: ${line}`);
6120
+ }
6121
+ return p;
6122
+ }
6123
+ /**
6124
+ * Mint the VOLUME keypair of every group that has one, without touching a single
6125
+ * agent HOME. Called from the boot provisioning reconciler so a public key exists
6126
+ * to REGISTER before the group's first agent ever spawns — until this ran, the key
6127
+ * appeared at first spawn and only in the log, so an operator had to go read a log
6128
+ * to learn what to paste on the forge (the follow-up the isolation work left open).
6129
+ * Idempotent and best-effort: a `ssh-keygen` failure must never take down a boot,
6130
+ * and the spawn path still mints what is missing.
6131
+ */
6132
+ function ensureGroupVolumeKeys() {
6133
+ if (DEPLOYMENT_MODE !== "multi-tenant")
6134
+ return;
6135
+ const users = new Set([COMMONS_USER]);
6136
+ for (const g of loadGroups()) {
6137
+ const u = osUserFor(g);
6138
+ if (u)
6139
+ users.add(u);
6140
+ }
6141
+ for (const user of users) {
6142
+ try {
6143
+ ensureVolumeKeypair(user);
6144
+ }
6145
+ catch (e) {
6146
+ console.log(`ssh: could not mint a keypair for ${user} (best-effort) — ${e instanceof Error ? e.message : String(e)}`);
6147
+ }
6148
+ }
6149
+ }
6055
6150
  /**
6056
6151
  * Give a DROPPED agent a working `~/.ssh` (git-over-SSH, private clones).
6057
6152
  *
6058
- * The source keypair lives on the VOLUME under `SHADOK_SSH_DIR` (persists a
6059
- * recreate): the COMMONS (`main`) reuses the container's HISTORICAL key — the
6060
- * identity already registered on the forge (operator: "récupère la clef
6061
- * historique") — while each ISOLATED group has its OWN key under
6062
- * `groups/<os-user>/` (operator decision: one key per group). A dropped user
6063
- * cannot read the volume dir (it is under the 0700 `/root`), so the server
6064
- * (root) reads/generates the keypair there and writes a COPY into the agent's
6065
- * own HOME AS THE USER (never root writing through a 0700 HOME). Idempotent
6066
- * (skips when the HOME key is already present — so a recreate re-copies the same
6067
- * volume key, keeping the identity stable). Best-effort: SSH just stays
6068
- * unavailable to that agent on failure, never a spawn failure.
6153
+ * A dropped user cannot read the volume key store (it is under the 0700 `/root`),
6154
+ * so the server (root) takes the keypair from `ensureVolumeKeypair` and writes a
6155
+ * COPY into the agent's own HOME **as that user** — never root writing through a
6156
+ * 0700 HOME it does not own, where an agent could have planted a symlink (the
6157
+ * same rule as `writeAsUserAtomic` and the provisioning plans' root/user split).
6158
+ * Idempotent (skips the moment the HOME key is present, so a recreate re-copies
6159
+ * the same volume key and the identity stays stable). Best-effort: on failure SSH
6160
+ * is simply unavailable to that agent, never a failed spawn.
6161
+ *
6162
+ * Called from the spawn path AND from `preseedGroupHomes` at boot — the second is
6163
+ * what covers an agent that is merely RESUMED, and a HOME wiped by a recreate.
6069
6164
  */
6070
6165
  function ensureAgentSshKey(user, home) {
6071
6166
  try {
6072
6167
  const ssh = `${home}/.ssh`;
6073
6168
  if (spawnAsUserSync(user, "test", ["-f", `${ssh}/id_ed25519`]).ok)
6074
6169
  return; // already wired
6075
- const isCommons = user === COMMONS_USER;
6076
- const srcDir = isCommons ? SHADOK_SSH_DIR : path.join(SHADOK_SSH_DIR, "groups", user);
6077
- const srcKey = path.join(srcDir, "id_ed25519");
6078
- if (!fs.existsSync(srcKey)) {
6079
- // No historical key for the commons (a fresh instance), or a new group:
6080
- // mint one on the VOLUME, as root. accept-new keys are registered later.
6081
- fs.mkdirSync(srcDir, { recursive: true, mode: 0o700 });
6082
- execFileSync("ssh-keygen", ["-t", "ed25519", "-N", "", "-q", "-f", srcKey, "-C", `shadok-${user}`]);
6083
- }
6170
+ const { key: srcKey, shared: isCommons } = ensureVolumeKeypair(user);
6084
6171
  const priv = fs.readFileSync(srcKey, "utf8");
6085
6172
  const pub = fs.readFileSync(`${srcKey}.pub`, "utf8");
6086
6173
  spawnAsUserSync(user, "mkdir", ["-p", ssh]);
@@ -6292,6 +6379,12 @@ function preseedGroupHomes() {
6292
6379
  const home = homeForUser(user, GROUP_HOME_BASE);
6293
6380
  seedHomeTrust(user, home, [...cwds]);
6294
6381
  ensureTierStateDir(user, home);
6382
+ // `~/.ssh` too, at BOOT and not only at the next spawn. `/home` is NOT on the
6383
+ // volume (invariant 19's whole reason), so a docker recreate wipes every HOME
6384
+ // copy while the keypair survives on the volume — and a dropped agent that is
6385
+ // merely RESUMED never runs the spawn path, so it stayed without an identity
6386
+ // until someone reloaded it. Idempotent: skipped the moment the key is there.
6387
+ ensureAgentSshKey(user, home);
6295
6388
  }
6296
6389
  seedCommonsPeersOut();
6297
6390
  }