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
Make sql secret rotation use gateway
x032205fce67385626c
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.tsindex cd88f889e7..b1cc8ad236 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts@@ -107,6 +107,7 @@ export type TSecretRotationV2ServiceFactoryDep = {queueService: Pick<TQueueServiceFactory, "queuePg">;appConnectionDAL: Pick<TAppConnectionDALFactory, "findById" | "update" | "updateById">;folderCommitService: Pick<TFolderCommitServiceFactory, "createCommit">;+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;};export type TSecretRotationV2ServiceFactory = ReturnType<typeof secretRotationV2ServiceFactory>;@@ -148,7 +149,8 @@ export const secretRotationV2ServiceFactory = ({keyStore,queueService,folderCommitService,- appConnectionDAL+ appConnectionDAL,+ gatewayService}: TSecretRotationV2ServiceFactoryDep) => {const $queueSendSecretRotationStatusNotification = async (secretRotation: TSecretRotationV2Raw) => {const appCfg = getConfig();@@ -461,7 +463,8 @@ export const secretRotationV2ServiceFactory = ({rotationInterval: payload.rotationInterval} as TSecretRotationV2WithConnection,appConnectionDAL,- kmsService+ kmsService,+ gatewayService);// even though we have a db constraint we want to check before any rotation of credentials is attempted@@ -824,7 +827,8 @@ export const secretRotationV2ServiceFactory = ({connection: appConnection} as TSecretRotationV2WithConnection,appConnectionDAL,- kmsService+ kmsService,+ gatewayService);const generatedCredentials = await decryptSecretRotationCredentials({@@ -907,7 +911,8 @@ export const secretRotationV2ServiceFactory = ({connection: appConnection} as TSecretRotationV2WithConnection,appConnectionDAL,- kmsService+ kmsService,+ gatewayService);const updatedRotation = await rotationFactory.rotateCredentials(diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.tsindex 5547d45828..b90d0ed882 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts@@ -1,4 +1,5 @@import { AuditLogInfo } from "@app/ee/services/audit-log/audit-log-types";+import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";import { TSqlCredentialsRotationGeneratedCredentials } from "@app/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-types";import { OrderByDirection } from "@app/lib/types";import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";@@ -239,7 +240,8 @@ export type TRotationFactory<> = (secretRotation: T,appConnectionDAL: Pick<TAppConnectionDALFactory, "findById" | "update" | "updateById">,- kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">,+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">) => {issueCredentials: TRotationFactoryIssueCredentials<C, P>;revokeCredentials: TRotationFactoryRevokeCredentials<C>;diff --git a/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.tsindex 12e9b5964b..3e6e5d265b 100644--- a/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts+++ b/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts@@ -1,3 +1,5 @@+import { Knex } from "knex";+import {TRotationFactory,TRotationFactoryGetSecretsPayload,@@ -5,7 +7,10 @@ import {TRotationFactoryRevokeCredentials,TRotationFactoryRotateCredentials} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-types";-import { getSqlConnectionClient, SQL_CONNECTION_ALTER_LOGIN_STATEMENT } from "@app/services/app-connection/shared/sql";+import {+ executeWithPotentialGateway,+ SQL_CONNECTION_ALTER_LOGIN_STATEMENT+} from "@app/services/app-connection/shared/sql";import { generatePassword } from "../utils";import {@@ -30,7 +35,7 @@ const redactPasswords = (e: unknown, credentials: TSqlCredentialsRotationGeneratexport const sqlCredentialsRotationFactory: TRotationFactory<TSqlCredentialsRotationWithConnection,TSqlCredentialsRotationGeneratedCredentials-> = (secretRotation) => {+> = (secretRotation, _appConnectionDAL, _kmsService, gatewayService) => {const {connection,parameters: { username1, username2 },@@ -38,29 +43,38 @@ export const sqlCredentialsRotationFactory: TRotationFactory<secretsMapping} = secretRotation;- const $validateCredentials = async (credentials: TSqlCredentialsRotationGeneratedCredentials[number]) => {- const client = await getSqlConnectionClient({- ...connection,- credentials: {- ...connection.credentials,- ...credentials- }- });+ const executeOperation = <T>(+ operation: (client: Knex) => Promise<T>,+ credentialsOverride?: TSqlCredentialsRotationGeneratedCredentials[number]+ ) => {+ const finalCredentials = {+ ...connection.credentials,+ ...credentialsOverride+ };++ return executeWithPotentialGateway(+ {+ ...connection,+ credentials: finalCredentials+ },+ gatewayService,+ (client) => operation(client)+ );+ };+ const $validateCredentials = async (credentials: TSqlCredentialsRotationGeneratedCredentials[number]) => {try {- await client.raw("SELECT 1");+ await executeOperation(async (client) => {+ await client.raw("SELECT 1");+ }, credentials);} catch (error) {throw new Error(redactPasswords(error, [credentials]));- } finally {- await client.destroy();}};const issueCredentials: TRotationFactoryIssueCredentials<TSqlCredentialsRotationGeneratedCredentials> = async (callback) => {- const client = await getSqlConnectionClient(connection);-// For SQL, since we get existing users, we change both their passwords// on issue to invalidate their existing passwordsconst credentialsSet = [@@ -69,15 +83,15 @@ export const sqlCredentialsRotationFactory: TRotationFactory<];try {- await client.transaction(async (tx) => {- for await (const credentials of credentialsSet) {- await tx.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));- }+ await executeOperation(async (client) => {+ await client.transaction(async (tx) => {+ for await (const credentials of credentialsSet) {+ await tx.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));+ }+ });});} catch (error) {throw new Error(redactPasswords(error, credentialsSet));- } finally {- await client.destroy();}for await (const credentials of credentialsSet) {@@ -91,21 +105,19 @@ export const sqlCredentialsRotationFactory: TRotationFactory<credentialsToRevoke,callback) => {- const client = await getSqlConnectionClient(connection);-const revokedCredentials = credentialsToRevoke.map(({ username }) => ({ username, password: generatePassword() }));try {- await client.transaction(async (tx) => {- for await (const credentials of revokedCredentials) {- // invalidate previous passwords- await tx.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));- }+ await executeOperation(async (client) => {+ await client.transaction(async (tx) => {+ for await (const credentials of revokedCredentials) {+ // invalidate previous passwords+ await tx.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));+ }+ });});} catch (error) {throw new Error(redactPasswords(error, revokedCredentials));- } finally {- await client.destroy();}return callback();@@ -115,17 +127,15 @@ export const sqlCredentialsRotationFactory: TRotationFactory<_,callback) => {- const client = await getSqlConnectionClient(connection);-// generate new password for the next active userconst credentials = { username: activeIndex === 0 ? username2 : username1, password: generatePassword() };try {- await client.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));+ await executeOperation(async (client) => {+ await client.raw(...SQL_CONNECTION_ALTER_LOGIN_STATEMENT[connection.app](credentials));+ });} catch (error) {throw new Error(redactPasswords(error, [credentials]));- } finally {- await client.destroy();}await $validateCredentials(credentials);diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex 9fc7965541..fbfcd21773 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -1805,7 +1805,8 @@ export const registerRoutes = async (snapshotService,secretQueueService,queueService,- appConnectionDAL+ appConnectionDAL,+ gatewayService});const certificateAuthorityService = certificateAuthorityServiceFactory({diff --git a/backend/src/services/app-connection/app-connection-fns.ts b/backend/src/services/app-connection/app-connection-fns.tsindex 9dadcc4e5e..c2290af119 100644--- a/backend/src/services/app-connection/app-connection-fns.ts+++ b/backend/src/services/app-connection/app-connection-fns.ts@@ -5,6 +5,7 @@ import {validateOCIConnectionCredentials} from "@app/ee/services/app-connections/oci";import { getOracleDBConnectionListItem, OracleDBConnectionMethod } from "@app/ee/services/app-connections/oracledb";+import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";import { crypto } from "@app/lib/crypto/cryptography";import { BadRequestError } from "@app/lib/errors";@@ -193,7 +194,8 @@ export const decryptAppConnectionCredentials = async ({};export const validateAppConnectionCredentials = async (- appConnection: TAppConnectionConfig+ appConnection: TAppConnectionConfig,+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">): Promise<TAppConnection["credentials"]> => {const VALIDATE_APP_CONNECTION_CREDENTIALS_MAP: Record<AppConnection, TAppConnectionCredentialsValidator> = {[AppConnection.AWS]: validateAwsConnectionCredentials as TAppConnectionCredentialsValidator,@@ -232,7 +234,7 @@ export const validateAppConnectionCredentials = async ([AppConnection.Bitbucket]: validateBitbucketConnectionCredentials as TAppConnectionCredentialsValidator};- return VALIDATE_APP_CONNECTION_CREDENTIALS_MAP[appConnection.app](appConnection);+ return VALIDATE_APP_CONNECTION_CREDENTIALS_MAP[appConnection.app](appConnection, gatewayService);};export const getAppConnectionMethodName = (method: TAppConnection["method"]) => {diff --git a/backend/src/services/app-connection/app-connection-service.ts b/backend/src/services/app-connection/app-connection-service.tsindex faa813557e..b4ffb27a02 100644--- a/backend/src/services/app-connection/app-connection-service.ts+++ b/backend/src/services/app-connection/app-connection-service.ts@@ -242,12 +242,15 @@ export const appConnectionServiceFactory = ({"Failed to create app connection due to plan restriction. Upgrade plan to access enterprise app connections.");- const validatedCredentials = await validateAppConnectionCredentials({- app,- credentials,- method,- orgId: actor.orgId- } as TAppConnectionConfig);+ const validatedCredentials = await validateAppConnectionCredentials(+ {+ app,+ credentials,+ method,+ orgId: actor.orgId+ } as TAppConnectionConfig,+ gatewayService+ );try {const createConnection = async (connectionCredentials: TAppConnection["credentials"]) => {@@ -349,12 +352,15 @@ export const appConnectionServiceFactory = ({} Connection with method ${getAppConnectionMethodName(method)}`});- updatedCredentials = await validateAppConnectionCredentials({- app,- orgId: actor.orgId,- credentials,- method- } as TAppConnectionConfig);+ updatedCredentials = await validateAppConnectionCredentials(+ {+ app,+ orgId: actor.orgId,+ credentials,+ method+ } as TAppConnectionConfig,+ gatewayService+ );if (!updatedCredentials)throw new BadRequestError({ message: "Unable to validate connection - check credentials" });diff --git a/backend/src/services/app-connection/app-connection-types.ts b/backend/src/services/app-connection/app-connection-types.tsindex ffc8145291..211944f19c 100644--- a/backend/src/services/app-connection/app-connection-types.ts+++ b/backend/src/services/app-connection/app-connection-types.ts@@ -350,7 +350,8 @@ export type TListAwsConnectionIamUsers = {};export type TAppConnectionCredentialsValidator = (- appConnection: TAppConnectionConfig+ appConnection: TAppConnectionConfig,+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">) => Promise<TAppConnection["credentials"]>;export type TAppConnectionTransitionCredentialsToPlatform = (diff --git a/backend/src/services/app-connection/shared/sql/sql-connection-fns.ts b/backend/src/services/app-connection/shared/sql/sql-connection-fns.tsindex 86953edced..49204e9e43 100644--- a/backend/src/services/app-connection/shared/sql/sql-connection-fns.ts+++ b/backend/src/services/app-connection/shared/sql/sql-connection-fns.ts@@ -100,7 +100,7 @@ export const getSqlConnectionClient = async (appConnection: Pick<TSqlConnection,return client;};-const executeWithPotentialGateway = async <T>(+export const executeWithPotentialGateway = async <T>(config: TSqlConnectionConfig,gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,operation: (client: Knex) => Promise<T>@@ -159,7 +159,7 @@ const executeWithPotentialGateway = async <T>(};export const validateSqlConnectionCredentials = async (- config: TSqlConnectionConfig & { gatewayId?: string },+ config: TSqlConnectionConfig,gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">) => {try {