Skip to content

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

21.5 KB
Raw Blame
import { ForbiddenError, subject } from "@casl/ability";
import RE2 from "re2";

import { ActionProjectType } from "@app/db/schemas";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
  ProjectPermissionDynamicSecretActions,
  ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { getConfig } from "@app/lib/config/env";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { ms } from "@app/lib/ms";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import {
  recordDynamicSecretRevocationOutcomeMetric,
  recordDynamicSecretUnrecordedCredentialMetric
} from "@app/lib/telemetry/metrics";
import { ActorType } from "@app/services/auth/auth-type";
import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
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 { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
import { SecretValidationRuleType } from "@app/services/secret-validation-rule/secret-validation-rule-enums";
import { convertDynamicSecretProviderToValidationRuleProvider } from "@app/services/secret-validation-rule/secret-validation-rule-fns";
import { TSecretValidationRuleServiceFactory } from "@app/services/secret-validation-rule/secret-validation-rule-service";
import { TUserDALFactory } from "@app/services/user/user-dal";

import { TDynamicSecretDALFactory } from "../dynamic-secret/dynamic-secret-dal";
import { DynamicSecretProviders, TDynamicProviderFns } from "../dynamic-secret/providers/models";
import { toSafeUsername } from "../dynamic-secret/providers/templateUtils";
import { TDynamicSecretLeaseDALFactory } from "./dynamic-secret-lease-dal";
import {
  decryptLeaseData,
  pickPersistedLeaseData,
  recordDynamicSecretRevocationResult
} from "./dynamic-secret-lease-fns";
import { TDynamicSecretLeaseQueueServiceFactory } from "./dynamic-secret-lease-queue";
import {
  DynamicSecretLeaseStatus,
  TDynamicSecretLeaseConfig,
  TDynamicSecretLeaseServiceFactory
} from "./dynamic-secret-lease-types";

type TDynamicSecretLeaseServiceFactoryDep = {
  dynamicSecretLeaseDAL: TDynamicSecretLeaseDALFactory;
  dynamicSecretDAL: Pick<TDynamicSecretDALFactory, "findOne" | "update">;
  dynamicSecretProviders: Record<DynamicSecretProviders, TDynamicProviderFns>;
  dynamicSecretQueueService: TDynamicSecretLeaseQueueServiceFactory;
  licenseService: Pick<TLicenseServiceFactory, "getPlan">;
  folderDAL: Pick<TSecretFolderDALFactory, "findBySecretPath">;
  permissionService: Pick<TPermissionServiceFactory, "getProjectPermission">;
  projectDAL: Pick<TProjectDALFactory, "findProjectBySlug">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  userDAL: Pick<TUserDALFactory, "findById">;
  identityDAL: TIdentityDALFactory;
  secretValidationRuleService: Pick<TSecretValidationRuleServiceFactory, "findConstraintsForGeneratedSecret">;
};

export const dynamicSecretLeaseServiceFactory = ({
  dynamicSecretLeaseDAL,
  dynamicSecretProviders,
  dynamicSecretDAL,
  folderDAL,
  permissionService,
  dynamicSecretQueueService,
  projectDAL,
  licenseService,
  kmsService,
  userDAL,
  identityDAL,
  secretValidationRuleService
}: TDynamicSecretLeaseServiceFactoryDep): TDynamicSecretLeaseServiceFactory => {
  const extractEmailUsername = (email: string) => {
    const regex = new RE2(/^([^@]+)/);
    const match = email.match(regex);
    return match ? match[1] : email;
  };

  const create: TDynamicSecretLeaseServiceFactory["create"] = async ({
    environmentSlug,
    path,
    name,
    projectSlug,
    actor,
    actorId,
    actorOrgId,
    actorAuthMethod,
    ttl,
    config
  }) => {
    const appCfg = getConfig();
    const project = await projectDAL.findProjectBySlug(projectSlug, actorOrgId);
    if (!project) throw new NotFoundError({ message: `Project with slug '${projectSlug}' not found` });

    const projectId = project.id;
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.SecretManager
    });

    const plan = await licenseService.getPlan(actorOrgId);
    if (!plan?.dynamicSecret) {
      throw new BadRequestError({
        message: "Failed to create lease due to plan restriction. Upgrade plan to create dynamic secret."
      });
    }

    const folder = await folderDAL.findBySecretPath(projectId, environmentSlug, path);
    if (!folder)
      throw new NotFoundError({
        message: `Folder with path '${path}' in environment with slug '${environmentSlug}' not found`
      });

    const dynamicSecretCfg = await dynamicSecretDAL.findOne({ name, folderId: folder.id });
    if (!dynamicSecretCfg)
      throw new NotFoundError({
        message: `Dynamic secret with name '${name}' in folder with path '${path}' not found`
      });

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionDynamicSecretActions.Lease,
      subject(ProjectPermissionSub.DynamicSecrets, {
        environment: environmentSlug,
        secretPath: path,
        metadata: dynamicSecretCfg.metadata
      })
    );

    const totalLeasesTaken = await dynamicSecretLeaseDAL.countLeasesForDynamicSecret(dynamicSecretCfg.id);
    if (totalLeasesTaken >= appCfg.MAX_LEASE_LIMIT)
      throw new BadRequestError({ message: `Max lease limit reached. Limit: ${appCfg.MAX_LEASE_LIMIT}` });

    const selectedProvider = dynamicSecretProviders[dynamicSecretCfg.type as DynamicSecretProviders];

    const { encryptor: secretManagerEncryptor, decryptor: secretManagerDecryptor } =
      await kmsService.createCipherPairWithDataKey({
        type: KmsDataKey.SecretManager,
        projectId
      });

    const decryptedStoredInput = JSON.parse(
      secretManagerDecryptor({ cipherTextBlob: Buffer.from(dynamicSecretCfg.encryptedInput) }).toString()
    ) as object;

    const selectedTTL = ttl || dynamicSecretCfg.defaultTTL;
    const { maxTTL } = dynamicSecretCfg;
    const expireAt = new Date(new Date().getTime() + ms(selectedTTL));
    if (maxTTL) {
      const maxExpiryDate = new Date(new Date().getTime() + ms(maxTTL));
      if (expireAt > maxExpiryDate) throw new BadRequestError({ message: "TTL cannot be larger than max TTL" });
    }

    let result;
    try {
      const identity: { name: string } = { name: "" };
      if (actor === ActorType.USER) {
        const user = await requestMemoize(requestMemoKeys.userFindById(actorId), () => userDAL.findById(actorId));
        if (user) {
          identity.name = toSafeUsername(extractEmailUsername(user.username));
        }
      } else if (actor === ActorType.IDENTITY) {
        const machineIdentity = await requestMemoize(requestMemoKeys.identityFindById(actorId), () =>
          identityDAL.findById(actorId)
        );
        if (machineIdentity) {
          identity.name = toSafeUsername(machineIdentity.name);
        }
      }
      const ruleProvider = convertDynamicSecretProviderToValidationRuleProvider(
        dynamicSecretCfg.type as DynamicSecretProviders
      );

      let passwordValidation;
      if (ruleProvider) {
        const matched = await secretValidationRuleService.findConstraintsForGeneratedSecret({
          projectId,
          envId: folder.envId,
          secretPath: path,
          type: SecretValidationRuleType.DynamicSecrets,
          provider: ruleProvider
        });
        if (matched.ruleNames.length) {
          passwordValidation = matched;
        }
      }

      result = await selectedProvider.create({
        inputs: decryptedStoredInput,
        expireAt: expireAt.getTime(),
        usernameTemplate: dynamicSecretCfg.usernameTemplate,
        identity,
        dynamicSecret: dynamicSecretCfg,
        metadata: { projectId, passwordValidation },
        config
      });
    } catch (error: unknown) {
      if (error && typeof error === "object" && error !== null && "sqlMessage" in error) {
        throw new BadRequestError({ message: error.sqlMessage as string });
      }
      throw error;
    }
    const { entityId, data } = result;

    const leaseData = selectedProvider.persistedLeaseFields?.length
      ? pickPersistedLeaseData(data, selectedProvider.persistedLeaseFields)
      : undefined;
    const encryptedLeaseData = leaseData
      ? secretManagerEncryptor({ plainText: Buffer.from(JSON.stringify(leaseData)) }).cipherTextBlob
      : undefined;

    let dynamicSecretLease;
    try {
      dynamicSecretLease = await dynamicSecretLeaseDAL.create({
        expireAt,
        version: 1,
        dynamicSecretId: dynamicSecretCfg.id,
        externalEntityId: entityId,
        config,
        encryptedLeaseData
      });
    } catch (error) {
      // the credential already exists upstream; without a lease row nothing would ever revoke it
      await selectedProvider
        .revoke(decryptedStoredInput, entityId, { projectId, leaseData }, config)
        .catch((revokeError: Error) => {
          recordDynamicSecretUnrecordedCredentialMetric({ provider: dynamicSecretCfg.type });
          logger.error(
            revokeError,
            `Failed to revoke credential after lease insert failed [dynamicSecretId=${dynamicSecretCfg.id}] [provider=${dynamicSecretCfg.type}]`
          );
        });
      throw error;
    }

    await dynamicSecretQueueService.setLeaseRevocation(dynamicSecretLease.id, dynamicSecretCfg.id, expireAt);
    return {
      lease: dynamicSecretLease,
      dynamicSecret: dynamicSecretCfg,
      data,
      projectId,
      environment: environmentSlug,
      secretPath: path
    };
  };

  const renewLease: TDynamicSecretLeaseServiceFactory["renewLease"] = async ({
    ttl,
    actorAuthMethod,
    actorOrgId,
    actorId,
    actor,
    projectSlug,
    path,
    environmentSlug,
    leaseId
  }) => {
    const project = await projectDAL.findProjectBySlug(projectSlug, actorOrgId);
    if (!project) throw new NotFoundError({ message: `Project with slug '${projectSlug}' not found` });

    const projectId = project.id;
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.SecretManager
    });

    const { decryptor: secretManagerDecryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.SecretManager,
      projectId
    });

    const plan = await licenseService.getPlan(actorOrgId);
    if (!plan?.dynamicSecret) {
      throw new BadRequestError({
        message: "Failed to renew lease due to plan restriction. Upgrade plan to create dynamic secret."
      });
    }

    const folder = await folderDAL.findBySecretPath(projectId, environmentSlug, path);
    if (!folder)
      throw new NotFoundError({
        message: `Folder with path '${path}' in environment with slug '${environmentSlug}' not found`
      });

    const dynamicSecretLease = await dynamicSecretLeaseDAL.findById(leaseId);
    if (!dynamicSecretLease || dynamicSecretLease.dynamicSecret.folderId !== folder.id) {
      throw new NotFoundError({ message: `Dynamic secret lease with ID '${leaseId}' not found` });
    }

    const dynamicSecretCfg = await dynamicSecretDAL.findOne({
      id: dynamicSecretLease.dynamicSecretId,
      folderId: folder.id
    });

    if (!dynamicSecretCfg)
      throw new NotFoundError({
        message: `Dynamic secret with ID '${dynamicSecretLease.dynamicSecretId}' not found`
      });

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionDynamicSecretActions.Lease,
      subject(ProjectPermissionSub.DynamicSecrets, {
        environment: environmentSlug,
        secretPath: path,
        metadata: dynamicSecretCfg.metadata
      })
    );

    const selectedProvider = dynamicSecretProviders[dynamicSecretCfg.type as DynamicSecretProviders];
    const decryptedStoredInput = JSON.parse(
      secretManagerDecryptor({ cipherTextBlob: Buffer.from(dynamicSecretCfg.encryptedInput) }).toString()
    ) as object;

    const selectedTTL = ttl || dynamicSecretCfg.defaultTTL;
    const { maxTTL } = dynamicSecretCfg;
    const expireAt = new Date(dynamicSecretLease.expireAt.getTime() + ms(selectedTTL));
    if (maxTTL) {
      const maxExpiryDate = new Date(dynamicSecretLease.createdAt.getTime() + ms(maxTTL));
      if (expireAt > maxExpiryDate)
        throw new BadRequestError({
          message: "The requested renewal would exceed the maximum allowed lease duration. Please choose a shorter TTL"
        });
    }

    const { entityId } = await selectedProvider.renew(
      decryptedStoredInput,
      dynamicSecretLease.externalEntityId,
      expireAt.getTime(),
      { projectId }
    );

    await dynamicSecretQueueService.unsetLeaseRevocation(dynamicSecretLease.id);
    await dynamicSecretQueueService.unsetRestoredLeaseRevocation(dynamicSecretLease.id);
    await dynamicSecretQueueService.setLeaseRevocation(dynamicSecretLease.id, dynamicSecretCfg.id, expireAt);
    const updatedDynamicSecretLease = await dynamicSecretLeaseDAL.updateById(dynamicSecretLease.id, {
      expireAt,
      externalEntityId: entityId
    });
    return {
      lease: updatedDynamicSecretLease,
      dynamicSecret: dynamicSecretCfg,
      projectId,
      environment: environmentSlug,
      secretPath: path
    };
  };

  const revokeLease: TDynamicSecretLeaseServiceFactory["revokeLease"] = async ({
    leaseId,
    environmentSlug,
    path,
    projectSlug,
    actor,
    actorId,
    actorOrgId,
    actorAuthMethod,
    isForced
  }) => {
    const project = await projectDAL.findProjectBySlug(projectSlug, actorOrgId);
    if (!project) throw new NotFoundError({ message: `Project with slug '${projectSlug}' not found` });

    const projectId = project.id;
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.SecretManager
    });

    const { decryptor: secretManagerDecryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.SecretManager,
      projectId
    });

    const folder = await folderDAL.findBySecretPath(projectId, environmentSlug, path);
    if (!folder)
      throw new NotFoundError({
        message: `Folder with path '${path}' in environment with slug '${environmentSlug}' not found`
      });

    const dynamicSecretLease = await dynamicSecretLeaseDAL.findById(leaseId);
    if (!dynamicSecretLease || dynamicSecretLease.dynamicSecret.folderId !== folder.id)
      throw new NotFoundError({ message: `Dynamic secret lease with ID '${leaseId}' not found` });

    const dynamicSecretCfg = await dynamicSecretDAL.findOne({
      id: dynamicSecretLease.dynamicSecretId,
      folderId: folder.id
    });

    if (!dynamicSecretCfg)
      throw new NotFoundError({
        message: `Dynamic secret with ID '${dynamicSecretLease.dynamicSecretId}' not found`
      });

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionDynamicSecretActions.Lease,
      subject(ProjectPermissionSub.DynamicSecrets, {
        environment: environmentSlug,
        secretPath: path,
        metadata: dynamicSecretCfg.metadata
      })
    );

    const selectedProvider = dynamicSecretProviders[dynamicSecretCfg.type as DynamicSecretProviders];
    const decryptedStoredInput = JSON.parse(
      secretManagerDecryptor({ cipherTextBlob: Buffer.from(dynamicSecretCfg.encryptedInput) }).toString()
    ) as object;

    let isRevokeErrorSwallowed = false;
    const revokeResponse = await selectedProvider
      .revoke(
        decryptedStoredInput,
        dynamicSecretLease.externalEntityId,
        { projectId, leaseData: decryptLeaseData(secretManagerDecryptor, dynamicSecretLease.encryptedLeaseData) },
        dynamicSecretLease.config as TDynamicSecretLeaseConfig
      )
      .catch(async (err) => {
        // only propogate this error if forced is false
        if (!isForced) return { error: err as Error };
        isRevokeErrorSwallowed = true;
      });

    // A forced revoke that failed upstream is the user discarding the lease, not a revocation result.
    if (!isRevokeErrorSwallowed) {
      recordDynamicSecretRevocationOutcomeMetric({
        provider: dynamicSecretCfg.type,
        trigger: "manual",
        outcome: (revokeResponse as { error?: Error })?.error ? "failure" : "success",
        attemptsExhausted: true
      });
    }

    if ((revokeResponse as { error?: Error })?.error) {
      const { error } = revokeResponse as { error?: Error };
      logger.error(error?.message, "Failed to revoke lease");
      const updatedDynamicSecretLease = await dynamicSecretLeaseDAL.updateById(dynamicSecretLease.id, {
        status: DynamicSecretLeaseStatus.FailedDeletion,
        statusDetails: error?.message?.slice(0, 255)
      });
      // queue a job to retry the revocation at a later time
      await dynamicSecretQueueService.queueFailedRevocation(dynamicSecretLease.id, dynamicSecretCfg.id);
      return {
        lease: updatedDynamicSecretLease,
        dynamicSecret: dynamicSecretCfg,
        projectId,
        environment: environmentSlug,
        secretPath: path
      };
    }

    if (!isRevokeErrorSwallowed) {
      await recordDynamicSecretRevocationResult({
        dynamicSecretDAL,
        dynamicSecretId: dynamicSecretCfg.id,
        provider: dynamicSecretCfg.type,
        isRevoked: true,
        wasFailing: dynamicSecretCfg.revocationFailing
      });
    }

    await dynamicSecretQueueService.unsetLeaseRevocation(dynamicSecretLease.id);
    const deletedDynamicSecretLease = await dynamicSecretLeaseDAL.deleteById(dynamicSecretLease.id);
    return {
      lease: deletedDynamicSecretLease,
      dynamicSecret: dynamicSecretCfg,
      projectId,
      environment: environmentSlug,
      secretPath: path
    };
  };

  const listLeases: TDynamicSecretLeaseServiceFactory["listLeases"] = async ({
    path,
    name,
    actor,
    actorId,
    projectSlug,
    actorOrgId,
    environmentSlug,
    actorAuthMethod
  }) => {
    const project = await projectDAL.findProjectBySlug(projectSlug, actorOrgId);
    if (!project) throw new NotFoundError({ message: `Project with slug '${projectSlug}' not found` });

    const projectId = project.id;
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.SecretManager
    });

    const folder = await folderDAL.findBySecretPath(projectId, environmentSlug, path);
    if (!folder)
      throw new NotFoundError({
        message: `Folder with path '${path}' in environment with slug '${environmentSlug}' not found`
      });

    const dynamicSecretCfg = await dynamicSecretDAL.findOne({ name, folderId: folder.id });
    if (!dynamicSecretCfg)
      throw new NotFoundError({
        message: `Dynamic secret with name '${name}' in folder with path '${path}' not found`
      });

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionDynamicSecretActions.Lease,
      subject(ProjectPermissionSub.DynamicSecrets, {
        environment: environmentSlug,
        secretPath: path,
        metadata: dynamicSecretCfg.metadata
      })
    );

    const dynamicSecretLeases = await dynamicSecretLeaseDAL.find({ dynamicSecretId: dynamicSecretCfg.id });
    return {
      leases: dynamicSecretLeases,
      dynamicSecret: dynamicSecretCfg,
      projectId,
      environment: environmentSlug,
      secretPath: path
    };
  };

  const getLeaseDetails: TDynamicSecretLeaseServiceFactory["getLeaseDetails"] = async ({
    projectSlug,
    actorOrgId,
    path,
    environmentSlug,
    actor,
    actorId,
    leaseId,
    actorAuthMethod
  }) => {
    const project = await projectDAL.findProjectBySlug(projectSlug, actorOrgId);
    if (!project) throw new NotFoundError({ message: `Project with slug '${projectSlug}' not found` });

    const projectId = project.id;
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.SecretManager
    });

    const folder = await folderDAL.findBySecretPath(projectId, environmentSlug, path);
    if (!folder) throw new NotFoundError({ message: `Folder with path '${path}' not found` });

    const dynamicSecretLease = await dynamicSecretLeaseDAL.findById(leaseId);
    if (!dynamicSecretLease)
      throw new NotFoundError({ message: `Dynamic secret lease with ID '${leaseId}' not found` });

    const dynamicSecretCfg = await dynamicSecretDAL.findOne({
      id: dynamicSecretLease.dynamicSecretId,
      folderId: folder.id
    });

    if (!dynamicSecretCfg)
      throw new NotFoundError({
        message: `Dynamic secret with ID '${dynamicSecretLease.dynamicSecretId}' not found`
      });

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionDynamicSecretActions.Lease,
      subject(ProjectPermissionSub.DynamicSecrets, {
        environment: environmentSlug,
        secretPath: path,
        metadata: dynamicSecretCfg.metadata
      })
    );

    return {
      lease: dynamicSecretLease,
      dynamicSecret: dynamicSecretCfg,
      projectId,
      environment: environmentSlug,
      secretPath: path
    };
  };

  return {
    create,
    listLeases,
    revokeLease,
    renewLease,
    getLeaseDetails
  };
};