Send terminate error messages

This commit is contained in:
Owen
2026-01-16 14:57:54 -08:00
parent a126494c12
commit 9114dd5992
11 changed files with 41 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ import logger from "@server/logger";
import { sendTerminateClient } from "@server/routers/client/terminate"; import { sendTerminateClient } from "@server/routers/client/terminate";
import { and, eq, notInArray, type InferInsertModel } from "drizzle-orm"; import { and, eq, notInArray, type InferInsertModel } from "drizzle-orm";
import { rebuildClientAssociationsFromClient } from "./rebuildClientAssociations"; import { rebuildClientAssociationsFromClient } from "./rebuildClientAssociations";
import { OlmErrorCodes } from "@server/routers/olm/error";
export async function calculateUserClientsForOrgs( export async function calculateUserClientsForOrgs(
userId: string, userId: string,
@@ -305,6 +306,8 @@ async function cleanupOrphanedClients(
if (deletedClient.olmId) { if (deletedClient.olmId) {
await sendTerminateClient( await sendTerminateClient(
deletedClient.clientId, deletedClient.clientId,
OlmErrorCodes.TERMINATED_DELETED,
"Deleted",
deletedClient.olmId deletedClient.olmId
); );
} }

View File

@@ -24,6 +24,7 @@ import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { hashPassword } from "@server/auth/password"; import { hashPassword } from "@server/auth/password";
import { disconnectClient, sendToClient } from "#private/routers/ws"; import { disconnectClient, sendToClient } from "#private/routers/ws";
import { OlmErrorCodes, sendOlmError } from "@server/routers/olm/error";
const reGenerateSecretParamsSchema = z.strictObject({ const reGenerateSecretParamsSchema = z.strictObject({
clientId: z.string().transform(Number).pipe(z.int().positive()) clientId: z.string().transform(Number).pipe(z.int().positive())
@@ -119,7 +120,10 @@ export async function reGenerateClientSecret(
if (disconnect) { if (disconnect) {
const payload = { const payload = {
type: `olm/terminate`, type: `olm/terminate`,
data: {} data: {
code: OlmErrorCodes.TERMINATED_REKEYED,
message: "Client secret has been regenerated"
}
}; };
// Don't await this to prevent blocking the response // Don't await this to prevent blocking the response
sendToClient(existingOlms[0].olmId, payload).catch((error) => { sendToClient(existingOlms[0].olmId, payload).catch((error) => {

View File

@@ -11,6 +11,7 @@ import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations"; import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations";
import { sendTerminateClient } from "./terminate"; import { sendTerminateClient } from "./terminate";
import { OlmErrorCodes } from "../olm/error";
const archiveClientSchema = z.strictObject({ const archiveClientSchema = z.strictObject({
clientId: z.string().transform(Number).pipe(z.int().positive()) clientId: z.string().transform(Number).pipe(z.int().positive())
@@ -82,7 +83,7 @@ export async function archiveClient(
// Send terminate signal if there's an associated OLM // Send terminate signal if there's an associated OLM
if (client.olmId) { if (client.olmId) {
await sendTerminateClient(client.clientId, client.olmId); await sendTerminateClient(client.clientId, OlmErrorCodes.TERMINATED_ARCHIVED, "Archived", client.olmId);
} }
}); });

View File

@@ -10,6 +10,7 @@ import logger from "@server/logger";
import { fromError } from "zod-validation-error"; import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { sendTerminateClient } from "./terminate"; import { sendTerminateClient } from "./terminate";
import { OlmErrorCodes } from "../olm/error";
const blockClientSchema = z.strictObject({ const blockClientSchema = z.strictObject({
clientId: z.string().transform(Number).pipe(z.int().positive()) clientId: z.string().transform(Number).pipe(z.int().positive())
@@ -78,7 +79,7 @@ export async function blockClient(
// Send terminate signal if there's an associated OLM and it's connected // Send terminate signal if there's an associated OLM and it's connected
if (client.olmId && client.online) { if (client.olmId && client.online) {
await sendTerminateClient(client.clientId, client.olmId); await sendTerminateClient(client.clientId, OlmErrorCodes.TERMINATED_BLOCKED, "Blocked", client.olmId);
} }
}); });

View File

@@ -11,6 +11,7 @@ import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations"; import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations";
import { sendTerminateClient } from "./terminate"; import { sendTerminateClient } from "./terminate";
import { OlmErrorCodes } from "../olm/error";
const deleteClientSchema = z.strictObject({ const deleteClientSchema = z.strictObject({
clientId: z.string().transform(Number).pipe(z.int().positive()) clientId: z.string().transform(Number).pipe(z.int().positive())
@@ -91,7 +92,7 @@ export async function deleteClient(
await rebuildClientAssociationsFromClient(deletedClient, trx); await rebuildClientAssociationsFromClient(deletedClient, trx);
if (olm) { if (olm) {
await sendTerminateClient(deletedClient.clientId, olm.olmId); // the olmId needs to be provided because it cant look it up after deletion await sendTerminateClient(deletedClient.clientId, OlmErrorCodes.TERMINATED_DELETED, "Deleted", olm.olmId); // the olmId needs to be provided because it cant look it up after deletion
} }
}); });

View File

@@ -1,9 +1,12 @@
import { sendToClient } from "#dynamic/routers/ws"; import { sendToClient } from "#dynamic/routers/ws";
import { db, olms } from "@server/db"; import { db, olms } from "@server/db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { OlmErrorCodes } from "../olm/error";
export async function sendTerminateClient( export async function sendTerminateClient(
clientId: number, clientId: number,
code: (typeof OlmErrorCodes)[keyof typeof OlmErrorCodes],
message: string,
olmId?: string | null olmId?: string | null
) { ) {
if (!olmId) { if (!olmId) {
@@ -20,6 +23,9 @@ export async function sendTerminateClient(
await sendToClient(olmId, { await sendToClient(olmId, {
type: `olm/terminate`, type: `olm/terminate`,
data: {} data: {
code,
message
}
}); });
} }

View File

@@ -10,6 +10,7 @@ import { fromError } from "zod-validation-error";
import logger from "@server/logger"; import logger from "@server/logger";
import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations"; import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations";
import { sendTerminateClient } from "../client/terminate"; import { sendTerminateClient } from "../client/terminate";
import { OlmErrorCodes } from "./error";
const paramsSchema = z const paramsSchema = z
.object({ .object({
@@ -52,7 +53,7 @@ export async function archiveUserOlm(
.where(eq(clients.clientId, client.clientId)); .where(eq(clients.clientId, client.clientId));
await rebuildClientAssociationsFromClient(client, trx); await rebuildClientAssociationsFromClient(client, trx);
await sendTerminateClient(client.clientId, olmId); await sendTerminateClient(client.clientId, OlmErrorCodes.TERMINATED_ARCHIVED, "Archived", olmId);
} }
// Archive the OLM (set archived to true) // Archive the OLM (set archived to true)

View File

@@ -11,6 +11,7 @@ import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations"; import { rebuildClientAssociationsFromClient } from "@server/lib/rebuildClientAssociations";
import { sendTerminateClient } from "../client/terminate"; import { sendTerminateClient } from "../client/terminate";
import { OlmErrorCodes } from "./error";
const paramsSchema = z const paramsSchema = z
.object({ .object({
@@ -76,6 +77,8 @@ export async function deleteUserOlm(
if (olm) { if (olm) {
await sendTerminateClient( await sendTerminateClient(
deletedClient.clientId, deletedClient.clientId,
OlmErrorCodes.TERMINATED_DELETED,
"Deleted",
olm.olmId olm.olmId
); // the olmId needs to be provided because it cant look it up after deletion ); // the olmId needs to be provided because it cant look it up after deletion
} }

View File

@@ -10,7 +10,13 @@ export const OlmErrorCodes = {
USER_ID_NOT_FOUND: "USER_ID_NOT_FOUND", USER_ID_NOT_FOUND: "USER_ID_NOT_FOUND",
INVALID_USER_SESSION: "INVALID_USER_SESSION", INVALID_USER_SESSION: "INVALID_USER_SESSION",
USER_ID_MISMATCH: "USER_ID_MISMATCH", USER_ID_MISMATCH: "USER_ID_MISMATCH",
ACCESS_POLICY_DENIED: "ACCESS_POLICY_DENIED" ACCESS_POLICY_DENIED: "ACCESS_POLICY_DENIED",
TERMINATED_REKEYED: "TERMINATED_REKEYED",
TERMINATED_ORG_DELETED: "TERMINATED_ORG_DELETED",
TERMINATED_INACTIVITY: "TERMINATED_INACTIVITY",
TERMINATED_DELETED: "TERMINATED_DELETED",
TERMINATED_ARCHIVED: "TERMINATED_ARCHIVED",
TERMINATED_BLOCKED: "TERMINATED_BLOCKED"
} as const; } as const;
// Helper function to send registration error // Helper function to send registration error

View File

@@ -10,6 +10,7 @@ import { sendTerminateClient } from "../client/terminate";
import { encodeHexLowerCase } from "@oslojs/encoding"; import { encodeHexLowerCase } from "@oslojs/encoding";
import { sha256 } from "@oslojs/crypto/sha2"; import { sha256 } from "@oslojs/crypto/sha2";
import { sendOlmSyncMessage } from "./sync"; import { sendOlmSyncMessage } from "./sync";
import { OlmErrorCodes } from "./error";
// Track if the offline checker interval is running // Track if the offline checker interval is running
let offlineCheckerInterval: NodeJS.Timeout | null = null; let offlineCheckerInterval: NodeJS.Timeout | null = null;
@@ -64,6 +65,8 @@ export const startOlmOfflineChecker = (): void => {
try { try {
await sendTerminateClient( await sendTerminateClient(
offlineClient.clientId, offlineClient.clientId,
OlmErrorCodes.TERMINATED_INACTIVITY,
"Client terminated due to inactivity",
offlineClient.olmId offlineClient.olmId
); // terminate first ); // terminate first
// wait a moment to ensure the message is sent // wait a moment to ensure the message is sent

View File

@@ -21,6 +21,7 @@ import { fromError } from "zod-validation-error";
import { sendToClient } from "#dynamic/routers/ws"; import { sendToClient } from "#dynamic/routers/ws";
import { deletePeer } from "../gerbil/peers"; import { deletePeer } from "../gerbil/peers";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { OlmErrorCodes } from "../olm/error";
const deleteOrgSchema = z.strictObject({ const deleteOrgSchema = z.strictObject({
orgId: z.string() orgId: z.string()
@@ -208,7 +209,10 @@ export async function deleteOrg(
for (const olmId of olmsToTerminate) { for (const olmId of olmsToTerminate) {
sendToClient(olmId, { sendToClient(olmId, {
type: "olm/terminate", type: "olm/terminate",
data: {} data: {
code: OlmErrorCodes.TERMINATED_REKEYED,
message: "Organization has been deleted"
}
}).catch((error) => { }).catch((error) => {
logger.error( logger.error(
"Failed to send termination message to olm:", "Failed to send termination message to olm:",