GeezerVison is showing github.com — fix(relay): apply mint timeout across whole connect operation · pingdotgg/t3code@e7dc492
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/environments/EnvironmentConnector.test.ts +107-1Lines changed: 107 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,10 @@ import * as NodeCrypto from "node:crypto"; | 2 | 2 | import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; | 3 | 3 |
| 4 | 4 | import { | | 5 | + RelayApi, | | 6 | + RelayClientPrincipal, | | 7 | + RelayDpopClientAuth, | | 8 | + RelayEnvironmentConnectRequest, | 5 | 9 | RelayCloudEnvironmentHealthRequest, | 6 | 10 | RelayCloudMintCredentialRequest, | 7 | 11 | RelayCloudEnvironmentHealthProofPayload, | @@ -25,12 +29,23 @@ import * as Result from "effect/Result"; | 25 | 29 | import * as Schema from "effect/Schema"; | 26 | 30 | import * as TestClock from "effect/testing/TestClock"; | 27 | 31 | import * as Tracer from "effect/Tracer"; | 28 | | -import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; | | 32 | +import { | | 33 | + HttpClient, | | 34 | + HttpClientRequest, | | 35 | + HttpClientResponse, | | 36 | + HttpRouter, | | 37 | + HttpServer, | | 38 | + HttpServerRequest, | | 39 | + HttpServerResponse, | | 40 | +} from "effect/unstable/http"; | | 41 | +import { HttpApi, HttpApiBuilder } from "effect/unstable/httpapi"; | 29 | 42 |
| 30 | 43 | import * as EnvironmentLinks from "./EnvironmentLinks.ts"; | 31 | 44 | import * as RelayConfiguration from "../Config.ts"; | 32 | 45 | import * as EnvironmentConnector from "./EnvironmentConnector.ts"; | 33 | 46 | import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts"; | | 47 | +import * as DpopProofs from "../auth/DpopProofs.ts"; | | 48 | +import { dpopClientApi, RELAY_REQUEST_DEADLINE_MS, traceRelayHttpRequest } from "../http/Api.ts"; | 34 | 49 |
| 35 | 50 | const cloudKeyPair = NodeCrypto.generateKeyPairSync("ed25519", { | 36 | 51 | privateKeyEncoding: { format: "pem", type: "pkcs8" }, | @@ -53,6 +68,9 @@ const decodeHealthRequestBody = Schema.decodeUnknownSync( | 53 | 68 | const decodeMintRequestBody = Schema.decodeUnknownSync( | 54 | 69 | Schema.fromJsonString(RelayCloudMintCredentialRequest), | 55 | 70 | ); | | 71 | +const encodeConnectRequestBody = Schema.encodeSync( | | 72 | + Schema.fromJsonString(RelayEnvironmentConnectRequest), | | 73 | +); | 56 | 74 | const isEnvironmentConnectNotAuthorized = Schema.is( | 57 | 75 | EnvironmentConnector.EnvironmentConnectNotAuthorized, | 58 | 76 | ); | @@ -783,6 +801,94 @@ describe("EnvironmentConnector", () => { | 783 | 801 | }).pipe(Effect.provide(connectorTestLayer(execute))); | 784 | 802 | }); | 785 | 803 |
| | 804 | + for (const stalledStep of ["proof", "link", "allocation", "mint"] as const) { | | 805 | + it.effect(`preserves the endpoint timeout under the relay deadline during ${stalledStep}`, () => | | 806 | + Effect.gen(function* () { | | 807 | + const proofStarted = yield* Deferred.make<void>(); | | 808 | + const releaseProof = yield* Deferred.make<void>(); | | 809 | + const stepStarted = yield* Deferred.make<void>(); | | 810 | + const interrupted = yield* Deferred.make<void>(); | | 811 | + const stall = Deferred.succeed(stepStarted, undefined).pipe( | | 812 | + Effect.andThen(Effect.never), | | 813 | + Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)), | | 814 | + ); | | 815 | + const links = makeLinks(); | | 816 | + const allocations = makeAllocations(); | | 817 | + const handlers = dpopClientApi.pipe( | | 818 | + Layer.provide( | | 819 | + connectorTestLayer(() => stall, { | | 820 | + links: { | | 821 | + ...links, | | 822 | + getForUser: (input) => (stalledStep === "link" ? stall : links.getForUser(input)), | | 823 | + }, | | 824 | + allocations: { | | 825 | + ...allocations, | | 826 | + get: (input) => (stalledStep === "allocation" ? stall : allocations.get(input)), | | 827 | + }, | | 828 | + }), | | 829 | + ), | | 830 | + Layer.provide( | | 831 | + Layer.mock(DpopProofs.DpopProofReplay, { | | 832 | + verifyAndConsume: () => | | 833 | + Deferred.succeed(proofStarted, undefined).pipe( | | 834 | + Effect.andThen(stalledStep === "proof" ? stall : Deferred.await(releaseProof)), | | 835 | + Effect.as("client-proof-key-thumbprint"), | | 836 | + ), | | 837 | + }), | | 838 | + ), | | 839 | + Layer.provideMerge( | | 840 | + Layer.succeed(RelayDpopClientAuth, { | | 841 | + relayDpop: (effect) => | | 842 | + Effect.provideService(effect, RelayClientPrincipal, { | | 843 | + userId: "user_123", | | 844 | + token: "test-token", | | 845 | + proofKeyThumbprint: "client-proof-key-thumbprint", | | 846 | + dpopScopes: ["environment:connect"], | | 847 | + }), | | 848 | + }), | | 849 | + ), | | 850 | + ); | | 851 | + const httpEffect = yield* HttpRouter.toHttpEffect( | | 852 | + HttpApiBuilder.layer(HttpApi.make("RelayApi").add(RelayApi.groups.dpopClient)).pipe( | | 853 | + Layer.provide(handlers), | | 854 | + ), | | 855 | + ); | | 856 | + const fiber = yield* traceRelayHttpRequest(httpEffect).pipe( | | 857 | + Effect.provideService( | | 858 | + HttpServerRequest.HttpServerRequest, | | 859 | + HttpServerRequest.fromWeb( | | 860 | + new Request("https://relay.example.test/v1/environments/env-connector-test/connect", { | | 861 | + method: "POST", | | 862 | + headers: { | | 863 | + authorization: "DPoP test-token", | | 864 | + dpop: "test-proof", | | 865 | + "content-type": "application/json", | | 866 | + }, | | 867 | + body: encodeConnectRequestBody({ | | 868 | + clientProofKeyThumbprint: "client-proof-key-thumbprint", | | 869 | + }), | | 870 | + }), | | 871 | + ), | | 872 | + ), | | 873 | + Effect.forkScoped, | | 874 | + ); | | 875 | + yield* Deferred.await(proofStarted); | | 876 | + yield* TestClock.adjust(Duration.seconds(3)); | | 877 | + if (stalledStep !== "proof") { | | 878 | + yield* Deferred.succeed(releaseProof, undefined); | | 879 | + } | | 880 | + yield* Deferred.await(stepStarted); | | 881 | + yield* TestClock.adjust(Duration.millis(RELAY_REQUEST_DEADLINE_MS - 3_000)); | | 882 | + const response = yield* Fiber.join(fiber); | | 883 | + const body = yield* Effect.promise(() => HttpServerResponse.toWeb(response).json()); | | 884 | + | | 885 | + expect(response.status).toBe(504); | | 886 | + expect(body).toMatchObject({ code: "environment_endpoint_timed_out" }); | | 887 | + expect(yield* Deferred.isDone(interrupted)).toBe(true); | | 888 | + }).pipe(Effect.provide(HttpServer.layerServices), Effect.scoped), | | 889 | + ); | | 890 | + } | | 891 | + | 786 | 892 | it.effect("times out hung managed endpoint mint requests", () => { | 787 | 893 | let resolveRequestStarted: (() => void) | undefined; | 788 | 894 | const requestStarted = new Promise<void>((resolve) => { | |
Collapse file Copy file name to clipboardExpand all lines: infra/relay/src/environments/EnvironmentConnector.ts +116-109Lines changed: 116 additions & 109 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,6 +132,23 @@ export type EnvironmentConnectorError = | 132 | 132 | export const ENVIRONMENT_MINT_REQUEST_TIMEOUT_MS = 7_000; | 133 | 133 | const ENVIRONMENT_HEALTH_CLOCK_SKEW_MILLIS = 60 * 1_000; | 134 | 134 |
| | 135 | +export const withEnvironmentMintTimeout = <A, E, R>( | | 136 | + effect: Effect.Effect<A, E, R>, | | 137 | + environmentId: string, | | 138 | +) => | | 139 | + effect.pipe( | | 140 | + Effect.timeoutOrElse({ | | 141 | + duration: Duration.millis(ENVIRONMENT_MINT_REQUEST_TIMEOUT_MS), | | 142 | + orElse: () => | | 143 | + Effect.fail( | | 144 | + new EnvironmentMintRequestTimedOut({ | | 145 | + environmentId, | | 146 | + timeoutMs: ENVIRONMENT_MINT_REQUEST_TIMEOUT_MS, | | 147 | + }), | | 148 | + ), | | 149 | + }), | | 150 | + ); | | 151 | + | 135 | 152 | export class EnvironmentConnector extends Context.Service< | 136 | 153 | EnvironmentConnector, | 137 | 154 | { | @@ -541,56 +558,43 @@ const make = Effect.gen(function* () { | 541 | 558 | descriptor: decoded.descriptor, | 542 | 559 | }; | 543 | 560 | }), | 544 | | - connect: Effect.fn("relay.environment_connector.connect")(function* (input) { | 545 | | - yield* Effect.annotateCurrentSpan({ | 546 | | - "relay.environment_id": input.environmentId, | 547 | | - "relay.operation": "connect", | 548 | | - "relay.connect.has_device_id": input.deviceId !== undefined, | 549 | | - ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), | 550 | | - }); | 551 | | - if (input.clientProofKeyThumbprint.trim().length === 0) { | 552 | | - return yield* new EnvironmentConnectNotAuthorized({ | 553 | | - environmentId: input.environmentId, | 554 | | - operation: "connect", | 555 | | - reason: "client_proof_key_thumbprint_missing", | | 561 | + connect: Effect.fn("relay.environment_connector.connect")( | | 562 | + function* (input) { | | 563 | + yield* Effect.annotateCurrentSpan({ | | 564 | + "relay.environment_id": input.environmentId, | | 565 | + "relay.operation": "connect", | | 566 | + "relay.connect.has_device_id": input.deviceId !== undefined, | | 567 | + ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), | 556 | 568 | }); | 557 | | - } | 558 | | - const { link, allocation } = yield* Effect.all( | 559 | | - { | 560 | | - link: links.getForUser(input), | 561 | | - allocation: allocations.get(input), | 562 | | - }, | 563 | | - { concurrency: 2 }, | 564 | | - ); | 565 | | - if (!link) { | 566 | | - return yield* new EnvironmentConnectNotAuthorized({ | 567 | | - environmentId: input.environmentId, | | 569 | + if (input.clientProofKeyThumbprint.trim().length === 0) { | | 570 | + return yield* new EnvironmentConnectNotAuthorized({ | | 571 | + environmentId: input.environmentId, | | 572 | + operation: "connect", | | 573 | + reason: "client_proof_key_thumbprint_missing", | | 574 | + }); | | 575 | + } | | 576 | + const { link, allocation } = yield* Effect.all( | | 577 | + { | | 578 | + link: links.getForUser(input), | | 579 | + allocation: allocations.get(input), | | 580 | + }, | | 581 | + { concurrency: 2 }, | | 582 | + ); | | 583 | + if (!link) { | | 584 | + return yield* new EnvironmentConnectNotAuthorized({ | | 585 | + environmentId: input.environmentId, | | 586 | + operation: "connect", | | 587 | + reason: "environment_link_not_found", | | 588 | + }); | | 589 | + } | | 590 | + const endpoint = yield* resolveManagedEndpoint({ | 568 | 591 | operation: "connect", | 569 | | - reason: "environment_link_not_found", | | 592 | + link, | | 593 | + allocation, | 570 | 594 | }); | 571 | | - } | 572 | | - const endpoint = yield* resolveManagedEndpoint({ | 573 | | - operation: "connect", | 574 | | - link, | 575 | | - allocation, | 576 | | - }); | 577 | | - const now = yield* DateTime.now; | 578 | | - const expiresAt = DateTime.add(now, { minutes: 2 }); | 579 | | - const nonce = yield* crypto.randomUUIDv4.pipe( | 580 | | - Effect.mapError( | 581 | | - (cause) => | 582 | | - new EnvironmentMintRequestFailed({ | 583 | | - environmentId: input.environmentId, | 584 | | - operation: "connect", | 585 | | - cause, | 586 | | - }), | 587 | | - ), | 588 | | - ); | 589 | | - const payload = { | 590 | | - iss: relayIssuer, | 591 | | - aud: `t3-env:${link.environmentId}`, | 592 | | - sub: input.userId, | 593 | | - jti: yield* crypto.randomUUIDv4.pipe( | | 595 | + const now = yield* DateTime.now; | | 596 | + const expiresAt = DateTime.add(now, { minutes: 2 }); | | 597 | + const nonce = yield* crypto.randomUUIDv4.pipe( | 594 | 598 | Effect.mapError( | 595 | 599 | (cause) => | 596 | 600 | new EnvironmentMintRequestFailed({ | @@ -599,35 +603,35 @@ const make = Effect.gen(function* () { | 599 | 603 | cause, | 600 | 604 | }), | 601 | 605 | ), | 602 | | - ), | 603 | | - iat: Math.floor(now.epochMilliseconds / 1_000), | 604 | | - exp: Math.floor(expiresAt.epochMilliseconds / 1_000), | 605 | | - environmentId: link.environmentId, | 606 | | - clientProofKeyThumbprint: input.clientProofKeyThumbprint, | 607 | | - cnf: { jkt: input.clientProofKeyThumbprint }, | 608 | | - ...(input.deviceId ? { deviceId: input.deviceId } : {}), | 609 | | - nonce, | 610 | | - scope: ["environment:connect"], | 611 | | - } satisfies RelayCloudMintCredentialProofPayload; | 612 | | - const proof = yield* signRelayJwt({ | 613 | | - privateKey: Redacted.value(settings.cloudMintPrivateKey), | 614 | | - typ: RELAY_MINT_REQUEST_TYP, | 615 | | - payload, | 616 | | - }).pipe( | 617 | | - Effect.mapError( | 618 | | - (cause) => | 619 | | - new EnvironmentMintRequestFailed({ | 620 | | - environmentId: input.environmentId, | 621 | | - operation: "connect", | 622 | | - cause, | 623 | | - }), | 624 | | - ), | 625 | | - ); | 626 | | - const environmentClient = yield* makeEnvironmentClient(endpoint.httpBaseUrl); | 627 | | - const decoded = yield* environmentClient.connect | 628 | | - .t3MintCredential({ payload: { proof } }) | 629 | | - .pipe( | 630 | | - withoutRedirects, | | 606 | + ); | | 607 | + const payload = { | | 608 | + iss: relayIssuer, | | 609 | + aud: `t3-env:${link.environmentId}`, | | 610 | + sub: input.userId, | | 611 | + jti: yield* crypto.randomUUIDv4.pipe( | | 612 | + Effect.mapError( | | 613 | + (cause) => | | 614 | + new EnvironmentMintRequestFailed({ | | 615 | + environmentId: input.environmentId, | | 616 | + operation: "connect", | | 617 | + cause, | | 618 | + }), | | 619 | + ), | | 620 | + ), | | 621 | + iat: Math.floor(now.epochMilliseconds / 1_000), | | 622 | + exp: Math.floor(expiresAt.epochMilliseconds / 1_000), | | 623 | + environmentId: link.environmentId, | | 624 | + clientProofKeyThumbprint: input.clientProofKeyThumbprint, | | 625 | + cnf: { jkt: input.clientProofKeyThumbprint }, | | 626 | + ...(input.deviceId ? { deviceId: input.deviceId } : {}), | | 627 | + nonce, | | 628 | + scope: ["environment:connect"], | | 629 | + } satisfies RelayCloudMintCredentialProofPayload; | | 630 | + const proof = yield* signRelayJwt({ | | 631 | + privateKey: Redacted.value(settings.cloudMintPrivateKey), | | 632 | + typ: RELAY_MINT_REQUEST_TYP, | | 633 | + payload, | | 634 | + }).pipe( | 631 | 635 | Effect.mapError( | 632 | 636 | (cause) => | 633 | 637 | new EnvironmentMintRequestFailed({ | @@ -636,42 +640,45 @@ const make = Effect.gen(function* () { | 636 | 640 | cause, | 637 | 641 | }), | 638 | 642 | ), | 639 | | - Effect.timeoutOption(Duration.millis(ENVIRONMENT_MINT_REQUEST_TIMEOUT_MS)), | 640 | | - Effect.flatMap( | 641 | | - Option.match({ | 642 | | - onNone: () => | 643 | | - Effect.fail( | 644 | | - new EnvironmentMintRequestTimedOut({ | 645 | | - environmentId: input.environmentId, | 646 | | - timeoutMs: ENVIRONMENT_MINT_REQUEST_TIMEOUT_MS, | 647 | | - }), | 648 | | - ), | 649 | | - onSome: Effect.succeed, | 650 | | - }), | 651 | | - ), | 652 | 643 | ); | 653 | | - const verified = yield* verifyEnvironmentResponse({ | 654 | | - response: decoded, | 655 | | - environmentId: input.environmentId, | 656 | | - requestNonce: nonce, | 657 | | - clientProofKeyThumbprint: input.clientProofKeyThumbprint, | 658 | | - environmentPublicKeys: [link.environmentPublicKey], | 659 | | - relayIssuer, | 660 | | - nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000), | 661 | | - }); | 662 | | - if (!verified) { | 663 | | - return yield* new EnvironmentMintResponseInvalid({ | | 644 | + const environmentClient = yield* makeEnvironmentClient(endpoint.httpBaseUrl); | | 645 | + const decoded = yield* environmentClient.connect | | 646 | + .t3MintCredential({ payload: { proof } }) | | 647 | + .pipe( | | 648 | + withoutRedirects, | | 649 | + Effect.mapError( | | 650 | + (cause) => | | 651 | + new EnvironmentMintRequestFailed({ | | 652 | + environmentId: input.environmentId, | | 653 | + operation: "connect", | | 654 | + cause, | | 655 | + }), | | 656 | + ), | | 657 | + ); | | 658 | + const verified = yield* verifyEnvironmentResponse({ | | 659 | + response: decoded, | 664 | 660 | environmentId: input.environmentId, | 665 | | - operation: "connect", | | 661 | + requestNonce: nonce, | | 662 | + clientProofKeyThumbprint: input.clientProofKeyThumbprint, | | 663 | + environmentPublicKeys: [link.environmentPublicKey], | | 664 | + relayIssuer, | | 665 | + nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000), | 666 | 666 | }); | 667 | | - } | 668 | | - return { | 669 | | - environmentId: link.environmentId, | 670 | | - endpoint, | 671 | | - credential: decoded.credential, | 672 | | - expiresAt: decoded.expiresAt, | 673 | | - }; | 674 | | - }), | | 667 | + if (!verified) { | | 668 | + return yield* new EnvironmentMintResponseInvalid({ | | 669 | + environmentId: input.environmentId, | | 670 | + operation: "connect", | | 671 | + }); | | 672 | + } | | 673 | + return { | | 674 | + environmentId: link.environmentId, | | 675 | + endpoint, | | 676 | + credential: decoded.credential, | | 677 | + expiresAt: decoded.expiresAt, | | 678 | + }; | | 679 | + }, | | 680 | + (effect, input) => withEnvironmentMintTimeout(effect, input.environmentId), | | 681 | + ), | 675 | 682 | }); | 676 | 683 | }); | 677 | 684 |
| |
|