Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Add redis resources
Fang-Pen Line7a3d2522822
diff --git a/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts b/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.tsindex 732d1dcf16..38f22a00a0 100644--- a/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts+++ b/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts@@ -9,6 +9,7 @@ import { SanitizedMySQLAccountWithResourceSchema } from "@app/ee/services/pam-reimport { PamResource } from "@app/ee/services/pam-resource/pam-resource-enums";import { GatewayAccessResponseSchema } from "@app/ee/services/pam-resource/pam-resource-schemas";import { SanitizedPostgresAccountWithResourceSchema } from "@app/ee/services/pam-resource/postgres/postgres-resource-schemas";+import { SanitizedRedisAccountWithResourceSchema } from "@app/ee/services/pam-resource/redis/redis-resource-schemas";import { SanitizedSSHAccountWithResourceSchema } from "@app/ee/services/pam-resource/ssh/ssh-resource-schemas";import { BadRequestError } from "@app/lib/errors";import { removeTrailingSlash } from "@app/lib/fn";@@ -22,6 +23,7 @@ const SanitizedAccountSchema = z.union([SanitizedSSHAccountWithResourceSchema, // ORDER MATTERSSanitizedPostgresAccountWithResourceSchema,SanitizedMySQLAccountWithResourceSchema,+ SanitizedRedisAccountWithResourceSchema,SanitizedKubernetesAccountWithResourceSchema,SanitizedAwsIamAccountWithResourceSchema]);@@ -134,9 +136,10 @@ export const registerPamAccountRouter = async (server: FastifyZodProvider) => {}),response: {200: z.discriminatedUnion("resourceType", [- // Gateway-based resources (Postgres, MySQL, SSH)+ // Gateway-based resources (Postgres, MySQL, Redis, SSH)GatewayAccessResponseSchema.extend({ resourceType: z.literal(PamResource.Postgres) }),GatewayAccessResponseSchema.extend({ resourceType: z.literal(PamResource.MySQL) }),+ GatewayAccessResponseSchema.extend({ resourceType: z.literal(PamResource.Redis) }),GatewayAccessResponseSchema.extend({ resourceType: z.literal(PamResource.SSH) }),GatewayAccessResponseSchema.extend({ resourceType: z.literal(PamResource.Kubernetes) }),// AWS IAM (no gateway, returns console URL)diff --git a/backend/src/ee/routes/v1/pam-resource-routers/index.ts b/backend/src/ee/routes/v1/pam-resource-routers/index.tsindex ad40cdbc2f..a5b041fb39 100644--- a/backend/src/ee/routes/v1/pam-resource-routers/index.ts+++ b/backend/src/ee/routes/v1/pam-resource-routers/index.ts@@ -19,6 +19,11 @@ import {SanitizedPostgresResourceSchema,UpdatePostgresResourceSchema} from "@app/ee/services/pam-resource/postgres/postgres-resource-schemas";+import {+ CreateRedisResourceSchema,+ SanitizedRedisResourceSchema,+ UpdateRedisResourceSchema+} from "@app/ee/services/pam-resource/redis/redis-resource-schemas";import {CreateSSHResourceSchema,SanitizedSSHResourceSchema,@@ -78,5 +83,14 @@ export const PAM_RESOURCE_REGISTER_ROUTER_MAP: Record<PamResource, (server: FastcreateResourceSchema: CreateAwsIamResourceSchema,updateResourceSchema: UpdateAwsIamResourceSchema});+ },+ [PamResource.Redis]: async (server: FastifyZodProvider) => {+ registerPamResourceEndpoints({+ server,+ resourceType: PamResource.Redis,+ resourceResponseSchema: SanitizedRedisResourceSchema,+ createResourceSchema: CreateRedisResourceSchema,+ updateResourceSchema: UpdateRedisResourceSchema+ });}};diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.tsindex 7ddb83ccb0..002e567fbe 100644--- a/backend/src/ee/services/pam-account/pam-account-service.ts+++ b/backend/src/ee/services/pam-account/pam-account-service.ts@@ -58,6 +58,7 @@ import { getFullPamFolderPath } from "../pam-folder/pam-folder-fns";import { TPamResourceDALFactory } from "../pam-resource/pam-resource-dal";import { PamResource } from "../pam-resource/pam-resource-enums";import { TPamAccountCredentials } from "../pam-resource/pam-resource-types";+import { TRedisAccountCredentials, TRedisResourceConnectionDetails } from "../pam-resource/redis/redis-resource-types";import { TSqlAccountCredentials, TSqlResourceConnectionDetails } from "../pam-resource/shared/sql/sql-resource-types";import { TSSHAccountCredentials, TSSHResourceMetadata } from "../pam-resource/ssh/ssh-resource-types";import { TPamSessionDALFactory } from "../pam-session/pam-session-dal";@@ -883,6 +884,29 @@ export const pamAccountServiceFactory = ({};}break;+ case PamResource.Redis:+ {+ const connectionCredentials = (await decryptResourceConnectionDetails({+ encryptedConnectionDetails: resource.encryptedConnectionDetails,+ kmsService,+ projectId+ })) as TRedisResourceConnectionDetails;++ const credentials = (await decryptAccountCredentials({+ encryptedCredentials: account.encryptedCredentials,+ kmsService,+ projectId+ })) as TRedisAccountCredentials;++ metadata = {+ username: credentials.username,+ host: connectionCredentials.host,+ port: connectionCredentials.port,+ accountName: account.name,+ accountPath: folderPath+ };+ }+ break;case PamResource.SSH:{const credentials = (await decryptAccountCredentials({diff --git a/backend/src/ee/services/pam-resource/pam-resource-enums.ts b/backend/src/ee/services/pam-resource/pam-resource-enums.tsindex c8c57b03b2..07222342c8 100644--- a/backend/src/ee/services/pam-resource/pam-resource-enums.ts+++ b/backend/src/ee/services/pam-resource/pam-resource-enums.ts@@ -3,7 +3,8 @@ export enum PamResource {MySQL = "mysql",SSH = "ssh",Kubernetes = "kubernetes",- AwsIam = "aws-iam"+ AwsIam = "aws-iam",+ Redis = "redis"}export enum PamResourceOrderBy {diff --git a/backend/src/ee/services/pam-resource/pam-resource-factory.ts b/backend/src/ee/services/pam-resource/pam-resource-factory.tsindex bf8d13d664..895f7702fc 100644--- a/backend/src/ee/services/pam-resource/pam-resource-factory.ts+++ b/backend/src/ee/services/pam-resource/pam-resource-factory.ts@@ -2,6 +2,7 @@ import { awsIamResourceFactory } from "./aws-iam/aws-iam-resource-factory";import { kubernetesResourceFactory } from "./kubernetes/kubernetes-resource-factory";import { PamResource } from "./pam-resource-enums";import { TPamAccountCredentials, TPamResourceConnectionDetails, TPamResourceFactory } from "./pam-resource-types";+import { redisResourceFactory } from "./redis/redis-resource-factory";import { sqlResourceFactory } from "./shared/sql/sql-resource-factory";import { sshResourceFactory } from "./ssh/ssh-resource-factory";@@ -12,5 +13,6 @@ export const PAM_RESOURCE_FACTORY_MAP: Record<PamResource, TPamResourceFactoryIm[PamResource.MySQL]: sqlResourceFactory as TPamResourceFactoryImplementation,[PamResource.SSH]: sshResourceFactory as TPamResourceFactoryImplementation,[PamResource.Kubernetes]: kubernetesResourceFactory as TPamResourceFactoryImplementation,- [PamResource.AwsIam]: awsIamResourceFactory as TPamResourceFactoryImplementation+ [PamResource.AwsIam]: awsIamResourceFactory as TPamResourceFactoryImplementation,+ [PamResource.Redis]: redisResourceFactory as TPamResourceFactoryImplementation};diff --git a/backend/src/ee/services/pam-resource/pam-resource-types.ts b/backend/src/ee/services/pam-resource/pam-resource-types.tsindex 0f8cd1bb0a..5cfa91d592 100644--- a/backend/src/ee/services/pam-resource/pam-resource-types.ts+++ b/backend/src/ee/services/pam-resource/pam-resource-types.ts@@ -26,6 +26,12 @@ import {TPostgresResource,TPostgresResourceConnectionDetails} from "./postgres/postgres-resource-types";+import {+ TRedisAccount,+ TRedisAccountCredentials,+ TRedisResource,+ TRedisResourceConnectionDetails+} from "./redis/redis-resource-types";import {TSSHAccount,TSSHAccountCredentials,@@ -35,17 +41,30 @@ import {} from "./ssh/ssh-resource-types";// Resource types-export type TPamResource = TPostgresResource | TMySQLResource | TSSHResource | TAwsIamResource | TKubernetesResource;+export type TPamResource =+ | TPostgresResource+ | TMySQLResource+ | TSSHResource+ | TAwsIamResource+ | TKubernetesResource+ | TRedisResource;export type TPamResourceConnectionDetails =| TPostgresResourceConnectionDetails| TMySQLResourceConnectionDetails| TSSHResourceConnectionDetails| TKubernetesResourceConnectionDetails- | TAwsIamResourceConnectionDetails;+ | TAwsIamResourceConnectionDetails+ | TRedisResourceConnectionDetails;export type TPamResourceMetadata = TSSHResourceMetadata;// Account types-export type TPamAccount = TPostgresAccount | TMySQLAccount | TSSHAccount | TAwsIamAccount | TKubernetesAccount;+export type TPamAccount =+ | TPostgresAccount+ | TMySQLAccount+ | TSSHAccount+ | TAwsIamAccount+ | TKubernetesAccount+ | TRedisAccount;export type TPamAccountCredentials =| TPostgresAccountCredentials@@ -53,7 +72,8 @@ export type TPamAccountCredentials =| TMySQLAccountCredentials| TSSHAccountCredentials| TKubernetesAccountCredentials- | TAwsIamAccountCredentials;+ | TAwsIamAccountCredentials+ | TRedisAccountCredentials;// Resource DTOsexport type TCreateResourceDTO = Pick<TPamResource, "name" | "connectionDetails" | "resourceType" | "projectId"> & {diff --git a/backend/src/ee/services/pam-resource/redis/redis-resource-factory.ts b/backend/src/ee/services/pam-resource/redis/redis-resource-factory.tsnew file mode 100644index 0000000000..7c1ef772eb--- /dev/null+++ b/backend/src/ee/services/pam-resource/redis/redis-resource-factory.ts@@ -0,0 +1,263 @@+import { Redis } from "ioredis";++import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";+import { BadRequestError } from "@app/lib/errors";+import { GatewayProxyProtocol } from "@app/lib/gateway";+import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";++import {+ TPamResourceFactory,+ TPamResourceFactoryRotateAccountCredentials,+ TPamResourceFactoryValidateAccountCredentials+} from "../pam-resource-types";+import { TRedisAccountCredentials, TRedisResourceConnectionDetails } from "./redis-resource-types";++const EXTERNAL_REQUEST_TIMEOUT = 10 * 1000;++const TEST_CONNECTION_USERNAME = "infisical-gateway-connection-test";+const TEST_CONNECTION_PASSWORD = "infisical-gateway-connection-test-password";++export interface RedisResourceConnection {+ /**+ * Check and see if the connection is good or not.+ *+ * @param connectOnly when true, if we only want to know that making the connection is possible or not,+ * we don't care about authentication failures+ * @returns Promise to be resolved when the connection is good, otherwise an error will be errbacked+ */+ validate: (connectOnly: boolean) => Promise<void>;++ /**+ * Close the connection.+ *+ * @returns Promise for closing the connection+ */+ close: () => Promise<void>;+}++const makeRedisConnection = (+ proxyPort: number,+ config: {+ connectionDetails: TRedisResourceConnectionDetails;+ username?: string;+ password?: string;+ }+): RedisResourceConnection => {+ const { connectionDetails, username, password } = config;+ const { sslEnabled, sslRejectUnauthorized, sslCertificate } = connectionDetails;+ const actualUsername = username ?? connectionDetails.username ?? TEST_CONNECTION_USERNAME;+ const actualPassword = password ?? connectionDetails.password ?? TEST_CONNECTION_PASSWORD;++ let client: Redis | null = null;++ const createClient = () => {+ return new Redis({+ host: "localhost",+ port: proxyPort,+ username: actualUsername,+ password: actualPassword,+ connectTimeout: EXTERNAL_REQUEST_TIMEOUT,+ commandTimeout: EXTERNAL_REQUEST_TIMEOUT,+ ...(sslEnabled && {+ tls: {+ rejectUnauthorized: sslRejectUnauthorized,+ ca: sslCertificate+ }+ })+ });+ };++ return {+ validate: async (connectOnly) => {+ try {+ client = createClient();++ // Test authentication+ let result: string;+ if (actualPassword) {+ result = await client.auth(actualUsername, actualPassword, () => {});+ } else {+ result = await client.auth(actualUsername, () => {});+ }++ if (result !== "OK") {+ throw new BadRequestError({ message: `Redis authentication failed: ${result}` });+ }++ // Test connection with a simple command+ await client.ping();+ } catch (error) {+ if (connectOnly) {+ // If we're only checking connection, authentication failures are acceptable+ if (+ error instanceof Error &&+ (error.message.includes("NOAUTH") ||+ error.message.includes("WRONGPASS") ||+ error.message.includes("invalid password"))+ ) {+ return;+ }+ }+ throw new BadRequestError({+ message: `Unable to validate Redis connection: ${(error as Error).message || String(error)}`+ });+ } finally {+ if (client) {+ await client.quit();+ client = null;+ }+ }+ },+ close: async () => {+ if (client) {+ await client.quit();+ client = null;+ }+ }+ };+};++export const executeWithGateway = async <T>(+ config: {+ connectionDetails: TRedisResourceConnectionDetails;+ gatewayId: string;+ username?: string;+ password?: string;+ },+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,+ operation: (connection: RedisResourceConnection) => Promise<T>+): Promise<T> => {+ const { connectionDetails, gatewayId } = config;+ const [targetHost] = await verifyHostInputValidity(connectionDetails.host, true);+ const platformConnectionDetails = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({+ gatewayId,+ targetHost,+ targetPort: connectionDetails.port+ });++ if (!platformConnectionDetails) {+ throw new BadRequestError({ message: "Unable to connect to gateway, no platform connection details found" });+ }++ return withGatewayV2Proxy(+ async (proxyPort) => {+ const connection = makeRedisConnection(proxyPort, config);+ try {+ return await operation(connection);+ } finally {+ await connection.close();+ }+ },+ {+ protocol: GatewayProxyProtocol.Tcp,+ relayHost: platformConnectionDetails.relayHost,+ gateway: platformConnectionDetails.gateway,+ relay: platformConnectionDetails.relay+ }+ );+};++export const redisResourceFactory: TPamResourceFactory<TRedisResourceConnectionDetails, TRedisAccountCredentials> = (+ resourceType,+ connectionDetails,+ gatewayId,+ gatewayV2Service+) => {+ const validateConnection = async () => {+ if (!gatewayId) {+ throw new BadRequestError({ message: "Gateway ID is required" });+ }++ try {+ await executeWithGateway({ connectionDetails, gatewayId }, gatewayV2Service, async (client) => {+ await client.validate(true);+ });+ return connectionDetails;+ } catch (error) {+ if (error instanceof BadRequestError && error.message === "Connection terminated unexpectedly") {+ throw new BadRequestError({+ message: "Connection terminated unexpectedly. Verify that host and port are correct"+ });+ }++ throw new BadRequestError({+ message: `Unable to validate connection to ${resourceType}: ${(error as Error).message || String(error)}`+ });+ }+ };++ const validateAccountCredentials: TPamResourceFactoryValidateAccountCredentials<TRedisAccountCredentials> = async (+ credentials+ ) => {+ try {+ if (!gatewayId) {+ throw new BadRequestError({ message: "Gateway ID is required" });+ }++ await executeWithGateway(+ {+ connectionDetails,+ gatewayId,+ username: credentials.username,+ password: credentials.password+ },+ gatewayV2Service,+ async (client) => {+ await client.validate(false);+ }+ );+ return credentials;+ } catch (error) {+ if (error instanceof BadRequestError) {+ if (+ error.message.includes("NOAUTH") ||+ error.message.includes("WRONGPASS") ||+ error.message.includes("invalid password")+ ) {+ throw new BadRequestError({+ message: "Account credentials invalid: Username or password incorrect"+ });+ }++ if (error.message === "Connection terminated unexpectedly") {+ throw new BadRequestError({+ message: "Connection terminated unexpectedly. Verify that host and port are correct"+ });+ }+ }++ throw new BadRequestError({+ message: `Unable to validate account credentials for ${resourceType}: ${(error as Error).message || String(error)}`+ });+ }+ };++ const rotateAccountCredentials: TPamResourceFactoryRotateAccountCredentials<TRedisAccountCredentials> = async () => {+ // TODO: Redis password rotation is not supported yet+ throw new BadRequestError({+ message: "Unsupported operation"+ });+ };++ const handleOverwritePreventionForCensoredValues = async (+ updatedAccountCredentials: TRedisAccountCredentials,+ currentCredentials: TRedisAccountCredentials+ ) => {+ if (updatedAccountCredentials.password === "__INFISICAL_UNCHANGED__") {+ return {+ ...updatedAccountCredentials,+ password: currentCredentials.password+ };+ }++ return updatedAccountCredentials;+ };++ return {+ validateConnection,+ validateAccountCredentials,+ rotateAccountCredentials,+ handleOverwritePreventionForCensoredValues+ };+};diff --git a/backend/src/ee/services/pam-resource/redis/redis-resource-schemas.ts b/backend/src/ee/services/pam-resource/redis/redis-resource-schemas.tsnew file mode 100644index 0000000000..6ad4cd6eaa--- /dev/null+++ b/backend/src/ee/services/pam-resource/redis/redis-resource-schemas.ts@@ -0,0 +1,85 @@+import { z } from "zod";++import { PamResource } from "../pam-resource-enums";+import {+ BaseCreateGatewayPamResourceSchema,+ BaseCreatePamAccountSchema,+ BasePamAccountSchema,+ BasePamAccountSchemaWithResource,+ BasePamResourceSchema,+ BaseUpdateGatewayPamResourceSchema,+ BaseUpdatePamAccountSchema+} from "../pam-resource-schemas";++// Resources+export const RedisResourceConnectionDetailsSchema = z.object({+ host: z.string().trim().min(1).max(255),+ port: z.coerce.number(),+ username: z.string().trim().min(1).max(63).optional(),+ password: z.string().trim().max(256).optional(),+ sslEnabled: z.boolean(),+ sslRejectUnauthorized: z.boolean(),+ sslCertificate: z+ .string()+ .trim()+ .transform((value) => value || undefined)+ .optional()+});++export const RedisAccountCredentialsSchema = z.object({+ username: z.string().trim().min(1).max(63),+ password: z.string().trim().min(1).max(256)+});++const BaseRedisResourceSchema = BasePamResourceSchema.extend({ resourceType: z.literal(PamResource.Redis) });++export const RedisResourceSchema = BaseRedisResourceSchema.extend({+ connectionDetails: RedisResourceConnectionDetailsSchema,+ rotationAccountCredentials: RedisAccountCredentialsSchema.nullable().optional()+});++export const SanitizedRedisResourceSchema = BaseRedisResourceSchema.extend({+ connectionDetails: RedisResourceConnectionDetailsSchema,+ rotationAccountCredentials: RedisAccountCredentialsSchema.pick({+ username: true+ })+ .nullable()+ .optional()+});++export const RedisResourceListItemSchema = z.object({+ name: z.literal("Redis"),+ resource: z.literal(PamResource.Redis)+});++export const CreateRedisResourceSchema = BaseCreateGatewayPamResourceSchema.extend({+ connectionDetails: RedisResourceConnectionDetailsSchema,+ rotationAccountCredentials: RedisAccountCredentialsSchema.nullable().optional()+});++export const UpdateRedisResourceSchema = BaseUpdateGatewayPamResourceSchema.extend({+ connectionDetails: RedisResourceConnectionDetailsSchema.optional(),+ rotationAccountCredentials: RedisAccountCredentialsSchema.nullable().optional()+});++// Accounts+export const RedisAccountSchema = BasePamAccountSchema.extend({+ credentials: RedisAccountCredentialsSchema+});++export const CreateRedisAccountSchema = BaseCreatePamAccountSchema.extend({+ credentials: RedisAccountCredentialsSchema+});++export const UpdateRedisAccountSchema = BaseUpdatePamAccountSchema.extend({+ credentials: RedisAccountCredentialsSchema.optional()+});++export const SanitizedRedisAccountWithResourceSchema = BasePamAccountSchemaWithResource.extend({+ credentials: RedisAccountCredentialsSchema.pick({+ username: true+ })+});++// Sessions+export const RedisSessionCredentialsSchema = RedisResourceConnectionDetailsSchema.and(RedisAccountCredentialsSchema);diff --git a/backend/src/ee/services/pam-resource/redis/redis-resource-types.ts b/backend/src/ee/services/pam-resource/redis/redis-resource-types.tsnew file mode 100644index 0000000000..c199b03c27--- /dev/null+++ b/backend/src/ee/services/pam-resource/redis/redis-resource-types.ts@@ -0,0 +1,16 @@+import { z } from "zod";++import {+ RedisAccountCredentialsSchema,+ RedisAccountSchema,+ RedisResourceConnectionDetailsSchema,+ RedisResourceSchema+} from "./redis-resource-schemas";++// Resources+export type TRedisResource = z.infer<typeof RedisResourceSchema>;+export type TRedisResourceConnectionDetails = z.infer<typeof RedisResourceConnectionDetailsSchema>;++// Accounts+export type TRedisAccount = z.infer<typeof RedisAccountSchema>;+export type TRedisAccountCredentials = z.infer<typeof RedisAccountCredentialsSchema>;