@foldrun/core 20.0.0 → 20.2.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.
@@ -44,8 +44,31 @@ interface StoredSecret {
44
44
  * unbound form. */
45
45
  v?: 2;
46
46
  }
47
- type SecretsFile = Record<string, StoredSecret>;
47
+ export type SecretsFile = Record<string, StoredSecret>;
48
48
  declare const writeBound: () => boolean;
49
+ export interface VaultStore {
50
+ /** Is this scope (workspace undefined: the account's) served from the store? */
51
+ on(tenant: string, workspace?: string): boolean;
52
+ read(tenant: string, workspace?: string): SecretsFile;
53
+ /** mutateVaultFile's contract: `change` gets what is stored now and returns
54
+ * the new contents, or null to leave them; one writer at a time per scope
55
+ * (the callback may run again if another writer got in first), all or
56
+ * nothing; returns what is stored after. Throws the same "busy" error. */
57
+ mutate(tenant: string, workspace: string | undefined, change: (all: SecretsFile) => SecretsFile | null): SecretsFile;
58
+ /** The workspaces of this account whose scope is in the store. */
59
+ workspaces(tenant: string): string[];
60
+ /** The scope's FILE was just written while the store is not serving it
61
+ * (switched off): whatever the store still holds for it is stale and must
62
+ * go, so switching back on starts from the file. */
63
+ forget?(tenant: string, workspace?: string): void;
64
+ }
65
+ /** Register (or, with null, remove) the platform's vault store. None: files. */
66
+ export declare function setVaultStore(store: VaultStore | null): void;
67
+ /** Where a scope's vault file is (a platform store imports from it and keeps
68
+ * it current while files remain the fallback). */
69
+ export declare function vaultFilePath(tenant: string, workspace?: string): string;
70
+ /** A vault file's records as they are stored; {} when there is none. */
71
+ export declare function readVaultFile(file: string): SecretsFile;
49
72
  /**
50
73
  * An exclusive lock on `file` for a read-change-write, across processes and
51
74
  * pods: `<file>.lock` opened O_EXCL (NFS 4.1 and EFS honour it), taken over
@@ -195,8 +195,39 @@ function openRecord(tenant, workspace, name, rec) {
195
195
  }
196
196
  return requireBound() ? null : decrypt(tenant, rec);
197
197
  }
198
+ let vaultStore = null;
199
+ /** Register (or, with null, remove) the platform's vault store. None: files. */
200
+ export function setVaultStore(store) {
201
+ vaultStore = store;
202
+ }
203
+ const inStore = (tenant, workspace) => (vaultStore?.on(tenant, workspace) ? vaultStore : null);
198
204
  function read(tenant, workspace) {
199
- return readFile(secretsFile(tenant, workspace));
205
+ const store = inStore(tenant, workspace);
206
+ return store ? store.read(tenant, workspace) : readFile(secretsFile(tenant, workspace));
207
+ }
208
+ /** Change one vault scope safely, wherever it lives (see mutateVaultFile). */
209
+ function mutateVault(tenant, workspace, change) {
210
+ const store = inStore(tenant, workspace);
211
+ if (store)
212
+ return store.mutate(tenant, workspace, change);
213
+ let wrote = false;
214
+ const after = mutateVaultFile(secretsFile(tenant, workspace), (all) => {
215
+ const next = change(all);
216
+ wrote = next !== null;
217
+ return next;
218
+ });
219
+ if (wrote)
220
+ vaultStore?.forget?.(tenant, workspace);
221
+ return after;
222
+ }
223
+ /** Where a scope's vault file is (a platform store imports from it and keeps
224
+ * it current while files remain the fallback). */
225
+ export function vaultFilePath(tenant, workspace) {
226
+ return secretsFile(tenant, workspace);
227
+ }
228
+ /** A vault file's records as they are stored; {} when there is none. */
229
+ export function readVaultFile(file) {
230
+ return readFile(file);
200
231
  }
201
232
  // ------------------------------------------------------------ safe writes
202
233
  //
@@ -326,7 +357,7 @@ export function setSecret(tenant, name, value, workspace, kind, limit = {}) {
326
357
  if (value.length > 8192)
327
358
  throw new Error("secret too long");
328
359
  const wanted = limitFor(workspace, limit);
329
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
360
+ mutateVault(tenant, workspace, (all) => {
330
361
  // A new value keeps the sites the old one was bound to: binding is a
331
362
  // property of the credential's use, not of its current value, and a
332
363
  // rotation that silently unbound it would put the new value in pods.
@@ -369,7 +400,7 @@ export function setSecretSites(tenant, name, sites, workspace) {
369
400
  throw new Error(`"${site}" is not a site — a domain like api.example.com or *.example.com, no scheme, path or port`);
370
401
  }
371
402
  let found = false;
372
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
403
+ mutateVault(tenant, workspace, (all) => {
373
404
  const rec = all[name];
374
405
  if (!rec)
375
406
  return null;
@@ -545,7 +576,7 @@ export function setSecretWorkspaces(tenant, name, workspaces, opts = {}) {
545
576
  const clean = cleanWorkspaceList(workspaces);
546
577
  const expected = opts.ifWorkspaces === undefined ? undefined : cleanWorkspaceList(opts.ifWorkspaces);
547
578
  let found = false;
548
- mutateVaultFile(secretsFile(tenant), (all) => {
579
+ mutateVault(tenant, undefined, (all) => {
549
580
  const rec = all[name];
550
581
  if (!rec)
551
582
  return null;
@@ -592,10 +623,9 @@ function withWorkspaces(tenant, name, rec, workspaces) {
592
623
  export function retargetSecretWorkspaces(tenant, from, to) {
593
624
  const changed = [];
594
625
  const failed = [];
595
- const file = secretsFile(tenant);
596
- if (!fs.existsSync(file))
626
+ if (!inStore(tenant) && !fs.existsSync(secretsFile(tenant)))
597
627
  return { changed, failed };
598
- mutateVaultFile(file, (all) => {
628
+ mutateVault(tenant, undefined, (all) => {
599
629
  changed.length = 0;
600
630
  failed.length = 0;
601
631
  const staged = {};
@@ -634,7 +664,7 @@ export function retargetSecretWorkspaces(tenant, from, to) {
634
664
  return { changed, failed };
635
665
  }
636
666
  export function deleteSecret(tenant, name, workspace) {
637
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
667
+ mutateVault(tenant, workspace, (all) => {
638
668
  if (!(name in all))
639
669
  return null;
640
670
  delete all[name];
@@ -719,6 +749,12 @@ function secretFiles(tenant) {
719
749
  out.push({ file: f, workspace: entry.name });
720
750
  }
721
751
  }
752
+ // Scopes in the platform's store have no file; they are named by where
753
+ // the file would be (the result's `file` is a label, never opened).
754
+ for (const workspace of vaultStore?.workspaces(tenant) ?? []) {
755
+ if (!out.some((x) => x.workspace === workspace))
756
+ out.push({ file: secretsFile(tenant, workspace), workspace });
757
+ }
722
758
  return out;
723
759
  }
724
760
  /**
@@ -744,7 +780,7 @@ export function rotateMasterKey(tenant, from, to) {
744
780
  const unreadable = [];
745
781
  for (const { file, workspace } of secretFiles(tenant)) {
746
782
  let count = 0;
747
- mutateVaultFile(file, (all) => {
783
+ mutateVault(tenant, workspace, (all) => {
748
784
  const next = {};
749
785
  for (const [name, rec] of Object.entries(all)) {
750
786
  // A record under the ACCOUNT's key is not under the install key, so
@@ -873,7 +909,7 @@ export function replaceOAuth2RefreshToken(tenant, name, workspace, usedValue, re
873
909
  const next = OAUTH2_PREFIX + JSON.stringify({ ...config, refresh_token: refreshToken });
874
910
  for (const scope of workspace ? [workspace, undefined] : [undefined]) {
875
911
  let replaced = false;
876
- mutateVaultFile(secretsFile(tenant, scope), (all) => {
912
+ mutateVault(tenant, scope, (all) => {
877
913
  const rec = all[name];
878
914
  if (rec && openRecord(tenant, scope, name, rec) === usedValue) {
879
915
  // Built whole, not spread over the old record: a spread kept a stale
@@ -1019,6 +1055,8 @@ function workspacesWithSecrets(tenant) {
1019
1055
  if (fs.existsSync(path.join(base, n, "secrets.json")))
1020
1056
  out.add(n);
1021
1057
  }
1058
+ for (const n of vaultStore?.workspaces(tenant) ?? [])
1059
+ out.add(n);
1022
1060
  return [...out];
1023
1061
  }
1024
1062
  /**
@@ -1402,7 +1440,7 @@ export function setClientCertSecret(tenant, name, config, workspace, limit = {})
1402
1440
  // step: a value set over a secret keeps its site binding, and a wildcard
1403
1441
  // one would present the certificate to a whole domain.
1404
1442
  const wanted = limitFor(workspace, limit);
1405
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
1443
+ mutateVault(tenant, workspace, (all) => {
1406
1444
  const sites = all[name]?.sites;
1407
1445
  if (wanted !== undefined)
1408
1446
  assertWorkspacesExist(wanted, limit.exists);
@@ -1617,8 +1655,8 @@ export function sealVaultBindings(tenant) {
1617
1655
  assertSafeName(tenant, "tenant");
1618
1656
  let sealed = 0;
1619
1657
  const unreadable = [];
1620
- for (const { file, workspace } of secretFiles(tenant)) {
1621
- mutateVaultFile(file, (all) => {
1658
+ for (const { workspace } of secretFiles(tenant)) {
1659
+ mutateVault(tenant, workspace, (all) => {
1622
1660
  const plain = new Map();
1623
1661
  const bad = [];
1624
1662
  for (const [name, rec] of Object.entries(all)) {
@@ -1660,10 +1698,10 @@ export function rewrapTenantSecrets(tenant) {
1660
1698
  return { moved: 0, unreadable: [] };
1661
1699
  let moved = 0;
1662
1700
  const unreadable = [];
1663
- for (const { file, workspace } of secretFiles(tenant)) {
1701
+ for (const { workspace } of secretFiles(tenant)) {
1664
1702
  // Under the vault's lock: every pod runs this at boot, and a save made
1665
1703
  // between our read and our write must not be lost.
1666
- mutateVaultFile(file, (all) => {
1704
+ mutateVault(tenant, workspace, (all) => {
1667
1705
  const plain = new Map();
1668
1706
  const unreadableHere = [];
1669
1707
  for (const [name, rec] of Object.entries(all)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "20.0.0",
3
+ "version": "20.2.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/secrets.ts CHANGED
@@ -128,7 +128,7 @@ interface StoredSecret {
128
128
  v?: 2;
129
129
  }
130
130
 
131
- type SecretsFile = Record<string, StoredSecret>;
131
+ export type SecretsFile = Record<string, StoredSecret>;
132
132
 
133
133
  // ------------------------------------------------------------ binding
134
134
  //
@@ -251,8 +251,67 @@ function openRecord(tenant: string, workspace: string | undefined, name: string,
251
251
  return requireBound() ? null : decrypt(tenant, rec);
252
252
  }
253
253
 
254
+ // ------------------------------------------------------------ where a vault lives
255
+ //
256
+ // A vault scope (an account's, or one workspace's) is its secrets.json — or,
257
+ // on a platform that has registered a store and moved that scope, rows in its
258
+ // database (FOL-126 phase 6: the last thing the egress pod needs the shared
259
+ // volume for). The records are the same sealed records either way: the store
260
+ // never sees a value. A scope is served from exactly one place — a store
261
+ // that says `on` for it is the only thing read and written, so a secret
262
+ // deleted there cannot come back from a file left behind.
263
+
264
+ export interface VaultStore {
265
+ /** Is this scope (workspace undefined: the account's) served from the store? */
266
+ on(tenant: string, workspace?: string): boolean;
267
+ read(tenant: string, workspace?: string): SecretsFile;
268
+ /** mutateVaultFile's contract: `change` gets what is stored now and returns
269
+ * the new contents, or null to leave them; one writer at a time per scope
270
+ * (the callback may run again if another writer got in first), all or
271
+ * nothing; returns what is stored after. Throws the same "busy" error. */
272
+ mutate(tenant: string, workspace: string | undefined, change: (all: SecretsFile) => SecretsFile | null): SecretsFile;
273
+ /** The workspaces of this account whose scope is in the store. */
274
+ workspaces(tenant: string): string[];
275
+ /** The scope's FILE was just written while the store is not serving it
276
+ * (switched off): whatever the store still holds for it is stale and must
277
+ * go, so switching back on starts from the file. */
278
+ forget?(tenant: string, workspace?: string): void;
279
+ }
280
+
281
+ let vaultStore: VaultStore | null = null;
282
+ /** Register (or, with null, remove) the platform's vault store. None: files. */
283
+ export function setVaultStore(store: VaultStore | null): void {
284
+ vaultStore = store;
285
+ }
286
+ const inStore = (tenant: string, workspace?: string): VaultStore | null => (vaultStore?.on(tenant, workspace) ? vaultStore : null);
287
+
254
288
  function read(tenant: string, workspace?: string): SecretsFile {
255
- return readFile(secretsFile(tenant, workspace));
289
+ const store = inStore(tenant, workspace);
290
+ return store ? store.read(tenant, workspace) : readFile(secretsFile(tenant, workspace));
291
+ }
292
+
293
+ /** Change one vault scope safely, wherever it lives (see mutateVaultFile). */
294
+ function mutateVault(tenant: string, workspace: string | undefined, change: (all: SecretsFile) => SecretsFile | null): SecretsFile {
295
+ const store = inStore(tenant, workspace);
296
+ if (store) return store.mutate(tenant, workspace, change);
297
+ let wrote = false;
298
+ const after = mutateVaultFile(secretsFile(tenant, workspace), (all) => {
299
+ const next = change(all);
300
+ wrote = next !== null;
301
+ return next;
302
+ });
303
+ if (wrote) vaultStore?.forget?.(tenant, workspace);
304
+ return after;
305
+ }
306
+
307
+ /** Where a scope's vault file is (a platform store imports from it and keeps
308
+ * it current while files remain the fallback). */
309
+ export function vaultFilePath(tenant: string, workspace?: string): string {
310
+ return secretsFile(tenant, workspace);
311
+ }
312
+ /** A vault file's records as they are stored; {} when there is none. */
313
+ export function readVaultFile(file: string): SecretsFile {
314
+ return readFile(file);
256
315
  }
257
316
 
258
317
  // ------------------------------------------------------------ safe writes
@@ -401,7 +460,7 @@ export function setSecret(
401
460
  }
402
461
  if (value.length > 8192) throw new Error("secret too long");
403
462
  const wanted = limitFor(workspace, limit);
404
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
463
+ mutateVault(tenant, workspace, (all) => {
405
464
  // A new value keeps the sites the old one was bound to: binding is a
406
465
  // property of the credential's use, not of its current value, and a
407
466
  // rotation that silently unbound it would put the new value in pods.
@@ -441,7 +500,7 @@ export function setSecretSites(tenant: string, name: string, sites: string[], wo
441
500
  if (!SITE.test(site)) throw new Error(`"${site}" is not a site — a domain like api.example.com or *.example.com, no scheme, path or port`);
442
501
  }
443
502
  let found = false;
444
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
503
+ mutateVault(tenant, workspace, (all) => {
445
504
  const rec = all[name];
446
505
  if (!rec) return null;
447
506
  found = true;
@@ -630,7 +689,7 @@ export function setSecretWorkspaces(
630
689
  const clean = cleanWorkspaceList(workspaces);
631
690
  const expected = opts.ifWorkspaces === undefined ? undefined : cleanWorkspaceList(opts.ifWorkspaces);
632
691
  let found = false;
633
- mutateVaultFile(secretsFile(tenant), (all) => {
692
+ mutateVault(tenant, undefined, (all) => {
634
693
  const rec = all[name];
635
694
  if (!rec) return null;
636
695
  found = true;
@@ -676,9 +735,8 @@ function withWorkspaces(tenant: string, name: string, rec: StoredSecret, workspa
676
735
  export function retargetSecretWorkspaces(tenant: string, from: string, to: string | null): { changed: string[]; failed: string[] } {
677
736
  const changed: string[] = [];
678
737
  const failed: string[] = [];
679
- const file = secretsFile(tenant);
680
- if (!fs.existsSync(file)) return { changed, failed };
681
- mutateVaultFile(file, (all) => {
738
+ if (!inStore(tenant) && !fs.existsSync(secretsFile(tenant))) return { changed, failed };
739
+ mutateVault(tenant, undefined, (all) => {
682
740
  changed.length = 0;
683
741
  failed.length = 0;
684
742
  const staged: typeof all = {};
@@ -714,7 +772,7 @@ export function retargetSecretWorkspaces(tenant: string, from: string, to: strin
714
772
  }
715
773
 
716
774
  export function deleteSecret(tenant: string, name: string, workspace?: string) {
717
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
775
+ mutateVault(tenant, workspace, (all) => {
718
776
  if (!(name in all)) return null;
719
777
  delete all[name];
720
778
  return all;
@@ -817,6 +875,11 @@ function secretFiles(tenant: string): { file: string; workspace?: string }[] {
817
875
  if (fs.existsSync(f)) out.push({ file: f, workspace: entry.name });
818
876
  }
819
877
  }
878
+ // Scopes in the platform's store have no file; they are named by where
879
+ // the file would be (the result's `file` is a label, never opened).
880
+ for (const workspace of vaultStore?.workspaces(tenant) ?? []) {
881
+ if (!out.some((x) => x.workspace === workspace)) out.push({ file: secretsFile(tenant, workspace), workspace });
882
+ }
820
883
  return out;
821
884
  }
822
885
 
@@ -856,7 +919,7 @@ export function rotateMasterKey(
856
919
 
857
920
  for (const { file, workspace } of secretFiles(tenant)) {
858
921
  let count = 0;
859
- mutateVaultFile(file, (all) => {
922
+ mutateVault(tenant, workspace, (all) => {
860
923
  const next: SecretsFile = {};
861
924
  for (const [name, rec] of Object.entries(all)) {
862
925
  // A record under the ACCOUNT's key is not under the install key, so
@@ -1043,7 +1106,7 @@ export function replaceOAuth2RefreshToken(tenant: string, name: string, workspac
1043
1106
  const next = OAUTH2_PREFIX + JSON.stringify({ ...config, refresh_token: refreshToken });
1044
1107
  for (const scope of workspace ? [workspace, undefined] : [undefined]) {
1045
1108
  let replaced = false;
1046
- mutateVaultFile(secretsFile(tenant, scope), (all) => {
1109
+ mutateVault(tenant, scope, (all) => {
1047
1110
  const rec = all[name];
1048
1111
  if (rec && openRecord(tenant, scope, name, rec) === usedValue) {
1049
1112
  // Built whole, not spread over the old record: a spread kept a stale
@@ -1192,6 +1255,7 @@ function workspacesWithSecrets(tenant: string): string[] {
1192
1255
  }
1193
1256
  for (const n of names) if (fs.existsSync(path.join(base, n, "secrets.json"))) out.add(n);
1194
1257
  }
1258
+ for (const n of vaultStore?.workspaces(tenant) ?? []) out.add(n);
1195
1259
  return [...out];
1196
1260
  }
1197
1261
 
@@ -1641,7 +1705,7 @@ export function setClientCertSecret(tenant: string, name: string, config: Client
1641
1705
  // step: a value set over a secret keeps its site binding, and a wildcard
1642
1706
  // one would present the certificate to a whole domain.
1643
1707
  const wanted = limitFor(workspace, limit);
1644
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
1708
+ mutateVault(tenant, workspace, (all) => {
1645
1709
  const sites = all[name]?.sites;
1646
1710
  if (wanted !== undefined) assertWorkspacesExist(wanted, limit.exists);
1647
1711
  const workspaces = wanted !== undefined ? (wanted ?? undefined) : all[name]?.workspaces;
@@ -1895,8 +1959,8 @@ export function sealVaultBindings(tenant: string): { sealed: number; unreadable:
1895
1959
  assertSafeName(tenant, "tenant");
1896
1960
  let sealed = 0;
1897
1961
  const unreadable: string[] = [];
1898
- for (const { file, workspace } of secretFiles(tenant)) {
1899
- mutateVaultFile(file, (all) => {
1962
+ for (const { workspace } of secretFiles(tenant)) {
1963
+ mutateVault(tenant, workspace, (all) => {
1900
1964
  const plain = new Map<string, string>();
1901
1965
  const bad: string[] = [];
1902
1966
  for (const [name, rec] of Object.entries(all)) {
@@ -1937,10 +2001,10 @@ export function rewrapTenantSecrets(tenant: string): { moved: number; unreadable
1937
2001
  let moved = 0;
1938
2002
  const unreadable: string[] = [];
1939
2003
 
1940
- for (const { file, workspace } of secretFiles(tenant)) {
2004
+ for (const { workspace } of secretFiles(tenant)) {
1941
2005
  // Under the vault's lock: every pod runs this at boot, and a save made
1942
2006
  // between our read and our write must not be lost.
1943
- mutateVaultFile(file, (all) => {
2007
+ mutateVault(tenant, workspace, (all) => {
1944
2008
  const plain = new Map<string, string>();
1945
2009
  const unreadableHere: string[] = [];
1946
2010
  for (const [name, rec] of Object.entries(all)) {