Skip to content

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

12.0 KB
Raw Blame
import { ForbiddenError } from "@casl/ability";
import slugify from "@sindresorhus/slugify";

import { AccessScope, OrganizationActionScope, OrgMembershipRole, OrgMembershipStatus } from "@app/db/schemas";
import { bootstrapAgentVaultProject } from "@app/ee/services/agent-vault-project/agent-vault-project-bootstrap";
import { bootstrapPamProject } from "@app/ee/services/pam-project/pam-project-bootstrap";
import { bootstrapSecretScanningProject } from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-project-bootstrap";
import { BadRequestError } from "@app/lib/errors";
import { ActorType } from "@app/services/auth/auth-type";
import { bootstrapCertManagerProject } from "@app/services/cert-manager-instance/cert-manager-project-bootstrap";
import { TCertificatePolicyDALFactory } from "@app/services/certificate-policy/certificate-policy-dal";
import { AgentVaultIdentities, PamIdentities } from "@app/services/license-client";
import { TUsageMeteringServiceFactory } from "@app/services/license-client/usage";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
import { TMembershipRoleDALFactory } from "@app/services/membership/membership-role-dal";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { TProjectDALFactory } from "@app/services/project/project-dal";

import { TLicenseServiceFactory } from "../license/license-service";
import { OrgPermissionSubjects, OrgPermissionSubOrgActions } from "../permission/org-permission";
import { TPermissionServiceFactory } from "../permission/permission-service-types";
import { TCreateSubOrgDTO, TDeleteSubOrgDTO, TJoinSubOrgDTO, TListSubOrgDTO, TUpdateSubOrgDTO } from "./sub-org-types";

type TSubOrgServiceFactoryDep = {
  orgDAL: Pick<
    TOrgDALFactory,
    "findOne" | "create" | "transaction" | "listSubOrganizations" | "updateById" | "findById" | "deleteById"
  >;
  permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
  licenseService: Pick<TLicenseServiceFactory, "getPlan">;
  membershipDAL: Pick<TMembershipDALFactory, "create" | "findOne">;
  membershipRoleDAL: Pick<TMembershipRoleDALFactory, "create">;
  projectDAL: Pick<TProjectDALFactory, "create" | "findOne">;
  certificatePolicyDAL: Pick<TCertificatePolicyDALFactory, "create">;
  usageMeteringService: Pick<TUsageMeteringServiceFactory, "emit">;
};

export type TSubOrgServiceFactory = ReturnType<typeof subOrgServiceFactory>;

export const subOrgServiceFactory = ({
  orgDAL,
  permissionService,
  licenseService,
  membershipDAL,
  membershipRoleDAL,
  projectDAL,
  certificatePolicyDAL,
  usageMeteringService
}: TSubOrgServiceFactoryDep) => {
  const createSubOrg = async ({ name, slug, permission }: TCreateSubOrgDTO) => {
    const { permission: orgPermission } = await permissionService.getOrgPermission({
      actorId: permission.id,
      actor: permission.type,
      orgId: permission.orgId,
      actorOrgId: permission.orgId,
      actorAuthMethod: permission.authMethod,
      scope: OrganizationActionScope.ParentOrganization
    });

    ForbiddenError.from(orgPermission).throwUnlessCan(
      OrgPermissionSubOrgActions.Create,
      OrgPermissionSubjects.SubOrganization
    );

    const orgLicensePlan = await licenseService.getPlan(permission.rootOrgId);
    if (!orgLicensePlan.subOrganization) {
      throw new BadRequestError({
        message: "Sub-organization creation failed. Please upgrade your instance to Infisical's Enterprise plan."
      });
    }

    // Generate slug from name if not provided
    const generatedSlug = slug ?? slugify(name, { lowercase: true });

    const existingSubOrg = await orgDAL.findOne({
      parentOrgId: permission.orgId,
      slug: generatedSlug
    });
    if (existingSubOrg) {
      throw new BadRequestError({ message: `Sub-organization with slug "${generatedSlug}" already exists` });
    }

    const organization = await orgDAL.transaction(async (tx) => {
      const org = await orgDAL.create(
        { name, slug: generatedSlug, rootOrgId: permission.rootOrgId, parentOrgId: permission.orgId },
        tx
      );
      const membership = await membershipDAL.create(
        {
          scope: AccessScope.Organization,
          [permission.type === ActorType.IDENTITY ? "actorIdentityId" : "actorUserId"]: permission.id,
          scopeOrgId: org.id,
          status: OrgMembershipStatus.Accepted,
          isActive: true
        },
        tx
      );
      await membershipRoleDAL.create(
        {
          membershipId: membership.id,
          role: OrgMembershipRole.Admin
        },
        tx
      );

      await bootstrapCertManagerProject(
        {
          orgId: org.id,
          adminUserIds: permission.type === ActorType.USER ? [permission.id] : [],
          adminIdentityIds: permission.type === ActorType.IDENTITY ? [permission.id] : []
        },
        { projectDAL, membershipDAL, membershipRoleDAL, certificatePolicyDAL },
        tx
      );

      await bootstrapPamProject(
        {
          orgId: org.id,
          adminUserIds: permission.type === ActorType.USER ? [permission.id] : [],
          adminIdentityIds: permission.type === ActorType.IDENTITY ? [permission.id] : []
        },
        { projectDAL, membershipDAL, membershipRoleDAL },
        tx
      );

      await bootstrapAgentVaultProject(
        {
          orgId: org.id,
          adminUserIds: permission.type === ActorType.USER ? [permission.id] : [],
          adminIdentityIds: permission.type === ActorType.IDENTITY ? [permission.id] : []
        },
        { projectDAL, membershipDAL, membershipRoleDAL },
        tx
      );

      await bootstrapSecretScanningProject(
        {
          orgId: org.id,
          adminUserIds: permission.type === ActorType.USER ? [permission.id] : [],
          adminIdentityIds: permission.type === ActorType.IDENTITY ? [permission.id] : []
        },
        { projectDAL, membershipDAL, membershipRoleDAL },
        tx
      );

      return org;
    });

    usageMeteringService.emit(organization.id, PamIdentities.key);
    usageMeteringService.emit(organization.id, AgentVaultIdentities.key);

    return {
      organization
    };
  };

  const listSubOrgs = async ({ permission, data }: TListSubOrgDTO) => {
    await permissionService.getOrgPermission({
      actorId: permission.id,
      actor: permission.type,
      orgId: permission.rootOrgId,
      actorOrgId: permission.rootOrgId,
      actorAuthMethod: permission.authMethod,
      scope: OrganizationActionScope.Any
    });

    const { orgs: organizations, totalCount } = await orgDAL.listSubOrganizations({
      actorId: permission.id,
      actorType: permission.type,
      orgId: permission.rootOrgId,
      isAccessible: data?.isAccessible,
      search: data?.search,
      orderBy: data?.orderBy,
      orderDirection: data?.orderDirection,
      limit: data?.limit,
      offset: data?.offset
    });

    return {
      organizations,
      totalCount
    };
  };

  const updateSubOrg = async ({ subOrgId, name, slug, permission }: TUpdateSubOrgDTO) => {
    const subOrg = await orgDAL.findOne({
      rootOrgId: permission.rootOrgId,
      id: subOrgId
    });
    if (!subOrg) {
      throw new BadRequestError({ message: "Sub-organization not found" });
    }

    const { permission: orgPermission } = await permissionService.getOrgPermission({
      actorId: permission.id,
      actor: permission.type,
      orgId: permission.rootOrgId,
      actorOrgId: permission.rootOrgId,
      actorAuthMethod: permission.authMethod,
      scope: OrganizationActionScope.ParentOrganization
    });

    ForbiddenError.from(orgPermission).throwUnlessCan(
      OrgPermissionSubOrgActions.Edit,
      OrgPermissionSubjects.SubOrganization
    );

    const updateData: { name?: string; slug?: string } = {};

    // Backward compatibility: if only name is provided (no slug), update both name and slug
    // This maintains the legacy behavior where name was used as both display name and slug
    if (name !== undefined && slug === undefined) {
      const generatedSlug = slugify(name, { lowercase: true });

      // Check for slug uniqueness
      const existingSubOrg = await orgDAL.findOne({
        parentOrgId: subOrg.parentOrgId,
        slug: generatedSlug
      });

      if (existingSubOrg && existingSubOrg.id !== subOrgId) {
        throw new BadRequestError({ message: `Sub-organization with slug "${generatedSlug}" already exists` });
      }

      updateData.name = name;
      updateData.slug = generatedSlug;
    } else {
      // update fields independently
      if (name !== undefined) updateData.name = name;

      if (slug !== undefined) {
        // Check for slug uniqueness
        const existingSubOrg = await orgDAL.findOne({
          parentOrgId: subOrg.parentOrgId,
          slug
        });

        if (existingSubOrg && existingSubOrg.id !== subOrgId) {
          throw new BadRequestError({ message: `Sub-organization with slug "${slug}" already exists` });
        }

        updateData.slug = slug;
      }
    }

    if (Object.keys(updateData).length === 0) {
      return { organization: subOrg };
    }

    const organization = await orgDAL.updateById(subOrgId, updateData);

    return {
      organization
    };
  };

  const joinSubOrg = async ({ subOrgId, permission }: TJoinSubOrgDTO) => {
    const orgLicensePlan = await licenseService.getPlan(permission.rootOrgId);
    if (!orgLicensePlan.subOrganization) {
      throw new BadRequestError({
        message:
          "Sub-organization feature is not available. Please upgrade your instance to Infisical's Enterprise plan."
      });
    }

    const subOrg = await orgDAL.findOne({
      rootOrgId: permission.rootOrgId,
      id: subOrgId
    });
    if (!subOrg) {
      throw new BadRequestError({ message: "Sub-organization not found" });
    }

    const { permission: orgPermission } = await permissionService.getOrgPermission({
      actorId: permission.id,
      actor: permission.type,
      orgId: permission.rootOrgId,
      actorOrgId: permission.rootOrgId,
      actorAuthMethod: permission.authMethod,
      scope: OrganizationActionScope.ParentOrganization
    });

    ForbiddenError.from(orgPermission).throwUnlessCan(
      OrgPermissionSubOrgActions.DirectAccess,
      OrgPermissionSubjects.SubOrganization
    );

    const actorField = permission.type === ActorType.IDENTITY ? "actorIdentityId" : "actorUserId";
    const existingMembership = await membershipDAL.findOne({
      scope: AccessScope.Organization,
      [actorField]: permission.id,
      scopeOrgId: subOrgId
    });

    if (existingMembership) {
      throw new BadRequestError({ message: "You are already a member of this sub-organization" });
    }

    await orgDAL.transaction(async (tx) => {
      const membership = await membershipDAL.create(
        {
          scope: AccessScope.Organization,
          [actorField]: permission.id,
          scopeOrgId: subOrgId,
          status: OrgMembershipStatus.Accepted,
          isActive: true
        },
        tx
      );
      await membershipRoleDAL.create(
        {
          membershipId: membership.id,
          role: OrgMembershipRole.Member
        },
        tx
      );
    });

    return { organization: subOrg };
  };

  const deleteSubOrg = async ({ subOrgId, permission }: TDeleteSubOrgDTO) => {
    const subOrg = await orgDAL.findOne({
      rootOrgId: permission.rootOrgId,
      id: subOrgId
    });
    if (!subOrg) {
      throw new BadRequestError({ message: "Sub-organization not found" });
    }

    const { permission: orgPermission } = await permissionService.getOrgPermission({
      actorId: permission.id,
      actor: permission.type,
      orgId: permission.rootOrgId,
      actorOrgId: permission.rootOrgId,
      actorAuthMethod: permission.authMethod,
      scope: OrganizationActionScope.ParentOrganization
    });

    ForbiddenError.from(orgPermission).throwUnlessCan(
      OrgPermissionSubOrgActions.Delete,
      OrgPermissionSubjects.SubOrganization
    );

    await orgDAL.deleteById(subOrgId);

    return { organization: subOrg };
  };

  return {
    createSubOrg,
    listSubOrgs,
    updateSubOrg,
    deleteSubOrg,
    joinSubOrg
  };
};