Skip to content

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

22.3 KB
Raw Blame
import { ForbiddenError } from "@casl/ability";

import { AccessScope, OrganizationActionScope, OrgMembershipRole, TableName, TRoles } from "@app/db/schemas";
import { getEnforcedIdentityLimit } from "@app/ee/services/license/license-fns";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { OrgPermissionIdentityActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import {
  assertRoleSetBoundary,
  constructPermissionErrorMessage,
  validatePrivilegeChangeOperation
} from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError, NotFoundError, PermissionBoundaryError } from "@app/lib/errors";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { containsLogUnsafeCharacters, LOG_UNSAFE_TEXT_MESSAGE } from "@app/lib/validator/log-safe-text";
import { TAlertServiceFactory } from "@app/services/alert/alert-service";
import { IDENTITY_AUTHENTICATION_RESOURCE_TYPE } from "@app/services/alert/providers/identity-credential-alert-provider";
import { TIdentityProjectDALFactory } from "@app/services/identity-project/identity-project-dal";
import { AgentVaultIdentities, IdentitiesMeter, PamIdentities, SecretIdentities } from "@app/services/license-client";
import { TUsageMeteringServiceFactory } from "@app/services/license-client/usage";

import { TAdditionalPrivilegeDALFactory } from "../additional-privilege/additional-privilege-dal";
import { ActorType } from "../auth/auth-type";
import { TEventEmitter } from "../event-outbox/event-outbox-types";
import { TIdentityAccessTokenServiceFactory } from "../identity-access-token/identity-access-token-service";
import { TMembershipIdentityDALFactory } from "../membership-identity/membership-identity-dal";
import { resolveMembershipRoleSlugs } from "../membership/membership-fns";
import { TMembershipRoleDALFactory } from "../membership/membership-role-dal";
import { TOrgDALFactory } from "../org/org-dal";
import { emitOrgMembershipRemoved, toOrgMembershipActors } from "../org/org-membership-events";
import { validateIdentityUpdateForSuperAdminPrivileges } from "../super-admin/super-admin-fns";
import { TIdentityDALFactory } from "./identity-dal";
import { getIdentityActiveLockoutAuthMethods } from "./identity-fns";
import { TIdentityMetadataDALFactory } from "./identity-metadata-dal";
import { TIdentityOrgDALFactory } from "./identity-org-dal";
import {
  TCreateIdentityDTO,
  TDeleteIdentityDTO,
  TGetIdentityByIdDTO,
  TListOrgIdentitiesByOrgIdDTO,
  TListProjectIdentitiesByIdentityIdDTO,
  TSearchOrgIdentitiesByOrgIdDTO,
  TUpdateIdentityDTO
} from "./identity-types";

type TIdentityServiceFactoryDep = {
  identityDAL: TIdentityDALFactory;
  identityMetadataDAL: TIdentityMetadataDALFactory;
  identityOrgMembershipDAL: TIdentityOrgDALFactory;
  membershipIdentityDAL: TMembershipIdentityDALFactory;
  membershipRoleDAL: TMembershipRoleDALFactory;
  identityProjectDAL: Pick<TIdentityProjectDALFactory, "findByIdentityId">;
  permissionService: Pick<TPermissionServiceFactory, "getOrgPermission" | "getOrgPermissionByRoles">;
  licenseService: Pick<TLicenseServiceFactory, "getPlan" | "getOrgSeatUsage" | "updateSubscriptionOrgMemberCount">;
  keyStore: Pick<TKeyStoreFactory, "sortedSetRangeByScore">;
  orgDAL: Pick<TOrgDALFactory, "findById" | "findEffectiveOrgMembership">;
  additionalPrivilegeDAL: Pick<TAdditionalPrivilegeDALFactory, "delete">;
  usageMeteringService: Pick<TUsageMeteringServiceFactory, "emit">;
  alertService: Pick<TAlertServiceFactory, "deleteAlertsForResource" | "deleteAlertsForDeletedResource">;
  identityAccessTokenService: Pick<
    TIdentityAccessTokenServiceFactory,
    "insertIdentityWideRevocationMarker" | "insertOrgMembershipRevocationMarker" | "bumpIdentityRevocationVersion"
  >;
  eventEmitter: TEventEmitter;
};

export type TIdentityServiceFactory = ReturnType<typeof identityServiceFactory>;

export const identityServiceFactory = ({
  identityDAL,
  identityMetadataDAL,
  identityOrgMembershipDAL,
  identityProjectDAL,
  permissionService,
  licenseService,
  keyStore,
  orgDAL,
  membershipIdentityDAL,
  membershipRoleDAL,
  additionalPrivilegeDAL,
  usageMeteringService,
  alertService,
  identityAccessTokenService,
  eventEmitter
}: TIdentityServiceFactoryDep) => {
  const createIdentity = async ({
    name,
    role,
    hasDeleteProtection,
    actor,
    orgId,
    actorId,
    actorAuthMethod,
    actorOrgId,
    metadata
  }: TCreateIdentityDTO) => {
    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Create, OrgPermissionSubjects.Identity);

    const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], orgId);

    const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
      orgDAL.findById(actorOrgId)
    );
    const isCustomRole = Boolean(rolePermissionDetails?.role);
    if (isCustomRole) {
      const plan = await licenseService.getPlan(orgId);
      if (!plan?.rbac)
        throw new BadRequestError({
          message:
            "Failed to assign custom role to identity due to plan RBAC restriction. Upgrade to Infisical Enterprise to assign custom roles."
        });
    }
    if (role !== OrgMembershipRole.NoAccess) {
      const permissionBoundary = validatePrivilegeChangeOperation(
        shouldUseNewPrivilegeSystem,
        OrgPermissionIdentityActions.GrantPrivileges,
        OrgPermissionSubjects.Identity,
        permission,
        rolePermissionDetails.permission
      );
      if (!permissionBoundary.isValid)
        throw new PermissionBoundaryError({
          message: constructPermissionErrorMessage(
            "Failed to create identity",
            shouldUseNewPrivilegeSystem,
            OrgPermissionIdentityActions.GrantPrivileges,
            OrgPermissionSubjects.Identity
          ),
          details: { missingPermissions: permissionBoundary.missingPermissions }
        });
    }

    const identityLimit = getEnforcedIdentityLimit(await licenseService.getPlan(orgId));

    const identity = await identityDAL.transaction(async (tx) => {
      // Acquire advisory lock to prevent race conditions when checking identity limits
      // This ensures that concurrent requests cannot bypass the identity limit check
      await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.CreateIdentity(orgId)]);

      // Count seats inside the transaction, after the lock, so a concurrent create can't slip past
      if (identityLimit) {
        const { identitiesUsed } = await licenseService.getOrgSeatUsage(orgId, tx);
        if (identitiesUsed >= identityLimit) {
          throw new BadRequestError({
            message: "Failed to create identity due to identity limit reached. Upgrade plan to create more identities."
          });
        }
      }

      const newIdentity = await identityDAL.create({ name, hasDeleteProtection, orgId }, tx);
      const membership = await membershipIdentityDAL.create(
        {
          scope: AccessScope.Organization,
          actorIdentityId: newIdentity.id,
          scopeOrgId: orgId
        },
        tx
      );

      await membershipRoleDAL.create(
        {
          membershipId: membership.id,
          role: isCustomRole ? OrgMembershipRole.Custom : role,
          customRoleId: rolePermissionDetails?.role?.id
        },
        tx
      );

      let insertedMetadata: Array<{
        id: string;
        key: string;
        value: string;
      }> = [];

      if (metadata && metadata.length) {
        const rowsToInsert = metadata.map(({ key, value }) => ({
          identityId: newIdentity.id,
          orgId: newIdentity.orgId,
          key,
          value
        }));

        insertedMetadata = await identityMetadataDAL.insertMany(rowsToInsert, tx);
      }

      return {
        ...newIdentity,
        authMethods: [],
        metadata: insertedMetadata
      };
    });
    await licenseService.updateSubscriptionOrgMemberCount(orgId);
    usageMeteringService.emit(orgId, IdentitiesMeter.key);

    return identity;
  };

  const updateIdentity = async ({
    id,
    role,
    hasDeleteProtection,
    name,
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    metadata,
    isActorSuperAdmin
  }: TUpdateIdentityDTO) => {
    const identityOrgMembership = await orgDAL.findEffectiveOrgMembership({
      actorType: ActorType.IDENTITY,
      actorId: id,
      orgId: actorOrgId
    });
    if (!identityOrgMembership) throw new NotFoundError({ message: `Failed to find identity with id ${id}` });

    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId: identityOrgMembership.scopeOrgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Edit, OrgPermissionSubjects.Identity);

    let customRole: TRoles | undefined;
    if (role) {
      const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], actorOrgId);
      const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
        orgDAL.findById(actorOrgId)
      );

      const isCustomRole = Boolean(rolePermissionDetails?.role);
      if (isCustomRole) {
        const plan = await licenseService.getPlan(actorOrgId);
        if (!plan?.rbac)
          throw new BadRequestError({
            message:
              "Failed to assign custom role to identity due to plan RBAC restriction. Upgrade to Infisical Enterprise to assign custom roles."
          });
      }
      const targetRoles = resolveMembershipRoleSlugs(
        await membershipRoleDAL.findRolesByMembershipIds([identityOrgMembership.id])
      );
      const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, actorOrgId, {
        ignoreUnresolvedRoles: true
      });

      assertRoleSetBoundary({
        shouldUseNewPrivilegeSystem,
        opActions: OrgPermissionIdentityActions.GrantPrivileges,
        opSubject: OrgPermissionSubjects.Identity,
        actorPermission: permission,
        targetPermissions,
        baseMessage: "Failed to change the roles of a more privileged identity"
      });

      const appliedRolePermissionBoundary = validatePrivilegeChangeOperation(
        shouldUseNewPrivilegeSystem,
        OrgPermissionIdentityActions.GrantPrivileges,
        OrgPermissionSubjects.Identity,
        permission,
        rolePermissionDetails?.permission
      );
      if (!appliedRolePermissionBoundary.isValid)
        throw new PermissionBoundaryError({
          message: constructPermissionErrorMessage(
            "Failed to update identity",
            shouldUseNewPrivilegeSystem,
            OrgPermissionIdentityActions.GrantPrivileges,
            OrgPermissionSubjects.Identity
          ),
          details: { missingPermissions: appliedRolePermissionBoundary.missingPermissions }
        });

      if (isCustomRole) customRole = rolePermissionDetails?.role;
    }

    await validateIdentityUpdateForSuperAdminPrivileges(id, isActorSuperAdmin);

    const identityDetails = await requestMemoize(requestMemoKeys.identityFindById(id), () => identityDAL.findById(id));

    if (identityDetails.projectId) {
      throw new BadRequestError({ message: `Identity is managed by project` });
    }

    if (name && name !== identityDetails.name && containsLogUnsafeCharacters(name)) {
      throw new BadRequestError({ message: LOG_UNSAFE_TEXT_MESSAGE });
    }

    const identity = await identityDAL.transaction(async (tx) => {
      const newIdentity =
        identityDetails.orgId === actorOrgId && (name || hasDeleteProtection)
          ? await identityDAL.updateById(id, { name, hasDeleteProtection }, tx)
          : identityDetails;

      if (role) {
        await membershipRoleDAL.delete({ membershipId: identityOrgMembership.id }, tx);
        await membershipRoleDAL.create(
          {
            membershipId: identityOrgMembership.id,
            role: customRole ? OrgMembershipRole.Custom : role,
            customRoleId: customRole?.id || null
          },
          tx
        );
      }
      let insertedMetadata: Array<{
        id: string;
        key: string;
        value: string;
      }> = [];

      if (metadata && identityDetails.orgId === actorOrgId) {
        await identityMetadataDAL.delete({ orgId: newIdentity.orgId, identityId: id }, tx);

        if (metadata.length) {
          const rowsToInsert = metadata.map(({ key, value }) => ({
            identityId: newIdentity.id,
            orgId: newIdentity.orgId,
            key,
            value
          }));

          insertedMetadata = await identityMetadataDAL.insertMany(rowsToInsert, tx);
        }
      }

      return {
        ...newIdentity,
        metadata: insertedMetadata
      };
    });

    return { ...identity, orgId: identityOrgMembership.scopeOrgId };
  };

  const getIdentityById = async ({ id, actor, actorId, actorOrgId, actorAuthMethod }: TGetIdentityByIdDTO) => {
    const doc = await identityOrgMembershipDAL.find({
      [`${TableName.Membership}.actorIdentityId` as "actorIdentityId"]: id,
      scope: AccessScope.Organization,
      scopeOrgId: actorOrgId
    });
    const identity = doc[0];
    if (!identity) throw new NotFoundError({ message: `Failed to find identity with id ${id}` });

    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId: identity.orgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity);

    const activeLockoutAuthMethods = await getIdentityActiveLockoutAuthMethods(id, keyStore);

    return {
      ...identity,
      identity: { ...identity.identity, activeLockoutAuthMethods }
    };
  };

  const deleteIdentity = async ({
    actorId,
    actor,
    actorOrgId,
    actorAuthMethod,
    id,
    isActorSuperAdmin
  }: TDeleteIdentityDTO) => {
    const identityOrgMembership = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId: id
    });
    if (!identityOrgMembership) throw new NotFoundError({ message: `Failed to find identity with id ${id}` });

    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId: identityOrgMembership.scopeOrgId,
      actorAuthMethod,
      actorOrgId
    });

    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Delete, OrgPermissionSubjects.Identity);

    const targetRoles = resolveMembershipRoleSlugs(identityOrgMembership.roles);
    const targetPermissions = await permissionService.getOrgPermissionByRoles(targetRoles, actorOrgId, {
      ignoreUnresolvedRoles: true
    });
    const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
      orgDAL.findById(actorOrgId)
    );

    assertRoleSetBoundary({
      shouldUseNewPrivilegeSystem,
      opActions: OrgPermissionIdentityActions.Delete,
      opSubject: OrgPermissionSubjects.Identity,
      actorPermission: permission,
      targetPermissions,
      baseMessage: "Failed to remove a more privileged identity from the organization"
    });

    await validateIdentityUpdateForSuperAdminPrivileges(id, isActorSuperAdmin);

    if (identityOrgMembership.identity.projectId) {
      throw new BadRequestError({ message: `Identity is managed by project` });
    }

    if (identityOrgMembership.identity.orgId === actorOrgId) {
      if (identityOrgMembership.identity.hasDeleteProtection)
        throw new BadRequestError({ message: "Identity has delete protection" });

      const deletedIdentity = await identityDAL.transaction(async (tx) => {
        await identityAccessTokenService.insertIdentityWideRevocationMarker({ identityId: id, tx });

        await alertService.deleteAlertsForDeletedResource(
          {
            resourceType: IDENTITY_AUTHENTICATION_RESOURCE_TYPE,
            resourceId: id
          },
          tx
        );
        return identityDAL.deleteById(id, tx);
      });
      await identityAccessTokenService.bumpIdentityRevocationVersion({ identityId: id });
      await licenseService.updateSubscriptionOrgMemberCount(identityOrgMembership.scopeOrgId);
      usageMeteringService.emit(identityOrgMembership.scopeOrgId, IdentitiesMeter.key);
      usageMeteringService.emit(identityOrgMembership.scopeOrgId, SecretIdentities.key);
      usageMeteringService.emit(identityOrgMembership.scopeOrgId, PamIdentities.key);
      usageMeteringService.emit(identityOrgMembership.scopeOrgId, AgentVaultIdentities.key);
      return { ...deletedIdentity, orgId: identityOrgMembership.scopeOrgId };
    }

    await membershipIdentityDAL.transaction(async (tx) => {
      await identityMetadataDAL.delete(
        {
          identityId: id,
          orgId: actorOrgId
        },
        tx
      );
      const identityProjectMembership = await membershipIdentityDAL.find(
        {
          actorIdentityId: id,
          scope: AccessScope.Project,
          scopeOrgId: actorOrgId
        },
        { tx }
      );
      await additionalPrivilegeDAL.delete(
        {
          actorIdentityId: id,
          $in: {
            projectId: identityProjectMembership.map((el) => el.scopeProjectId)
          }
        },
        tx
      );
      const doc = await membershipIdentityDAL.delete({ actorIdentityId: id, scopeOrgId: actorOrgId }, tx);

      await identityAccessTokenService.insertOrgMembershipRevocationMarker({ identityId: id, orgId: actorOrgId, tx });
      await emitOrgMembershipRemoved(
        eventEmitter,
        { orgId: actorOrgId, actors: toOrgMembershipActors({ identityIds: [id] }) },
        tx
      );

      await alertService.deleteAlertsForResource(
        {
          orgId: actorOrgId,
          resourceType: IDENTITY_AUTHENTICATION_RESOURCE_TYPE,
          resourceId: id
        },
        tx
      );

      return doc;
    });

    await identityAccessTokenService.bumpIdentityRevocationVersion({ identityId: id });

    const deletedIdentity = await requestMemoize(requestMemoKeys.identityFindById(id), () => identityDAL.findById(id));
    usageMeteringService.emit(identityOrgMembership.scopeOrgId, IdentitiesMeter.key);
    // Deleting the identity cascades its project + group memberships, so the secret-manager and PAM
    // identity meters change too.
    usageMeteringService.emit(identityOrgMembership.scopeOrgId, SecretIdentities.key);
    usageMeteringService.emit(identityOrgMembership.scopeOrgId, PamIdentities.key);
    usageMeteringService.emit(identityOrgMembership.scopeOrgId, AgentVaultIdentities.key);
    return { ...deletedIdentity, orgId: identityOrgMembership.scopeOrgId };
  };

  const listOrgIdentities = async ({
    orgId,
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    limit,
    offset,
    orderBy,
    orderDirection,
    search
  }: TListOrgIdentitiesByOrgIdDTO) => {
    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity);

    const identityMemberships = await identityOrgMembershipDAL.find({
      [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
      scope: AccessScope.Organization,
      limit,
      offset,
      orderBy,
      orderDirection,
      search
    });

    const totalCount = await identityOrgMembershipDAL.countAllOrgIdentities({
      [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
      search
    });

    return { identityMemberships, totalCount };
  };

  const searchOrgIdentities = async ({
    orgId,
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    limit,
    offset,
    orderBy,
    orderDirection,
    searchFilter = {}
  }: TSearchOrgIdentitiesByOrgIdDTO) => {
    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId: actorOrgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity);

    const { totalCount, docs } = await identityOrgMembershipDAL.searchIdentities({
      orgId,
      limit,
      offset,
      orderBy,
      orderDirection,
      searchFilter
    });

    const docsWithLockouts = await Promise.all(
      docs.map(async (doc) => ({
        ...doc,
        identity: {
          ...doc.identity,
          activeLockoutAuthMethods: await getIdentityActiveLockoutAuthMethods(doc.identity.id, keyStore)
        }
      }))
    );

    return { identityMemberships: docsWithLockouts, totalCount };
  };

  const listProjectIdentitiesByIdentityId = async ({
    identityId,
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId
  }: TListProjectIdentitiesByIdentityIdDTO) => {
    const identityOrgMembership = await orgDAL.findEffectiveOrgMembership({
      actorType: ActorType.IDENTITY,
      actorId: identityId,
      orgId: actorOrgId
    });
    if (!identityOrgMembership) throw new NotFoundError({ message: `Failed to find identity with id ${identityId}` });

    const { permission } = await permissionService.getOrgPermission({
      scope: OrganizationActionScope.Any,
      actor,
      actorId,
      orgId: identityOrgMembership.scopeOrgId,
      actorAuthMethod,
      actorOrgId
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity);

    const identityMemberships = await identityProjectDAL.findByIdentityId(identityId, actorOrgId);
    return identityMemberships;
  };

  return {
    createIdentity,
    updateIdentity,
    deleteIdentity,
    listOrgIdentities,
    getIdentityById,
    searchOrgIdentities,
    listProjectIdentitiesByIdentityId
  };
};