@selvajs/cli 4.20.1-beta.4 → 4.21.0-beta.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selvajs/cli",
3
- "version": "4.20.1-beta.4",
3
+ "version": "4.21.0-beta.10",
4
4
  "description": "Scaffold and operate a Selva white-label deployment. `npx @selvajs/cli <dir>` to bootstrap, `selva <cmd>` to manage.",
5
5
  "license": "MIT",
6
6
  "author": "VektorNode",
@@ -46,6 +46,6 @@
46
46
  "scripts": {
47
47
  "start": "node ./bin/cli.js",
48
48
  "selva": "node ./bin/selva.js",
49
- "test": "node --check ./bin/cli.js && node --check ./bin/selva.js && node --input-type=module -e \"for (const m of ['./src/cli.js','./src/prompts.js','./src/env.js','./src/paths.js','./src/secrets.js','./src/node-range.js','./src/deployment-package.js','./src/commands/create.js','./src/commands/init.js','./src/commands/doctor.js','./src/commands/pm2.js','./src/commands/migrate.js','./src/commands/keys.js','./src/commands/config.js','./src/env-migration.js']) { await import(m); }\" && node --test \"./src/__tests__/*.test.js\""
49
+ "test": "node --check ./bin/cli.js && node --check ./bin/selva.js && node --input-type=module -e \"for (const m of ['./src/cli.js','./src/prompts.js','./src/env.js','./src/paths.js','./src/secrets.js','./src/node-range.js','./src/deployment-package.js','./src/commands/create.js','./src/commands/init.js','./src/commands/doctor.js','./src/commands/pm2.js','./src/commands/migrate.js','./src/commands/keys.js','./src/commands/config.js','./src/commands/setup-url.js','./src/env-migration.js']) { await import(m); }\" && node --test \"./src/__tests__/*.test.js\""
50
50
  }
51
51
  }
package/src/cli.js CHANGED
@@ -16,6 +16,7 @@ const COMMANDS = {
16
16
  update: () => import('./commands/pm2.js').then((m) => m.runUpdate),
17
17
  migrate: () => import('./commands/migrate.js').then((m) => m.runMigrate),
18
18
  'setup-proxy': () => import('./commands/proxy.js').then((m) => m.runSetupProxy),
19
+ 'setup-url': () => import('./commands/setup-url.js').then((m) => m.runSetupUrl),
19
20
  keys: () => import('./commands/keys.js').then((m) => keysDispatch(m)),
20
21
  config: () => import('./commands/config.js').then((m) => m.runConfig)
21
22
  };
@@ -67,6 +68,11 @@ const USAGE = {
67
68
  ['--acme-email', "Address Let's Encrypt sends expiry notices to."]
68
69
  ]
69
70
  },
71
+ 'setup-url': {
72
+ usage: 'selva setup-url',
73
+ blurb:
74
+ 'Print the link that opens /setup with its token. The password form needs it\nto create the first admin; it stops working once an admin exists.'
75
+ },
70
76
  config: {
71
77
  usage: 'selva config <show [--all] | set KEY=VALUE | unset KEY>',
72
78
  blurb:
@@ -155,6 +161,7 @@ function printHelp() {
155
161
  ' update npm update @selvajs/cli + @selvajs/selva, then restart',
156
162
  ' migrate Bring package.json onto the current layout',
157
163
  ' setup-proxy Install + configure Caddy with TLS for this deployment',
164
+ ' setup-url Print the link that creates the first admin',
158
165
  ' config show|set|unset What .env sets; change one key safely',
159
166
  ' keys rotate <hmac|at-rest> Rotate a secret; the old one keeps working',
160
167
  '',
@@ -166,7 +173,8 @@ function printHelp() {
166
173
  '',
167
174
  pc.dim('`npx selva` works in a deployment directory because @selvajs/cli is one of'),
168
175
  pc.dim('its dependencies. The package.json scripts (`npm run doctor`, `npm start`)'),
169
- pc.dim('are thin aliases for the same commands.')
176
+ pc.dim('are thin aliases for the same commands. Outside a deployment directory, npx'),
177
+ pc.dim('resolves `selva` to an unrelated npm package: do not let it install one.')
170
178
  ].join('\n')
171
179
  );
172
180
  }
@@ -9,6 +9,7 @@ import pc from 'picocolors';
9
9
  import { collectConfig, collectConfigFromEnv } from '../prompts.js';
10
10
  import { generateKey } from '../secrets.js';
11
11
  import { writeEnvFile } from '../env.js';
12
+ import { setupUrlFor } from './setup-url.js';
12
13
  import { isEmptyOrMissing, requiredNodeRange, runtimeTemplatePath } from '../paths.js';
13
14
  import {
14
15
  buildDeploymentPackageJson,
@@ -151,15 +152,31 @@ function nextSteps({ rawDir, values, supabase }) {
151
152
 
152
153
  lines.push(
153
154
  '',
154
- values.ORIGIN
155
- ? `Then visit ${pc.cyan(values.ORIGIN)} and open ${pc.cyan('/setup')} to claim admin.`
156
- : `Then visit ${pc.cyan('http://localhost:3000')} and open ${pc.cyan('/setup')} to claim admin.`,
155
+ ...claimAdminLines(values),
157
156
  '',
158
157
  pc.dim('`npm run doctor --fix` repairs what it can, including pm2 boot persistence.')
159
158
  );
160
159
  return lines;
161
160
  }
162
161
 
162
+ /**
163
+ * A password form cannot prove the email typed into it, so it takes the token
164
+ * in this link. Proxy sign-in proves the email, and has no form.
165
+ */
166
+ function claimAdminLines(values) {
167
+ if (values.SELVA_AUTH_PROVIDER === 'header') {
168
+ return [
169
+ `Then sign in through your proxy as ${pc.cyan(values.BOOTSTRAP_INSTANCE_ADMIN_EMAIL ?? 'the admin')} to claim admin.`
170
+ ];
171
+ }
172
+ const { url } = setupUrlFor(values);
173
+ return [
174
+ 'Then open the setup link to claim admin:',
175
+ ` ${pc.cyan(url)}`,
176
+ pc.dim(' `npx selva setup-url` prints it again. It stops working once an admin exists.')
177
+ ];
178
+ }
179
+
163
180
  function targetDirLabel(rawDir) {
164
181
  return resolve(rawDir);
165
182
  }
@@ -325,6 +325,9 @@ export function degradedReason(body) {
325
325
  );
326
326
  }
327
327
  if (boot?.atRestSecrets?.ok === false) {
328
+ if (boot.atRestSecrets.failures?.some((f) => f.reason === 'unknown_envelope')) {
329
+ return 'stored compute API keys were sealed by a newer Selva version than the one installed';
330
+ }
328
331
  return 'stored compute API keys cannot be decrypted under the current SELVA_AT_REST_KEY';
329
332
  }
330
333
  return 'see `pm2 logs selva-compute`';
@@ -0,0 +1,33 @@
1
+ // `selva setup-url`: the link that claims an instance with no admin yet.
2
+
3
+ import { join } from 'node:path';
4
+ import pc from 'picocolors';
5
+ import { readEnvFile } from '../env.js';
6
+ import { requireDeploymentDir, resolveDeploymentDir } from '../paths.js';
7
+ import { setupUrl } from '../generated/config.js';
8
+
9
+ /** The link for this deployment's `.env`, or an error naming what is missing. */
10
+ export function setupUrlFor(env) {
11
+ const key = env.SELVA_HMAC_KEY?.trim();
12
+ if (!key) return { error: 'SELVA_HMAC_KEY is not set in .env. Run `selva doctor`.' };
13
+ const origin = env.ORIGIN?.trim() || `http://localhost:${env.PORT?.trim() || '3000'}`;
14
+ return { url: setupUrl(origin, key) };
15
+ }
16
+
17
+ export async function runSetupUrl() {
18
+ const dir = resolveDeploymentDir();
19
+ requireDeploymentDir(dir);
20
+ const result = setupUrlFor(readEnvFile(join(dir, '.env')));
21
+ if (result.error) {
22
+ console.error(pc.red(result.error));
23
+ process.exit(1);
24
+ }
25
+ console.log(
26
+ [
27
+ result.url,
28
+ '',
29
+ pc.dim('Works only while the instance has no admin. Anyone holding it can claim'),
30
+ pc.dim('the instance until then, so do not paste it into a chat or a ticket.')
31
+ ].join('\n')
32
+ );
33
+ }
@@ -480,6 +480,19 @@ function parseDeadline(text) {
480
480
  Math.floor(Number(`0${fraction}`) * 1e3)
481
481
  );
482
482
  }
483
+ // ../platform/src/config/setup-token.ts
484
+ /**
485
+ * The secret in the first-run setup link. Derived from `SELVA_HMAC_KEY`, so
486
+ * claiming an instance that has no admin takes read access to its `.env`.
487
+ * Stateless: every instance and the CLI compute the same value.
488
+ */
489
+ function setupToken(hmacKey) {
490
+ return createHmac('sha256', hmacKey.trim()).update('selva:setup:v1').digest('base64url');
491
+ }
492
+ /** Where the operator claims the instance. `origin` has no trailing slash. */
493
+ function setupUrl(origin, hmacKey) {
494
+ return `${origin.replace(/\/+$/, '')}/setup?token=${setupToken(hmacKey)}`;
495
+ }
483
496
  // ../server/src/config/settings.ts
484
497
  var flag = (key, label, description, impact) =>
485
498
  defineSetting({
@@ -1092,6 +1105,15 @@ var APP_SETTINGS = composeManifest([
1092
1105
  description:
1093
1106
  'Deployment directory the admin updater runs in. Defaults to the working directory.'
1094
1107
  }),
1108
+ defineSetting({
1109
+ key: 'NPM_CONFIG_REGISTRY',
1110
+ label: 'npm registry',
1111
+ type: 'url',
1112
+ onInvalid: 'fail',
1113
+ class: 'bootstrap',
1114
+ description:
1115
+ 'Registry updates are checked against and installed from. Defaults to registry.npmjs.org.'
1116
+ }),
1095
1117
  defineSetting({
1096
1118
  key: 'SELVA_EVENT_CALLBACK_ORIGIN',
1097
1119
  label: 'Solve event callback address',
@@ -1260,5 +1282,6 @@ export {
1260
1282
  keyRotationStatus,
1261
1283
  legacyValue,
1262
1284
  resolveConfig,
1285
+ setupUrl,
1263
1286
  takeSnapshot
1264
1287
  };