Skip to content

Commit

Permalink
Update IAP middleware to allow healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcampagnolitg committed Sep 10, 2024
1 parent 60326bf commit 37a651d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/fastify/fastifyApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<http.Server>,
Expand All @@ -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<void> {
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/fastify/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FastifyInstance } from 'fastify';
import { sendBadRequest } from './responses';
import {DEFAULT_HEALTHCHECK} from "#fastify/fastifyApp";

export interface RouteInterface {
method: string | string[];
Expand All @@ -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: '',
Expand Down
5 changes: 3 additions & 2 deletions src/fastify/userMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
Expand Down

0 comments on commit 37a651d

Please sign in to comment.