Merge pull request #946 from om-3004/enhancement/check-if-url-resolves

check if the url resolves before adding the monitor
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-10-19 16:48:42 +08:00
commit 287117132b
10 files changed
+830 -362

No files matched your search

+43 -2
View File
@@ -3,6 +3,7 @@ import {
getMonitorByIdQueryValidation,
getMonitorsByTeamIdValidation,
createMonitorBodyValidation,
getMonitorURLByQueryValidation,
editMonitorBodyValidation,
getMonitorsAndSummaryByTeamIdParamValidation,
getMonitorsAndSummaryByTeamIdQueryValidation,
@@ -13,13 +14,14 @@ import {
getCertificateParamValidation,
} from "../validation/joi.js";
import sslChecker from "ssl-checker";
const SERVICE_NAME = "monitorController";
import { errorMessages, successMessages } from "../utils/messages.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import logger from "../utils/logger.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
import dns from "dns";
const SERVICE_NAME = "monitorController";
/**
* Returns all monitors
@@ -257,6 +259,44 @@ const createMonitor = async (req, res, next) => {
}
};
/**
* Checks if the endpoint can be resolved
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.query - The query parameters of the request.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the resolution result.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
const checkEndpointResolution = async (req, res, next) => {
try {
await getMonitorURLByQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
let { monitorURL } = req.query;
monitorURL = new URL(monitorURL);
await new Promise((resolve, reject) => {
dns.resolve(monitorURL.hostname, (error) => {
if (error) {
reject(error);
}
resolve();
});
});
return res.status(200).json({
success: true,
msg: `URL resolved successfully`,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
}
}
/**
* Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
* @async
@@ -477,6 +517,7 @@ export {
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
checkEndpointResolution,
deleteMonitor,
deleteAllMonitors,
editMonitor,
+303 -63
View File
@@ -22,12 +22,16 @@
"variables": {
"PORT": {
"description": "API Port",
"enum": ["5000"],
"enum": [
"5000"
],
"default": "5000"
},
"API_PATH": {
"description": "API Base Path",
"enum": ["api/v1"],
"enum": [
"api/v1"
],
"default": "api/v1"
}
}
@@ -38,7 +42,9 @@
"variables": {
"API_PATH": {
"description": "API Base Path",
"enum": ["api/v1"],
"enum": [
"api/v1"
],
"default": "api/v1"
}
}
@@ -49,12 +55,16 @@
"variables": {
"PORT": {
"description": "API Port",
"enum": ["5000"],
"enum": [
"5000"
],
"default": "5000"
},
"API_PATH": {
"description": "API Base Path",
"enum": ["api/v1"],
"enum": [
"api/v1"
],
"default": "api/v1"
}
}
@@ -89,7 +99,9 @@
"paths": {
"/auth/register": {
"post": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Register a new user",
"requestBody": {
"content": {
@@ -125,8 +137,23 @@
},
"role": {
"type": "array",
"enum": [["user"], ["admin"], ["superadmin"], ["Demo"]],
"default": ["superadmin"]
"enum": [
[
"user"
],
[
"admin"
],
[
"superadmin"
],
[
"Demo"
]
],
"default": [
"superadmin"
]
},
"teamId": {
"type": "string",
@@ -173,14 +200,19 @@
},
"/auth/login": {
"post": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Login with credentials",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["email", "password"],
"required": [
"email",
"password"
],
"properties": {
"email": {
"type": "string",
@@ -231,7 +263,9 @@
},
"/auth/user/{userId}": {
"put": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Change user information",
"parameters": [
{
@@ -292,7 +326,9 @@
]
},
"delete": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Delete user",
"parameters": [
{
@@ -345,7 +381,9 @@
},
"/auth/users/superadmin": {
"get": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Checks to see if an admin account exists",
"responses": {
"200": {
@@ -388,7 +426,9 @@
},
"/auth/users": {
"get": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Get all users",
"responses": {
"200": {
@@ -431,14 +471,18 @@
},
"/auth/recovery/request": {
"post": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Request a recovery token",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["email"],
"required": [
"email"
],
"properties": {
"email": {
"type": "string",
@@ -485,14 +529,18 @@
},
"/auth/recovery/validate": {
"post": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Validate recovery token",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["recoveryToken"],
"required": [
"recoveryToken"
],
"properties": {
"recoveryToken": {
"type": "string"
@@ -538,14 +586,19 @@
},
"/auth/recovery/reset": {
"post": {
"tags": ["auth"],
"tags": [
"auth"
],
"description": "Password reset",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["recoveryToken", "password"],
"required": [
"recoveryToken",
"password"
],
"properties": {
"recoveryToken": {
"type": "string"
@@ -594,14 +647,19 @@
},
"/invite": {
"post": {
"tags": ["invite"],
"tags": [
"invite"
],
"description": "Request an invitation",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["email", "role"],
"required": [
"email",
"role"
],
"properties": {
"email": {
"type": "string"
@@ -655,14 +713,18 @@
},
"/invite/verify": {
"post": {
"tags": ["invite"],
"tags": [
"invite"
],
"description": "Request an invitation",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["token"],
"required": [
"token"
],
"properties": {
"token": {
"type": "string"
@@ -713,7 +775,9 @@
},
"/monitors": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get all monitors",
"responses": {
"200": {
@@ -754,7 +818,9 @@
]
},
"post": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Create a new monitor",
"requestBody": {
"content": {
@@ -804,7 +870,9 @@
]
},
"delete": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Delete all monitors",
"responses": {
"200": {
@@ -845,9 +913,78 @@
]
}
},
"/monitors/resolution/url": {
"get": {
"tags": [
"monitors"
],
"description": "Check DNS resolution for a given URL",
"parameters": [
{
"name": "monitorURL",
"in": "query",
"required": true,
"schema": {
"type": "string",
"example": "https://example.com"
},
"description": "The URL to check DNS resolution for"
}
],
"responses": {
"200": {
"description": "URL resolved successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuccessResponse"
}
}
}
},
"400": {
"description": "DNS resolution failed",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
},
"422": {
"description": "Unprocessable Content",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
},
"security": [
{
"bearerAuth": []
}
]
}
},
"/monitors/{monitorId}": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get monitor by id",
"parameters": [
{
@@ -898,7 +1035,9 @@
]
},
"put": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Update monitor by id",
"parameters": [
{
@@ -958,7 +1097,9 @@
]
},
"delete": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Delete monitor by id",
"parameters": [
{
@@ -1011,7 +1152,9 @@
},
"/monitors/stats/{monitorId}": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get monitor stats",
"parameters": [
{
@@ -1064,7 +1207,9 @@
},
"/monitors/certificate/{monitorId}": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get monitor certificate",
"parameters": [
{
@@ -1117,7 +1262,9 @@
},
"/monitors/team/summary/{teamId}": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get monitors and summary by teamId",
"parameters": [
{
@@ -1134,7 +1281,11 @@
"required": false,
"schema": {
"type": "array",
"enum": ["http", "ping", "pagespeed"]
"enum": [
"http",
"ping",
"pagespeed"
]
}
}
],
@@ -1179,7 +1330,9 @@
},
"/monitors/team/{teamId}": {
"get": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Get monitors by teamId",
"parameters": [
{
@@ -1206,7 +1359,10 @@
"required": false,
"schema": {
"type": "string",
"enum": ["asc", "desc"]
"enum": [
"asc",
"desc"
]
}
},
{
@@ -1225,7 +1381,11 @@
"required": false,
"schema": {
"type": "string",
"enum": ["http", "ping", "pagespeed"]
"enum": [
"http",
"ping",
"pagespeed"
]
}
},
{
@@ -1269,7 +1429,11 @@
"required": false,
"schema": {
"type": "string",
"enum": ["http", "ping", "pagespeed"]
"enum": [
"http",
"ping",
"pagespeed"
]
}
}
],
@@ -1314,7 +1478,9 @@
},
"/monitors/pause/{monitorId}": {
"post": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Pause monitor",
"parameters": [
{
@@ -1367,7 +1533,9 @@
},
"/monitors/demo": {
"post": {
"tags": ["monitors"],
"tags": [
"monitors"
],
"description": "Create a demo monitor",
"requestBody": {
"content": {
@@ -1419,7 +1587,9 @@
},
"/checks/{monitorId}": {
"get": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Get all checks for a monitor",
"parameters": [
{
@@ -1470,7 +1640,9 @@
]
},
"post": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Create a new check",
"parameters": [
{
@@ -1530,7 +1702,9 @@
]
},
"delete": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Delete all checks for a monitor",
"parameters": [
{
@@ -1583,7 +1757,9 @@
},
"/checks/team/{teamId}": {
"get": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Get all checks for a team",
"parameters": [
{
@@ -1634,7 +1810,9 @@
]
},
"delete": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Delete all checks for a team",
"parameters": [
{
@@ -1687,7 +1865,9 @@
},
"/checks/team/ttl": {
"put": {
"tags": ["checks"],
"tags": [
"checks"
],
"description": "Update check TTL",
"requestBody": {
"content": {
@@ -1739,7 +1919,9 @@
},
"/maintenance-window/monitor/{monitorId}": {
"get": {
"tags": ["maintenance-window"],
"tags": [
"maintenance-window"
],
"description": "Get maintenance window for monitor",
"parameters": [
{
@@ -1790,7 +1972,9 @@
]
},
"post": {
"tags": ["maintenance-window"],
"tags": [
"maintenance-window"
],
"description": "Create maintenance window for monitor",
"parameters": [
{
@@ -1852,7 +2036,9 @@
},
"/maintenance-window/user/{userId}": {
"get": {
"tags": ["maintenance-window"],
"tags": [
"maintenance-window"
],
"description": "Get maintenance window for user",
"parameters": [
{
@@ -1905,7 +2091,9 @@
},
"/queue/jobs": {
"get": {
"tags": ["queue"],
"tags": [
"queue"
],
"description": "Get all jobs in queue",
"responses": {
"200": {
@@ -1946,7 +2134,9 @@
]
},
"post": {
"tags": ["queue"],
"tags": [
"queue"
],
"description": "Create a new job. Useful for testing scaling workers",
"responses": {
"200": {
@@ -1989,7 +2179,9 @@
},
"/queue/metrics": {
"get": {
"tags": ["queue"],
"tags": [
"queue"
],
"description": "Get queue metrics",
"responses": {
"200": {
@@ -2032,7 +2224,9 @@
},
"/queue/obliterate": {
"post": {
"tags": ["queue"],
"tags": [
"queue"
],
"description": "Obliterate job queue",
"responses": {
"200": {
@@ -2074,7 +2268,6 @@
}
}
},
"components": {
"securitySchemes": {
"bearerAuth": {
@@ -2113,7 +2306,14 @@
},
"UserUpdateRequest": {
"type": "object",
"required": ["firstName", "lastName", "email", "password", "role", "teamId"],
"required": [
"firstName",
"lastName",
"email",
"password",
"role",
"teamId"
],
"properties": {
"firstName": {
"type": "string"
@@ -2135,8 +2335,23 @@
},
"role": {
"type": "array",
"enum": [["user"], ["admin"], ["superadmin"], ["Demo"]],
"default": ["superadmin"]
"enum": [
[
"user"
],
[
"admin"
],
[
"superadmin"
],
[
"Demo"
]
],
"default": [
"superadmin"
]
},
"deleteProfileImage": {
"type": "boolean"
@@ -2145,7 +2360,14 @@
},
"CreateMonitorBody": {
"type": "object",
"required": ["userId", "teamId", "name", "description", "type", "url"],
"required": [
"userId",
"teamId",
"name",
"description",
"type",
"url"
],
"properties": {
"_id": {
"type": "string"
@@ -2164,7 +2386,11 @@
},
"type": {
"type": "string",
"enum": ["http", "ping", "pagespeed"]
"enum": [
"http",
"ping",
"pagespeed"
]
},
"url": {
"type": "string"
@@ -2205,7 +2431,13 @@
},
"CreateCheckBody": {
"type": "object",
"required": ["monitorId", "status", "responseTime", "statusCode", "message"],
"required": [
"monitorId",
"status",
"responseTime",
"statusCode",
"message"
],
"properties": {
"monitorId": {
"type": "string"
@@ -2226,7 +2458,9 @@
},
"UpdateCheckTTLBody": {
"type": "object",
"required": ["ttl"],
"required": [
"ttl"
],
"properties": {
"ttl": {
"type": "integer"
@@ -2235,7 +2469,13 @@
},
"CreateMaintenanceWindowBody": {
"type": "object",
"required": ["userId", "active", "oneTime", "start", "end"],
"required": [
"userId",
"active",
"oneTime",
"start",
"end"
],
"properties": {
"userId": {
"type": "string"
@@ -2262,4 +2502,4 @@
}
}
}
}
}
+26 -15
View File
@@ -1,17 +1,18 @@
import { Router } from "express";
import {
getAllMonitors,
getMonitorStatsById,
getMonitorCertificate,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
deleteMonitor,
deleteAllMonitors,
editMonitor,
pauseMonitor,
addDemoMonitors,
getAllMonitors,
getMonitorStatsById,
getMonitorCertificate,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
checkEndpointResolution,
deleteMonitor,
deleteAllMonitors,
editMonitor,
pauseMonitor,
addDemoMonitors,
} from "../controllers/monitorController.js";
import { isAllowed } from "../middleware/isAllowed.js";
import { fetchMonitorCertificate } from "../controllers/controllerUtils.js";
@@ -21,15 +22,25 @@ const router = Router();
router.get("/", getAllMonitors);
router.get("/stats/:monitorId", getMonitorStatsById);
router.get("/certificate/:monitorId", (req, res, next) => {
getMonitorCertificate(req, res, next, fetchMonitorCertificate);
getMonitorCertificate(req, res, next, fetchMonitorCertificate);
});
router.get("/:monitorId", getMonitorById);
router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId);
router.get("/team/:teamId", getMonitorsByTeamId);
router.post("/", isAllowed(["admin", "superadmin"]), createMonitor);
router.get(
"/resolution/url",
isAllowed(["admin", "superadmin"]),
checkEndpointResolution
)
router.delete("/:monitorId", isAllowed(["admin", "superadmin"]), deleteMonitor);
router.delete(
"/:monitorId",
isAllowed(["admin", "superadmin"]),
deleteMonitor
);
router.post("/", isAllowed(["admin", "superadmin"]), createMonitor);
router.put("/:monitorId", isAllowed(["admin", "superadmin"]), editMonitor);
@@ -6,6 +6,7 @@ import {
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
checkEndpointResolution,
deleteMonitor,
deleteAllMonitors,
editMonitor,
@@ -16,9 +17,7 @@ import jwt from "jsonwebtoken";
import sinon from "sinon";
import { successMessages } from "../../utils/messages.js";
import logger from "../../utils/logger.js";
import * as monitorController from "../../controllers/monitorController.js";
import { fetchMonitorCertificate } from "../../controllers/controllerUtils.js";
import dns from "dns";
const SERVICE_NAME = "monitorController";
describe("Monitor Controller - getAllMonitors", () => {
@@ -460,6 +459,50 @@ describe("Monitor Controller - createMonitor", () => {
});
});
describe("Monitor Controllor - checkEndpointResolution", () => {
let req, res, next, dnsResolveStub;
beforeEach(() => {
req = { query: { monitorURL: 'https://example.com' } };
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
next = sinon.stub();
dnsResolveStub = sinon.stub(dns, 'resolve');
});
afterEach(() => {
dnsResolveStub.restore();
});
it('should resolve the URL successfully', async () => {
dnsResolveStub.callsFake((hostname, callback) => callback(null));
await checkEndpointResolution(req, res, next);
expect(res.status.calledWith(200)).to.be.true;
expect(res.json.calledWith({
success: true,
msg: 'URL resolved successfully',
})).to.be.true;
expect(next.called).to.be.false;
});
it("should return an error if DNS resolution fails", async () => {
const dnsError = new Error("DNS resolution failed");
dnsError.code = 'ENOTFOUND';
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
const errorPassedToNext = next.getCall(0).args[0];
expect(errorPassedToNext).to.be.an.instanceOf(Error);
expect(errorPassedToNext.message).to.include('DNS resolution failed');
expect(errorPassedToNext.code).to.equal('ENOTFOUND');
expect(errorPassedToNext.status).to.equal(500);
});
it('should reject with an error if query validation fails', async () => {
req.query.monitorURL = 'invalid-url';
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
const error = next.getCall(0).args[0];
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
expect(error.message).to.equal('"monitorURL" must be a valid uri');
});
});
describe("Monitor Controller - deleteMonitor", () => {
let req, res, next;
beforeEach(() => {
+5
View File
@@ -236,6 +236,10 @@ const pauseMonitorParamValidation = joi.object({
monitorId: joi.string().required(),
});
const getMonitorURLByQueryValidation = joi.object({
monitorURL: joi.string().uri().required(),
});
//****************************************
// Alerts
//****************************************
@@ -446,6 +450,7 @@ export {
getCertificateParamValidation,
editMonitorBodyValidation,
pauseMonitorParamValidation,
getMonitorURLByQueryValidation,
editUserParamValidation,
editUserBodyValidation,
createAlertParamValidation,