Skip to content

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

feat: custom transformer for destination and sync config

Daniel Hougaardfe9c3946da33
diff --git a/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-fns.ts b/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-fns.ts
index ef16f222d8..6c806f1201 100644
--- a/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-fns.ts
+++ b/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-fns.ts
@@ -1,6 +1,11 @@
import { request } from "@app/lib/config/request";
+import { BadRequestError } from "@app/lib/errors";
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
-import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
+import {
+ TPreSaveTransformDestinationConfigFn,
+ TPreSaveTransformSyncOptionsFn,
+ TSecretMap
+} from "@app/services/secret-sync/secret-sync-types";
import { TAppConnectionDALFactory } from "../../app-connection/app-connection-dal";
import { getAzureEntraIdConnectionAccessToken } from "../../app-connection/azure-entra-id/azure-entra-id-connection-fns";
@@ -58,3 +63,55 @@ export const AzureEntraIdScimSyncFns = {
// No-op: removing SCIM token is not supported
}
};
+
+// Resolves secretKey (name) to secretId (UUID) before saving
+export const azureEntraIdScimPreSaveTransformSyncOptions: TPreSaveTransformSyncOptionsFn = async (
+ { syncOptions, folderId },
+ { secretV2BridgeDAL }
+) => {
+ if (!syncOptions || !("secretKey" in syncOptions)) return syncOptions;
+
+ const { secretKey, ...rest } = syncOptions;
+ const secret = await secretV2BridgeDAL.findOne({ key: secretKey as string, folderId });
+ if (!secret) {
+ throw new BadRequestError({
+ message: `Secret with key "${secretKey as string}" not found in the specified source folder`
+ });
+ }
+ return { ...rest, secretId: secret.id };
+};
+
+// Fetches service principal display name from Azure Graph API and stores it in destinationConfig
+export const azureEntraIdScimPreSaveTransformDestinationConfig: TPreSaveTransformDestinationConfigFn = async (
+ { destinationConfig, connectionId },
+ { appConnectionDAL, kmsService }
+) => {
+ if (!destinationConfig) return destinationConfig;
+
+ const { servicePrincipalId } = destinationConfig;
+ if (!servicePrincipalId) return destinationConfig;
+
+ try {
+ const accessToken = await getAzureEntraIdConnectionAccessToken(connectionId, appConnectionDAL, kmsService);
+
+ const { data } = await request.get<{ displayName?: string }>(
+ `https://graph.microsoft.com/v1.0/servicePrincipals/${servicePrincipalId as string}`,
+ {
+ headers: {
+ Authorization: `Bearer ${accessToken}`
+ },
+ params: {
+ $select: "displayName"
+ }
+ }
+ );
+
+ return {
+ ...destinationConfig,
+ ...(data.displayName && { servicePrincipalDisplayName: data.displayName })
+ };
+ } catch {
+ // If we can't fetch the name, proceed without it
+ return destinationConfig;
+ }
+};
diff --git a/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-schemas.ts b/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-schemas.ts
index b147165a86..734fe44449 100644
--- a/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-schemas.ts
+++ b/backend/src/services/secret-sync/azure-entra-id-scim/azure-entra-id-scim-sync-schemas.ts
@@ -21,7 +21,7 @@ const AzureEntraIdScimSyncDestinationConfigSchema = z.object({
servicePrincipalDisplayName: z
.string()
.optional()
- .describe("The display name of the Azure Entra ID Enterprise Application service principal.")
+ .describe(SecretSyncs.DESTINATION_CONFIG.AZURE_ENTRA_ID_SCIM.servicePrincipalDisplayName)
});
const AzureEntraIdScimSyncOptionsConfig: TSyncOptionsConfig = {
diff --git a/backend/src/services/secret-sync/secret-sync-fns.ts b/backend/src/services/secret-sync/secret-sync-fns.ts
index bf6408e059..fbf57ccbfe 100644
--- a/backend/src/services/secret-sync/secret-sync-fns.ts
+++ b/backend/src/services/secret-sync/secret-sync-fns.ts
@@ -20,6 +20,11 @@ import { GITHUB_SYNC_LIST_OPTION, GithubSyncFns } from "@app/services/secret-syn
import { SecretSync, SecretSyncPlanType } from "@app/services/secret-sync/secret-sync-enums";
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
import {
+ TPreSaveTransformDeps,
+ TPreSaveTransformDestinationConfigFn,
+ TPreSaveTransformDestinationConfigParams,
+ TPreSaveTransformSyncOptionsFn,
+ TPreSaveTransformSyncOptionsParams,
TSecretMap,
TSecretSyncListItem,
TSecretSyncWithCredentials
@@ -31,7 +36,12 @@ import { ONEPASS_SYNC_LIST_OPTION, OnePassSyncFns } from "./1password";
import { AwsSecretsManagerSyncMappingBehavior } from "./aws-secrets-manager/aws-secrets-manager-sync-enums";
import { AZURE_APP_CONFIGURATION_SYNC_LIST_OPTION, azureAppConfigurationSyncFactory } from "./azure-app-configuration";
import { AZURE_DEVOPS_SYNC_LIST_OPTION, azureDevOpsSyncFactory } from "./azure-devops";
-import { AZURE_ENTRA_ID_SCIM_SYNC_LIST_OPTION, AzureEntraIdScimSyncFns } from "./azure-entra-id-scim";
+import {
+ AZURE_ENTRA_ID_SCIM_SYNC_LIST_OPTION,
+ azureEntraIdScimPreSaveTransformDestinationConfig,
+ azureEntraIdScimPreSaveTransformSyncOptions,
+ AzureEntraIdScimSyncFns
+} from "./azure-entra-id-scim";
import { AZURE_KEY_VAULT_SYNC_LIST_OPTION, azureKeyVaultSyncFactory } from "./azure-key-vault";
import { BITBUCKET_SYNC_LIST_OPTION, BitbucketSyncFns } from "./bitbucket";
import { CAMUNDA_SYNC_LIST_OPTION, camundaSyncFactory } from "./camunda";
@@ -111,6 +121,34 @@ export const listSecretSyncOptions = () => {
return Object.values(SECRET_SYNC_LIST_OPTIONS).sort((a, b) => a.name.localeCompare(b.name));
};
+const PRE_SAVE_TRANSFORM_SYNC_OPTIONS_MAP: Partial<Record<SecretSync, TPreSaveTransformSyncOptionsFn>> = {
+ [SecretSync.AzureEntraIdScim]: azureEntraIdScimPreSaveTransformSyncOptions
+};
+
+const PRE_SAVE_TRANSFORM_DESTINATION_CONFIG_MAP: Partial<Record<SecretSync, TPreSaveTransformDestinationConfigFn>> = {
+ [SecretSync.AzureEntraIdScim]: azureEntraIdScimPreSaveTransformDestinationConfig
+};
+
+export const preSaveTransformSyncOptions = async (
+ destination: SecretSync,
+ params: TPreSaveTransformSyncOptionsParams,
+ deps: TPreSaveTransformDeps
+) => {
+ const transform = PRE_SAVE_TRANSFORM_SYNC_OPTIONS_MAP[destination];
+ if (!transform) return params.syncOptions;
+ return transform(params, deps);
+};
+
+export const preSaveTransformDestinationConfig = async (
+ destination: SecretSync,
+ params: TPreSaveTransformDestinationConfigParams,
+ deps: TPreSaveTransformDeps
+) => {
+ const transform = PRE_SAVE_TRANSFORM_DESTINATION_CONFIG_MAP[destination];
+ if (!transform) return params.destinationConfig;
+ return transform(params, deps);
+};
+
type TSyncSecretDeps = {
appConnectionDAL: Pick<TAppConnectionDALFactory, "findById" | "update" | "updateById">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts
index d4ae5c8936..4a99138f2b 100644
--- a/backend/src/services/secret-sync/secret-sync-service.ts
+++ b/backend/src/services/secret-sync/secret-sync-service.ts
@@ -10,7 +10,6 @@ import {
ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
-import { request } from "@app/lib/config/request";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { deepEqualSkipFields } from "@app/lib/fn/object";
@@ -21,7 +20,12 @@ import { TProjectDALFactory } from "@app/services/project/project-dal";
import { TProjectBotServiceFactory } from "@app/services/project-bot/project-bot-service";
import { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
import { SecretSync } from "@app/services/secret-sync/secret-sync-enums";
-import { enterpriseSyncCheck, listSecretSyncOptions } from "@app/services/secret-sync/secret-sync-fns";
+import {
+ enterpriseSyncCheck,
+ listSecretSyncOptions,
+ preSaveTransformDestinationConfig,
+ preSaveTransformSyncOptions
+} from "@app/services/secret-sync/secret-sync-fns";
import {
SecretSyncStatus,
TCheckDuplicateDestinationDTO,
@@ -39,7 +43,6 @@ import {
} from "@app/services/secret-sync/secret-sync-types";
import { TAppConnectionDALFactory } from "../app-connection/app-connection-dal";
-import { getAzureEntraIdConnectionAccessToken } from "../app-connection/azure-entra-id/azure-entra-id-connection-fns";
import { TKmsServiceFactory } from "../kms/kms-service";
import { TSecretImportDALFactory } from "../secret-import/secret-import-dal";
import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
@@ -114,68 +117,7 @@ export const secretSyncServiceFactory = ({
});
};
- const resolveSecretKeyToId = async (folderId: string, secretKey: string): Promise<string> => {
- const secret = await secretV2BridgeDAL.findOne({ key: secretKey, folderId });
- if (!secret) {
- throw new BadRequestError({
- message: `Secret with key "${secretKey}" not found in the specified source folder`
- });
- }
- return secret.id;
- };
-
- // For Azure Entra ID SCIM syncs, resolve secretKey (name) in syncOptions to secretId (UUID) before saving
- const resolveAzureEntraIdScimSyncOptions = async (
- destination: SecretSync,
- syncOptions: Record<string, unknown> | undefined,
- folderId: string
- ) => {
- if (destination !== SecretSync.AzureEntraIdScim || !syncOptions || !("secretKey" in syncOptions)) {
- return syncOptions;
- }
-
- const { secretKey, ...rest } = syncOptions;
- const secretId = await resolveSecretKeyToId(folderId, secretKey as string);
- return { ...rest, secretId };
- };
-
- // For Azure Entra ID SCIM syncs, fetch the service principal display name from Azure and store it
- const enrichAzureEntraIdScimDestinationConfig = async (
- destination: SecretSync,
- destinationConfig: Record<string, unknown> | undefined,
- connectionId: string
- ) => {
- if (destination !== SecretSync.AzureEntraIdScim || !destinationConfig) {
- return destinationConfig;
- }
-
- const { servicePrincipalId } = destinationConfig;
- if (!servicePrincipalId) return destinationConfig;
-
- try {
- const accessToken = await getAzureEntraIdConnectionAccessToken(connectionId, appConnectionDAL, kmsService);
-
- const { data } = await request.get<{ displayName?: string }>(
- `https://graph.microsoft.com/v1.0/servicePrincipals/${servicePrincipalId as string}`,
- {
- headers: {
- Authorization: `Bearer ${accessToken}`
- },
- params: {
- $select: "displayName"
- }
- }
- );
-
- return {
- ...destinationConfig,
- ...(data.displayName && { servicePrincipalDisplayName: data.displayName })
- };
- } catch {
- // If we can't fetch the name, proceed without it
- return destinationConfig;
- }
- };
+ const preSaveTransformDeps = { secretV2BridgeDAL, appConnectionDAL, kmsService };
const listSecretSyncsByProjectId = async (
{ projectId, destination }: TListSecretSyncsByProjectId,
@@ -451,16 +393,19 @@ export const secretSyncServiceFactory = ({
actor
);
- const resolvedSyncOptions = await resolveAzureEntraIdScimSyncOptions(
+ const resolvedSyncOptions = await preSaveTransformSyncOptions(
params.destination,
- params.syncOptions as Record<string, unknown> | undefined,
- folder.id
+ { syncOptions: params.syncOptions as Record<string, unknown> | undefined, folderId: folder.id },
+ preSaveTransformDeps
);
- const enrichedDestinationConfig = await enrichAzureEntraIdScimDestinationConfig(
+ const enrichedDestinationConfig = await preSaveTransformDestinationConfig(
params.destination,
- params.destinationConfig as Record<string, unknown> | undefined,
- params.connectionId
+ {
+ destinationConfig: params.destinationConfig as Record<string, unknown> | undefined,
+ connectionId: params.connectionId
+ },
+ preSaveTransformDeps
);
try {
@@ -618,19 +563,24 @@ export const secretSyncServiceFactory = ({
const isAutoSyncEnabled = params.isAutoSyncEnabled ?? secretSync.isAutoSyncEnabled;
- const resolvedSyncOptions = await resolveAzureEntraIdScimSyncOptions(
- destination,
- params.syncOptions as Record<string, unknown> | undefined,
- folderId
- );
+ const resolvedSyncOptions = folderId
+ ? await preSaveTransformSyncOptions(
+ destination,
+ { syncOptions: params.syncOptions as Record<string, unknown> | undefined, folderId },
+ preSaveTransformDeps
+ )
+ : (params.syncOptions as Record<string, unknown> | undefined);
- const connectionIdForEnrich = params.connectionId ?? secretSync.connectionId ?? secretSync.connection?.id;
+ const connectionIdForEnrich = params.connectionId ?? secretSync.connectionId;
const enrichedDestinationConfig = connectionIdForEnrich
- ? await enrichAzureEntraIdScimDestinationConfig(
+ ? await preSaveTransformDestinationConfig(
destination,
- params.destinationConfig as Record<string, unknown> | undefined,
- connectionIdForEnrich
+ {
+ destinationConfig: params.destinationConfig as Record<string, unknown> | undefined,
+ connectionId: connectionIdForEnrich
+ },
+ preSaveTransformDeps
)
: params.destinationConfig;
diff --git a/backend/src/services/secret-sync/secret-sync-types.ts b/backend/src/services/secret-sync/secret-sync-types.ts
index 9fbabe2f97..ef04fd41aa 100644
--- a/backend/src/services/secret-sync/secret-sync-types.ts
+++ b/backend/src/services/secret-sync/secret-sync-types.ts
@@ -48,6 +48,9 @@ import {
TWindmillSyncWithCredentials
} from "@app/services/secret-sync/windmill";
+import { TAppConnectionDALFactory } from "../app-connection/app-connection-dal";
+import { TKmsServiceFactory } from "../kms/kms-service";
+import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
import {
TOnePassSync,
TOnePassSyncInput,
@@ -478,3 +481,29 @@ export type DestinationDuplicateCheckFn = (
existingConfig: Record<string, unknown>,
newConfig: Record<string, unknown>
) => boolean;
+
+export type TPreSaveTransformDeps = {
+ secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "findOne">;
+ appConnectionDAL: Pick<TAppConnectionDALFactory, "findById" | "updateById">;
+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
+};
+
+export type TPreSaveTransformSyncOptionsParams = {
+ syncOptions: Record<string, unknown> | undefined;
+ folderId: string;
+};
+
+export type TPreSaveTransformDestinationConfigParams = {
+ destinationConfig: Record<string, unknown> | undefined;
+ connectionId: string;
+};
+
+export type TPreSaveTransformSyncOptionsFn = (
+ params: TPreSaveTransformSyncOptionsParams,
+ deps: TPreSaveTransformDeps
+) => Promise<Record<string, unknown> | undefined>;
+
+export type TPreSaveTransformDestinationConfigFn = (
+ params: TPreSaveTransformDestinationConfigParams,
+ deps: TPreSaveTransformDeps
+) => Promise<Record<string, unknown> | undefined>;