Skip to content

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

add normalization

adilson6023778d1a6d
diff --git a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.test.ts b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.test.ts
index bda91cc28a..acb2bff26a 100644
--- a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.test.ts
+++ b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.test.ts
@@ -139,4 +139,18 @@ describe("Azure Key Vault secret names", () => {
undefined
);
});
+
+ test("matches a vault secret whose name differs only in case", () => {
+ expect(findInfisicalSecretKeyForAzureKeyVaultName("User-Password", { user_password: { value: "a" } })).toBe(
+ "user_password"
+ );
+ expect(resolveAzureKeyVaultImportedSecretKey("API-KEY", { api_key: { value: "a" } })).toBe("api_key");
+ });
+
+ test("fails the sync when Infisical keys differ only in case once written to Azure Key Vault", () => {
+ const sync = () => assertUniqueAzureKeyVaultSecretNames(["API_KEY", "api-key"]);
+
+ expect(sync).toThrow(SecretSyncError);
+ expect(sync).toThrow("'API_KEY' and 'api-key' both become 'API-KEY'");
+ });
});
diff --git a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.ts b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.ts
index cc827f50c0..e9ccce748d 100644
--- a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.ts
+++ b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-secret-name.ts
@@ -6,13 +6,13 @@ import { SecretSyncError } from "../secret-sync-errors";
// either character, so an imported vault name is stored unchanged. Sync still rewrites each
// underscore to a hyphen on the way out, because that is the only form Key Vault accepts.
// Two Infisical names that differ only by that substitution refer to the same vault secret,
-// which keeps names imported before this change (hyphens rewritten to underscores) matched.
+// which keeps older underscored imports matched. Key Vault names are also case-insensitive, so
+// `API_KEY` and `api-key` are one vault secret as well.
export const toAzureKeyVaultSecretName = (infisicalKey: string) => infisicalKey.replaceAll("_", "-");
-// Key schemas are written with Infisical separators, often underscores. Those separators are
-// hyphens in the vault. Put the separators back so schema stripping removes only the wrapper
-// and leaves hyphens that belong to the secret name.
+export const normalizeAzureKeyVaultSecretName = (name: string) => toAzureKeyVaultSecretName(name).toLowerCase();
+
export const infisicalImportKeyFromAzureKeyVaultName = (azureKey: string, environment: string, schema?: string) => {
if (!schema) return azureKey;
@@ -43,11 +43,12 @@ export const infisicalImportKeyFromAzureKeyVaultName = (azureKey: string, enviro
export const findInfisicalSecretKeyForAzureKeyVaultName = (azureKey: string, secretMap: object): string | undefined => {
if (Object.hasOwn(secretMap, azureKey)) return azureKey;
- return Object.keys(secretMap).find((infisicalKey) => toAzureKeyVaultSecretName(infisicalKey) === azureKey);
+ const normalizedAzureKey = normalizeAzureKeyVaultSecretName(azureKey);
+ return Object.keys(secretMap).find(
+ (infisicalKey) => normalizeAzureKeyVaultSecretName(infisicalKey) === normalizedAzureKey
+ );
};
-// Re-import updates a secret already stored under the older underscored name instead of
-// creating a second secret. A vault name with no match is kept as-is, hyphens included.
export const resolveAzureKeyVaultImportedSecretKey = (azureKey: string, existingSecrets: object) =>
findInfisicalSecretKeyForAzureKeyVaultName(azureKey, existingSecrets) ?? azureKey;
@@ -66,24 +67,24 @@ export const assertUniqueAzureKeyVaultSecretNames = (infisicalKeys: Iterable<str
const keysByAzureName = new Map<string, string[]>();
for (const infisicalKey of infisicalKeys) {
- const azureKey = toAzureKeyVaultSecretName(infisicalKey);
+ const azureKey = normalizeAzureKeyVaultSecretName(infisicalKey);
const group = keysByAzureName.get(azureKey);
if (group) group.push(infisicalKey);
else keysByAzureName.set(azureKey, [infisicalKey]);
}
- const collisions = [...keysByAzureName.entries()].filter(([, keys]) => keys.length > 1);
+ const collisions = [...keysByAzureName.values()].filter((keys) => keys.length > 1);
if (collisions.length === 0) return;
const details = collisions
- .map(([azureKey, keys]) => {
+ .map((keys) => {
const verb = keys.length === 2 ? "both become" : "all become";
- return `${formatSecretNameList(keys)} ${verb} '${azureKey}'`;
+ return `${formatSecretNameList(keys)} ${verb} '${toAzureKeyVaultSecretName(keys[0])}'`;
})
.join(". ");
throw new SecretSyncError({
- message: `These Infisical secret names would overwrite each other in Azure Key Vault, because each underscore is written as a hyphen. ${details}. Rename them so each vault name belongs to one secret.`,
+ message: `These Infisical secret names would overwrite each other in Azure Key Vault, because each underscore is written as a hyphen and vault names ignore case. ${details}. Rename them so each vault name belongs to one secret.`,
shouldRetry: false
});
};
diff --git a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-sync-fns.ts b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-sync-fns.ts
index e94acf53f2..e73441c439 100644
--- a/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-sync-fns.ts
+++ b/backend/src/services/secret-sync/azure-key-vault/azure-key-vault-sync-fns.ts
@@ -18,6 +18,7 @@ import {
assertUniqueAzureKeyVaultSecretNames,
findInfisicalSecretKeyForAzureKeyVaultName,
infisicalImportKeyFromAzureKeyVaultName,
+ normalizeAzureKeyVaultSecretName,
toAzureKeyVaultSecretName
} from "./azure-key-vault-secret-name";
import { GetAzureKeyVaultSecret, TAzureKeyVaultSyncWithCredentials } from "./azure-key-vault-sync-types";
@@ -157,18 +158,23 @@ export const azureKeyVaultSyncFactory = ({
const deleteSecrets: string[] = [];
+ const vaultKeysByNormalizedName = new Map(
+ Object.keys(vaultSecrets).map((key) => [normalizeAzureKeyVaultSecretName(key), key])
+ );
+
Object.keys(secretMap).forEach((infisicalKey) => {
const hyphenatedKey = toAzureKeyVaultSecretName(infisicalKey);
- if (!(hyphenatedKey in vaultSecrets)) {
+ const vaultKey = vaultKeysByNormalizedName.get(normalizeAzureKeyVaultSecretName(infisicalKey));
+ if (!vaultKey) {
// case: secret has been created
setSecrets.push({
key: hyphenatedKey,
value: secretMap[infisicalKey].value
});
- } else if (secretMap[infisicalKey].value !== vaultSecrets[hyphenatedKey].value) {
+ } else if (secretMap[infisicalKey].value !== vaultSecrets[vaultKey].value) {
// case: secret has been updated
setSecrets.push({
- key: hyphenatedKey,
+ key: vaultKey,
value: secretMap[infisicalKey].value
});
}
@@ -184,7 +190,8 @@ export const azureKeyVaultSyncFactory = ({
let isSecretSet = false;
let syncError: Error | null = null;
let maxTries = 6;
- if (disabledAzureKeyVaultSecretKeys.includes(key)) return;
+ if (disabledAzureKeyVaultSecretKeys.some((disabledKey) => disabledKey.toLowerCase() === key.toLowerCase()))
+ return;
while (!isSecretSet && maxTries > 0) {
// try to set secret
@@ -246,10 +253,18 @@ export const azureKeyVaultSyncFactory = ({
if (secretSync.syncOptions.disableSecretDeletion) return;
+ const environmentSlug = secretSync.environment?.slug || "";
+ const { keySchema } = secretSync.syncOptions;
+
+ // The key schema is written with Infisical separators, so compare it against the Infisical form
+ // of the vault name (`dev-FOO` must match `{{environment}}_{{secretKey}}`).
for await (const deleteSecretKey of deleteSecrets.filter(
(secret) =>
- matchesSchema(secret, secretSync.environment?.slug || "", secretSync.syncOptions.keySchema) &&
- !setSecrets.find((setSecret) => setSecret.key === secret)
+ matchesSchema(
+ infisicalImportKeyFromAzureKeyVaultName(secret, environmentSlug, keySchema),
+ environmentSlug,
+ keySchema
+ ) && !setSecrets.find((setSecret) => setSecret.key === secret)
)) {
await requestWithAzureKeyVaultGateway(gatewayConnection, gatewayV2Service, {
method: "DELETE",