Skip to content

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

46.6 KB
Raw Blame
import RE2 from "re2";
import { z } from "zod";

import { TDynamicSecrets } from "@app/db/schemas";
import { SSH_CERT_KEY_ALGORITHMS, SshCertKeyAlgorithm } from "@app/lib/ssh";
import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
import { ResourceMetadataNonEncryptionSchema } from "@app/services/resource-metadata/resource-metadata-schema";
import { TConstraints } from "@app/services/secret-validation-rule/secret-validation-rule-types";

import {
  ActorIdentityAttributes,
  TDynamicSecretLeaseConfig
} from "../../dynamic-secret-lease/dynamic-secret-lease-types";

export type PasswordRequirements = {
  length: number;
  required: {
    lowercase: number;
    uppercase: number;
    digits: number;
    symbols: number;
  };
  allowedSymbols?: string;
};

export enum SqlProviders {
  Postgres = "postgres",
  MySQL = "mysql2",
  Oracle = "oracledb",
  MsSQL = "mssql",
  SapAse = "sap-ase",
  Vertica = "vertica"
}

export enum AwsIamAuthType {
  AssumeRole = "assume-role",
  AccessKey = "access-key",
  IRSA = "irsa"
}

export enum AwsIamCredentialType {
  IamUser = "iam-user",
  TemporaryCredentials = "temporary-credentials"
}

export enum ElasticSearchAuthTypes {
  User = "user",
  ApiKey = "api-key"
}

export enum LdapCredentialType {
  Dynamic = "dynamic",
  Static = "static"
}

export enum KubernetesCredentialType {
  Static = "static",
  Dynamic = "dynamic"
}

export enum KubernetesRoleType {
  ClusterRole = "cluster-role",
  Role = "role"
}

export enum KubernetesAuthMethod {
  Gateway = "gateway",
  Api = "api"
}

export enum TotpConfigType {
  URL = "url",
  MANUAL = "manual"
}

export enum TotpAlgorithm {
  SHA1 = "sha1",
  SHA256 = "sha256",
  SHA512 = "sha512"
}

export const DynamicSecretRedisDBSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  username: z.string().trim(), // this is often "default".
  password: z.string().trim().optional(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true)
});

export const DynamicSecretAwsElastiCacheSchema = z.object({
  clusterName: z.string().trim().min(1),
  accessKeyId: z.string().trim().min(1),
  secretAccessKey: z.string().trim().min(1),

  region: z.string().trim(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim()
});

export enum AwsMemoryDbAuthType {
  IAM = "iam"
}

export const DynamicSecretAwsMemoryDbSchema = z.object({
  clusterName: z.string().trim().min(1),
  auth: z.discriminatedUnion("type", [
    z.object({
      type: z.literal(AwsMemoryDbAuthType.IAM),
      accessKeyId: z.string().trim().min(1),
      secretAccessKey: z.string().trim().min(1)
    })
  ]),
  region: z.string().trim().min(1),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim()
});

export const DynamicSecretElasticSearchSchema = z.object({
  host: z.string().trim().min(1),
  port: z.number(),
  roles: z.array(z.string().trim().min(1)).min(1),

  // two auth types "user, apikey"
  auth: z.discriminatedUnion("type", [
    z.object({
      type: z.literal(ElasticSearchAuthTypes.User),
      username: z.string().trim(),
      password: z.string().trim()
    }),
    z.object({
      type: z.literal(ElasticSearchAuthTypes.ApiKey),
      apiKey: z.string().trim(),
      apiKeyId: z.string().trim()
    })
  ]),

  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true)
});

export const DynamicSecretRabbitMqSchema = z.object({
  host: z.string().trim().min(1),
  port: z.number(),
  tags: z.array(z.string().trim()).default([]),

  username: z.string().trim().min(1),
  password: z.string().trim().min(1),

  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true),

  virtualHost: z.object({
    name: z.string().trim().min(1),
    permissions: z.object({
      read: z.string().trim().min(1),
      write: z.string().trim().min(1),
      configure: z.string().trim().min(1)
    })
  })
});

export const DynamicSecretSqlDBSchema = z.object({
  client: z.nativeEnum(SqlProviders),
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  database: z.string().trim(),
  username: z.string().trim(),
  password: z.string().trim(),
  passwordRequirements: z
    .object({
      length: z.number().min(1).max(250),
      required: z
        .object({
          lowercase: z.number().min(0),
          uppercase: z.number().min(0),
          digits: z.number().min(0),
          symbols: z.number().min(0)
        })
        .refine((data) => {
          const total = Object.values(data).reduce((sum, count) => sum + count, 0);
          return total <= 250;
        }, "Sum of required characters cannot exceed 250"),
      allowedSymbols: z.string().optional()
    })
    .refine((data) => {
      const total = Object.values(data.required).reduce((sum, count) => sum + count, 0);
      return total <= data.length;
    }, "Sum of required characters cannot exceed the total length")
    .optional()
    .describe("Password generation requirements"),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  sslEnabled: z.boolean().optional(),
  sslRejectUnauthorized: z.boolean().default(true),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional()
});

export const DynamicSecretClickhouseSchema = z.object({
  host: z.string().trim(),
  port: z.number(),
  database: z.string().trim(),
  username: z.string().trim(),
  password: z.string().trim(),
  passwordRequirements: z
    .object({
      length: z.number().min(1).max(250),
      required: z
        .object({
          lowercase: z.number().min(0),
          uppercase: z.number().min(0),
          digits: z.number().min(0),
          symbols: z.number().min(0)
        })
        .refine((data) => {
          const total = Object.values(data).reduce((sum, count) => sum + count, 0);
          return total <= 250;
        }, "Sum of required characters cannot exceed 250"),
      allowedSymbols: z.string().optional()
    })
    .refine((data) => {
      const total = Object.values(data.required).reduce((sum, count) => sum + count, 0);
      return total <= data.length;
    }, "Sum of required characters cannot exceed the total length")
    .optional()
    .describe("Password generation requirements"),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional()
});

export const DynamicSecretCassandraSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  localDataCenter: z.string().trim().min(1),
  keyspace: z.string().trim().optional(),
  username: z.string().trim(),
  password: z.string().trim(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true)
});

export const DynamicSecretSapAseSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  database: z.string().trim(),
  username: z.string().trim(),
  password: z.string().trim(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim()
});

export const DynamicSecretAwsIamSchema = z.preprocess(
  (val) => {
    if (typeof val === "object" && val !== null && !Object.hasOwn(val, "method")) {
      // eslint-disable-next-line no-param-reassign
      (val as { method: string }).method = AwsIamAuthType.AccessKey;
    }
    return val;
  },
  z
    .discriminatedUnion("method", [
      z.object({
        method: z.literal(AwsIamAuthType.AccessKey),
        credentialType: z.nativeEnum(AwsIamCredentialType).default(AwsIamCredentialType.IamUser),
        accessKey: z.string().trim().min(1),
        secretAccessKey: z.string().trim().min(1),
        region: z.string().trim().min(1),
        awsPath: z.string().trim().optional(),
        permissionBoundaryPolicyArn: z.string().trim().optional(),
        policyDocument: z.string().trim().optional(),
        userGroups: z.string().trim().optional(),
        policyArns: z.string().trim().optional(),
        tags: ResourceMetadataNonEncryptionSchema.optional()
      }),
      z.object({
        method: z.literal(AwsIamAuthType.AssumeRole),
        credentialType: z.nativeEnum(AwsIamCredentialType).default(AwsIamCredentialType.IamUser),
        roleArn: z.string().trim().min(1, "Role ARN required"),
        region: z.string().trim().min(1),
        awsPath: z.string().trim().optional(),
        permissionBoundaryPolicyArn: z.string().trim().optional(),
        policyDocument: z.string().trim().optional(),
        userGroups: z.string().trim().optional(),
        policyArns: z.string().trim().optional(),
        sessionPolicyArns: z.string().trim().optional(),
        sessionPolicyDocument: z.string().trim().optional(),
        tags: ResourceMetadataNonEncryptionSchema.optional()
      }),
      z.object({
        method: z.literal(AwsIamAuthType.IRSA),
        credentialType: z.nativeEnum(AwsIamCredentialType).default(AwsIamCredentialType.IamUser),
        region: z.string().trim().min(1),
        awsPath: z.string().trim().optional(),
        permissionBoundaryPolicyArn: z.string().trim().optional(),
        policyDocument: z.string().trim().optional(),
        userGroups: z.string().trim().optional(),
        policyArns: z.string().trim().optional(),
        tags: ResourceMetadataNonEncryptionSchema.optional()
      })
    ])
    .superRefine((data, ctx) => {
      const reject = (path: string, message: string) =>
        ctx.addIssue({ path: [path], code: z.ZodIssueCode.custom, message });
      const countCsvArns = (value: string) => value.split(",").filter((arn) => arn.trim()).length;

      if (data.credentialType === AwsIamCredentialType.TemporaryCredentials) {
        // session policy fields exist only on the AssumeRole member (STS AssumeRole alone supports them; GetSessionToken,
        // used by access key / IRSA, has no Policy/PolicyArns params), so this guard also narrows the type for TS
        if (data.method === AwsIamAuthType.AssumeRole) {
          if (data.sessionPolicyArns && countCsvArns(data.sessionPolicyArns) > 10) {
            reject("sessionPolicyArns", "AWS allows at most 10 managed policy ARNs as session policies");
          }
          // session policies count the full plaintext against the 2,048 character limit
          if (data.sessionPolicyDocument && data.sessionPolicyDocument.length > 2048) {
            reject("sessionPolicyDocument", "AWS limits inline session policies to 2,048 characters of plaintext");
          }
        }
        return;
      }

      // IAM User credential type: session policies have no meaning because no STS session is created
      if (data.method === AwsIamAuthType.AssumeRole) {
        if (data.sessionPolicyArns) {
          reject(
            "sessionPolicyArns",
            "Session policy ARNs only apply to the Temporary Credentials credential type. Use Policy ARNs to grant permissions to created IAM users."
          );
        }
        if (data.sessionPolicyDocument) {
          reject(
            "sessionPolicyDocument",
            "A session policy document only applies to the Temporary Credentials credential type. Use a policy document to grant permissions to created IAM users."
          );
        }
      }
    })
);

export const DynamicSecretMongoAtlasSchema = z.object({
  adminPublicKey: z.string().trim().min(1).describe("Admin user public api key"),
  adminPrivateKey: z.string().trim().min(1).describe("Admin user private api key"),
  groupId: z
    .string()
    .trim()
    .min(1)
    .describe("Unique 24-hexadecimal digit string that identifies your project. This is same as project id"),
  roles: z
    .object({
      collectionName: z.string().optional().describe("Collection on which this role applies."),
      databaseName: z.string().min(1).describe("Database to which the user is granted access privileges."),
      roleName: z
        .string()
        .min(1)
        .describe(
          ' Enum: "atlasAdmin" "backup" "clusterMonitor" "dbAdmin" "dbAdminAnyDatabase" "enableSharding" "read" "readAnyDatabase" "readWrite" "readWriteAnyDatabase" "<a custom role name>".Human-readable label that identifies a group of privileges assigned to a database user. This value can either be a built-in role or a custom role.'
        )
    })
    .array()
    .min(1),
  scopes: z
    .object({
      name: z
        .string()
        .min(1)
        .describe(
          "Human-readable label that identifies the cluster or MongoDB Atlas Data Lake that this database user can access."
        ),
      type: z
        .string()
        .min(1)
        .describe("Category of resource that this database user can access. Enum: CLUSTER, DATA_LAKE, STREAM")
    })
    .array()
});

export const DynamicSecretMongoDBSchema = z.object({
  host: z.string().min(1).trim().toLowerCase(),
  port: z.number().optional().nullable(),
  username: z.string().min(1).trim(),
  password: z.string().min(1).trim(),
  database: z.string().min(1).trim(),
  ca: z.string().trim().optional().nullable(),
  sslRejectUnauthorized: z.boolean().default(true),
  roles: z
    .string()
    .array()
    .min(1)
    .describe(
      'Enum: "atlasAdmin" "backup" "clusterMonitor" "dbAdmin" "dbAdminAnyDatabase" "enableSharding" "read" "readAnyDatabase" "readWrite" "readWriteAnyDatabase" "<a custom role name>".Human-readable label that identifies a group of privileges assigned to a database user. This value can either be a built-in role or a custom role.'
    )
});

export const DynamicSecretSapHanaSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  username: z.string().trim(),
  password: z.string().trim(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true)
});

export const DynamicSecretSnowflakeSchema = z.object({
  accountId: z.string().trim().min(1),
  orgId: z.string().trim().min(1),
  username: z.string().trim().min(1),
  password: z.string().trim().min(1),
  creationStatement: z.string().trim().min(1),
  revocationStatement: z.string().trim().min(1),
  renewStatement: z.string().trim().optional()
});

export const AzureEntraIDSchema = z.object({
  tenantId: z.string().trim().min(1),
  userId: z.string().trim().min(1),
  email: z.string().trim().min(1),
  applicationId: z.string().trim().min(1),
  clientSecret: z.string().trim().min(1)
});

export const DynamicSecretAzureSqlDBSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  database: z.string().trim(),
  masterDatabase: z.string().trim().optional().default("master"),
  username: z.string().trim(),
  password: z.string().trim(),
  passwordRequirements: z
    .object({
      length: z.number().min(1).max(250),
      required: z
        .object({
          lowercase: z.number().min(0),
          uppercase: z.number().min(0),
          digits: z.number().min(0),
          symbols: z.number().min(0)
        })
        .refine((data) => {
          const total = Object.values(data).reduce((sum, count) => sum + count, 0);
          return total <= 250;
        }, "Sum of required characters cannot exceed 250"),
      allowedSymbols: z.string().optional()
    })
    .refine((data) => {
      const total = Object.values(data.required).reduce((sum, count) => sum + count, 0);
      return total <= data.length;
    }, "Sum of required characters cannot exceed the total length")
    .optional()
    .describe("Password generation requirements"),
  masterCreationStatement: z.string().trim(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  renewStatement: z.string().trim().optional(),
  ca: z.string().optional(),
  sslEnabled: z.boolean().optional(),
  sslRejectUnauthorized: z.boolean().default(true),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional()
});

export const LdapSchema = z.union([
  z.object({
    url: z.string().trim().min(1),
    binddn: z.string().trim().min(1),
    bindpass: z.string().trim().min(1),
    ca: z.string().optional(),
    sslRejectUnauthorized: z.boolean().default(true),
    credentialType: z.literal(LdapCredentialType.Dynamic).optional().default(LdapCredentialType.Dynamic),
    creationLdif: z.string().min(1),
    revocationLdif: z.string().min(1),
    rollbackLdif: z.string().optional()
  }),
  z.object({
    url: z.string().trim().min(1),
    binddn: z.string().trim().min(1),
    bindpass: z.string().trim().min(1),
    ca: z.string().optional(),
    sslRejectUnauthorized: z.boolean().default(true),
    credentialType: z.literal(LdapCredentialType.Static),
    rotationLdif: z.string().min(1)
  })
]);

export const DynamicSecretKubernetesSchema = z
  .discriminatedUnion("credentialType", [
    z.object({
      url: z
        .string()
        .optional()
        .refine((val: string | undefined) => !val || new RE2(/^https?:\/\/.+/).test(val), {
          message: "Invalid URL. Must start with http:// or https:// (e.g. https://example.com)"
        }),
      clusterToken: z.string().trim().optional(),
      ca: z.string().optional(),
      sslEnabled: z.boolean().default(false),
      sslRejectUnauthorized: z.boolean().default(true),
      credentialType: z.literal(KubernetesCredentialType.Static),
      serviceAccountName: z.string().trim().min(1),
      namespace: z
        .string()
        .trim()
        .min(1)
        .refine((val) => !val.includes(","), "Namespace must be a single value, not a comma-separated list")
        .refine(
          (val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(val),
          "Invalid namespace format"
        ),
      gatewayId: z.string().optional().nullable(),
      gatewayPoolId: z.string().optional().nullable(),
      audiences: z.array(z.string().trim().min(1)),
      authMethod: z.nativeEnum(KubernetesAuthMethod).default(KubernetesAuthMethod.Api)
    }),
    z.object({
      url: z
        .string()
        .url()
        .optional()
        .refine((val: string | undefined) => !val || new RE2(/^https?:\/\/.+/).test(val), {
          message: "Invalid URL. Must start with http:// or https:// (e.g. https://example.com)"
        }),
      clusterToken: z.string().trim().optional(),
      ca: z.string().optional(),
      sslEnabled: z.boolean().default(false),
      sslRejectUnauthorized: z.boolean().default(true),
      credentialType: z.literal(KubernetesCredentialType.Dynamic),
      namespace: z
        .string()
        .trim()
        .min(1)
        .refine((val) => {
          const namespaces = val.split(",").map((ns) => ns.trim());
          return (
            namespaces.length > 0 &&
            namespaces.every((ns) => ns.length > 0) &&
            namespaces.every((ns) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen])(ns))
          );
        }, "Must be a valid comma-separated list of namespace values"),
      gatewayId: z.string().optional().nullable(),
      gatewayPoolId: z.string().optional().nullable(),
      audiences: z.array(z.string().trim().min(1)),
      roleType: z.nativeEnum(KubernetesRoleType),
      role: z.string().trim().min(1),
      authMethod: z.nativeEnum(KubernetesAuthMethod).default(KubernetesAuthMethod.Api)
    })
  ])
  .superRefine((data, ctx) => {
    if (data.gatewayId && data.gatewayPoolId) {
      ctx.addIssue({
        path: ["gatewayPoolId"],
        code: z.ZodIssueCode.custom,
        message: "Cannot specify both a gateway and a gateway pool"
      });
    }
    if (data.authMethod === KubernetesAuthMethod.Gateway && !data.gatewayId && !data.gatewayPoolId) {
      ctx.addIssue({
        path: ["gatewayId"],
        code: z.ZodIssueCode.custom,
        message: "When auth method is set to Gateway, a gateway or gateway pool must be selected"
      });
    }
    if (data.authMethod === KubernetesAuthMethod.Api || !data.authMethod) {
      if (!data.clusterToken) {
        ctx.addIssue({
          path: ["clusterToken"],
          code: z.ZodIssueCode.custom,
          message: "When auth method is set to Token, a cluster token must be provided"
        });
      }
      if (!data.url) {
        ctx.addIssue({
          path: ["url"],
          code: z.ZodIssueCode.custom,
          message: "When auth method is set to Token, a cluster URL must be provided"
        });
      }
    }
  });

export const DynamicSecretVerticaSchema = z.object({
  host: z.string().trim().toLowerCase(),
  port: z.number(),
  username: z.string().trim(),
  password: z.string().trim(),
  database: z.string().trim(),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional(),
  creationStatement: z.string().trim(),
  revocationStatement: z.string().trim(),
  passwordRequirements: z
    .object({
      length: z.number().min(1).max(250),
      required: z
        .object({
          lowercase: z.number().min(0),
          uppercase: z.number().min(0),
          digits: z.number().min(0),
          symbols: z.number().min(0)
        })
        .refine((data) => {
          const total = Object.values(data).reduce((sum, count) => sum + count, 0);
          return total <= 250;
        }, "Sum of required characters cannot exceed 250"),
      allowedSymbols: z.string().optional()
    })
    .refine((data) => {
      const total = Object.values(data.required).reduce((sum, count) => sum + count, 0);
      return total <= data.length;
    }, "Sum of required characters cannot exceed the total length")
    .optional()
    .describe("Password generation requirements")
});

export const DynamicSecretTotpSchema = z.discriminatedUnion("configType", [
  z.object({
    configType: z.literal(TotpConfigType.URL),
    url: z
      .string()
      .url()
      .trim()
      .min(1)
      .refine((val) => {
        const urlObj = new URL(val);
        const secret = urlObj.searchParams.get("secret");

        return Boolean(secret);
      }, "OTP URL must contain secret field")
  }),
  z.object({
    configType: z.literal(TotpConfigType.MANUAL),
    secret: z
      .string()
      .trim()
      .min(1)
      .transform((val) => val.replace(/\s+/g, "")),
    period: z.number().optional(),
    algorithm: z.nativeEnum(TotpAlgorithm).optional(),
    digits: z.number().optional()
  })
]);

export const DynamicSecretGcpIamSchema = z.object({
  serviceAccountEmail: z.string().email().trim().min(1, "Service account email required").max(128),
  tokenScopes: z
    .array(z.string().trim().min(1))
    .min(1, "At least one scope is required")
    .default(["https://www.googleapis.com/auth/iam", "https://www.googleapis.com/auth/cloud-platform"])
    .describe("OAuth scopes for the generated access token.")
});

export const DynamicSecretGithubSchema = z.object({
  appId: z.number().min(1).describe("The ID of your GitHub App."),
  installationId: z.number().min(1).describe("The ID of the GitHub App installation."),
  privateKey: z
    .string()
    .trim()
    .min(1)
    .refine(
      (val) =>
        new RE2(
          /^-----BEGIN(?:(?: RSA| PGP| ENCRYPTED)? PRIVATE KEY)-----\s*[\s\S]*?-----END(?:(?: RSA| PGP| ENCRYPTED)? PRIVATE KEY)-----$/
        ).test(val),
      "Invalid PEM format for private key"
    )
    .describe("The private key generated for your GitHub App.")
});

export const DynamicSecretCouchbaseSchema = z.object({
  url: z.string().url().trim().min(1).describe("Couchbase Cloud API URL"),
  orgId: z.string().trim().min(1).describe("Organization ID"),
  projectId: z.string().trim().min(1).describe("Project ID"),
  clusterId: z.string().trim().min(1).describe("Cluster ID"),
  roles: z.array(z.string().trim().min(1)).min(1).describe("Roles to assign to the user"),
  buckets: z
    .union([
      z
        .string()
        .trim()
        .min(1)
        .default("*")
        .refine((val) => {
          if (val.includes(",")) {
            const buckets = val
              .split(",")
              .map((b) => b.trim())
              .filter((b) => b.length > 0);
            if (buckets.includes("*") && buckets.length > 1) {
              return false;
            }
          }
          return true;
        }, "Cannot combine '*' with other bucket names"),
      z
        .array(
          z.object({
            name: z.string().trim().min(1).describe("Bucket name"),
            scopes: z
              .array(
                z.object({
                  name: z.string().trim().min(1).describe("Scope name"),
                  collections: z.array(z.string().trim().min(1)).optional().describe("Collection names")
                })
              )
              .optional()
              .describe("Scopes within the bucket")
          })
        )
        .refine((buckets) => {
          const hasWildcard = buckets.some((bucket) => bucket.name === "*");
          if (hasWildcard && buckets.length > 1) {
            return false;
          }
          return true;
        }, "Cannot combine '*' bucket with other buckets")
    ])
    .default("*")
    .describe(
      "Bucket configuration: '*' for all buckets, scopes, and collections or array of bucket objects with specific scopes and collections"
    ),
  passwordRequirements: z
    .object({
      length: z.number().min(8, "Password must be at least 8 characters").max(128),
      required: z
        .object({
          lowercase: z.number().min(1, "At least 1 lowercase character required"),
          uppercase: z.number().min(1, "At least 1 uppercase character required"),
          digits: z.number().min(1, "At least 1 digit required"),
          symbols: z.number().min(1, "At least 1 special character required")
        })
        .refine((data) => {
          const total = Object.values(data).reduce((sum, count) => sum + count, 0);
          return total <= 128;
        }, "Sum of required characters cannot exceed 128"),
      allowedSymbols: z
        .string()
        .refine((symbols) => {
          const forbiddenChars = ["<", ">", ";", ".", "*", "&", "|", "£"];
          return !forbiddenChars.some((char) => symbols?.includes(char));
        }, "Cannot contain: < > ; . * & | £")
        .optional()
    })
    .refine((data) => {
      const total = Object.values(data.required).reduce((sum, count) => sum + count, 0);
      return total <= data.length;
    }, "Sum of required characters cannot exceed the total length")
    .optional()
    .describe("Password generation requirements for Couchbase"),
  auth: z.object({
    apiKey: z.string().trim().min(1).describe("Couchbase Cloud API Key")
  })
});

export const DynamicSecretMilvusSchema = z.object({
  host: z
    .string()
    .trim()
    .min(1)
    .describe(
      "Milvus endpoint host; uses https when the host includes https:// or a CA is provided, http when the host includes http://, otherwise http (e.g. localhost)."
    ),
  port: z.number().int().min(1).max(65535),
  username: z.string().trim().min(1).describe("Admin username used to manage Milvus users and roles"),
  password: z.string().trim().min(1).describe("Admin password used to manage Milvus users and roles"),
  database: z.string().trim().min(1).default("default").describe("Default Milvus database used for privilege grants"),
  privileges: z
    .array(
      z.object({
        objectType: z
          .string()
          .trim()
          .min(1)
          .describe('Milvus object type (e.g. "Collection", "Database", "Global", "User", "Cluster")'),
        objectName: z.string().trim().min(1).default("*").describe('Name of the target object, or "*" to apply to all'),
        privilege: z
          .string()
          .trim()
          .min(1)
          .describe('Milvus privilege name or built-in privilege group (e.g. "Search", "COLL_RO", "DB_Admin")'),
        dbName: z.string().trim().min(1).optional().describe("Optional database override for this privilege")
      })
    )
    .default([])
    .describe(
      "Privileges granted to an ephemeral role bound to the lease user. Leave empty to create the user with only the built-in public role."
    ),
  ca: z.string().optional(),
  sslRejectUnauthorized: z.boolean().default(true),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional()
});

export enum DynamicSecretProviders {
  SqlDatabase = "sql-database",
  Clickhouse = "clickhouse",
  Cassandra = "cassandra",
  AwsIam = "aws-iam",
  Redis = "redis",
  AwsElastiCache = "aws-elasticache",
  AwsMemoryDb = "aws-memorydb",
  MongoAtlas = "mongo-db-atlas",
  ElasticSearch = "elastic-search",
  MongoDB = "mongo-db",
  RabbitMq = "rabbit-mq",
  AzureEntraID = "azure-entra-id",
  AzureSqlDatabase = "azure-sql-database",
  Ldap = "ldap",
  SapHana = "sap-hana",
  Snowflake = "snowflake",
  Totp = "totp",
  SapAse = "sap-ase",
  Kubernetes = "kubernetes",
  Vertica = "vertica",
  GcpIam = "gcp-iam",
  Github = "github",
  Couchbase = "couchbase",
  Milvus = "milvus",
  Ssh = "ssh",
  IbmApiConnect = "ibm-api-connect",
  Tailscale = "tailscale",
  OAuth2 = "oauth2",
  Kafka = "kafka"
}

export const DynamicSecretIbmApiConnectSchema = z.object({
  clientId: z.string().trim().min(1, "Client ID is required"),
  clientSecret: z.string().trim().min(1, "Client Secret is required"),
  instanceUrl: z.string().url("Must be a valid URL").trim().min(1, "Instance URL is required"),
  apiKey: z.string().trim().min(1, "API Key is required"),
  orgId: z.string().trim().min(1, "Organization is required"),
  catalogId: z.string().trim().min(1, "Catalog is required"),
  consumerOrgId: z.string().trim().min(1, "Consumer Organization is required"),
  appId: z.string().trim().min(1, "Application is required"),
  gatewayId: z.string().nullable().optional(),
  gatewayPoolId: z.string().nullable().optional()
});

export enum TailscaleKeyAuthType {
  AuthKeys = "auth_keys",
  OAuthKeys = "oauth_keys",
  FederatedKeys = "federated_keys"
}

export enum TailscaleAuthMethod {
  ApiKey = "api_key",
  OAuth = "oauth"
}

// OAuth/federated credentials issued with these scopes could create or modify other credentials,
// which would let a lease escalate its own privileges. Block them at both the schema and provider level.
export const TAILSCALE_BLOCKED_SCOPES = new Set([
  "auth_keys",
  "oauth_keys",
  "federated_keys",
  "api_access_tokens",
  "all"
]);

const DynamicSecretTailscaleAuthSchema = z.discriminatedUnion("method", [
  z.object({
    method: z.literal(TailscaleAuthMethod.ApiKey),
    apiKey: z.string().trim().min(1).describe("Tailscale API access token used to create and revoke keys.")
  }),
  z.object({
    method: z.literal(TailscaleAuthMethod.OAuth),
    clientId: z.string().trim().min(1).describe("Tailscale OAuth client ID."),
    clientSecret: z.string().trim().min(1).describe("Tailscale OAuth client secret.")
  })
]);

export const DynamicSecretTailscaleSchema = z
  .discriminatedUnion("authType", [
    z.object({
      authType: z.literal(TailscaleKeyAuthType.AuthKeys),
      auth: DynamicSecretTailscaleAuthSchema,
      tailnet: z
        .string()
        .trim()
        .min(1)
        .default("-")
        .describe("Tailnet identifier. Use '-' for the token owner's default tailnet."),
      description: z.string().trim().max(50).optional().describe("Description applied to the created key."),
      tags: z
        .array(z.string().trim().min(1))
        .default([])
        .describe("ACL tags to attach to devices (e.g. tag:ci). Required when authenticating with an OAuth token."),
      reusable: z.boolean().default(false).describe("Whether the auth key can register multiple devices."),
      preauthorized: z.boolean().default(false).describe("Whether devices registered with the key are pre-authorized.")
    }),
    z.object({
      authType: z.literal(TailscaleKeyAuthType.OAuthKeys),
      auth: DynamicSecretTailscaleAuthSchema,
      tailnet: z
        .string()
        .trim()
        .min(1)
        .default("-")
        .describe("Tailnet identifier. Use '-' for the token owner's default tailnet."),
      description: z.string().trim().max(50).optional().describe("Description applied to the created OAuth client."),
      tags: z
        .array(z.string().trim().min(1))
        .default([])
        .describe("ACL tags to attach (e.g. tag:ci). Required if scopes include devices:core or auth_keys."),
      scopes: z.array(z.string().trim().min(1)).min(1).describe("OAuth scopes granted to the client.")
    }),
    z.object({
      authType: z.literal(TailscaleKeyAuthType.FederatedKeys),
      auth: DynamicSecretTailscaleAuthSchema,
      tailnet: z
        .string()
        .trim()
        .min(1)
        .default("-")
        .describe("Tailnet identifier. Use '-' for the token owner's default tailnet."),
      description: z
        .string()
        .trim()
        .max(50)
        .optional()
        .describe("Description applied to the created federated identity."),
      tags: z
        .array(z.string().trim().min(1))
        .default([])
        .describe("ACL tags to attach (e.g. tag:ci). Required if scopes include devices:core or auth_keys."),
      scopes: z
        .array(z.string().trim().min(1))
        .min(1)
        .describe("OAuth scopes granted to tokens issued via this federated identity."),
      issuer: z.string().trim().min(1).describe("HTTPS URL of the OIDC issuer trusted for token exchange."),
      subject: z.string().trim().min(1).describe("Pattern matched against the sub claim of the OIDC identity token."),
      audience: z
        .string()
        .trim()
        .optional()
        .describe("Audience for the OIDC token exchange. Tailscale auto-generates one if omitted.")
    })
  ])
  .superRefine((data, ctx) => {
    // Tailscale requires ACL tags on tailnet-owned auth keys, which are the ones created via OAuth.
    // Auth keys created with a user's API access token are user-owned and may omit tags.
    if (
      (data.authType === TailscaleKeyAuthType.OAuthKeys || data.authType === TailscaleKeyAuthType.FederatedKeys) &&
      data.tags.length === 0
    ) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        path: ["tags"],
        message: "Tags are required when the key type is OAuth or Federated Identity"
      });
    }

    // Block scopes that would let a lease escalate its own privileges (e.g. mint new auth/OAuth keys).
    if (
      (data.authType === TailscaleKeyAuthType.OAuthKeys || data.authType === TailscaleKeyAuthType.FederatedKeys) &&
      data.scopes.some((scope) => TAILSCALE_BLOCKED_SCOPES.has(scope))
    ) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        path: ["scopes"],
        message: "OAuth credentials cannot be used to create or modify other credentials"
      });
    }
  });

export enum OAuth2GrantType {
  ClientCredentials = "client-credentials"
}

export const OAUTH2_MAX_EXTRA_PARAMS = 20;

// Parameters Infisical sets itself or that carry client authentication; letting extra params
// override them would change the grant or leak credentials into the request body.
export const OAUTH2_RESERVED_PARAMS = new Set([
  "grant_type",
  "scope",
  "client_id",
  "client_secret",
  "client_assertion",
  "client_assertion_type"
]);

// RFC 6749 section 3.3: scope-token = 1*( %x21 / %x23-5B / %x5D-7E )
const OAUTH2_SCOPE_TOKEN_REGEX = new RE2(/^[\x21\x23-\x5B\x5D-\x7E]+$/);
const OAUTH2_PARAM_KEY_REGEX = new RE2(/^[A-Za-z0-9._~:[\]-]+$/);
const WHITESPACE_REGEX = new RE2(/\s+/);

export const normalizeOAuth2Scope = (scope: string) =>
  [...new Set(scope.split(WHITESPACE_REGEX).filter(Boolean))].join(" ");

// RFC 7591 registry names, so they match what servers advertise in token_endpoint_auth_methods_supported
export enum OAuth2ClientAuthMethod {
  ClientSecretBasic = "client_secret_basic",
  ClientSecretPost = "client_secret_post"
}

const OAuth2ClientSecretSchema = z
  .string()
  .trim()
  .min(1, "Client secret is required")
  .max(4096)
  .describe("The OAuth 2.0 client secret.");

export const OAuth2ClientAuthSchema = z.discriminatedUnion("method", [
  z.object({
    method: z
      .literal(OAuth2ClientAuthMethod.ClientSecretBasic)
      .describe("Send the client credentials in an HTTP Basic Authorization header."),
    clientSecret: OAuth2ClientSecretSchema
  }),
  z.object({
    method: z
      .literal(OAuth2ClientAuthMethod.ClientSecretPost)
      .describe("Send the client credentials as client_id and client_secret form parameters."),
    clientSecret: OAuth2ClientSecretSchema
  })
]);

export const DynamicSecretOAuth2Schema = z.discriminatedUnion("grantType", [
  z.object({
    grantType: z.literal(OAuth2GrantType.ClientCredentials),
    tokenUrl: z
      .string()
      .trim()
      .max(2048)
      .url("Token URL must be a valid URL")
      .describe("The authorization server's token endpoint."),
    revocationUrl: z
      .string()
      .trim()
      .max(2048)
      .url("Revocation URL must be a valid URL")
      .describe("The authorization server's RFC 7009 token revocation endpoint."),
    clientId: z.string().trim().min(1, "Client ID is required").max(1024).describe("The OAuth 2.0 client ID."),
    clientAuth: OAuth2ClientAuthSchema.describe(
      "How Infisical authenticates the client to the token and revocation endpoints."
    ),
    scope: z
      .string()
      .trim()
      .max(2048)
      .superRefine((scope, ctx) => {
        const invalidTokens = scope
          .split(WHITESPACE_REGEX)
          .filter((token) => token && !OAUTH2_SCOPE_TOKEN_REGEX.test(token));
        if (invalidTokens.length) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `Scope contains invalid characters: ${invalidTokens.map((token) => `'${token}'`).join(", ")}`
          });
        }
      })
      .transform((scope) => normalizeOAuth2Scope(scope) || undefined)
      .optional()
      .describe("Space-separated scopes to request. Leave empty to use the client's default scopes."),
    extraParams: z
      .array(
        z.object({
          key: z
            .string()
            .trim()
            .min(1, "Parameter name is required")
            .max(128)
            .refine((key) => OAUTH2_PARAM_KEY_REGEX.test(key), "Parameter name contains invalid characters")
            .refine(
              (key) => !OAUTH2_RESERVED_PARAMS.has(key.toLowerCase()),
              (key) => ({ message: `'${key}' is set by Infisical and can't be used as an extra parameter` })
            ),
          value: z.string().trim().min(1, "Parameter value is required").max(2048)
        })
      )
      .max(OAUTH2_MAX_EXTRA_PARAMS, `At most ${OAUTH2_MAX_EXTRA_PARAMS} extra parameters are allowed`)
      .superRefine((params, ctx) => {
        const seen = new Set<string>();
        params.forEach(({ key }, index) => {
          if (seen.has(key)) {
            ctx.addIssue({
              code: z.ZodIssueCode.custom,
              path: [index, "key"],
              message: `Extra parameter '${key}' is set more than once`
            });
          }
          seen.add(key);
        });
      })
      .default([])
      .describe("Additional form parameters sent with the token request, such as audience or resource.")
  })
]);

export enum KafkaSaslMechanism {
  Plain = "PLAIN",
  ScramSha256 = "SCRAM-SHA-256",
  ScramSha512 = "SCRAM-SHA-512"
}

// ACL values use Kafka's own names, which are also the keys of @platformatic/kafka's enum maps
export enum KafkaAclResourceType {
  Topic = "TOPIC",
  Group = "GROUP",
  Cluster = "CLUSTER",
  TransactionalId = "TRANSACTIONAL_ID"
}

export enum KafkaAclPatternType {
  Literal = "LITERAL",
  Prefixed = "PREFIXED"
}

export enum KafkaAclOperation {
  All = "ALL",
  Read = "READ",
  Write = "WRITE",
  Create = "CREATE",
  Delete = "DELETE",
  Alter = "ALTER",
  Describe = "DESCRIBE",
  DescribeConfigs = "DESCRIBE_CONFIGS",
  AlterConfigs = "ALTER_CONFIGS",
  IdempotentWrite = "IDEMPOTENT_WRITE"
}

export enum KafkaAclPermissionType {
  Allow = "ALLOW",
  Deny = "DENY"
}

const KAFKA_CLUSTER_RESOURCE_NAME = "kafka-cluster";

export const DynamicSecretKafkaSchema = z.object({
  bootstrapServers: z
    .array(z.object({ host: z.string().trim().min(1).max(255), port: z.number().int().min(1).max(65535) }))
    .min(1)
    .describe("Brokers Infisical tries in order until one connects"),
  saslMechanism: z
    .nativeEnum(KafkaSaslMechanism)
    .describe("SASL mechanism Infisical uses to authenticate as the admin user"),
  username: z.string().trim().min(1).max(255).describe("Admin username used to manage SCRAM users and ACLs"),
  password: z.string().min(1).max(1024).describe("Admin password used to manage SCRAM users and ACLs"),
  acls: z
    .array(
      z
        .object({
          resourceType: z.nativeEnum(KafkaAclResourceType),
          patternType: z.nativeEnum(KafkaAclPatternType).default(KafkaAclPatternType.Literal),
          resourceName: z.string().trim().min(1).max(255).describe('Resource name or prefix, or "*" for all'),
          operation: z.nativeEnum(KafkaAclOperation),
          permissionType: z.nativeEnum(KafkaAclPermissionType).default(KafkaAclPermissionType.Allow)
        })
        .refine(
          ({ resourceType, resourceName }) =>
            resourceType !== KafkaAclResourceType.Cluster || resourceName === KAFKA_CLUSTER_RESOURCE_NAME,
          {
            message: `Cluster ACLs must use the resource name '${KAFKA_CLUSTER_RESOURCE_NAME}'`,
            path: ["resourceName"]
          }
        )
    )
    .min(1)
    .describe("ACLs granted to each lease user"),
  sslEnabled: z.boolean().default(false),
  ca: z.string().max(10240).optional(),
  sslRejectUnauthorized: z.boolean().default(true)
});

export const DynamicSecretSshSchema = z.object({
  principals: z.array(z.string().trim().min(1)).min(1),
  keyAlgorithm: z.enum(SSH_CERT_KEY_ALGORITHMS).default(SshCertKeyAlgorithm.ED25519),
  caKeyAlgorithm: z.enum(SSH_CERT_KEY_ALGORITHMS).default(SshCertKeyAlgorithm.ED25519)
});

export const SshStoredSchema = z.object({
  caPrivateKey: z.string(),
  caPublicKey: z.string(),
  principals: z.array(z.string().trim().min(1)).min(1),
  keyAlgorithm: z.enum(SSH_CERT_KEY_ALGORITHMS),
  caKeyAlgorithm: z.enum(SSH_CERT_KEY_ALGORITHMS).default(SshCertKeyAlgorithm.ED25519)
});

export const DynamicSecretProviderSchema = z.discriminatedUnion("type", [
  z.object({ type: z.literal(DynamicSecretProviders.SqlDatabase), inputs: DynamicSecretSqlDBSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Clickhouse), inputs: DynamicSecretClickhouseSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Cassandra), inputs: DynamicSecretCassandraSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.SapAse), inputs: DynamicSecretSapAseSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.AwsIam), inputs: DynamicSecretAwsIamSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Redis), inputs: DynamicSecretRedisDBSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.SapHana), inputs: DynamicSecretSapHanaSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.AwsElastiCache), inputs: DynamicSecretAwsElastiCacheSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.AwsMemoryDb), inputs: DynamicSecretAwsMemoryDbSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.MongoAtlas), inputs: DynamicSecretMongoAtlasSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.ElasticSearch), inputs: DynamicSecretElasticSearchSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.MongoDB), inputs: DynamicSecretMongoDBSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.RabbitMq), inputs: DynamicSecretRabbitMqSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.AzureEntraID), inputs: AzureEntraIDSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.AzureSqlDatabase), inputs: DynamicSecretAzureSqlDBSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Ldap), inputs: LdapSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Snowflake), inputs: DynamicSecretSnowflakeSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Totp), inputs: DynamicSecretTotpSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Kubernetes), inputs: DynamicSecretKubernetesSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Vertica), inputs: DynamicSecretVerticaSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.GcpIam), inputs: DynamicSecretGcpIamSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Github), inputs: DynamicSecretGithubSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Couchbase), inputs: DynamicSecretCouchbaseSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Milvus), inputs: DynamicSecretMilvusSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.Ssh), inputs: DynamicSecretSshSchema }),
  z.object({
    type: z.literal(DynamicSecretProviders.IbmApiConnect),
    inputs: DynamicSecretIbmApiConnectSchema
  }),
  z.object({ type: z.literal(DynamicSecretProviders.Tailscale), inputs: DynamicSecretTailscaleSchema }),
  z.object({ type: z.literal(DynamicSecretProviders.OAuth2), inputs: DynamicSecretOAuth2Schema }),
  z.object({ type: z.literal(DynamicSecretProviders.Kafka), inputs: DynamicSecretKafkaSchema })
]);

// Extended metadata passed to a provider's create() call. When the project
// has a matching secret validation rule, `passwordValidation` carries the
// constraints that any generated password must satisfy; providers that
// generate passwords (e.g. sql-database, milvus) honor it in place of the
// user-configured passwordRequirements.
export type TDynamicProviderCreateMetadata = {
  projectId: string;
  passwordValidation?: {
    constraints: TConstraints;
    ruleNames: string[];
  };
};

export type TDynamicProviderValidateMetadata = {
  projectId: string;
  previousInputs?: object;
  defaultTTL?: string;
  maxTTL?: string | null;
  hasActiveLeases?: boolean;
};

export type TDynamicProviderFns = {
  // keys of create()'s `data` that revoke() needs; stored encrypted on the lease and passed back as metadata.leaseData
  persistedLeaseFields?: string[];
  create: (arg: {
    inputs: unknown;
    expireAt: number;
    usernameTemplate?: string | null;
    identity: ActorIdentityAttributes;
    dynamicSecret: TDynamicSecrets;
    metadata: TDynamicProviderCreateMetadata;
    config?: TDynamicSecretLeaseConfig;
  }) => Promise<{ entityId: string; data: unknown }>;
  validateConnection: (
    inputs: unknown,
    metadata: { projectId: string; defaultTTL?: string; maxTTL?: string | null }
  ) => Promise<boolean>;
  validateProviderInputs: (inputs: object, metadata: TDynamicProviderValidateMetadata) => Promise<unknown>;
  revoke: (
    inputs: unknown,
    entityId: string,
    metadata: { projectId: string; leaseData?: Record<string, string> },
    config?: TDynamicSecretLeaseConfig
  ) => Promise<{ entityId: string }>;
  renew: (
    inputs: unknown,
    entityId: string,
    expireAt: number,
    metadata: { projectId: string }
  ) => Promise<{ entityId: string }>;
};