Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Record the latest granter on re-grant in the in-memory ACL store, as Postgres does (#1921)

Regan Belld62b76ffcd5a
diff --git a/src/acl/acl-store.ts b/src/acl/acl-store.ts
index 7df628d92..9ca615673 100644
--- a/src/acl/acl-store.ts
+++ b/src/acl/acl-store.ts
@@ -121,7 +121,9 @@ function createMemoryGrantPersistence(): GrantPersistence {
});
},
async put(g) {
- if (!grants.some((x) => sameGrant(x, g))) grants.push(g);
+ const existing = grants.findIndex((x) => sameGrant(x, g));
+ if (existing === -1) grants.push(g);
+ else grants[existing] = g;
},
async remove(g) {
for (let i = grants.length - 1; i >= 0; i--) {
diff --git a/test/acl-regrant-attribution.test.ts b/test/acl-regrant-attribution.test.ts
new file mode 100644
index 000000000..835aceaa1
--- /dev/null
+++ b/test/acl-regrant-attribution.test.ts
@@ -0,0 +1,41 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { createAclStore } from "../src/acl/acl-store.ts";
+import { createPostgresGrantStore } from "../src/acl/postgres-grant-store.ts";
+import type { Grant } from "../src/types.ts";
+
+const base = { ownerScopeId: "org:default-org", granteeScopeId: "personal:alice", permission: "read" } as const;
+
+async function regrant(acl: ReturnType<typeof createAclStore>, ref: string): Promise<Grant[]> {
+ await acl.grant({ ...base, ref, grantedBy: "first-admin" });
+ await acl.grant({ ...base, ref, grantedBy: "second-admin" });
+ return acl.grantsFor(base.ownerScopeId, ref);
+}
+
+test("re-granting the same access records the latest granter (in-memory store)", async () => {
+ const acl = createAclStore(undefined, { manages: async () => true });
+ const grants = await regrant(acl, "service-cred:regrant-memory");
+ assert.deepEqual(
+ grants.map((g) => g.grantedBy),
+ ["second-admin"],
+ );
+});
+
+test(
+ "re-granting the same access records the latest granter (Postgres store)",
+ { skip: !process.env.DATABASE_URL && "requires Postgres" },
+ async () => {
+ const persist = createPostgresGrantStore(process.env.DATABASE_URL!);
+ const acl = createAclStore(persist, { manages: async () => true });
+ const ref = `service-cred:regrant-${Date.now()}`;
+ try {
+ const grants = await regrant(acl, ref);
+ assert.deepEqual(
+ grants.map((g) => g.grantedBy),
+ ["second-admin"],
+ );
+ } finally {
+ await persist.remove({ ...base, ref, grantedBy: "second-admin" });
+ }
+ },
+);