Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

5.6 KB
Raw Blame
import { z } from "zod";

import { RemindersSchema } from "@app/db/schemas/reminders";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { MAX_CHANNELS_PER_ALERT, MAX_RECIPIENTS_PER_CHANNEL } from "@app/services/alert/alert-types";
import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

import { UpdateChannelInputSchema } from "../alert-channel-schemas";

export const registerSecretReminderRouter = async (server: FastifyZodProvider) => {
  server.route({
    url: "/:secretId",
    method: "POST",
    config: {
      rateLimit: writeLimit
    },
    schema: {
      operationId: "createSecretReminder",
      params: z.object({
        secretId: z.string().uuid()
      }),
      body: z
        .object({
          message: z.string().trim().max(1024).optional(),
          repeatDays: z.number().min(1).nullable().optional(),
          nextReminderDate: z.string().datetime().nullable().optional(),
          fromDate: z.string().datetime().nullable().optional(),
          recipients: z
            .string()
            .uuid()
            .array()
            .max(MAX_RECIPIENTS_PER_CHANNEL * MAX_CHANNELS_PER_ALERT)
            .optional(),
          // The reminder alert's complete channel list, saved with the schedule. Replaces `recipients`.
          channels: z.array(UpdateChannelInputSchema).min(1).max(MAX_CHANNELS_PER_ALERT).optional()
        })
        .refine((data) => {
          return data.repeatDays || data.nextReminderDate;
        }, "At least one of repeatDays or nextReminderDate is required"),
      response: {
        200: z.object({
          message: z.string()
        })
      }
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),
    handler: async (req) => {
      await server.services.reminder.createReminder({
        actorId: req.permission.id,
        actor: req.permission.type,
        actorOrgId: req.permission.orgId,
        actorAuthMethod: req.permission.authMethod,
        reminder: {
          secretId: req.params.secretId,
          message: req.body.message,
          repeatDays: req.body.repeatDays,
          nextReminderDate: req.body.nextReminderDate,
          fromDate: req.body.fromDate,
          recipients: req.body.recipients,
          channels: req.body.channels
        }
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: req.permission.orgId,
        event: {
          type: EventType.CREATE_SECRET_REMINDER,
          metadata: {
            secretId: req.params.secretId,
            message: req.body.message,
            repeatDays: req.body.repeatDays,
            nextReminderDate: req.body.nextReminderDate,
            recipients: req.body.recipients
          }
        }
      });

      await server.services.telemetry.sendPostHogEvents({
        event: PostHogEventTypes.SecretReminderCreated,
        distinctId: getTelemetryDistinctId(req),
        organizationId: req.permission.orgId,
        properties: {
          secretId: req.params.secretId,
          reminderRepeatDays: req.body.repeatDays,
          hasNote: !!req.body.message,
          isOneTime: !req.body.repeatDays
        }
      });

      return { message: "Successfully created reminder" };
    }
  });

  server.route({
    url: "/:secretId",
    method: "GET",
    config: {
      rateLimit: readLimit
    },
    schema: {
      operationId: "getSecretReminder",
      params: z.object({
        secretId: z.string().uuid()
      }),
      response: {
        200: z.object({
          reminder: RemindersSchema.extend({
            recipients: z.string().array().optional()
          })
            .optional()
            .nullable()
        })
      }
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),
    handler: async (req) => {
      const reminder = await server.services.reminder.getReminder({
        actorId: req.permission.id,
        actor: req.permission.type,
        actorOrgId: req.permission.orgId,
        actorAuthMethod: req.permission.authMethod,
        secretId: req.params.secretId
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: req.permission.orgId,
        event: {
          type: EventType.GET_SECRET_REMINDER,
          metadata: {
            secretId: req.params.secretId
          }
        }
      });
      return { reminder };
    }
  });

  server.route({
    url: "/:secretId",
    method: "DELETE",
    config: {
      rateLimit: writeLimit
    },
    schema: {
      operationId: "deleteSecretReminder",
      params: z.object({
        secretId: z.string().uuid()
      }),
      response: {
        200: z.object({
          message: z.string()
        })
      }
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),
    handler: async (req) => {
      await server.services.reminder.deleteReminder({
        actorId: req.permission.id,
        actor: req.permission.type,
        actorOrgId: req.permission.orgId,
        actorAuthMethod: req.permission.authMethod,
        secretId: req.params.secretId
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: req.permission.orgId,
        event: {
          type: EventType.DELETE_SECRET_REMINDER,
          metadata: {
            secretId: req.params.secretId
          }
        }
      });
      return { message: "Successfully deleted reminder" };
    }
  });
};