kagura-memory 0.6.0 → 0.7.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.
- package/README.md +106 -3
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +472 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +473 -2
- package/dist/index.d.ts +473 -2
- package/dist/index.js +458 -1
- package/dist/index.js.map +1 -1
- package/package.json +12 -2
package/dist/index.js
CHANGED
|
@@ -126,6 +126,12 @@ var KaguraFetchError = class extends KaguraError {
|
|
|
126
126
|
};
|
|
127
127
|
var KaguraIngestError = class extends KaguraError {
|
|
128
128
|
};
|
|
129
|
+
var KaguraSecretError = class extends KaguraError {
|
|
130
|
+
};
|
|
131
|
+
var KaguraCryptoError = class extends KaguraSecretError {
|
|
132
|
+
};
|
|
133
|
+
var KaguraKeyCustodyError = class extends KaguraSecretError {
|
|
134
|
+
};
|
|
129
135
|
|
|
130
136
|
// src/auth/credentials.ts
|
|
131
137
|
import * as crypto from "crypto";
|
|
@@ -137,7 +143,7 @@ import * as path3 from "path";
|
|
|
137
143
|
import { setTimeout as sleepMs } from "timers/promises";
|
|
138
144
|
|
|
139
145
|
// src/version.ts
|
|
140
|
-
var SDK_VERSION = "0.
|
|
146
|
+
var SDK_VERSION = "0.7.0";
|
|
141
147
|
|
|
142
148
|
// src/http.ts
|
|
143
149
|
function baseUrlFromMcp(mcpUrl) {
|
|
@@ -1261,6 +1267,38 @@ var KaguraClient = class _KaguraClient {
|
|
|
1261
1267
|
_KaguraClient.raiseForMcpError(result, toolName);
|
|
1262
1268
|
return result;
|
|
1263
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* Call any MCP tool by name — the escape hatch for tools this SDK has no
|
|
1272
|
+
* typed wrapper for yet (#28).
|
|
1273
|
+
*
|
|
1274
|
+
* Everything above is a typed wrapper over a specific tool, and that is
|
|
1275
|
+
* still the surface to prefer: wrappers validate arguments locally, map
|
|
1276
|
+
* camelCase to the wire's snake_case, and give you a return type. But
|
|
1277
|
+
* `callTool` is private, so before this existed one forgotten wrapper left
|
|
1278
|
+
* a tool completely unreachable — a caller had to vendor a patched SDK or
|
|
1279
|
+
* hand-roll JSON-RPC. `secret_*` was exactly that case.
|
|
1280
|
+
*
|
|
1281
|
+
* Args are passed through **verbatim**, so they must already be in wire
|
|
1282
|
+
* form (`context_id`, not `contextId`), and the result is an untyped
|
|
1283
|
+
* `ToolResult`.
|
|
1284
|
+
*
|
|
1285
|
+
* Domain errors are still translated, so a server `{"status": "error"}`
|
|
1286
|
+
* throws rather than coming back as data — but only error codes the SDK
|
|
1287
|
+
* recognizes get a specific class. A code from a tool with no wrapper
|
|
1288
|
+
* (`secret_not_found`, say) lands on the generic {@link KaguraError}, so
|
|
1289
|
+
* match on the message or add the code to `raiseForMcpError` when you
|
|
1290
|
+
* confirm it against the server.
|
|
1291
|
+
*
|
|
1292
|
+
* Use `getToolDefinitions()` to discover what the connected server offers.
|
|
1293
|
+
*
|
|
1294
|
+
* @throws Error if `toolName` is empty or whitespace.
|
|
1295
|
+
*/
|
|
1296
|
+
async callRawTool(toolName, args = {}) {
|
|
1297
|
+
if (typeof toolName !== "string" || !toolName.trim()) {
|
|
1298
|
+
throw new Error("toolName must be a non-empty string");
|
|
1299
|
+
}
|
|
1300
|
+
return this.callToolChecked(toolName, args);
|
|
1301
|
+
}
|
|
1264
1302
|
/**
|
|
1265
1303
|
* Translate an MCP tool's structured error response to an SDK error.
|
|
1266
1304
|
*
|
|
@@ -3404,6 +3442,411 @@ function isMintShape(payload) {
|
|
|
3404
3442
|
return typeof rec.id === "number" && typeof rec.name === "string" && typeof rec.key_prefix === "string";
|
|
3405
3443
|
}
|
|
3406
3444
|
|
|
3445
|
+
// src/secrets/crypto.ts
|
|
3446
|
+
import { createHash as createHash2 } from "crypto";
|
|
3447
|
+
var RECIPIENT_RE = /^age1[02-9ac-hj-np-z]{20,110}$/;
|
|
3448
|
+
var MAX_CIPHERTEXT_BYTES = 262144;
|
|
3449
|
+
var ARMOR_BEGIN = "-----BEGIN AGE ENCRYPTED FILE-----";
|
|
3450
|
+
var ARMOR_END = "-----END AGE ENCRYPTED FILE-----";
|
|
3451
|
+
var agePromise = null;
|
|
3452
|
+
async function ensureWebCrypto() {
|
|
3453
|
+
if (globalThis.crypto !== void 0) {
|
|
3454
|
+
return;
|
|
3455
|
+
}
|
|
3456
|
+
const { webcrypto } = await import("crypto");
|
|
3457
|
+
Object.defineProperty(globalThis, "crypto", {
|
|
3458
|
+
value: webcrypto,
|
|
3459
|
+
configurable: true,
|
|
3460
|
+
enumerable: false,
|
|
3461
|
+
writable: false
|
|
3462
|
+
});
|
|
3463
|
+
}
|
|
3464
|
+
async function loadAge() {
|
|
3465
|
+
agePromise ??= (async () => {
|
|
3466
|
+
await ensureWebCrypto();
|
|
3467
|
+
try {
|
|
3468
|
+
return await import("age-encryption");
|
|
3469
|
+
} catch (e) {
|
|
3470
|
+
throw new KaguraCryptoError(
|
|
3471
|
+
`The secret store needs the optional 'age-encryption' package, which is not installed. The base SDK has zero runtime dependencies, so the crypto package is opt-in.
|
|
3472
|
+
Install it with: npm install age-encryption
|
|
3473
|
+
(original error: ${excMessage(e)})`,
|
|
3474
|
+
{ cause: e }
|
|
3475
|
+
);
|
|
3476
|
+
}
|
|
3477
|
+
})().catch((e) => {
|
|
3478
|
+
agePromise = null;
|
|
3479
|
+
throw e;
|
|
3480
|
+
});
|
|
3481
|
+
return agePromise;
|
|
3482
|
+
}
|
|
3483
|
+
async function generateKeypair() {
|
|
3484
|
+
const age = await loadAge();
|
|
3485
|
+
const identity = await age.generateX25519Identity();
|
|
3486
|
+
return { identity, recipient: await age.identityToRecipient(identity) };
|
|
3487
|
+
}
|
|
3488
|
+
async function recipientFromIdentity(identity) {
|
|
3489
|
+
const age = await loadAge();
|
|
3490
|
+
try {
|
|
3491
|
+
return await age.identityToRecipient(identity);
|
|
3492
|
+
} catch (e) {
|
|
3493
|
+
throw identityParseError(e);
|
|
3494
|
+
}
|
|
3495
|
+
}
|
|
3496
|
+
function identityParseError(_cause) {
|
|
3497
|
+
return new KaguraCryptoError(
|
|
3498
|
+
"invalid age identity: the identity string is not a well-formed AGE-SECRET-KEY-1 value. The underlying parser error is withheld because it echoes the input, which would write the private key to logs."
|
|
3499
|
+
);
|
|
3500
|
+
}
|
|
3501
|
+
function fingerprint(pubkey) {
|
|
3502
|
+
return createHash2("sha256").update(pubkey, "utf8").digest("hex");
|
|
3503
|
+
}
|
|
3504
|
+
function armorEncode(binary) {
|
|
3505
|
+
const b64 = Buffer.from(binary).toString("base64");
|
|
3506
|
+
const lines = [];
|
|
3507
|
+
for (let i = 0; i < b64.length; i += 64) {
|
|
3508
|
+
lines.push(b64.slice(i, i + 64));
|
|
3509
|
+
}
|
|
3510
|
+
const body = lines.length > 0 ? `${lines.join("\n")}
|
|
3511
|
+
` : "";
|
|
3512
|
+
return `${ARMOR_BEGIN}
|
|
3513
|
+
${body}${ARMOR_END}
|
|
3514
|
+
`;
|
|
3515
|
+
}
|
|
3516
|
+
function armorDecode(armored) {
|
|
3517
|
+
const lines = armored.trim().split(/\r?\n/).map((line) => line.trim());
|
|
3518
|
+
if (lines.length < 2 || lines[0] !== ARMOR_BEGIN || lines[lines.length - 1] !== ARMOR_END) {
|
|
3519
|
+
throw new KaguraCryptoError("not an armored age file (missing PEM header/footer)");
|
|
3520
|
+
}
|
|
3521
|
+
const body = lines.slice(1, -1).join("");
|
|
3522
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(body) || body.length % 4 !== 0) {
|
|
3523
|
+
throw new KaguraCryptoError("corrupt armored age body: invalid base64");
|
|
3524
|
+
}
|
|
3525
|
+
return new Uint8Array(Buffer.from(body, "base64"));
|
|
3526
|
+
}
|
|
3527
|
+
async function encrypt(plaintext, recipients) {
|
|
3528
|
+
if (recipients.length === 0) {
|
|
3529
|
+
throw new KaguraCryptoError("at least one recipient is required to encrypt");
|
|
3530
|
+
}
|
|
3531
|
+
for (const r of recipients) {
|
|
3532
|
+
if (!RECIPIENT_RE.test(r)) {
|
|
3533
|
+
throw new KaguraCryptoError(`malformed age recipient: ${JSON.stringify(r)}`);
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
const age = await loadAge();
|
|
3537
|
+
let binary;
|
|
3538
|
+
try {
|
|
3539
|
+
const encrypter = new age.Encrypter();
|
|
3540
|
+
for (const r of recipients) {
|
|
3541
|
+
encrypter.addRecipient(r);
|
|
3542
|
+
}
|
|
3543
|
+
binary = await encrypter.encrypt(plaintext);
|
|
3544
|
+
} catch (e) {
|
|
3545
|
+
throw new KaguraCryptoError(`age encryption failed: ${excMessage(e)}`, { cause: e });
|
|
3546
|
+
}
|
|
3547
|
+
const armored = armorEncode(binary);
|
|
3548
|
+
const size = Buffer.byteLength(armored, "ascii");
|
|
3549
|
+
if (size > MAX_CIPHERTEXT_BYTES) {
|
|
3550
|
+
throw new KaguraCryptoError(
|
|
3551
|
+
`ciphertext is ${size} bytes, exceeds the ${MAX_CIPHERTEXT_BYTES}-byte cap`
|
|
3552
|
+
);
|
|
3553
|
+
}
|
|
3554
|
+
return armored;
|
|
3555
|
+
}
|
|
3556
|
+
async function decrypt(armored, identity) {
|
|
3557
|
+
const size = Buffer.byteLength(armored, "utf8");
|
|
3558
|
+
if (size > MAX_CIPHERTEXT_BYTES) {
|
|
3559
|
+
throw new KaguraCryptoError(
|
|
3560
|
+
`ciphertext is ${size} bytes, exceeds the ${MAX_CIPHERTEXT_BYTES}-byte cap`
|
|
3561
|
+
);
|
|
3562
|
+
}
|
|
3563
|
+
const binary = armorDecode(armored);
|
|
3564
|
+
const age = await loadAge();
|
|
3565
|
+
const decrypter = new age.Decrypter();
|
|
3566
|
+
try {
|
|
3567
|
+
decrypter.addIdentity(identity);
|
|
3568
|
+
} catch (e) {
|
|
3569
|
+
throw identityParseError(e);
|
|
3570
|
+
}
|
|
3571
|
+
try {
|
|
3572
|
+
return await decrypter.decrypt(binary);
|
|
3573
|
+
} catch (e) {
|
|
3574
|
+
throw new KaguraCryptoError(`age decryption failed: ${excMessage(e)}`, { cause: e });
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
|
|
3578
|
+
// src/secrets/client.ts
|
|
3579
|
+
var BASE = "/api/v1/config/secrets";
|
|
3580
|
+
var SecretClient = class extends KaguraRestClient {
|
|
3581
|
+
// ---- KaguraRestClient hooks -----------------------------------------
|
|
3582
|
+
/**
|
|
3583
|
+
* 403 → an actionable message instead of a bare `HTTP 403`.
|
|
3584
|
+
*
|
|
3585
|
+
* The server answers 403, not 404, for a secret the caller may not read,
|
|
3586
|
+
* so that the response does not reveal whether the secret exists. That
|
|
3587
|
+
* means a 403 here has three possible causes and the message has to name
|
|
3588
|
+
* all of them rather than guess.
|
|
3589
|
+
*/
|
|
3590
|
+
error403(response, _context) {
|
|
3591
|
+
const detail = extractDetail(response.text);
|
|
3592
|
+
const base = "Access denied (HTTP 403): you may not have a grant on this secret, it may not exist, or you lack permission for this operation.";
|
|
3593
|
+
return new KaguraConnectionError(detail ? `${base} (${detail})` : base);
|
|
3594
|
+
}
|
|
3595
|
+
/**
|
|
3596
|
+
* 429 → the generic `HTTP 429` mapping, not {@link KaguraQuotaError}.
|
|
3597
|
+
*
|
|
3598
|
+
* Deliberate divergence from the base class, preserved from the Python
|
|
3599
|
+
* port: the secret surface has always rendered 429 through the generic
|
|
3600
|
+
* branch, and changing it would silently reclassify errors for existing
|
|
3601
|
+
* callers on a file both SDKs share.
|
|
3602
|
+
*/
|
|
3603
|
+
error429(response) {
|
|
3604
|
+
return this.genericError(response);
|
|
3605
|
+
}
|
|
3606
|
+
// -- pubkey registry ---------------------------------------------------
|
|
3607
|
+
/** Register a public age recipient. Lands in `pending` until an owner approves it. */
|
|
3608
|
+
async registerPubkey(pubkey, label) {
|
|
3609
|
+
const body = { pubkey };
|
|
3610
|
+
if (label !== void 0) {
|
|
3611
|
+
body.label = label;
|
|
3612
|
+
}
|
|
3613
|
+
const response = await this.request("POST", `${BASE}/pubkeys`, { json: body });
|
|
3614
|
+
return this.json(response);
|
|
3615
|
+
}
|
|
3616
|
+
/** List every pubkey in the workspace (owner/admin view). */
|
|
3617
|
+
async listPubkeys() {
|
|
3618
|
+
const response = await this.request("GET", `${BASE}/pubkeys`);
|
|
3619
|
+
return this.expectList(response);
|
|
3620
|
+
}
|
|
3621
|
+
/** List the caller's own pubkeys. */
|
|
3622
|
+
async listMyPubkeys() {
|
|
3623
|
+
const response = await this.request("GET", `${BASE}/pubkeys/me`);
|
|
3624
|
+
return this.expectList(response);
|
|
3625
|
+
}
|
|
3626
|
+
/** Approve a pending pubkey (owner only; TOFU attestation). */
|
|
3627
|
+
async approvePubkey(pubkeyId) {
|
|
3628
|
+
const response = await this.request("POST", `${BASE}/pubkeys/${pubkeyId}/approve`);
|
|
3629
|
+
return this.json(response);
|
|
3630
|
+
}
|
|
3631
|
+
/** Revoke a pubkey (owner only). */
|
|
3632
|
+
async revokePubkey(pubkeyId) {
|
|
3633
|
+
const response = await this.request("POST", `${BASE}/pubkeys/${pubkeyId}/revoke`);
|
|
3634
|
+
return this.json(response);
|
|
3635
|
+
}
|
|
3636
|
+
// -- secrets -----------------------------------------------------------
|
|
3637
|
+
/**
|
|
3638
|
+
* Store a new ciphertext version (low-level).
|
|
3639
|
+
*
|
|
3640
|
+
* The server enforces `set(recipientsSnapshot) === {fingerprint(pk) for pk
|
|
3641
|
+
* in grantPubkeyIds}` and answers 400 on mismatch. Prefer
|
|
3642
|
+
* {@link putSecretForRecipients}, which derives both lists from one
|
|
3643
|
+
* recipient set so they match by construction.
|
|
3644
|
+
*/
|
|
3645
|
+
async putSecret(options) {
|
|
3646
|
+
const response = await this.request("POST", BASE, {
|
|
3647
|
+
json: {
|
|
3648
|
+
name: options.name,
|
|
3649
|
+
ciphertext: options.ciphertext,
|
|
3650
|
+
recipients_snapshot: options.recipientsSnapshot,
|
|
3651
|
+
grant_pubkey_ids: options.grantPubkeyIds
|
|
3652
|
+
}
|
|
3653
|
+
});
|
|
3654
|
+
return this.json(response);
|
|
3655
|
+
}
|
|
3656
|
+
/** List secret metadata. Never includes values. */
|
|
3657
|
+
async listSecrets() {
|
|
3658
|
+
const response = await this.request("GET", BASE);
|
|
3659
|
+
return this.expectList(response);
|
|
3660
|
+
}
|
|
3661
|
+
/**
|
|
3662
|
+
* Fetch a secret's armored ciphertext.
|
|
3663
|
+
*
|
|
3664
|
+
* The name travels in the body, not the path, so names containing `/`
|
|
3665
|
+
* (e.g. `cloudflare/api-token`) need no escaping here.
|
|
3666
|
+
*/
|
|
3667
|
+
async fetchSecret(name, versionNumber) {
|
|
3668
|
+
const body = { name };
|
|
3669
|
+
if (versionNumber !== void 0) {
|
|
3670
|
+
body.version_number = versionNumber;
|
|
3671
|
+
}
|
|
3672
|
+
const response = await this.request("POST", `${BASE}/fetch`, { json: body });
|
|
3673
|
+
return this.json(response);
|
|
3674
|
+
}
|
|
3675
|
+
/**
|
|
3676
|
+
* Revoke one recipient's grant on a secret. Flags `rotation_needed`.
|
|
3677
|
+
*
|
|
3678
|
+
* Revoking does **not** invalidate copies already fetched. To contain a
|
|
3679
|
+
* leak: rotate the upstream credential, then re-encrypt to the remaining
|
|
3680
|
+
* recipients with {@link putSecretForRecipients}.
|
|
3681
|
+
*/
|
|
3682
|
+
async revokeGrant(name, recipientPubkeyId) {
|
|
3683
|
+
const response = await this.request("POST", `${BASE}/revoke-grant`, {
|
|
3684
|
+
json: { name, recipient_pubkey_id: recipientPubkeyId }
|
|
3685
|
+
});
|
|
3686
|
+
return this.json(response);
|
|
3687
|
+
}
|
|
3688
|
+
/**
|
|
3689
|
+
* Hard-delete a secret and all its versions and grants (owner only).
|
|
3690
|
+
*
|
|
3691
|
+
* The server appends a `delete` entry to the tamper-evident audit chain
|
|
3692
|
+
* *before* removal, so {@link verifyAudit} still passes afterwards.
|
|
3693
|
+
*
|
|
3694
|
+
* **Cleanup, not a security control.** Removing the stored ciphertext
|
|
3695
|
+
* neither un-shares a value a recipient already fetched nor rotates the
|
|
3696
|
+
* live upstream credential. To contain a leak, rotate first, then delete.
|
|
3697
|
+
*
|
|
3698
|
+
* @param name Slash-containing names are addressable: each path segment
|
|
3699
|
+
* is percent-encoded individually so the `/` separators stay structural
|
|
3700
|
+
* and the server's `{name:path}` converter still routes on them.
|
|
3701
|
+
*
|
|
3702
|
+
* Dot segments and empty segments are rejected rather than encoded,
|
|
3703
|
+
* because percent-encoding does not neutralize them: `.` and `..` are
|
|
3704
|
+
* RFC 3986 *unreserved*, so `encodeURIComponent` leaves them untouched
|
|
3705
|
+
* and the URL parser in `fetch` then resolves them away. Verified:
|
|
3706
|
+
* `deleteSecret("cloudflare/../openai")` would have issued
|
|
3707
|
+
* `DELETE /api/v1/config/secrets/openai` — deleting a different secret —
|
|
3708
|
+
* and `deleteSecret("..")` would have issued `DELETE /api/v1/config/`.
|
|
3709
|
+
* On a destructive, owner-only operation that is not a risk worth
|
|
3710
|
+
* carrying for a name shape the server cannot address anyway.
|
|
3711
|
+
*
|
|
3712
|
+
* The Python SDK has the same hole (`quote(".", safe="") === "."`) and
|
|
3713
|
+
* wants the same guard; this is a divergence in the safe direction until
|
|
3714
|
+
* it gets one.
|
|
3715
|
+
*
|
|
3716
|
+
* @throws KaguraSecretError if `name` is empty or has an empty, `.`, or
|
|
3717
|
+
* `..` segment.
|
|
3718
|
+
*/
|
|
3719
|
+
async deleteSecret(name) {
|
|
3720
|
+
const segments = name.split("/");
|
|
3721
|
+
const offending = segments.find((s) => s === "" || s === "." || s === "..");
|
|
3722
|
+
if (offending !== void 0) {
|
|
3723
|
+
throw new KaguraSecretError(
|
|
3724
|
+
`refusing to delete secret ${JSON.stringify(name)}: a name may not be empty or contain an empty, '.', or '..' path segment. Such a name would resolve to a different URL than intended and delete the wrong secret.`
|
|
3725
|
+
);
|
|
3726
|
+
}
|
|
3727
|
+
const encoded = segments.map((segment) => encodeURIComponent(segment)).join("/");
|
|
3728
|
+
await this.request("DELETE", `${BASE}/${encoded}`);
|
|
3729
|
+
}
|
|
3730
|
+
/** Verify the tamper-evident audit chain (owner/admin). */
|
|
3731
|
+
async verifyAudit() {
|
|
3732
|
+
const response = await this.request("GET", `${BASE}/audit/verify`);
|
|
3733
|
+
return this.json(response);
|
|
3734
|
+
}
|
|
3735
|
+
// -- high-level orchestration ------------------------------------------
|
|
3736
|
+
/**
|
|
3737
|
+
* Encrypt `plaintext` to `recipients` and store it in one call.
|
|
3738
|
+
*
|
|
3739
|
+
* Enforces the server's grant-consistency invariant client-side, before
|
|
3740
|
+
* the network: every recipient must be `active` and must carry a
|
|
3741
|
+
* fingerprint matching its own pubkey. `recipients_snapshot` and
|
|
3742
|
+
* `grant_pubkey_ids` are then derived 1:1 from the same list, so the two
|
|
3743
|
+
* sets agree by construction rather than by the caller's care.
|
|
3744
|
+
*
|
|
3745
|
+
* The fingerprint check is the one that matters for security: it is what
|
|
3746
|
+
* stops the client from encrypting to a pubkey the server has swapped
|
|
3747
|
+
* under a fingerprint the caller verified out of band.
|
|
3748
|
+
*
|
|
3749
|
+
* Requires the optional `age-encryption` package (see
|
|
3750
|
+
* {@link ./crypto.js | crypto}); every other method on this client works
|
|
3751
|
+
* without it.
|
|
3752
|
+
*
|
|
3753
|
+
* @throws KaguraSecretError on an empty recipient list, a non-active
|
|
3754
|
+
* recipient, or a pubkey/fingerprint mismatch.
|
|
3755
|
+
* @throws KaguraCryptoError if `age-encryption` is missing or encryption
|
|
3756
|
+
* fails.
|
|
3757
|
+
*/
|
|
3758
|
+
async putSecretForRecipients(options) {
|
|
3759
|
+
const { name, recipients } = options;
|
|
3760
|
+
if (recipients.length === 0) {
|
|
3761
|
+
throw new KaguraSecretError("at least one recipient is required to put a secret");
|
|
3762
|
+
}
|
|
3763
|
+
for (const r of recipients) {
|
|
3764
|
+
if (r.status !== "active") {
|
|
3765
|
+
throw new KaguraSecretError(
|
|
3766
|
+
`recipient ${r.pubkey} is not active (status=${JSON.stringify(r.status)}); grants require an owner-approved (active) pubkey`
|
|
3767
|
+
);
|
|
3768
|
+
}
|
|
3769
|
+
if (fingerprint(r.pubkey) !== r.fingerprint) {
|
|
3770
|
+
throw new KaguraSecretError(
|
|
3771
|
+
`pubkey/fingerprint mismatch for recipient ${r.id} \u2014 refusing to encrypt to a pubkey whose advertised fingerprint is inconsistent`
|
|
3772
|
+
);
|
|
3773
|
+
}
|
|
3774
|
+
}
|
|
3775
|
+
const plaintext = typeof options.plaintext === "string" ? new TextEncoder().encode(options.plaintext) : options.plaintext;
|
|
3776
|
+
const ciphertext = await encrypt(
|
|
3777
|
+
plaintext,
|
|
3778
|
+
recipients.map((r) => r.pubkey)
|
|
3779
|
+
);
|
|
3780
|
+
return this.putSecret({
|
|
3781
|
+
name,
|
|
3782
|
+
ciphertext,
|
|
3783
|
+
recipientsSnapshot: recipients.map((r) => r.fingerprint),
|
|
3784
|
+
grantPubkeyIds: recipients.map((r) => r.id)
|
|
3785
|
+
});
|
|
3786
|
+
}
|
|
3787
|
+
};
|
|
3788
|
+
|
|
3789
|
+
// src/secrets/keyManager.ts
|
|
3790
|
+
var KeyManager = class {
|
|
3791
|
+
store;
|
|
3792
|
+
profile;
|
|
3793
|
+
constructor(options) {
|
|
3794
|
+
this.store = options.store;
|
|
3795
|
+
this.profile = options.profile ?? "default";
|
|
3796
|
+
}
|
|
3797
|
+
get keyName() {
|
|
3798
|
+
return `identity:${this.profile}`;
|
|
3799
|
+
}
|
|
3800
|
+
/** True when a private key is already in custody for this profile. */
|
|
3801
|
+
async hasKey() {
|
|
3802
|
+
return await this.store.get(this.keyName) !== null;
|
|
3803
|
+
}
|
|
3804
|
+
/**
|
|
3805
|
+
* Generate a keypair, store the private key, return the public half.
|
|
3806
|
+
*
|
|
3807
|
+
* @throws KaguraKeyCustodyError if a key already exists for this profile —
|
|
3808
|
+
* overwriting it would silently orphan every ciphertext encrypted to the
|
|
3809
|
+
* old recipient.
|
|
3810
|
+
* @throws KaguraCryptoError if `age-encryption` is not installed.
|
|
3811
|
+
*/
|
|
3812
|
+
async enroll() {
|
|
3813
|
+
if (await this.hasKey()) {
|
|
3814
|
+
throw new KaguraKeyCustodyError(
|
|
3815
|
+
`a key already exists for profile ${JSON.stringify(this.profile)}; refusing to overwrite it. Delete it first, or enroll under a different profile.`
|
|
3816
|
+
);
|
|
3817
|
+
}
|
|
3818
|
+
const { identity, recipient } = await generateKeypair();
|
|
3819
|
+
await this.store.set(this.keyName, identity);
|
|
3820
|
+
return { recipient, fingerprint: fingerprint(recipient) };
|
|
3821
|
+
}
|
|
3822
|
+
/**
|
|
3823
|
+
* Return the custodied private key.
|
|
3824
|
+
*
|
|
3825
|
+
* @throws KaguraKeyCustodyError if no key is enrolled for this profile.
|
|
3826
|
+
*/
|
|
3827
|
+
async getIdentity() {
|
|
3828
|
+
const identity = await this.store.get(this.keyName);
|
|
3829
|
+
if (identity === null) {
|
|
3830
|
+
throw new KaguraKeyCustodyError(
|
|
3831
|
+
`no age key in custody for profile ${JSON.stringify(this.profile)}; enroll one first.`
|
|
3832
|
+
);
|
|
3833
|
+
}
|
|
3834
|
+
return identity;
|
|
3835
|
+
}
|
|
3836
|
+
/** Return the public `age1` recipient derived from the custodied key. */
|
|
3837
|
+
async getRecipient() {
|
|
3838
|
+
return recipientFromIdentity(await this.getIdentity());
|
|
3839
|
+
}
|
|
3840
|
+
/** Return the sha256 fingerprint of this profile's public recipient. */
|
|
3841
|
+
async fingerprint() {
|
|
3842
|
+
return fingerprint(await this.getRecipient());
|
|
3843
|
+
}
|
|
3844
|
+
/** Remove this profile's private key from custody. Idempotent. */
|
|
3845
|
+
async delete() {
|
|
3846
|
+
await this.store.delete(this.keyName);
|
|
3847
|
+
}
|
|
3848
|
+
};
|
|
3849
|
+
|
|
3407
3850
|
// src/auth/login.ts
|
|
3408
3851
|
var DEFAULT_SCOPE = "memory:read memory:write";
|
|
3409
3852
|
var READ_ONLY_SCOPE = "memory:read";
|
|
@@ -3535,37 +3978,51 @@ export {
|
|
|
3535
3978
|
KaguraClient,
|
|
3536
3979
|
KaguraConnectionError,
|
|
3537
3980
|
KaguraContextError,
|
|
3981
|
+
KaguraCryptoError,
|
|
3538
3982
|
KaguraError,
|
|
3539
3983
|
KaguraFetchError,
|
|
3540
3984
|
KaguraIngestError,
|
|
3541
3985
|
KaguraIntegrityError,
|
|
3986
|
+
KaguraKeyCustodyError,
|
|
3542
3987
|
KaguraLLMError,
|
|
3543
3988
|
KaguraNotFoundError,
|
|
3544
3989
|
KaguraOAuth,
|
|
3545
3990
|
KaguraQuotaError,
|
|
3546
3991
|
KaguraRateLimitError,
|
|
3547
3992
|
KaguraRestClient,
|
|
3993
|
+
KaguraSecretError,
|
|
3994
|
+
KeyManager,
|
|
3995
|
+
MAX_CIPHERTEXT_BYTES,
|
|
3548
3996
|
MIN_SERVER_VERSION,
|
|
3549
3997
|
READ_ONLY_SCOPE,
|
|
3998
|
+
RECIPIENT_RE,
|
|
3550
3999
|
REFRESH_SKEW_SEC,
|
|
3551
4000
|
REFRESH_TOKEN_GRANT_TYPE,
|
|
3552
4001
|
ResourceClient,
|
|
3553
4002
|
SDK_VERSION,
|
|
3554
4003
|
SETUP_OAUTH_NOT_SUPPORTED_MSG,
|
|
4004
|
+
SecretClient,
|
|
3555
4005
|
VALID_ASSIGNABLE_ROLES,
|
|
3556
4006
|
VALID_INVITE_EXPIRES,
|
|
3557
4007
|
WorkspaceClient,
|
|
4008
|
+
armorDecode,
|
|
4009
|
+
armorEncode,
|
|
3558
4010
|
authorizeDevice,
|
|
4011
|
+
decrypt,
|
|
3559
4012
|
defaultCredentialsPath,
|
|
3560
4013
|
deleteCredentialsFile,
|
|
3561
4014
|
deleteProfile,
|
|
3562
4015
|
emptyCredentialsFile,
|
|
4016
|
+
encrypt,
|
|
4017
|
+
fingerprint,
|
|
4018
|
+
generateKeypair,
|
|
3563
4019
|
getProfile,
|
|
3564
4020
|
isExpired,
|
|
3565
4021
|
loadConfig,
|
|
3566
4022
|
loadCredentialsFile,
|
|
3567
4023
|
login,
|
|
3568
4024
|
pollForToken,
|
|
4025
|
+
recipientFromIdentity,
|
|
3569
4026
|
refresh,
|
|
3570
4027
|
refreshAccessToken,
|
|
3571
4028
|
removeProfile,
|