kagura-memory 0.5.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 +107 -4
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +475 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +482 -2
- package/dist/index.d.ts +482 -2
- package/dist/index.js +461 -1
- package/dist/index.js.map +1 -1
- package/package.json +12 -2
package/dist/index.cjs
CHANGED
|
@@ -43,37 +43,51 @@ __export(index_exports, {
|
|
|
43
43
|
KaguraClient: () => KaguraClient,
|
|
44
44
|
KaguraConnectionError: () => KaguraConnectionError,
|
|
45
45
|
KaguraContextError: () => KaguraContextError,
|
|
46
|
+
KaguraCryptoError: () => KaguraCryptoError,
|
|
46
47
|
KaguraError: () => KaguraError,
|
|
47
48
|
KaguraFetchError: () => KaguraFetchError,
|
|
48
49
|
KaguraIngestError: () => KaguraIngestError,
|
|
49
50
|
KaguraIntegrityError: () => KaguraIntegrityError,
|
|
51
|
+
KaguraKeyCustodyError: () => KaguraKeyCustodyError,
|
|
50
52
|
KaguraLLMError: () => KaguraLLMError,
|
|
51
53
|
KaguraNotFoundError: () => KaguraNotFoundError,
|
|
52
54
|
KaguraOAuth: () => KaguraOAuth,
|
|
53
55
|
KaguraQuotaError: () => KaguraQuotaError,
|
|
54
56
|
KaguraRateLimitError: () => KaguraRateLimitError,
|
|
55
57
|
KaguraRestClient: () => KaguraRestClient,
|
|
58
|
+
KaguraSecretError: () => KaguraSecretError,
|
|
59
|
+
KeyManager: () => KeyManager,
|
|
60
|
+
MAX_CIPHERTEXT_BYTES: () => MAX_CIPHERTEXT_BYTES,
|
|
56
61
|
MIN_SERVER_VERSION: () => MIN_SERVER_VERSION,
|
|
57
62
|
READ_ONLY_SCOPE: () => READ_ONLY_SCOPE,
|
|
63
|
+
RECIPIENT_RE: () => RECIPIENT_RE,
|
|
58
64
|
REFRESH_SKEW_SEC: () => REFRESH_SKEW_SEC,
|
|
59
65
|
REFRESH_TOKEN_GRANT_TYPE: () => REFRESH_TOKEN_GRANT_TYPE,
|
|
60
66
|
ResourceClient: () => ResourceClient,
|
|
61
67
|
SDK_VERSION: () => SDK_VERSION,
|
|
62
68
|
SETUP_OAUTH_NOT_SUPPORTED_MSG: () => SETUP_OAUTH_NOT_SUPPORTED_MSG,
|
|
69
|
+
SecretClient: () => SecretClient,
|
|
63
70
|
VALID_ASSIGNABLE_ROLES: () => VALID_ASSIGNABLE_ROLES,
|
|
64
71
|
VALID_INVITE_EXPIRES: () => VALID_INVITE_EXPIRES,
|
|
65
72
|
WorkspaceClient: () => WorkspaceClient,
|
|
73
|
+
armorDecode: () => armorDecode,
|
|
74
|
+
armorEncode: () => armorEncode,
|
|
66
75
|
authorizeDevice: () => authorizeDevice,
|
|
76
|
+
decrypt: () => decrypt,
|
|
67
77
|
defaultCredentialsPath: () => defaultCredentialsPath,
|
|
68
78
|
deleteCredentialsFile: () => deleteCredentialsFile,
|
|
69
79
|
deleteProfile: () => deleteProfile,
|
|
70
80
|
emptyCredentialsFile: () => emptyCredentialsFile,
|
|
81
|
+
encrypt: () => encrypt,
|
|
82
|
+
fingerprint: () => fingerprint,
|
|
83
|
+
generateKeypair: () => generateKeypair,
|
|
71
84
|
getProfile: () => getProfile,
|
|
72
85
|
isExpired: () => isExpired,
|
|
73
86
|
loadConfig: () => loadConfig,
|
|
74
87
|
loadCredentialsFile: () => loadCredentialsFile,
|
|
75
88
|
login: () => login,
|
|
76
89
|
pollForToken: () => pollForToken,
|
|
90
|
+
recipientFromIdentity: () => recipientFromIdentity,
|
|
77
91
|
refresh: () => refresh,
|
|
78
92
|
refreshAccessToken: () => refreshAccessToken,
|
|
79
93
|
removeProfile: () => removeProfile,
|
|
@@ -215,6 +229,12 @@ var KaguraFetchError = class extends KaguraError {
|
|
|
215
229
|
};
|
|
216
230
|
var KaguraIngestError = class extends KaguraError {
|
|
217
231
|
};
|
|
232
|
+
var KaguraSecretError = class extends KaguraError {
|
|
233
|
+
};
|
|
234
|
+
var KaguraCryptoError = class extends KaguraSecretError {
|
|
235
|
+
};
|
|
236
|
+
var KaguraKeyCustodyError = class extends KaguraSecretError {
|
|
237
|
+
};
|
|
218
238
|
|
|
219
239
|
// src/auth/credentials.ts
|
|
220
240
|
var crypto = __toESM(require("crypto"), 1);
|
|
@@ -226,7 +246,7 @@ var path3 = __toESM(require("path"), 1);
|
|
|
226
246
|
var import_promises = require("timers/promises");
|
|
227
247
|
|
|
228
248
|
// src/version.ts
|
|
229
|
-
var SDK_VERSION = "0.
|
|
249
|
+
var SDK_VERSION = "0.7.0";
|
|
230
250
|
|
|
231
251
|
// src/http.ts
|
|
232
252
|
function baseUrlFromMcp(mcpUrl) {
|
|
@@ -1350,6 +1370,38 @@ var KaguraClient = class _KaguraClient {
|
|
|
1350
1370
|
_KaguraClient.raiseForMcpError(result, toolName);
|
|
1351
1371
|
return result;
|
|
1352
1372
|
}
|
|
1373
|
+
/**
|
|
1374
|
+
* Call any MCP tool by name — the escape hatch for tools this SDK has no
|
|
1375
|
+
* typed wrapper for yet (#28).
|
|
1376
|
+
*
|
|
1377
|
+
* Everything above is a typed wrapper over a specific tool, and that is
|
|
1378
|
+
* still the surface to prefer: wrappers validate arguments locally, map
|
|
1379
|
+
* camelCase to the wire's snake_case, and give you a return type. But
|
|
1380
|
+
* `callTool` is private, so before this existed one forgotten wrapper left
|
|
1381
|
+
* a tool completely unreachable — a caller had to vendor a patched SDK or
|
|
1382
|
+
* hand-roll JSON-RPC. `secret_*` was exactly that case.
|
|
1383
|
+
*
|
|
1384
|
+
* Args are passed through **verbatim**, so they must already be in wire
|
|
1385
|
+
* form (`context_id`, not `contextId`), and the result is an untyped
|
|
1386
|
+
* `ToolResult`.
|
|
1387
|
+
*
|
|
1388
|
+
* Domain errors are still translated, so a server `{"status": "error"}`
|
|
1389
|
+
* throws rather than coming back as data — but only error codes the SDK
|
|
1390
|
+
* recognizes get a specific class. A code from a tool with no wrapper
|
|
1391
|
+
* (`secret_not_found`, say) lands on the generic {@link KaguraError}, so
|
|
1392
|
+
* match on the message or add the code to `raiseForMcpError` when you
|
|
1393
|
+
* confirm it against the server.
|
|
1394
|
+
*
|
|
1395
|
+
* Use `getToolDefinitions()` to discover what the connected server offers.
|
|
1396
|
+
*
|
|
1397
|
+
* @throws Error if `toolName` is empty or whitespace.
|
|
1398
|
+
*/
|
|
1399
|
+
async callRawTool(toolName, args = {}) {
|
|
1400
|
+
if (typeof toolName !== "string" || !toolName.trim()) {
|
|
1401
|
+
throw new Error("toolName must be a non-empty string");
|
|
1402
|
+
}
|
|
1403
|
+
return this.callToolChecked(toolName, args);
|
|
1404
|
+
}
|
|
1353
1405
|
/**
|
|
1354
1406
|
* Translate an MCP tool's structured error response to an SDK error.
|
|
1355
1407
|
*
|
|
@@ -1452,6 +1504,9 @@ var KaguraClient = class _KaguraClient {
|
|
|
1452
1504
|
if (options.includeExploreHints) {
|
|
1453
1505
|
args.include_explore_hints = true;
|
|
1454
1506
|
}
|
|
1507
|
+
if (options.includeSuperseded) {
|
|
1508
|
+
args.include_superseded = true;
|
|
1509
|
+
}
|
|
1455
1510
|
return this.callToolChecked("recall", args);
|
|
1456
1511
|
}
|
|
1457
1512
|
/**
|
|
@@ -3490,6 +3545,411 @@ function isMintShape(payload) {
|
|
|
3490
3545
|
return typeof rec.id === "number" && typeof rec.name === "string" && typeof rec.key_prefix === "string";
|
|
3491
3546
|
}
|
|
3492
3547
|
|
|
3548
|
+
// src/secrets/crypto.ts
|
|
3549
|
+
var import_node_crypto2 = require("crypto");
|
|
3550
|
+
var RECIPIENT_RE = /^age1[02-9ac-hj-np-z]{20,110}$/;
|
|
3551
|
+
var MAX_CIPHERTEXT_BYTES = 262144;
|
|
3552
|
+
var ARMOR_BEGIN = "-----BEGIN AGE ENCRYPTED FILE-----";
|
|
3553
|
+
var ARMOR_END = "-----END AGE ENCRYPTED FILE-----";
|
|
3554
|
+
var agePromise = null;
|
|
3555
|
+
async function ensureWebCrypto() {
|
|
3556
|
+
if (globalThis.crypto !== void 0) {
|
|
3557
|
+
return;
|
|
3558
|
+
}
|
|
3559
|
+
const { webcrypto } = await import("crypto");
|
|
3560
|
+
Object.defineProperty(globalThis, "crypto", {
|
|
3561
|
+
value: webcrypto,
|
|
3562
|
+
configurable: true,
|
|
3563
|
+
enumerable: false,
|
|
3564
|
+
writable: false
|
|
3565
|
+
});
|
|
3566
|
+
}
|
|
3567
|
+
async function loadAge() {
|
|
3568
|
+
agePromise ??= (async () => {
|
|
3569
|
+
await ensureWebCrypto();
|
|
3570
|
+
try {
|
|
3571
|
+
return await import("age-encryption");
|
|
3572
|
+
} catch (e) {
|
|
3573
|
+
throw new KaguraCryptoError(
|
|
3574
|
+
`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.
|
|
3575
|
+
Install it with: npm install age-encryption
|
|
3576
|
+
(original error: ${excMessage(e)})`,
|
|
3577
|
+
{ cause: e }
|
|
3578
|
+
);
|
|
3579
|
+
}
|
|
3580
|
+
})().catch((e) => {
|
|
3581
|
+
agePromise = null;
|
|
3582
|
+
throw e;
|
|
3583
|
+
});
|
|
3584
|
+
return agePromise;
|
|
3585
|
+
}
|
|
3586
|
+
async function generateKeypair() {
|
|
3587
|
+
const age = await loadAge();
|
|
3588
|
+
const identity = await age.generateX25519Identity();
|
|
3589
|
+
return { identity, recipient: await age.identityToRecipient(identity) };
|
|
3590
|
+
}
|
|
3591
|
+
async function recipientFromIdentity(identity) {
|
|
3592
|
+
const age = await loadAge();
|
|
3593
|
+
try {
|
|
3594
|
+
return await age.identityToRecipient(identity);
|
|
3595
|
+
} catch (e) {
|
|
3596
|
+
throw identityParseError(e);
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
function identityParseError(_cause) {
|
|
3600
|
+
return new KaguraCryptoError(
|
|
3601
|
+
"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."
|
|
3602
|
+
);
|
|
3603
|
+
}
|
|
3604
|
+
function fingerprint(pubkey) {
|
|
3605
|
+
return (0, import_node_crypto2.createHash)("sha256").update(pubkey, "utf8").digest("hex");
|
|
3606
|
+
}
|
|
3607
|
+
function armorEncode(binary) {
|
|
3608
|
+
const b64 = Buffer.from(binary).toString("base64");
|
|
3609
|
+
const lines = [];
|
|
3610
|
+
for (let i = 0; i < b64.length; i += 64) {
|
|
3611
|
+
lines.push(b64.slice(i, i + 64));
|
|
3612
|
+
}
|
|
3613
|
+
const body = lines.length > 0 ? `${lines.join("\n")}
|
|
3614
|
+
` : "";
|
|
3615
|
+
return `${ARMOR_BEGIN}
|
|
3616
|
+
${body}${ARMOR_END}
|
|
3617
|
+
`;
|
|
3618
|
+
}
|
|
3619
|
+
function armorDecode(armored) {
|
|
3620
|
+
const lines = armored.trim().split(/\r?\n/).map((line) => line.trim());
|
|
3621
|
+
if (lines.length < 2 || lines[0] !== ARMOR_BEGIN || lines[lines.length - 1] !== ARMOR_END) {
|
|
3622
|
+
throw new KaguraCryptoError("not an armored age file (missing PEM header/footer)");
|
|
3623
|
+
}
|
|
3624
|
+
const body = lines.slice(1, -1).join("");
|
|
3625
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(body) || body.length % 4 !== 0) {
|
|
3626
|
+
throw new KaguraCryptoError("corrupt armored age body: invalid base64");
|
|
3627
|
+
}
|
|
3628
|
+
return new Uint8Array(Buffer.from(body, "base64"));
|
|
3629
|
+
}
|
|
3630
|
+
async function encrypt(plaintext, recipients) {
|
|
3631
|
+
if (recipients.length === 0) {
|
|
3632
|
+
throw new KaguraCryptoError("at least one recipient is required to encrypt");
|
|
3633
|
+
}
|
|
3634
|
+
for (const r of recipients) {
|
|
3635
|
+
if (!RECIPIENT_RE.test(r)) {
|
|
3636
|
+
throw new KaguraCryptoError(`malformed age recipient: ${JSON.stringify(r)}`);
|
|
3637
|
+
}
|
|
3638
|
+
}
|
|
3639
|
+
const age = await loadAge();
|
|
3640
|
+
let binary;
|
|
3641
|
+
try {
|
|
3642
|
+
const encrypter = new age.Encrypter();
|
|
3643
|
+
for (const r of recipients) {
|
|
3644
|
+
encrypter.addRecipient(r);
|
|
3645
|
+
}
|
|
3646
|
+
binary = await encrypter.encrypt(plaintext);
|
|
3647
|
+
} catch (e) {
|
|
3648
|
+
throw new KaguraCryptoError(`age encryption failed: ${excMessage(e)}`, { cause: e });
|
|
3649
|
+
}
|
|
3650
|
+
const armored = armorEncode(binary);
|
|
3651
|
+
const size = Buffer.byteLength(armored, "ascii");
|
|
3652
|
+
if (size > MAX_CIPHERTEXT_BYTES) {
|
|
3653
|
+
throw new KaguraCryptoError(
|
|
3654
|
+
`ciphertext is ${size} bytes, exceeds the ${MAX_CIPHERTEXT_BYTES}-byte cap`
|
|
3655
|
+
);
|
|
3656
|
+
}
|
|
3657
|
+
return armored;
|
|
3658
|
+
}
|
|
3659
|
+
async function decrypt(armored, identity) {
|
|
3660
|
+
const size = Buffer.byteLength(armored, "utf8");
|
|
3661
|
+
if (size > MAX_CIPHERTEXT_BYTES) {
|
|
3662
|
+
throw new KaguraCryptoError(
|
|
3663
|
+
`ciphertext is ${size} bytes, exceeds the ${MAX_CIPHERTEXT_BYTES}-byte cap`
|
|
3664
|
+
);
|
|
3665
|
+
}
|
|
3666
|
+
const binary = armorDecode(armored);
|
|
3667
|
+
const age = await loadAge();
|
|
3668
|
+
const decrypter = new age.Decrypter();
|
|
3669
|
+
try {
|
|
3670
|
+
decrypter.addIdentity(identity);
|
|
3671
|
+
} catch (e) {
|
|
3672
|
+
throw identityParseError(e);
|
|
3673
|
+
}
|
|
3674
|
+
try {
|
|
3675
|
+
return await decrypter.decrypt(binary);
|
|
3676
|
+
} catch (e) {
|
|
3677
|
+
throw new KaguraCryptoError(`age decryption failed: ${excMessage(e)}`, { cause: e });
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
|
|
3681
|
+
// src/secrets/client.ts
|
|
3682
|
+
var BASE = "/api/v1/config/secrets";
|
|
3683
|
+
var SecretClient = class extends KaguraRestClient {
|
|
3684
|
+
// ---- KaguraRestClient hooks -----------------------------------------
|
|
3685
|
+
/**
|
|
3686
|
+
* 403 → an actionable message instead of a bare `HTTP 403`.
|
|
3687
|
+
*
|
|
3688
|
+
* The server answers 403, not 404, for a secret the caller may not read,
|
|
3689
|
+
* so that the response does not reveal whether the secret exists. That
|
|
3690
|
+
* means a 403 here has three possible causes and the message has to name
|
|
3691
|
+
* all of them rather than guess.
|
|
3692
|
+
*/
|
|
3693
|
+
error403(response, _context) {
|
|
3694
|
+
const detail = extractDetail(response.text);
|
|
3695
|
+
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.";
|
|
3696
|
+
return new KaguraConnectionError(detail ? `${base} (${detail})` : base);
|
|
3697
|
+
}
|
|
3698
|
+
/**
|
|
3699
|
+
* 429 → the generic `HTTP 429` mapping, not {@link KaguraQuotaError}.
|
|
3700
|
+
*
|
|
3701
|
+
* Deliberate divergence from the base class, preserved from the Python
|
|
3702
|
+
* port: the secret surface has always rendered 429 through the generic
|
|
3703
|
+
* branch, and changing it would silently reclassify errors for existing
|
|
3704
|
+
* callers on a file both SDKs share.
|
|
3705
|
+
*/
|
|
3706
|
+
error429(response) {
|
|
3707
|
+
return this.genericError(response);
|
|
3708
|
+
}
|
|
3709
|
+
// -- pubkey registry ---------------------------------------------------
|
|
3710
|
+
/** Register a public age recipient. Lands in `pending` until an owner approves it. */
|
|
3711
|
+
async registerPubkey(pubkey, label) {
|
|
3712
|
+
const body = { pubkey };
|
|
3713
|
+
if (label !== void 0) {
|
|
3714
|
+
body.label = label;
|
|
3715
|
+
}
|
|
3716
|
+
const response = await this.request("POST", `${BASE}/pubkeys`, { json: body });
|
|
3717
|
+
return this.json(response);
|
|
3718
|
+
}
|
|
3719
|
+
/** List every pubkey in the workspace (owner/admin view). */
|
|
3720
|
+
async listPubkeys() {
|
|
3721
|
+
const response = await this.request("GET", `${BASE}/pubkeys`);
|
|
3722
|
+
return this.expectList(response);
|
|
3723
|
+
}
|
|
3724
|
+
/** List the caller's own pubkeys. */
|
|
3725
|
+
async listMyPubkeys() {
|
|
3726
|
+
const response = await this.request("GET", `${BASE}/pubkeys/me`);
|
|
3727
|
+
return this.expectList(response);
|
|
3728
|
+
}
|
|
3729
|
+
/** Approve a pending pubkey (owner only; TOFU attestation). */
|
|
3730
|
+
async approvePubkey(pubkeyId) {
|
|
3731
|
+
const response = await this.request("POST", `${BASE}/pubkeys/${pubkeyId}/approve`);
|
|
3732
|
+
return this.json(response);
|
|
3733
|
+
}
|
|
3734
|
+
/** Revoke a pubkey (owner only). */
|
|
3735
|
+
async revokePubkey(pubkeyId) {
|
|
3736
|
+
const response = await this.request("POST", `${BASE}/pubkeys/${pubkeyId}/revoke`);
|
|
3737
|
+
return this.json(response);
|
|
3738
|
+
}
|
|
3739
|
+
// -- secrets -----------------------------------------------------------
|
|
3740
|
+
/**
|
|
3741
|
+
* Store a new ciphertext version (low-level).
|
|
3742
|
+
*
|
|
3743
|
+
* The server enforces `set(recipientsSnapshot) === {fingerprint(pk) for pk
|
|
3744
|
+
* in grantPubkeyIds}` and answers 400 on mismatch. Prefer
|
|
3745
|
+
* {@link putSecretForRecipients}, which derives both lists from one
|
|
3746
|
+
* recipient set so they match by construction.
|
|
3747
|
+
*/
|
|
3748
|
+
async putSecret(options) {
|
|
3749
|
+
const response = await this.request("POST", BASE, {
|
|
3750
|
+
json: {
|
|
3751
|
+
name: options.name,
|
|
3752
|
+
ciphertext: options.ciphertext,
|
|
3753
|
+
recipients_snapshot: options.recipientsSnapshot,
|
|
3754
|
+
grant_pubkey_ids: options.grantPubkeyIds
|
|
3755
|
+
}
|
|
3756
|
+
});
|
|
3757
|
+
return this.json(response);
|
|
3758
|
+
}
|
|
3759
|
+
/** List secret metadata. Never includes values. */
|
|
3760
|
+
async listSecrets() {
|
|
3761
|
+
const response = await this.request("GET", BASE);
|
|
3762
|
+
return this.expectList(response);
|
|
3763
|
+
}
|
|
3764
|
+
/**
|
|
3765
|
+
* Fetch a secret's armored ciphertext.
|
|
3766
|
+
*
|
|
3767
|
+
* The name travels in the body, not the path, so names containing `/`
|
|
3768
|
+
* (e.g. `cloudflare/api-token`) need no escaping here.
|
|
3769
|
+
*/
|
|
3770
|
+
async fetchSecret(name, versionNumber) {
|
|
3771
|
+
const body = { name };
|
|
3772
|
+
if (versionNumber !== void 0) {
|
|
3773
|
+
body.version_number = versionNumber;
|
|
3774
|
+
}
|
|
3775
|
+
const response = await this.request("POST", `${BASE}/fetch`, { json: body });
|
|
3776
|
+
return this.json(response);
|
|
3777
|
+
}
|
|
3778
|
+
/**
|
|
3779
|
+
* Revoke one recipient's grant on a secret. Flags `rotation_needed`.
|
|
3780
|
+
*
|
|
3781
|
+
* Revoking does **not** invalidate copies already fetched. To contain a
|
|
3782
|
+
* leak: rotate the upstream credential, then re-encrypt to the remaining
|
|
3783
|
+
* recipients with {@link putSecretForRecipients}.
|
|
3784
|
+
*/
|
|
3785
|
+
async revokeGrant(name, recipientPubkeyId) {
|
|
3786
|
+
const response = await this.request("POST", `${BASE}/revoke-grant`, {
|
|
3787
|
+
json: { name, recipient_pubkey_id: recipientPubkeyId }
|
|
3788
|
+
});
|
|
3789
|
+
return this.json(response);
|
|
3790
|
+
}
|
|
3791
|
+
/**
|
|
3792
|
+
* Hard-delete a secret and all its versions and grants (owner only).
|
|
3793
|
+
*
|
|
3794
|
+
* The server appends a `delete` entry to the tamper-evident audit chain
|
|
3795
|
+
* *before* removal, so {@link verifyAudit} still passes afterwards.
|
|
3796
|
+
*
|
|
3797
|
+
* **Cleanup, not a security control.** Removing the stored ciphertext
|
|
3798
|
+
* neither un-shares a value a recipient already fetched nor rotates the
|
|
3799
|
+
* live upstream credential. To contain a leak, rotate first, then delete.
|
|
3800
|
+
*
|
|
3801
|
+
* @param name Slash-containing names are addressable: each path segment
|
|
3802
|
+
* is percent-encoded individually so the `/` separators stay structural
|
|
3803
|
+
* and the server's `{name:path}` converter still routes on them.
|
|
3804
|
+
*
|
|
3805
|
+
* Dot segments and empty segments are rejected rather than encoded,
|
|
3806
|
+
* because percent-encoding does not neutralize them: `.` and `..` are
|
|
3807
|
+
* RFC 3986 *unreserved*, so `encodeURIComponent` leaves them untouched
|
|
3808
|
+
* and the URL parser in `fetch` then resolves them away. Verified:
|
|
3809
|
+
* `deleteSecret("cloudflare/../openai")` would have issued
|
|
3810
|
+
* `DELETE /api/v1/config/secrets/openai` — deleting a different secret —
|
|
3811
|
+
* and `deleteSecret("..")` would have issued `DELETE /api/v1/config/`.
|
|
3812
|
+
* On a destructive, owner-only operation that is not a risk worth
|
|
3813
|
+
* carrying for a name shape the server cannot address anyway.
|
|
3814
|
+
*
|
|
3815
|
+
* The Python SDK has the same hole (`quote(".", safe="") === "."`) and
|
|
3816
|
+
* wants the same guard; this is a divergence in the safe direction until
|
|
3817
|
+
* it gets one.
|
|
3818
|
+
*
|
|
3819
|
+
* @throws KaguraSecretError if `name` is empty or has an empty, `.`, or
|
|
3820
|
+
* `..` segment.
|
|
3821
|
+
*/
|
|
3822
|
+
async deleteSecret(name) {
|
|
3823
|
+
const segments = name.split("/");
|
|
3824
|
+
const offending = segments.find((s) => s === "" || s === "." || s === "..");
|
|
3825
|
+
if (offending !== void 0) {
|
|
3826
|
+
throw new KaguraSecretError(
|
|
3827
|
+
`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.`
|
|
3828
|
+
);
|
|
3829
|
+
}
|
|
3830
|
+
const encoded = segments.map((segment) => encodeURIComponent(segment)).join("/");
|
|
3831
|
+
await this.request("DELETE", `${BASE}/${encoded}`);
|
|
3832
|
+
}
|
|
3833
|
+
/** Verify the tamper-evident audit chain (owner/admin). */
|
|
3834
|
+
async verifyAudit() {
|
|
3835
|
+
const response = await this.request("GET", `${BASE}/audit/verify`);
|
|
3836
|
+
return this.json(response);
|
|
3837
|
+
}
|
|
3838
|
+
// -- high-level orchestration ------------------------------------------
|
|
3839
|
+
/**
|
|
3840
|
+
* Encrypt `plaintext` to `recipients` and store it in one call.
|
|
3841
|
+
*
|
|
3842
|
+
* Enforces the server's grant-consistency invariant client-side, before
|
|
3843
|
+
* the network: every recipient must be `active` and must carry a
|
|
3844
|
+
* fingerprint matching its own pubkey. `recipients_snapshot` and
|
|
3845
|
+
* `grant_pubkey_ids` are then derived 1:1 from the same list, so the two
|
|
3846
|
+
* sets agree by construction rather than by the caller's care.
|
|
3847
|
+
*
|
|
3848
|
+
* The fingerprint check is the one that matters for security: it is what
|
|
3849
|
+
* stops the client from encrypting to a pubkey the server has swapped
|
|
3850
|
+
* under a fingerprint the caller verified out of band.
|
|
3851
|
+
*
|
|
3852
|
+
* Requires the optional `age-encryption` package (see
|
|
3853
|
+
* {@link ./crypto.js | crypto}); every other method on this client works
|
|
3854
|
+
* without it.
|
|
3855
|
+
*
|
|
3856
|
+
* @throws KaguraSecretError on an empty recipient list, a non-active
|
|
3857
|
+
* recipient, or a pubkey/fingerprint mismatch.
|
|
3858
|
+
* @throws KaguraCryptoError if `age-encryption` is missing or encryption
|
|
3859
|
+
* fails.
|
|
3860
|
+
*/
|
|
3861
|
+
async putSecretForRecipients(options) {
|
|
3862
|
+
const { name, recipients } = options;
|
|
3863
|
+
if (recipients.length === 0) {
|
|
3864
|
+
throw new KaguraSecretError("at least one recipient is required to put a secret");
|
|
3865
|
+
}
|
|
3866
|
+
for (const r of recipients) {
|
|
3867
|
+
if (r.status !== "active") {
|
|
3868
|
+
throw new KaguraSecretError(
|
|
3869
|
+
`recipient ${r.pubkey} is not active (status=${JSON.stringify(r.status)}); grants require an owner-approved (active) pubkey`
|
|
3870
|
+
);
|
|
3871
|
+
}
|
|
3872
|
+
if (fingerprint(r.pubkey) !== r.fingerprint) {
|
|
3873
|
+
throw new KaguraSecretError(
|
|
3874
|
+
`pubkey/fingerprint mismatch for recipient ${r.id} \u2014 refusing to encrypt to a pubkey whose advertised fingerprint is inconsistent`
|
|
3875
|
+
);
|
|
3876
|
+
}
|
|
3877
|
+
}
|
|
3878
|
+
const plaintext = typeof options.plaintext === "string" ? new TextEncoder().encode(options.plaintext) : options.plaintext;
|
|
3879
|
+
const ciphertext = await encrypt(
|
|
3880
|
+
plaintext,
|
|
3881
|
+
recipients.map((r) => r.pubkey)
|
|
3882
|
+
);
|
|
3883
|
+
return this.putSecret({
|
|
3884
|
+
name,
|
|
3885
|
+
ciphertext,
|
|
3886
|
+
recipientsSnapshot: recipients.map((r) => r.fingerprint),
|
|
3887
|
+
grantPubkeyIds: recipients.map((r) => r.id)
|
|
3888
|
+
});
|
|
3889
|
+
}
|
|
3890
|
+
};
|
|
3891
|
+
|
|
3892
|
+
// src/secrets/keyManager.ts
|
|
3893
|
+
var KeyManager = class {
|
|
3894
|
+
store;
|
|
3895
|
+
profile;
|
|
3896
|
+
constructor(options) {
|
|
3897
|
+
this.store = options.store;
|
|
3898
|
+
this.profile = options.profile ?? "default";
|
|
3899
|
+
}
|
|
3900
|
+
get keyName() {
|
|
3901
|
+
return `identity:${this.profile}`;
|
|
3902
|
+
}
|
|
3903
|
+
/** True when a private key is already in custody for this profile. */
|
|
3904
|
+
async hasKey() {
|
|
3905
|
+
return await this.store.get(this.keyName) !== null;
|
|
3906
|
+
}
|
|
3907
|
+
/**
|
|
3908
|
+
* Generate a keypair, store the private key, return the public half.
|
|
3909
|
+
*
|
|
3910
|
+
* @throws KaguraKeyCustodyError if a key already exists for this profile —
|
|
3911
|
+
* overwriting it would silently orphan every ciphertext encrypted to the
|
|
3912
|
+
* old recipient.
|
|
3913
|
+
* @throws KaguraCryptoError if `age-encryption` is not installed.
|
|
3914
|
+
*/
|
|
3915
|
+
async enroll() {
|
|
3916
|
+
if (await this.hasKey()) {
|
|
3917
|
+
throw new KaguraKeyCustodyError(
|
|
3918
|
+
`a key already exists for profile ${JSON.stringify(this.profile)}; refusing to overwrite it. Delete it first, or enroll under a different profile.`
|
|
3919
|
+
);
|
|
3920
|
+
}
|
|
3921
|
+
const { identity, recipient } = await generateKeypair();
|
|
3922
|
+
await this.store.set(this.keyName, identity);
|
|
3923
|
+
return { recipient, fingerprint: fingerprint(recipient) };
|
|
3924
|
+
}
|
|
3925
|
+
/**
|
|
3926
|
+
* Return the custodied private key.
|
|
3927
|
+
*
|
|
3928
|
+
* @throws KaguraKeyCustodyError if no key is enrolled for this profile.
|
|
3929
|
+
*/
|
|
3930
|
+
async getIdentity() {
|
|
3931
|
+
const identity = await this.store.get(this.keyName);
|
|
3932
|
+
if (identity === null) {
|
|
3933
|
+
throw new KaguraKeyCustodyError(
|
|
3934
|
+
`no age key in custody for profile ${JSON.stringify(this.profile)}; enroll one first.`
|
|
3935
|
+
);
|
|
3936
|
+
}
|
|
3937
|
+
return identity;
|
|
3938
|
+
}
|
|
3939
|
+
/** Return the public `age1` recipient derived from the custodied key. */
|
|
3940
|
+
async getRecipient() {
|
|
3941
|
+
return recipientFromIdentity(await this.getIdentity());
|
|
3942
|
+
}
|
|
3943
|
+
/** Return the sha256 fingerprint of this profile's public recipient. */
|
|
3944
|
+
async fingerprint() {
|
|
3945
|
+
return fingerprint(await this.getRecipient());
|
|
3946
|
+
}
|
|
3947
|
+
/** Remove this profile's private key from custody. Idempotent. */
|
|
3948
|
+
async delete() {
|
|
3949
|
+
await this.store.delete(this.keyName);
|
|
3950
|
+
}
|
|
3951
|
+
};
|
|
3952
|
+
|
|
3493
3953
|
// src/auth/login.ts
|
|
3494
3954
|
var DEFAULT_SCOPE = "memory:read memory:write";
|
|
3495
3955
|
var READ_ONLY_SCOPE = "memory:read";
|
|
@@ -3622,37 +4082,51 @@ async function consentToWiderScope(stored, scope, options, httpOptions) {
|
|
|
3622
4082
|
KaguraClient,
|
|
3623
4083
|
KaguraConnectionError,
|
|
3624
4084
|
KaguraContextError,
|
|
4085
|
+
KaguraCryptoError,
|
|
3625
4086
|
KaguraError,
|
|
3626
4087
|
KaguraFetchError,
|
|
3627
4088
|
KaguraIngestError,
|
|
3628
4089
|
KaguraIntegrityError,
|
|
4090
|
+
KaguraKeyCustodyError,
|
|
3629
4091
|
KaguraLLMError,
|
|
3630
4092
|
KaguraNotFoundError,
|
|
3631
4093
|
KaguraOAuth,
|
|
3632
4094
|
KaguraQuotaError,
|
|
3633
4095
|
KaguraRateLimitError,
|
|
3634
4096
|
KaguraRestClient,
|
|
4097
|
+
KaguraSecretError,
|
|
4098
|
+
KeyManager,
|
|
4099
|
+
MAX_CIPHERTEXT_BYTES,
|
|
3635
4100
|
MIN_SERVER_VERSION,
|
|
3636
4101
|
READ_ONLY_SCOPE,
|
|
4102
|
+
RECIPIENT_RE,
|
|
3637
4103
|
REFRESH_SKEW_SEC,
|
|
3638
4104
|
REFRESH_TOKEN_GRANT_TYPE,
|
|
3639
4105
|
ResourceClient,
|
|
3640
4106
|
SDK_VERSION,
|
|
3641
4107
|
SETUP_OAUTH_NOT_SUPPORTED_MSG,
|
|
4108
|
+
SecretClient,
|
|
3642
4109
|
VALID_ASSIGNABLE_ROLES,
|
|
3643
4110
|
VALID_INVITE_EXPIRES,
|
|
3644
4111
|
WorkspaceClient,
|
|
4112
|
+
armorDecode,
|
|
4113
|
+
armorEncode,
|
|
3645
4114
|
authorizeDevice,
|
|
4115
|
+
decrypt,
|
|
3646
4116
|
defaultCredentialsPath,
|
|
3647
4117
|
deleteCredentialsFile,
|
|
3648
4118
|
deleteProfile,
|
|
3649
4119
|
emptyCredentialsFile,
|
|
4120
|
+
encrypt,
|
|
4121
|
+
fingerprint,
|
|
4122
|
+
generateKeypair,
|
|
3650
4123
|
getProfile,
|
|
3651
4124
|
isExpired,
|
|
3652
4125
|
loadConfig,
|
|
3653
4126
|
loadCredentialsFile,
|
|
3654
4127
|
login,
|
|
3655
4128
|
pollForToken,
|
|
4129
|
+
recipientFromIdentity,
|
|
3656
4130
|
refresh,
|
|
3657
4131
|
refreshAccessToken,
|
|
3658
4132
|
removeProfile,
|