@dotrino/remote-agent 0.29.0 → 0.30.0

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.
Files changed (3) hide show
  1. package/README.md +8 -0
  2. package/package.json +5 -5
  3. package/src/link.js +44 -5
package/README.md CHANGED
@@ -107,6 +107,14 @@ parámetros (`{ ns, proxyUrl: link.proxy, masterPubkey: link.iss, device, cert,
107
107
  Un agente que corre UNA vez (un bot por cron) no tiene el tic de renovación de
108
108
  `startRemoteAgent`: que llame a `renewLink(link)` en cada corrida, o su cert muere a los 30 días.
109
109
 
110
+ ### El enlace va cifrado en reposo
111
+
112
+ `saveLink` escribe `link.json` cifrado con `@dotrino/vault/atrest` (≥ 0.115.0), el mismo
113
+ cifrado de la bóveda: una clave sacada de esta máquina, así que el archivo copiado a otro
114
+ equipo no sirve. `loadLink` reescribe cifrado un enlace en claro de una versión anterior.
115
+ Si el enlace existe y no se puede abrir, **lanza** `link-unreadable`: `null` es solo «no
116
+ hay enlace». No protege contra quien ya entra a esta máquina como tu usuario.
117
+
110
118
  ### Sin bóveda: identidad propia (`selfLink`)
111
119
 
112
120
  Un agente no tiene por qué exigir una bóveda. `selfLink({ label, dir })` crea las llaves
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/remote-agent",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "Middleware compartido del ecosistema Dotrino para apps de agente remoto cifrado punto a punto (handshake E2E, emparejamiento SAS con vault, revocación, descubrimiento). Lo consumen dotrino-terminal (PTY) y dotrino-ia (chat con IA).",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -50,11 +50,11 @@
50
50
  "peerDependencies": {
51
51
  "@dotrino/identity": ">=0.121.7",
52
52
  "@dotrino/proxy-client": ">=0.36.1",
53
- "@dotrino/vault": ">=0.90.0"
53
+ "@dotrino/vault": ">=0.115.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@dotrino/identity": "0.121.7",
57
- "@dotrino/proxy-client": "0.36.2",
58
- "@dotrino/vault": "0.90.2"
56
+ "@dotrino/identity": "0.135.0",
57
+ "@dotrino/proxy-client": "0.45.3",
58
+ "@dotrino/vault": "0.115.0"
59
59
  }
60
60
  }
package/src/link.js CHANGED
@@ -22,6 +22,9 @@ import { installNodeGlobals } from '../node-globals.js'
22
22
  import { adoptRecord } from './record.js'
23
23
  import { verifySealerChain, genesisActa, sealActa } from '@dotrino/identity/acta'
24
24
  import { DEFAULT_PROXY, RENEW_BEFORE_MS } from '../protocol.js'
25
+ // Estático y no perezoso: `loadLink`/`saveLink` son síncronos y así siguen. Necesita
26
+ // @dotrino/vault >= 0.115.0 (la primera que exporta `atrest`).
27
+ import { atRestFor, isEncrypted } from '@dotrino/vault/atrest'
25
28
 
26
29
  export function dataDir (name = 'dotrino-remote-agent') {
27
30
  if (process.env.DOTRINO_REMOTE_AGENT_DIR) return process.env.DOTRINO_REMOTE_AGENT_DIR
@@ -31,15 +34,51 @@ export function dataDir (name = 'dotrino-remote-agent') {
31
34
 
32
35
  const linkPath = (dir) => path.join(dir, 'link.json')
33
36
 
34
- /** Lee el enlace persistido (o null si no está enrolado). */
37
+ /*
38
+ * EL ENLACE VA CIFRADO EN REPOSO (0.30.0). Lleva la llave privada del aparato, y hasta aquí
39
+ * estaba en claro (0600): copiar el archivo bastaba para hacerse pasar por el aparato. Se cifra
40
+ * con `@dotrino/vault/atrest`, el mismo cifrado de la bóveda y de la identidad de un servicio:
41
+ * una clave sacada de esta máquina (`/etc/machine-id` + un salt de la carpeta), así que el
42
+ * archivo copiado a otro equipo no sirve. No protege contra quien ya entra a esta máquina
43
+ * como tu usuario; eso pediría un TPM o el llavero del sistema.
44
+ */
45
+
46
+ /**
47
+ * Lee el enlace persistido, o `null` si NO EXISTE. Un enlace en claro (anterior a 0.30.0) se
48
+ * lee y se reescribe cifrado en el acto, comprobando antes que se vuelve a leer igual.
49
+ *
50
+ * Si existe y no se puede leer —otro equipo, la máquina cambió, el archivo está roto— LANZA
51
+ * (`link-unreadable`). Devolver `null` diría «no hay enlace», y quien lo llama crearía otro
52
+ * encima del que ya era el aparato: la identidad se perdería en silencio.
53
+ */
35
54
  export function loadLink (dir = dataDir()) {
36
- try { return JSON.parse(fs.readFileSync(linkPath(dir), 'utf8')) } catch { return null }
55
+ let text
56
+ try { text = fs.readFileSync(linkPath(dir), 'utf8') } catch (e) {
57
+ if (e?.code === 'ENOENT') return null
58
+ throw Object.assign(new Error(`cannot read the link (${linkPath(dir)}): ${e.message}`), { code: 'link-unreadable' })
59
+ }
60
+ const box = atRestFor(dir)
61
+ let link
62
+ try { link = JSON.parse(box.decrypt(text)) } catch (e) {
63
+ throw Object.assign(new Error(`cannot open the link (${linkPath(dir)}): it was encrypted on another machine, or the file is damaged. Enroll this device again.`), { code: 'link-unreadable', cause: e })
64
+ }
65
+ if (!isEncrypted(text)) writeEncrypted(dir, box, link)
66
+ return link
67
+ }
68
+
69
+ function writeEncrypted (dir, box, link) {
70
+ const json = JSON.stringify(link, null, 2)
71
+ const blob = box.encrypt(json)
72
+ if (box.decrypt(blob) !== json) throw Object.assign(new Error('link: at-rest verification failed, nothing was written'), { code: 'link-unwritable' })
73
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 })
74
+ const tmp = linkPath(dir) + '.tmp'
75
+ fs.writeFileSync(tmp, blob, { mode: 0o600 })
76
+ fs.renameSync(tmp, linkPath(dir))
37
77
  }
38
78
 
39
- /** Persiste el enlace (0600). Exportada porque la RENOVACIÓN del cert lo reescribe. */
79
+ /** Persiste el enlace, cifrado (0600). Exportada porque la RENOVACIÓN del cert lo reescribe. */
40
80
  export function saveLink (dir, link) {
41
- fs.mkdirSync(dir, { recursive: true })
42
- fs.writeFileSync(linkPath(dir), JSON.stringify(link, null, 2), { mode: 0o600 })
81
+ writeEncrypted(dir, atRestFor(dir), link)
43
82
  }
44
83
 
45
84
  /**