From 37a651df83fa04f71e555435b684aa7e9886e2d5 Mon Sep 17 00:00:00 2001 From: Daniel Campagnoli Date: Tue, 10 Sep 2024 12:48:44 +0800 Subject: [PATCH] Update IAP middleware to allow healthcheck --- src/fastify/fastifyApp.ts | 8 +++++--- src/fastify/hooks.ts | 3 ++- src/fastify/userMiddleware.ts | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/fastify/fastifyApp.ts b/src/fastify/fastifyApp.ts index 797861e9..c21fdcc6 100644 --- a/src/fastify/fastifyApp.ts +++ b/src/fastify/fastifyApp.ts @@ -22,6 +22,8 @@ import { loadOnRequestHooks } from './hooks'; const NODE_ENV = process.env.NODE_ENV ?? 'local'; +export const DEFAULT_HEALTHCHECK = '/health-check'; + export type TypeBoxFastifyInstance = FastifyInstance< http.Server, RawRequestDefaultExpression, @@ -42,8 +44,8 @@ export interface FastifyConfig { routes: RouteDefinition[]; instanceDecorators?: { [key: string]: any }; requestDecorators?: { [key: string]: any }; - /** Overrides the default url of /health-check */ - healthcheckUrl?: string; + /** Overrides the default url of /health-check. IAP middleware is currently dependent on DEFAULT_HEALTHCHECK */ + // healthcheckUrl?: string; } export async function initFastify(config: FastifyConfig): Promise { @@ -105,7 +107,7 @@ async function loadPlugins(config: FastifyConfig) { credentials: true, }); fastifyInstance.register(require('fastify-healthcheck'), { - healthcheckUrl: config.healthcheckUrl ?? '/health-check', + healthcheckUrl: /* config.healthcheckUrl ?? */ DEFAULT_HEALTHCHECK, }); await fastifyInstance.register(import('fastify-raw-body'), { field: 'rawBody', diff --git a/src/fastify/hooks.ts b/src/fastify/hooks.ts index d9261801..93830fd0 100644 --- a/src/fastify/hooks.ts +++ b/src/fastify/hooks.ts @@ -1,5 +1,6 @@ import { FastifyInstance } from 'fastify'; import { sendBadRequest } from './responses'; +import {DEFAULT_HEALTHCHECK} from "#fastify/fastifyApp"; export interface RouteInterface { method: string | string[]; @@ -17,7 +18,7 @@ export function loadOnRequestHooks(fastify: FastifyInstance) { } request.custom = {}; - if (routerPath === '/health-check') { + if (routerPath === DEFAULT_HEALTHCHECK) { request.custom.requestRoute = { method: routerMethod, version: '', diff --git a/src/fastify/userMiddleware.ts b/src/fastify/userMiddleware.ts index e9874b7d..5ed09c85 100644 --- a/src/fastify/userMiddleware.ts +++ b/src/fastify/userMiddleware.ts @@ -1,3 +1,4 @@ +import { DEFAULT_HEALTHCHECK } from '#fastify/fastifyApp'; import { logger } from '#o11y/logger'; import { runWithUser } from '#user/userService/userContext'; import { appContext } from '../app'; @@ -12,8 +13,8 @@ export function singleUserMiddleware(req: any, _res, next: () => void): void { } export function googleIapMiddleware(req: any, _res, next: () => void) { - logger.info(req.raw.url); - if (req.raw.url.startsWith('/webhooks/')) { + // It would be nicer if the health-check was earlier in the chain. Maybe when nextauthjs integration is done. + if (req.raw.url.startsWith('/webhooks/' || req.raw.url === DEFAULT_HEALTHCHECK)) { next(); return; }