File size: 3,735 Bytes
b855ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cce9364
b855ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cce9364
b855ced
 
 
 
 
 
cce9364
 
 
b855ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
// Operator command line for a running Taper server. It talks to the server's
// local admin socket (TAPER_ADMIN_SOCKET), which only the operator's account
// can open. Operators can't read messages: they can list, suspend and delete
// accounts, sign devices out, and change limits.
//
//   node admin.js users
//   node admin.js suspend alice        (signs them out; they can't log in)
//   node admin.js unsuspend alice
//   node admin.js revoke alice         (signs every device out)
//   node admin.js unlock alice         (clears the security keys of someone who lost them)
//   node admin.js delete alice         (removes the account for good)
//   node admin.js limits               (show)  |  limits registration_open false
//   node admin.js limits register_pow_bits 24  |  limits invite_only true
//   node admin.js invite [TTL_HOURS]   (create a single-use registration invite)
//   node admin.js rotate-signing-key   (rotate the server's Ed25519 signing key)
//   node admin.js repin-peer SERVER    (clear a pinned peer signing key after out-of-band verification)
//   node admin.js stats
//
// Dead man's switch (docs/dead-mans-switch.md):
//   node admin.js dms                  (status)
//   node admin.js checkin              (reset the dead hand's clock)
//   node admin.js order TOKEN          (a signed order from dms-order.js)
//   node admin.js drill                (list what a wipe would destroy; destroys nothing)
//   node admin.js decommission NAME    (wipe now; NAME is the server name, as confirmation)
//
// The socket: --socket PATH, or TAPER_ADMIN_SOCKET.

import net from 'node:net';

const argv = process.argv.slice(2);
let socketPath = process.env.TAPER_ADMIN_SOCKET;
const si = argv.indexOf('--socket');
if (si >= 0) { socketPath = argv[si + 1]; argv.splice(si, 2); }
const [cmd, a, b] = argv;

const USAGE = 'usage: admin.js [--socket PATH] users | stats | suspend USER | unsuspend USER | revoke USER | unlock USER | delete USER | invite [TTL_HOURS] | rotate-signing-key | repin-peer SERVER | limits [NAME VALUE] | dms | checkin | order TOKEN | drill | decommission SERVER_NAME';
if (!cmd || !socketPath) { console.error(socketPath ? USAGE : `Set TAPER_ADMIN_SOCKET or pass --socket.\n${USAGE}`); process.exit(2); }

let args = {};
if (['suspend', 'unsuspend', 'revoke', 'unlock', 'delete'].includes(cmd)) {
  if (!a) { console.error(USAGE); process.exit(2); }
  args = { user: a };
} else if (cmd === 'repin-peer') {
  if (!a) { console.error(USAGE); process.exit(2); }
  args = { server: a };
} else if (cmd === 'invite') {
  if (a !== undefined) args = { ttl_hours: Number(a) };
} else if (cmd === 'order' || cmd === 'decommission') {
  if (!a) { console.error(USAGE); process.exit(2); }
  args = cmd === 'order' ? { token: a } : { confirm: a };
} else if (cmd === 'limits' && a) {
  let value = b;
  try { value = JSON.parse(b); } catch { /* left as text: the server will refuse it */ }
  args = { name: a, value };
}

const sock = net.createConnection(socketPath);
let out = '';
sock.on('error', (e) => { console.error(`Can't reach the server's admin socket (${e.code ?? e.message}). Is it running, and are you the operator account?`); process.exit(1); });
sock.on('data', (d) => { out += d; });
sock.on('end', () => {
  const res = JSON.parse(out);
  if (!res.ok) { console.error(res.error); process.exit(1); }
  if (cmd === 'users') {
    for (const u of res.result) console.log(`${u.user_id}\tcreated ${new Date(u.created_ts).toISOString().slice(0, 10)}${u.suspended ? '\tSUSPENDED' : ''}${u.security_keys ? `\t${u.security_keys} key(s)` : ''}`);
  } else {
    console.log(JSON.stringify(res.result, null, 2));
  }
});
sock.write(`${JSON.stringify({ cmd, args })}\n`);