Skip to content

Commit

Permalink
Enhance logging in Realtime utilities and add ping monitoring check i…
Browse files Browse the repository at this point in the history
…n Register service
  • Loading branch information
simlarsen committed Sep 25, 2024
1 parent e9b611b commit 8afa381
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Common/Server/Utils/Realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default abstract class Realtime {
// before joining room check the user token and check if the user has access to this tenant
// and to this model and to this event type

logger.debug("Extracting user access token from socket");
const userAccessToken: string | undefined =
UserMiddleware.getAccessTokenFromSocket(socket);

Expand All @@ -100,6 +101,7 @@ export default abstract class Realtime {
return;
}

logger.debug("Decoding user access token");
const userAuthorizationData: JSONWebTokenData =
JSONWebToken.decode(userAccessToken);

Expand All @@ -115,20 +117,23 @@ export default abstract class Realtime {
return;
}

logger.debug("Checking user access permissions");
let hasAccess: boolean = false;

if (userAuthorizationData.isMasterAdmin) {
logger.debug("User is a master admin, granting access");
hasAccess = true;
}

logger.debug("Fetching user global access permissions");
const userGlobalAccessPermission: UserGlobalAccessPermission | null =
await AccessTokenService.getUserGlobalAccessPermission(
userAuthorizationData.userId,
);

// check if the user has access to this tenant

if (userGlobalAccessPermission && !hasAccess) {
logger.debug("Checking if user has access to the tenant");
const hasAccessToProjectId: boolean =
userGlobalAccessPermission.projectIds.some((projectId: ObjectID) => {
return projectId.toString() === data.tenantId.toString();
Expand All @@ -141,6 +146,7 @@ export default abstract class Realtime {
return;
}

logger.debug("User has access to the tenant, checking model access");
const userId: ObjectID = new ObjectID(
userAuthorizationData.userId.toString(),
);
Expand All @@ -156,14 +162,17 @@ export default abstract class Realtime {
let databaseRequestType: DatabaseRequestType = DatabaseRequestType.Read;

if (data.eventType === ModelEventType.Create) {
logger.debug("Event type is Create, setting request type to Create");
databaseRequestType = DatabaseRequestType.Create;
}

if (data.eventType === ModelEventType.Update) {
logger.debug("Event type is Update, setting request type to Update");
databaseRequestType = DatabaseRequestType.Update;
}

if (data.eventType === ModelEventType.Delete) {
logger.debug("Event type is Delete, setting request type to Delete");
databaseRequestType = DatabaseRequestType.Delete;
}

Expand All @@ -176,6 +185,7 @@ export default abstract class Realtime {
databaseRequestType,
)
) {
logger.debug("User has access to the model, granting access");
hasAccess = true;
}
}
Expand Down
15 changes: 15 additions & 0 deletions Probe/Services/Register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ import LocalCache from "Common/Server/Infrastructure/LocalCache";
import logger from "Common/Server/Utils/Logger";

export default class Register {
public static async isPingMonitoringEnabled(): Promise<boolean> {
// check cache
const pingMonitoring: string | null = LocalCache.getString(
"PROBE",
"PING_MONITORING",
);

if (pingMonitoring) {
return pingMonitoring === "PING";
}

return true;
}

public static async reportIfOffline(): Promise<void> {
const pingMonitoringCheck: boolean =
await OnlineCheck.canProbeMonitorPingMonitors();
Expand All @@ -33,6 +47,7 @@ export default class Register {
logger.warn(
"Ping monitoring is disabled on this machine. Ping/ICMP checks are usually disabled by cloud providers (Azure, AWS, GCP, etc.). If you need ICMP checks, please use a different provider or use port checks.",
);

LocalCache.setString("PROBE", "PING_MONITORING", "PORT");
}

Expand Down
15 changes: 14 additions & 1 deletion Probe/Utils/Monitors/MonitorTypes/PortMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import PositiveNumber from "Common/Types/PositiveNumber";
import Sleep from "Common/Types/Sleep";
import logger from "Common/Server/Utils/Logger";
import net from "net";
import Register from "../../../Services/Register";

// TODO - make sure it work for the IPV6
export interface PortMonitorResponse {
Expand Down Expand Up @@ -115,7 +116,19 @@ export default class PortMonitor {
logger.debug("Ping timeout");

if (!hasPromiseResolved) {
reject(new UnableToReachServer("Ping timeout"));
// this could mean port 25 is blocked by the cloud provider and is timing out but is actually online.
// so we will return isOnline as true
if (
!Register.isPingMonitoringEnabled() &&
port.toNumber() === 25
) {
logger.debug(
"Ping monitoring is disabled because this is deployed in the cloud",
);
resolve(new PositiveNumber(timeout));
} else {
reject(new UnableToReachServer("Ping timeout"));
}
}

hasPromiseResolved = true;
Expand Down

0 comments on commit 8afa381

Please sign in to comment.