Skip to content

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

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

import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { ForbiddenRequestError } from "@app/lib/errors";
import { ActorType } from "@app/services/auth/auth-type";
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 { TUserDALFactory } from "@app/services/user/user-dal";

import { TPamAccountDALFactory } from "../pam-account/pam-account-dal";
import { PamSessionStatus } from "../pam/pam-enums";
import { getResourceIdsWithActionsForActors, pamActorKey } from "../pam/pam-permission";
import { ResourcePermissionPamResourceActions } from "../permission/resource-permission";
import { TPamSessionDALFactory } from "./pam-session-dal";
import { LIVE_PAM_SESSION_STATUSES, sendPamSessionCancellationSignal, terminatePamSessions } from "./pam-session-fns";

// Users and machine identities can both hold PAM sessions, and the two are tracked in separate columns
// (`userId` / `identityId` on the session, `actorUserId` / `actorIdentityId` on the membership). Carrying
// the kind alongside the id is what keeps a lookup from silently crossing the two.
export type TPamSessionActor = { type: ActorType.USER | ActorType.IDENTITY; id: string };

// Exactly one actor column is set per session row.
export const resolveSessionActor = (session: {
  userId?: string | null;
  identityId?: string | null;
}): TPamSessionActor | null => {
  if (session.identityId) return { type: ActorType.IDENTITY, id: session.identityId };
  if (session.userId) return { type: ActorType.USER, id: session.userId };
  return null;
};

type TLaunchableResources = { folderIds: Set<string>; accountIds: Set<string> };

// Mirrors checkAccountAccess's union: the account's folder carries the action, or a direct membership on
// the account does. With no resolved memberships at all there is no access.
export const hasLaunchAccessToAccount = (
  launchable: TLaunchableResources | undefined,
  account: { id: string; folderId?: string | null }
) => {
  if (!launchable) return false;
  if (account.folderId && launchable.folderIds.has(account.folderId)) return true;
  return launchable.accountIds.has(account.id);
};

type TTerminatePamSessionsWithoutLaunchAccessDTO = {
  projectId: string;
  // The actors whose access just changed, users and machine identities alike.
  actors: TPamSessionActor[];
  // The admin who made the access change; named as the terminating actor on the gateway signal. Absent when
  // SCIM or an IdP group sync made it.
  actorId?: string;
  membershipDAL: Pick<TMembershipDALFactory, "findResourceMembershipsForActors">;
  membershipRoleDAL: Pick<TMembershipRoleDALFactory, "find">;
  pamAccountDAL: Pick<TPamAccountDALFactory, "find">;
  pamSessionDAL: Pick<TPamSessionDALFactory, "find" | "update">;
  userDAL: Pick<TUserDALFactory, "findById">;
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPAMConnectionDetails">;
  tx: Knex;
};

/**
 * Closes the sessions an access change just invalidated, and returns the callback that cuts their tunnels.
 *
 * Losing a membership (or being demoted out of a role that carries LaunchSessions) does nothing on its own
 * to a session that is already running: neither the gateway nor the web-access loop re-checks permissions
 * mid-session, so a revoked Connector would keep their privileged tunnel until it expired. This re-derives
 * LaunchSessions for every live session the given actors hold in the project and terminates the ones that
 * no longer pass. Machine identities count: they can launch CLI sessions, so they lose them the same way.
 *
 * Re-deriving beats diffing the membership row that changed: the same launch access can also come from the
 * account's folder, a second direct membership, or a group, so removing one row does not necessarily end
 * access.
 *
 * Runs inside the caller's membership transaction and reads through `tx` throughout. That is what makes it
 * correct rather than racy — the check has to see the caller's own uncommitted revocation, which a replica
 * read would miss, and rolling back the membership change has to roll back the terminations with it. Only
 * the tunnel-cancellation signals are deferred, since those cannot be undone; the caller fires the returned
 * callback after COMMIT.
 *
 * Sharing that transaction is only affordable because the queries here are a fixed handful however many
 * actors and sessions are involved. Keep it that way: a per-actor lookup would pin the connection for
 * hundreds of round trips on a large group.
 */
export const terminatePamSessionsWithoutLaunchAccess = async ({
  projectId,
  actors,
  actorId,
  membershipDAL,
  membershipRoleDAL,
  pamAccountDAL,
  pamSessionDAL,
  userDAL,
  gatewayV2Service,
  tx
}: TTerminatePamSessionsWithoutLaunchAccessDTO): Promise<() => void> => {
  const noop = () => {};
  if (actors.length === 0) return noop;

  const userIds = actors.filter((actor) => actor.type === ActorType.USER).map((actor) => actor.id);
  const identityIds = actors.filter((actor) => actor.type === ActorType.IDENTITY).map((actor) => actor.id);
  const liveStatuses = [PamSessionStatus.Active, PamSessionStatus.Starting];

  // Two queries rather than one: the actor columns are mutually exclusive, and ormify's `$in` entries are
  // ANDed together, so a single filter on both columns would match nothing.
  const userSessions = userIds.length
    ? await pamSessionDAL.find({ projectId, $in: { userId: userIds, status: liveStatuses } }, { tx })
    : [];
  const identitySessions = identityIds.length
    ? await pamSessionDAL.find({ projectId, $in: { identityId: identityIds, status: liveStatuses } }, { tx })
    : [];

  // A session whose account was deleted keeps its history with a null accountId and has no access left to
  // re-check; the account delete path terminated it already.
  const sessions = [...userSessions, ...identitySessions].flatMap((session) => {
    const sessionActor = resolveSessionActor(session);
    if (!session.accountId || !sessionActor) return [];
    return [{ ...session, accountId: session.accountId, sessionActor }];
  });
  if (sessions.length === 0) return noop;

  const accounts = await pamAccountDAL.find(
    {
      projectId,
      $in: { id: [...new Set(sessions.map((session) => session.accountId))] }
    },
    { tx }
  );
  const accountById = new Map(accounts.map((account) => [account.id, account]));

  // Resolved per actor rather than per session, since an actor's grants cover every account.
  const launchableByActor = await getResourceIdsWithActionsForActors(
    membershipDAL,
    membershipRoleDAL,
    projectId,
    { allOf: [ResourcePermissionPamResourceActions.LaunchSessions] },
    [...new Map(sessions.map((s) => [pamActorKey(s.sessionActor), s.sessionActor])).values()],
    tx
  );

  const revoked = sessions.filter((session) => {
    const account = accountById.get(session.accountId);
    // The account is gone from under the session; its own delete path owns closing it.
    if (!account) return false;
    return !hasLaunchAccessToAccount(launchableByActor.get(pamActorKey(session.sessionActor)), account);
  });
  if (revoked.length === 0) return noop;

  const actor = actorId ? await userDAL.findById(actorId, tx) : undefined;
  return terminatePamSessions({
    sessions: revoked,
    actorId: actorId ?? "",
    actorEmail: actor?.email ?? "",
    pamSessionDAL,
    gatewayV2Service,
    tx
  });
};

type TTerminatePamSessionsLosingGroupAccessDTO = Omit<
  TTerminatePamSessionsWithoutLaunchAccessDTO,
  "projectId" | "actors"
> & {
  userIds: string[];
  identityIds?: string[];
};

// A group can reach PAM projects in sub-orgs it is linked into, so this re-checks every project the actors hold a
// live session in. Same transaction contract as terminatePamSessionsWithoutLaunchAccess.
export const terminatePamSessionsLosingGroupAccess = async ({
  userIds,
  identityIds = [],
  pamSessionDAL,
  tx,
  ...deps
}: TTerminatePamSessionsLosingGroupAccessDTO): Promise<() => void> => {
  const userSessions = userIds.length
    ? await pamSessionDAL.find({ $in: { userId: userIds, status: LIVE_PAM_SESSION_STATUSES } }, { tx })
    : [];
  const identitySessions = identityIds.length
    ? await pamSessionDAL.find({ $in: { identityId: identityIds, status: LIVE_PAM_SESSION_STATUSES } }, { tx })
    : [];

  const actorsByProject = new Map<string, Map<string, TPamSessionActor>>();
  for (const session of [...userSessions, ...identitySessions]) {
    const sessionActor = resolveSessionActor(session);
    if (sessionActor) {
      const projectActors = actorsByProject.get(session.projectId) ?? new Map<string, TPamSessionActor>();
      projectActors.set(pamActorKey(sessionActor), sessionActor);
      actorsByProject.set(session.projectId, projectActors);
    }
  }

  const sendCancellations: (() => void)[] = [];
  for (const [projectId, projectActors] of actorsByProject) {
    sendCancellations.push(
      // eslint-disable-next-line no-await-in-loop
      await terminatePamSessionsWithoutLaunchAccess({
        ...deps,
        projectId,
        actors: [...projectActors.values()],
        pamSessionDAL,
        tx
      })
    );
  }

  return () => sendCancellations.forEach((send) => send());
};

type TTerminatePamSessionsForUsersDTO = {
  orgIds: string[];
  userIds: string[];
  pamSessionDAL: Pick<TPamSessionDALFactory, "findLiveByOrgAndUserIds" | "update">;
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPAMConnectionDetails">;
  tx?: Knex;
};

// Same transaction contract as `terminatePamSessions`: the row update rolls back with the caller's
// transaction, the cancellation signals cannot, so the caller fires the returned callback after COMMIT.
export const terminatePamSessionsForUsers = async ({
  orgIds,
  userIds,
  pamSessionDAL,
  gatewayV2Service,
  tx
}: TTerminatePamSessionsForUsersDTO): Promise<() => void> => {
  const noop = () => {};
  if (userIds.length === 0) return noop;

  const sessions = await pamSessionDAL.findLiveByOrgAndUserIds(orgIds, userIds, tx);
  if (sessions.length === 0) return noop;

  await pamSessionDAL.update(
    { $in: { id: sessions.map((session) => session.id), status: LIVE_PAM_SESSION_STATUSES } },
    { status: PamSessionStatus.Terminated, endedAt: new Date() },
    tx
  );

  return () => {
    for (const session of sessions) {
      // Gateway-less types (AWS IAM) hand STS credentials straight to the user; AWS has no per-session
      // revocation, so those stay valid until they expire.
      if (session.gatewayId) {
        const sessionActor = resolveSessionActor(session);
        sendPamSessionCancellationSignal({
          sessionId: session.id,
          gatewayId: session.gatewayId,
          accountType: session.accountType,
          actorId: sessionActor?.id ?? "",
          actorType: sessionActor?.type,
          actorEmail: session.actorEmail || session.actorName,
          gatewayV2Service
        });
      }
    }
  };
};

export const assertUserStillActiveInOrg = async ({
  orgId,
  userId,
  membershipDAL,
  orgDAL,
  tx
}: {
  orgId: string;
  userId: string;
  membershipDAL: Pick<TMembershipDALFactory, "lockOrgMembershipForUser">;
  orgDAL: Pick<TOrgDALFactory, "findById" | "lockActorsForOrgAccess" | "findActorsWithActiveOrgAccess">;
  tx: Knex;
}) => {
  const org = await orgDAL.findById(orgId, tx);

  const orgIds = org?.rootOrgId ? [orgId, org.rootOrgId] : [orgId];

  for await (const id of orgIds) {
    const membership = await membershipDAL.lockOrgMembershipForUser(id, userId, tx);
    let isActive = Boolean(membership?.isActive);
    if (!membership) {
      await orgDAL.lockActorsForOrgAccess({ orgId: id, actorIds: [userId] }, tx);
      const withAccess = await orgDAL.findActorsWithActiveOrgAccess(
        { orgId: id, userIds: [userId], identityIds: [] },
        tx
      );
      isActive = withAccess.userIds.has(userId);
    }
    if (!isActive) {
      throw new ForbiddenRequestError({
        message: "Your organization membership is no longer active. Contact an organization admin to restore access."
      });
    }
  }
};