From b19a23c4403c149d9e4183818524e0c210bad6fb Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sat, 8 Feb 2025 09:26:29 +0800 Subject: [PATCH 1/2] fix: add auth health check --- src/server/routes/system/health.ts | 55 +++++++++++++++++------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/server/routes/system/health.ts b/src/server/routes/system/health.ts index 23672c92f..f887ca451 100644 --- a/src/server/routes/system/health.ts +++ b/src/server/routes/system/health.ts @@ -5,7 +5,6 @@ import { isDatabaseReachable } from "../../../shared/db/client"; import { env } from "../../../shared/utils/env"; import { isRedisReachable } from "../../../shared/utils/redis/redis"; import { thirdwebClientId } from "../../../shared/utils/sdk"; -import { createCustomError } from "../../middleware/error"; type EngineFeature = | "KEYPAIR_AUTH" @@ -15,7 +14,9 @@ type EngineFeature = | "SMART_BACKEND_WALLETS"; const ReplySchemaOk = Type.Object({ - status: Type.String(), + db: Type.Boolean(), + redis: Type.Boolean(), + auth: Type.Boolean(), engineVersion: Type.Optional(Type.String()), engineTier: Type.Optional(Type.String()), features: Type.Array( @@ -54,29 +55,22 @@ export async function healthCheck(fastify: FastifyInstance) { }, }, handler: async (_, res) => { - if (!(await isDatabaseReachable())) { - throw createCustomError( - "The database is unreachable.", - StatusCodes.SERVICE_UNAVAILABLE, - "FAILED_HEALTHCHECK", - ); - } + const db = await isDatabaseReachable(); + const redis = await isRedisReachable(); + const auth = await isAuthValid(); + const isHealthy = db && redis && auth; - if (!(await isRedisReachable())) { - throw createCustomError( - "Redis is unreachable.", - StatusCodes.SERVICE_UNAVAILABLE, - "FAILED_HEALTHCHECK", - ); - } - - res.status(StatusCodes.OK).send({ - status: "OK", - engineVersion: env.ENGINE_VERSION, - engineTier: env.ENGINE_TIER ?? "SELF_HOSTED", - features: getFeatures(), - clientId: thirdwebClientId, - }); + res + .status(isHealthy ? StatusCodes.OK : StatusCodes.SERVICE_UNAVAILABLE) + .send({ + db, + redis, + auth, + engineVersion: env.ENGINE_VERSION, + engineTier: env.ENGINE_TIER ?? "SELF_HOSTED", + features: getFeatures(), + clientId: thirdwebClientId, + }); }, }); } @@ -95,3 +89,16 @@ const getFeatures = (): EngineFeature[] => { return features; }; + +async function isAuthValid() { + try { + const resp = await fetch("https://api.thirdweb.com/v2/keys/use", { + headers: { + "x-secret-key": env.THIRDWEB_API_SECRET_KEY, + }, + }); + return resp.ok; + } catch { + return false; + } +} From 5f9bbf010367774999efb975a8e96f59e49a6538 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sat, 8 Feb 2025 09:31:45 +0800 Subject: [PATCH 2/2] fix reply schema for 5xx --- src/server/routes/system/health.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/server/routes/system/health.ts b/src/server/routes/system/health.ts index f887ca451..f358ad845 100644 --- a/src/server/routes/system/health.ts +++ b/src/server/routes/system/health.ts @@ -13,7 +13,7 @@ type EngineFeature = | "HETEROGENEOUS_WALLET_TYPES" | "SMART_BACKEND_WALLETS"; -const ReplySchemaOk = Type.Object({ +const ReplySchema = Type.Object({ db: Type.Boolean(), redis: Type.Boolean(), auth: Type.Boolean(), @@ -31,15 +31,9 @@ const ReplySchemaOk = Type.Object({ clientId: Type.String(), }); -const ReplySchemaError = Type.Object({ - error: Type.String(), -}); - -const responseBodySchema = Type.Union([ReplySchemaOk, ReplySchemaError]); - export async function healthCheck(fastify: FastifyInstance) { fastify.route<{ - Reply: Static; + Reply: Static; }>({ method: "GET", url: "/system/health", @@ -50,8 +44,8 @@ export async function healthCheck(fastify: FastifyInstance) { tags: ["System"], operationId: "checkHealth", response: { - [StatusCodes.OK]: ReplySchemaOk, - [StatusCodes.SERVICE_UNAVAILABLE]: ReplySchemaError, + [StatusCodes.OK]: ReplySchema, + [StatusCodes.SERVICE_UNAVAILABLE]: ReplySchema, }, }, handler: async (_, res) => {