Skip to content

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

16.0 KB
Raw Blame
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { testBuiltConnectionWithGateway } from "@app/ee/services/gateway-v2/gateway-v2-fns";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { ResourcePermissionPamResourceActions } from "@app/ee/services/permission/resource-permission";
import { NotFoundError } from "@app/lib/errors";
import { GatewayFailureKind } from "@app/lib/gateway-v2/test-connection-rpc";
import { logger } from "@app/lib/logger";
import { createSshCert, createSshKeyPair, SshCertKeyAlgorithm, SshCertType } from "@app/lib/ssh";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
import { TProjectDALFactory } from "@app/services/project/project-dal";

import { isWindowsRotatableType, redactRotationError, TRotatableType } from "../pam-account-rotation/pam-rotation-fns";
import { PAM_ROTATION_FACTORY_MAP, winrmConnectUsername } from "../pam-account-rotation/pam-rotation-handlers";
import { PamTemplateSettingsSchema } from "../pam-account-template/pam-account-template-schemas";
import {
  buildGatewayConnectionTest,
  CLOUD_CONNECTION_VALIDATORS,
  exceedsOraclePasswordLimit,
  ORACLE_MAX_PASSWORD_LENGTH,
  testVerifiesCredential
} from "../pam-account/pam-account-connection-test";
import { TPamAccountDALFactory, TPamAccountDetail } from "../pam-account/pam-account-dal";
import {
  collectCredentialSecrets,
  parseInternalMetadata,
  validateConnectionDetails,
  validateCredentials
} from "../pam-account/pam-account-schemas";
import { PamAccountType, PamHeartbeatStatus, PamSshAuthMethod } from "../pam/pam-enums";
import { checkAccountAccess, TActorContext } from "../pam/pam-permission";
import { TCheckAccountHeartbeatDTO, TPamHeartbeatResult } from "./pam-account-heartbeat-types";
import {
  classifyCloudProbeError,
  computeNextHeartbeatAt,
  describeFailure,
  GATEWAY_MISSING_CHECK_NOTE,
  gatewayIsMissingCheckSupport,
  HEARTBEAT_SSH_CERT_TTL_SECONDS,
  HEARTBEAT_TIMEOUT_MS,
  isHeartbeatScheduled,
  statusForFailureKind,
  stopsSchedule
} from "./pam-heartbeat-fns";

type TPamAccountHeartbeatServiceFactoryDep = {
  pamAccountDAL: TPamAccountDALFactory;
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
  gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId" | "assertGatewayBindingInOrg">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  permissionService: TPermissionServiceFactory;
  projectDAL: Pick<TProjectDALFactory, "findById">;
  rotationHandlers?: typeof PAM_ROTATION_FACTORY_MAP;
};

export type TPamAccountHeartbeatServiceFactory = ReturnType<typeof pamAccountHeartbeatServiceFactory>;

export const pamAccountHeartbeatServiceFactory = ({
  pamAccountDAL,
  gatewayV2Service,
  gatewayPoolService,
  kmsService,
  permissionService,
  projectDAL,
  rotationHandlers = PAM_ROTATION_FACTORY_MAP
}: TPamAccountHeartbeatServiceFactoryDep) => {
  const withCipher = async (projectId: string) =>
    kmsService.createCipherPairWithDataKey({ type: KmsDataKey.SecretManager, projectId });

  const decrypt = async (projectId: string, blob: Buffer): Promise<Record<string, unknown>> => {
    const { decryptor } = await withCipher(projectId);
    return JSON.parse(decryptor({ cipherTextBlob: blob }).toString()) as Record<string, unknown>;
  };

  const encryptMessage = async (projectId: string, message: string): Promise<Buffer> => {
    const { encryptor } = await withCipher(projectId);
    return encryptor({ plainText: Buffer.from(JSON.stringify({ message })) }).cipherTextBlob;
  };

  const mintEphemeralSshCertificate = async (
    account: TPamAccountDetail,
    credentials: Record<string, unknown>
  ): Promise<Record<string, unknown>> => {
    if (credentials.authMethod !== PamSshAuthMethod.Certificate || !account.encryptedInternalMetadata) {
      return credentials;
    }

    const internalMetadata = parseInternalMetadata(
      account.accountType as PamAccountType,
      await decrypt(account.projectId as string, account.encryptedInternalMetadata)
    );
    if (!internalMetadata?.caPrivateKey) return credentials;

    const keyAlgorithm = (internalMetadata.caKeyAlgorithm as SshCertKeyAlgorithm) || SshCertKeyAlgorithm.ED25519;
    const { publicKey: clientPublicKey, privateKey: clientPrivateKey } = await createSshKeyPair(keyAlgorithm);
    const { signedPublicKey } = await createSshCert({
      caPrivateKey: internalMetadata.caPrivateKey,
      clientPublicKey,
      keyId: `pam-hb-${account.id}`,
      principals: [credentials.username as string],
      requestedTtl: `${HEARTBEAT_SSH_CERT_TTL_SECONDS}s`,
      certType: SshCertType.USER
    });

    return { ...credentials, privateKey: clientPrivateKey, certificate: signedPublicKey };
  };

  const resolveWindowsVerifier = async (
    account: TPamAccountDetail,
    accountType: PamAccountType
  ): Promise<{ username: string; password: string } | undefined> => {
    if (accountType !== PamAccountType.Windows) return undefined;
    if (!account.rotationAccountId || account.rotationAccountId === account.id) return undefined;

    const rotator = await pamAccountDAL.findByIdWithDetails(account.rotationAccountId);
    if (!rotator || rotator.projectId !== account.projectId) return undefined;

    const rotatorCredentials = (await decrypt(account.projectId as string, rotator.encryptedCredentials)) as {
      username?: string;
      password?: string;
    };
    if (!rotatorCredentials.username || !rotatorCredentials.password) return undefined;

    return { username: rotatorCredentials.username, password: rotatorCredentials.password };
  };

  const probe = async (
    account: TPamAccountDetail,
    usedSecrets: string[]
  ): Promise<{ status: PamHeartbeatStatus; message?: string }> => {
    const projectId = account.projectId as string;
    const accountType = account.accountType as PamAccountType;
    const project = await projectDAL.findById(projectId);
    if (!project) throw new NotFoundError({ message: "Project not found" });
    const { orgId } = project;

    if (!account.credentialConfigured) {
      return { status: PamHeartbeatStatus.Unknown, message: "No credential is stored for this account" };
    }

    const connectionDetails = validateConnectionDetails(
      accountType,
      await decrypt(projectId, account.encryptedConnectionDetails)
    ) as Record<string, unknown>;
    const credentials = validateCredentials(
      accountType,
      await decrypt(projectId, account.encryptedCredentials)
    ) as Record<string, unknown>;
    usedSecrets.push(...collectCredentialSecrets(accountType, credentials));

    const validateCloud = CLOUD_CONNECTION_VALIDATORS[accountType];
    if (validateCloud) {
      try {
        await validateCloud({ connectionDetails, credentials, orgId });
        return { status: PamHeartbeatStatus.Healthy };
      } catch (err) {
        const status = classifyCloudProbeError(err);
        return { status, message: err instanceof Error ? err.message : "Unable to validate credentials" };
      }
    }

    await gatewayPoolService.assertGatewayBindingInOrg({
      gatewayId: account.gatewayId ?? account.templateGatewayId,
      gatewayPoolId: account.gatewayPoolId ?? account.templateGatewayPoolId,
      orgId
    });

    // The shared connection test only reaches these over TCP; the rotation handlers hold the real probe.
    if (isWindowsRotatableType(accountType)) {
      const handler = rotationHandlers[accountType as TRotatableType];
      const { username, password } = credentials as { username: string; password?: string };
      if (!password) {
        return { status: PamHeartbeatStatus.Unknown, message: "No password is stored for this account" };
      }

      const verifyVia = await resolveWindowsVerifier(account, accountType);
      if (verifyVia?.password) usedSecrets.push(verifyVia.password);

      // WinRM throws a 401 instead of returning false, which would otherwise read as unreachable and retry.
      let authenticated: boolean;
      try {
        authenticated = await handler.testCredential(
          {
            accountType: accountType as TRotatableType,
            connectionDetails,
            auth: {
              username: winrmConnectUsername(accountType as TRotatableType, connectionDetails, username),
              password
            },
            verifyVia,
            gatewayId: account.gatewayId ?? account.templateGatewayId,
            gatewayPoolId: account.gatewayPoolId ?? account.templateGatewayPoolId
          },
          { gatewayV2Service, gatewayPoolService }
        );
      } catch (err) {
        const kind = (err as { gatewayFailureKind?: GatewayFailureKind | null }).gatewayFailureKind ?? null;
        if (kind === "auth") {
          // The rotator's own login was refused, which says nothing about this account's credential.
          if (verifyVia) {
            return {
              status: PamHeartbeatStatus.CannotCheck,
              message: "The rotation account could not sign in to the host, so this credential was not checked"
            };
          }
          return {
            status: PamHeartbeatStatus.InvalidCredentials,
            message: "The target rejected this credential"
          };
        }
        throw err;
      }

      return authenticated
        ? { status: PamHeartbeatStatus.Healthy }
        : { status: PamHeartbeatStatus.InvalidCredentials, message: "The target rejected this credential" };
    }

    const probeCredentials =
      accountType === PamAccountType.SSH ? await mintEphemeralSshCertificate(account, credentials) : credentials;

    if (exceedsOraclePasswordLimit(accountType, credentials)) {
      return {
        status: PamHeartbeatStatus.CannotCheck,
        message: `This account's password is longer than ${ORACLE_MAX_PASSWORD_LENGTH} characters, so this credential was not checked`
      };
    }

    const test = await buildGatewayConnectionTest(accountType, connectionDetails, probeCredentials, orgId, {
      allowNewerGatewayTests: true
    });
    if (!test) {
      return { status: PamHeartbeatStatus.Unknown, message: "This account type cannot be checked yet" };
    }

    // Every type has a login-based check, so a check without one means the credential is incomplete, not healthy.
    if (!testVerifiesCredential(test.request)) {
      return {
        status: PamHeartbeatStatus.Unknown,
        message: "This account is missing the credential details needed to sign in"
      };
    }

    const gatewayId = await gatewayPoolService.resolveEffectiveGatewayId({
      gatewayId: account.gatewayId ?? account.templateGatewayId,
      gatewayPoolId:
        (account.gatewayId ?? account.templateGatewayId)
          ? null
          : (account.gatewayPoolId ?? account.templateGatewayPoolId)
    });
    if (!gatewayId) {
      return { status: PamHeartbeatStatus.CannotCheck, message: "No gateway is attached to this account" };
    }

    const result = await testBuiltConnectionWithGateway(test, gatewayId, gatewayV2Service, HEARTBEAT_TIMEOUT_MS);

    if (!result) {
      return { status: PamHeartbeatStatus.CannotCheck, message: "The gateway could not be reached" };
    }
    if (!result.ok) {
      if (gatewayIsMissingCheckSupport(result.errorMessage)) {
        return { status: PamHeartbeatStatus.CannotCheck, message: GATEWAY_MISSING_CHECK_NOTE };
      }
      return {
        status: statusForFailureKind(result.kind),
        message: describeFailure(result.kind, result.errorMessage)
      };
    }
    return { status: PamHeartbeatStatus.Healthy };
  };

  const recordResult = async (
    account: TPamAccountDetail,
    outcome: { status: PamHeartbeatStatus; message?: string },
    now: Date
  ) => {
    const projectId = account.projectId as string;
    const heartbeat = PamTemplateSettingsSchema.safeParse(account.templateSettings).data?.heartbeat;

    const nextHeartbeatAt =
      isHeartbeatScheduled(heartbeat) && !stopsSchedule(outcome.status)
        ? computeNextHeartbeatAt({ anchor: now, intervalSeconds: heartbeat.intervalSeconds as number, now })
        : null;

    await pamAccountDAL.updateById(account.id, {
      heartbeatStatus: outcome.status,
      lastHeartbeatAt: now,
      ...(outcome.status === PamHeartbeatStatus.Healthy ? { lastHeartbeatHealthyAt: now } : {}),
      encryptedLastHeartbeatMessage: outcome.message ? await encryptMessage(projectId, outcome.message) : null,
      nextHeartbeatAt
    });
  };

  const runCheck = async (account: TPamAccountDetail): Promise<TPamHeartbeatResult> => {
    const now = new Date();
    const usedSecrets: string[] = [];
    let outcome: { status: PamHeartbeatStatus; message?: string };
    try {
      outcome = await probe(account, usedSecrets);
    } catch (err) {
      const message = redactRotationError(err, usedSecrets);
      // Never the error object: a target can echo the credential back, and no later redaction reaches a log.
      logger.warn(`PAM heartbeat could not complete [accountId=${account.id}]: ${message}`);
      outcome = { status: PamHeartbeatStatus.CannotCheck, message };
    }
    if (outcome.message) {
      outcome = { ...outcome, message: redactRotationError(new Error(outcome.message), usedSecrets) };
    }

    await recordResult(account, outcome, now);

    return {
      accountId: account.id,
      accountName: account.name,
      projectId: account.projectId as string,
      accountType: account.accountType as PamAccountType,
      status: outcome.status,
      ...(outcome.message ? { message: outcome.message } : {})
    };
  };

  const checkScheduledAccount = async (accountId: string): Promise<TPamHeartbeatResult | null> => {
    const account = await pamAccountDAL.findByIdWithDetails(accountId);
    if (!account) return null;

    // Settings can change between the scan that queued this and the worker picking it up.
    const heartbeat = PamTemplateSettingsSchema.safeParse(account.templateSettings).data?.heartbeat;
    if (!isHeartbeatScheduled(heartbeat)) return null;

    return runCheck(account);
  };

  const checkAccount = async ({ accountId, projectId }: TCheckAccountHeartbeatDTO, ctx: TActorContext) => {
    const account = await pamAccountDAL.findByIdWithDetails(accountId);
    if (!account || account.projectId !== projectId) {
      throw new NotFoundError({ message: `Account with ID '${accountId}' not found` });
    }

    // ViewCredentials, not EditAccounts: this sends the credential wherever the account currently points.
    await checkAccountAccess(
      permissionService,
      account.id,
      account.folderId,
      projectId,
      ResourcePermissionPamResourceActions.ViewCredentials,
      ctx
    );

    return runCheck(account);
  };

  const getHeartbeat = async ({ accountId, projectId }: TCheckAccountHeartbeatDTO, ctx: TActorContext) => {
    const account = await pamAccountDAL.findByIdWithDetails(accountId);
    if (!account || account.projectId !== projectId) {
      throw new NotFoundError({ message: `Account with ID '${accountId}' not found` });
    }

    await checkAccountAccess(
      permissionService,
      account.id,
      account.folderId,
      projectId,
      ResourcePermissionPamResourceActions.ReadAccounts,
      ctx
    );

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

    let lastMessage: string | null = null;
    if (account.encryptedLastHeartbeatMessage) {
      const decrypted = await decrypt(projectId, account.encryptedLastHeartbeatMessage);
      lastMessage = (decrypted as { message?: string }).message ?? null;
    }

    return {
      enabled: heartbeat?.enabled ?? false,
      intervalSeconds: heartbeat?.intervalSeconds ?? null,
      status: (account.heartbeatStatus as PamHeartbeatStatus | null) ?? null,
      lastCheckedAt: account.lastHeartbeatAt ?? null,
      lastHealthyAt: account.lastHeartbeatHealthyAt ?? null,
      nextCheckAt: account.nextHeartbeatAt ?? null,
      templateName: account.templateName,
      lastMessage
    };
  };

  return { checkAccount, checkScheduledAccount, getHeartbeat };
};