@goodandready/dsh-key-rotation 0.7.39 → 0.7.40
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/README.md +5 -10
- package/README.ru.md +5 -10
- package/README.zh.md +1 -3
- package/lib/http-bridge.js +169 -0
- package/lib/index.js +40 -940
- package/lib/rotate.js +237 -0
- package/lib/routes-ops.js +587 -0
- package/package.json +3 -2
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
// lib/routes-ops.js — operational HTTP routes for dsh-key-rotation (#253).
|
|
2
|
+
// Registration is injected with the live apply() dependencies.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
json,
|
|
6
|
+
readJson,
|
|
7
|
+
descriptorOf,
|
|
8
|
+
NS,
|
|
9
|
+
} from './http-bridge.js';
|
|
10
|
+
import {
|
|
11
|
+
isTrustedBridgeRequest,
|
|
12
|
+
keyTail,
|
|
13
|
+
envValue,
|
|
14
|
+
isValidRef,
|
|
15
|
+
computeHealthScore,
|
|
16
|
+
recordFailure,
|
|
17
|
+
costForDay,
|
|
18
|
+
costForWeek,
|
|
19
|
+
} from './pool.js';
|
|
20
|
+
import { bucketInfo } from './bucket.js';
|
|
21
|
+
import { usageRows, usageCsv } from './usage-report.js';
|
|
22
|
+
import { findSecrets, looksLikeApiSecret } from './keycheck.js';
|
|
23
|
+
import { nextQuotaReset } from './quota-window.js';
|
|
24
|
+
|
|
25
|
+
const STATUS_PATH = '/dsh-key-rotation/status';
|
|
26
|
+
const SNAPSHOT_PATH = '/dsh-key-rotation/snapshot';
|
|
27
|
+
const KEY_PATH = '/dsh-key-rotation/key';
|
|
28
|
+
const RESET_PATH = '/dsh-key-rotation/reset';
|
|
29
|
+
const IMPORT_PATH = '/dsh-key-rotation/import';
|
|
30
|
+
const HEALTH_PATH = '/dsh-key-rotation/health';
|
|
31
|
+
const USAGE_PATH = '/dsh-key-rotation/usage';
|
|
32
|
+
const TEST_PATH = '/dsh-key-rotation/test';
|
|
33
|
+
const SANDBOX_CACHE_PATH = '/dsh-key-rotation/sandbox-cache';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {object} ctx cordis context
|
|
37
|
+
* @param {object} deps live dependencies from apply()
|
|
38
|
+
*/
|
|
39
|
+
export function registerOpsRoutes(ctx, deps) {
|
|
40
|
+
const {
|
|
41
|
+
buildRuntime,
|
|
42
|
+
latencyHistogram,
|
|
43
|
+
lastTestCache,
|
|
44
|
+
ensureSandboxRunner,
|
|
45
|
+
poolState,
|
|
46
|
+
getRotationDisabled,
|
|
47
|
+
setRotationDisabled,
|
|
48
|
+
} = deps;
|
|
49
|
+
|
|
50
|
+
// ── status route: what the settings card cannot know on its own ──
|
|
51
|
+
//
|
|
52
|
+
// Reports, per configured provider, which key is in use, which are cooling
|
|
53
|
+
// down and until when, whether an env name resolves to a credential at all
|
|
54
|
+
// (a typo is otherwise silent), and how often rotation has fired.
|
|
55
|
+
//
|
|
56
|
+
// Key VALUES never leave the host — only the boolean fact that one exists.
|
|
57
|
+
ctx.effect(() => ctx.webServer.register({
|
|
58
|
+
kind: 'exact',
|
|
59
|
+
path: STATUS_PATH,
|
|
60
|
+
handler: async (req, res) => {
|
|
61
|
+
if (req.method !== 'GET') {
|
|
62
|
+
json(res, 405, { error: { code: 'method', message: 'GET only' } });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!isTrustedBridgeRequest(req)) {
|
|
66
|
+
json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: status is local-only' } });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const runtime = buildRuntime();
|
|
70
|
+
const { poolByRef, providerTags, providerBudgets, latencySloMs } = runtime;
|
|
71
|
+
const base = ctx.get('credentials');
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
const providers = [];
|
|
75
|
+
for (const pool of poolByRef.values()) {
|
|
76
|
+
if (seen.has(pool.base)) continue;
|
|
77
|
+
seen.add(pool.base);
|
|
78
|
+
try {
|
|
79
|
+
const keys = [];
|
|
80
|
+
for (const ref of pool.refs) {
|
|
81
|
+
let present = false;
|
|
82
|
+
let tail = '';
|
|
83
|
+
let source = null;
|
|
84
|
+
let writable = true;
|
|
85
|
+
try {
|
|
86
|
+
// The resolve patch is installed on this same service, so ask for
|
|
87
|
+
// the exact ref: a pool ref would otherwise round-robin to another
|
|
88
|
+
// key and report a missing name as present.
|
|
89
|
+
let hit = await (base?.__dshKeyRotationOriginalResolve ?? base?.resolve)?.call(base, ref);
|
|
90
|
+
present = Boolean(hit && typeof hit.value === 'string' && hit.value.length > 0);
|
|
91
|
+
if (present) tail = keyTail(hit.value);
|
|
92
|
+
// fallback: env var bootstrapping (issue #7)
|
|
93
|
+
if (!present) {
|
|
94
|
+
const ev = envValue(ref);
|
|
95
|
+
if (ev !== undefined) { present = true; tail = keyTail(ev); source = 'env'; writable = false; }
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
present = false;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const described = await base?.describe?.(ref);
|
|
102
|
+
source = described?.source ?? null;
|
|
103
|
+
writable = described?.writable !== false;
|
|
104
|
+
} catch {
|
|
105
|
+
/* describe is optional — the card falls back to editable */
|
|
106
|
+
}
|
|
107
|
+
const until = pool.state.failedUntil.get(ref);
|
|
108
|
+
keys.push({
|
|
109
|
+
ref,
|
|
110
|
+
present,
|
|
111
|
+
tail,
|
|
112
|
+
source,
|
|
113
|
+
writable,
|
|
114
|
+
active: pool.state.lastUsed === ref,
|
|
115
|
+
cooldownMsLeft: until !== undefined && until > now ? until - now : 0,
|
|
116
|
+
// #210: RPM capacity snapshot (null when rpmLimit is off)
|
|
117
|
+
rpm: bucketInfo(pool.state.rpmWindows, ref, pool.rpmLimit, now),
|
|
118
|
+
// #215: effective round-robin weight of this key
|
|
119
|
+
weight: pool.weights?.[pool.refs.indexOf(ref)] ?? 1,
|
|
120
|
+
usage: pool.state.usageCounts?.get(ref) ?? 0,
|
|
121
|
+
byModel: pool.state.byModel?.get(ref) ? Object.fromEntries(pool.state.byModel.get(ref)) : {},
|
|
122
|
+
usageDays: pool.state.usageDays?.get(ref) ? Object.fromEntries(pool.state.usageDays.get(ref)) : {},
|
|
123
|
+
cost: pool.state.costPerKey?.get(ref) ?? 0,
|
|
124
|
+
lastUsedAt: pool.state.lastUsedAt?.get(ref) ?? null,
|
|
125
|
+
expiresAt: pool.expiresAt?.[ref] ?? null,
|
|
126
|
+
expired: pool.expiresAt?.[ref] !== undefined && now >= pool.expiresAt[ref],
|
|
127
|
+
broken: pool.state.brokenUntil?.has(ref) ?? false,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
providers.push({
|
|
131
|
+
provider: pool.base,
|
|
132
|
+
keys,
|
|
133
|
+
tags: providerTags.get(pool.base) ?? [],
|
|
134
|
+
switches: pool.state.switches ?? 0,
|
|
135
|
+
lastReason: pool.state.lastReason ?? null,
|
|
136
|
+
lastSwitchAt: pool.state.lastSwitchAt ?? null,
|
|
137
|
+
lastExhaustionAt: pool.state.lastExhaustionAt ?? null,
|
|
138
|
+
exhaustionCount: pool.state.exhaustionCount ?? 0,
|
|
139
|
+
totalUsage: (() => { let s = 0; if (pool.state.usageCounts) for (const v of pool.state.usageCounts.values()) s += v; return s; })(),
|
|
140
|
+
// #225: aggregate p95 across the pool's keys
|
|
141
|
+
p95: (() => {
|
|
142
|
+
const vals = (pool.refs ?? []).map((r) => latencyHistogram.snapshot(r)).filter((s) => s && s.p95 != null).map((s) => s.p95);
|
|
143
|
+
return vals.length ? Math.round(Math.max(...vals)) : null;
|
|
144
|
+
})(),
|
|
145
|
+
latencySloMs,
|
|
146
|
+
events: (pool.state.events ?? []).slice(-50),
|
|
147
|
+
healthScore: computeHealthScore(pool.state),
|
|
148
|
+
// #208: today/week spend + configured budget for the card
|
|
149
|
+
todayCost: costForDay(pool.state.costDays),
|
|
150
|
+
weeklyCost: costForWeek(pool.state.costDays, now),
|
|
151
|
+
budgetDaily: providerBudgets.get(pool.base)?.costBudgetDaily ?? 0,
|
|
152
|
+
budgetWeekly: providerBudgets.get(pool.base)?.costBudgetWeekly ?? 0,
|
|
153
|
+
pauseOnBudget: providerBudgets.get(pool.base)?.pauseOnBudget ?? false,
|
|
154
|
+
});
|
|
155
|
+
} catch (e) {
|
|
156
|
+
console.warn(`[dsh-key-rotation] status: pool ${pool.base} failed: ${String(e?.message ?? e)} ${e?.stack ?? ''}`);
|
|
157
|
+
providers.push({ provider: pool.base, keys: [], statusError: String(e?.message ?? e) });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
json(res, 200, { providers });
|
|
161
|
+
},
|
|
162
|
+
}), 'dsh-key-rotation: status route');
|
|
163
|
+
|
|
164
|
+
// #209: usage report - per-key requests/cost over the last N days.
|
|
165
|
+
// ?format=csv returns text/csv; ?days=N window (1..90, default 7).
|
|
166
|
+
ctx.effect(() => ctx.webServer.register({
|
|
167
|
+
kind: 'exact',
|
|
168
|
+
path: USAGE_PATH,
|
|
169
|
+
handler: (req, res) => {
|
|
170
|
+
if (req.method !== 'GET') { json(res, 405, { error: { code: 'method', message: 'GET only' } }); return; }
|
|
171
|
+
if (!isTrustedBridgeRequest(req)) { json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: usage is local-only' } }); return; }
|
|
172
|
+
const url = new URL(req.url ?? USAGE_PATH, 'http://localhost');
|
|
173
|
+
const days = Math.min(90, Math.max(1, Number(url.searchParams.get('days')) || 7));
|
|
174
|
+
const csv = url.searchParams.get('format') === 'csv';
|
|
175
|
+
const provider = url.searchParams.get('provider') ?? '';
|
|
176
|
+
const runtime = buildRuntime();
|
|
177
|
+
const now = Date.now();
|
|
178
|
+
const seen = new Set();
|
|
179
|
+
const report = [];
|
|
180
|
+
for (const pool of runtime.poolByRef.values()) {
|
|
181
|
+
if (seen.has(pool.base)) continue;
|
|
182
|
+
seen.add(pool.base);
|
|
183
|
+
if (provider && pool.base !== provider) continue;
|
|
184
|
+
report.push({ provider: pool.base, rows: usageRows(pool, days, now) });
|
|
185
|
+
}
|
|
186
|
+
if (csv) {
|
|
187
|
+
res.writeHead(200, { 'content-type': 'text/csv; charset=utf-8', 'content-disposition': 'attachment; filename="dsh-key-rotation-usage.csv"' });
|
|
188
|
+
const parts = [];
|
|
189
|
+
for (const p of report) {
|
|
190
|
+
if (parts.length > 0) parts.push('');
|
|
191
|
+
parts.push('# ' + p.provider);
|
|
192
|
+
parts.push(usageCsv(p.rows));
|
|
193
|
+
}
|
|
194
|
+
res.end(parts.join('\n') + '\n');
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
json(res, 200, { at: now, days, providers: report });
|
|
198
|
+
},
|
|
199
|
+
}), 'dsh-key-rotation: usage route');
|
|
200
|
+
|
|
201
|
+
// #218: full config snapshot - one JSON file to move between machines.
|
|
202
|
+
// Secret values never travel: only credential/env names. Token fields are
|
|
203
|
+
// exported as empty strings; on import they keep existing values when empty.
|
|
204
|
+
ctx.effect(() => ctx.webServer.register({
|
|
205
|
+
kind: 'exact',
|
|
206
|
+
path: SNAPSHOT_PATH,
|
|
207
|
+
handler: async (req, res) => {
|
|
208
|
+
if (req.method !== 'GET' && req.method !== 'POST') { json(res, 405, { error: { code: 'method', message: 'GET (export) or POST (import) only' } }); return; }
|
|
209
|
+
if (!isTrustedBridgeRequest(req)) { json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: snapshot is local-only' } }); return; }
|
|
210
|
+
if (req.method === 'GET') {
|
|
211
|
+
const descriptor = descriptorOf(ctx, NS);
|
|
212
|
+
const value = descriptor?.value ?? {};
|
|
213
|
+
const exportable = { ...value };
|
|
214
|
+
// token-shaped fields stay empty in the file; refs are names, not secrets
|
|
215
|
+
exportable.webhookActionToken = '';
|
|
216
|
+
json(res, 200, { at: Date.now(), version: 1, snapshot: exportable });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
// POST = import: { snapshot } -> merge with current section, PUT semantics
|
|
220
|
+
let body;
|
|
221
|
+
try { body = await readJson(req); } catch (e) { json(res, 400, { error: { code: 'bad-request', message: String(e?.message ?? e) } }); return; }
|
|
222
|
+
const snap = body?.snapshot;
|
|
223
|
+
if (!snap || typeof snap !== 'object' || Array.isArray(snap)) { json(res, 400, { error: { code: 'bad-format', message: 'dsh-key-rotation: POST requires {"snapshot": {...}}' } }); return; }
|
|
224
|
+
// #200 leak guard applies to imported content too
|
|
225
|
+
try {
|
|
226
|
+
const masked = structuredClone(snap);
|
|
227
|
+
if (masked.webhookActionToken) masked.webhookActionToken = '***';
|
|
228
|
+
if (masked.notifyWebhook) masked.notifyWebhook = '***';
|
|
229
|
+
const findings = findSecrets(JSON.stringify(masked));
|
|
230
|
+
if (findings.length > 0) { json(res, 400, { error: { code: 'secret-in-snapshot', message: 'dsh-key-rotation: snapshot carries a live-looking credential', findings } }); return; }
|
|
231
|
+
} catch { /* scanning must never block a valid import */ }
|
|
232
|
+
const settings = ctx.get('settings');
|
|
233
|
+
if (!settings) { json(res, 503, { error: { code: 'settings-rejected', message: 'dsh-key-rotation: no settings provider' } }); return; }
|
|
234
|
+
const desc = descriptorOf(ctx, NS);
|
|
235
|
+
if (desc === void 0) { json(res, 500, { error: { code: 'settings-rejected', message: 'dsh-key-rotation: namespace missing' } }); return; }
|
|
236
|
+
const cur = desc.value ?? {};
|
|
237
|
+
// empty token fields in the file keep the current values (never wipe a secret)
|
|
238
|
+
const merged = { ...cur, ...snap };
|
|
239
|
+
if (!snap.webhookActionToken) merged.webhookActionToken = cur.webhookActionToken ?? '';
|
|
240
|
+
try {
|
|
241
|
+
await settings.replace(NS, merged, desc.revision);
|
|
242
|
+
const after = descriptorOf(ctx, NS);
|
|
243
|
+
json(res, 200, { ok: true, revision: after?.revision });
|
|
244
|
+
} catch (e) {
|
|
245
|
+
json(res, e?.code === 'SETTINGS_CONFLICT' ? 409 : 400, { error: { code: 'settings-rejected', message: String(e?.message ?? e) } });
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
}), 'dsh-key-rotation: snapshot route');
|
|
249
|
+
|
|
250
|
+
// ── key route: store a key value without leaving the rotation card ──
|
|
251
|
+
//
|
|
252
|
+
// Adding a key used to mean two screens: create the credential elsewhere,
|
|
253
|
+
// then type its env name here. The value is write-only from the browser —
|
|
254
|
+
// it is never sent back, only its last few characters are (see the status
|
|
255
|
+
// route) — and the route is loopback- and same-origin-gated like the config
|
|
256
|
+
// bridge next to it.
|
|
257
|
+
ctx.effect(() => ctx.webServer.register({
|
|
258
|
+
kind: 'exact',
|
|
259
|
+
path: KEY_PATH,
|
|
260
|
+
handler: async (req, res) => {
|
|
261
|
+
if (req.method !== 'PUT' && req.method !== 'DELETE') {
|
|
262
|
+
json(res, 405, { error: { code: 'method', message: 'PUT or DELETE only' } });
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (!isTrustedBridgeRequest(req)) {
|
|
266
|
+
json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: keys are local-only' } });
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const credentialsService = ctx.get('credentials');
|
|
270
|
+
if (!credentialsService || typeof credentialsService.set !== 'function') {
|
|
271
|
+
json(res, 503, { error: { code: 'no-credentials', message: 'dsh-key-rotation: no credentials service is mounted' } });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
let body;
|
|
275
|
+
try {
|
|
276
|
+
body = await readJson(req);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
json(res, 400, { error: { code: 'bad-request', message: String(error?.message ?? error) } });
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const ref = typeof body?.ref === 'string' ? body.ref.trim() : '';
|
|
282
|
+
if (!isValidRef(ref)) {
|
|
283
|
+
json(res, 400, { error: { code: 'bad-ref', message: 'dsh-key-rotation: ref must be an environment variable name' } });
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
if (req.method === 'DELETE') {
|
|
288
|
+
await credentialsService.unset(ref);
|
|
289
|
+
json(res, 200, { ok: true, ref });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const value = typeof body?.value === 'string' ? body.value.trim() : '';
|
|
293
|
+
if (value.length === 0) {
|
|
294
|
+
json(res, 400, { error: { code: 'empty-value', message: 'dsh-key-rotation: an empty key cannot be stored' } });
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
await credentialsService.set(ref, value);
|
|
298
|
+
// #200: leak-detector hint - stored value should look like a credential
|
|
299
|
+
const secretShape = looksLikeApiSecret(value);
|
|
300
|
+
json(res, 200, { ok: true, ref, tail: keyTail(value), looksLikeSecret: secretShape });
|
|
301
|
+
} catch (error) {
|
|
302
|
+
// A ref supplied by the launching environment is read-only, and the
|
|
303
|
+
// service says so in plain words — pass that through to the card.
|
|
304
|
+
json(res, 409, { error: { code: 'write-rejected', message: String(error?.message ?? error) } });
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
}), 'dsh-key-rotation: key route');
|
|
308
|
+
|
|
309
|
+
// ── reset route: clear cooldown for a provider (or a single ref) ──
|
|
310
|
+
ctx.effect(() => ctx.webServer.register({
|
|
311
|
+
kind: 'exact',
|
|
312
|
+
path: RESET_PATH,
|
|
313
|
+
handler: async (req, res) => {
|
|
314
|
+
if (req.method !== 'POST') {
|
|
315
|
+
json(res, 405, { error: { code: 'method', message: 'POST only' } });
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (!isTrustedBridgeRequest(req)) {
|
|
319
|
+
json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: reset is local-only' } });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
let body;
|
|
323
|
+
try { body = await readJson(req); } catch (e) {
|
|
324
|
+
json(res, 400, { error: { code: 'bad-request', message: String(e?.message ?? e) } });
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const provider = typeof body?.provider === 'string' ? body.provider.trim() : '';
|
|
328
|
+
const ref = typeof body?.ref === 'string' ? body.ref.trim() : '';
|
|
329
|
+
if (provider) {
|
|
330
|
+
const st = poolState.get(provider);
|
|
331
|
+
if (!st) { json(res, 404, { error: { code: 'not-found', message: `dsh-key-rotation: no pool for '${provider}'` } }); return; }
|
|
332
|
+
const cleared = st.failedUntil.size;
|
|
333
|
+
st.failedUntil.clear();
|
|
334
|
+
st.failCounts?.clear();
|
|
335
|
+
st.authFailCounts?.clear();
|
|
336
|
+
st.brokenUntil?.clear();
|
|
337
|
+
st.switches = 0; st.lastReason = undefined; st.lastSwitchAt = undefined;
|
|
338
|
+
json(res, 200, { ok: true, provider, cleared });
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (ref) {
|
|
342
|
+
let found = false;
|
|
343
|
+
for (const st of poolState.values()) {
|
|
344
|
+
if (st.failedUntil.has(ref) || st.failCounts?.has(ref)) {
|
|
345
|
+
st.failedUntil.delete(ref);
|
|
346
|
+
st.failCounts?.delete(ref);
|
|
347
|
+
st.authFailCounts?.delete(ref);
|
|
348
|
+
st.brokenUntil?.delete(ref);
|
|
349
|
+
if (st.lastUsed === ref) st.lastUsed = undefined;
|
|
350
|
+
found = true; break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// idempotent: even if ref was not cooling, report ok if it looks like a valid ref name
|
|
354
|
+
if (!found && !isValidRef(ref)) { json(res, 400, { error: { code: 'bad-ref', message: 'dsh-key-rotation: ref must be an environment variable name' } }); return; }
|
|
355
|
+
json(res, 200, { ok: true, ref });
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
json(res, 400, { error: { code: 'bad-request', message: 'dsh-key-rotation: POST requires {"provider": "..."} or {"ref": "..."}' } });
|
|
359
|
+
},
|
|
360
|
+
}), 'dsh-key-rotation: reset route');
|
|
361
|
+
|
|
362
|
+
ctx.effect(() => ctx.webServer.register({
|
|
363
|
+
kind: 'exact',
|
|
364
|
+
path: IMPORT_PATH,
|
|
365
|
+
handler: async (req, res) => {
|
|
366
|
+
if (req.method !== 'POST') { json(res, 405, { error: { code: 'method', message: 'POST only' } }); return; }
|
|
367
|
+
if (!isTrustedBridgeRequest(req)) { json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: import is local-only' } }); return; }
|
|
368
|
+
let body; try { body = await readJson(req); } catch (e) { json(res, 400, { error: { code: 'bad-request', message: String(e?.message ?? e) } }); return; }
|
|
369
|
+
const url = typeof body?.url === 'string' ? body.url.trim() : '';
|
|
370
|
+
if (!url || !url.startsWith('https://')) { json(res, 400, { error: { code: 'bad-url', message: 'dsh-key-rotation: only HTTPS URLs are allowed' } }); return; }
|
|
371
|
+
try {
|
|
372
|
+
const resp = await fetch(url);
|
|
373
|
+
if (!resp.ok) { json(res, 400, { error: { code: 'fetch-failed', message: 'dsh-key-rotation: fetch returned ' + resp.status } }); return; }
|
|
374
|
+
const data = await resp.json();
|
|
375
|
+
if (!Array.isArray(data)) { json(res, 400, { error: { code: 'bad-format', message: 'dsh-key-rotation: expected JSON array of providers' } }); return; }
|
|
376
|
+
const settings = ctx.get('settings');
|
|
377
|
+
if (!settings) { json(res, 503, { error: { code: 'settings-rejected', message: 'dsh-key-rotation: no settings provider' } }); return; }
|
|
378
|
+
const desc = settings.describe({ redactSecrets: true }).find((c) => c.ns === NS);
|
|
379
|
+
const cur = desc?.value?.providers ?? [];
|
|
380
|
+
const merged = new Map();
|
|
381
|
+
for (const p of cur) if (p && p.provider) merged.set(p.provider, p);
|
|
382
|
+
for (const p of data) if (p && p.provider && typeof p.provider === 'string') merged.set(p.provider, p);
|
|
383
|
+
const mergedArr = [...merged.values()];
|
|
384
|
+
await settings.replace(NS, { ...(desc?.value ?? {}), providers: mergedArr }, desc?.revision);
|
|
385
|
+
json(res, 200, { ok: true, providersImported: data.length, total: mergedArr.length });
|
|
386
|
+
} catch (e) { json(res, 400, { error: { code: 'import-failed', message: String(e?.message ?? e) } }); }
|
|
387
|
+
},
|
|
388
|
+
}), 'dsh-key-rotation: import route');
|
|
389
|
+
|
|
390
|
+
// Health for external panels (Beszel/Uptime)
|
|
391
|
+
ctx.effect(() => ctx.webServer.register({
|
|
392
|
+
kind: 'exact',
|
|
393
|
+
path: HEALTH_PATH,
|
|
394
|
+
handler: async (req, res) => {
|
|
395
|
+
if (!isTrustedBridgeRequest(req) && req.socket?.remoteAddress !== '127.0.0.1' && req.socket?.remoteAddress !== '::1') { } // allow same-origin already checked
|
|
396
|
+
if (!isTrustedBridgeRequest(req)) {
|
|
397
|
+
// also allow plain loopback without Origin
|
|
398
|
+
if (!isLoopbackAddress(req.socket?.remoteAddress)) { res.writeHead(403); res.end(); return; }
|
|
399
|
+
if (req.headers['sec-fetch-site'] === 'cross-site') { res.writeHead(403); res.end(); return; }
|
|
400
|
+
}
|
|
401
|
+
if (req.method !== 'GET') { json(res, 405, { error: { code: 'method', message: 'GET only' } }); return; }
|
|
402
|
+
const now = Date.now();
|
|
403
|
+
const pools = {};
|
|
404
|
+
let exhaustedAny = false;
|
|
405
|
+
const { poolByRef: pr, providerTags } = buildRuntime();
|
|
406
|
+
const seenH = new Set();
|
|
407
|
+
for (const pool of pr.values()) {
|
|
408
|
+
if (seenH.has(pool.base)) continue;
|
|
409
|
+
seenH.add(pool.base);
|
|
410
|
+
let healthy = 0;
|
|
411
|
+
for (const ref of pool.refs) {
|
|
412
|
+
const until = pool.state.failedUntil.get(ref);
|
|
413
|
+
if (until !== undefined && until > now) continue;
|
|
414
|
+
const exp = pool.expiresAt?.[ref];
|
|
415
|
+
if (exp !== undefined && now >= exp) continue;
|
|
416
|
+
healthy++;
|
|
417
|
+
}
|
|
418
|
+
const total = pool.refs.length;
|
|
419
|
+
const exhausted = healthy === 0 && total > 0;
|
|
420
|
+
if (exhausted) exhaustedAny = true;
|
|
421
|
+
pools[pool.base] = { healthy, total, exhausted, healthScore: computeHealthScore(pool.state) };
|
|
422
|
+
}
|
|
423
|
+
json(res, 200, { status: exhaustedAny ? 'degraded' : 'ok', pools, exhaustedAny, latency: latencyHistogram.snapshotAll(), quota: quotaStore.snapshot() });
|
|
424
|
+
},
|
|
425
|
+
}), 'dsh-key-rotation: health');
|
|
426
|
+
|
|
427
|
+
// ── test route: dry-run a single key without rotation ──
|
|
428
|
+
ctx.effect(() => ctx.webServer.register({
|
|
429
|
+
kind: 'exact',
|
|
430
|
+
path: TEST_PATH,
|
|
431
|
+
handler: async (req, res) => {
|
|
432
|
+
if (req.method !== 'POST') { json(res, 405, { error: { code: 'method', message: 'POST only' } }); return; }
|
|
433
|
+
if (!isTrustedBridgeRequest(req)) { json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: test is local-only' } }); return; }
|
|
434
|
+
let body; try { body = await readJson(req); } catch (e) { json(res, 400, { error: { code: 'bad-request', message: String(e?.message ?? e) } }); return; }
|
|
435
|
+
const ref = typeof body?.ref === 'string' ? body.ref.trim() : '';
|
|
436
|
+
if (!isValidRef(ref)) { json(res, 400, { error: { code: 'bad-ref', message: 'dsh-key-rotation: ref must be an environment variable name' } }); return; }
|
|
437
|
+
// Optional value for pre-save validation (issue #118)
|
|
438
|
+
const testValue = typeof body?.value === 'string' && body.value.length > 0 ? body.value : undefined;
|
|
439
|
+
const probe = body?.probe === 'models' || body?.probe === 'chat' ? body.probe : undefined;
|
|
440
|
+
const base = ctx.get('credentials');
|
|
441
|
+
try {
|
|
442
|
+
let hit = await (base?.__dshKeyRotationOriginalResolve ?? base?.resolve)?.call(base, ref);
|
|
443
|
+
let present = Boolean(hit && typeof hit.value === 'string' && hit.value.length > 0);
|
|
444
|
+
const effectiveValue = testValue || hit?.value;
|
|
445
|
+
const valid = present ? Boolean(effectiveValue && typeof effectiveValue === 'string' && effectiveValue.length > 0) : Boolean(testValue);
|
|
446
|
+
const tail = valid ? keyTail(effectiveValue) : '';
|
|
447
|
+
let source = null;
|
|
448
|
+
try { const d = await base?.describe?.(ref); source = d?.source ?? null; } catch {}
|
|
449
|
+
if (!present && !testValue) { json(res, 200, { ok: false, ref, code: 'no-credential', message: 'no such credential' }); return; }
|
|
450
|
+
if (!present && testValue) { source = 'pre-save'; }
|
|
451
|
+
else if (!present) {
|
|
452
|
+
const ev = envValue(ref);
|
|
453
|
+
if (ev !== undefined) { present = true; json(res, 200, { ok: true, ref, tail: keyTail(ev), source: 'env' }); return; }
|
|
454
|
+
}
|
|
455
|
+
// sandbox probe (models is free; chat is hook-only, see sandbox.js)
|
|
456
|
+
if (probe) {
|
|
457
|
+
const keyForProbe = effectiveValue;
|
|
458
|
+
const runner = ensureSandboxRunner(ctx);
|
|
459
|
+
const result = probe === 'chat' ? await runner.probeChat(ref, keyForProbe) : await runner.probeModels(ref, keyForProbe);
|
|
460
|
+
const cached = { ...result, at: Date.now() };
|
|
461
|
+
lastTestCache.set(ref, cached);
|
|
462
|
+
if (cached.ok) {
|
|
463
|
+
for (const st of poolState.values()) {
|
|
464
|
+
if (st.failedUntil?.has(ref) || st.failCounts?.has(ref) || st.brokenUntil?.has(ref)) {
|
|
465
|
+
st.failedUntil?.delete(ref);
|
|
466
|
+
st.failCounts?.delete(ref);
|
|
467
|
+
st.authFailCounts?.delete(ref);
|
|
468
|
+
st.brokenUntil?.delete(ref);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
json(res, 200, { ok: cached.ok, ref, tail, source, probe, code: cached.code, latencyMs: cached.latencyMs, modelsCount: cached.modelsCount });
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
json(res, 200, { ok: true, ref, tail, source });
|
|
476
|
+
} catch (e) {
|
|
477
|
+
json(res, 200, { ok: false, ref, code: 'error', message: String(e?.message ?? e) });
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
}), 'dsh-key-rotation: test route');
|
|
481
|
+
|
|
482
|
+
// Intercept the llm/stream waterfall: rotate any request whose provider maps
|
|
483
|
+
// to a configured key pool; pass everything else (and internal dispatches)
|
|
484
|
+
// straight through.
|
|
485
|
+
// Read-only cache snapshot for clients (badge polling).
|
|
486
|
+
ctx.effect(() => ctx.webServer.register({
|
|
487
|
+
kind: 'exact',
|
|
488
|
+
path: SANDBOX_CACHE_PATH,
|
|
489
|
+
handler: (req, res) => {
|
|
490
|
+
if (!isTrustedBridgeRequest(req)) { json(res, 403, { error: { code: 'forbidden', message: 'dsh-key-rotation: cache is local-only' } }); return; }
|
|
491
|
+
json(res, 200, lastTestCache.snapshot());
|
|
492
|
+
},
|
|
493
|
+
}), 'dsh-key-rotation: sandbox cache');
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
// #199 webhook-action: interactive webhook buttons call back here.
|
|
498
|
+
// Auth: bearer token from Config (external services like Telegram/Discord
|
|
499
|
+
// cannot be same-origin, so a shared secret is the gate).
|
|
500
|
+
ctx.effect(() => ctx.webServer.register({
|
|
501
|
+
kind: 'exact',
|
|
502
|
+
path: '/dsh-key-rotation/webhook-action',
|
|
503
|
+
handler: async (req, res) => {
|
|
504
|
+
if (req.method !== 'POST') { json(res, 405, { error: { code: 'method', message: 'POST only' } }); return; }
|
|
505
|
+
const runtime = buildRuntime();
|
|
506
|
+
const expected = runtime.webhookActionToken;
|
|
507
|
+
if (!expected) { json(res, 503, { error: { code: 'no-token', message: 'dsh-key-rotation: webhookActionToken is not configured' } }); return; }
|
|
508
|
+
const auth = String(req.headers.authorization ?? '');
|
|
509
|
+
if (auth !== `Bearer ${expected}`) { json(res, 401, { error: { code: 'unauthorized', message: 'dsh-key-rotation: bad webhook action token' } }); return; }
|
|
510
|
+
let body;
|
|
511
|
+
try { body = await readJson(req); } catch (e) { json(res, 400, { error: { code: 'bad-request', message: String(e?.message ?? e) } }); return; }
|
|
512
|
+
// Accept callback payloads from formatInteractive (Telegram/Discord/Slack) or plain {action}
|
|
513
|
+
let action = typeof body?.action === 'string' ? body.action : '';
|
|
514
|
+
if (!action && typeof body?.data === 'string') {
|
|
515
|
+
try { action = String(JSON.parse(body.data)?.id ?? ''); } catch { action = ''; }
|
|
516
|
+
}
|
|
517
|
+
if (!action && typeof body?.callback_data === 'string') {
|
|
518
|
+
try { action = String(JSON.parse(body.callback_data)?.id ?? ''); } catch { action = ''; }
|
|
519
|
+
}
|
|
520
|
+
// #222: Telegram update envelope {update_id, callback_query:{data}}
|
|
521
|
+
if (!action && typeof body?.callback_query?.data === 'string') {
|
|
522
|
+
try { action = String(JSON.parse(body.callback_query.data)?.id ?? ''); } catch { action = ''; }
|
|
523
|
+
}
|
|
524
|
+
// #222: Telegram setWebhook registration helper
|
|
525
|
+
if (typeof body?.setWebhook === 'object' && body.setWebhook) {
|
|
526
|
+
const botToken = typeof body.setWebhook.botToken === 'string' ? body.setWebhook.botToken : '';
|
|
527
|
+
if (!botToken) { json(res, 400, { error: { code: 'bad-request', message: 'dsh-key-rotation: setWebhook.botToken required' } }); return; }
|
|
528
|
+
// derive the public URL from request headers; explicit URL wins
|
|
529
|
+
const url = typeof body.setWebhook.url === 'string' && body.setWebhook.url ? body.setWebhook.url : `https://${String(req.headers.host ?? '')}/dsh-key-rotation/webhook-action`;
|
|
530
|
+
try {
|
|
531
|
+
const hookRes = await fetch(`https://api.telegram.org/bot${botToken}/setWebhook`, {
|
|
532
|
+
method: 'POST', headers: { 'content-type': 'application/json' },
|
|
533
|
+
body: JSON.stringify({ url, allowed_updates: ['callback_query'] }),
|
|
534
|
+
});
|
|
535
|
+
const hookData = await hookRes.json().catch(() => ({}));
|
|
536
|
+
json(res, 200, { ok: hookRes.ok, url, telegram: hookData });
|
|
537
|
+
} catch (e) {
|
|
538
|
+
json(res, 502, { error: { code: 'telegram-failed', message: String(e?.message ?? e) } });
|
|
539
|
+
}
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (!action) { json(res, 400, { error: { code: 'bad-action', message: 'dsh-key-rotation: no action in payload' } }); return; }
|
|
543
|
+
const provider = action.startsWith('pause-') || action.startsWith('reset-') ? action.replace(/^(pause|reset)-/, '') : '';
|
|
544
|
+
try {
|
|
545
|
+
if (action === 'disable-rotation') {
|
|
546
|
+
setRotationDisabled(true);
|
|
547
|
+
console.warn('[dsh-key-rotation] rotation DISABLED via webhook action');
|
|
548
|
+
json(res, 200, { ok: true, action });
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (action === 'enable-rotation') {
|
|
552
|
+
setRotationDisabled(false);
|
|
553
|
+
json(res, 200, { ok: true, action });
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (action.startsWith('pause-') || action.startsWith('reset-')) {
|
|
557
|
+
const st = poolState.get(provider);
|
|
558
|
+
if (!st) { json(res, 404, { error: { code: 'not-found', message: `dsh-key-rotation: no pool for '${provider}'` } }); return; }
|
|
559
|
+
if (action.startsWith('pause-')) {
|
|
560
|
+
const until = Date.now() + 3600000; // 1h pause
|
|
561
|
+
for (const ref of (st.failedUntil ? [...st.failedUntil.keys()] : [])) st.failedUntil.set(ref, Math.max(st.failedUntil.get(ref) ?? 0, until));
|
|
562
|
+
// also pause every key currently healthy
|
|
563
|
+
for (const p of buildRuntime().poolByRef.values()) {
|
|
564
|
+
if (p.base !== provider) continue;
|
|
565
|
+
for (const ref of p.refs) st.failedUntil.set(ref, Math.max(st.failedUntil.get(ref) ?? 0, until));
|
|
566
|
+
}
|
|
567
|
+
console.warn(`[dsh-key-rotation] pool ${provider} PAUSED 1h via webhook action`);
|
|
568
|
+
json(res, 200, { ok: true, action, provider, until: Date.now() + 3600000 });
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const cleared = st.failedUntil.size;
|
|
572
|
+
st.failedUntil.clear(); st.failCounts?.clear(); st.brokenUntil?.clear();
|
|
573
|
+
console.warn(`[dsh-key-rotation] pool ${provider} RESET via webhook action`);
|
|
574
|
+
json(res, 200, { ok: true, action, provider, cleared });
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
json(res, 400, { error: { code: 'unknown-action', message: `dsh-key-rotation: unknown action '${action}'` } });
|
|
578
|
+
} catch (e) {
|
|
579
|
+
json(res, 500, { error: { code: 'action-failed', message: String(e?.message ?? e) } });
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
}), 'dsh-key-rotation: webhook-action');
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-key-rotation",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.40",
|
|
4
|
+
"packageManager": "pnpm@10.33.2",
|
|
4
5
|
"description": "Per-provider API key rotation for DeepSeek Harness: a key pool per provider, auto-created clone routes, and switching to the next key on quota/rate-limit errors. Includes a Settings section (Key Rotation) to edit the key pools, cooldown and switch codes.",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"deepseek-harness",
|
|
@@ -55,4 +56,4 @@
|
|
|
55
56
|
"scripts": {
|
|
56
57
|
"test": "node --test --test-timeout=10000 test/*.test.js test/*.test.mjs"
|
|
57
58
|
}
|
|
58
|
-
}
|
|
59
|
+
}
|