GeezerVison is showing github.com — Fix relay credential lookup for unlinked environments (#4692) · pingdotgg/t3code@e00781a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/db.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ import * as RemovalPolicy from "alchemy/RemovalPolicy"; | 7 | 7 | import type { EffectPgDatabase } from "drizzle-orm/effect-postgres"; | 8 | 8 | import * as Context from "effect/Context"; | 9 | 9 | import * as Effect from "effect/Effect"; | | 10 | +import * as Layer from "effect/Layer"; | 10 | 11 |
| 11 | 12 | import { relayDatabaseMode } from "./dbConfig.ts"; | 12 | 13 |
| @@ -17,6 +18,23 @@ export class RelayDb extends Context.Service< | 17 | 18 | } | 18 | 19 | >()("t3code-relay/db/RelayDb") {} | 19 | 20 |
| | 21 | +export class RelayTransactions extends Context.Service< | | 22 | + RelayTransactions, | | 23 | + { | | 24 | + readonly withTransaction: RelayDb["Service"]["$client"]["withTransaction"]; | | 25 | + } | | 26 | +>()("t3code-relay/db/RelayTransactions") { | | 27 | + static readonly layer = Layer.effect( | | 28 | + RelayTransactions, | | 29 | + Effect.gen(function* () { | | 30 | + const db = yield* RelayDb; | | 31 | + return RelayTransactions.of({ | | 32 | + withTransaction: db.$client.withTransaction, | | 33 | + }); | | 34 | + }), | | 35 | + ); | | 36 | +} | | 37 | + | 20 | 38 | export const PlanetscaleDatabase = Effect.gen(function* () { | 21 | 39 | const { stage } = yield* Alchemy.Stack; | 22 | 40 | const schema = yield* Drizzle.Schema("RelaySchema", { | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/EnvironmentConnector.test.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -198,7 +198,9 @@ function makeAllocations( | 198 | 198 | recordDns: () => Effect.die("unused"), | 199 | 199 | markReady: () => Effect.die("unused"), | 200 | 200 | claimRelease: () => Effect.die("unused"), | | 201 | + claimDeprovision: () => Effect.die("unused"), | 201 | 202 | remove: () => Effect.die("unused"), | | 203 | + removeClaimed: () => Effect.die("unused"), | 202 | 204 | }; | 203 | 205 | } | 204 | 206 |
| |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/EnvironmentCredentials.test.ts +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; | 2 | 2 | import { describe, expect, it } from "@effect/vitest"; | 3 | | -import { PgDialect, QueryBuilder } from "drizzle-orm/pg-core"; | | 3 | +import { PgDialect } from "drizzle-orm/pg-core"; | 4 | 4 | import * as Effect from "effect/Effect"; | 5 | 5 | import * as Layer from "effect/Layer"; | 6 | 6 |
| @@ -58,13 +58,17 @@ describe("EnvironmentCredentials", () => { | 58 | 58 | it.effect("does not retain credential tokens when lookup persistence fails", () => { | 59 | 59 | const cause = new Error("database unavailable"); | 60 | 60 | const token = "t3env_sensitive-credential-token"; | | 61 | + const whereConditions: Array<unknown> = []; | 61 | 62 | const fakeDb = { | 62 | 63 | select: () => ({ | 63 | 64 | from: (table: unknown) => { | 64 | 65 | expect(table).toBe(relayEnvironmentCredentials); | 65 | 66 | return { | 66 | | - where: () => ({ | 67 | | - limit: () => Effect.fail(cause), | | 67 | + where: (condition: unknown) => ({ | | 68 | + limit: () => { | | 69 | + whereConditions.push(condition); | | 70 | + return Effect.fail(cause); | | 71 | + }, | 68 | 72 | }), | 69 | 73 | }; | 70 | 74 | }, | @@ -81,6 +85,13 @@ describe("EnvironmentCredentials", () => { | 81 | 85 | }); | 82 | 86 | expect(error.cause).toBe(cause); | 83 | 87 | expect(error).not.toHaveProperty("token"); | | 88 | + expect(whereConditions).toHaveLength(1); | | 89 | + | | 90 | + const query = new PgDialect().sqlToQuery(whereConditions[0] as never); | | 91 | + expect(query.sql).toContain("exists"); | | 92 | + expect(query.sql).toContain('"relay_environment_links"."environment_id"'); | | 93 | + expect(query.sql).toContain('"relay_environment_links"."environment_public_key"'); | | 94 | + expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null'); | 84 | 95 | }).pipe( | 85 | 96 | Effect.provide( | 86 | 97 | EnvironmentCredentials.layer.pipe( | @@ -180,7 +191,6 @@ describe("EnvironmentCredentials", () => { | 180 | 191 | const updateValues: Array<Record<string, unknown>> = []; | 181 | 192 | const whereConditions: Array<unknown> = []; | 182 | 193 | const fakeDb = { | 183 | | - select: (fields: Parameters<QueryBuilder["select"]>[0]) => new QueryBuilder().select(fields), | 184 | 194 | update: (table: unknown) => { | 185 | 195 | expect(table).toBe(relayEnvironmentCredentials); | 186 | 196 | return { | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/EnvironmentCredentials.ts +21-2Lines changed: 21 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,8 @@ import * as Encoding from "effect/Encoding"; | 6 | 6 | import * as Layer from "effect/Layer"; | 7 | 7 | import * as Option from "effect/Option"; | 8 | 8 | import * as Schema from "effect/Schema"; | 9 | | -import { and, eq, isNull, ne, notExists } from "drizzle-orm"; | | 9 | +import { and, eq, exists, isNull, ne, notExists } from "drizzle-orm"; | | 10 | +import { QueryBuilder } from "drizzle-orm/pg-core"; | 10 | 11 |
| 11 | 12 | import * as RelayDb from "../db.ts"; | 12 | 13 | import { relayEnvironmentCredentials, relayEnvironmentLinks } from "../persistence/schema.ts"; | @@ -196,6 +197,24 @@ const make = Effect.gen(function* () { | 196 | 197 | and( | 197 | 198 | eq(relayEnvironmentCredentials.credentialHash, credentialHash), | 198 | 199 | isNull(relayEnvironmentCredentials.revokedAt), | | 200 | + exists( | | 201 | + new QueryBuilder() | | 202 | + .select({ userId: relayEnvironmentLinks.userId }) | | 203 | + .from(relayEnvironmentLinks) | | 204 | + .where( | | 205 | + and( | | 206 | + eq( | | 207 | + relayEnvironmentLinks.environmentId, | | 208 | + relayEnvironmentCredentials.environmentId, | | 209 | + ), | | 210 | + eq( | | 211 | + relayEnvironmentLinks.environmentPublicKey, | | 212 | + relayEnvironmentCredentials.environmentPublicKey, | | 213 | + ), | | 214 | + isNull(relayEnvironmentLinks.revokedAt), | | 215 | + ), | | 216 | + ), | | 217 | + ), | 199 | 218 | ), | 200 | 219 | ) | 201 | 220 | .limit(1) | @@ -238,7 +257,7 @@ const make = Effect.gen(function* () { | 238 | 257 | eq(relayEnvironmentCredentials.environmentPublicKey, input.environmentPublicKey), | 239 | 258 | isNull(relayEnvironmentCredentials.revokedAt), | 240 | 259 | notExists( | 241 | | - db | | 260 | + new QueryBuilder() | 242 | 261 | .select({ userId: relayEnvironmentLinks.userId }) | 243 | 262 | .from(relayEnvironmentLinks) | 244 | 263 | .where( | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/EnvironmentLinker.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,6 +136,7 @@ function testLayer(input?: { | 136 | 136 | revokeForEnvironmentPublicKey: () => Effect.succeed(false), | 137 | 137 | }), | 138 | 138 | Layer.succeed(ManagedEndpointProvider.ManagedEndpointProvider, { | | 139 | + prepareDeprovision: () => Effect.succeed(null), | 139 | 140 | deprovision: input?.deprovision ?? (() => Effect.void), | 140 | 141 | release: () => Effect.succeed(true), | 141 | 142 | provision: () => | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/ManagedEndpointAllocations.test.ts +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,61 @@ const layerWithDb = (db: RelayDb.RelayDb["Service"]) => | 10 | 10 | ManagedEndpointAllocations.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db))); | 11 | 11 |
| 12 | 12 | describe("ManagedEndpointAllocations", () => { | | 13 | + it.effect("returns a claim generation only when deprovision wins the allocation CAS", () => { | | 14 | + let claimedAt: string | undefined; | | 15 | + const fakeDb = { | | 16 | + update: (table: unknown) => { | | 17 | + expect(table).toBe(relayManagedEndpointAllocations); | | 18 | + return { | | 19 | + set: (values: { readonly updatedAt: string }) => { | | 20 | + claimedAt = values.updatedAt; | | 21 | + return { | | 22 | + where: () => ({ | | 23 | + returning: () => Effect.succeed([{ userId: "user-1" }]), | | 24 | + }), | | 25 | + }; | | 26 | + }, | | 27 | + }; | | 28 | + }, | | 29 | + } as unknown as RelayDb.RelayDb["Service"]; | | 30 | + | | 31 | + return Effect.gen(function* () { | | 32 | + const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations; | | 33 | + const generation = yield* allocations.claimDeprovision({ | | 34 | + userId: "user-1", | | 35 | + environmentId: "environment-1", | | 36 | + updatedAt: "captured-generation", | | 37 | + }); | | 38 | + | | 39 | + expect(generation).toBe(claimedAt); | | 40 | + expect(generation).not.toBeNull(); | | 41 | + }).pipe(Effect.provide(layerWithDb(fakeDb))); | | 42 | + }); | | 43 | + | | 44 | + it.effect("does not remove an allocation superseded after a deprovision claim", () => { | | 45 | + const fakeDb = { | | 46 | + delete: (table: unknown) => { | | 47 | + expect(table).toBe(relayManagedEndpointAllocations); | | 48 | + return { | | 49 | + where: () => ({ | | 50 | + returning: () => Effect.succeed([]), | | 51 | + }), | | 52 | + }; | | 53 | + }, | | 54 | + } as unknown as RelayDb.RelayDb["Service"]; | | 55 | + | | 56 | + return Effect.gen(function* () { | | 57 | + const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations; | | 58 | + expect( | | 59 | + yield* allocations.removeClaimed({ | | 60 | + userId: "user-1", | | 61 | + environmentId: "environment-1", | | 62 | + updatedAt: "outdated-claim-generation", | | 63 | + }), | | 64 | + ).toBe(false); | | 65 | + }).pipe(Effect.provide(layerWithDb(fakeDb))); | | 66 | + }); | | 67 | + | 13 | 68 | it.effect("retains database failures with allocation operation and identity", () => { | 14 | 69 | const cause = new Error("database unavailable"); | 15 | 70 | const fakeDb = { | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/ManagedEndpointAllocations.ts +78Lines changed: 78 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,7 +51,9 @@ export class ManagedEndpointAllocationPersistenceError extends Schema.TaggedErro | 51 | 51 | "record-dns", | 52 | 52 | "mark-ready", | 53 | 53 | "claim-release", | | 54 | + "claim-deprovision", | 54 | 55 | "remove", | | 56 | + "remove-claimed", | 55 | 57 | ]), | 56 | 58 | stage: Schema.Literals(["database-request", "resolve-reservation"]), | 57 | 59 | userId: Schema.String, | @@ -91,6 +93,14 @@ interface ClaimManagedEndpointReleaseInput extends ManagedEndpointAllocationKey | 91 | 93 | readonly updatedAt: string; | 92 | 94 | } | 93 | 95 |
| | 96 | +interface ClaimManagedEndpointDeprovisionInput extends ManagedEndpointAllocationKey { | | 97 | + readonly updatedAt: string; | | 98 | +} | | 99 | + | | 100 | +interface RemoveClaimedManagedEndpointAllocationInput extends ManagedEndpointAllocationKey { | | 101 | + readonly updatedAt: string; | | 102 | +} | | 103 | + | 94 | 104 | export class ManagedEndpointAllocations extends Context.Service< | 95 | 105 | ManagedEndpointAllocations, | 96 | 106 | { | @@ -119,9 +129,22 @@ export class ManagedEndpointAllocations extends Context.Service< | 119 | 129 | readonly claimRelease: ( | 120 | 130 | input: ClaimManagedEndpointReleaseInput, | 121 | 131 | ) => Effect.Effect<boolean, ManagedEndpointAllocationPersistenceError>; | | 132 | + /** | | 133 | + * Claims the complete allocation for teardown only if its generation still | | 134 | + * matches the snapshot captured by the unlink operation. | | 135 | + * | | 136 | + * Returns the claim generation used by `removeClaimed`, or null when a | | 137 | + * concurrent provision has already superseded the snapshot. | | 138 | + */ | | 139 | + readonly claimDeprovision: ( | | 140 | + input: ClaimManagedEndpointDeprovisionInput, | | 141 | + ) => Effect.Effect<string | null, ManagedEndpointAllocationPersistenceError>; | 122 | 142 | readonly remove: ( | 123 | 143 | input: ManagedEndpointAllocationKey, | 124 | 144 | ) => Effect.Effect<void, ManagedEndpointAllocationPersistenceError>; | | 145 | + readonly removeClaimed: ( | | 146 | + input: RemoveClaimedManagedEndpointAllocationInput, | | 147 | + ) => Effect.Effect<boolean, ManagedEndpointAllocationPersistenceError>; | 125 | 148 | } | 126 | 149 | >()("t3code-relay/environments/ManagedEndpointAllocations") {} | 127 | 150 |
| @@ -321,6 +344,35 @@ export const make = Effect.gen(function* () { | 321 | 344 | ); | 322 | 345 | return claimed; | 323 | 346 | }), | | 347 | + claimDeprovision: Effect.fn("relay.managed_endpoint_allocations.claim_deprovision")(function* ( | | 348 | + input: ClaimManagedEndpointDeprovisionInput, | | 349 | + ) { | | 350 | + const claimedAt = DateTime.formatIso(yield* DateTime.now); | | 351 | + const claimed = yield* db | | 352 | + .update(relayManagedEndpointAllocations) | | 353 | + .set({ updatedAt: claimedAt }) | | 354 | + .where( | | 355 | + and( | | 356 | + whereAllocation(input), | | 357 | + eq(relayManagedEndpointAllocations.updatedAt, input.updatedAt), | | 358 | + ), | | 359 | + ) | | 360 | + .returning({ userId: relayManagedEndpointAllocations.userId }) | | 361 | + .pipe( | | 362 | + Effect.map((rows) => rows.length > 0), | | 363 | + Effect.mapError( | | 364 | + (cause) => | | 365 | + new ManagedEndpointAllocationPersistenceError({ | | 366 | + operation: "claim-deprovision", | | 367 | + stage: "database-request", | | 368 | + userId: input.userId, | | 369 | + environmentId: input.environmentId, | | 370 | + cause, | | 371 | + }), | | 372 | + ), | | 373 | + ); | | 374 | + return claimed ? claimedAt : null; | | 375 | + }), | 324 | 376 | remove: Effect.fn("relay.managed_endpoint_allocations.remove")(function* ( | 325 | 377 | input: ManagedEndpointAllocationKey, | 326 | 378 | ) { | @@ -339,6 +391,32 @@ export const make = Effect.gen(function* () { | 339 | 391 | ), | 340 | 392 | ); | 341 | 393 | }), | | 394 | + removeClaimed: Effect.fn("relay.managed_endpoint_allocations.remove_claimed")(function* ( | | 395 | + input: RemoveClaimedManagedEndpointAllocationInput, | | 396 | + ) { | | 397 | + return yield* db | | 398 | + .delete(relayManagedEndpointAllocations) | | 399 | + .where( | | 400 | + and( | | 401 | + whereAllocation(input), | | 402 | + eq(relayManagedEndpointAllocations.updatedAt, input.updatedAt), | | 403 | + ), | | 404 | + ) | | 405 | + .returning({ userId: relayManagedEndpointAllocations.userId }) | | 406 | + .pipe( | | 407 | + Effect.map((rows) => rows.length > 0), | | 408 | + Effect.mapError( | | 409 | + (cause) => | | 410 | + new ManagedEndpointAllocationPersistenceError({ | | 411 | + operation: "remove-claimed", | | 412 | + stage: "database-request", | | 413 | + userId: input.userId, | | 414 | + environmentId: input.environmentId, | | 415 | + cause, | | 416 | + }), | | 417 | + ), | | 418 | + ); | | 419 | + }), | 342 | 420 | }); | 343 | 421 | }); | 344 | 422 |
| |
|