Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(workflow-recipes): keep the oauth-broker delete watermark across a token ADDED
Alfredo Lopez4d8132a410bf
diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.jsonindex 73bb7e1f4..7473e78b9 100644--- a/workflow-recipes/package-lock.json+++ b/workflow-recipes/package-lock.json@@ -1,12 +1,12 @@{"name": "workload-recipes-controller",- "version": "0.1.416",+ "version": "0.1.417","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "workload-recipes-controller",- "version": "0.1.416",+ "version": "0.1.417","license": "SEE LICENSE IN LICENSE","dependencies": {"@aws-sdk/client-s3": "^3.1009.0",diff --git a/workflow-recipes/package.json b/workflow-recipes/package.jsonindex 4cd8722a8..ba541fbcc 100644--- a/workflow-recipes/package.json+++ b/workflow-recipes/package.json@@ -1,6 +1,6 @@{"name": "workload-recipes-controller",- "version": "0.1.416",+ "version": "0.1.417","description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface","main": "dist/main.js","engines": {diff --git a/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.test.ts b/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.test.tsindex a918e2f55..469e2e0a0 100644--- a/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.test.ts+++ b/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.test.ts@@ -91,6 +91,33 @@ describe('OAuthBrokerDeleteLedger', () => {expect(ledger.shouldDeletePolicy(ref('r', 4), 1)).toBe(false)})+ it('R2-L1: an ADDED after a newer generation provisioned the token does not re-arm an older pass', () => {+ const ledger = new OAuthBrokerDeleteLedger()+ // gen4 has no backgroundAccess: the Secret is reaped and recorded.+ recordSecret(ledger, ref('r', 4))+ // gen5 turns backgroundAccess on and the token is issued.+ ledger.noteSecretProvisioned(ref('r', 5))+ // The token's watch ADDED.+ ledger.invalidateSecret('r')++ // A queued pass still carrying the gen4 object must not delete the live token.+ expect(ledger.shouldDeleteSecret(ref('r', 4))).toBe(false)+ // Liveness witness: a newer generation that drops backgroundAccess reaps it.+ expect(ledger.shouldDeleteSecret(ref('r', 6))).toBe(true)+ })++ it('R2-L1: invalidateSecret keeps the watermark: below it stays skipped, at it re-arms', () => {+ const ledger = new OAuthBrokerDeleteLedger()+ recordSecret(ledger, ref('r', 5))+ ledger.invalidateSecret('r')+ expect(ledger.shouldDeleteSecret(ref('r', 4))).toBe(false)+ expect(ledger.shouldDeleteSecret(ref('r', 5))).toBe(true)++ // Recording the re-armed delete disarms it again.+ recordSecret(ledger, ref('r', 5))+ expect(ledger.shouldDeleteSecret(ref('r', 5))).toBe(false)+ })+it('does not record a Secret delete when an invalidation landed after the epoch was read', () => {const ledger = new OAuthBrokerDeleteLedger()const epochBeforeDelete = ledger.secretEpoch('r')diff --git a/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.ts b/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.tsindex 3b9068f98..bcb1c5667 100644--- a/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.ts+++ b/workflow-recipes/src/reconciler/oauthBrokerDeleteLedger.ts@@ -4,16 +4,24 @@* 1h TTL so a later pass can reap a leftover without a watch. Indexing only* by recipe name is the E.6 vacuity mutation: a generation bump must miss.*- * One entry per recipe, holding the highest generation recorded for the- * recipe's current uid. A pass whose generation is at or below that value is- * skipped: a queued pass still carrying an older object must not delete what- * a newer generation provisioned. A different uid is a recipe recreated under- * the same name, whose generations restart at 1, so it always deletes and- * replaces the entry even when the recipe DELETE event was missed.+ * One entry per recipe, holding the highest generation (the watermark) seen+ * for the recipe's current uid. A pass whose generation is below the+ * watermark is always skipped: a queued pass still carrying an older object+ * must not delete what a newer generation provisioned. A pass at the+ * watermark is skipped unless a token ADDED re-armed it. A different uid is a+ * recipe recreated under the same name, whose generations restart at 1, so it+ * always deletes and replaces the entry even when the recipe DELETE event was+ * missed.*- * `invalidateSecret` bumps a per-recipe epoch. A Secret delete is recorded- * only when the epoch is unchanged since before the DELETE was sent, so an- * ADDED handled while the DELETE is in flight is not overwritten by it.+ * The Secret watermark rises on a recorded delete and on+ * `noteSecretProvisioned`, which the backgroundAccess branch calls for the+ * generation that wants the token.+ *+ * `invalidateSecret` (a token ADDED) keeps the uid and the watermark and+ * re-arms the delete only for passes at or above the watermark. It also bumps+ * a per-recipe epoch: a Secret delete is recorded only when the epoch is+ * unchanged since before the DELETE was sent, so an ADDED handled while the+ * DELETE is in flight is not overwritten by it.*/export const OAUTH_BROKER_NP_TTL_MS = 60 * 60 * 1000@@ -25,12 +33,17 @@ export interface OAuthBrokerLedgerRecipe {generation?: number}-interface SecretEntry {+interface WatermarkEntry {uid: string | undefinedgeneration: number}-interface PolicyEntry extends SecretEntry {+interface SecretEntry extends WatermarkEntry {+ /** A token ADDED landed after the watermark was set. */+ rearmed: boolean+}++interface PolicyEntry extends WatermarkEntry {expiresAt: number}@@ -38,7 +51,7 @@ function normalizeGeneration(generation: number | undefined): number {return generation ?? 0}-function coversPass(entry: SecretEntry | undefined, recipe: OAuthBrokerLedgerRecipe): boolean {+function coversPass(entry: WatermarkEntry | undefined, recipe: OAuthBrokerLedgerRecipe): boolean {return (entry !== undefined &&entry.uid === recipe.uid &&@@ -46,7 +59,10 @@ function coversPass(entry: SecretEntry | undefined, recipe: OAuthBrokerLedgerRec)}-function nextEntry(entry: SecretEntry | undefined, recipe: OAuthBrokerLedgerRecipe): SecretEntry {+function nextEntry(+ entry: WatermarkEntry | undefined,+ recipe: OAuthBrokerLedgerRecipe+): WatermarkEntry {const generation = normalizeGeneration(recipe.generation)if (entry !== undefined && entry.uid === recipe.uid) {return { uid: recipe.uid, generation: Math.max(entry.generation, generation) }@@ -61,7 +77,11 @@ export class OAuthBrokerDeleteLedger {private epochClock = 0shouldDeleteSecret(recipe: OAuthBrokerLedgerRecipe): boolean {- return !coversPass(this.secrets.get(recipe.name), recipe)+ const entry = this.secrets.get(recipe.name)+ if (entry === undefined || entry.uid !== recipe.uid) return true+ const generation = normalizeGeneration(recipe.generation)+ if (generation !== entry.generation) return generation > entry.generation+ return entry.rearmed}/** Read before sending the DELETE and hand back to `recordSecretDelete`. */@@ -75,10 +95,25 @@ export class OAuthBrokerDeleteLedger {*/recordSecretDelete(recipe: OAuthBrokerLedgerRecipe, epochBeforeDelete: number): boolean {if (this.secretEpoch(recipe.name) !== epochBeforeDelete) return false- this.secrets.set(recipe.name, nextEntry(this.secrets.get(recipe.name), recipe))+ this.raiseSecretWatermark(recipe)return true}+ /**+ * The recipe's generation wants the token (backgroundAccess), so no pass at+ * or below it may delete the Secret, even after a later token ADDED.+ */+ noteSecretProvisioned(recipe: OAuthBrokerLedgerRecipe): void {+ this.raiseSecretWatermark(recipe)+ }++ private raiseSecretWatermark(recipe: OAuthBrokerLedgerRecipe): void {+ const entry = this.secrets.get(recipe.name)+ const generation = normalizeGeneration(recipe.generation)+ if (entry !== undefined && entry.uid === recipe.uid && generation < entry.generation) return+ this.secrets.set(recipe.name, { uid: recipe.uid, generation, rearmed: false })+ }+shouldDeletePolicy(recipe: OAuthBrokerLedgerRecipe, nowMs = Date.now()): boolean {const entry = this.policies.get(recipe.name)if (entry === undefined || !coversPass(entry, recipe)) return true@@ -93,13 +128,15 @@ export class OAuthBrokerDeleteLedger {}/**- * A recreated token Secret re-arms only the Secret side. The NetworkPolicy- * TTL is not tied to that Secret, and a watch reconnect replays ADDED for- * every existing token, so clearing the policy side here would cost one- * extra DELETE per recipe after each reconnect.+ * A recreated token Secret re-arms only the Secret side, and only for+ * passes at or above the watermark; the uid and watermark are kept. The+ * NetworkPolicy TTL is not tied to that Secret, and a watch reconnect+ * replays ADDED for every existing token, so clearing the policy side here+ * would cost one extra DELETE per recipe after each reconnect.*/invalidateSecret(recipeName: string): void {- this.secrets.delete(recipeName)+ const entry = this.secrets.get(recipeName)+ if (entry !== undefined) entry.rearmed = truethis.epochClock += 1this.secretEpochs.set(recipeName, this.epochClock)}diff --git a/workflow-recipes/src/reconciler/reconciler.test.ts b/workflow-recipes/src/reconciler/reconciler.test.tsindex 9f0346828..4cdaf050a 100644--- a/workflow-recipes/src/reconciler/reconciler.test.ts+++ b/workflow-recipes/src/reconciler/reconciler.test.ts@@ -15681,6 +15681,70 @@ describe('WorkflowRecipeReconciler', () => {expect(secretDeletes()).toBe(2)})+ it('R2-L1: a stale pass of an older generation does not delete the token a newer one provisioned', async () => {+ const issue = vi+ .spyOn(brokerIssuer, 'issueOAuthBrokerToken')+ .mockResolvedValue({ brokerToken: 'issued-token' } as Awaited<+ ReturnType<typeof brokerIssuer.issueOAuthBrokerToken>+ >)+ const infoSpy = captureLogger('info')+ try {+ // gen4 has no backgroundAccess: the Secret is reaped and recorded.+ await reconciler.ensureOAuthBrokerTokenSecret(reapRecipe(4))+ expect(secretDeletes()).toBe(1)++ // gen5 turns backgroundAccess on and the token is issued.+ const withBackgroundAccess = makeRecipe({+ metadata: {+ name: 'test-recipe',+ namespace: 'sandbox-recipes',+ uid: 'uid-123',+ generation: 5,+ },+ spec: {+ workloads: [+ { id: 'app', type: 'deployment', image: 'nginx:1.30.1-alpine', port: 8080 },+ ],+ oauthClients: [+ {+ id: 'gmail',+ provider: 'google',+ clientIdRef: { name: 'creds', key: 'client-id' },+ clientSecretRef: { name: 'creds', key: 'client-secret' },+ scopes: ['https://www.googleapis.com/auth/gmail.readonly'],+ backgroundAccess: true,+ },+ ],+ },+ })+ mockCoreApi.readNamespacedSecret.mockRejectedValueOnce({ code: 404 })+ await reconciler.ensureOAuthBrokerTokenSecret(withBackgroundAccess)+ // Witness that the issuance branch ran for gen5.+ expect(issue).toHaveBeenCalledTimes(1)+ expect(+ mockCoreApi.createNamespacedSecret.mock.calls.filter(+ ([arg]) => arg.body?.metadata?.name === SECRET_NAME+ )+ ).toHaveLength(1)++ // The token's watch ADDED, then a queued pass still carrying gen4.+ reconciler.invalidateOAuthBrokerSecretLedger('test-recipe')+ await reconciler.ensureOAuthBrokerTokenSecret(reapRecipe(4))+ expect(secretDeletes()).toBe(1)+ expect(infoSpy).toHaveBeenCalledWith(+ expect.stringContaining('Skipping oauth-broker-token delete'),+ expect.objectContaining({ recipe: 'test-recipe', generation: 4 })+ )++ // Liveness witness: a newer generation that drops backgroundAccess reaps it.+ await reconciler.ensureOAuthBrokerTokenSecret(reapRecipe(6))+ expect(secretDeletes()).toBe(2)+ } finally {+ infoSpy.mockRestore()+ issue.mockRestore()+ }+ })+describe.each([['active', 'running'],['terminal', 'completed'],diff --git a/workflow-recipes/src/reconciler/workflowRecipeReconciler.ts b/workflow-recipes/src/reconciler/workflowRecipeReconciler.tsindex 51858a580..e99e34ad3 100644--- a/workflow-recipes/src/reconciler/workflowRecipeReconciler.ts+++ b/workflow-recipes/src/reconciler/workflowRecipeReconciler.ts@@ -7584,6 +7584,10 @@ export class WorkflowRecipeReconciler {return}+ // This generation wants the token: a queued pass still carrying an older+ // generation without backgroundAccess must not delete it after its ADDED.+ this.oauthBrokerDeleteLedger.noteSecretProvisioned(recipe.metadata)+let existing: k8s.V1Secret | null = nulltry {existing = await this.coreApi.readNamespacedSecret({ name: secretName, namespace: ns })