Skip to content

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

24.2 KB
Raw Blame
import { Knex } from "knex";

import { TDbClient } from "@app/db";
import { TableName, TPamAccounts } from "@app/db/schemas";
import { sanitizeSqlLikeString } from "@app/lib/fn";
import { ormify } from "@app/lib/knex";

import { computeNextHeartbeatAt, heartbeatJitterCapSeconds } from "../pam-account-heartbeat/pam-heartbeat-fns";
import {
  ACCOUNT_NEEDS_ROTATION_ACCOUNT_SQL,
  ACCOUNT_WILL_ROTATE_SQL,
  computeNextRotationAt,
  getRotationReadiness,
  ROTATABLE_ACCOUNT_TYPES,
  rotationJitterCapSeconds
} from "../pam-account-rotation/pam-rotation-fns";
import { PamTemplateSettingsSchema } from "../pam-account-template/pam-account-template-schemas";
import { PamRecordingStorageBackend } from "../pam-session-recording/pam-recording-enums";
import { PamAccountType, PamHeartbeatStatus } from "../pam/pam-enums";
import { ACCOUNT_TYPE_CONFIGS, accountTypeRequiresCredential } from "./pam-account-schemas";

const ROTATABLE_TYPE_VALUES = ROTATABLE_ACCOUNT_TYPES as readonly string[];

export type TPamRotationCandidate = {
  id: string;
  name: string;
  folderId: string | null;
  folderName: string | null;
  accountType: string;
  encryptedConnectionDetails: Buffer;
};

const recordingRequiredAccountTypes = [PamAccountType.Windows, PamAccountType.WindowsAd]
  .map((type) => `'${type}'`)
  .join(", ");

const gatewayExemptAccountTypes = (Object.entries(ACCOUNT_TYPE_CONFIGS) as [string, { requiresGateway?: boolean }][])
  .filter(([, config]) => config.requiresGateway === false)
  .map(([type]) => `'${type}'`)
  .join(", ");

// Derived from accountTypeRequiresCredential so this predicate cannot drift from the TypeScript one in
// getAccountAccessibilityIssues. A type that is reachable without credentials (Redis) has
// credentialConfigured false on every account, so requiring it here would hide those accounts from every
// accessible listing while the dashboard still showed them as launchable.
const credentialExemptAccountTypes = Object.values(PamAccountType)
  .filter((type) => !accountTypeRequiresCredential(type))
  .map((type) => `'${type}'`)
  .join(", ");

// Guarded because an empty exemption list would render as `type in ()`, which is a syntax error rather
// than a no-op.
const credentialConfiguredSql = (accountTable: string, templateTable: string): string =>
  credentialExemptAccountTypes
    ? `("${templateTable}"."type" in (${credentialExemptAccountTypes})
      or "${accountTable}"."credentialConfigured" = true)`
    : `"${accountTable}"."credentialConfigured" = true`;

export const staleAccountExistsSql = (accountTable: string): string =>
  `exists (
    select 1 from "${TableName.PamDiscoveredAccount}"
    where "${TableName.PamDiscoveredAccount}"."importedAccountId" = "${accountTable}"."id"
      and "${TableName.PamDiscoveredAccount}"."isStale" = true
  )`;

export const accountAccessibilitySql = (accountTable: string, templateTable: string): string =>
  `(
    ("${templateTable}"."type" in (${gatewayExemptAccountTypes})
      or "${accountTable}"."gatewayId" is not null or "${accountTable}"."gatewayPoolId" is not null
      or "${templateTable}"."gatewayId" is not null or "${templateTable}"."gatewayPoolId" is not null)
    and ${credentialConfiguredSql(accountTable, templateTable)}
    and ("${templateTable}"."type" not in (${recordingRequiredAccountTypes})
      or (
        "${templateTable}"."settings"->>'recordingStorageBackend' = '${PamRecordingStorageBackend.AwsS3}'
        and ("${accountTable}"."recordingConnectionId" is not null
          or "${templateTable}"."recordingConnectionId" is not null)
        and coalesce(
          "${accountTable}"."settingsOverrides"->'recordingS3Config',
          "${templateTable}"."settings"->'recordingS3Config'
        ) is not null
      ))
  )`;

type TPamAccountTemplateInheritedFields = {
  credentialConfigured: boolean;
  encryptedCredentials: Buffer;
  templateGatewayId: string | null;
  templateGatewayPoolId: string | null;
  templateRecordingConnectionId: string | null;
  templateSettings: unknown;
  templatePolicies: unknown;
};

export type TPamAccountListItem = Pick<
  TPamAccounts,
  | "id"
  | "name"
  | "description"
  | "folderId"
  | "projectId"
  | "templateId"
  | "gatewayId"
  | "gatewayPoolId"
  | "recordingConnectionId"
  | "settingsOverrides"
  | "heartbeatStatus"
  | "createdAt"
  | "updatedAt"
> &
  TPamAccountTemplateInheritedFields & {
    accountType: string;
    templateName: string;
    folderName: string | null;
    isStale: boolean;
    heartbeatEnabled: boolean;
  };

export type TPamAccountDetail = TPamAccounts &
  TPamAccountTemplateInheritedFields & {
    accountType: string;
    templateName: string;
    templatePolicies: unknown;
    templateSettings: unknown;
    folderName: string | null;
    isStale: boolean;
  };

const effectiveGatewayIdSql = `coalesce("${TableName.PamAccount}"."gatewayId", "${TableName.PamAccountTemplate}"."gatewayId")`;

export type TPamAccountDALFactory = ReturnType<typeof pamAccountDALFactory>;

export const pamAccountDALFactory = (db: TDbClient) => {
  const orm = ormify(db, TableName.PamAccount);

  const findAccessible = async (
    projectId: string,
    accessibleFolderIds: string[],
    accessibleAccountIds: string[],
    filters?: {
      folderId?: string;
      templateId?: string;
      accountType?: string;
      search?: string;
      onlyAccessible?: boolean;
      offset?: number;
      limit?: number;
    },
    tx?: Knex
  ) => {
    const baseQuery = (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .where(`${TableName.PamAccount}.projectId`, projectId)
      .where((builder) => {
        if (accessibleFolderIds.length > 0) {
          void builder.whereIn(`${TableName.PamAccount}.folderId`, accessibleFolderIds);
        }
        if (accessibleAccountIds.length > 0) {
          void builder.orWhereIn(`${TableName.PamAccount}.id`, accessibleAccountIds);
        }
      });

    if (filters?.folderId) {
      void baseQuery.where(`${TableName.PamAccount}.folderId`, filters.folderId);
    }
    if (filters?.templateId) {
      void baseQuery.where(`${TableName.PamAccount}.templateId`, filters.templateId);
    }
    if (filters?.accountType) {
      void baseQuery.where(`${TableName.PamAccountTemplate}.type`, filters.accountType);
    }
    if (filters?.search) {
      const pattern = `%${sanitizeSqlLikeString(filters.search)}%`;
      void baseQuery.whereILike(`${TableName.PamAccount}.name`, pattern);
    }
    if (filters?.onlyAccessible) {
      void baseQuery.whereRaw(accountAccessibilitySql(TableName.PamAccount, TableName.PamAccountTemplate));
    }

    const countQuery = baseQuery
      .clone()
      .clearSelect()
      .count(`${TableName.PamAccount}.id as count`)
      .first<{ count: string }>();

    const dataQuery = baseQuery
      .clone()
      .select(
        `${TableName.PamAccount}.id`,
        `${TableName.PamAccount}.name`,
        `${TableName.PamAccount}.description`,
        `${TableName.PamAccount}.folderId`,
        `${TableName.PamAccount}.projectId`,
        `${TableName.PamAccount}.templateId`,
        `${TableName.PamAccount}.gatewayId`,
        `${TableName.PamAccount}.gatewayPoolId`,
        `${TableName.PamAccount}.recordingConnectionId`,
        `${TableName.PamAccount}.settingsOverrides`,
        `${TableName.PamAccount}.credentialConfigured`,
        `${TableName.PamAccount}.encryptedCredentials`,
        `${TableName.PamAccount}.createdAt`,
        `${TableName.PamAccount}.updatedAt`,
        `${TableName.PamAccountTemplate}.type as accountType`,
        `${TableName.PamAccountTemplate}.name as templateName`,
        `${TableName.PamAccountTemplate}.settings as templateSettings`,
        `${TableName.PamAccountTemplate}.policies as templatePolicies`,
        `${TableName.PamAccountTemplate}.gatewayId as templateGatewayId`,
        `${TableName.PamAccountTemplate}.gatewayPoolId as templateGatewayPoolId`,
        `${TableName.PamAccountTemplate}.recordingConnectionId as templateRecordingConnectionId`,
        `${TableName.PamFolder}.name as folderName`,
        `${TableName.PamAccount}.heartbeatStatus`,
        db.raw(
          `("${TableName.PamAccountTemplate}"."settings"->'heartbeat'->>'enabled' = 'true'
            and "${TableName.PamAccountTemplate}"."settings"->'heartbeat'->>'intervalSeconds' is not null) as "heartbeatEnabled"`
        ),
        db.raw(`${staleAccountExistsSql(TableName.PamAccount)} as "isStale"`)
      )
      .orderBy(`${TableName.PamFolder}.name`, "asc")
      .orderBy(`${TableName.PamAccount}.name`, "asc");

    if (filters?.limit) void dataQuery.limit(filters.limit);
    if (filters?.offset) void dataQuery.offset(filters.offset);

    const [countResult, accounts] = await Promise.all([countQuery, dataQuery]);

    return {
      accounts: accounts as unknown as TPamAccountListItem[],
      totalCount: Number(countResult?.count ?? 0)
    };
  };

  const detailSelect = [
    `${TableName.PamAccount}.*`,
    `${TableName.PamAccountTemplate}.type as accountType`,
    `${TableName.PamAccountTemplate}.name as templateName`,
    `${TableName.PamAccountTemplate}.policies as templatePolicies`,
    `${TableName.PamAccountTemplate}.settings as templateSettings`,
    `${TableName.PamAccountTemplate}.gatewayId as templateGatewayId`,
    `${TableName.PamAccountTemplate}.gatewayPoolId as templateGatewayPoolId`,
    `${TableName.PamAccountTemplate}.recordingConnectionId as templateRecordingConnectionId`,
    `${TableName.PamFolder}.name as folderName`,
    db.raw(`${staleAccountExistsSql(TableName.PamAccount)} as "isStale"`)
  ];

  const findByIdWithDetails = async (accountId: string, tx?: Knex): Promise<TPamAccountDetail | null> => {
    const rows = (await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .where(`${TableName.PamAccount}.id`, accountId)
      .select(detailSelect)) as unknown as TPamAccountDetail[];

    return rows[0] || null;
  };

  // Locks the account's template and then the account, the order a template save takes them, so neither
  // changes before the caller's transaction commits and the two can't deadlock
  const findTemplatePoliciesForShare = async (accountId: string, tx: Knex) => {
    const account = await tx(TableName.PamAccount).where({ id: accountId }).first("templateId");
    if (!account) return undefined;

    const template = await tx(TableName.PamAccountTemplate)
      .where({ id: account.templateId })
      .forShare()
      .first("policies");
    const locked = await tx(TableName.PamAccount)
      .where({ id: accountId })
      .forShare()
      .first("projectId", "folderId", "templateId");
    // The account moved to another template before it was locked, so these policies may not be its own
    if (!template || !locked || locked.templateId !== account.templateId) return undefined;

    return { projectId: locked.projectId, folderId: locked.folderId, templatePolicies: template.policies as unknown };
  };

  const findByIdsWithDetails = async (
    accountIds: string[],
    accountType: string,
    tx?: Knex
  ): Promise<TPamAccountDetail[]> => {
    if (!accountIds.length) return [];
    return (await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .whereIn(`${TableName.PamAccount}.id`, accountIds)
      .where(`${TableName.PamAccountTemplate}.type`, accountType)
      .select(detailSelect)) as unknown as TPamAccountDetail[];
  };

  // Due, rotatable, enabled; excludes soft-deleted projects (rotating their accounts is a side effect during cleanup).
  const dueRotationQuery = (now: Date, tx?: Knex) =>
    (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .join(TableName.Project, `${TableName.PamAccount}.projectId`, `${TableName.Project}.id`)
      .whereNull(`${TableName.Project}.deleteAfter`)
      .whereNotNull(`${TableName.PamAccount}.nextRotationAt`)
      .where(`${TableName.PamAccount}.nextRotationAt`, "<=", now)
      .whereIn(`${TableName.PamAccountTemplate}.type`, ROTATABLE_TYPE_VALUES)
      .whereRaw(`"${TableName.PamAccountTemplate}"."settings"->'rotation'->>'enabled' = 'true'`);

  // Only ready accounts get a nextRotationAt, so every due row here is already rotation-ready.
  const findAccountsToRotate = async (now: Date, limit: number, tx?: Knex): Promise<TPamAccountDetail[]> => {
    const rows = (await dueRotationQuery(now, tx)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .orderBy(`${TableName.PamAccount}.nextRotationAt`, "asc")
      .limit(limit)
      .select(detailSelect)) as unknown as TPamAccountDetail[];
    return rows;
  };

  const countAccountsToRotate = async (now: Date, tx?: Knex): Promise<number> => {
    const result = await dueRotationQuery(now, tx).count<[{ count: string }]>(`${TableName.PamAccount}.id`);
    return Number(result[0]?.count ?? 0);
  };

  const dueHeartbeatQuery = (now: Date, tx?: Knex) =>
    (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .join(TableName.Project, `${TableName.PamAccount}.projectId`, `${TableName.Project}.id`)
      .whereNull(`${TableName.Project}.deleteAfter`)
      .whereNotNull(`${TableName.PamAccount}.nextHeartbeatAt`)
      .where(`${TableName.PamAccount}.nextHeartbeatAt`, "<=", now)
      // Both halves of isHeartbeatScheduled: an enabled config with no interval is manual-only.
      .whereRaw(`"${TableName.PamAccountTemplate}"."settings"->'heartbeat'->>'enabled' = 'true'`)
      .whereRaw(`"${TableName.PamAccountTemplate}"."settings"->'heartbeat'->>'intervalSeconds' is not null`);

  const findAccountsToHeartbeat = async (now: Date, limit: number, tx?: Knex): Promise<TPamAccountDetail[]> => {
    return (await dueHeartbeatQuery(now, tx)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .orderBy(`${TableName.PamAccount}.nextHeartbeatAt`, "asc")
      .limit(limit)
      .select(detailSelect)) as unknown as TPamAccountDetail[];
  };

  const countAccountsToHeartbeat = async (now: Date, tx?: Knex): Promise<number> => {
    const result = await dueHeartbeatQuery(now, tx).count<[{ count: string }]>(`${TableName.PamAccount}.id`);
    return Number(result[0]?.count ?? 0);
  };

  // Accounts stopped by a rejected credential stay stopped: a settings change is not what clears that.
  const reconcileHeartbeatScheduleForTemplate = async (
    templateId: string,
    opts?: { rescheduleAll?: boolean },
    tx?: Knex
  ): Promise<void> => {
    const dbClient = tx || db;
    const template = await dbClient(TableName.PamAccountTemplate)
      .where({ id: templateId })
      .first<{ settings: unknown } | undefined>("settings");
    if (!template) return;

    const heartbeat = PamTemplateSettingsSchema.safeParse(template.settings).data?.heartbeat;

    if (!heartbeat?.enabled || heartbeat.intervalSeconds == null) {
      await dbClient(TableName.PamAccount)
        .where({ templateId })
        .whereNotNull("nextHeartbeatAt")
        .update({ nextHeartbeatAt: null });
      return;
    }

    const { intervalSeconds } = heartbeat;
    const jitterCapSeconds = heartbeatJitterCapSeconds(intervalSeconds);

    // NULL-safe on purpose: a never-checked account has a null status, which `whereNot` would drop.
    const update = dbClient(TableName.PamAccount)
      .where({ templateId })
      .whereRaw(`("heartbeatStatus" is null or "heartbeatStatus" <> ?)`, [PamHeartbeatStatus.InvalidCredentials]);
    if (!opts?.rescheduleAll) {
      void update.whereNull("nextHeartbeatAt");
    }
    await update.update({
      nextHeartbeatAt: dbClient.raw(
        `GREATEST(COALESCE(??, now()) + make_interval(secs => ?), now()) + make_interval(secs => floor(random() * ?)::int)`,
        ["lastHeartbeatAt", intervalSeconds, jitterCapSeconds]
      ) as unknown as Date
    });
  };

  const reconcileHeartbeatScheduleForAccount = async (accountId: string, tx?: Knex): Promise<void> => {
    const account = await findByIdWithDetails(accountId, tx ?? db);
    if (!account) return;

    const heartbeat = PamTemplateSettingsSchema.safeParse(account.templateSettings).data?.heartbeat;
    const current = account.nextHeartbeatAt ?? null;

    let nextHeartbeatAt: Date | null = current;
    if (!heartbeat?.enabled || heartbeat.intervalSeconds == null) {
      nextHeartbeatAt = null;
    } else if (!current && account.heartbeatStatus === PamHeartbeatStatus.InvalidCredentials) {
      nextHeartbeatAt = null;
    } else if (!current) {
      nextHeartbeatAt = computeNextHeartbeatAt({
        anchor: account.lastHeartbeatAt ?? null,
        intervalSeconds: heartbeat.intervalSeconds,
        now: new Date()
      });
    }

    if ((nextHeartbeatAt?.getTime() ?? null) !== (current?.getTime() ?? null)) {
      await (tx || db)(TableName.PamAccount).where({ id: accountId }).update({ nextHeartbeatAt });
    }
  };

  // Total PAM accounts across the org's projects (excluding soft-deleted projects), used to enforce the
  // plan's maxPamAccounts at creation time.
  const countByOrgId = async (orgId: string, tx?: Knex): Promise<number> => {
    const result = await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.Project, `${TableName.PamAccount}.projectId`, `${TableName.Project}.id`)
      .where(`${TableName.Project}.orgId`, orgId)
      .whereNull(`${TableName.Project}.deleteAfter`)
      .count<[{ count: string }]>(`${TableName.PamAccount}.id`);
    return Number(result[0]?.count ?? 0);
  };

  const findRotationCandidates = async (
    projectId: string,
    accessibleFolderIds: string[],
    accessibleAccountIds: string[],
    accountType: string,
    tx?: Knex
  ): Promise<TPamRotationCandidate[]> => {
    if (accessibleFolderIds.length === 0 && accessibleAccountIds.length === 0) return [];

    const rows = (await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .where(`${TableName.PamAccount}.projectId`, projectId)
      .where(`${TableName.PamAccountTemplate}.type`, accountType)
      // A delegated rotator authenticates with its own stored password, so exclude accounts without one.
      .where(`${TableName.PamAccount}.credentialConfigured`, true)
      .where((builder) => {
        if (accessibleFolderIds.length > 0) {
          void builder.whereIn(`${TableName.PamAccount}.folderId`, accessibleFolderIds);
        }
        if (accessibleAccountIds.length > 0) {
          void builder.orWhereIn(`${TableName.PamAccount}.id`, accessibleAccountIds);
        }
      })
      .select(
        `${TableName.PamAccount}.id`,
        `${TableName.PamAccount}.name`,
        `${TableName.PamAccount}.folderId`,
        `${TableName.PamAccount}.encryptedConnectionDetails`,
        `${TableName.PamAccountTemplate}.type as accountType`,
        `${TableName.PamFolder}.name as folderName`
      )
      .orderBy(`${TableName.PamFolder}.name`, "asc")
      .orderBy(`${TableName.PamAccount}.name`, "asc")) as unknown as TPamRotationCandidate[];
    return rows;
  };

  // Bulk set-based reschedule of a template's accounts; rescheduleReady also recomputes already-scheduled ones.
  const reconcileRotationScheduleForTemplate = async (
    templateId: string,
    opts?: { rescheduleReady?: boolean },
    tx?: Knex
  ): Promise<void> => {
    const dbClient = tx || db;
    const template = await dbClient(TableName.PamAccountTemplate)
      .where({ id: templateId })
      .first<{ type: string; settings: unknown } | undefined>("type", "settings");
    if (!template) return;

    const parsed = PamTemplateSettingsSchema.safeParse(template.settings);
    const rotation = parsed.success ? parsed.data.rotation : undefined;
    const enabled = rotation?.enabled === true && ROTATABLE_TYPE_VALUES.includes(template.type);

    if (!enabled || !rotation || rotation.intervalSeconds == null) {
      await dbClient(TableName.PamAccount)
        .where({ templateId })
        .whereNotNull("nextRotationAt")
        .update({ nextRotationAt: null });
      return;
    }

    const { intervalSeconds } = rotation;
    const jitterCapSeconds = rotationJitterCapSeconds(intervalSeconds);

    await dbClient(TableName.PamAccount)
      .where({ templateId })
      .whereNotNull("nextRotationAt")
      .whereRaw(`(${ACCOUNT_NEEDS_ROTATION_ACCOUNT_SQL})`)
      .update({ nextRotationAt: null });

    const readyUpdate = dbClient(TableName.PamAccount).where({ templateId }).whereRaw(`(${ACCOUNT_WILL_ROTATE_SQL})`);
    if (!opts?.rescheduleReady) {
      void readyUpdate.whereNull("nextRotationAt");
    }
    await readyUpdate.update({
      nextRotationAt: dbClient.raw(
        `GREATEST(COALESCE(??, now()) + make_interval(secs => ?), now()) + make_interval(secs => floor(random() * ?)::int)`,
        ["lastRotatedAt", intervalSeconds, jitterCapSeconds]
      ) as unknown as Date
    });
  };

  const reconcileRotationScheduleForAccount = async (accountId: string, tx?: Knex): Promise<void> => {
    const account = await findByIdWithDetails(accountId, tx ?? db);
    if (!account) return;

    const readiness = getRotationReadiness({
      accountId: account.id,
      accountType: account.accountType,
      rotationAccountId: account.rotationAccountId,
      credentialConfigured: account.credentialConfigured
    });
    const rotation = PamTemplateSettingsSchema.safeParse(account.templateSettings).data?.rotation;
    const current = account.nextRotationAt ?? null;

    let nextRotationAt: Date | null = current;
    if (!readiness.ready || !rotation?.enabled || rotation.intervalSeconds == null) {
      nextRotationAt = null;
    } else if (!current) {
      nextRotationAt = computeNextRotationAt({
        anchor: account.lastRotatedAt ?? null,
        intervalSeconds: rotation.intervalSeconds,
        now: new Date()
      });
    }

    if ((nextRotationAt?.getTime() ?? null) !== (current?.getTime() ?? null)) {
      await (tx || db)(TableName.PamAccount).where({ id: accountId }).update({ nextRotationAt });
    }
  };

  const findByGatewayId = async (gatewayId: string, tx?: Knex) => {
    const docs = await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .leftJoin(TableName.PamFolder, `${TableName.PamAccount}.folderId`, `${TableName.PamFolder}.id`)
      .whereRaw(`${effectiveGatewayIdSql} = ?`, [gatewayId])
      .select(
        `${TableName.PamAccount}.id`,
        `${TableName.PamAccount}.name`,
        `${TableName.PamAccountTemplate}.type as accountType`,
        `${TableName.PamFolder}.name as folderName`
      );

    return docs as { id: string; name: string; accountType: string; folderName: string | null }[];
  };

  const countByGatewayIds = async (gatewayIds: string[], tx?: Knex) =>
    (await (tx || db.replicaNode())(TableName.PamAccount)
      .join(TableName.PamAccountTemplate, `${TableName.PamAccount}.templateId`, `${TableName.PamAccountTemplate}.id`)
      .whereRaw(`${effectiveGatewayIdSql} = ANY(?::uuid[])`, [gatewayIds])
      .groupByRaw(effectiveGatewayIdSql)
      .select(db.raw(`${effectiveGatewayIdSql} as id`), db.raw("count(*)::int as count"))) as {
      id: string;
      count: number;
    }[];

  return {
    ...orm,
    findByGatewayId,
    countByGatewayIds,
    findAccessible,
    findByIdWithDetails,
    findTemplatePoliciesForShare,
    findByIdsWithDetails,
    findAccountsToRotate,
    countAccountsToRotate,
    findAccountsToHeartbeat,
    countAccountsToHeartbeat,
    reconcileHeartbeatScheduleForTemplate,
    reconcileHeartbeatScheduleForAccount,
    countByOrgId,
    findRotationCandidates,
    reconcileRotationScheduleForTemplate,
    reconcileRotationScheduleForAccount,
    primaryNode: () => db.primaryNode()
  };
};