From 4ba2be8622fa889145c2efee00ba4c12d746a600 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 5 Dec 2022 11:35:48 +0530 Subject: [PATCH] acme validation. --- Common/Types/Exception/ExceptionCode.ts | 1 + Common/Types/Exception/NotFoundException.ts | 8 +++++ CommonServer/Utils/Response.ts | 20 ++++++++++++ StatusPage/Index.ts | 35 +++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 Common/Types/Exception/NotFoundException.ts diff --git a/Common/Types/Exception/ExceptionCode.ts b/Common/Types/Exception/ExceptionCode.ts index b310452f55..2bbf3d06f8 100644 --- a/Common/Types/Exception/ExceptionCode.ts +++ b/Common/Types/Exception/ExceptionCode.ts @@ -9,6 +9,7 @@ enum ExceptionCode { NotAuthorizedException = 403, NotAuthenticatedxception = 401, PaymentRequiredException = 402, + NotFoundException = 404, } export default ExceptionCode; diff --git a/Common/Types/Exception/NotFoundException.ts b/Common/Types/Exception/NotFoundException.ts new file mode 100644 index 0000000000..1359628030 --- /dev/null +++ b/Common/Types/Exception/NotFoundException.ts @@ -0,0 +1,8 @@ +import Exception from './Exception'; +import ExceptionCode from './ExceptionCode'; + +export default class NotFoundException extends Exception { + public constructor(message: string) { + super(ExceptionCode.NotFoundException, message); + } +} diff --git a/CommonServer/Utils/Response.ts b/CommonServer/Utils/Response.ts index a35700d5e3..2a44e46456 100644 --- a/CommonServer/Utils/Response.ts +++ b/CommonServer/Utils/Response.ts @@ -243,4 +243,24 @@ export default class Response { oneUptimeResponse.status(200).send(item); this.logResponse(req, res, item as JSONObject); } + + public static sendTextResponse( + req: ExpressRequest, + res: ExpressResponse, + text: string + ): void { + const oneUptimeRequest: OneUptimeRequest = req as OneUptimeRequest; + const oneUptimeResponse: OneUptimeResponse = res as OneUptimeResponse; + + oneUptimeResponse.set( + 'ExpressRequest-Id', + oneUptimeRequest.id.toString() + ); + + oneUptimeResponse.set('Pod-Id', process.env['POD_NAME']); + + oneUptimeResponse.logBody = { text: text as string }; + oneUptimeResponse.status(200).send(text); + this.logResponse(req, res, { text: text as string }); + } } diff --git a/StatusPage/Index.ts b/StatusPage/Index.ts index a2bd170e2a..bd3151ff3b 100755 --- a/StatusPage/Index.ts +++ b/StatusPage/Index.ts @@ -7,6 +7,11 @@ import Express, { ExpressStatic, } from 'CommonServer/Utils/Express'; import logger from 'CommonServer/Utils/Logger'; +import { PostgresAppInstance } from 'CommonServer/Infrastructure/PostgresDatabase'; +import GreenlockChallengeService from "CommonServer/Services/GreenlockChallengeService"; +import GreenlockChallenge from 'Model/Models/GreenlockChallenge'; +import Response from 'CommonServer/Utils/Response'; +import NotFoundException from 'Common/Types/Exception/NotFoundException'; export const APP_NAME: string = 'status-page'; @@ -21,6 +26,30 @@ app.use( ExpressStatic(path.join(__dirname, 'dist')) ); +// ACME Challenge Validation. +app.use(['/.well-known/acme-challenge/:token'], async ( + req: ExpressRequest, + res: ExpressResponse +) => { + const challenge : GreenlockChallenge | null = await GreenlockChallengeService.findOneBy({ + query: { + key: req.params['token'] as string + }, + select: { + challenge: true, + }, + props: { + isRoot: true, + } + }) + + if (!challenge) { + return Response.sendErrorResponse(req, res, new NotFoundException("Challenge not found")); + } + + return Response.sendTextResponse(req, res, challenge.challenge as string); +}); + app.get('/*', (_req: ExpressRequest, res: ExpressResponse) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); @@ -29,6 +58,12 @@ const init: Function = async (): Promise => { try { // init the app await App(APP_NAME); + + // connect to the database. + await PostgresAppInstance.connect( + PostgresAppInstance.getDatasourceOptions() + ); + } catch (err) { logger.error('App Init Failed:'); logger.error(err);