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
feat: added support for kv v1 for vault secret sync
Daniel Hougaard2019a574cbc7
diff --git a/backend/src/lib/gateway-v2/gateway-v2.ts b/backend/src/lib/gateway-v2/gateway-v2.tsindex e8f614666d..b1b630244b 100644--- a/backend/src/lib/gateway-v2/gateway-v2.ts+++ b/backend/src/lib/gateway-v2/gateway-v2.ts@@ -1,7 +1,7 @@import net from "node:net";import tls from "node:tls";-import axios from "axios";+import axios, { AxiosError } from "axios";import https from "https";import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";@@ -277,6 +277,10 @@ export const withGatewayV2Proxy = async <T>(errorMessage = (err.response?.data as { message: string }).message;}+ if (err instanceof AxiosError) {+ throw err;+ }+throw new BadRequestError({ message: errorMessage });} finally {// Ensure cleanup happens regardless of success or failurediff --git a/backend/src/services/app-connection/hc-vault/hc-vault-connection-service.ts b/backend/src/services/app-connection/hc-vault/hc-vault-connection-service.tsindex fb5695596b..4318c7d206 100644--- a/backend/src/services/app-connection/hc-vault/hc-vault-connection-service.ts+++ b/backend/src/services/app-connection/hc-vault/hc-vault-connection-service.ts@@ -24,7 +24,9 @@ export const hcVaultConnectionService = (try {const mounts = await listHCVaultMounts(appConnection, gatewayService, gatewayV2Service);// Filter for KV version 2 mounts only and extract just the paths- return mounts.filter((mount) => mount.type === "kv" && mount.version === "2").map((mount) => mount.path);+ return mounts+ .filter((mount) => mount.type === "kv" && (mount.version === "2" || mount.version === "1"))+ .map((mount) => mount.path);} catch (error) {logger.error(error, "Failed to establish connection with Hashicorp Vault");return [];diff --git a/backend/src/services/external-migration/external-migration-fns/vault.ts b/backend/src/services/external-migration/external-migration-fns/vault.tsindex cbe7611df5..0de2662c2a 100644--- a/backend/src/services/external-migration/external-migration-fns/vault.ts+++ b/backend/src/services/external-migration/external-migration-fns/vault.ts@@ -9,12 +9,7 @@ import { GatewayProxyProtocol, withGatewayProxy } from "@app/lib/gateway";import { logger } from "@app/lib/logger";import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";-import { InfisicalImportData, VaultMappingType } from "../external-migration-types";--enum KvVersion {- V1 = "1",- V2 = "2"-}+import { InfisicalImportData, KvVersion, VaultMappingType } from "../external-migration-types";type VaultData = {namespace: string;diff --git a/backend/src/services/external-migration/external-migration-types.ts b/backend/src/services/external-migration/external-migration-types.tsindex 49e5bfa883..2b9069b919 100644--- a/backend/src/services/external-migration/external-migration-types.ts+++ b/backend/src/services/external-migration/external-migration-types.ts@@ -2,6 +2,11 @@ import { OrgServiceActor, TOrgPermission } from "@app/lib/types";import { ActorAuthMethod, ActorType } from "../auth/auth-type";+export enum KvVersion {+ V1 = "1",+ V2 = "2"+}+export enum VaultMappingType {Namespace = "namespace",KeyVault = "key-vault",diff --git a/backend/src/services/secret-sync/hc-vault/hc-vault-sync-fns.ts b/backend/src/services/secret-sync/hc-vault/hc-vault-sync-fns.tsindex 4e9857f2c9..a30b000b89 100644--- a/backend/src/services/secret-sync/hc-vault/hc-vault-sync-fns.ts+++ b/backend/src/services/secret-sync/hc-vault/hc-vault-sync-fns.ts@@ -9,6 +9,7 @@ import {requestWithHCVaultGateway,THCVaultConnection} from "@app/services/app-connection/hc-vault";+import { KvVersion } from "@app/services/external-migration/external-migration-types";import {THCVaultListVariables,THCVaultListVariablesResponse,@@ -19,28 +20,101 @@ import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";+const getKvMountVersion = async ({+ instanceUrl,+ namespace,+ mount,+ accessToken,+ connection,+ gatewayService,+ gatewayV2Service+}: {+ instanceUrl: string;+ namespace?: string;+ mount: string;+ accessToken: string;+ connection: THCVaultConnection;+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;+}) => {+ const { data } = await requestWithHCVaultGateway<{+ data: {+ [key: string]: {+ options: {+ version?: string;+ } | null;+ };+ };+ }>(connection, gatewayService, gatewayV2Service, {+ url: `${instanceUrl}/v1/sys/mounts`,+ method: "GET",+ headers: {+ "X-Vault-Token": accessToken,+ ...(namespace ? { "X-Vault-Namespace": namespace } : {})+ }+ });++ const normalizedMount = `${removeTrailingSlash(mount)}/`;++ const mountData = data.data[normalizedMount];++ if (!mountData) {+ throw new Error(`Mount '${mount}' not found`);+ }++ // default to v1+ const kvVersion = mountData.options?.version === KvVersion.V2 ? KvVersion.V2 : KvVersion.V1;++ return {+ version: kvVersion+ };+};+const listHCVaultVariables = async (- { instanceUrl, namespace, mount, accessToken, path }: THCVaultListVariables,+ { instanceUrl, namespace, mount, mountVersion, accessToken, path }: THCVaultListVariables,connection: THCVaultConnection,gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">-) => {+): Promise<{ [key: string]: string }> => {try {- const { data } = await requestWithHCVaultGateway<THCVaultListVariablesResponse>(- connection,- gatewayService,- gatewayV2Service,- {- url: `${instanceUrl}/v1/${removeTrailingSlash(mount)}/data/${path}`,- method: "GET",- headers: {- "X-Vault-Token": accessToken,- ...(namespace ? { "X-Vault-Namespace": namespace } : {})+ // KV v2 uses /data/ in the path, KV v1 does not+ const urlPath =+ mountVersion === KvVersion.V2+ ? `${removeTrailingSlash(mount)}/data/${path}`+ : `${removeTrailingSlash(mount)}/${path}`;++ if (mountVersion === KvVersion.V2) {+ // KV v2 response structure+ const { data } = await requestWithHCVaultGateway<THCVaultListVariablesResponse>(+ connection,+ gatewayService,+ gatewayV2Service,+ {+ url: `${instanceUrl}/v1/${urlPath}`,+ method: "GET",+ headers: {+ "X-Vault-Token": accessToken,+ ...(namespace ? { "X-Vault-Namespace": namespace } : {})+ }}- }- );+ );+ return data.data.data;+ }- return data.data.data;+ // KV v1 response structure (data is one level less nested)+ const { data } = await requestWithHCVaultGateway<{+ data: {+ [key: string]: string;+ };+ }>(connection, gatewayService, gatewayV2Service, {+ url: `${instanceUrl}/v1/${urlPath}`,+ method: "GET",+ headers: {+ "X-Vault-Token": accessToken,+ ...(namespace ? { "X-Vault-Namespace": namespace } : {})+ }+ });+ return data.data;} catch (error: unknown) {// Returning an empty set when a path isn't found allows that path to be created by a later POST requestif (isAxiosError(error) && error.response?.status === 404) {@@ -52,20 +126,29 @@ const listHCVaultVariables = async (// Hashicorp Vault updates all variables in one batch. This is to respect their versioningconst updateHCVaultVariables = async (- { path, instanceUrl, namespace, accessToken, mount, data }: TPostHCVaultVariable,+ { path, instanceUrl, namespace, accessToken, mount, mountVersion, data }: TPostHCVaultVariable,connection: THCVaultConnection,gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">) => {+ // KV v2 uses /data/ in the path, KV v1 does not+ const urlPath =+ mountVersion === KvVersion.V2+ ? `${removeTrailingSlash(mount)}/data/${path}`+ : `${removeTrailingSlash(mount)}/${path}`;++ // KV v2 wraps data in { data: { ... } }, KV v1 sends data directly+ const payload = mountVersion === KvVersion.V2 ? { data } : data;+return requestWithHCVaultGateway(connection, gatewayService, gatewayV2Service, {- url: `${instanceUrl}/v1/${removeTrailingSlash(mount)}/data/${path}`,+ url: `${instanceUrl}/v1/${urlPath}`,method: "POST",headers: {"X-Vault-Token": accessToken,...(namespace ? { "X-Vault-Namespace": namespace } : {}),"Content-Type": "application/json"},- data: { data }+ data: payload});};@@ -87,12 +170,24 @@ export const HCVaultSyncFns = {const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);const instanceUrl = await getHCVaultInstanceUrl(connection);+ // Get mount details to determine KV version+ const { version: mountVersion } = await getKvMountVersion({+ instanceUrl,+ namespace,+ mount,+ accessToken,+ connection,+ gatewayService,+ gatewayV2Service+ });+const variables = await listHCVaultVariables({instanceUrl,accessToken,namespace,mount,+ mountVersion,path},connection,@@ -126,7 +221,7 @@ export const HCVaultSyncFns = {try {await updateHCVaultVariables(- { accessToken, instanceUrl, namespace, mount, path, data: variables },+ { accessToken, instanceUrl, namespace, mount, mountVersion, path, data: variables },connection,gatewayService,gatewayV2Service@@ -152,8 +247,19 @@ export const HCVaultSyncFns = {const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);const instanceUrl = await getHCVaultInstanceUrl(connection);+ // Get mount details to determine KV version+ const { version: mountVersion } = await getKvMountVersion({+ instanceUrl,+ namespace,+ mount,+ accessToken,+ connection,+ gatewayService,+ gatewayV2Service+ });+const variables = await listHCVaultVariables(- { instanceUrl, namespace, accessToken, mount, path },+ { instanceUrl, namespace, accessToken, mount, mountVersion, path },connection,gatewayService,gatewayV2Service@@ -167,7 +273,7 @@ export const HCVaultSyncFns = {try {await updateHCVaultVariables(- { accessToken, instanceUrl, namespace, mount, path, data: variables },+ { accessToken, instanceUrl, namespace, mount, mountVersion, path, data: variables },connection,gatewayService,gatewayV2Service@@ -192,12 +298,24 @@ export const HCVaultSyncFns = {const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);const instanceUrl = await getHCVaultInstanceUrl(connection);+ // Get mount details to determine KV version+ const { version: mountVersion } = await getKvMountVersion({+ instanceUrl,+ namespace,+ mount,+ accessToken,+ connection,+ gatewayService,+ gatewayV2Service+ });+const variables = await listHCVaultVariables({instanceUrl,namespace,accessToken,mount,+ mountVersion,path},connection,diff --git a/backend/src/services/secret-sync/hc-vault/hc-vault-sync-types.ts b/backend/src/services/secret-sync/hc-vault/hc-vault-sync-types.tsindex 4da823a76c..f5a5da2789 100644--- a/backend/src/services/secret-sync/hc-vault/hc-vault-sync-types.ts+++ b/backend/src/services/secret-sync/hc-vault/hc-vault-sync-types.ts@@ -1,6 +1,7 @@import { z } from "zod";import { THCVaultConnection } from "@app/services/app-connection/hc-vault";+import { KvVersion } from "@app/services/external-migration/external-migration-types";import { CreateHCVaultSyncSchema, HCVaultSyncListItemSchema, HCVaultSyncSchema } from "./hc-vault-sync-schemas";@@ -27,6 +28,7 @@ export type THCVaultListVariables = {instanceUrl: string;namespace?: string;mount: string;+ mountVersion: KvVersion;path: string;};diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/HCVaultSyncFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/HCVaultSyncFields.tsxindex 76e14b6d70..d06d242bbf 100644--- a/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/HCVaultSyncFields.tsx+++ b/frontend/src/components/secret-syncs/forms/SecretSyncDestinationFields/HCVaultSyncFields.tsx@@ -44,7 +44,7 @@ export const HCVaultSyncFields = () => {helperText={<TooltipclassName="max-w-md"- content="Ensure the Secrets Engine mount exists and that your App Role / Access Token has permission to access it. Infisical only supports version 2 KV Secrets Engine mounts. If you're using Hashicorp Cloud Platform, ensure that you correctly defined your 'namespace' when creating the App Connection."+ content="Ensure the Secrets Engine mount exists and that your App Role / Access Token has permission to access it. Infisical currently supports KV Engines version 1 and 2. If you're using Hashicorp Cloud Platform, ensure that you correctly defined your 'namespace' when creating the App Connection."><div><span>Don't see the mount you're looking for?</span>{" "}