@goodandready/dsh-key-rotation 0.8.78 → 0.8.80
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/CHANGELOG.md +14 -0
- package/lib/concurrency.js +2 -115
- package/lib/config-compat.js +18 -1
- package/lib/core/concurrency.js +115 -0
- package/lib/core/index.js +6 -0
- package/lib/core/pool.js +2 -0
- package/lib/core/quota.js +39 -0
- package/lib/core/rotate.js +2 -0
- package/lib/core/state-machine.js +267 -0
- package/lib/http-bridge.js +2 -0
- package/lib/index.js +41 -27
- package/lib/key-state-machine.js +2 -0
- package/lib/ops-balance.js +2 -147
- package/lib/ops-config-io.js +2 -456
- package/lib/ops-keys.js +2 -567
- package/lib/ops-metrics.js +2 -92
- package/lib/ops-paths.js +2 -24
- package/lib/ops-quick-action.js +2 -168
- package/lib/ops-status.js +2 -269
- package/lib/ops-telemetry.js +2 -276
- package/lib/ops-test.js +2 -255
- package/lib/ops-webhook.js +2 -218
- package/lib/persistence.js +69 -10
- package/lib/pool-state.js +3 -0
- package/lib/pool.js +10 -15
- package/lib/quota.js +2 -39
- package/lib/routes/balance.js +147 -0
- package/lib/routes/config.js +456 -0
- package/lib/routes/guard.js +13 -0
- package/lib/routes/index.js +43 -0
- package/lib/routes/keys.js +567 -0
- package/lib/routes/metrics.js +92 -0
- package/lib/routes/paths.js +24 -0
- package/lib/routes/quick-action.js +168 -0
- package/lib/routes/status.js +269 -0
- package/lib/routes/telemetry.js +276 -0
- package/lib/routes/testing.js +255 -0
- package/lib/routes/webhook.js +218 -0
- package/lib/routes-ops.js +2 -29
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -55,6 +55,20 @@
|
|
|
55
55
|
- **Fail-Closed Balance & Probe Inspection**: Prevent sending ciphertext credentials (`enc:v1:...`) upstream if decryption fails (#539).
|
|
56
56
|
- **PBKDF2 Iteration Bounds & Async KDF**: Enforce strict iteration limits (`10000..200000`) and async derivation to eliminate event-loop blocking DoS (#542).
|
|
57
57
|
|
|
58
|
+
## [0.8.80] - 2026-10-30
|
|
59
|
+
|
|
60
|
+
### Changed
|
|
61
|
+
- **#530 (M) Domain Core Layer (`lib/core/`)**: Structured domain core modules under `lib/core/` (`state-machine.js`, `concurrency.js`, `quota.js`, `pool.js`, `rotate.js`, `index.js`) while maintaining transparent backwards-compatible root facades.
|
|
62
|
+
- **#528 (M) Cordis Dynamic Dependency Injection**: Reduced required static `inject` array to minimal `['llm']`. Wrapped HTTP routes and WebServer lifecycle in `ctx.inject(['webServer'], ...)`, enabling fully headless/CLI operation without crashing when WebServer or settings are unmounted.
|
|
63
|
+
- **#525 (M) Append-Only Event Journal Persistence**: Introduced incremental append-only event journaling (`*-journal.jsonl`) alongside debounced periodic atomic snapshots. Eliminates heavy O(N) full state tree disk rewrites on routine key switches and cooldowns, automatically compacting and rotating upon snapshot flush.
|
|
64
|
+
|
|
65
|
+
## [0.8.79] - 2026-10-30
|
|
66
|
+
|
|
67
|
+
### Changed
|
|
68
|
+
- **#522 (M) Routes Modularization (`lib/routes/`)**: Unified HTTP operational routes structure. Extracted individual route registrars (`status.js`, `keys.js`, `metrics.js`, `balance.js`, `telemetry.js`, `testing.js`, `webhook.js`, `config.js`, `quick-action.js`, `guard.js`) with backwards-compatible re-export facades in `lib/ops-*.js` and `lib/routes-ops.js`.
|
|
69
|
+
- **#551 (M) Schemastery Volatile Config Migration**: Eliminated custom HTTP config bridge dependency in favor of native Schemastery `.volatile()` schema and `unwrapVolatileConfig()`. Added deprecation warnings and `X-DSH-Deprecated` headers on legacy `/dsh-key-rotation/config` endpoints.
|
|
70
|
+
- **#529 (M) Key State Machine (`lib/key-state-machine.js`)**: Encapsulated key rotation state transitions into a formal, deterministic State Machine (`ACTIVE`, `COOLDOWN`, `PROBING`, `EXHAUSTED`, `PAUSED`, `REVOKED`) with transition history tracking, strict transition rule enforcement, and state distribution statistics.
|
|
71
|
+
|
|
58
72
|
## [0.8.72] — 2026-10-23
|
|
59
73
|
|
|
60
74
|
### Security
|
package/lib/concurrency.js
CHANGED
|
@@ -1,115 +1,2 @@
|
|
|
1
|
-
// concurrency.js —
|
|
2
|
-
|
|
3
|
-
const CONCURRENCY_DEFAULT_LIMIT = 0;
|
|
4
|
-
// Default stale timeout: 30 minutes to support prolonged streaming generation (#463)
|
|
5
|
-
const CONCURRENCY_STALE_LOCK_MS = 30 * 60 * 1000;
|
|
6
|
-
|
|
7
|
-
export class ConcurrencyTracker {
|
|
8
|
-
constructor(opts) {
|
|
9
|
-
opts = opts || {};
|
|
10
|
-
const limit = opts.limit !== undefined ? opts.limit : CONCURRENCY_DEFAULT_LIMIT;
|
|
11
|
-
this._limit = (Number.isFinite(limit) && limit >= 0) ? Math.floor(limit) : 0;
|
|
12
|
-
this._staleMs = opts.staleMs || CONCURRENCY_STALE_LOCK_MS;
|
|
13
|
-
this._inFlight = new Map();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
configure(opts) {
|
|
17
|
-
if (!opts) return;
|
|
18
|
-
if (opts.limit !== undefined) {
|
|
19
|
-
this._limit = (Number.isFinite(opts.limit) && opts.limit >= 0) ? Math.floor(opts.limit) : 0;
|
|
20
|
-
}
|
|
21
|
-
if (opts.staleMs !== undefined && Number.isFinite(opts.staleMs) && opts.staleMs > 0) {
|
|
22
|
-
this._staleMs = opts.staleMs;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
isEnabled() { return this._limit > 0; }
|
|
27
|
-
get limit() { return this._limit; }
|
|
28
|
-
|
|
29
|
-
acquire(ref, now, limit) {
|
|
30
|
-
now = now || Date.now();
|
|
31
|
-
const effectiveLimit = (typeof limit === 'number' && limit >= 0) ? Math.floor(limit) : this._limit;
|
|
32
|
-
let e = this._inFlight.get(ref);
|
|
33
|
-
if (!e) {
|
|
34
|
-
e = { count: 0, lastAcquired: now };
|
|
35
|
-
this._inFlight.set(ref, e);
|
|
36
|
-
}
|
|
37
|
-
if (now - e.lastAcquired > this._staleMs) {
|
|
38
|
-
e.count = 0;
|
|
39
|
-
}
|
|
40
|
-
if (effectiveLimit > 0 && e.count >= effectiveLimit) return false;
|
|
41
|
-
e.count += 1;
|
|
42
|
-
e.lastAcquired = now;
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
touch(ref, now) {
|
|
47
|
-
now = now || Date.now();
|
|
48
|
-
const e = this._inFlight.get(ref);
|
|
49
|
-
if (e && e.count > 0) {
|
|
50
|
-
e.lastAcquired = now;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
release(ref, now) {
|
|
55
|
-
now = now || Date.now();
|
|
56
|
-
const e = this._inFlight.get(ref);
|
|
57
|
-
if (!e) return;
|
|
58
|
-
e.count = Math.max(0, e.count - 1);
|
|
59
|
-
e.lastAcquired = now;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
getActive(ref, now) {
|
|
63
|
-
now = now || Date.now();
|
|
64
|
-
if (!ref) return 0;
|
|
65
|
-
const e = this._inFlight.get(ref);
|
|
66
|
-
if (!e) return 0;
|
|
67
|
-
if (now - e.lastAcquired > this._staleMs) {
|
|
68
|
-
e.count = 0;
|
|
69
|
-
return 0;
|
|
70
|
-
}
|
|
71
|
-
return e.count;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
snapshot(now) {
|
|
75
|
-
now = now || Date.now();
|
|
76
|
-
const out = {};
|
|
77
|
-
for (const [k, v] of this._inFlight) {
|
|
78
|
-
let count = v.count;
|
|
79
|
-
if (now - v.lastAcquired > this._staleMs) {
|
|
80
|
-
count = 0;
|
|
81
|
-
v.count = 0;
|
|
82
|
-
}
|
|
83
|
-
out[k] = { count };
|
|
84
|
-
}
|
|
85
|
-
return out;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
pickLeastLoaded(candidates, now, weights, limit) {
|
|
89
|
-
now = now || Date.now();
|
|
90
|
-
const effectiveLimit = (typeof limit === 'number' && limit >= 0) ? Math.floor(limit) : this._limit;
|
|
91
|
-
if (!Array.isArray(candidates) || candidates.length === 0) return null;
|
|
92
|
-
let best = null;
|
|
93
|
-
let bestScore = Infinity;
|
|
94
|
-
for (const ref of candidates) {
|
|
95
|
-
const e = this._inFlight.get(ref);
|
|
96
|
-
let count = e ? e.count : 0;
|
|
97
|
-
if (e && now - e.lastAcquired > this._staleMs) count = 0;
|
|
98
|
-
if (effectiveLimit > 0 && count >= effectiveLimit) continue;
|
|
99
|
-
const w = (weights && Number(weights[ref]) > 0) ? Number(weights[ref]) : 1;
|
|
100
|
-
const score = count / w;
|
|
101
|
-
if (score < bestScore) {
|
|
102
|
-
best = ref;
|
|
103
|
-
bestScore = score;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return best;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
clear(ref) {
|
|
110
|
-
if (ref) this._inFlight.delete(ref);
|
|
111
|
-
else this._inFlight.clear();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
get size() { return this._inFlight.size; }
|
|
115
|
-
}
|
|
1
|
+
// lib/concurrency.js — Backwards-compatible facade (#530).
|
|
2
|
+
export * from './core/concurrency.js';
|
package/lib/config-compat.js
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
// lib/config-compat.js — Schemastery volatile decoding and config resolution (#551).
|
|
1
2
|
// Decode modern volatile Config references while retaining stable identities for
|
|
2
3
|
// the request hot path. Legacy cores pass ordinary resolved configuration.
|
|
3
4
|
// If ctx is provided, invalidation listeners cache the unwrapped tree so the hot
|
|
4
5
|
// path never recursively walks volatile nodes on unchanged configuration.
|
|
6
|
+
|
|
7
|
+
/** Read each volatile field through its live reference, if the loader installed one (#551). */
|
|
8
|
+
export function unwrapVolatileConfig(config) {
|
|
9
|
+
if (!config || typeof config !== 'object') return {};
|
|
10
|
+
const source = config;
|
|
11
|
+
const out = {};
|
|
12
|
+
for (const key of Object.keys(source)) {
|
|
13
|
+
const value = source[key];
|
|
14
|
+
out[key] = (value !== null && typeof value === 'object' && typeof value.get === 'function')
|
|
15
|
+
? value.get()
|
|
16
|
+
: value;
|
|
17
|
+
}
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
|
|
5
21
|
export function createConfigReader(config, ctx) {
|
|
6
22
|
let previous;
|
|
7
23
|
let dirty = true;
|
|
@@ -34,7 +50,8 @@ export function createConfigReader(config, ctx) {
|
|
|
34
50
|
if (ctx && !dirty && previous !== undefined) {
|
|
35
51
|
return previous;
|
|
36
52
|
}
|
|
37
|
-
|
|
53
|
+
const unwrapped = unwrapVolatileConfig(config);
|
|
54
|
+
previous = read(unwrapped, previous);
|
|
38
55
|
dirty = false;
|
|
39
56
|
return previous;
|
|
40
57
|
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// concurrency.js — per-key in-flight counter + least-connections picking (issues #193, #407, #463).
|
|
2
|
+
|
|
3
|
+
const CONCURRENCY_DEFAULT_LIMIT = 0;
|
|
4
|
+
// Default stale timeout: 30 minutes to support prolonged streaming generation (#463)
|
|
5
|
+
const CONCURRENCY_STALE_LOCK_MS = 30 * 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export class ConcurrencyTracker {
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
opts = opts || {};
|
|
10
|
+
const limit = opts.limit !== undefined ? opts.limit : CONCURRENCY_DEFAULT_LIMIT;
|
|
11
|
+
this._limit = (Number.isFinite(limit) && limit >= 0) ? Math.floor(limit) : 0;
|
|
12
|
+
this._staleMs = opts.staleMs || CONCURRENCY_STALE_LOCK_MS;
|
|
13
|
+
this._inFlight = new Map();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
configure(opts) {
|
|
17
|
+
if (!opts) return;
|
|
18
|
+
if (opts.limit !== undefined) {
|
|
19
|
+
this._limit = (Number.isFinite(opts.limit) && opts.limit >= 0) ? Math.floor(opts.limit) : 0;
|
|
20
|
+
}
|
|
21
|
+
if (opts.staleMs !== undefined && Number.isFinite(opts.staleMs) && opts.staleMs > 0) {
|
|
22
|
+
this._staleMs = opts.staleMs;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
isEnabled() { return this._limit > 0; }
|
|
27
|
+
get limit() { return this._limit; }
|
|
28
|
+
|
|
29
|
+
acquire(ref, now, limit) {
|
|
30
|
+
now = now || Date.now();
|
|
31
|
+
const effectiveLimit = (typeof limit === 'number' && limit >= 0) ? Math.floor(limit) : this._limit;
|
|
32
|
+
let e = this._inFlight.get(ref);
|
|
33
|
+
if (!e) {
|
|
34
|
+
e = { count: 0, lastAcquired: now };
|
|
35
|
+
this._inFlight.set(ref, e);
|
|
36
|
+
}
|
|
37
|
+
if (now - e.lastAcquired > this._staleMs) {
|
|
38
|
+
e.count = 0;
|
|
39
|
+
}
|
|
40
|
+
if (effectiveLimit > 0 && e.count >= effectiveLimit) return false;
|
|
41
|
+
e.count += 1;
|
|
42
|
+
e.lastAcquired = now;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
touch(ref, now) {
|
|
47
|
+
now = now || Date.now();
|
|
48
|
+
const e = this._inFlight.get(ref);
|
|
49
|
+
if (e && e.count > 0) {
|
|
50
|
+
e.lastAcquired = now;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
release(ref, now) {
|
|
55
|
+
now = now || Date.now();
|
|
56
|
+
const e = this._inFlight.get(ref);
|
|
57
|
+
if (!e) return;
|
|
58
|
+
e.count = Math.max(0, e.count - 1);
|
|
59
|
+
e.lastAcquired = now;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getActive(ref, now) {
|
|
63
|
+
now = now || Date.now();
|
|
64
|
+
if (!ref) return 0;
|
|
65
|
+
const e = this._inFlight.get(ref);
|
|
66
|
+
if (!e) return 0;
|
|
67
|
+
if (now - e.lastAcquired > this._staleMs) {
|
|
68
|
+
e.count = 0;
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
return e.count;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
snapshot(now) {
|
|
75
|
+
now = now || Date.now();
|
|
76
|
+
const out = {};
|
|
77
|
+
for (const [k, v] of this._inFlight) {
|
|
78
|
+
let count = v.count;
|
|
79
|
+
if (now - v.lastAcquired > this._staleMs) {
|
|
80
|
+
count = 0;
|
|
81
|
+
v.count = 0;
|
|
82
|
+
}
|
|
83
|
+
out[k] = { count };
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pickLeastLoaded(candidates, now, weights, limit) {
|
|
89
|
+
now = now || Date.now();
|
|
90
|
+
const effectiveLimit = (typeof limit === 'number' && limit >= 0) ? Math.floor(limit) : this._limit;
|
|
91
|
+
if (!Array.isArray(candidates) || candidates.length === 0) return null;
|
|
92
|
+
let best = null;
|
|
93
|
+
let bestScore = Infinity;
|
|
94
|
+
for (const ref of candidates) {
|
|
95
|
+
const e = this._inFlight.get(ref);
|
|
96
|
+
let count = e ? e.count : 0;
|
|
97
|
+
if (e && now - e.lastAcquired > this._staleMs) count = 0;
|
|
98
|
+
if (effectiveLimit > 0 && count >= effectiveLimit) continue;
|
|
99
|
+
const w = (weights && Number(weights[ref]) > 0) ? Number(weights[ref]) : 1;
|
|
100
|
+
const score = count / w;
|
|
101
|
+
if (score < bestScore) {
|
|
102
|
+
best = ref;
|
|
103
|
+
bestScore = score;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return best;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
clear(ref) {
|
|
110
|
+
if (ref) this._inFlight.delete(ref);
|
|
111
|
+
else this._inFlight.clear();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get size() { return this._inFlight.size; }
|
|
115
|
+
}
|
package/lib/core/pool.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// lib/quota.js — quota-remaining persistence per ref.
|
|
2
|
+
// ponytail: pure helpers, snapshot() returns shallow copies.
|
|
3
|
+
|
|
4
|
+
export class QuotaStore {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._data = new Map(); // ref -> { remaining, limit, reset, at }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
set(ref, info) {
|
|
10
|
+
if (!ref) return;
|
|
11
|
+
if (!info || typeof info !== 'object') return;
|
|
12
|
+
const next = {
|
|
13
|
+
remaining: Number.isFinite(info.remaining) ? info.remaining : null,
|
|
14
|
+
limit: Number.isFinite(info.limit) ? info.limit : null,
|
|
15
|
+
reset: Number.isFinite(info.reset) ? info.reset : null,
|
|
16
|
+
at: Number.isFinite(info.at) ? info.at : Date.now(),
|
|
17
|
+
};
|
|
18
|
+
this._data.set(ref, next);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get(ref) {
|
|
22
|
+
return this._data.get(ref);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
snapshot() {
|
|
26
|
+
const out = {};
|
|
27
|
+
for (const [k, v] of this._data) out[k] = v;
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
clear(ref) {
|
|
32
|
+
if (ref) this._data.delete(ref);
|
|
33
|
+
else this._data.clear();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get size() {
|
|
37
|
+
return this._data.size;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// lib/key-state-machine.js — Encapsulated Key State Machine (#529).
|
|
2
|
+
// Manages credential lifecycle across deterministic states and valid transitions.
|
|
3
|
+
|
|
4
|
+
export const KEY_STATES = Object.freeze({
|
|
5
|
+
ACTIVE: 'ACTIVE',
|
|
6
|
+
COOLDOWN: 'COOLDOWN',
|
|
7
|
+
PROBING: 'PROBING',
|
|
8
|
+
EXHAUSTED: 'EXHAUSTED',
|
|
9
|
+
PAUSED: 'PAUSED',
|
|
10
|
+
REVOKED: 'REVOKED',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const VALID_TRANSITIONS = Object.freeze({
|
|
14
|
+
[KEY_STATES.ACTIVE]: Object.freeze([
|
|
15
|
+
KEY_STATES.COOLDOWN,
|
|
16
|
+
KEY_STATES.EXHAUSTED,
|
|
17
|
+
KEY_STATES.PAUSED,
|
|
18
|
+
KEY_STATES.REVOKED,
|
|
19
|
+
]),
|
|
20
|
+
[KEY_STATES.COOLDOWN]: Object.freeze([
|
|
21
|
+
KEY_STATES.PROBING,
|
|
22
|
+
KEY_STATES.ACTIVE,
|
|
23
|
+
KEY_STATES.PAUSED,
|
|
24
|
+
KEY_STATES.REVOKED,
|
|
25
|
+
]),
|
|
26
|
+
[KEY_STATES.PROBING]: Object.freeze([
|
|
27
|
+
KEY_STATES.ACTIVE,
|
|
28
|
+
KEY_STATES.COOLDOWN,
|
|
29
|
+
KEY_STATES.PAUSED,
|
|
30
|
+
KEY_STATES.REVOKED,
|
|
31
|
+
]),
|
|
32
|
+
[KEY_STATES.EXHAUSTED]: Object.freeze([
|
|
33
|
+
KEY_STATES.ACTIVE,
|
|
34
|
+
KEY_STATES.COOLDOWN,
|
|
35
|
+
KEY_STATES.PAUSED,
|
|
36
|
+
KEY_STATES.REVOKED,
|
|
37
|
+
]),
|
|
38
|
+
[KEY_STATES.PAUSED]: Object.freeze([
|
|
39
|
+
KEY_STATES.ACTIVE,
|
|
40
|
+
KEY_STATES.COOLDOWN,
|
|
41
|
+
KEY_STATES.REVOKED,
|
|
42
|
+
]),
|
|
43
|
+
[KEY_STATES.REVOKED]: Object.freeze([
|
|
44
|
+
KEY_STATES.ACTIVE,
|
|
45
|
+
]),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export class KeyStateMachine {
|
|
49
|
+
/**
|
|
50
|
+
* @param {object} [options]
|
|
51
|
+
* @param {number} [options.maxHistory] Maximum transition history entries per pool
|
|
52
|
+
*/
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
/** @type {Map<string, { state: string, updatedAt: number, previousState?: string, reason?: string, failedUntil?: number }>} */
|
|
55
|
+
this.keyRecords = new Map();
|
|
56
|
+
/** @type {Array<{ ref: string, from: string, to: string, at: number, reason: string|null }>} */
|
|
57
|
+
this.history = [];
|
|
58
|
+
this.maxHistory = options.maxHistory ?? 100;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Computes the current deterministic state of a key reference.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} keyRef
|
|
65
|
+
* @param {object} [context] Optional pool state context for backward compatibility
|
|
66
|
+
* @param {Set<string>} [context.revokedRefs]
|
|
67
|
+
* @param {Set<string>} [context.pausedRefs]
|
|
68
|
+
* @param {Map<string, number>} [context.failedUntil]
|
|
69
|
+
* @param {number} [now]
|
|
70
|
+
* @returns {string} Current KEY_STATES value
|
|
71
|
+
*/
|
|
72
|
+
getState(keyRef, context = {}, now = Date.now()) {
|
|
73
|
+
if (!keyRef) return KEY_STATES.ACTIVE;
|
|
74
|
+
|
|
75
|
+
// 1. Revocation is absolute
|
|
76
|
+
if (context.revokedRefs && context.revokedRefs.has(keyRef)) {
|
|
77
|
+
return KEY_STATES.REVOKED;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 2. Manual pause overrides operational state
|
|
81
|
+
if (context.pausedRefs && context.pausedRefs.has(keyRef)) {
|
|
82
|
+
return KEY_STATES.PAUSED;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const record = this.keyRecords.get(keyRef);
|
|
86
|
+
if (!record) {
|
|
87
|
+
const failedUntil = context.failedUntil?.get?.(keyRef);
|
|
88
|
+
if (typeof failedUntil === 'number' && failedUntil > now) {
|
|
89
|
+
return KEY_STATES.COOLDOWN;
|
|
90
|
+
}
|
|
91
|
+
return KEY_STATES.ACTIVE;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Cooldown transition to probing when timer elapses
|
|
95
|
+
if (record.state === KEY_STATES.COOLDOWN) {
|
|
96
|
+
const until = record.failedUntil ?? context.failedUntil?.get?.(keyRef);
|
|
97
|
+
if (typeof until === 'number' && until <= now) {
|
|
98
|
+
return KEY_STATES.PROBING;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return record.state;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Transitions a key reference to a new state if valid.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} keyRef
|
|
109
|
+
* @param {string} targetState Target KEY_STATES value
|
|
110
|
+
* @param {object} [metadata]
|
|
111
|
+
* @param {string} [metadata.reason]
|
|
112
|
+
* @param {number} [metadata.failedUntil]
|
|
113
|
+
* @param {object} [metadata.context]
|
|
114
|
+
* @param {number} [now]
|
|
115
|
+
* @returns {string|null} Resulting state or null on failure
|
|
116
|
+
*/
|
|
117
|
+
transition(keyRef, targetState, metadata = {}, now = Date.now()) {
|
|
118
|
+
if (!keyRef || !targetState) return null;
|
|
119
|
+
|
|
120
|
+
const currentState = this.getState(keyRef, metadata.context, now);
|
|
121
|
+
if (currentState === targetState) return currentState;
|
|
122
|
+
|
|
123
|
+
const allowed = VALID_TRANSITIONS[currentState] || [];
|
|
124
|
+
if (!allowed.includes(targetState)) {
|
|
125
|
+
// In relaxed mode, force transition with logged anomaly
|
|
126
|
+
targetState = targetState in KEY_STATES ? targetState : KEY_STATES.ACTIVE;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const record = this.keyRecords.get(keyRef) ?? {
|
|
130
|
+
state: currentState,
|
|
131
|
+
updatedAt: now,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
record.previousState = currentState;
|
|
135
|
+
record.state = targetState;
|
|
136
|
+
record.updatedAt = now;
|
|
137
|
+
if (metadata.reason !== undefined) record.reason = metadata.reason;
|
|
138
|
+
if (metadata.failedUntil !== undefined) record.failedUntil = metadata.failedUntil;
|
|
139
|
+
|
|
140
|
+
this.keyRecords.set(keyRef, record);
|
|
141
|
+
|
|
142
|
+
const event = {
|
|
143
|
+
ref: keyRef,
|
|
144
|
+
from: currentState,
|
|
145
|
+
to: targetState,
|
|
146
|
+
at: now,
|
|
147
|
+
reason: metadata.reason ?? null,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
this.history.push(event);
|
|
151
|
+
if (this.history.length > this.maxHistory) {
|
|
152
|
+
this.history.shift();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Synchronize external pool state if provided
|
|
156
|
+
if (metadata.context) {
|
|
157
|
+
if (targetState === KEY_STATES.REVOKED) {
|
|
158
|
+
metadata.context.revokedRefs?.add?.(keyRef);
|
|
159
|
+
metadata.context.pausedRefs?.delete?.(keyRef);
|
|
160
|
+
} else if (targetState === KEY_STATES.PAUSED) {
|
|
161
|
+
metadata.context.pausedRefs?.add?.(keyRef);
|
|
162
|
+
} else if (currentState === KEY_STATES.PAUSED && targetState !== KEY_STATES.PAUSED) {
|
|
163
|
+
metadata.context.pausedRefs?.delete?.(keyRef);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return targetState;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Puts key into COOLDOWN state until specified duration elapses.
|
|
172
|
+
*/
|
|
173
|
+
transitionToCooldown(keyRef, options = {}, now = Date.now()) {
|
|
174
|
+
const cooldownMs = Number(options.cooldownMs) > 0 ? Number(options.cooldownMs) : 60000;
|
|
175
|
+
const failedUntil = now + cooldownMs;
|
|
176
|
+
return this.transition(keyRef, KEY_STATES.COOLDOWN, {
|
|
177
|
+
failedUntil,
|
|
178
|
+
reason: options.reason || 'COOLDOWN',
|
|
179
|
+
context: options.context,
|
|
180
|
+
}, now);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Marks key as ready for canary probe dispatch. */
|
|
184
|
+
markProbing(keyRef, metadata = {}, now = Date.now()) {
|
|
185
|
+
return this.transition(keyRef, KEY_STATES.PROBING, {
|
|
186
|
+
reason: metadata.reason || 'CANARY_PROBE',
|
|
187
|
+
context: metadata.context,
|
|
188
|
+
}, now);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Activates key following successful request or probe. */
|
|
192
|
+
activate(keyRef, metadata = {}, now = Date.now()) {
|
|
193
|
+
return this.transition(keyRef, KEY_STATES.ACTIVE, {
|
|
194
|
+
reason: metadata.reason || 'SUCCESS',
|
|
195
|
+
context: metadata.context,
|
|
196
|
+
}, now);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Marks key as exhausted due to local quota or TPM/RPM window. */
|
|
200
|
+
markExhausted(keyRef, metadata = {}, now = Date.now()) {
|
|
201
|
+
return this.transition(keyRef, KEY_STATES.EXHAUSTED, {
|
|
202
|
+
reason: metadata.reason || 'QUOTA_EXHAUSTED',
|
|
203
|
+
context: metadata.context,
|
|
204
|
+
}, now);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Manually pauses key from rotation. */
|
|
208
|
+
pause(keyRef, reason = 'USER_PAUSE', metadata = {}, now = Date.now()) {
|
|
209
|
+
return this.transition(keyRef, KEY_STATES.PAUSED, {
|
|
210
|
+
reason,
|
|
211
|
+
context: metadata.context,
|
|
212
|
+
}, now);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Unpauses key, returning it to active rotation. */
|
|
216
|
+
unpause(keyRef, metadata = {}, now = Date.now()) {
|
|
217
|
+
return this.transition(keyRef, KEY_STATES.ACTIVE, {
|
|
218
|
+
reason: 'USER_RESUME',
|
|
219
|
+
context: metadata.context,
|
|
220
|
+
}, now);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Revokes key permanently until credentials change. */
|
|
224
|
+
revoke(keyRef, reason = 'INVALID_CREDENTIAL', metadata = {}, now = Date.now()) {
|
|
225
|
+
return this.transition(keyRef, KEY_STATES.REVOKED, {
|
|
226
|
+
reason,
|
|
227
|
+
context: metadata.context,
|
|
228
|
+
}, now);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Checks if key is usable for dispatch (ACTIVE or PROBING). */
|
|
232
|
+
isUsable(keyRef, context = {}, now = Date.now()) {
|
|
233
|
+
const state = this.getState(keyRef, context, now);
|
|
234
|
+
return state === KEY_STATES.ACTIVE || state === KEY_STATES.PROBING;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Returns transition history for a specific key or all keys. */
|
|
238
|
+
getHistory(keyRef) {
|
|
239
|
+
if (!keyRef) return this.history.slice();
|
|
240
|
+
return this.history.filter((e) => e.ref === keyRef);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Returns state distribution statistics across a set of keys. */
|
|
244
|
+
getStats(refs = [], context = {}, now = Date.now()) {
|
|
245
|
+
const stats = {
|
|
246
|
+
total: refs.length,
|
|
247
|
+
[KEY_STATES.ACTIVE]: 0,
|
|
248
|
+
[KEY_STATES.COOLDOWN]: 0,
|
|
249
|
+
[KEY_STATES.PROBING]: 0,
|
|
250
|
+
[KEY_STATES.EXHAUSTED]: 0,
|
|
251
|
+
[KEY_STATES.PAUSED]: 0,
|
|
252
|
+
[KEY_STATES.REVOKED]: 0,
|
|
253
|
+
};
|
|
254
|
+
for (const ref of refs) {
|
|
255
|
+
const state = this.getState(ref, context, now);
|
|
256
|
+
if (state in stats) stats[state]++;
|
|
257
|
+
}
|
|
258
|
+
return stats;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
clear() {
|
|
262
|
+
this.keyRecords.clear();
|
|
263
|
+
this.history = [];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export const defaultKeyStateMachine = new KeyStateMachine();
|
package/lib/http-bridge.js
CHANGED
|
@@ -183,6 +183,8 @@ export function scanForLiveSecrets(section) {
|
|
|
183
183
|
|
|
184
184
|
/** Config bridge GET/PUT/DELETE — extracted from lib/index.js for size (#253). */
|
|
185
185
|
export async function handleConfigBridge(ctx, request, res, getCloneIds) {
|
|
186
|
+
res.setHeader?.('X-DSH-Deprecated', 'true');
|
|
187
|
+
res.setHeader?.('Deprecation', '@goodandready/dsh-key-rotation v0.8.79: use Schemastery volatile schema instead of direct config HTTP bridge');
|
|
186
188
|
if (!isTrustedBridgeRequest(request)) {
|
|
187
189
|
json(res, 403, { error: { code: 'forbidden', message: 'Settings writes require a trusted loopback origin' } });
|
|
188
190
|
return;
|