@ornncompute/cli 1.0.196 → 1.0.198

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
@@ -85,7 +85,11 @@ use the same context. Reserve browsing and purchasing still require approval.
85
85
  Node and VM requests use `@ornn/api-client/orchestrator`; CLI selection, polling,
86
86
  and SSH remain here. VM readiness comes from the guest resource, separately from
87
87
  physical-host readiness. Bare Metal uses the node's SSH endpoint and waits
88
- when the service reports that the endpoint is unavailable. See [machine behavior](../../docs/cli.md#customer-workflows).
88
+ when the service reports that the endpoint is unavailable. VM and Bare Metal
89
+ `connect` use the CLI's own key (`connect_ed25519` in the config directory),
90
+ saved as the user's `ornn connect (<email>)` organization key and assigned to the
91
+ reservation; `--identity-file` applies only to Slurm. See
92
+ [machine behavior](../../docs/cli.md#customer-workflows).
89
93
 
90
94
  Directory-enabled Slurm access requires `ornn connect <cluster-id> --user <directory-username>`
91
95
  and the matching personal SSH key (`--identity-file` if needed). Clusters without
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ornncompute/cli",
3
- "version": "1.0.196",
3
+ "version": "1.0.198",
4
4
  "description": "Command-line interface for Ornn compute access workflows.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1193,6 +1193,12 @@ export async function runCatalogCli(argv, io, { version }) {
1193
1193
  }
1194
1194
 
1195
1195
  const authSnapshot = {};
1196
+ const directConnectOptions = {
1197
+ env: io.env,
1198
+ now: io.now,
1199
+ sleep: io.sleep,
1200
+ spawnProcess: io.spawnProcess,
1201
+ };
1196
1202
  const execution = await executeCapability(request.capability, catalog, io, version, {
1197
1203
  argv: request.argv ?? [],
1198
1204
  executionRuntime: {
@@ -1248,8 +1254,8 @@ export async function runCatalogCli(argv, io, { version }) {
1248
1254
  state,
1249
1255
  io.connectModeHandlers ?? [
1250
1256
  createSlurmConnectModeHandler({ spawnProcess: io.spawnProcess }),
1251
- createBareMetalConnectModeHandler({ spawnProcess: io.spawnProcess }),
1252
- createVmConnectModeHandler({ spawnProcess: io.spawnProcess }),
1257
+ createBareMetalConnectModeHandler(directConnectOptions),
1258
+ createVmConnectModeHandler(directConnectOptions),
1253
1259
  kubernetesConnectModeHandler,
1254
1260
  ]
1255
1261
  ),
@@ -1267,8 +1273,8 @@ export async function runCatalogCli(argv, io, { version }) {
1267
1273
  };
1268
1274
  return continueConnectWithHandlers(normalized, [
1269
1275
  createSlurmConnectModeHandler({ spawnProcess: io.spawnProcess }),
1270
- createBareMetalConnectModeHandler({ spawnProcess: io.spawnProcess }),
1271
- createVmConnectModeHandler({ spawnProcess: io.spawnProcess }),
1276
+ createBareMetalConnectModeHandler(directConnectOptions),
1277
+ createVmConnectModeHandler(directConnectOptions),
1272
1278
  kubernetesConnectModeHandler,
1273
1279
  ]);
1274
1280
  },
@@ -19,10 +19,11 @@ import * as orchestrator from "../vendor/api-client/orchestrator.js";
19
19
  import { getReservation } from "../vendor/api-client/commerce.js";
20
20
  import { spawn as spawnProcessDefault } from "node:child_process";
21
21
  import { createHash } from "node:crypto";
22
- import { readFile } from "node:fs/promises";
22
+ import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
23
23
  import { homedir } from "node:os";
24
24
  import { isIP } from "node:net";
25
- import { identityFilePermissionProblem } from "./identity-file.mjs";
25
+ import { dirname, join } from "node:path";
26
+ import { getAuthConfigDir, loadAuthSession } from "./auth-store.mjs";
26
27
  import { isRecoverableWaitFailure } from "./interaction-runtime.mjs";
27
28
  import { resolvedMountPath } from "./mount-commands.mjs";
28
29
  import {
@@ -226,7 +227,10 @@ function sshInvocation(machine, input = {}, gateway) {
226
227
  throw new Error("SSH identity file contains control characters.");
227
228
  }
228
229
  const args = [];
229
- if (text(input.identityFile)) args.push("-i", expandPath(input.identityFile));
230
+ if (text(input.identityFile)) {
231
+ args.push("-i", expandPath(input.identityFile));
232
+ if (input.identitiesOnly) args.push("-o", "IdentitiesOnly=yes");
233
+ }
230
234
  const port = machinePort(machine);
231
235
  if (port !== 22) args.push("-p", String(port));
232
236
  const accessArgs = sshArguments({
@@ -2864,12 +2868,48 @@ async function resolveDirectConnectMachine(reference, context, interaction) {
2864
2868
  return selected;
2865
2869
  }
2866
2870
 
2871
+ /**
2872
+ * A VM named by its own ID is both the machine and the SSH target. Its host,
2873
+ * when it is in the same reservation, supplies the SSH jump gateway.
2874
+ */
2875
+ async function resolveVirtualMachineTarget(reference, context, interaction) {
2876
+ if (!CANONICAL_ID.test(text(reference) ?? "")) return null;
2877
+ const { reservations, virtualMachines } = await cachedDirectConnectCandidates(
2878
+ context,
2879
+ interaction
2880
+ );
2881
+ const index = virtualMachines.findIndex((payload) =>
2882
+ virtualMachinesInPayload(payload).some((vm) => vm.id === reference)
2883
+ );
2884
+ if (index < 0) return null;
2885
+ const virtualMachine = virtualMachinesInPayload(virtualMachines[index]).find(
2886
+ (vm) => vm.id === reference
2887
+ );
2888
+ const reservation = reservations[index];
2889
+ const hostId = text(virtualMachine.node_id);
2890
+ const host = hostId
2891
+ ? await resolveMachineReference(hostId, context, interaction, { allowMiss: true })
2892
+ : null;
2893
+ const hostReservationId = text(host?.reservation_id) ?? text(host?.commerce_reservation_id);
2894
+ return {
2895
+ host: hostReservationId === reservation.reservationId ? host : null,
2896
+ machine: virtualMachine,
2897
+ reservation,
2898
+ virtualMachine,
2899
+ };
2900
+ }
2901
+
2902
+ function virtualMachinesInPayload(payload) {
2903
+ const listed = record(payload).virtual_machines;
2904
+ return (Array.isArray(listed) ? listed : []).map(record);
2905
+ }
2906
+
2867
2907
  function cachedDirectConnectTarget(reference, context, interaction) {
2868
2908
  const cached = directConnectResolutionCache.get(context);
2869
2909
  if (cached?.reference === reference) return cached.target;
2870
2910
  const target = resolveDirectConnectMachine(reference, context, interaction).then(
2871
2911
  async (machine) => {
2872
- if (!machine) return null;
2912
+ if (!machine) return resolveVirtualMachineTarget(reference, context, interaction);
2873
2913
  const reservationId = text(machine.reservation_id) ?? text(machine.commerce_reservation_id);
2874
2914
  if (!reservationId) {
2875
2915
  interaction.error("The machine is missing its capacity-reservation reference.", {
@@ -2912,10 +2952,213 @@ async function directConnectCandidates(context, interaction, acceptedModes, labe
2912
2952
  }));
2913
2953
  }
2914
2954
 
2955
+ const CONNECT_KEY_FILE = "connect_ed25519";
2956
+ const CONNECT_KEY_WAIT_MS = 180_000;
2957
+ const CONNECT_KEY_POLL_MS = 3_000;
2958
+
2959
+ function runKeygen(spawnProcess, args) {
2960
+ return new Promise((resolve, reject) => {
2961
+ let stdout = "";
2962
+ const child = spawnProcess("ssh-keygen", args, { stdio: ["ignore", "pipe", "ignore"] });
2963
+ child.stdout?.on("data", (chunk) => {
2964
+ stdout += chunk;
2965
+ });
2966
+ child.on("error", reject);
2967
+ child.on("exit", (code) => resolve({ code: code ?? 1, stdout }));
2968
+ });
2969
+ }
2970
+
2971
+ /** The CLI's own key pair. Only the public half ever leaves this machine. */
2972
+ async function connectKeyFile(env, spawnProcess, interaction) {
2973
+ const path = join(getAuthConfigDir(env), CONNECT_KEY_FILE);
2974
+ const publicKey = async () => text(await readFile(`${path}.pub`, "utf8"));
2975
+ const existing = await stat(path).catch((error) => {
2976
+ if (error?.code === "ENOENT") return null;
2977
+ throw error;
2978
+ });
2979
+ if (existing) {
2980
+ try {
2981
+ return { path, publicKey: await publicKey() };
2982
+ } catch (error) {
2983
+ if (error?.code !== "ENOENT") throw error;
2984
+ }
2985
+ }
2986
+ await mkdir(dirname(path), { recursive: true, mode: 0o700 });
2987
+ let generated;
2988
+ try {
2989
+ if (existing) {
2990
+ generated = await runKeygen(spawnProcess, ["-y", "-f", path]);
2991
+ if (generated.code === 0) await writeFile(`${path}.pub`, generated.stdout, { mode: 0o644 });
2992
+ } else {
2993
+ await rm(`${path}.pub`, { force: true });
2994
+ generated = await runKeygen(spawnProcess, [
2995
+ "-q",
2996
+ "-t",
2997
+ "ed25519",
2998
+ "-N",
2999
+ "",
3000
+ "-C",
3001
+ "ornn-connect",
3002
+ "-f",
3003
+ path,
3004
+ ]);
3005
+ }
3006
+ } catch (error) {
3007
+ if (error?.code !== "ENOENT") throw error;
3008
+ generated = { code: 127 };
3009
+ }
3010
+ if (generated.code !== 0) {
3011
+ interaction.error("ornn connect needs OpenSSH's ssh-keygen to create its SSH key.", {
3012
+ code: "ssh_keygen_unavailable",
3013
+ });
3014
+ }
3015
+ return { path, publicKey: await publicKey() };
3016
+ }
3017
+
3018
+ /**
3019
+ * The user's one `ornn connect` organization key. A previous CLI key under the
3020
+ * same label is replaced, so a Cloud Shell, which keeps no files between
3021
+ * sessions, does not leave a key behind each time.
3022
+ */
3023
+ async function saveConnectKey(context, publicKey, label, interaction) {
3024
+ const keyFingerprint = fingerprint(publicKey);
3025
+ const keys = await fetchOrganizationKeys(context);
3026
+ const current = keys.find((key) => text(key.fingerprint) === keyFingerprint);
3027
+ if (current) return current;
3028
+ for (const stale of keys.filter((key) => text(key.label) === label)) {
3029
+ await sshKeyDeleteCapability.execute(context, {
3030
+ keyId: resourceId(stale),
3031
+ idempotencyKey: interaction.idempotencyKey(),
3032
+ });
3033
+ }
3034
+ return materializeKey(
3035
+ context,
3036
+ {
3037
+ kind: "create",
3038
+ key: {
3039
+ id: null,
3040
+ label,
3041
+ fingerprint: keyFingerprint,
3042
+ public_key: publicKey,
3043
+ status: "active",
3044
+ },
3045
+ },
3046
+ interaction,
3047
+ interaction.idempotencyKey()
3048
+ );
3049
+ }
3050
+
3051
+ async function connectKeyInstalled(state, key) {
3052
+ const { context, machine, reservationId } = state;
3053
+ const virtualMachineId = text(record(state.virtualMachine).id);
3054
+ if (virtualMachineId) {
3055
+ const installed = keysFromPayload(
3056
+ await orchestrator.listReservationSshKeys(context.fetch, reservationId, {
3057
+ virtual_machine_id: virtualMachineId,
3058
+ })
3059
+ );
3060
+ return installed.some((candidate) => resourceId(candidate) === resourceId(key));
3061
+ }
3062
+ const node = record(await orchestrator.getNode(context.fetch, machineId(machine)));
3063
+ return (
3064
+ sshKeyDeliveryStatus(key, {
3065
+ authorized_key_metadata: node.authorized_key_metadata,
3066
+ authorized_key_fingerprints: node.authorized_key_fingerprints ?? [],
3067
+ keys_pushed_at: node.keys_pushed_at,
3068
+ }).status === "installed"
3069
+ );
3070
+ }
3071
+
3072
+ /**
3073
+ * Saves the CLI's public key as the user's `ornn connect` SSH key, assigns it
3074
+ * to the target's reservation, and waits until the target has it. Returns the
3075
+ * private key path, or a finished result when the target never confirms it.
3076
+ */
3077
+ async function prepareConnectKey(state, { env, now, sleep, spawnProcess }) {
3078
+ const { access, context, interaction, machine, reservationId } = state;
3079
+ const displayName = machineHostname(machine) || "the selected machine";
3080
+ const resume = { kind: "machine", ref: machineId(machine) };
3081
+ const data = { machine: projectMachine(machine, state.virtualMachine ? access : undefined) };
3082
+ const local = await connectKeyFile(env, spawnProcess, interaction);
3083
+ const session = await loadAuthSession({ env });
3084
+ const owner = text(session?.user?.email) ?? text(session?.user?.id) ?? "CLI";
3085
+ const key = await saveConnectKey(
3086
+ context,
3087
+ local.publicKey,
3088
+ `ornn connect (${owner})`,
3089
+ interaction
3090
+ );
3091
+ await ensureAssignment(context, reservationId, key, interaction.idempotencyKey(), interaction);
3092
+ const deadline = now() + CONNECT_KEY_WAIT_MS;
3093
+ let informed = false;
3094
+ for (;;) {
3095
+ if (await connectKeyInstalled(state, key)) return { path: local.path };
3096
+ if (now() >= deadline) {
3097
+ return {
3098
+ result: {
3099
+ action: {
3100
+ command: `ornn connect ${machineId(machine)}`,
3101
+ description: "Retry once SSH access is installed",
3102
+ type: "command",
3103
+ },
3104
+ data,
3105
+ errors: [
3106
+ {
3107
+ code: "connect_key_delivery_pending",
3108
+ message: `${displayName} has not confirmed SSH access for ornn connect yet.`,
3109
+ retryable: true,
3110
+ },
3111
+ ],
3112
+ exitCode: 4,
3113
+ outcome: { class: "working", phase: "key_delivery_pending" },
3114
+ resume,
3115
+ presentation: {
3116
+ lines: [`${displayName} has not confirmed SSH access yet.`],
3117
+ next: `ornn connect ${machineId(machine)}`,
3118
+ },
3119
+ },
3120
+ };
3121
+ }
3122
+ if (!informed) {
3123
+ interaction.inform?.([`Setting up SSH access to ${displayName}...`]);
3124
+ informed = true;
3125
+ }
3126
+ await sleep(CONNECT_KEY_POLL_MS);
3127
+ }
3128
+ }
3129
+
3130
+ /** Kubernetes and Slurm members are reached through their cluster. */
3131
+ function connectClusterRefusal(state) {
3132
+ const { access, machine } = state;
3133
+ const clusterId =
3134
+ text(record(state.virtualMachine).controller_id) ??
3135
+ text(record(state.host).controller_id) ??
3136
+ text(machine.controller_id);
3137
+ if (!clusterId) return null;
3138
+ const message =
3139
+ "This machine is part of a Kubernetes or Slurm cluster. Connect to the cluster instead.";
3140
+ return {
3141
+ action: {
3142
+ command: `ornn connect ${clusterId}`,
3143
+ description: "Connect to the cluster instead",
3144
+ type: "command",
3145
+ },
3146
+ data: { machine: projectMachine(machine, state.virtualMachine ? access : undefined) },
3147
+ errors: [{ code: "connect_target_in_cluster", message, retryable: false }],
3148
+ exitCode: 1,
3149
+ outcome: { class: "failed", phase: "in_cluster" },
3150
+ resume: { kind: "machine", ref: machineId(machine) },
3151
+ presentation: { lines: [message], next: `ornn connect ${clusterId}` },
3152
+ };
3153
+ }
3154
+
2915
3155
  function createDirectMachineConnectModeHandler({
2916
3156
  acceptedModes,
2917
3157
  label,
2918
3158
  mode,
3159
+ env,
3160
+ now = () => Date.now(),
3161
+ sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
2919
3162
  spawnProcess = spawnProcessDefault,
2920
3163
  }) {
2921
3164
  return {
@@ -2937,19 +3180,21 @@ function createDirectMachineConnectModeHandler({
2937
3180
  input,
2938
3181
  interaction,
2939
3182
  machine,
3183
+ host: target.host ?? machine,
2940
3184
  access,
2941
3185
  mode,
2942
3186
  privateTransportRequired,
2943
3187
  reservation,
2944
3188
  reservationId,
2945
3189
  targetReference: reference,
3190
+ virtualMachine,
2946
3191
  };
2947
3192
  },
2948
3193
  matches(state) {
2949
3194
  return state.mode === mode;
2950
3195
  },
2951
3196
  async connect(state) {
2952
- const { input, interaction, machine, access } = state;
3197
+ const { context, input, interaction, machine, access } = state;
2953
3198
  if (input.output || input.overwrite) {
2954
3199
  interaction.usage(
2955
3200
  `${label} connect uses native SSH and does not write a kubeconfig or other output file.`,
@@ -2959,13 +3204,30 @@ function createDirectMachineConnectModeHandler({
2959
3204
  }
2960
3205
  );
2961
3206
  }
2962
- const reachability = machineReachability(machine, access);
3207
+ if (text(input.identityFile)) {
3208
+ interaction.inform?.([
3209
+ "--identity-file is ignored: ornn connect sets up its own SSH key for this machine.",
3210
+ ]);
3211
+ }
3212
+ const refusal = connectClusterRefusal(state);
3213
+ if (refusal) return refusal;
3214
+ let reachability = machineReachability(machine, access);
3215
+ let identityFile = null;
3216
+ if (reachability.class === "ready" || reachability.phase === "key_delivery_pending") {
3217
+ const prepared = await prepareConnectKey(state, { env, now, sleep, spawnProcess });
3218
+ if (prepared.result) return prepared.result;
3219
+ identityFile = prepared.path;
3220
+ reachability = { class: "ready", phase: "ssh_ready" };
3221
+ }
3222
+ const gatewayId = text(record(state.host ?? machine).ssh_gateway_id);
2963
3223
  const gateway =
2964
- machine.ssh_gateway_id && !state.privateNetwork
2965
- ? await orchestrator.getSupplierGateway(context.fetch, machine.ssh_gateway_id)
3224
+ gatewayId && !state.privateNetwork
3225
+ ? await orchestrator.getSupplierGateway(context.fetch, gatewayId)
2966
3226
  : undefined;
2967
3227
  const invocation =
2968
- reachability.class === "ready" ? sshInvocation(access, input, gateway) : null;
3228
+ reachability.class === "ready"
3229
+ ? sshInvocation(access, { user: input.user, identityFile, identitiesOnly: true }, gateway)
3230
+ : null;
2969
3231
  const resume = { kind: "machine", ref: machineId(machine) };
2970
3232
  const displayName = machineHostname(machine) || "the selected machine";
2971
3233
  const waitTarget = machineHostname(machine)
@@ -3032,15 +3294,6 @@ function createDirectMachineConnectModeHandler({
3032
3294
  ssh_command: command,
3033
3295
  };
3034
3296
  if (interaction.eligibleHuman && !interaction.machine && input.print !== true) {
3035
- const keyProblem = await identityFilePermissionProblem(
3036
- invocation.raw || !text(input.identityFile) ? null : expandPath(input.identityFile)
3037
- );
3038
- if (keyProblem) {
3039
- interaction.usage(keyProblem, {
3040
- code: "identity_file_permissions",
3041
- field: "identityFile",
3042
- });
3043
- }
3044
3297
  interaction.stderr?.write(
3045
3298
  `Opening SSH to ${displayName}.\nRun: ${command}\nWaiting for SSH to exit. Press Ctrl-C to stop only local SSH; the remote machine keeps running.\n`
3046
3299
  );
@@ -6356,7 +6356,7 @@ var connectCapability = defineCapability({
6356
6356
  roles: ["user"],
6357
6357
  mutation: "read",
6358
6358
  input: connectInput2,
6359
- description: "Connect to one exact native machine or cluster target through recoverable private transport when required.",
6359
+ description: "Connect to one exact native machine, VM, or cluster target through recoverable private transport when required. VM and Bare Metal SSH uses the CLI's own key, never your private key.",
6360
6360
  mcp: false,
6361
6361
  slack: false,
6362
6362
  cli: {
@@ -6366,11 +6366,12 @@ var connectCapability = defineCapability({
6366
6366
  audience: "customer",
6367
6367
  category: "journey",
6368
6368
  order: 80,
6369
- outcome: "Open or render native access from exact machine, cluster, or reservation facts.",
6370
- human: "ornn connect [<exact-hostname-or-canonical-machine-id-or-cluster-id-or-reservation-id>] [--identity-file <path>] [--user <login>] [--output <path>] [--print] [--overwrite]",
6371
- automation: "ornn connect <canonical-machine-or-cluster-id> [--identity-file <path>] [--user <login>] [--output <path>] [--print] [--overwrite] --format json",
6369
+ outcome: "Open or render native access from exact machine, VM, cluster, or reservation facts.",
6370
+ human: "ornn connect [<exact-hostname-or-canonical-machine-id-or-vm-id-or-cluster-id-or-reservation-id>] [--user <login>] [--output <path>] [--print] [--overwrite]; Slurm may add --identity-file <path>",
6371
+ automation: "ornn connect <canonical-machine-vm-or-cluster-id> [--user <login>] [--output <path>] [--print] [--overwrite] --format json; Slurm may add --identity-file <path>",
6372
6372
  acceptedReferences: [
6373
6373
  "canonical machine ID",
6374
+ "canonical VM ID",
6374
6375
  "canonical cluster ID",
6375
6376
  "exact unique hostname",
6376
6377
  "canonical capacity-reservation ID",