History

preserve relay API projection failures

Julius Marminge committed 4 months ago

Commit 986fe931 parent 39441ec2 files · +263 −25Browse files

Unified · Split

2 files
  1. infra/relay/src/http/Api.test.ts +171 −1
  2. infra/relay/src/http/Api.ts +92 −24
infra/relay/src/http/Api.test.ts+171−1
@@ -1,21 +1,30 @@
1import { createClerkClient, verifyToken } from "@clerk/backend";1import { createClerkClient, verifyToken } from "@clerk/backend";
2import { describe, expect, it } from "@effect/vitest";2import { describe, expect, it } from "@effect/vitest";
3import { vi } from "vite-plus/test";3import { vi } from "vite-plus/test";
4import * as Cause from "effect/Cause";
4import * as Context from "effect/Context";5import * as Context from "effect/Context";
5import * as Effect from "effect/Effect";6import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";7import * as Layer from "effect/Layer";
8import * as Logger from "effect/Logger";
7import * as Option from "effect/Option";9import * as Option from "effect/Option";
8import * as Predicate from "effect/Predicate";10import * as Predicate from "effect/Predicate";
9import * as Redacted from "effect/Redacted";11import * as Redacted from "effect/Redacted";
12import * as References from "effect/References";
10import * as Tracer from "effect/Tracer";13import * as Tracer from "effect/Tracer";
11import * as HttpRouter from "effect/unstable/http/HttpRouter";14import * as HttpRouter from "effect/unstable/http/HttpRouter";
12import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";15import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
13import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";16import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
14import { RelayEnvironmentAuth } from "@t3tools/contracts/relay";17import {
18 RelayEnvironmentAuth,
19 RelayEnvironmentConnectNotAuthorizedError,
20 RelayInternalError,
21} from "@t3tools/contracts/relay";
15 22
16import {23import {
17 ClerkBearerTokenVerificationError,24 ClerkBearerTokenVerificationError,
18 ClerkTokenVerificationError,25 ClerkTokenVerificationError,
26 mapErrorTags,
27 mapRelayCommonApiErrors,
19 relayCors,28 relayCors,
20 relayDocsRedirectRoute,29 relayDocsRedirectRoute,
21 relayEnvironmentAuthLayer,30 relayEnvironmentAuthLayer,
@@ -27,6 +36,7 @@ import { · 5 unmodified lines
27} from "./Api.ts";36} from "./Api.ts";
28import * as RelayConfiguration from "../Config.ts";37import * as RelayConfiguration from "../Config.ts";
29import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts";38import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts";
39import * as EnvironmentConnector from "../environments/EnvironmentConnector.ts";
30 40
31vi.mock("@clerk/backend", () => ({41vi.mock("@clerk/backend", () => ({
32 createClerkClient: vi.fn(),42 createClerkClient: vi.fn(),
@@ -52,6 +62,19 @@ const relaySettings: RelayConfiguration.RelayConfiguration["Service"] = { · 19 unmodified lines
52 managedEndpointNamespace: undefined,62 managedEndpointNamespace: undefined,
53};63};
54 64
65const projectionTraceId = "00000000000000000000000000000042";
66const projectionParentSpan = Tracer.externalSpan({
67 traceId: projectionTraceId,
68 spanId: "0000000000000042",
69 sampled: true,
70});
71
72interface CapturedLog {
73 readonly message: unknown;
74 readonly cause: Cause.Cause<unknown>;
75 readonly annotations: Readonly<Record<string, unknown>>;
76}
77
55describe("relay client authentication", () => {78describe("relay client authentication", () => {
56 it.effect("preserves the existing Clerk session JWT path", () =>79 it.effect("preserves the existing Clerk session JWT path", () =>
57 Effect.gen(function* () {80 Effect.gen(function* () {
@@ -242,6 +265,14 @@ describe("relay DPoP token exchange authentication", () => { · 184 unmodified lines
242 265
243describe("relay environment authentication", () => {266describe("relay environment authentication", () => {
244 it.effect("preserves credential lookup persistence failures as internal errors", () => {267 it.effect("preserves credential lookup persistence failures as internal errors", () => {
268 const logs: CapturedLog[] = [];
269 const logger = Logger.make(({ cause, fiber, message }) => {
270 logs.push({
271 message,
272 cause,
273 annotations: fiber.getRef(References.CurrentLogAnnotations),
274 });
275 });
245 const failure = new EnvironmentCredentials.EnvironmentCredentialAuthenticatePersistenceError({276 const failure = new EnvironmentCredentials.EnvironmentCredentialAuthenticatePersistenceError({
246 stage: "lookup-credential",277 stage: "lookup-credential",
247 cause: "database unavailable",278 cause: "database unavailable",
@@ -265,8 +296,25 @@ describe("relay environment authentication", () => { · 17 unmodified lines
265 expect(Predicate.isTagged(error, "RelayInternalError")).toBe(true);296 expect(Predicate.isTagged(error, "RelayInternalError")).toBe(true);
266 if (Predicate.isTagged(error, "RelayInternalError")) {297 if (Predicate.isTagged(error, "RelayInternalError")) {
267 expect(error.reason).toBe("persistence_failed");298 expect(error.reason).toBe("persistence_failed");
299 expect(error.traceId).toBe(projectionTraceId);
300 expect(error).not.toHaveProperty("cause");
268 }301 }
302 expect(logs).toEqual([
303 {
304 message: ["relay API failure projected to wire response"],
305 cause: Cause.fail(failure),
306 annotations: expect.objectContaining({
307 traceId: projectionTraceId,
308 sourceErrorTag: "EnvironmentCredentialAuthenticatePersistenceError",
309 responseErrorTag: "RelayInternalError",
310 responseCode: "internal_error",
311 responseReason: "persistence_failed",
312 }),
313 },
314 ]);
269 }).pipe(315 }).pipe(
316 Effect.provideService(Tracer.ParentSpan, projectionParentSpan),
317 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
270 Effect.provideService(318 Effect.provideService(
271 HttpServerRequest.HttpServerRequest,319 HttpServerRequest.HttpServerRequest,
272 HttpServerRequest.fromWeb(new Request("https://relay.test/v1/server/link")),320 HttpServerRequest.fromWeb(new Request("https://relay.test/v1/server/link")),
@@ -286,6 +334,128 @@ describe("relay environment authentication", () => { · 13 unmodified lines
286 });334 });
287});335});
288 336
337describe("relay API error projection", () => {
338 it.effect("logs common persistence failures with the wire response trace", () => {
339 const logs: CapturedLog[] = [];
340 const logger = Logger.make(({ cause, fiber, message }) => {
341 logs.push({
342 message,
343 cause,
344 annotations: fiber.getRef(References.CurrentLogAnnotations),
345 });
346 });
347 const databaseCause = new Error("database connection refused");
348 const failure = new EnvironmentCredentials.EnvironmentCredentialRevokePersistenceError({
349 environmentId: "env-test",
350 cause: databaseCause,
351 });
352
353 return Effect.gen(function* () {
354 const error = yield* Effect.fail(failure).pipe(
355 mapRelayCommonApiErrors("not_authorized"),
356 Effect.flip,
357 );
358
359 expect(error).toBeInstanceOf(RelayInternalError);
360 expect(error).toMatchObject({
361 code: "internal_error",
362 reason: "persistence_failed",
363 traceId: projectionTraceId,
364 });
365 expect(error).not.toHaveProperty("cause");
366 expect(logs).toEqual([
367 {
368 message: ["relay API failure projected to wire response"],
369 cause: Cause.fail(failure),
370 annotations: expect.objectContaining({
371 traceId: projectionTraceId,
372 sourceErrorTag: "EnvironmentCredentialRevokePersistenceError",
373 responseErrorTag: "RelayInternalError",
374 responseCode: "internal_error",
375 responseReason: "persistence_failed",
376 }),
377 },
378 ]);
379 expect(failure.cause).toBe(databaseCause);
380 }).pipe(
381 Effect.provideService(Tracer.ParentSpan, projectionParentSpan),
382 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
383 );
384 });
385
386 it.effect("logs selected server failures but not expected authorization projections", () => {
387 const logs: CapturedLog[] = [];
388 const logger = Logger.make(({ cause, fiber, message }) => {
389 logs.push({
390 message,
391 cause,
392 annotations: fiber.getRef(References.CurrentLogAnnotations),
393 });
394 });
395 const serverCause = new Error("mint request transport failed");
396 const serverFailure = EnvironmentConnector.EnvironmentMintRequestFailed.fromEndpoint({
397 userId: "user-test",
398 environmentId: "env-test",
399 operation: "connect",
400 stage: "send_request",
401 httpBaseUrl: "https://environment.example.test",
402 cause: serverCause,
403 });
404 const authorizationFailure = new EnvironmentConnector.EnvironmentConnectNotAuthorized({
405 environmentId: "env-test",
406 operation: "connect",
407 reason: "environment_link_not_found",
408 });
409
410 return Effect.gen(function* () {
411 const serverError = yield* Effect.fail(serverFailure).pipe(
412 mapErrorTags({
413 EnvironmentMintRequestFailed: (_error, traceId) =>
414 new RelayInternalError({
415 code: "internal_error",
416 reason: "upstream_unavailable",
417 traceId,
418 }),
419 }),
420 Effect.flip,
421 );
422 const authorizationError = yield* Effect.fail(authorizationFailure).pipe(
423 mapErrorTags({
424 EnvironmentConnectNotAuthorized: (_error, traceId) =>
425 new RelayEnvironmentConnectNotAuthorizedError({
426 code: "environment_connect_not_authorized",
427 traceId,
428 }),
429 }),
430 Effect.flip,
431 );
432
433 expect(serverError).toMatchObject({
434 reason: "upstream_unavailable",
435 traceId: projectionTraceId,
436 });
437 expect(authorizationError.traceId).toBe(projectionTraceId);
438 expect(logs).toEqual([
439 {
440 message: ["relay API failure projected to wire response"],
441 cause: Cause.fail(serverFailure),
442 annotations: expect.objectContaining({
443 traceId: projectionTraceId,
444 sourceErrorTag: "EnvironmentMintRequestFailed",
445 responseErrorTag: "RelayInternalError",
446 responseCode: "internal_error",
447 responseReason: "upstream_unavailable",
448 }),
449 },
450 ]);
451 expect(serverFailure.cause).toBe(serverCause);
452 }).pipe(
453 Effect.provideService(Tracer.ParentSpan, projectionParentSpan),
454 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
455 );
456 });
457});
458
289describe("relay request tracing", () => {459describe("relay request tracing", () => {
290 it.effect(460 it.effect(
291 "does not parent endpoint spans to an ambient parent captured while building handlers",461 "does not parent endpoint spans to an ambient parent captured while building handlers",
infra/relay/src/http/Api.ts+92−24
@@ -1,5 +1,6 @@
1import { createClerkClient, verifyToken } from "@clerk/backend";1import { createClerkClient, verifyToken } from "@clerk/backend";
2import { sql as drizzleSql } from "drizzle-orm";2import { sql as drizzleSql } from "drizzle-orm";
3import * as Cause from "effect/Cause";
3import * as Crypto from "effect/Crypto";4import * as Crypto from "effect/Crypto";
4import * as Context from "effect/Context";5import * as Context from "effect/Context";
5import * as DateTime from "effect/DateTime";6import * as DateTime from "effect/DateTime";
@@ -239,8 +240,8 @@ export const relayEnvironmentAuthLayer = Layer.effect( · 233 unmodified lines
239 const token = readHttpAuthorizationCredential(credential);240 const token = readHttpAuthorizationCredential(credential);
240 const principal = yield* credentials.authenticate(token).pipe(241 const principal = yield* credentials.authenticate(token).pipe(
241 Effect.catchTags({242 Effect.catchTags({
242 EnvironmentCredentialAuthenticatePersistenceError: () =>243 EnvironmentCredentialAuthenticatePersistenceError: (error) =>
243 relayInternalErrorResponse("persistence_failed"),244 relayInternalErrorResponse("persistence_failed", error),
244 }),245 }),
245 );246 );
246 if (principal._tag === "None") {247 if (principal._tag === "None") {
@@ -353,7 +354,7 @@ export const healthApi = HttpApiBuilder.group( · 106 unmodified lines
353 yield* db.execute(drizzleSql`SELECT 1`);354 yield* db.execute(drizzleSql`SELECT 1`);
354 return { ok: true, service: "relay" as const };355 return { ok: true, service: "relay" as const };
355 },356 },
356 Effect.catch(() => relayInternalErrorResponse("database_unavailable")),357 Effect.catch((error) => relayInternalErrorResponse("database_unavailable", error)),
357 ),358 ),
358 );359 );
359 }),360 }),
@@ -512,7 +513,7 @@ export const clientApi = HttpApiBuilder.group( · 152 unmodified lines
512 const now = yield* DateTime.now;513 const now = yield* DateTime.now;
513 const expiresAt = DateTime.add(now, { minutes: 5 });514 const expiresAt = DateTime.add(now, { minutes: 5 });
514 const jti = yield* crypto.randomUUIDv4.pipe(515 const jti = yield* crypto.randomUUIDv4.pipe(
515 Effect.catch(() => relayInternalErrorResponse("internal_error")),516 Effect.catch((error) => relayInternalErrorResponse("internal_error", error)),
516 );517 );
517 const challenge = yield* relayTokens518 const challenge = yield* relayTokens
518 .issueLinkChallenge({519 .issueLinkChallenge({
@@ -522,7 +523,7 @@ export const clientApi = HttpApiBuilder.group( · 3 unmodified lines
522 issuedAtEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),523 issuedAtEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
523 expiresAtEpochSeconds: Math.floor(expiresAt.epochMilliseconds / 1_000),524 expiresAtEpochSeconds: Math.floor(expiresAt.epochMilliseconds / 1_000),
524 })525 })
525 .pipe(Effect.catch(() => relayInternalErrorResponse("internal_error")));526 .pipe(Effect.catch((error) => relayInternalErrorResponse("internal_error", error)));
526 return { challenge, expiresAt: DateTime.formatIso(expiresAt) };527 return { challenge, expiresAt: DateTime.formatIso(expiresAt) };
527 }, mapRelayCommonApiErrors("not_authorized")),528 }, mapRelayCommonApiErrors("not_authorized")),
528 )529 )
@@ -536,7 +537,9 @@ export const clientApi = HttpApiBuilder.group( · 7 unmodified lines
536 userId,537 userId,
537 environmentId: params.environmentId,538 environmentId: params.environmentId,
538 })539 })
539 .pipe(Effect.catch(() => relayInternalErrorResponse("upstream_unavailable")));540 .pipe(
541 Effect.catch((error) => relayInternalErrorResponse("upstream_unavailable", error)),
542 );
540 const link = yield* links.getForUser({543 const link = yield* links.getForUser({
541 userId,544 userId,
542 environmentId: params.environmentId,545 environmentId: params.environmentId,
@@ -596,7 +599,7 @@ export const tokenApi = HttpApiBuilder.group( · 53 unmodified lines
596 const now = yield* DateTime.now;599 const now = yield* DateTime.now;
597 const expiresAt = DateTime.addDuration(now, RelayTokens.RELAY_DPOP_ACCESS_TOKEN_TTL);600 const expiresAt = DateTime.addDuration(now, RelayTokens.RELAY_DPOP_ACCESS_TOKEN_TTL);
598 const jti = yield* crypto.randomUUIDv4.pipe(601 const jti = yield* crypto.randomUUIDv4.pipe(
599 Effect.catch(() => relayInternalErrorResponse("internal_error")),602 Effect.catch((error) => relayInternalErrorResponse("internal_error", error)),
600 );603 );
601 return {604 return {
602 access_token: yield* relayTokens605 access_token: yield* relayTokens
@@ -609,7 +612,7 @@ export const tokenApi = HttpApiBuilder.group( · 6 unmodified lines
609 clientId: args.payload.client_id,612 clientId: args.payload.client_id,
610 scopes: requestedScopes,613 scopes: requestedScopes,
611 })614 })
612 .pipe(Effect.catch(() => relayInternalErrorResponse("internal_error"))),615 .pipe(Effect.catch((error) => relayInternalErrorResponse("internal_error", error))),
613 issued_token_type: RelayAccessTokenType,616 issued_token_type: RelayAccessTokenType,
614 token_type: "DPoP" as const,617 token_type: "DPoP" as const,
615 expires_in: Duration.toSeconds(RelayTokens.RELAY_DPOP_ACCESS_TOKEN_TTL),618 expires_in: Duration.toSeconds(RelayTokens.RELAY_DPOP_ACCESS_TOKEN_TTL),
@@ -901,6 +904,50 @@ const currentTraceId = Effect.currentParentSpan.pipe( · 285 unmodified lines
901 Effect.orElseSucceed(() => "unavailable"),904 Effect.orElseSucceed(() => "unavailable"),
902);905);
903 906
907function relayApiErrorTag(error: unknown): string {
908 if (
909 typeof error === "object" &&
910 error !== null &&
911 "_tag" in error &&
912 typeof error._tag === "string"
913 ) {
914 return error._tag;
915 }
916 if (error instanceof Error && error.name.length > 0) {
917 return error.name;
918 }
919 return typeof error;
920}
921
922function safeRelayApiLogCode(value: unknown): string | undefined {
923 return typeof value === "string" && value.length <= 64 && /^[a-z0-9._-]+$/iu.test(value)
924 ? value
925 : undefined;
926}
927
928function relayApiProjectionResponseFields(error: unknown) {
929 if (typeof error !== "object" || error === null) {
930 return {};
931 }
932 const code = "code" in error ? safeRelayApiLogCode(error.code) : undefined;
933 const reason = "reason" in error ? safeRelayApiLogCode(error.reason) : undefined;
934 return {
935 ...(code === undefined ? {} : { responseCode: code }),
936 ...(reason === undefined ? {} : { responseReason: reason }),
937 };
938}
939
940function logRelayApiProjection(cause: unknown, responseError: unknown, traceId: string) {
941 return Effect.logError("relay API failure projected to wire response", Cause.fail(cause)).pipe(
942 Effect.annotateLogs({
943 traceId,
944 sourceErrorTag: relayApiErrorTag(cause),
945 responseErrorTag: relayApiErrorTag(responseError),
946 ...relayApiProjectionResponseFields(responseError),
947 }),
948 );
949}
950
904const RelayCommonPersistenceError = Schema.Union([951const RelayCommonPersistenceError = Schema.Union([
905 Devices.DeviceRegistrationPersistenceError,952 Devices.DeviceRegistrationPersistenceError,
906 Devices.DeviceUnregistrationPersistenceError,953 Devices.DeviceUnregistrationPersistenceError,
@@ -930,15 +977,16 @@ type MapRelayCommonApiError<E> = · 23 unmodified lines
930 | (Extract<E, HttpApiError.Unauthorized> extends never ? never : RelayAuthInvalidError)977 | (Extract<E, HttpApiError.Unauthorized> extends never ? never : RelayAuthInvalidError)
931 | (Extract<E, RelayCommonPersistenceError> extends never ? never : RelayInternalError);978 | (Extract<E, RelayCommonPersistenceError> extends never ? never : RelayInternalError);
932 979
933function relayInternalErrorResponse(reason: RelayInternalError["reason"]) {980function relayInternalErrorResponse(reason: RelayInternalError["reason"], cause: unknown) {
934 return currentTraceId.pipe(981 return Effect.gen(function* () {
935 Effect.flatMap((traceId) =>982 const traceId = yield* currentTraceId;
936 Effect.fail(new RelayInternalError({ code: "internal_error", reason, traceId })),983 const responseError = new RelayInternalError({ code: "internal_error", reason, traceId });
937 ),984 yield* logRelayApiProjection(cause, responseError, traceId);
938 );985 return yield* responseError;
986 });
939}987}
940 988
941function mapRelayCommonApiErrors(authReason: RelayAuthInvalidReason) {989export function mapRelayCommonApiErrors(authReason: RelayAuthInvalidReason) {
942 const mapError = Effect.fnUntraced(function* <E>(error: E) {990 const mapError = Effect.fnUntraced(function* <E>(error: E) {
943 const traceId = yield* currentTraceId;991 const traceId = yield* currentTraceId;
944 if (isHttpUnauthorized(error)) {992 if (isHttpUnauthorized(error)) {
@@ -951,13 +999,13 @@ function mapRelayCommonApiErrors(authReason: RelayAuthInvalidReason) { · 6 unmodified lines
951 );999 );
952 }1000 }
953 if (isRelayCommonPersistenceError(error)) {1001 if (isRelayCommonPersistenceError(error)) {
954 return yield* Effect.fail(1002 const responseError = new RelayInternalError({
955 new RelayInternalError({1003 code: "internal_error",
956 code: "internal_error",1004 reason: "persistence_failed",
957 reason: "persistence_failed",1005 traceId,
958 traceId,1006 });
959 }) as MapRelayCommonApiError<E>,1007 yield* logRelayApiProjection(error, responseError, traceId);
960 );1008 return yield* Effect.fail(responseError as MapRelayCommonApiError<E>);
961 }1009 }
962 1010
963 return yield* Effect.fail(error as MapRelayCommonApiError<E>);1011 return yield* Effect.fail(error as MapRelayCommonApiError<E>);
@@ -989,7 +1037,19 @@ type CatchTagCases<E, Cases> = { · 25 unmodified lines
989 ) => Effect.Effect<never, MappedTagError<Cases>>;1037 ) => Effect.Effect<never, MappedTagError<Cases>>;
990} & (unknown extends E ? {} : { readonly [K in Exclude<keyof Cases, TaggedErrorTag<E>>]: never });1038} & (unknown extends E ? {} : { readonly [K in Exclude<keyof Cases, TaggedErrorTag<E>>]: never });
991 1039
992function mapErrorTags<1040const relayApiProjectedFailureLogTags = new Set([
1041 "ManagedEndpointProvisioningNotConfigured",
1042 "ManagedEndpointProvisioningFailed",
1043 "EnvironmentLinkUpsertPersistenceError",
1044 "EnvironmentCredentialCreatePersistenceError",
1045 "DpopProofReplayPersistenceError",
1046 "EnvironmentMintRequestFailed",
1047 "EnvironmentMintRequestTimedOut",
1048 "EnvironmentMintResponseInvalid",
1049 "ApnsDeliveryQueueSendError",
1050]);
1051
1052export function mapErrorTags<
993 E,1053 E,
994 Cases extends MapErrorTagCases<E> &1054 Cases extends MapErrorTagCases<E> &
995 (unknown extends E ? {} : { readonly [K in Exclude<keyof Cases, TaggedErrorTag<E>>]: never }),1055 (unknown extends E ? {} : { readonly [K in Exclude<keyof Cases, TaggedErrorTag<E>>]: never }),
@@ -1000,7 +1060,15 @@ function mapErrorTags< · 4 unmodified lines
1000 (error: never, traceId: string) => MappedTagError<Cases>1060 (error: never, traceId: string) => MappedTagError<Cases>
1001 >,1061 >,
1002 (makeError) => (error: never) =>1062 (makeError) => (error: never) =>
1003 currentTraceId.pipe(Effect.flatMap((traceId) => Effect.fail(makeError(error, traceId)))),1063 currentTraceId.pipe(
1064 Effect.flatMap((traceId) => {
1065 const responseError = makeError(error, traceId);
1066 const logProjection = relayApiProjectedFailureLogTags.has(relayApiErrorTag(error))
1067 ? logRelayApiProjection(error, responseError, traceId)
1068 : Effect.void;
1069 return logProjection.pipe(Effect.andThen(Effect.fail(responseError)));
1070 }),
1071 ),
1004 ) as CatchTagCases<E, Cases>;1072 ) as CatchTagCases<E, Cases>;
1005 1073
1006 return <A, R>(1074 return <A, R>(

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t