Add secret keys for server and incoming request monitors

This commit is contained in:
Simon Larsen 2024-03-07 14:47:35 +00:00
parent 6fbb5948fb
commit 75f22b4bee
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
5 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import ObjectID from '../../ObjectID';
export default interface ServerMonitorRequest {
monitorId: ObjectID;
secretKey: ObjectID;
}

View File

@ -0,0 +1,8 @@
import BasicInfrastructureMetrics from "../../Infrastrucutre/BasicMetrics";
import ObjectID from "../../ObjectID";
export interface ServerMonitorResponse {
monitorId: ObjectID;
secretKey: ObjectID;
basicInfrastructureMetrics: BasicInfrastructureMetrics
}

View File

@ -91,10 +91,20 @@ export class Service extends DatabaseService<Model> {
); );
} }
if(createBy.data.monitorType === MonitorType.Server) {
createBy.data.serverMonitorSecretKey = ObjectID.generate();
}
if(createBy.data.monitorType === MonitorType.IncomingRequest) {
createBy.data.incomingRequestSecretKey = ObjectID.generate();
}
if (!createBy.props.tenantId) { if (!createBy.props.tenantId) {
throw new BadDataException('ProjectId required to create monitor.'); throw new BadDataException('ProjectId required to create monitor.');
} }
const monitorStatus: MonitorStatus | null = const monitorStatus: MonitorStatus | null =
await MonitorStatusService.findOneBy({ await MonitorStatusService.findOneBy({
query: { query: {

View File

@ -0,0 +1,78 @@
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
RequestHandler,
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
import BadDataException from 'Common/Types/Exception/BadDataException';
import ProbeMonitorResponseService from 'CommonServer/Utils/Probe/ProbeMonitorResponse';
import Dictionary from 'Common/Types/Dictionary';
import { JSONObject } from 'Common/Types/JSON';
import ObjectID from 'Common/Types/ObjectID';
import IncomingMonitorRequest from 'Common/Types/Monitor/IncomingMonitor/IncomingMonitorRequest';
import OneUptimeDate from 'Common/Types/Date';
const router: ExpressRouter = Express.getRouter();
const processServerMonitor: RequestHandler = async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
try {
const requestHeaders: Dictionary<string> =
req.headers as Dictionary<string>;
const requestBody: string | JSONObject = req.body as
| string
| JSONObject;
const monitorSecretKeyAsString: string | undefined = req.params['secretkey'];
if (!monitorSecretKeyAsString) {
throw new BadDataException('Monitor Secret Key is required');
}
const monitorId: ObjectID = ObjectID.fromString(monitorSecretKeyAsString);
const serverMonitorRequest: ServerMonitorRequest = {
monitorId: monitorId,
requestHeaders: requestHeaders,
requestBody: requestBody,
incomingRequestReceivedAt: OneUptimeDate.getCurrentDate(),
onlyCheckForIncomingRequestReceivedAt: false,
};
// process probe response here.
await ProbeMonitorResponseService.processProbeResponse(incomingRequest);
return Response.sendEmptyResponse(req, res);
} catch (err) {
return next(err);
}
};
router.get(
'/server-monitor/:secretkey',
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
processServerMonitor(req, res, next);
}
);
router.post(
'/server-monitor/response/ingest',
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
processServerMonitor(req, res, next);
}
);
export default router;

View File

@ -761,4 +761,115 @@ export default class Monitor extends BaseModel {
default: false, default: false,
}) })
public disableActiveMonitoringBecauseOfManualIncident?: boolean = undefined; public disableActiveMonitoringBecauseOfManualIncident?: boolean = undefined;
@ColumnAccessControl({
create: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
read: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanReadProjectMonitor,
],
update: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
})
@Index()
@TableColumn({
type: TableColumnType.Date,
required: false,
isDefaultValueColumn: false,
title: 'Server Monitor Request Received At',
description:
'This field is for Server Monitor only. When was the last time we received a request?',
})
@Column({
type: ColumnType.Date,
nullable: true,
})
public serverMonitorRequestReceivedAt?: Date = undefined;
@ColumnAccessControl({
create: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
read: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanReadProjectMonitor,
],
update: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
})
@Index()
@TableColumn({
type: TableColumnType.ObjectID,
required: false,
isDefaultValueColumn: false,
title: 'Server Monitor Secret Key',
description:
'This field is for Server Monitor only. Secret Key to authenticate the request.',
})
@Column({
type: ColumnType.ObjectID,
nullable: true,
transformer: ObjectID.getDatabaseTransformer(),
})
public serverMonitorSecretKey?: ObjectID = undefined;
@ColumnAccessControl({
create: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
read: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanReadProjectMonitor,
],
update: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CanCreateProjectMonitor,
],
})
@Index()
@TableColumn({
type: TableColumnType.ObjectID,
required: false,
isDefaultValueColumn: false,
title: 'Incoming Request Secret Key',
description:
'This field is for Incoming Request Monitor only. Secret Key to authenticate the request.',
})
@Column({
type: ColumnType.ObjectID,
nullable: true,
transformer: ObjectID.getDatabaseTransformer(),
})
public incomingRequestSecretKey?: ObjectID = undefined;
} }