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
checkpoint
Daniel Hougaardd347dc217a7f
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.tsindex 405e601591..cd6097703b 100644--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts@@ -53,6 +53,7 @@ const baseSecretScanningDataSourceQuery = ({db.ref("projectId").withSchema(TableName.AppConnection).as("connectionProjectId"),db.ref("createdAt").withSchema(TableName.AppConnection).as("connectionCreatedAt"),db.ref("updatedAt").withSchema(TableName.AppConnection).as("connectionUpdatedAt"),+ db.ref("isAutoRotationEnabled").withSchema(TableName.AppConnection).as("connectionIsAutoRotationEnabled"),db.ref("isPlatformManagedCredentials").withSchema(TableName.AppConnection)@@ -77,6 +78,7 @@ const expandSecretScanningDataSource = <connectionName,connectionId,connectionOrgId,+ connectionIsAutoRotationEnabled,connectionEncryptedCredentials,connectionMethod,connectionDescription,@@ -104,6 +106,7 @@ const expandSecretScanningDataSource = <createdAt: connectionCreatedAt,updatedAt: connectionUpdatedAt,version: connectionVersion,+ isAutoRotationEnabled: connectionIsAutoRotationEnabled,isPlatformManagedCredentials: connectionIsPlatformManagedCredentials,gatewayId: connectionGatewayId,projectId: connectionProjectIddiff --git a/backend/src/server/routes/v1/app-connection-routers/app-connection-endpoints.ts b/backend/src/server/routes/v1/app-connection-routers/app-connection-endpoints.tsindex 258b73ff55..77bb2de1eb 100644--- a/backend/src/server/routes/v1/app-connection-routers/app-connection-endpoints.ts+++ b/backend/src/server/routes/v1/app-connection-routers/app-connection-endpoints.ts@@ -26,10 +26,10 @@ export const registerAppConnectionEndpoints = <T extends TAppConnection, I extencredentials: I["credentials"];description?: string | null;isPlatformManagedCredentials?: boolean;- isAutoRotationEnabled?: boolean;+ isAutoRotationEnabled?: boolean | null;gatewayId?: string | null;projectId?: string;- rotation?: TCreateAppConnectionCredentialRotationSchema;+ rotation?: TCreateAppConnectionCredentialRotationSchema | null;}>;updateSchema: z.ZodType<{name?: string;@@ -298,7 +298,7 @@ export const registerAppConnectionEndpoints = <T extends TAppConnection, I extengatewayId,projectId,rotation,- isAutoRotationEnabled+ isAutoRotationEnabled: isAutoRotationEnabled ?? false},req.permission)) as T;diff --git a/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-service.ts b/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-service.tsindex c30347f30e..7137b2d789 100644--- a/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-service.ts+++ b/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-service.ts@@ -11,7 +11,6 @@ import {decryptAppConnectionCredentials,encryptAppConnectionCredentials} from "@app/services/app-connection/app-connection-fns";-import { AzureKeyVaultConnectionMethod } from "@app/services/app-connection/azure-key-vault/azure-key-vault-connection-enums";import { TKmsServiceFactory } from "@app/services/kms/kms-service";import { TAppConnection } from "../app-connection-types";@@ -31,19 +30,13 @@ import {parseRotationErrorMessage} from "./app-connection-credential-rotation-fns";import {- TAppConnectionCredentialRotationGeneratedCredentials,- TAzureClientSecretStrategyConfig,+ TAppConnectionCredentialRotationStrategyConfig,TCreateAppConnectionCredentialRotationDTO,+ TCredentialRotationProvider,TTriggerAppConnectionCredentialRotationDTO,TUpdateAppConnectionCredentialRotationDTO} from "./app-connection-credential-rotation-types";-import {- createAzureClientSecret,- getApplicationObjectId,- listAzurePasswordCredentials,- revokeAzureClientSecret,- validateAzureClientSecretRotationConfig-} from "./providers/azure-client-secret-credential-rotation";+import { azureClientSecretRotationProvider } from "./providers/azure-client-secret-credential-rotation";const MAX_GENERATED_CREDENTIALS_LENGTH = 2;@@ -51,6 +44,10 @@ const STRATEGY_MAP: Record<string, AppConnectionCredentialRotationStrategy> = {[AppConnection.AzureKeyVault]: AppConnectionCredentialRotationStrategy.AzureClientSecret};+const CREDENTIAL_ROTATION_PROVIDER_MAP: Record<AppConnectionCredentialRotationStrategy, TCredentialRotationProvider> = {+ [AppConnectionCredentialRotationStrategy.AzureClientSecret]: azureClientSecretRotationProvider+};+export type TAppConnectionCredentialRotationServiceFactoryDep = {appConnectionCredentialRotationDAL: TAppConnectionCredentialRotationDALFactory;appConnectionDAL: Pick<TAppConnectionDALFactory, "findById" | "updateById" | "transaction">;@@ -71,43 +68,32 @@ export const appConnectionCredentialRotationServiceFactory = ({queueService}: TAppConnectionCredentialRotationServiceFactoryDep) => {/**- * Decrypt connection credentials and extract clientId, clientSecret, tenantId.+ * Decrypt connection credentials.*/const getConnectionClientCredentials = async (connection: {orgId: string;encryptedCredentials: Buffer;projectId?: string | null;- }) => {- const credentials = (await decryptAppConnectionCredentials({+ }): Promise<Record<string, unknown>> => {+ return decryptAppConnectionCredentials({orgId: connection.orgId,encryptedCredentials: connection.encryptedCredentials,kmsService,projectId: connection.projectId- })) as { clientId: string; clientSecret: string; tenantId: string; applicationObjectId?: string };-- return credentials;+ }) as Promise<Record<string, unknown>>;};/**- * Update the connection's stored credentials with a new client secret and optionally save applicationObjectId.+ * Update the connection's stored credentials.*/const updateConnectionCredentials = async (connectionId: string,- newClientSecret: string,- applicationObjectId?: string,+ updatedCredentials: TAppConnection["credentials"],tx?: Knex) => {const connection = await appConnectionDAL.findById(connectionId, tx);if (!connection) throw new NotFoundError({ message: `Connection ${connectionId} not found` });- const credentials = await getConnectionClientCredentials(connection);-- const updatedCredentials = {- ...credentials,- clientSecret: newClientSecret,- ...(applicationObjectId && { applicationObjectId })- } as TAppConnection["credentials"];-const encryptedCredentials = await encryptAppConnectionCredentials({orgId: connection.orgId,credentials: updatedCredentials,@@ -115,13 +101,7 @@ export const appConnectionCredentialRotationServiceFactory = ({projectId: connection.projectId});- await appConnectionDAL.updateById(- connectionId,- {- encryptedCredentials- },- tx- );+ await appConnectionDAL.updateById(connectionId, { encryptedCredentials }, tx);};/**@@ -134,23 +114,15 @@ export const appConnectionCredentialRotationServiceFactory = ({const connection = await appConnectionDAL.findById(connectionId, tx);if (!connection) throw new NotFoundError({ message: `Connection ${connectionId} not found` });- // Validate connection type supports rotation- if (connection.app !== AppConnection.AzureKeyVault) {- throw new BadRequestError({ message: "Credential rotation is only supported for Azure Key Vault connections" });- }-- if (connection.method !== AzureKeyVaultConnectionMethod.ClientSecret) {- throw new BadRequestError({- message: "Credential rotation is only supported for Client Secret auth method"- });- }-// Auto-infer strategy from connection typeconst strategy = STRATEGY_MAP[connection.app];if (!strategy) {- throw new BadRequestError({ message: `No rotation strategy available for ${connection.app} connections` });+ throw new BadRequestError({ message: `Credential rotation is not supported for ${connection.app} connections` });}+ const provider = CREDENTIAL_ROTATION_PROVIDER_MAP[strategy];+ provider.validateConnectionMethod(connection.method);+// Check if rotation config already existsconst existingRotation = await appConnectionCredentialRotationDAL.findByConnectionId(connectionId, tx);if (existingRotation) {@@ -160,35 +132,13 @@ export const appConnectionCredentialRotationServiceFactory = ({// Get the connection's own credentials for self-rotationconst credentials = await getConnectionClientCredentials(connection);- // Auto-detect applicationObjectId if not already stored- let { applicationObjectId } = credentials;- if (!applicationObjectId) {- applicationObjectId = await getApplicationObjectId(credentials);- }-- const strategyConfig: TAzureClientSecretStrategyConfig = { objectId: applicationObjectId };-- // Validate strategy config (check objectId is accessible via Graph API)- await validateAzureClientSecretRotationConfig(strategyConfig, credentials);-- // List existing password credentials before creating a new one, so we can track the original- const existingCredentials = await listAzurePasswordCredentials(strategyConfig, credentials);-- // Perform initial credential issuance- const newCredential = await createAzureClientSecret(strategyConfig, credentials, rotationInterval, 0);-- // Update the connection to use the new credential and save applicationObjectId- await updateConnectionCredentials(connectionId, newCredential.clientSecret, applicationObjectId, tx);+ const { strategyConfig, generatedCredentials, updatedCredentials } = await provider.issueInitialCredentials(+ credentials,+ rotationInterval+ );- // Track the original credential at index 1 so it gets revoked on the first rotation cycle.- // This maintains the dual-credential pattern: two active secrets at all times for zero downtime.- const originalCredential = existingCredentials.find((cred) => cred.keyId !== newCredential.keyId);- const generatedCredentials: TAppConnectionCredentialRotationGeneratedCredentials = [- newCredential,- originalCredential- ? { keyId: originalCredential.keyId, clientSecret: credentials.clientSecret, createdAt: "" }- : null- ];+ // Update the connection to use the new credential+ await updateConnectionCredentials(connectionId, updatedCredentials as TAppConnection["credentials"], tx);const now = new Date();@@ -398,7 +348,7 @@ export const appConnectionCredentialRotationServiceFactory = ({const credentials = await getConnectionClientCredentials(connection);// Decrypt strategy config and generated credentials- const config = await decryptStrategyConfig<TAzureClientSecretStrategyConfig>({+ const config = await decryptStrategyConfig<TAppConnectionCredentialRotationStrategyConfig>({orgId,encryptedStrategyConfig: encryptedConfig,kmsService@@ -410,25 +360,31 @@ export const appConnectionCredentialRotationServiceFactory = ({kmsService});+ const provider = CREDENTIAL_ROTATION_PROVIDER_MAP[strategy as AppConnectionCredentialRotationStrategy];+// Two-credential rotation: create-first, revoke-after.// This ordering ensures that if create fails, we still have 2 valid secrets.const inactiveIndex = (activeIndex + 1) % MAX_GENERATED_CREDENTIALS_LENGTH;const inactiveCredential = generatedCredentials[inactiveIndex];// Step 1: Create new credential first (safe — worst case we have 3 secrets temporarily)- const newCredential = await createAzureClientSecret(config, credentials, rotationInterval, inactiveIndex);+ const newCredential = await provider.createCredential(config, credentials, rotationInterval, inactiveIndex);// Step 2: Update connection credentials with new secret- await updateConnectionCredentials(connectionId, newCredential.clientSecret);+ const updatedCredentials = provider.mergeCredentials(+ credentials,+ newCredential+ ) as TAppConnection["credentials"];+ await updateConnectionCredentials(connectionId, updatedCredentials);// Step 3: Revoke old credential at inactive index (best-effort, non-fatal)- // If this fails, we have 3 secrets on Azure temporarily — they'll be cleaned up on the next cycle.- if (inactiveCredential?.keyId) {+ // If this fails, we have 3 secrets temporarily — they'll be cleaned up on the next cycle.+ if (inactiveCredential) {try {logger.info(`credentialRotation: Revoking old credential keyId=${inactiveCredential.keyId} at index=${inactiveIndex} [rotationId=${rotationId}]`);- await revokeAzureClientSecret(inactiveCredential.keyId, config, credentials);+ await provider.revokeCredential(inactiveCredential, config, credentials);} catch (revokeError) {logger.warn(revokeError,diff --git a/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-types.ts b/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-types.tsindex 59135146d9..2570b06d6e 100644--- a/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-types.ts+++ b/backend/src/services/app-connection/credential-rotation/app-connection-credential-rotation-types.ts@@ -2,6 +2,7 @@ import { z } from "zod";import { TAppConnectionCredentialRotations } from "../../../db/schemas/app-connection-credential-rotations";import { CreateAppConnectionCredentialRotationSchema } from "./app-connection-credential-rotation-schemas";+import { TAzureClientSecretGeneratedCredential, TAzureClientSecretStrategyConfig } from "./providers";export type TAppConnectionCredentialRotationRaw = TAppConnectionCredentialRotations;@@ -14,22 +15,51 @@ export type TAppConnectionCredentialRotation = Omit<export type TCreateAppConnectionCredentialRotationSchema = z.infer<typeof CreateAppConnectionCredentialRotationSchema>;-// Strategy config types-export type TAzureClientSecretStrategyConfig = {- objectId: string;-};-export type TAppConnectionCredentialRotationStrategyConfig = TAzureClientSecretStrategyConfig;-// Generated credentials types-export type TAzureClientSecretGeneratedCredential = {- keyId: string;- clientSecret: string;- createdAt: string;-};-export type TAppConnectionCredentialRotationGeneratedCredentials = (TAzureClientSecretGeneratedCredential | null)[];+export type TCredentialRotationProvider = {+ // Validates the connection's auth method is supported — throws BadRequestError if not+ validateConnectionMethod(method: string): void;++ // Called during createRotation:+ // - Auto-detects strategy config (e.g. applicationObjectId) from credentials+ // - Validates accessibility (e.g. Graph API call)+ // - Lists existing credentials (to track original for first revocation cycle)+ // - Creates the initial credential+ issueInitialCredentials(+ credentials: Record<string, unknown>,+ rotationInterval: number+ ): Promise<{+ strategyConfig: TAppConnectionCredentialRotationStrategyConfig;+ generatedCredentials: TAppConnectionCredentialRotationGeneratedCredentials;+ updatedCredentials: Record<string, unknown>;+ }>;++ // Called during rotateCredentials (queue worker):+ // Creates a new credential at the given index+ createCredential(+ strategyConfig: TAppConnectionCredentialRotationStrategyConfig,+ credentials: Record<string, unknown>,+ rotationInterval: number,+ inactiveIndex: number+ ): Promise<TAzureClientSecretGeneratedCredential>;++ // Merges newly created credential back into the connection's credentials object+ mergeCredentials(+ currentCredentials: Record<string, unknown>,+ newCredential: TAzureClientSecretGeneratedCredential+ ): Record<string, unknown>;++ // Revokes the inactive/old credential (best-effort, non-fatal)+ revokeCredential(+ inactiveCredential: TAzureClientSecretGeneratedCredential,+ strategyConfig: TAppConnectionCredentialRotationStrategyConfig,+ credentials: Record<string, unknown>+ ): Promise<void>;+};+// DTOsexport type TCreateAppConnectionCredentialRotationDTO = {connectionId: string;diff --git a/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret-credential-rotation.ts b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-fns.tssimilarity index 80%rename from backend/src/services/app-connection/credential-rotation/providers/azure-client-secret-credential-rotation.tsrename to backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-fns.tsindex 2b16b7f29d..aff4613b1d 100644--- a/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret-credential-rotation.ts+++ b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-fns.ts@@ -4,10 +4,13 @@ import { AxiosError } from "axios";import { request } from "@app/lib/config/request";import { BadRequestError } from "@app/lib/errors";import { logger } from "@app/lib/logger";+import { AzureKeyVaultConnectionMethod } from "@app/services/app-connection/azure-key-vault/azure-key-vault-connection-enums";import {+ TAppConnectionCredentialRotationGeneratedCredentials,TAzureClientSecretGeneratedCredential,- TAzureClientSecretStrategyConfig+ TAzureClientSecretStrategyConfig,+ TCredentialRotationProvider} from "../app-connection-credential-rotation-types";const GRAPH_API_BASE = "https://graph.microsoft.com/v1.0";@@ -333,3 +336,64 @@ export const revokeAzureClientSecret = async (parseAzureError(error, `Failed to revoke client secret keyId=${keyId} from app ${config.objectId}`);}};++export const azureClientSecretRotationProvider: TCredentialRotationProvider = {+ validateConnectionMethod(method: string) {+ if (method !== AzureKeyVaultConnectionMethod.ClientSecret) {+ throw new BadRequestError({+ message: "Credential rotation is only supported for Client Secret auth method"+ });+ }+ },++ async issueInitialCredentials(credentials, rotationInterval) {+ const azureCredentials = credentials as {+ clientId: string;+ clientSecret: string;+ tenantId: string;+ applicationObjectId?: string;+ };+ let { applicationObjectId } = azureCredentials;+ if (!applicationObjectId) {+ applicationObjectId = await getApplicationObjectId(azureCredentials);+ }+ const strategyConfig: TAzureClientSecretStrategyConfig = { objectId: applicationObjectId };+ await validateAzureClientSecretRotationConfig(strategyConfig, azureCredentials);+ const existingCreds = await listAzurePasswordCredentials(strategyConfig, azureCredentials);+ const newCredential = await createAzureClientSecret(strategyConfig, azureCredentials, rotationInterval, 0);+ const originalCredential = existingCreds.find((c) => c.keyId !== newCredential.keyId);+ const generatedCredentials: TAppConnectionCredentialRotationGeneratedCredentials = [+ newCredential,+ originalCredential+ ? { keyId: originalCredential.keyId, clientSecret: azureCredentials.clientSecret, createdAt: "" }+ : null+ ];+ return {+ strategyConfig,+ generatedCredentials,+ updatedCredentials: { ...azureCredentials, clientSecret: newCredential.clientSecret, applicationObjectId }+ };+ },++ async createCredential(strategyConfig, credentials, rotationInterval, inactiveIndex) {+ return createAzureClientSecret(+ strategyConfig,+ credentials as { clientId: string; clientSecret: string; tenantId: string; applicationObjectId?: string },+ rotationInterval,+ inactiveIndex+ );+ },++ mergeCredentials(currentCredentials, newCredential) {+ return { ...currentCredentials, clientSecret: newCredential.clientSecret };+ },++ async revokeCredential(inactiveCredential, strategyConfig, credentials) {+ if (!inactiveCredential?.keyId) return;+ await revokeAzureClientSecret(+ inactiveCredential.keyId,+ strategyConfig,+ credentials as { clientId: string; clientSecret: string; tenantId: string }+ );+ }+};diff --git a/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-types.ts b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-types.tsnew file mode 100644index 0000000000..6a8a8b4824--- /dev/null+++ b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/azure-client-secret-credential-rotation-types.ts@@ -0,0 +1,11 @@+// Strategy config types+export type TAzureClientSecretStrategyConfig = {+ objectId: string;+};++// Generated credentials types+export type TAzureClientSecretGeneratedCredential = {+ keyId: string;+ clientSecret: string;+ createdAt: string;+};diff --git a/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/index.ts b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/index.tsnew file mode 100644index 0000000000..ed7800cc7a--- /dev/null+++ b/backend/src/services/app-connection/credential-rotation/providers/azure-client-secret/index.ts@@ -0,0 +1,2 @@+export * from "./azure-client-secret-credential-rotation-fns";+export * from "./azure-client-secret-credential-rotation-types";diff --git a/backend/src/services/app-connection/credential-rotation/providers/index.ts b/backend/src/services/app-connection/credential-rotation/providers/index.tsindex 0cbb7a410d..b52db11506 100644--- a/backend/src/services/app-connection/credential-rotation/providers/index.ts+++ b/backend/src/services/app-connection/credential-rotation/providers/index.ts@@ -1,8 +1 @@-export {- createAzureClientSecret,- getApplicationObjectId,- getGraphApiToken,- listAzurePasswordCredentials,- revokeAzureClientSecret,- validateAzureClientSecretRotationConfig-} from "./azure-client-secret-credential-rotation";+export * from "./azure-client-secret";