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
simplify the fallback
adilson69b2892fc08a
diff --git a/backend/src/services/app-connection/hc-vault/hc-vault-connection-fns.ts b/backend/src/services/app-connection/hc-vault/hc-vault-connection-fns.tsindex aa6eea0a1e..587a0270df 100644--- a/backend/src/services/app-connection/hc-vault/hc-vault-connection-fns.ts+++ b/backend/src/services/app-connection/hc-vault/hc-vault-connection-fns.ts@@ -718,6 +718,15 @@ export const listHCVaultMounts = async (return mounts;};+// vault 1.0.0 does not support namespace root or /, responding with a 301 when the namespace header is sent.+// This sentinel signals the caller to retry the request without the X-Vault-Namespace header.+class NamespaceHeaderNotSupportedError extends Error {+ constructor() {+ super("HashiCorp Vault rejected the namespace header");+ this.name = "NamespaceHeaderNotSupportedError";+ }+}+export const listHCVaultSecretPaths = async (namespace: string,connection: THCVaultConnection,@@ -729,11 +738,12 @@ export const listHCVaultSecretPaths = async (const instanceUrl = await getHCVaultInstanceUrl(connection);const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);- // vault 1.0.0 does not support namespace root or /, responding with a 301 when the namespace header is sent.- // Once we detect this, all subsequent requests should skip the namespace header to avoid the wasted 301 round-trip.- let skipNamespaceHeader = false;-- const getPaths = async (mountPath: string, secretPath: string, kvVersion: "1" | "2"): Promise<string[] | null> => {+ const getPaths = async (+ mountPath: string,+ secretPath: string,+ kvVersion: "1" | "2",+ skipNamespaceHeader: boolean = false+ ): Promise<string[] | null> => {let path: string;if (kvVersion === "2") {// For KV v2: /v1/{mount}/metadata/{path}?list=true@@ -743,8 +753,8 @@ export const listHCVaultSecretPaths = async (path = secretPath ? `${mountPath}/${secretPath}` : mountPath;}- const fetchPaths = (namespaceHeader?: string) =>- requestWithHCVaultGateway<{+ try {+ const { data } = await requestWithHCVaultGateway<{data: {keys: string[];};@@ -757,53 +767,25 @@ export const listHCVaultSecretPaths = async (method: "GET",headers: {"X-Vault-Token": accessToken,- ...(namespaceHeader ? { "X-Vault-Namespace": namespaceHeader } : {})+ ...(skipNamespaceHeader ? {} : { "X-Vault-Namespace": namespace })}},gatewayDetails);-- // Once we've detected the namespace header must be dropped, skip it upfront to avoid the wasted 301 round-trip.- if (skipNamespaceHeader) {- try {- const { data } = await fetchPaths();- return data.data.keys;- } catch (error) {- if ((error instanceof AxiosError && error.response?.status === 404) || isGateway404Error(error)) {- return null;- }- throw error;- }- }-- try {- const { data } = await fetchPaths(namespace);return data.data.keys;} catch (error) {if ((error instanceof AxiosError && error.response?.status === 404) || isGateway404Error(error)) {return null;}- // vault 1.0.0 does not support namespace root or /, so we need to handle this case- // if the error is 301 and the namespace is root or /, try to fetch paths without the namespace header+ // vault 1.0.0 does not support namespace root or /, responding with a 301 when the namespace header is sent.+ // Signal the caller to retry the whole mount without the namespace header.if (+ !skipNamespaceHeader &&((error instanceof AxiosError && error.response?.status === 301) || isGateway301Error(error)) &&- (!namespace || namespace === "/" || namespace === "root")+ (namespace === "/" || namespace === "root" || !namespace)) {- try {- const { data } = await fetchPaths();- // Remember so subsequent requests skip the namespace header entirely.- skipNamespaceHeader = true;- return data.data.keys;- } catch (fetchError) {- if (- (fetchError instanceof AxiosError && fetchError.response?.status === 404) ||- isGateway404Error(fetchError)- ) {- return null;- }- throw fetchError;- }+ throw new NamespaceHeaderNotSupportedError();}throw error;@@ -815,9 +797,10 @@ export const listHCVaultSecretPaths = async (mountPath: string,kvVersion: "1" | "2",limiter: ReturnType<typeof createConcurrencyLimiter>,- currentPath: string = ""+ currentPath: string = "",+ skipNamespaceHeader: boolean = false): Promise<string[]> => {- const paths = await limiter(() => getPaths(mountPath, currentPath, kvVersion));+ const paths = await limiter(() => getPaths(mountPath, currentPath, kvVersion, skipNamespaceHeader));if (paths === null || paths.length === 0) {return [];@@ -831,7 +814,7 @@ export const listHCVaultSecretPaths = async (if (path.endsWith("/")) {// it's a folder so we recurse into it- return recursivelyGetAllPaths(mountPath, kvVersion, limiter, fullItemPath);+ return recursivelyGetAllPaths(mountPath, kvVersion, limiter, fullItemPath, skipNamespaceHeader);}// it's a secret so we return itreturn [`${mountPath}/${fullItemPath}`];@@ -862,7 +845,15 @@ export const listHCVaultSecretPaths = async (kvMounts.map(async (mount) => {const kvVersion = mount.version === "2" ? "2" : "1";const cleanMountPath = mount.path.replace(/\/$/, ""); // Remove trailing slash- return recursivelyGetAllPaths(cleanMountPath, kvVersion, limiter);+ try {+ return await recursivelyGetAllPaths(cleanMountPath, kvVersion, limiter);+ } catch (error) {+ // Vault rejected the namespace header (301 on root/"/"), retry the mount without it.+ if (error instanceof NamespaceHeaderNotSupportedError) {+ return recursivelyGetAllPaths(cleanMountPath, kvVersion, limiter, "", true);+ }+ throw error;+ }}));