diff --git a/backend/src/ee/routes/v1/relay-router.ts b/backend/src/ee/routes/v1/relay-router.ts index 5db5c0323..766025c21 100644 --- a/backend/src/ee/routes/v1/relay-router.ts +++ b/backend/src/ee/routes/v1/relay-router.ts @@ -166,14 +166,24 @@ export const registerRelayRouter = async (server: FastifyZodProvider) => { onRequest: (req, _, next) => { const authHeader = req.headers.authorization; - if (appCfg.RELAY_AUTH_SECRET && authHeader) { - const expectedHeader = `Bearer ${appCfg.RELAY_AUTH_SECRET}`; - if ( - authHeader.length === expectedHeader.length && - crypto.nativeCrypto.timingSafeEqual(Buffer.from(authHeader), Buffer.from(expectedHeader)) - ) { - return next(); - } + if (!appCfg.RELAY_AUTH_SECRET) { + throw new UnauthorizedError({ + message: "Relay authentication not configured" + }); + } + + if (!authHeader) { + throw new UnauthorizedError({ + message: "Missing authorization header" + }); + } + + const expectedHeader = `Bearer ${appCfg.RELAY_AUTH_SECRET}`; + if ( + authHeader.length === expectedHeader.length && + crypto.nativeCrypto.timingSafeEqual(Buffer.from(authHeader), Buffer.from(expectedHeader)) + ) { + return next(); } throw new UnauthorizedError({ diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-dal.ts b/backend/src/ee/services/gateway-v2/gateway-v2-dal.ts index 36feb3809..4bb4f58ba 100644 --- a/backend/src/ee/services/gateway-v2/gateway-v2-dal.ts +++ b/backend/src/ee/services/gateway-v2/gateway-v2-dal.ts @@ -30,8 +30,7 @@ export const gatewayV2DalFactory = (db: TDbClient) => { .select(db.ref("name").withSchema(TableName.Identity).as("identityName")); if (isHeartbeatStale) { - const oneHourAgo = new Date(); - oneHourAgo.setHours(oneHourAgo.getHours() - 1); + const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000); void query.where(`${TableName.GatewayV2}.heartbeat`, "<", oneHourAgo); void query.where((v) => { void v diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts index 128f9a651..78a62bcb8 100644 --- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts +++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts @@ -902,8 +902,6 @@ export const gatewayV2ServiceFactory = ({ "Found gateways with last heartbeat over an hour ago. Sending notifications." ); - await Promise.all(unhealthyGateways.map((gw) => gatewayV2DAL.updateById(gw.id, { healthAlertedAt: new Date() }))); - const gatewaysByOrg = unhealthyGateways.reduce>((acc, gw) => { if (!acc[gw.orgId]) { acc[gw.orgId] = []; @@ -944,6 +942,8 @@ export const gatewayV2ServiceFactory = ({ }, template: SmtpTemplates.HealthAlert }); + + await Promise.all(gateways.map((gw) => gatewayV2DAL.updateById(gw.id, { healthAlertedAt: new Date() }))); } catch (error) { logger.error(error, `Failed to send gateway health notifications for organization [orgId=${orgId}]`); } diff --git a/backend/src/ee/services/relay/relay-dal.ts b/backend/src/ee/services/relay/relay-dal.ts index cef5e643a..687b2d0e8 100644 --- a/backend/src/ee/services/relay/relay-dal.ts +++ b/backend/src/ee/services/relay/relay-dal.ts @@ -20,8 +20,7 @@ export const relayDalFactory = (db: TDbClient) => { .where(buildFindFilter(regularFilter, TableName.Relay)); if (isHeartbeatStale) { - const oneHourAgo = new Date(); - oneHourAgo.setHours(oneHourAgo.getHours() - 1); + const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000); void query.where(`${TableName.Relay}.heartbeat`, "<", oneHourAgo); void query.where((v) => { void v diff --git a/backend/src/ee/services/relay/relay-service.ts b/backend/src/ee/services/relay/relay-service.ts index 661a3d304..6b56908f5 100644 --- a/backend/src/ee/services/relay/relay-service.ts +++ b/backend/src/ee/services/relay/relay-service.ts @@ -1209,8 +1209,6 @@ export const relayServiceFactory = ({ }; const $healthcheckNotify = async () => { - const oneHourAgo = new Date(); - oneHourAgo.setHours(oneHourAgo.getHours() - 1); const unhealthyRelays = await relayDAL.find({ isHeartbeatStale: true }); @@ -1222,8 +1220,6 @@ export const relayServiceFactory = ({ "Found relays with last heartbeat over an hour ago. Sending notifications." ); - await Promise.all(unhealthyRelays.map((r) => relayDAL.updateById(r.id, { healthAlertedAt: new Date() }))); - const relaysByOrg = unhealthyRelays.reduce>((acc, r) => { const key = r.orgId ?? "instance"; if (!acc[key]) { @@ -1285,6 +1281,8 @@ export const relayServiceFactory = ({ template: SmtpTemplates.HealthAlert }); } + + await Promise.all(relays.map((r) => relayDAL.updateById(r.id, { healthAlertedAt: new Date() }))); } catch (error) { logger.error(error, `Failed to send relay health notifications for organization [orgId=${orgId}]`); }