Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor: separate verifier and mutation password leases
Josuéc6b7397f9eca
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 2ff88a4d0..c3b4ac209 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.771",+ "version": "0.1.772","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.771",+ "version": "0.1.772","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 9af3bd1c1..eb09d0b8f 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.771",+ "version": "0.1.772","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/services/auth/passwordWorkOwnership.ts b/control-api/src/services/auth/passwordWorkOwnership.tsindex ac8679b7e..a1b93a947 100644--- a/control-api/src/services/auth/passwordWorkOwnership.ts+++ b/control-api/src/services/auth/passwordWorkOwnership.ts@@ -15,9 +15,8 @@ export interface PasswordMutationLease {export interface PasswordWorkLease {release(): Promise<void>}-// Existing protocol tests supply the raw acquired view. Both views resolve to-// the same private reservation; compatibility never reacquires another owner.-export type MemberPasswordWorkLease = PasswordMutationLease | PasswordWorkLease+// Preserve the directory/admin lease name while keeping it mutation-only.+export type MemberPasswordWorkLease = PasswordMutationLeaseconst logger = rootLogger.child({ module: 'password-admission' })let ownerInstance: string | undefined@@ -149,12 +148,10 @@ async function reservePasswordWork() {}const raw: PasswordWorkLease = {release: () => {- if (state === 'running') return Promise.reject(authorityFailure())state = 'terminal'return release()},}- reservations.set(raw, mutation)reservations.set(mutation, mutation)return {raw,diff --git a/control-api/test/passwordResetFatalCommit.realPostgres.integration.test.ts b/control-api/test/passwordResetFatalCommit.realPostgres.integration.test.tsindex bed3c28a0..29ab612c5 100644--- a/control-api/test/passwordResetFatalCommit.realPostgres.integration.test.ts+++ b/control-api/test/passwordResetFatalCommit.realPostgres.integration.test.ts@@ -6,7 +6,7 @@ import { Pool } from 'pg'import request from 'supertest'import { initDb } from '../src/db.js'import { sendPasswordAdmissionError } from '../src/middleware/passwordLoginAdmission.js'-import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'+import { withPasswordWorkLease } from '../src/services/auth/passwordWorkOwnership.js'import { setInvitationPasswordForEmail } from '../src/services/directory/membership.js'import { createSilentPasswordResetInvitation } from '../src/services/directory/passwordResetInvitations.js'import { createPostgresFatalCommitReplyInjector } from './helpers/realPostgresFatalCommit.js'@@ -102,21 +102,13 @@ realPg('R4-M2 PostgreSQL session termination after COMMIT dispatch', () => {it('treats a post-COMMIT FATAL as unknown and does not issue a replay session', async () => {holder.unknownCommitOutcomes = 0- const lease = await acquirePasswordWork()- expect(lease).not.toBeNull()- if (!lease) throw new Error('password-work owner was not acquired')-fault = await createPostgresFatalCommitReplyInjector(connectionString)proxyPool = new Pool({ connectionString: fault.connectionString, max: 2 })- holder.pool = proxyPool- const resetOutcome = setInvitationPasswordForEmail(- email,- resetInvitationId,- newPassword,- userId,- lease- ).then(+ const resetOutcome = withPasswordWorkLease(lease => {+ holder.pool = proxyPool+ return setInvitationPasswordForEmail(email, resetInvitationId, newPassword, userId, lease)+ }).then(value => ({ value }),error => ({ error }))@@ -175,15 +167,11 @@ realPg('R4-M2 PostgreSQL session termination after COMMIT dispatch', () => {expect(publicResponse.headers['set-cookie']).toBeUndefined()holder.pool = normalPool- const replayLease = await acquirePasswordWork()- expect(replayLease).not.toBeNull()- if (!replayLease) throw new Error('password-work owner was not reacquired')const replay = await setInvitationPasswordForEmail(email,resetInvitationId,newPassword,- userId,- replayLease+ userId)expect(replay).toEqual({ error: 'not_pending' })expect(diff --git a/control-api/test/passwordResetLostCommit.realPostgres.integration.test.ts b/control-api/test/passwordResetLostCommit.realPostgres.integration.test.tsindex 237e369e0..6af2b9fab 100644--- a/control-api/test/passwordResetLostCommit.realPostgres.integration.test.ts+++ b/control-api/test/passwordResetLostCommit.realPostgres.integration.test.ts@@ -6,7 +6,7 @@ import { Pool } from 'pg'import request from 'supertest'import { initDb } from '../src/db.js'import { sendPasswordAdmissionError } from '../src/middleware/passwordLoginAdmission.js'-import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'+import { withPasswordWorkLease } from '../src/services/auth/passwordWorkOwnership.js'import { setInvitationPasswordForEmail } from '../src/services/directory/membership.js'import { createSilentPasswordResetInvitation } from '../src/services/directory/passwordResetInvitations.js'import { createPostgresCommitReplyBlackhole } from './helpers/realPostgresCancellation.js'@@ -109,21 +109,13 @@ realPg('R2-H2 lost password-reset COMMIT acknowledgement on PostgreSQL 16', () =it('reports an unknown outcome without replay when reset committed but PostgreSQL reply was lost', async () => {holder.unknownCommitOutcomes = 0- const lease = await acquirePasswordWork()- expect(lease).not.toBeNull()- if (!lease) throw new Error('password-work owner was not acquired')-blackhole = await createPostgresCommitReplyBlackhole(connectionString)proxyPool = new Pool({ connectionString: blackhole.connectionString, max: 4 })- holder.pool = proxyPool- const outcomePromise = setInvitationPasswordForEmail(- email,- resetInvitationId,- newPassword,- userId,- lease- ).then(+ const outcomePromise = withPasswordWorkLease(lease => {+ holder.pool = proxyPool+ return setInvitationPasswordForEmail(email, resetInvitationId, newPassword, userId, lease)+ }).then(value => ({ value }),error => ({ error }))diff --git a/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.tsindex bb20ff473..e34470f4d 100644--- a/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts+++ b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts@@ -5,6 +5,7 @@ import { Pool } from 'pg'import { initDb } from '../src/db.js'import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'import {+ type PasswordMutationLease,runPasswordMutation,withPasswordWorkLease,} from '../src/services/auth/passwordWorkOwnership.js'@@ -132,21 +133,29 @@ realPg('password-work operation preservation on PostgreSQL 16', () => {expect(await bcrypt.compare('Synthetic-preservation-password', row.password_hash)).toBe(true)})- it('retains a committed raw supplied lease and does not release a successor owner', async () => {+ it('retains a committed mutation lease and does not release a successor owner', async () => {const fixture = await resetFixture()- const lease = await acquirePasswordWork()- const result = await setInvitationPasswordForEmail(- fixture.email,- fixture.invitation.id,- 'Synthetic-preservation-password',- fixture.userId,- lease!- )+ let lease: PasswordMutationLease | undefined+ const result = await withPasswordWorkLease(mutationLease => {+ lease = mutationLease+ return setInvitationPasswordForEmail(+ fixture.email,+ fixture.invitation.id,+ 'Synthetic-preservation-password',+ fixture.userId,+ mutationLease+ )+ })expect(result).toMatchObject({ data: { passwordUpdated: true, status: 'accepted' } })const successor = await acquirePasswordWork()expect(successor).not.toBeNull()try {- await lease!.release()+ await expect(+ runPasswordMutation(async () => 'must not run', {+ mode: 'standard',+ lease: lease!,+ })+ ).rejects.toMatchObject({ status: 503, message: 'authority_unavailable' })expect(await ownerCount()).toBe(1)} finally {await successor?.release()diff --git a/control-api/test/passwordWorkOwnership.operation.test.ts b/control-api/test/passwordWorkOwnership.operation.test.tsindex 44c772a4a..cbb3d1e52 100644--- a/control-api/test/passwordWorkOwnership.operation.test.ts+++ b/control-api/test/passwordWorkOwnership.operation.test.ts@@ -3,6 +3,7 @@ import fc from 'fast-check'import { PasswordAdmissionError as LeafError } from '../src/services/auth/passwordAdmissionError.js'import { PasswordAdmissionError } from '../src/services/auth/passwordCredentialVerification.js'import {+ type PasswordMutationLease,acquirePasswordWork,runPasswordMutation,withPasswordWorkLease,@@ -204,41 +205,34 @@ describe('password-work reservation preservation', () => {expect(query).not.toHaveBeenCalled()})- it('does not release a raw reservation while its terminal callback is running', async () => {+ it('rejects a raw verifier lease at the mutation provenance boundary', async () => {const raw = await acquirePasswordWork()- let resume!: () => void- const gate = new Promise<void>(resolve => {- resume = resolve- })- const terminal = runPasswordMutation(- async () => {- await gate- return 'committed'- },- {+ const work = vi.fn()+ await expect(+ runPasswordMutation(work, {mode: 'standard',- lease: raw!,- }- )- try {- await expect(raw!.release()).rejects.toBeInstanceOf(PasswordAdmissionError)- expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(0)- } finally {- resume()- }- expect(await terminal).toBe('committed')+ lease: raw as unknown as PasswordMutationLease,+ })+ ).rejects.toBeInstanceOf(PasswordAdmissionError)+ expect(work).not.toHaveBeenCalled()+ expect(query.mock.calls.filter(([sql]) => sql.includes('INSERT INTO'))).toHaveLength(1)+ expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(0)await raw!.release()expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(1)})- it('prevents mutation after raw release was already requested', async () => {+ it('keeps raw verifier release separate from mutation reservation', async () => {const raw = await acquirePasswordWork()- const releasing = raw!.release()+ expect(raw).not.toHaveProperty('commitWith')+ await raw!.release()const work = vi.fn()await expect(- runPasswordMutation(work, { mode: 'standard', lease: raw! })+ runPasswordMutation(work, {+ mode: 'standard',+ lease: raw as unknown as PasswordMutationLease,+ })).rejects.toBeInstanceOf(PasswordAdmissionError)- await releasingexpect(work).not.toHaveBeenCalled()+ expect(query.mock.calls.filter(([sql]) => sql.includes('INSERT INTO'))).toHaveLength(1)})})