Skip to content

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

37.3 KB
Raw Blame
import { ForbiddenError, subject } from "@casl/ability";
import { requestContext } from "@fastify/request-context";
import { createLocalJWKSet, errors as joseErrors, jwtVerify } from "jose";

import {
  AccessScope,
  ActionProjectType,
  IdentityAuthMethod,
  OrganizationActionScope,
  TIdentitySpiffeAuths,
  TIdentitySpiffeAuthsUpdate
} from "@app/db/schemas";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { OrgPermissionIdentityActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { ProjectPermissionIdentityActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
import { TKeyStoreFactory } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { BadRequestError, ForbiddenRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr, TIp } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import {
  AuthAttemptAuthMethod,
  AuthAttemptAuthResult,
  authAttemptCounter,
  recordAuthAttemptMetric
} from "@app/lib/telemetry/metrics";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
import {
  emitIdentityAuthMethodChanged,
  IdentityAuthMethodChange
} from "@app/services/identity/identity-auth-method-events";

import { ActorType } from "../auth/auth-type";
import { TIdentityAccessTokenDALFactory } from "../identity-access-token/identity-access-token-dal";
import { TIdentityAccessTokenServiceFactory } from "../identity-access-token/identity-access-token-service";
import { assertIdentityAuthAccessAllowed } from "../identity/identity-auth-permission-fns";
import { TIdentityDALFactory } from "../identity/identity-dal";
import { TKmsServiceFactory } from "../kms/kms-service";
import { KmsDataKey } from "../kms/kms-types";
import { TMembershipIdentityDALFactory } from "../membership-identity/membership-identity-dal";
import { recordIdentityLastLoginDebounced } from "../membership-identity/membership-identity-fns";
import { TOrgDALFactory } from "../org/org-dal";
import { validateIdentityUpdateForSuperAdminPrivileges } from "../super-admin/super-admin-fns";
import { TIdentitySpiffeAuthDALFactory } from "./identity-spiffe-auth-dal";
import {
  claimKidMissRefresh,
  doesSpiffeIdMatchPattern,
  extractTrustDomainFromSpiffeId,
  fetchRemoteBundleJwks,
  isValidSpiffeId,
  parseSpiffeBundleJwtAuthorities
} from "./identity-spiffe-auth-fns";
import {
  FIPS_APPROVED_JWT_ALGORITHMS,
  SpiffeTrustBundleProfile,
  TAttachSpiffeAuthDTO,
  TGetSpiffeAuthDTO,
  TLoginSpiffeAuthDTO,
  TRevokeSpiffeAuthDTO,
  TSpiffeTrustBundleDistribution,
  TSpiffeTrustBundleDistributionResponse,
  TUpdateSpiffeAuthDTO
} from "./identity-spiffe-auth-types";

type TIdentitySpiffeAuthServiceFactoryDep = {
  identityDAL: Pick<TIdentityDALFactory, "findById">;
  identitySpiffeAuthDAL: TIdentitySpiffeAuthDALFactory;
  membershipIdentityDAL: Pick<TMembershipIdentityDALFactory, "findOne" | "update" | "getIdentityById">;
  keyStore: Pick<TKeyStoreFactory, "setItemWithExpiryNX">;
  identityAccessTokenDAL: Pick<TIdentityAccessTokenDALFactory, "delete">;
  permissionService: Pick<
    TPermissionServiceFactory,
    "getOrgPermission" | "getProjectPermission" | "getActorGrantAbilities"
  >;
  licenseService: Pick<TLicenseServiceFactory, "getPlan">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  orgDAL: Pick<TOrgDALFactory, "findById" | "findOne" | "findEffectiveOrgMembership">;
  identityAccessTokenService: Pick<
    TIdentityAccessTokenServiceFactory,
    "issueIdentityAccessToken" | "revokeTokensForIdentityAuthMethod" | "invalidateTrustedIpsCache"
  >;
  eventEmitter: TEventEmitter;
};

export type TIdentitySpiffeAuthServiceFactory = ReturnType<typeof identitySpiffeAuthServiceFactory>;

const JWT_SVID_NO_MATCHING_KEY_MESSAGE = "No key in the SPIFFE trust bundle matches the JWT-SVID signing key";

const verifyJwtSvid = async (jwtValue: string, jwksJson: string, allowedAudiences: string[]) => {
  const jwks = createLocalJWKSet(parseSpiffeBundleJwtAuthorities(jwksJson));

  try {
    const { payload } = await jwtVerify(jwtValue, jwks, {
      audience: allowedAudiences,
      algorithms: FIPS_APPROVED_JWT_ALGORITHMS
    });
    return payload as Record<string, unknown>;
  } catch (error) {
    if (error instanceof joseErrors.JWTExpired) {
      throw new UnauthorizedError({ message: "JWT-SVID has expired" });
    }
    if (error instanceof joseErrors.JWSSignatureVerificationFailed) {
      throw new UnauthorizedError({ message: "JWT-SVID signature verification failed" });
    }
    if (error instanceof joseErrors.JWTClaimValidationFailed) {
      throw new UnauthorizedError({ message: "JWT-SVID audience validation failed" });
    }
    if (error instanceof joseErrors.JWKSNoMatchingKey) {
      throw new UnauthorizedError({ message: JWT_SVID_NO_MATCHING_KEY_MESSAGE });
    }
    throw new UnauthorizedError({ message: "JWT-SVID verification failed" });
  }
};

const validateSpiffeClaims = (
  tokenData: Record<string, unknown>,
  config: { trustDomain: string; allowedSpiffeIds: string }
): boolean => {
  const tokenSub = tokenData.sub as string;
  if (!tokenSub || !isValidSpiffeId(tokenSub)) {
    return false;
  }

  const tokenTrustDomain = extractTrustDomainFromSpiffeId(tokenSub);
  if (tokenTrustDomain !== config.trustDomain) {
    return false;
  }

  if (!doesSpiffeIdMatchPattern(tokenSub, config.allowedSpiffeIds)) {
    return false;
  }

  return true;
};

export const identitySpiffeAuthServiceFactory = ({
  identityDAL,
  identitySpiffeAuthDAL,
  membershipIdentityDAL,
  keyStore,
  permissionService,
  licenseService,
  identityAccessTokenDAL,
  kmsService,
  orgDAL,
  identityAccessTokenService,
  eventEmitter
}: TIdentitySpiffeAuthServiceFactoryDep) => {
  type TFlattenedTrustBundle = {
    configurationType: string;
    caBundleJwks: string | null;
    bundleEndpointUrl: string | null;
    bundleEndpointCaCert: string | null;
    bundleRefreshHintSeconds: number | undefined;
  };

  const $flattenTrustBundleDistribution = (dist: TSpiffeTrustBundleDistribution): TFlattenedTrustBundle => {
    if (dist.profile === SpiffeTrustBundleProfile.STATIC) {
      return {
        configurationType: dist.profile,
        caBundleJwks: dist.bundle,
        bundleEndpointUrl: null,
        bundleEndpointCaCert: null,
        bundleRefreshHintSeconds: undefined
      };
    }
    return {
      configurationType: dist.profile,
      caBundleJwks: null,
      bundleEndpointUrl: dist.endpointUrl,
      bundleEndpointCaCert: dist.caCert || null,
      bundleRefreshHintSeconds: dist.refreshHintSeconds
    };
  };

  const $buildTrustBundleDistributionResponse = (
    dbRow: Pick<
      TIdentitySpiffeAuths,
      "configurationType" | "bundleEndpointUrl" | "cachedBundleLastRefreshedAt" | "bundleRefreshHintSeconds"
    >,
    decryptedJwks: string,
    decryptedCaCert: string
  ): TSpiffeTrustBundleDistributionResponse => {
    if (dbRow.configurationType === SpiffeTrustBundleProfile.STATIC) {
      return { profile: SpiffeTrustBundleProfile.STATIC, bundle: decryptedJwks };
    }
    return {
      profile: SpiffeTrustBundleProfile.HTTPS_WEB_BUNDLE,
      endpointUrl: dbRow.bundleEndpointUrl || "",
      caCert: decryptedCaCert,
      refreshHintSeconds: dbRow.bundleRefreshHintSeconds,
      cachedBundleLastRefreshedAt: dbRow.cachedBundleLastRefreshedAt
    };
  };

  const $validateSpiffeConfig = async (dist: TSpiffeTrustBundleDistribution) => {
    if (dist.profile === SpiffeTrustBundleProfile.STATIC) {
      parseSpiffeBundleJwtAuthorities(dist.bundle);
      return;
    }

    try {
      const bundleJwks = await fetchRemoteBundleJwks(dist.endpointUrl, dist.caCert);
      parseSpiffeBundleJwtAuthorities(bundleJwks);
    } catch (error) {
      if (error instanceof BadRequestError) throw error;
      throw new BadRequestError({
        message:
          "Failed to fetch or validate the remote SPIFFE trust bundle. Verify the bundle endpoint URL and CA certificate are correct."
      });
    }
  };

  const $resolveJwks = async ({
    config,
    orgId,
    forceRefresh
  }: {
    config: Pick<
      TIdentitySpiffeAuths,
      | "id"
      | "configurationType"
      | "encryptedCaBundleJwks"
      | "encryptedCachedBundleJwks"
      | "bundleEndpointUrl"
      | "encryptedBundleEndpointCaCert"
      | "bundleRefreshHintSeconds"
      | "cachedBundleLastRefreshedAt"
    >;
    orgId: string;
    forceRefresh?: boolean;
  }): Promise<{ jwksJson: string; fromCache: boolean }> => {
    const { decryptor: orgDataKeyDecryptor, encryptor: orgDataKeyEncryptor } =
      await kmsService.createCipherPairWithDataKey({
        type: KmsDataKey.Organization,
        orgId
      });

    if (config.configurationType === SpiffeTrustBundleProfile.STATIC) {
      if (!config.encryptedCaBundleJwks) {
        throw new BadRequestError({ message: "Static SPIFFE auth has no CA bundle JWKS configured" });
      }

      return {
        jwksJson: orgDataKeyDecryptor({ cipherTextBlob: config.encryptedCaBundleJwks }).toString(),
        fromCache: false
      };
    }

    // Remote configuration: check cache freshness
    if (!forceRefresh && config.encryptedCachedBundleJwks) {
      const refreshIntervalMs = (config.bundleRefreshHintSeconds || 3600) * 1000;
      const lastRefreshed = config.cachedBundleLastRefreshedAt
        ? new Date(config.cachedBundleLastRefreshedAt).getTime()
        : 0;

      if (Date.now() - lastRefreshed < refreshIntervalMs) {
        return {
          jwksJson: orgDataKeyDecryptor({ cipherTextBlob: config.encryptedCachedBundleJwks }).toString(),
          fromCache: true
        };
      }
    }

    // Fetch fresh bundle
    if (!config.bundleEndpointUrl) {
      throw new BadRequestError({ message: "Remote SPIFFE auth has no bundle endpoint URL configured" });
    }

    await blockLocalAndPrivateIpAddresses(config.bundleEndpointUrl);

    let caCert: string | undefined;
    if (config.encryptedBundleEndpointCaCert) {
      caCert = orgDataKeyDecryptor({ cipherTextBlob: config.encryptedBundleEndpointCaCert }).toString();
    }

    let bundleJson: string;
    try {
      bundleJson = await fetchRemoteBundleJwks(config.bundleEndpointUrl, caCert);
    } catch (error) {
      const msg = error instanceof Error ? error.message : "Unknown error";
      throw new BadRequestError({ message: `Failed to fetch SPIFFE trust bundle from remote endpoint: ${msg}` });
    }

    parseSpiffeBundleJwtAuthorities(bundleJson);

    const { cipherTextBlob: encryptedCachedBundleJwks } = orgDataKeyEncryptor({
      plainText: Buffer.from(bundleJson)
    });

    await identitySpiffeAuthDAL.updateById(config.id, {
      encryptedCachedBundleJwks,
      cachedBundleLastRefreshedAt: new Date()
    });

    logger.info(`SPIFFE auth: refreshed JWKS bundle for config ${config.id}`);

    return { jwksJson: bundleJson, fromCache: false };
  };

  const login = async ({ identityId, jwt: jwtValue, organizationSlug }: TLoginSpiffeAuthDTO) => {
    const authMetricStartTime = performance.now();
    const appCfg = getConfig();
    const identitySpiffeAuth = await identitySpiffeAuthDAL.findOne({ identityId });
    if (!identitySpiffeAuth) {
      throw new NotFoundError({
        message: "SPIFFE auth method not found for identity, did you configure SPIFFE auth?"
      });
    }

    const identity = await requestMemoize(requestMemoKeys.identityFindById(identitySpiffeAuth.identityId), () =>
      identityDAL.findById(identitySpiffeAuth.identityId)
    );
    if (!identity)
      throw new UnauthorizedError({
        message: "Identity not found"
      });

    const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
      orgDAL.findById(identity.orgId)
    );
    const isSubOrgIdentity = Boolean(org.rootOrgId);
    let subOrganizationId = isSubOrgIdentity ? org.id : null;

    try {
      // Resolve JWKS (lazy fetch for remote, decrypt for static)
      let { jwksJson, fromCache } = await $resolveJwks({ config: identitySpiffeAuth, orgId: identity.orgId });

      const allowedAudiences = identitySpiffeAuth.allowedAudiences
        .split(",")
        .map((a) => a.trim())
        .filter(Boolean);

      let tokenData: Record<string, unknown>;
      try {
        tokenData = await verifyJwtSvid(jwtValue, jwksJson, allowedAudiences);
      } catch (verifyError) {
        if (
          fromCache &&
          verifyError instanceof UnauthorizedError &&
          verifyError.message === JWT_SVID_NO_MATCHING_KEY_MESSAGE &&
          (await claimKidMissRefresh(keyStore, identitySpiffeAuth.id))
        ) {
          ({ jwksJson, fromCache } = await $resolveJwks({
            config: identitySpiffeAuth,
            orgId: identity.orgId,
            forceRefresh: true
          }));
          try {
            tokenData = await verifyJwtSvid(jwtValue, jwksJson, allowedAudiences);
          } catch (retryError) {
            if (retryError instanceof UnauthorizedError) {
              retryError.detail = {
                reasonCode: "jwt_svid_verification_failed",
                identityId: identity.id,
                orgId: identity.orgId,
                identityName: identity.name
              };
            }
            throw retryError;
          }
        } else {
          if (verifyError instanceof UnauthorizedError) {
            verifyError.detail = {
              reasonCode: "jwt_svid_verification_failed",
              identityId: identity.id,
              orgId: identity.orgId,
              identityName: identity.name
            };
          }
          throw verifyError;
        }
      }

      if (!validateSpiffeClaims(tokenData, identitySpiffeAuth)) {
        throw new UnauthorizedError({
          message: "Access denied",
          detail: {
            reasonCode: "spiffe_claims_invalid",
            identityId: identity.id,
            orgId: identity.orgId,
            identityName: identity.name
          }
        });
      }

      // Sub-org resolution
      if (organizationSlug && org.slug !== organizationSlug) {
        if (!isSubOrgIdentity) {
          const subOrg = await orgDAL.findOne({ rootOrgId: org.id, slug: organizationSlug });
          if (!subOrg) {
            throw new NotFoundError({ message: `Sub organization with slug ${organizationSlug} not found` });
          }

          const subOrgMembership = await orgDAL.findEffectiveOrgMembership({
            actorType: ActorType.IDENTITY,
            actorId: identity.id,
            orgId: subOrg.id
          });

          if (!subOrgMembership) {
            throw new UnauthorizedError({
              message: `Identity not authorized to access sub organization ${organizationSlug}`,
              detail: {
                reasonCode: "sub_org_unauthorized",
                identityId: identity.id,
                orgId: identity.orgId,
                identityName: identity.name
              }
            });
          }

          subOrganizationId = subOrg.id;
        }
      }

      await recordIdentityLastLoginDebounced({
        keyStore,
        membershipIdentityDAL,
        identity,
        lastLoginAuthMethod: IdentityAuthMethod.SPIFFE_AUTH
      });

      const subOrgDetails =
        subOrganizationId && subOrganizationId !== org.id ? await orgDAL.findById(subOrganizationId) : null;
      const tokenScopeOrg = subOrgDetails ?? org;
      const tokenRootOrgId = tokenScopeOrg.rootOrgId ?? tokenScopeOrg.id;
      const tokenParentOrgId = tokenScopeOrg.parentOrgId ?? tokenRootOrgId;

      const { accessToken, identityAccessToken } = await identityAccessTokenService.issueIdentityAccessToken({
        identityId: identitySpiffeAuth.identityId,
        identityName: identity.name,
        authMethod: IdentityAuthMethod.SPIFFE_AUTH,
        orgId: tokenScopeOrg.id,
        rootOrgId: tokenRootOrgId,
        parentOrgId: tokenParentOrgId,
        subOrganizationId,
        accessTokenTTL: Number(identitySpiffeAuth.accessTokenTTL),
        accessTokenMaxTTL: Number(identitySpiffeAuth.accessTokenMaxTTL),
        accessTokenNumUsesLimit: Number(identitySpiffeAuth.accessTokenNumUsesLimit),
        accessTokenPeriod: 0,
        accessTokenTrustedIps: identitySpiffeAuth.accessTokenTrustedIps as TIp[]
      });

      if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
        authAttemptCounter.add(1, {
          "infisical.identity.id": identitySpiffeAuth.identityId,
          "infisical.identity.name": identity.name,
          "infisical.organization.id": org.id,
          "infisical.organization.name": org.name,
          "infisical.identity.auth_method": AuthAttemptAuthMethod.SPIFFE_AUTH,
          "infisical.identity.auth_result": AuthAttemptAuthResult.SUCCESS,
          "client.address": requestContext.get(RequestContextKey.Ip),
          "user_agent.original": requestContext.get(RequestContextKey.UserAgent)
        });
      }

      recordAuthAttemptMetric({
        startTime: authMetricStartTime,
        method: AuthAttemptAuthMethod.SPIFFE_AUTH,
        result: AuthAttemptAuthResult.SUCCESS,
        orgId: org.id
      });

      return { accessToken, identitySpiffeAuth, identityAccessToken, identity };
    } catch (error) {
      if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
        authAttemptCounter.add(1, {
          "infisical.identity.id": identitySpiffeAuth.identityId,
          "infisical.identity.name": identity.name,
          "infisical.organization.id": org.id,
          "infisical.organization.name": org.name,
          "infisical.identity.auth_method": AuthAttemptAuthMethod.SPIFFE_AUTH,
          "infisical.identity.auth_result": AuthAttemptAuthResult.FAILURE,
          "client.address": requestContext.get(RequestContextKey.Ip),
          "user_agent.original": requestContext.get(RequestContextKey.UserAgent)
        });
      }

      recordAuthAttemptMetric({
        startTime: authMetricStartTime,
        method: AuthAttemptAuthMethod.SPIFFE_AUTH,
        result: AuthAttemptAuthResult.FAILURE,
        orgId: org.id,
        error
      });
      throw error;
    }
  };

  const attachSpiffeAuth = async ({
    identityId,
    trustDomain,
    allowedSpiffeIds,
    allowedAudiences,
    trustBundleDistribution,
    accessTokenTTL,
    accessTokenMaxTTL,
    accessTokenNumUsesLimit,
    accessTokenTrustedIps,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId,
    isActorSuperAdmin
  }: TAttachSpiffeAuthDTO) => {
    const identityMembershipOrg = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId
    });
    if (!identityMembershipOrg) throw new NotFoundError({ message: `Failed to find identity with ID ${identityId}` });
    if (identityMembershipOrg.identity.orgId !== actorOrgId) {
      throw new ForbiddenRequestError({ message: "Sub organization not authorized to access this identity" });
    }

    if (identityMembershipOrg.identity.authMethods.includes(IdentityAuthMethod.SPIFFE_AUTH)) {
      throw new BadRequestError({
        message: "Failed to add SPIFFE Auth to already configured identity"
      });
    }

    if (accessTokenMaxTTL > 0 && accessTokenTTL > accessTokenMaxTTL) {
      throw new BadRequestError({ message: "Access token TTL cannot be greater than max TTL" });
    }

    await assertIdentityAuthAccessAllowed(
      { permissionService, orgDAL },
      {
        identityId,
        orgId: identityMembershipOrg.scopeOrgId,
        projectId: identityMembershipOrg.identity.projectId,
        action: OrgPermissionIdentityActions.EditAuth,
        baseMessage: "Failed to add SPIFFE auth to identity with more privileged role",
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      }
    );

    await validateIdentityUpdateForSuperAdminPrivileges(identityId, isActorSuperAdmin);

    const plan = await licenseService.getPlan(identityMembershipOrg.scopeOrgId);
    const reformattedAccessTokenTrustedIps = accessTokenTrustedIps.map((accessTokenTrustedIp) => {
      if (
        !plan.ipAllowlisting &&
        accessTokenTrustedIp.ipAddress !== "0.0.0.0/0" &&
        accessTokenTrustedIp.ipAddress !== "::/0"
      )
        throw new BadRequestError({
          message:
            "Failed to add IP access range to access token due to plan restriction. Upgrade plan to add IP access range."
        });
      if (!isValidIpOrCidr(accessTokenTrustedIp.ipAddress))
        throw new BadRequestError({
          message: "The IP is not a valid IPv4, IPv6, or CIDR block"
        });
      return extractIPDetails(accessTokenTrustedIp.ipAddress);
    });

    const flatBundle = $flattenTrustBundleDistribution(trustBundleDistribution);

    if (flatBundle.bundleEndpointUrl) {
      await blockLocalAndPrivateIpAddresses(flatBundle.bundleEndpointUrl);
    }

    await $validateSpiffeConfig(trustBundleDistribution);

    const { encryptor: orgDataKeyEncryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: actorOrgId
    });

    const encryptedCaBundleJwks = flatBundle.caBundleJwks
      ? orgDataKeyEncryptor({ plainText: Buffer.from(flatBundle.caBundleJwks) }).cipherTextBlob
      : null;

    const encryptedBundleEndpointCaCert = flatBundle.bundleEndpointCaCert
      ? orgDataKeyEncryptor({ plainText: Buffer.from(flatBundle.bundleEndpointCaCert) }).cipherTextBlob
      : null;

    const identitySpiffeAuth = await identitySpiffeAuthDAL.transaction(async (tx) => {
      const doc = await identitySpiffeAuthDAL.create(
        {
          identityId: identityMembershipOrg.identity.id,
          trustDomain,
          allowedSpiffeIds,
          allowedAudiences,
          configurationType: flatBundle.configurationType,
          encryptedCaBundleJwks,
          bundleEndpointUrl: flatBundle.bundleEndpointUrl || null,
          encryptedBundleEndpointCaCert,
          bundleRefreshHintSeconds: flatBundle.bundleRefreshHintSeconds || 300,
          accessTokenMaxTTL,
          accessTokenTTL,
          accessTokenNumUsesLimit,
          accessTokenTrustedIps: JSON.stringify(reformattedAccessTokenTrustedIps)
        },
        tx
      );

      await emitIdentityAuthMethodChanged(
        eventEmitter,
        {
          membership: identityMembershipOrg,
          authMethod: IdentityAuthMethod.SPIFFE_AUTH,
          change: IdentityAuthMethodChange.Added,
          actor,
          actorId
        },
        tx
      );
      return doc;
    });

    await identityAccessTokenService.invalidateTrustedIpsCache(identityId, IdentityAuthMethod.SPIFFE_AUTH);
    return {
      ...identitySpiffeAuth,
      orgId: identityMembershipOrg.scopeOrgId,
      trustBundleDistribution: $buildTrustBundleDistributionResponse(
        identitySpiffeAuth,
        flatBundle.caBundleJwks || "",
        flatBundle.bundleEndpointCaCert || ""
      )
    };
  };

  const updateSpiffeAuth = async ({
    identityId,
    trustDomain,
    allowedSpiffeIds,
    allowedAudiences,
    trustBundleDistribution,
    accessTokenTTL,
    accessTokenMaxTTL,
    accessTokenNumUsesLimit,
    accessTokenTrustedIps,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId,
    isActorSuperAdmin
  }: TUpdateSpiffeAuthDTO) => {
    const identityMembershipOrg = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId
    });
    if (!identityMembershipOrg) throw new NotFoundError({ message: `Failed to find identity with ID ${identityId}` });
    if (identityMembershipOrg.identity.orgId !== actorOrgId) {
      throw new ForbiddenRequestError({ message: "Sub organization not authorized to access this identity" });
    }

    if (!identityMembershipOrg.identity.authMethods.includes(IdentityAuthMethod.SPIFFE_AUTH)) {
      throw new BadRequestError({ message: "Failed to update SPIFFE Auth" });
    }

    const identitySpiffeAuth = await identitySpiffeAuthDAL.findOne({ identityId });

    if (
      (accessTokenMaxTTL || identitySpiffeAuth.accessTokenMaxTTL) > 0 &&
      (accessTokenTTL || identitySpiffeAuth.accessTokenTTL) >
        (accessTokenMaxTTL || identitySpiffeAuth.accessTokenMaxTTL)
    ) {
      throw new BadRequestError({ message: "Access token TTL cannot be greater than max TTL" });
    }

    await assertIdentityAuthAccessAllowed(
      { permissionService, orgDAL },
      {
        identityId,
        orgId: identityMembershipOrg.scopeOrgId,
        projectId: identityMembershipOrg.identity.projectId,
        action: OrgPermissionIdentityActions.EditAuth,
        baseMessage: "Failed to update SPIFFE auth of identity with more privileged role",
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      }
    );

    await validateIdentityUpdateForSuperAdminPrivileges(identityId, isActorSuperAdmin);

    const plan = await licenseService.getPlan(identityMembershipOrg.scopeOrgId);
    const reformattedAccessTokenTrustedIps = accessTokenTrustedIps?.map((accessTokenTrustedIp) => {
      if (
        !plan.ipAllowlisting &&
        accessTokenTrustedIp.ipAddress !== "0.0.0.0/0" &&
        accessTokenTrustedIp.ipAddress !== "::/0"
      )
        throw new BadRequestError({
          message:
            "Failed to add IP access range to access token due to plan restriction. Upgrade plan to add IP access range."
        });
      if (!isValidIpOrCidr(accessTokenTrustedIp.ipAddress))
        throw new BadRequestError({
          message: "The IP is not a valid IPv4, IPv6, or CIDR block"
        });
      return extractIPDetails(accessTokenTrustedIp.ipAddress);
    });

    const updateQuery: TIdentitySpiffeAuthsUpdate = {
      trustDomain,
      allowedSpiffeIds,
      allowedAudiences,
      accessTokenMaxTTL,
      accessTokenTTL,
      accessTokenNumUsesLimit,
      accessTokenTrustedIps: reformattedAccessTokenTrustedIps
        ? JSON.stringify(reformattedAccessTokenTrustedIps)
        : undefined
    };

    const { encryptor: orgDataKeyEncryptor, decryptor: orgDataKeyDecryptor } =
      await kmsService.createCipherPairWithDataKey({
        type: KmsDataKey.Organization,
        orgId: actorOrgId
      });

    if (trustBundleDistribution) {
      const flatBundle = $flattenTrustBundleDistribution(trustBundleDistribution);

      if (flatBundle.bundleEndpointUrl) {
        await blockLocalAndPrivateIpAddresses(flatBundle.bundleEndpointUrl);
      }

      await $validateSpiffeConfig(trustBundleDistribution);

      updateQuery.configurationType = flatBundle.configurationType;
      updateQuery.bundleEndpointUrl = flatBundle.bundleEndpointUrl;
      updateQuery.bundleRefreshHintSeconds = flatBundle.bundleRefreshHintSeconds;
      updateQuery.encryptedCaBundleJwks = flatBundle.caBundleJwks
        ? orgDataKeyEncryptor({ plainText: Buffer.from(flatBundle.caBundleJwks) }).cipherTextBlob
        : null;
      updateQuery.encryptedBundleEndpointCaCert = flatBundle.bundleEndpointCaCert
        ? orgDataKeyEncryptor({ plainText: Buffer.from(flatBundle.bundleEndpointCaCert) }).cipherTextBlob
        : null;
    }

    const updatedSpiffeAuth = await identitySpiffeAuthDAL.transaction(async (tx) => {
      const doc = await identitySpiffeAuthDAL.updateById(identitySpiffeAuth.id, updateQuery, tx);
      await emitIdentityAuthMethodChanged(
        eventEmitter,
        {
          membership: identityMembershipOrg,
          authMethod: IdentityAuthMethod.SPIFFE_AUTH,
          change: IdentityAuthMethodChange.Updated,
          actor,
          actorId
        },
        tx
      );
      return doc;
    });

    const decryptedCaBundleJwks = updatedSpiffeAuth.encryptedCaBundleJwks
      ? orgDataKeyDecryptor({ cipherTextBlob: updatedSpiffeAuth.encryptedCaBundleJwks }).toString()
      : "";
    const decryptedBundleEndpointCaCert = updatedSpiffeAuth.encryptedBundleEndpointCaCert
      ? orgDataKeyDecryptor({ cipherTextBlob: updatedSpiffeAuth.encryptedBundleEndpointCaCert }).toString()
      : "";

    await identityAccessTokenService.invalidateTrustedIpsCache(identityId, IdentityAuthMethod.SPIFFE_AUTH);
    return {
      ...updatedSpiffeAuth,
      orgId: identityMembershipOrg.scopeOrgId,
      trustBundleDistribution: $buildTrustBundleDistributionResponse(
        updatedSpiffeAuth,
        decryptedCaBundleJwks,
        decryptedBundleEndpointCaCert
      )
    };
  };

  const getSpiffeAuth = async ({ identityId, actorId, actor, actorAuthMethod, actorOrgId }: TGetSpiffeAuthDTO) => {
    const identityMembershipOrg = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId
    });
    if (!identityMembershipOrg) throw new NotFoundError({ message: `Failed to find identity with ID ${identityId}` });
    if (identityMembershipOrg.identity.orgId !== actorOrgId) {
      throw new ForbiddenRequestError({ message: "Sub organization not authorized to access this identity" });
    }

    if (!identityMembershipOrg.identity.authMethods.includes(IdentityAuthMethod.SPIFFE_AUTH)) {
      throw new BadRequestError({ message: "The identity does not have SPIFFE Auth attached" });
    }

    if (identityMembershipOrg.identity.projectId) {
      const { permission } = await permissionService.getProjectPermission({
        actionProjectType: ActionProjectType.Any,
        actor,
        actorId,
        projectId: identityMembershipOrg.identity.projectId,
        actorAuthMethod,
        actorOrgId
      });

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionIdentityActions.Read,
        subject(ProjectPermissionSub.Identity, { identityId })
      );
    } else {
      const { permission } = await permissionService.getOrgPermission({
        scope: OrganizationActionScope.Any,
        actor,
        actorId,
        orgId: identityMembershipOrg.scopeOrgId,
        actorAuthMethod,
        actorOrgId
      });

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

    const identitySpiffeAuth = await identitySpiffeAuthDAL.findOne({ identityId });

    const { decryptor: orgDataKeyDecryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: actorOrgId
    });

    const decryptedCaBundleJwks = identitySpiffeAuth.encryptedCaBundleJwks
      ? orgDataKeyDecryptor({ cipherTextBlob: identitySpiffeAuth.encryptedCaBundleJwks }).toString()
      : "";
    const decryptedBundleEndpointCaCert = identitySpiffeAuth.encryptedBundleEndpointCaCert
      ? orgDataKeyDecryptor({ cipherTextBlob: identitySpiffeAuth.encryptedBundleEndpointCaCert }).toString()
      : "";

    return {
      ...identitySpiffeAuth,
      orgId: identityMembershipOrg.scopeOrgId,
      trustBundleDistribution: $buildTrustBundleDistributionResponse(
        identitySpiffeAuth,
        decryptedCaBundleJwks,
        decryptedBundleEndpointCaCert
      )
    };
  };

  const revokeSpiffeAuth = async ({
    identityId,
    actorId,
    actor,
    actorAuthMethod,
    actorOrgId,
    isActorSuperAdmin
  }: TRevokeSpiffeAuthDTO) => {
    const identityMembershipOrg = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId
    });
    if (!identityMembershipOrg) {
      throw new NotFoundError({ message: "Failed to find identity" });
    }
    if (identityMembershipOrg.identity.orgId !== actorOrgId) {
      throw new ForbiddenRequestError({ message: "Sub organization not authorized to access this identity" });
    }

    if (!identityMembershipOrg.identity.authMethods.includes(IdentityAuthMethod.SPIFFE_AUTH)) {
      throw new BadRequestError({ message: "The identity does not have SPIFFE auth" });
    }

    await assertIdentityAuthAccessAllowed(
      { permissionService, orgDAL },
      {
        identityId,
        orgId: identityMembershipOrg.scopeOrgId,
        projectId: identityMembershipOrg.identity.projectId,
        action: OrgPermissionIdentityActions.RevokeAuth,
        baseMessage: "Failed to revoke SPIFFE auth of identity with more privileged role",
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      }
    );

    await validateIdentityUpdateForSuperAdminPrivileges(identityId, isActorSuperAdmin);

    const revokedIdentitySpiffeAuth = await identitySpiffeAuthDAL.transaction(async (tx) => {
      const deletedSpiffeAuth = await identitySpiffeAuthDAL.delete({ identityId }, tx);
      await identityAccessTokenDAL.delete({ identityId, authMethod: IdentityAuthMethod.SPIFFE_AUTH }, tx);

      await emitIdentityAuthMethodChanged(
        eventEmitter,
        {
          membership: identityMembershipOrg,
          authMethod: IdentityAuthMethod.SPIFFE_AUTH,
          change: IdentityAuthMethodChange.Removed,
          actor,
          actorId
        },
        tx
      );
      return deletedSpiffeAuth?.[0];
    });

    // Detaching the auth method must invalidate any tokens already issued
    // through it; without this, leaked tokens authenticate up to MAX_AGE
    // even after the admin pulled the auth method.
    await identityAccessTokenService.revokeTokensForIdentityAuthMethod({
      identityId,
      authMethod: IdentityAuthMethod.SPIFFE_AUTH
    });
    await identityAccessTokenService.invalidateTrustedIpsCache(identityId, IdentityAuthMethod.SPIFFE_AUTH);

    const { decryptor: orgDataKeyDecryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: identityMembershipOrg.scopeOrgId
    });

    const decryptedCaBundleJwks = revokedIdentitySpiffeAuth.encryptedCaBundleJwks
      ? orgDataKeyDecryptor({ cipherTextBlob: revokedIdentitySpiffeAuth.encryptedCaBundleJwks }).toString()
      : "";
    const decryptedBundleEndpointCaCert = revokedIdentitySpiffeAuth.encryptedBundleEndpointCaCert
      ? orgDataKeyDecryptor({ cipherTextBlob: revokedIdentitySpiffeAuth.encryptedBundleEndpointCaCert }).toString()
      : "";

    return {
      ...revokedIdentitySpiffeAuth,
      orgId: identityMembershipOrg.scopeOrgId,
      trustBundleDistribution: $buildTrustBundleDistributionResponse(
        revokedIdentitySpiffeAuth,
        decryptedCaBundleJwks,
        decryptedBundleEndpointCaCert
      )
    };
  };

  const refreshSpiffeBundle = async ({
    identityId,
    actorId,
    actor,
    actorAuthMethod,
    actorOrgId
  }: TGetSpiffeAuthDTO) => {
    const identityMembershipOrg = await membershipIdentityDAL.getIdentityById({
      scopeData: {
        scope: AccessScope.Organization,
        orgId: actorOrgId
      },
      identityId
    });
    if (!identityMembershipOrg) throw new NotFoundError({ message: `Failed to find identity with ID ${identityId}` });
    if (identityMembershipOrg.identity.orgId !== actorOrgId) {
      throw new ForbiddenRequestError({ message: "Sub organization not authorized to access this identity" });
    }

    if (!identityMembershipOrg.identity.authMethods.includes(IdentityAuthMethod.SPIFFE_AUTH)) {
      throw new BadRequestError({ message: "The identity does not have SPIFFE Auth attached" });
    }

    if (identityMembershipOrg.identity.projectId) {
      const { permission } = await permissionService.getProjectPermission({
        actionProjectType: ActionProjectType.Any,
        actor,
        actorId,
        projectId: identityMembershipOrg.identity.projectId,
        actorAuthMethod,
        actorOrgId
      });

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionIdentityActions.Edit,
        subject(ProjectPermissionSub.Identity, { identityId })
      );
    } else {
      const { permission } = await permissionService.getOrgPermission({
        scope: OrganizationActionScope.Any,
        actor,
        actorId,
        orgId: identityMembershipOrg.scopeOrgId,
        actorAuthMethod,
        actorOrgId
      });

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

    const identitySpiffeAuth = await identitySpiffeAuthDAL.findOne({ identityId });

    if (identitySpiffeAuth.configurationType === SpiffeTrustBundleProfile.STATIC) {
      throw new BadRequestError({
        message: "Bundle refresh is only applicable to identities with remote SPIFFE Auth configuration"
      });
    }

    const { jwksJson } = await $resolveJwks({
      config: identitySpiffeAuth,
      orgId: identityMembershipOrg.scopeOrgId,
      forceRefresh: true
    });

    const { decryptor: orgDataKeyDecryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: actorOrgId
    });

    const decryptedBundleEndpointCaCert = identitySpiffeAuth.encryptedBundleEndpointCaCert
      ? orgDataKeyDecryptor({ cipherTextBlob: identitySpiffeAuth.encryptedBundleEndpointCaCert }).toString()
      : "";

    // Use the existing row with the refresh timestamp set to now, avoiding a redundant DB read.
    const refreshedRow = {
      ...identitySpiffeAuth,
      cachedBundleLastRefreshedAt: new Date()
    };

    return {
      ...refreshedRow,
      orgId: identityMembershipOrg.scopeOrgId,
      trustBundleDistribution: $buildTrustBundleDistributionResponse(
        refreshedRow,
        jwksJson,
        decryptedBundleEndpointCaCert
      )
    };
  };

  return {
    login,
    attachSpiffeAuth,
    updateSpiffeAuth,
    getSpiffeAuth,
    revokeSpiffeAuth,
    refreshSpiffeBundle
  };
};