@dotrino/remote-agent 0.5.0 → 0.5.2

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 +4 -1
  2. package/package.json +12 -5
  3. package/src/link.js +41 -3
package/README.md CHANGED
@@ -33,7 +33,7 @@ Provee lo común — y **solo** eso:
33
33
  | `@dotrino/remote-agent` | isomórfico | `e2e` (`makeEphemeral`/`deriveKey`/`seal`/`open`) + constantes de protocolo |
34
34
  | `@dotrino/remote-agent/agent` | Node | `startRemoteAgent(opts)` |
35
35
  | `@dotrino/remote-agent/client` | navegador | `RemoteAgentClient` |
36
- | `@dotrino/remote-agent/link` | Node | `enroll`, `loadLink`, `saveLink`, `dataDir`, `parseQr`, `identityFromLink`, `clientLink` |
36
+ | `@dotrino/remote-agent/link` | Node | `enroll`, `loadLink`, `saveLink`, `dataDir`, `parseQr`, `identityFromLink`, `clientLink`, `renewLink` |
37
37
  | `@dotrino/remote-agent/discover` | navegador | `listAgentsByLabel` |
38
38
 
39
39
  ## Uso
@@ -90,6 +90,9 @@ hace que se exija ese permiso en el cert y queda en el enlace). Sus secretos los
90
90
  con `fetchSecrets`/`waitForSecrets` de `@dotrino/vault/service` pasando la identidad por
91
91
  parámetros (`{ ns, proxyUrl: link.proxy, masterPubkey: link.iss, device, cert, enc }`).
92
92
 
93
+ Un agente que corre UNA vez (un bot por cron) no tiene el tic de renovación de
94
+ `startRemoteAgent`: que llame a `renewLink(link)` en cada corrida, o su cert muere a los 30 días.
95
+
93
96
  ### Un agente como CLIENTE de otro agente (Node)
94
97
 
95
98
  Un agente Node también puede hablar con otro agente —un bot que guarda en el node de
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/remote-agent",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
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",
@@ -23,9 +23,6 @@
23
23
  "type-check": "tsc --noEmit"
24
24
  },
25
25
  "dependencies": {
26
- "@dotrino/identity": "0.56.0",
27
- "@dotrino/proxy-client": "0.10.1",
28
- "@dotrino/vault": "0.30.0",
29
26
  "ws": "8.21.0"
30
27
  },
31
28
  "engines": {
@@ -45,5 +42,15 @@
45
42
  "vault",
46
43
  "proxy",
47
44
  "p2p"
48
- ]
45
+ ],
46
+ "peerDependencies": {
47
+ "@dotrino/identity": ">=0.56.0",
48
+ "@dotrino/proxy-client": ">=0.10.1",
49
+ "@dotrino/vault": ">=0.30.0"
50
+ },
51
+ "devDependencies": {
52
+ "@dotrino/identity": "0.56.0",
53
+ "@dotrino/proxy-client": "0.10.1",
54
+ "@dotrino/vault": "0.30.0"
55
+ }
49
56
  }
package/src/link.js CHANGED
@@ -16,8 +16,8 @@
16
16
  import fs from 'node:fs'
17
17
  import path from 'node:path'
18
18
  import os from 'node:os'
19
- import { signWithDevice, pubkeyId } from '@dotrino/identity/capabilities'
20
- import { requestDevices } from '@dotrino/identity/vault/remote.js'
19
+ import { signWithDevice, pubkeyId, verifyDelegation } from '@dotrino/identity/capabilities'
20
+ import { requestDevices, requestRenew } from '@dotrino/identity/vault/remote.js'
21
21
  import { installNodeGlobals } from '../node-globals.js'
22
22
 
23
23
  export function dataDir (name = 'dotrino-remote-agent') {
@@ -127,9 +127,47 @@ export function identityFromLink (link, { dir = dataDir() } = {}) {
127
127
  }
128
128
  }
129
129
 
130
+ /** Con menos de esto de vida se pide cert nuevo (7 días; el cert dura 30). */
131
+ export const RENEW_BEFORE_MS = 7 * 24 * 60 * 60 * 1000
132
+
133
+ /**
134
+ * RENUEVA el cert del enlace si vence pronto y lo persiste. Es lo que hace el agente
135
+ * de larga duración en cada tic (`startRemoteAgent`), sacado aquí para los agentes que
136
+ * corren UNA vez (un bot por cron): sin esto su cert muere a los 30 días y hay que
137
+ * re-emparejar una máquina que nunca dejó de ser tuya.
138
+ *
139
+ * El cert nuevo se VERIFICA antes de guardarlo (misma maestra, misma llave, más vida).
140
+ * Un cert ya vencido no se renueva: ahí toca re-emparejar. No lanza: devuelve qué pasó.
141
+ *
142
+ * @param {object} link el enlace (se muta con el cert nuevo).
143
+ * @param {{ dir?: string, force?: boolean }} [opts]
144
+ * @returns {Promise<{ renewed: boolean, exp: number|null, reason?: string }>}
145
+ */
146
+ export async function renewLink (link, { dir = dataDir(), force = false } = {}) {
147
+ const exp = link?.cert?.exp
148
+ if (typeof exp !== 'number') return { renewed: false, exp: null, reason: 'no cert' }
149
+ if (exp <= Date.now()) return { renewed: false, exp, reason: 'expired: enroll again' }
150
+ if (!force && exp - Date.now() > RENEW_BEFORE_MS) return { renewed: false, exp, reason: 'not due' }
151
+ installNodeGlobals(dir)
152
+ try {
153
+ const res = await requestRenew({ master: link.iss, proxy: link.proxy || 'wss://proxy.dotrino.com', device: link.device, cert: link.cert })
154
+ const cert = res?.cert
155
+ if (!cert || cert.sub !== link.device.publickey || cert.iss !== link.iss || !(cert.exp > exp)) {
156
+ throw new Error('the vault returned a cert that is not for this machine')
157
+ }
158
+ const v = await verifyDelegation({ cert, expectedSub: link.device.publickey })
159
+ if (!v.ok) throw new Error(v.reason)
160
+ link.cert = cert
161
+ saveLink(dir, link)
162
+ return { renewed: true, exp: cert.exp }
163
+ } catch (e) {
164
+ return { renewed: false, exp, reason: e.message }
165
+ }
166
+ }
167
+
130
168
  /** El enlace como lo esperan `ContentClient.connect({ link })` y `RemoteAgentClient`. */
131
169
  export function clientLink (link, opts) {
132
170
  return { id: identityFromLink(link, opts), cert: link.cert, iss: link.iss, proxy: link.proxy || 'wss://proxy.dotrino.com' }
133
171
  }
134
172
 
135
- export default { dataDir, loadLink, saveLink, parseQr, enroll, identityFromLink, clientLink }
173
+ export default { dataDir, loadLink, saveLink, parseQr, enroll, identityFromLink, clientLink, renewLink }