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
fix: update aws sm sync to properly handle key schema when using many-to-one
Scott Wilsonb4dbdc3df97d
diff --git a/backend/src/services/secret-sync/aws-secrets-manager/aws-secrets-manager-sync-fns.ts b/backend/src/services/secret-sync/aws-secrets-manager/aws-secrets-manager-sync-fns.tsindex 8573d6ffcf..87882b0832 100644--- a/backend/src/services/secret-sync/aws-secrets-manager/aws-secrets-manager-sync-fns.ts+++ b/backend/src/services/secret-sync/aws-secrets-manager/aws-secrets-manager-sync-fns.ts@@ -29,7 +29,7 @@ import { crypto } from "@app/lib/crypto";import { getAwsConnectionConfig } from "@app/services/app-connection/aws/aws-connection-fns";import { AwsSecretsManagerSyncMappingBehavior } from "@app/services/secret-sync/aws-secrets-manager/aws-secrets-manager-sync-enums";import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";-import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";+import { getKeyWithSchema, matchesSchema } from "@app/services/secret-sync/secret-sync-fns";import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";import { TAwsSecretsManagerSyncWithCredentials } from "./aws-secrets-manager-sync-types";@@ -314,7 +314,11 @@ const processTags = ({};export const AwsSecretsManagerSyncFns = {- syncSecrets: async (secretSync: TAwsSecretsManagerSyncWithCredentials, secretMap: TSecretMap) => {+ syncSecrets: async (+ secretSync: TAwsSecretsManagerSyncWithCredentials,+ secretMap: TSecretMap,+ unmodifiedSecretMap: TSecretMap // ie not schematized+ ) => {const { destinationConfig, syncOptions, environment } = secretSync;const client = await getSecretsManagerClient(secretSync);@@ -428,18 +432,24 @@ export const AwsSecretsManagerSyncFns = {// Many-To-One Mappingconst secretValue = JSON.stringify(- Object.fromEntries(Object.entries(secretMap).map(([key, secretData]) => [key, secretData.value]))+ Object.fromEntries(Object.entries(unmodifiedSecretMap).map(([key, secretData]) => [key, secretData.value])));- if (awsSecretsRecord[destinationConfig.secretName]) {+ const secretName = getKeyWithSchema({+ key: destinationConfig.secretName,+ environment: environment!.slug, // wouldn't be sync if undefined+ schema: syncOptions.keySchema+ });++ if (awsSecretsRecord[secretName]) {await updateSecret(client, {- SecretId: destinationConfig.secretName,+ SecretId: secretName,SecretString: secretValue,KmsKeyId: keyId});} else {await createSecret(client, {- Name: destinationConfig.secretName,+ Name: secretName,SecretString: secretValue,KmsKeyId: keyId});@@ -449,28 +459,28 @@ export const AwsSecretsManagerSyncFns = {const { tagsToAdd, tagKeysToRemove } = processTags({syncTagsRecord,awsTagsRecord: Object.fromEntries(- awsDescriptionsRecord[destinationConfig.secretName]?.Tags?.map((tag) => [tag.Key!, tag.Value!]) ?? []+ awsDescriptionsRecord[secretName]?.Tags?.map((tag) => [tag.Key!, tag.Value!]) ?? [])});if (tagsToAdd.length) {try {- await addTags(client, destinationConfig.secretName, tagsToAdd);+ await addTags(client, secretName, tagsToAdd);} catch (error) {throw new SecretSyncError({error,- secretKey: destinationConfig.secretName+ secretKey: secretName});}}if (tagKeysToRemove.length) {try {- await removeTags(client, destinationConfig.secretName, tagKeysToRemove);+ await removeTags(client, secretName, tagKeysToRemove);} catch (error) {throw new SecretSyncError({error,- secretKey: destinationConfig.secretName+ secretKey: secretName});}}@@ -487,7 +497,7 @@ export const AwsSecretsManagerSyncFns = {);const awsValuesRecord = await getSecretValuesRecord(client, awsSecretsRecord);- const { destinationConfig } = secretSync;+ const { destinationConfig, environment, syncOptions } = secretSync;if (destinationConfig.mappingBehavior === AwsSecretsManagerSyncMappingBehavior.OneToOne) {return Object.fromEntries(@@ -499,7 +509,13 @@ export const AwsSecretsManagerSyncFns = {// Many-To-One Mapping- const secretValueEntry = awsValuesRecord[destinationConfig.secretName];+ const secretName = getKeyWithSchema({+ key: destinationConfig.secretName,+ environment: environment!.slug, // wouldn't be sync if undefined+ schema: syncOptions.keySchema+ });++ const secretValueEntry = awsValuesRecord[secretName];if (!secretValueEntry) return {};@@ -539,7 +555,13 @@ export const AwsSecretsManagerSyncFns = {}}} else {- await deleteSecret(client, destinationConfig.secretName);+ const secretName = getKeyWithSchema({+ key: destinationConfig.secretName,+ environment: environment!.slug, // wouldn't be sync if undefined+ schema: syncOptions.keySchema+ });++ await deleteSecret(client, secretName);}}};diff --git a/backend/src/services/secret-sync/secret-sync-fns.ts b/backend/src/services/secret-sync/secret-sync-fns.tsindex a3b5510622..534960559a 100644--- a/backend/src/services/secret-sync/secret-sync-fns.ts+++ b/backend/src/services/secret-sync/secret-sync-fns.ts@@ -28,6 +28,7 @@ import {import { TAppConnectionDALFactory } from "../app-connection/app-connection-dal";import { TKmsServiceFactory } from "../kms/kms-service";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_KEY_VAULT_SYNC_LIST_OPTION, azureKeyVaultSyncFactory } from "./azure-key-vault";@@ -115,6 +116,23 @@ type TSyncSecretDeps = {gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;};+export const getKeyWithSchema = ({+ key,+ environment,+ schema+}: {+ key: string;+ environment: string;+ schema?: string;+}) => {+ if (!schema) return key;++ return handlebars.compile(schema)({+ secretKey: key,+ environment+ });+};+// Add schema to secret keysconst addSchema = (unprocessedSecretMap: TSecretMap, environment: string, schema?: string): TSecretMap => {if (!schema) return unprocessedSecretMap;@@ -229,7 +247,7 @@ export const SecretSyncFns = {case SecretSync.AWSParameterStore:return AwsParameterStoreSyncFns.syncSecrets(secretSync, schemaSecretMap);case SecretSync.AWSSecretsManager:- return AwsSecretsManagerSyncFns.syncSecrets(secretSync, schemaSecretMap);+ return AwsSecretsManagerSyncFns.syncSecrets(secretSync, schemaSecretMap, secretMap);case SecretSync.GitHub:return GithubSyncFns.syncSecrets(secretSync, schemaSecretMap, gatewayService, gatewayV2Service);case SecretSync.GCPSecretManager:@@ -328,6 +346,9 @@ export const SecretSyncFns = {break;case SecretSync.AWSSecretsManager:secretMap = await AwsSecretsManagerSyncFns.getSecrets(secretSync);+ // if many-to-one we don't check for/strip schema, as schema is only applied to the secret name+ if (secretSync.destinationConfig.mappingBehavior === AwsSecretsManagerSyncMappingBehavior.ManyToOne)+ return secretMap;break;case SecretSync.GitHub:secretMap = await GithubSyncFns.getSecrets(secretSync);