clavarium 0.2.36-beta.1 → 0.2.36-next.1

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.
@@ -14,6 +14,7 @@
14
14
  * sudo clavarium relay status | repair | update-os | update-relay | forget-setup-key
15
15
  * sudo clavarium doctor [--fix] what works, what does not, what to do
16
16
  * sudo clavarium password EMAIL a new password for an administrator
17
+ * sudo clavarium client-id [ID] the sign-in client id (no ID: a random one)
17
18
  *
18
19
  * `install` runs deploy/install.sh of this package, which installs THIS
19
20
  * version from npm (checked) into /opt/clavarium/releases/X.Y.Z; every other
@@ -27,11 +28,18 @@ import { fileURLToPath } from 'node:url';
27
28
  const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
28
29
  const [verb, ...rest] = process.argv.slice(2);
29
30
  if (verb === '--version' || verb === '-v') { console.log(version); process.exit(0); }
30
- if (['doctor', 'password'].includes(verb)) {
31
+ if (['doctor', 'password', 'client-id'].includes(verb)) {
31
32
  const { doctor, newPassword, certificateWatch, installWatch } = await import('./doctor.mjs');
32
33
  if (process.getuid?.() !== 0) { console.error(`clavarium ${verb}: run it as root: sudo clavarium ${verb} ...`); process.exit(78); }
33
34
  const sys = (await import('./online.mjs')).realSys();
34
35
  try {
36
+ if (verb === 'client-id') {
37
+ if (rest.length > 1) throw new Error('usage: sudo clavarium client-id [ID] (no ID: a random one)');
38
+ const { setClientId } = await import('./oidc.mjs');
39
+ const r = await setClientId({ value: rest[0], run: sys.run, isRoot: true });
40
+ sys.print(r.changed.length ? `The sign-in client id is now "${r.id}" (dex and the proxy restarted).` : `The sign-in client id is already "${r.id}".`);
41
+ process.exit(0);
42
+ }
35
43
  if (verb === 'password') { await newPassword(sys, rest[0]); process.exit(0); }
36
44
  if (rest.length === 1 && rest[0] === '--certificate') process.exit(await certificateWatch(sys));
37
45
  if (rest.length === 1 && rest[0] === '--install-watch') { await installWatch(sys); process.exit(0); }
@@ -64,7 +72,8 @@ if (verb !== 'install') {
64
72
  + ' sudo clavarium key the public keys again (text and QR)\n'
65
73
  + ' sudo clavarium relay status|repair|update-os|update-relay|forget-setup-key\n'
66
74
  + ' sudo clavarium doctor [--fix] what works, what does not, what to do\n'
67
- + ' sudo clavarium password EMAIL a new password for an administrator\n\n'
75
+ + ' sudo clavarium password EMAIL a new password for an administrator\n'
76
+ + ' sudo clavarium client-id [ID] the sign-in client id (no ID: a random one)\n\n'
68
77
  + 'https://clavarium.org');
69
78
  process.exit(verb ? 64 : 0);
70
79
  }
package/deploy/install.sh CHANGED
@@ -41,6 +41,10 @@
41
41
  # --name NAME the name of this Clavarium, under its icon and in its
42
42
  # tab (default: drawn from --domain); changed in Settings.
43
43
  # Letters, digits, spaces, dots, hyphens: 30 at most
44
+ # --client-id ID the OAuth client id, seen in the open when a browser is
45
+ # sent to sign in. Default: a random string that tells
46
+ # nothing. Give one for a third party that connects here.
47
+ # Changed later: sudo clavarium client-id [ID]
44
48
  # --dry-run check the parameters, say what would be done, do nothing
45
49
  #
46
50
  # AFTER --local, going online is one command: `sudo clavarium online --relay
@@ -110,12 +114,12 @@ DEX_LAYER="680f1119fdbcdd7f0ad8985b2534ded871b08b5b77c40f337c38aebd062b59c2"
110
114
  DEX_SHA256="217aa675738d1cbaf25fe8d7bc04cb00dbc60f91d8465b76b8d8af963d571337"
111
115
  HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
112
116
 
113
- bad() { echo "install: $*" >&2; echo "usage: install.sh --domain NAME --operator EMAIL [--operator EMAIL...] [--port N] [--relay HOST [--relay-port N]] [--core-only] [--version X.Y.Z | --from-dir DIR] [--updates auto|asap|ask|off] [--update-window HH:MM-HH:MM] [--channel latest|next] [--name NAME] [--dry-run]" >&2; echo " or: install.sh --local [--port N] [same options] [--dry-run] to try it on this machine, with no domain" >&2; exit 64; }
117
+ bad() { echo "install: $*" >&2; echo "usage: install.sh --domain NAME --operator EMAIL [--operator EMAIL...] [--port N] [--relay HOST [--relay-port N]] [--core-only] [--version X.Y.Z | --from-dir DIR] [--updates auto|asap|ask|off] [--update-window HH:MM-HH:MM] [--channel latest|next] [--name NAME] [--client-id ID] [--dry-run]" >&2; echo " or: install.sh --local [--port N] [same options] [--dry-run] to try it on this machine, with no domain" >&2; exit 64; }
114
118
  refuse() { echo "install: REFUSED: $*" >&2; exit 78; }
115
119
  step() { echo "== $*"; }
116
120
 
117
121
  DOMAIN=""; OPERATORS=""; PORT="7717"; CORE_ONLY="no"; DRY="no"; RELAY=""; RELAY_PORT="22"; LOCAL="no"; KEEP_RELEASE="no"; RELAY_INSTALLED="no"
118
- VERSION=""; FROM_DIR=""; UPDATES=""; WINDOW=""; CHANNEL=""; APP_NAME=""
122
+ VERSION=""; FROM_DIR=""; UPDATES=""; WINDOW=""; CHANNEL=""; APP_NAME=""; CLIENT_ID=""
119
123
  while [ $# -gt 0 ]; do
120
124
  case "$1" in
121
125
  --domain) DOMAIN="${2:-}"; shift 2 || bad "--domain needs a value" ;;
@@ -133,6 +137,7 @@ while [ $# -gt 0 ]; do
133
137
  --keep-release) KEEP_RELEASE="yes"; shift ;;
134
138
  --relay-installed) RELAY_INSTALLED="yes"; shift ;;
135
139
  --name) APP_NAME="${2:-}"; [ -n "$APP_NAME" ] || bad "--name needs a value"; shift 2 ;;
140
+ --client-id) CLIENT_ID="${2:-}"; [ -n "$CLIENT_ID" ] || bad "--client-id needs a value"; shift 2 ;;
136
141
  --dry-run) DRY="yes"; shift ;;
137
142
  *) bad "unknown parameter: $1" ;;
138
143
  esac
@@ -176,6 +181,10 @@ fi
176
181
  [ -z "$CHANNEL" ] || [[ "$CHANNEL" =~ ^(latest|next)$ ]] || bad "--channel is latest or next"
177
182
  # It goes in the unit file, between quotes: plain characters only (any other is chosen in Settings).
178
183
  [ -z "$APP_NAME" ] || [[ "$APP_NAME" =~ ^[A-Za-z0-9]([A-Za-z0-9\ ._-]{0,28}[A-Za-z0-9.])?$ ]] || bad "--name is letters, digits, spaces, dots and hyphens, 30 at most, like \"Home lab\""
184
+ # The OAuth client id, seen in the open when a browser is sent to sign in. Default:
185
+ # a random string, made below, that tells nothing. A value given here is for an
186
+ # installation that a particular third party connects to (deploy/oidc.mjs).
187
+ [ -z "$CLIENT_ID" ] || [[ "$CLIENT_ID" =~ ^[A-Za-z0-9]([A-Za-z0-9._-]{0,62}[A-Za-z0-9])?$ ]] || bad "--client-id is letters, digits, dots, underscores and hyphens (no spaces), 64 at most"
179
188
  # What the updates will be, as the settings file has them or as asked now.
180
189
  had() { { sed -n "s/^$1=//p" "$UPDATE_CONF" 2>/dev/null || true; } | head -1; }
181
190
  SHOW_MODE="${UPDATES:-$(had MODE)}"; SHOW_MODE="${SHOW_MODE:-auto}"
@@ -195,6 +204,7 @@ would install Clavarium:
195
204
  address $([ "$LOCAL" = "yes" ] && echo "http://127.0.0.1:$PORT, from this machine only (--local)" || echo "https://$DOMAIN")
196
205
  operators $([ "$LOCAL" = "yes" ] && echo "whoever can open a page on this machine: no sign-in" || echo "$OPERATORS")
197
206
  name $([ -n "$APP_NAME" ] && echo "$APP_NAME" || echo "drawn from the address; changed in Settings")
207
+ sign-in id $([ "$LOCAL" = "yes" ] && echo "none (--local): no sign-in" || [ -n "$CLIENT_ID" ] && echo "$CLIENT_ID (given)" || echo "a random string, made at install: tells nothing (sudo clavarium client-id to change)")
198
208
  server 127.0.0.1:$PORT, as the account "$ACCOUNT", Node.js $NODE_VERSION
199
209
  release $([ "$KEEP_RELEASE" = "yes" ] && echo "the one that runs, kept (--keep-release)" || [ -n "$VERSION" ] && echo "clavarium@$VERSION from npm, checked (integrity, provenance from github.com/clavarium/clavarium, npm audit signatures)" || echo "the working tree $FROM_DIR, as it is (development: not checked)")
200
210
  updates $(case "$SHOW_MODE" in auto) echo "automatic, between ${SHOW_WINDOW%-*} and ${SHOW_WINDOW#*-}" ;; asap) echo "as soon as a version is out" ;; ask) echo "said in Settings, done when you say" ;; off) echo "off" ;; esac), channel $SHOW_CHANNEL
@@ -473,6 +483,11 @@ PASSWORDS=""
473
483
  if [ ! -e /etc/dex/config.yaml ]; then
474
484
  client_secret="$(openssl rand -hex 32)"
475
485
  cookie_secret="$(openssl rand -base64 32 | tr '+/' '-_')"
486
+ # The OAuth client id is seen in the open (the sign-in redirect). By default
487
+ # it is random, so it tells nothing about what this is; --client-id sets a
488
+ # chosen one. dex and the proxy below MUST carry the same value. The display
489
+ # name is the id too: nothing here names the product (deploy/oidc.mjs).
490
+ client_id="${CLIENT_ID:-$(openssl rand -hex 16)}"
476
491
  identity="$(mktemp)"
477
492
  cat > "$identity" <<DEX
478
493
  # Who may sign in to Clavarium on THIS machine. Written by install.sh.
@@ -487,8 +502,8 @@ web:
487
502
  oauth2:
488
503
  skipApprovalScreen: true
489
504
  staticClients:
490
- - id: clavarium
491
- name: Clavarium
505
+ - id: $client_id
506
+ name: $client_id
492
507
  secret: $client_secret
493
508
  redirectURIs:
494
509
  - https://$DOMAIN/oauth2/callback
@@ -512,7 +527,7 @@ DEX
512
527
  proxy="$(mktemp)"
513
528
  cat > "$proxy" <<PROXY
514
529
  provider = "oidc"
515
- client_id = "clavarium"
530
+ client_id = "$client_id"
516
531
  client_secret = "$client_secret"
517
532
  cookie_secret = "$cookie_secret"
518
533
  oidc_issuer_url = "https://$DOMAIN/dex"
@@ -0,0 +1,102 @@
1
+ /* ---------------------------------------------------------------------------
2
+ * THE SIGN-IN CLIENT ID. Between the sign-in proxy (oauth2-proxy) and the
3
+ * small identity provider (dex), one OAuth client ties them together: dex
4
+ * knows it under `staticClients`, the proxy names it as `client_id`. The two
5
+ * MUST carry the same id, or no one can sign in.
6
+ *
7
+ * That id travels in the open: an unconnected browser sent to sign in gets a
8
+ * redirect to .../auth?client_id=<id>... So the id tells nothing by default --
9
+ * it is a random string (randomClientId), made at install. An operator who
10
+ * needs a particular name (a third party that connects to this installation
11
+ * one day) sets it: `sudo clavarium client-id <name>`.
12
+ *
13
+ * Installations made before this (26-10) carried the literal "clavarium",
14
+ * which both named the product and gave it away. ensureGenericClientId, run
15
+ * by the updater each turn, migrates such an installation ONCE to a random
16
+ * id, writing dex and the proxy together so they never disagree -- and, should
17
+ * a migration be cut off half-way, converges them on a later turn.
18
+ *
19
+ * A machine with no sign-in (local mode, --core-only) has neither file: then
20
+ * every function here does nothing.
21
+ * ------------------------------------------------------------------------- */
22
+ import { readFileSync, writeFileSync, renameSync } from 'node:fs';
23
+ import { randomBytes } from 'node:crypto';
24
+
25
+ export const DEX_CONF = '/etc/dex/config.yaml';
26
+ export const PROXY_CFG = '/etc/oauth2-proxy/oauth2-proxy.cfg';
27
+
28
+ /* What the installer wrote before the id was made generic: an installation
29
+ still carrying either is migrated to a random id. */
30
+ export const LEGACY_ID = 'clavarium';
31
+ export const LEGACY_NAME = 'Clavarium';
32
+
33
+ /* A chosen id: letters and digits, with . _ - between them; no space, no colon,
34
+ nothing that could break the YAML line or the proxy line it sits on. 64 max. */
35
+ export const VALID_ID = /^[A-Za-z0-9](?:[A-Za-z0-9._-]{0,62}[A-Za-z0-9])?$/;
36
+
37
+ /* 32 hex characters: random, and telling nothing about what it is. */
38
+ export const randomClientId = () => randomBytes(16).toString('hex');
39
+
40
+ /* dex writes the client as " - id: X" then, on the next line, " name: Y".
41
+ The proxy writes client_id = "X". These read and rewrite exactly those. */
42
+ const DEX_ID = /^( - id: )(.*)$/m;
43
+ const DEX_NAME = /^( name: )(.*)$/m;
44
+ const PROXY_ID = /^(client_id = ")([^"\n]*)(")/m;
45
+
46
+ export const dexClientId = (yaml) => DEX_ID.exec(yaml ?? '')?.[2]?.trim() ?? null;
47
+ export const dexClientName = (yaml) => DEX_NAME.exec(yaml ?? '')?.[2]?.trim() ?? null;
48
+ export const proxyClientId = (cfg) => PROXY_ID.exec(cfg ?? '')?.[2] ?? null;
49
+
50
+ export const withDexClient = (yaml, id, name) => yaml.replace(DEX_ID, `$1${id}`).replace(DEX_NAME, `$1${name}`);
51
+ export const withProxyClient = (cfg, id) => cfg.replace(PROXY_ID, `$1${id}$3`);
52
+
53
+ /* Writes BOTH files to the one id, so dex and the proxy never disagree. Each
54
+ file is replaced atomically (.new then rename); as root the dex file is
55
+ chowned back to root:dex and both services are restarted (dex first: the
56
+ proxy talks to it). Returns the services whose file changed. Only the files
57
+ that differ are touched, so this is safe to call again. */
58
+ export async function applyClientId({ dexConf = DEX_CONF, proxyCfg = PROXY_CFG, id, name = id, run, isRoot = process.getuid?.() === 0 } = {}) {
59
+ let dex; let proxy;
60
+ try { dex = readFileSync(dexConf, 'utf8'); proxy = readFileSync(proxyCfg, 'utf8'); } catch { return { changed: [], id: null }; }
61
+ const changed = [];
62
+ if (dexClientId(dex) !== id || dexClientName(dex) !== name) {
63
+ writeFileSync(`${dexConf}.new`, withDexClient(dex, id, name), { mode: 0o640 });
64
+ if (isRoot && run) await run(['chown', 'root:dex', `${dexConf}.new`]);
65
+ renameSync(`${dexConf}.new`, dexConf);
66
+ changed.push('dex');
67
+ }
68
+ if (proxyClientId(proxy) !== id) {
69
+ writeFileSync(`${proxyCfg}.new`, withProxyClient(proxy, id), { mode: 0o600 });
70
+ renameSync(`${proxyCfg}.new`, proxyCfg);
71
+ changed.push('oauth2-proxy');
72
+ }
73
+ if (changed.length && isRoot && run) {
74
+ await run(['systemctl', 'restart', 'dex.service'], { timeoutMs: 60_000 });
75
+ await run(['systemctl', 'restart', 'oauth2-proxy.service'], { timeoutMs: 60_000 });
76
+ }
77
+ return { changed, id };
78
+ }
79
+
80
+ /* Idempotent, each updater turn. An installation still on the legacy id (or
81
+ name), or one left disagreeing by a cut-off migration, is converged onto a
82
+ single non-revealing id -- a fresh random one if it was still the legacy id,
83
+ otherwise the id dex already carries, with the proxy brought into line. */
84
+ export async function ensureGenericClientId({ dexConf = DEX_CONF, proxyCfg = PROXY_CFG, run, isRoot } = {}) {
85
+ let dex; let proxy;
86
+ try { dex = readFileSync(dexConf, 'utf8'); proxy = readFileSync(proxyCfg, 'utf8'); } catch { return { changed: [], id: null }; }
87
+ const dexId = dexClientId(dex); const dexName = dexClientName(dex); const proxyId = proxyClientId(proxy);
88
+ const legacy = dexId === null || dexId === LEGACY_ID || dexName === LEGACY_NAME;
89
+ const id = legacy ? randomClientId() : dexId;
90
+ const name = legacy ? id : (dexName ?? id);
91
+ if (dexId === id && dexName === name && proxyId === id) return { changed: [], id };
92
+ return applyClientId({ dexConf, proxyCfg, id, name, run, isRoot });
93
+ }
94
+
95
+ /* `sudo clavarium client-id [VALUE]`: set a particular id (a third party that
96
+ connects here), or, with no value or --random, a fresh random one. */
97
+ export async function setClientId({ value, dexConf = DEX_CONF, proxyCfg = PROXY_CFG, run, isRoot } = {}) {
98
+ const id = value && value !== '--random' ? value : randomClientId();
99
+ if (!VALID_ID.test(id)) throw new Error('a client id is letters, digits and . _ - (no spaces), 64 at most');
100
+ try { readFileSync(dexConf, 'utf8'); } catch { throw new Error('no sign-in on this machine (local mode): nothing to set'); }
101
+ return applyClientId({ dexConf, proxyCfg, id, name: id, run, isRoot });
102
+ }
@@ -55,6 +55,7 @@ import { tmpdir } from 'node:os';
55
55
  import { fileURLToPath } from 'node:url';
56
56
  import { fetchToolArchives, REGISTRY } from './parley-archive.mjs';
57
57
  import { ensureTunnel, tunnelUnit, relayOfUnit, TUNNEL_UNIT, TUNNEL_PROGRAM } from './tunnel.mjs';
58
+ import { ensureGenericClientId } from './oidc.mjs';
58
59
  /* The updater's run keeps the tunnel up to date: the same functions, from here too. */
59
60
  export { ensureTunnel, tunnelUnit, relayOfUnit, TUNNEL_UNIT, TUNNEL_PROGRAM };
60
61
 
@@ -909,6 +910,7 @@ async function main(argv) {
909
910
  try { const [{ installWatch }, { realSys }] = await Promise.all([import('./doctor.mjs'), import('./online.mjs')]); if (await installWatch(realSys())) console.error('updater: the certificate timer set up'); } catch (error) { console.error(`updater: the certificate timer could not be set up: ${error.message}`); }
910
911
  try { if (await u.ensureDropIn()) console.error('updater: the drop-in of the server\'s unit written'); } catch (error) { console.error(`updater: the drop-in could not be written: ${error.message}`); }
911
912
  try { if (await ensureProxySettings()) console.error('updater: the sign-in proxy takes one CSRF cookie per sign-in'); } catch (error) { console.error(`updater: the sign-in proxy could not be set: ${error.message}`); }
913
+ try { const c = await ensureGenericClientId({ run: runCommand }); if (c.changed.length) console.error('updater: the sign-in client id was made generic (nothing names the product in the open)'); } catch (error) { console.error(`updater: the sign-in client id could not be made generic: ${error.message}`); }
912
914
  try { if (await ensureTunnel({ run: runCommand })) console.error('updater: the tunnel to the relay notices a cut sooner'); } catch (error) { console.error(`updater: the tunnel could not be set: ${error.message}`); }
913
915
  /* What became of the relay is kept beside the state, for Settings (server/updates.mjs: relay). */
914
916
  try {
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.36-beta.1",
3
+ "version": "0.2.36-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.36-beta.1",
9
+ "version": "0.2.36-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.36-beta.1",
3
+ "version": "0.2.36-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",