Skip to content

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

15.2 KB
Raw Blame
import { z } from "zod";

import { IdentityAuthMethod, IdentityOciAuthsSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { ApiDocsTags, OCI_AUTH } from "@app/lib/api-docs";
import { UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type";
import { validateTenancy, validateUsernames } from "@app/services/identity-oci-auth/identity-oci-auth-validators";
import { TIdentityTrustedIp } from "@app/services/identity/identity-types";
import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";

export const registerIdentityOciAuthRouter = async (server: FastifyZodProvider) => {
  server.route({
    method: "POST",
    url: "/oci-auth/login",
    config: {
      rateLimit: writeLimit
    },
    schema: {
      hide: false,
      operationId: "loginWithOciAuth",
      tags: [ApiDocsTags.OciAuth],
      description: "Login with OCI Auth for machine identity",
      body: z.object({
        identityId: z.string().trim().describe(OCI_AUTH.LOGIN.identityId),
        userOcid: z.string().trim().describe(OCI_AUTH.LOGIN.userOcid),
        headers: z
          .object({
            authorization: z.string(),
            host: z.string(),
            "x-date": z.string().optional(),
            date: z.string().optional()
          })
          .superRefine((val, ctx) => {
            if (!val.date && !val["x-date"]) {
              ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: "Either date or x-date must be provided",
                path: ["headers", "date"]
              });
            }
          })
          .describe(OCI_AUTH.LOGIN.headers),
        organizationSlug: slugSchema().optional().describe(OCI_AUTH.LOGIN.organizationSlug)
      }),
      response: {
        200: z.object({
          accessToken: z.string(),
          expiresIn: z.coerce.number(),
          accessTokenMaxTTL: z.coerce.number(),
          tokenType: z.literal("Bearer")
        })
      }
    },
    handler: async (req) => {
      try {
        const { identityOciAuth, accessToken, identityAccessToken, identity } =
          await server.services.identityOciAuth.login(req.body);

        await server.services.auditLog.createAuditLog({
          ...req.auditLogInfo,
          actor: {
            type: ActorType.IDENTITY,
            metadata: {
              identityId: identityOciAuth.identityId,
              name: identity.name
            }
          },
          orgId: identity.orgId,
          event: {
            type: EventType.LOGIN_IDENTITY_OCI_AUTH,
            metadata: {
              identityId: identityOciAuth.identityId,
              identityAccessTokenId: identityAccessToken.id,
              identityOciAuthId: identityOciAuth.id
            }
          }
        });

        void server.services.telemetry
          .sendPostHogEvents({
            event: PostHogEventTypes.MachineIdentityLogin,
            distinctId: `identity-${identityOciAuth.identityId}`,
            organizationId: identity.orgId,
            properties: {
              identityId: identityOciAuth.identityId,
              orgId: identity.orgId,
              authMethod: IdentityAuthMethod.OCI_AUTH
            }
          })
          .catch((error) => {
            logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);
          });

        return {
          accessToken,
          tokenType: "Bearer" as const,
          expiresIn: identityOciAuth.accessTokenTTL,
          accessTokenMaxTTL: identityOciAuth.accessTokenMaxTTL
        };
      } catch (error) {
        if (
          error instanceof UnauthorizedError &&
          error.detail?.orgId &&
          error.detail?.identityId &&
          error.detail?.identityName
        ) {
          await server.services.auditLog.createAuditLog({
            ...req.auditLogInfo,
            actor: {
              type: ActorType.IDENTITY,
              metadata: {
                identityId: error.detail.identityId as string,
                name: error.detail.identityName as string
              }
            },
            orgId: error.detail.orgId as string,
            event: {
              type: EventType.LOGIN_IDENTITY_OCI_AUTH_FAILED,
              metadata: {
                identityId: error.detail.identityId as string,
                reasonCode: error.detail.reasonCode as string,
                message: error.message
              }
            }
          });
        }
        throw error;
      }
    }
  });

  server.route({
    method: "POST",
    url: "/oci-auth/identities/:identityId",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      hide: false,
      operationId: "attachOciAuth",
      tags: [ApiDocsTags.OciAuth],
      description: "Attach OCI Auth configuration onto machine identity",
      security: [
        {
          bearerAuth: []
        }
      ],
      params: z.object({
        identityId: z.string().trim().describe(OCI_AUTH.ATTACH.identityId)
      }),
      body: z
        .object({
          tenancyOcid: validateTenancy.describe(OCI_AUTH.ATTACH.tenancyOcid),
          allowedUsernames: validateUsernames.describe(OCI_AUTH.ATTACH.allowedUsernames),
          accessTokenTrustedIps: z
            .object({
              ipAddress: z.string().trim()
            })
            .array()
            .min(1)
            .default([{ ipAddress: "0.0.0.0/0" }, { ipAddress: "::/0" }])
            .describe(OCI_AUTH.ATTACH.accessTokenTrustedIps),
          accessTokenTTL: z
            .number()
            .int()
            .min(0)
            .max(315360000)
            .default(2592000)
            .describe(OCI_AUTH.ATTACH.accessTokenTTL),
          accessTokenMaxTTL: z
            .number()
            .int()
            .min(1)
            .max(315360000)
            .default(2592000)
            .describe(OCI_AUTH.ATTACH.accessTokenMaxTTL),
          accessTokenNumUsesLimit: z.number().int().min(0).default(0).describe(OCI_AUTH.ATTACH.accessTokenNumUsesLimit)
        })
        .refine(
          (val) => val.accessTokenTTL <= val.accessTokenMaxTTL,
          "Access Token TTL cannot be greater than Access Token Max TTL."
        ),
      response: {
        200: z.object({
          identityOciAuth: IdentityOciAuthsSchema
        })
      }
    },
    handler: async (req) => {
      const identityOciAuth = await server.services.identityOciAuth.attachOciAuth({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        ...req.body,
        identityId: req.params.identityId,
        isActorSuperAdmin: isSuperAdmin(req.auth)
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: identityOciAuth.orgId,
        event: {
          type: EventType.ADD_IDENTITY_OCI_AUTH,
          metadata: {
            identityId: identityOciAuth.identityId,
            tenancyOcid: identityOciAuth.tenancyOcid,
            allowedUsernames: identityOciAuth.allowedUsernames || null,
            accessTokenTTL: identityOciAuth.accessTokenTTL,
            accessTokenMaxTTL: identityOciAuth.accessTokenMaxTTL,
            accessTokenTrustedIps: identityOciAuth.accessTokenTrustedIps as TIdentityTrustedIp[],
            accessTokenNumUsesLimit: identityOciAuth.accessTokenNumUsesLimit
          }
        }
      });

      void server.services.telemetry
        .sendPostHogEvents({
          event: PostHogEventTypes.MachineIdentityAuthMethodAttached,
          distinctId: getTelemetryDistinctId(req),
          organizationId: identityOciAuth.orgId,
          properties: {
            identityId: identityOciAuth.identityId,
            orgId: identityOciAuth.orgId,
            authMethod: IdentityAuthMethod.OCI_AUTH
          }
        })
        .catch((error) => {
          logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);
        });

      return { identityOciAuth };
    }
  });

  server.route({
    method: "PATCH",
    url: "/oci-auth/identities/:identityId",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      hide: false,
      operationId: "updateOciAuth",
      tags: [ApiDocsTags.OciAuth],
      description: "Update OCI Auth configuration on machine identity",
      security: [
        {
          bearerAuth: []
        }
      ],
      params: z.object({
        identityId: z.string().describe(OCI_AUTH.UPDATE.identityId)
      }),
      body: z
        .object({
          tenancyOcid: validateTenancy.describe(OCI_AUTH.UPDATE.tenancyOcid),
          allowedUsernames: validateUsernames.describe(OCI_AUTH.UPDATE.allowedUsernames),
          accessTokenTrustedIps: z
            .object({
              ipAddress: z.string().trim()
            })
            .array()
            .min(1)
            .optional()
            .describe(OCI_AUTH.UPDATE.accessTokenTrustedIps),
          accessTokenTTL: z.number().int().min(0).max(315360000).optional().describe(OCI_AUTH.UPDATE.accessTokenTTL),
          accessTokenNumUsesLimit: z.number().int().min(0).optional().describe(OCI_AUTH.UPDATE.accessTokenNumUsesLimit),
          accessTokenMaxTTL: z
            .number()
            .int()
            .max(315360000)
            .min(0)
            .optional()
            .describe(OCI_AUTH.UPDATE.accessTokenMaxTTL)
        })
        .refine(
          (val) => (val.accessTokenMaxTTL && val.accessTokenTTL ? val.accessTokenTTL <= val.accessTokenMaxTTL : true),
          "Access Token TTL cannot be greater than Access Token Max TTL."
        ),
      response: {
        200: z.object({
          identityOciAuth: IdentityOciAuthsSchema
        })
      }
    },
    handler: async (req) => {
      const identityOciAuth = await server.services.identityOciAuth.updateOciAuth({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        ...req.body,
        identityId: req.params.identityId,
        allowedUsernames: req.body.allowedUsernames || null,
        isActorSuperAdmin: isSuperAdmin(req.auth)
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: identityOciAuth.orgId,
        event: {
          type: EventType.UPDATE_IDENTITY_OCI_AUTH,
          metadata: {
            identityId: identityOciAuth.identityId,
            tenancyOcid: identityOciAuth.tenancyOcid,
            allowedUsernames: identityOciAuth.allowedUsernames || null,
            accessTokenTTL: identityOciAuth.accessTokenTTL,
            accessTokenMaxTTL: identityOciAuth.accessTokenMaxTTL,
            accessTokenTrustedIps: identityOciAuth.accessTokenTrustedIps as TIdentityTrustedIp[],
            accessTokenNumUsesLimit: identityOciAuth.accessTokenNumUsesLimit
          }
        }
      });

      void server.services.telemetry
        .sendPostHogEvents({
          event: PostHogEventTypes.MachineIdentityAuthMethodUpdated,
          distinctId: getTelemetryDistinctId(req),
          organizationId: identityOciAuth.orgId,
          properties: {
            identityId: identityOciAuth.identityId,
            orgId: identityOciAuth.orgId,
            authMethod: IdentityAuthMethod.OCI_AUTH
          }
        })
        .catch((error) => {
          logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);
        });

      return { identityOciAuth };
    }
  });

  server.route({
    method: "GET",
    url: "/oci-auth/identities/:identityId",
    config: {
      rateLimit: readLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
    schema: {
      hide: false,
      operationId: "getOciAuth",
      tags: [ApiDocsTags.OciAuth],
      description: "Retrieve OCI Auth configuration on machine identity",
      security: [
        {
          bearerAuth: []
        }
      ],
      params: z.object({
        identityId: z.string().describe(OCI_AUTH.RETRIEVE.identityId)
      }),
      response: {
        200: z.object({
          identityOciAuth: IdentityOciAuthsSchema
        })
      }
    },
    handler: async (req) => {
      const identityOciAuth = await server.services.identityOciAuth.getOciAuth({
        identityId: req.params.identityId,
        actor: req.permission.type,
        actorId: req.permission.id,
        actorOrgId: req.permission.orgId,
        actorAuthMethod: req.permission.authMethod
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: identityOciAuth.orgId,
        event: {
          type: EventType.GET_IDENTITY_OCI_AUTH,
          metadata: {
            identityId: identityOciAuth.identityId
          }
        }
      });
      return { identityOciAuth };
    }
  });

  server.route({
    method: "DELETE",
    url: "/oci-auth/identities/:identityId",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      hide: false,
      operationId: "deleteOciAuth",
      tags: [ApiDocsTags.OciAuth],
      description: "Delete OCI Auth configuration on machine identity",
      security: [
        {
          bearerAuth: []
        }
      ],
      params: z.object({
        identityId: z.string().describe(OCI_AUTH.REVOKE.identityId)
      }),
      response: {
        200: z.object({
          identityOciAuth: IdentityOciAuthsSchema
        })
      }
    },
    handler: async (req) => {
      const identityOciAuth = await server.services.identityOciAuth.revokeIdentityOciAuth({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        identityId: req.params.identityId,
        isActorSuperAdmin: isSuperAdmin(req.auth)
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        orgId: identityOciAuth.orgId,
        event: {
          type: EventType.REVOKE_IDENTITY_OCI_AUTH,
          metadata: {
            identityId: identityOciAuth.identityId
          }
        }
      });

      void server.services.telemetry
        .sendPostHogEvents({
          event: PostHogEventTypes.MachineIdentityAuthMethodRevoked,
          distinctId: getTelemetryDistinctId(req),
          organizationId: identityOciAuth.orgId,
          properties: {
            identityId: identityOciAuth.identityId,
            orgId: identityOciAuth.orgId,
            authMethod: IdentityAuthMethod.OCI_AUTH
          }
        })
        .catch((error) => {
          logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);
        });

      return { identityOciAuth };
    }
  });
};