@foldrun/core 20.0.0 → 20.1.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,22 @@ 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
+ }
61
+ /** Register (or, with null, remove) the platform's vault store. None: files. */
62
+ export declare function setVaultStore(store: VaultStore | null): void;
49
63
  /**
50
64
  * An exclusive lock on `file` for a read-change-write, across processes and
51
65
  * pods: `<file>.lock` opened O_EXCL (NFS 4.1 and EFS honour it), taken over
@@ -195,8 +195,20 @@ 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
+ return store ? store.mutate(tenant, workspace, change) : mutateVaultFile(secretsFile(tenant, workspace), change);
200
212
  }
201
213
  // ------------------------------------------------------------ safe writes
202
214
  //
@@ -326,7 +338,7 @@ export function setSecret(tenant, name, value, workspace, kind, limit = {}) {
326
338
  if (value.length > 8192)
327
339
  throw new Error("secret too long");
328
340
  const wanted = limitFor(workspace, limit);
329
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
341
+ mutateVault(tenant, workspace, (all) => {
330
342
  // A new value keeps the sites the old one was bound to: binding is a
331
343
  // property of the credential's use, not of its current value, and a
332
344
  // rotation that silently unbound it would put the new value in pods.
@@ -369,7 +381,7 @@ export function setSecretSites(tenant, name, sites, workspace) {
369
381
  throw new Error(`"${site}" is not a site — a domain like api.example.com or *.example.com, no scheme, path or port`);
370
382
  }
371
383
  let found = false;
372
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
384
+ mutateVault(tenant, workspace, (all) => {
373
385
  const rec = all[name];
374
386
  if (!rec)
375
387
  return null;
@@ -545,7 +557,7 @@ export function setSecretWorkspaces(tenant, name, workspaces, opts = {}) {
545
557
  const clean = cleanWorkspaceList(workspaces);
546
558
  const expected = opts.ifWorkspaces === undefined ? undefined : cleanWorkspaceList(opts.ifWorkspaces);
547
559
  let found = false;
548
- mutateVaultFile(secretsFile(tenant), (all) => {
560
+ mutateVault(tenant, undefined, (all) => {
549
561
  const rec = all[name];
550
562
  if (!rec)
551
563
  return null;
@@ -592,10 +604,9 @@ function withWorkspaces(tenant, name, rec, workspaces) {
592
604
  export function retargetSecretWorkspaces(tenant, from, to) {
593
605
  const changed = [];
594
606
  const failed = [];
595
- const file = secretsFile(tenant);
596
- if (!fs.existsSync(file))
607
+ if (!inStore(tenant) && !fs.existsSync(secretsFile(tenant)))
597
608
  return { changed, failed };
598
- mutateVaultFile(file, (all) => {
609
+ mutateVault(tenant, undefined, (all) => {
599
610
  changed.length = 0;
600
611
  failed.length = 0;
601
612
  const staged = {};
@@ -634,7 +645,7 @@ export function retargetSecretWorkspaces(tenant, from, to) {
634
645
  return { changed, failed };
635
646
  }
636
647
  export function deleteSecret(tenant, name, workspace) {
637
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
648
+ mutateVault(tenant, workspace, (all) => {
638
649
  if (!(name in all))
639
650
  return null;
640
651
  delete all[name];
@@ -719,6 +730,12 @@ function secretFiles(tenant) {
719
730
  out.push({ file: f, workspace: entry.name });
720
731
  }
721
732
  }
733
+ // Scopes in the platform's store have no file; they are named by where
734
+ // the file would be (the result's `file` is a label, never opened).
735
+ for (const workspace of vaultStore?.workspaces(tenant) ?? []) {
736
+ if (!out.some((x) => x.workspace === workspace))
737
+ out.push({ file: secretsFile(tenant, workspace), workspace });
738
+ }
722
739
  return out;
723
740
  }
724
741
  /**
@@ -744,7 +761,7 @@ export function rotateMasterKey(tenant, from, to) {
744
761
  const unreadable = [];
745
762
  for (const { file, workspace } of secretFiles(tenant)) {
746
763
  let count = 0;
747
- mutateVaultFile(file, (all) => {
764
+ mutateVault(tenant, workspace, (all) => {
748
765
  const next = {};
749
766
  for (const [name, rec] of Object.entries(all)) {
750
767
  // A record under the ACCOUNT's key is not under the install key, so
@@ -873,7 +890,7 @@ export function replaceOAuth2RefreshToken(tenant, name, workspace, usedValue, re
873
890
  const next = OAUTH2_PREFIX + JSON.stringify({ ...config, refresh_token: refreshToken });
874
891
  for (const scope of workspace ? [workspace, undefined] : [undefined]) {
875
892
  let replaced = false;
876
- mutateVaultFile(secretsFile(tenant, scope), (all) => {
893
+ mutateVault(tenant, scope, (all) => {
877
894
  const rec = all[name];
878
895
  if (rec && openRecord(tenant, scope, name, rec) === usedValue) {
879
896
  // Built whole, not spread over the old record: a spread kept a stale
@@ -1019,6 +1036,8 @@ function workspacesWithSecrets(tenant) {
1019
1036
  if (fs.existsSync(path.join(base, n, "secrets.json")))
1020
1037
  out.add(n);
1021
1038
  }
1039
+ for (const n of vaultStore?.workspaces(tenant) ?? [])
1040
+ out.add(n);
1022
1041
  return [...out];
1023
1042
  }
1024
1043
  /**
@@ -1402,7 +1421,7 @@ export function setClientCertSecret(tenant, name, config, workspace, limit = {})
1402
1421
  // step: a value set over a secret keeps its site binding, and a wildcard
1403
1422
  // one would present the certificate to a whole domain.
1404
1423
  const wanted = limitFor(workspace, limit);
1405
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
1424
+ mutateVault(tenant, workspace, (all) => {
1406
1425
  const sites = all[name]?.sites;
1407
1426
  if (wanted !== undefined)
1408
1427
  assertWorkspacesExist(wanted, limit.exists);
@@ -1617,8 +1636,8 @@ export function sealVaultBindings(tenant) {
1617
1636
  assertSafeName(tenant, "tenant");
1618
1637
  let sealed = 0;
1619
1638
  const unreadable = [];
1620
- for (const { file, workspace } of secretFiles(tenant)) {
1621
- mutateVaultFile(file, (all) => {
1639
+ for (const { workspace } of secretFiles(tenant)) {
1640
+ mutateVault(tenant, workspace, (all) => {
1622
1641
  const plain = new Map();
1623
1642
  const bad = [];
1624
1643
  for (const [name, rec] of Object.entries(all)) {
@@ -1660,10 +1679,10 @@ export function rewrapTenantSecrets(tenant) {
1660
1679
  return { moved: 0, unreadable: [] };
1661
1680
  let moved = 0;
1662
1681
  const unreadable = [];
1663
- for (const { file, workspace } of secretFiles(tenant)) {
1682
+ for (const { workspace } of secretFiles(tenant)) {
1664
1683
  // Under the vault's lock: every pod runs this at boot, and a save made
1665
1684
  // between our read and our write must not be lost.
1666
- mutateVaultFile(file, (all) => {
1685
+ mutateVault(tenant, workspace, (all) => {
1667
1686
  const plain = new Map();
1668
1687
  const unreadableHere = [];
1669
1688
  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.1.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,45 @@ 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
+ }
276
+
277
+ let vaultStore: VaultStore | null = null;
278
+ /** Register (or, with null, remove) the platform's vault store. None: files. */
279
+ export function setVaultStore(store: VaultStore | null): void {
280
+ vaultStore = store;
281
+ }
282
+ const inStore = (tenant: string, workspace?: string): VaultStore | null => (vaultStore?.on(tenant, workspace) ? vaultStore : null);
283
+
254
284
  function read(tenant: string, workspace?: string): SecretsFile {
255
- return readFile(secretsFile(tenant, workspace));
285
+ const store = inStore(tenant, workspace);
286
+ return store ? store.read(tenant, workspace) : readFile(secretsFile(tenant, workspace));
287
+ }
288
+
289
+ /** Change one vault scope safely, wherever it lives (see mutateVaultFile). */
290
+ function mutateVault(tenant: string, workspace: string | undefined, change: (all: SecretsFile) => SecretsFile | null): SecretsFile {
291
+ const store = inStore(tenant, workspace);
292
+ return store ? store.mutate(tenant, workspace, change) : mutateVaultFile(secretsFile(tenant, workspace), change);
256
293
  }
257
294
 
258
295
  // ------------------------------------------------------------ safe writes
@@ -401,7 +438,7 @@ export function setSecret(
401
438
  }
402
439
  if (value.length > 8192) throw new Error("secret too long");
403
440
  const wanted = limitFor(workspace, limit);
404
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
441
+ mutateVault(tenant, workspace, (all) => {
405
442
  // A new value keeps the sites the old one was bound to: binding is a
406
443
  // property of the credential's use, not of its current value, and a
407
444
  // rotation that silently unbound it would put the new value in pods.
@@ -441,7 +478,7 @@ export function setSecretSites(tenant: string, name: string, sites: string[], wo
441
478
  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
479
  }
443
480
  let found = false;
444
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
481
+ mutateVault(tenant, workspace, (all) => {
445
482
  const rec = all[name];
446
483
  if (!rec) return null;
447
484
  found = true;
@@ -630,7 +667,7 @@ export function setSecretWorkspaces(
630
667
  const clean = cleanWorkspaceList(workspaces);
631
668
  const expected = opts.ifWorkspaces === undefined ? undefined : cleanWorkspaceList(opts.ifWorkspaces);
632
669
  let found = false;
633
- mutateVaultFile(secretsFile(tenant), (all) => {
670
+ mutateVault(tenant, undefined, (all) => {
634
671
  const rec = all[name];
635
672
  if (!rec) return null;
636
673
  found = true;
@@ -676,9 +713,8 @@ function withWorkspaces(tenant: string, name: string, rec: StoredSecret, workspa
676
713
  export function retargetSecretWorkspaces(tenant: string, from: string, to: string | null): { changed: string[]; failed: string[] } {
677
714
  const changed: string[] = [];
678
715
  const failed: string[] = [];
679
- const file = secretsFile(tenant);
680
- if (!fs.existsSync(file)) return { changed, failed };
681
- mutateVaultFile(file, (all) => {
716
+ if (!inStore(tenant) && !fs.existsSync(secretsFile(tenant))) return { changed, failed };
717
+ mutateVault(tenant, undefined, (all) => {
682
718
  changed.length = 0;
683
719
  failed.length = 0;
684
720
  const staged: typeof all = {};
@@ -714,7 +750,7 @@ export function retargetSecretWorkspaces(tenant: string, from: string, to: strin
714
750
  }
715
751
 
716
752
  export function deleteSecret(tenant: string, name: string, workspace?: string) {
717
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
753
+ mutateVault(tenant, workspace, (all) => {
718
754
  if (!(name in all)) return null;
719
755
  delete all[name];
720
756
  return all;
@@ -817,6 +853,11 @@ function secretFiles(tenant: string): { file: string; workspace?: string }[] {
817
853
  if (fs.existsSync(f)) out.push({ file: f, workspace: entry.name });
818
854
  }
819
855
  }
856
+ // Scopes in the platform's store have no file; they are named by where
857
+ // the file would be (the result's `file` is a label, never opened).
858
+ for (const workspace of vaultStore?.workspaces(tenant) ?? []) {
859
+ if (!out.some((x) => x.workspace === workspace)) out.push({ file: secretsFile(tenant, workspace), workspace });
860
+ }
820
861
  return out;
821
862
  }
822
863
 
@@ -856,7 +897,7 @@ export function rotateMasterKey(
856
897
 
857
898
  for (const { file, workspace } of secretFiles(tenant)) {
858
899
  let count = 0;
859
- mutateVaultFile(file, (all) => {
900
+ mutateVault(tenant, workspace, (all) => {
860
901
  const next: SecretsFile = {};
861
902
  for (const [name, rec] of Object.entries(all)) {
862
903
  // A record under the ACCOUNT's key is not under the install key, so
@@ -1043,7 +1084,7 @@ export function replaceOAuth2RefreshToken(tenant: string, name: string, workspac
1043
1084
  const next = OAUTH2_PREFIX + JSON.stringify({ ...config, refresh_token: refreshToken });
1044
1085
  for (const scope of workspace ? [workspace, undefined] : [undefined]) {
1045
1086
  let replaced = false;
1046
- mutateVaultFile(secretsFile(tenant, scope), (all) => {
1087
+ mutateVault(tenant, scope, (all) => {
1047
1088
  const rec = all[name];
1048
1089
  if (rec && openRecord(tenant, scope, name, rec) === usedValue) {
1049
1090
  // Built whole, not spread over the old record: a spread kept a stale
@@ -1192,6 +1233,7 @@ function workspacesWithSecrets(tenant: string): string[] {
1192
1233
  }
1193
1234
  for (const n of names) if (fs.existsSync(path.join(base, n, "secrets.json"))) out.add(n);
1194
1235
  }
1236
+ for (const n of vaultStore?.workspaces(tenant) ?? []) out.add(n);
1195
1237
  return [...out];
1196
1238
  }
1197
1239
 
@@ -1641,7 +1683,7 @@ export function setClientCertSecret(tenant: string, name: string, config: Client
1641
1683
  // step: a value set over a secret keeps its site binding, and a wildcard
1642
1684
  // one would present the certificate to a whole domain.
1643
1685
  const wanted = limitFor(workspace, limit);
1644
- mutateVaultFile(secretsFile(tenant, workspace), (all) => {
1686
+ mutateVault(tenant, workspace, (all) => {
1645
1687
  const sites = all[name]?.sites;
1646
1688
  if (wanted !== undefined) assertWorkspacesExist(wanted, limit.exists);
1647
1689
  const workspaces = wanted !== undefined ? (wanted ?? undefined) : all[name]?.workspaces;
@@ -1895,8 +1937,8 @@ export function sealVaultBindings(tenant: string): { sealed: number; unreadable:
1895
1937
  assertSafeName(tenant, "tenant");
1896
1938
  let sealed = 0;
1897
1939
  const unreadable: string[] = [];
1898
- for (const { file, workspace } of secretFiles(tenant)) {
1899
- mutateVaultFile(file, (all) => {
1940
+ for (const { workspace } of secretFiles(tenant)) {
1941
+ mutateVault(tenant, workspace, (all) => {
1900
1942
  const plain = new Map<string, string>();
1901
1943
  const bad: string[] = [];
1902
1944
  for (const [name, rec] of Object.entries(all)) {
@@ -1937,10 +1979,10 @@ export function rewrapTenantSecrets(tenant: string): { moved: number; unreadable
1937
1979
  let moved = 0;
1938
1980
  const unreadable: string[] = [];
1939
1981
 
1940
- for (const { file, workspace } of secretFiles(tenant)) {
1982
+ for (const { workspace } of secretFiles(tenant)) {
1941
1983
  // Under the vault's lock: every pod runs this at boot, and a save made
1942
1984
  // between our read and our write must not be lost.
1943
- mutateVaultFile(file, (all) => {
1985
+ mutateVault(tenant, workspace, (all) => {
1944
1986
  const plain = new Map<string, string>();
1945
1987
  const unreadableHere: string[] = [];
1946
1988
  for (const [name, rec] of Object.entries(all)) {