Skip to content

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

feat: reusable gateway

Daniel Hougaardcc1bb01eeac2
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.ts
index 725db975ff..9f00d189d5 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
@@ -4,10 +4,12 @@ import https from "https";
import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
+import { TGatewayV2ConnectionDetails } from "@app/ee/services/gateway-v2/gateway-v2-types";
import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
import { removeTrailingSlash } from "@app/lib/fn";
import { GatewayProxyProtocol, withGatewayProxy } from "@app/lib/gateway";
+import { GatewayVersion, TGatewayV1RelayDetails } from "@app/lib/gateway/types";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { logger } from "@app/lib/logger";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
@@ -15,6 +17,7 @@ import { AppConnection } from "@app/services/app-connection/app-connection-enums
import { HCVaultAuthType, HCVaultConnectionMethod } from "./hc-vault-connection-enums";
import {
+ TGatewayDetails,
THCVaultAuthMount,
THCVaultAuthMountResponse,
THCVaultConnection,
@@ -129,7 +132,8 @@ export const requestWithHCVaultGateway = async <T>(
appConnection: { gatewayId?: string | null },
gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
- requestConfig: AxiosRequestConfig
+ requestConfig: AxiosRequestConfig,
+ gatewayDetails?: TGatewayDetails
): Promise<AxiosResponse<T>> => {
const { gatewayId } = appConnection;
@@ -141,19 +145,35 @@ export const requestWithHCVaultGateway = async <T>(
return request.request(requestConfig);
}
- const [targetHost] = await verifyHostInputValidity({ host: url.hostname, isGateway: true, isDynamicSecret: false });
+ let gatewayConnectionDetailsV2: TGatewayV2ConnectionDetails | undefined;
- // port is empty string when using protocol's default port (443 for https, 80 for http)
- // eslint-disable-next-line no-nested-ternary
- const targetPort = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80;
+ if (gatewayDetails && gatewayDetails.gatewayVersion === GatewayVersion.V2) {
+ gatewayConnectionDetailsV2 = gatewayDetails.details;
+ }
- const gatewayConnectionDetails = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
- gatewayId,
- targetHost,
- targetPort
- });
+ let targetHost: string;
+ let targetPort: number;
+
+ if (gatewayDetails) {
+ targetHost = gatewayDetails.target.host;
+ targetPort = gatewayDetails.target.port;
+ } else {
+ [targetHost] = await verifyHostInputValidity({ host: url.hostname, isGateway: true, isDynamicSecret: false });
+ // port is empty string when using protocol's default port (443 for https, 80 for http)
+ // eslint-disable-next-line no-nested-ternary
+ targetPort = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80;
+ }
+
+ // if no gateway details are provided, we first try gateway v2, then gateway v1 as a fallback.
+ if (!gatewayDetails) {
+ gatewayConnectionDetailsV2 = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
+ gatewayId,
+ targetHost,
+ targetPort
+ });
+ }
- if (gatewayConnectionDetails) {
+ if (gatewayConnectionDetailsV2) {
return withGatewayV2Proxy(
async (proxyPort) => {
const isHttps = url.protocol === "https:";
@@ -188,15 +208,19 @@ export const requestWithHCVaultGateway = async <T>(
},
{
protocol: GatewayProxyProtocol.Tcp,
- relayHost: gatewayConnectionDetails.relayHost,
- gateway: gatewayConnectionDetails.gateway,
- relay: gatewayConnectionDetails.relay
+ relayHost: gatewayConnectionDetailsV2.relayHost,
+ gateway: gatewayConnectionDetailsV2.gateway,
+ relay: gatewayConnectionDetailsV2.relay
}
);
}
- const relayDetails = await gatewayService.fnGetGatewayClientTlsByGatewayId(gatewayId);
- const [relayHost, relayPort] = relayDetails.relayAddress.split(":");
+ let gatewayConnectionDetailsV1: TGatewayV1RelayDetails | undefined;
+ if (gatewayDetails && gatewayDetails.gatewayVersion === GatewayVersion.V1) {
+ gatewayConnectionDetailsV1 = gatewayDetails.details;
+ } else {
+ gatewayConnectionDetailsV1 = await gatewayService.fnGetGatewayClientTlsByGatewayId(gatewayId);
+ }
return withGatewayProxy(
async (proxyPort) => {
@@ -230,18 +254,10 @@ export const requestWithHCVaultGateway = async <T>(
}
},
{
+ relayDetails: gatewayConnectionDetailsV1,
protocol: GatewayProxyProtocol.Tcp,
targetHost,
- targetPort,
- relayHost,
- relayPort: Number(relayPort),
- identityId: relayDetails.identityId,
- orgId: relayDetails.orgId,
- tlsOptions: {
- ca: relayDetails.certChain,
- cert: relayDetails.certificate,
- key: relayDetails.privateKey.toString()
- }
+ targetPort
}
);
};
@@ -326,7 +342,8 @@ export const listHCVaultPolicies = async (
namespace: string,
connection: THCVaultConnection,
gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
- gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
+ gatewayDetails?: TGatewayDetails
) => {
const instanceUrl = await getHCVaultInstanceUrl(connection);
const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);
@@ -336,14 +353,20 @@ export const listHCVaultPolicies = async (
data: {
policies: string[];
};
- }>(connection, gatewayService, gatewayV2Service, {
- url: `${instanceUrl}/v1/sys/policy`,
- method: "GET",
- headers: {
- "X-Vault-Token": accessToken,
- "X-Vault-Namespace": namespace
- }
- });
+ }>(
+ connection,
+ gatewayService,
+ gatewayV2Service,
+ {
+ url: `${instanceUrl}/v1/sys/policy`,
+ method: "GET",
+ headers: {
+ "X-Vault-Token": accessToken,
+ "X-Vault-Namespace": namespace
+ }
+ },
+ gatewayDetails
+ );
const policyNames = listData.data.policies || [];
@@ -358,14 +381,20 @@ export const listHCVaultPolicies = async (
name: string;
rules: string;
};
- }>(connection, gatewayService, gatewayV2Service, {
- url: `${instanceUrl}/v1/sys/policy/${policyName}`,
- method: "GET",
- headers: {
- "X-Vault-Token": accessToken,
- "X-Vault-Namespace": namespace
- }
- });
+ }>(
+ connection,
+ gatewayService,
+ gatewayV2Service,
+ {
+ url: `${instanceUrl}/v1/sys/policy/${policyName}`,
+ method: "GET",
+ headers: {
+ "X-Vault-Token": accessToken,
+ "X-Vault-Namespace": namespace
+ }
+ },
+ gatewayDetails
+ );
return {
name: policyData.data.name,
@@ -519,7 +548,8 @@ export const listHCVaultMounts = async (
connection: THCVaultConnection,
gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
- namespace?: string
+ namespace?: string,
+ gatewayDetails?: TGatewayDetails
) => {
const instanceUrl = await getHCVaultInstanceUrl(connection);
const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);
@@ -537,7 +567,8 @@ export const listHCVaultMounts = async (
"X-Vault-Token": accessToken,
...(targetNamespace ? { "X-Vault-Namespace": targetNamespace } : {})
}
- }
+ },
+ gatewayDetails
);
const mounts: THCVaultMount[] = [];
@@ -558,7 +589,8 @@ export const listHCVaultSecretPaths = async (
connection: THCVaultConnection,
gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
- filterMountPath?: string
+ filterMountPath?: string,
+ gatewayDetails?: TGatewayDetails
) => {
const instanceUrl = await getHCVaultInstanceUrl(connection);
const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);
@@ -578,14 +610,20 @@ export const listHCVaultSecretPaths = async (
data: {
keys: string[];
};
- }>(connection, gatewayService, gatewayV2Service, {
- url: `${instanceUrl}/v1/${path}?list=true`,
- method: "GET",
- headers: {
- "X-Vault-Token": accessToken,
- "X-Vault-Namespace": namespace
- }
- });
+ }>(
+ connection,
+ gatewayService,
+ gatewayV2Service,
+ {
+ url: `${instanceUrl}/v1/${path}?list=true`,
+ method: "GET",
+ headers: {
+ "X-Vault-Token": accessToken,
+ "X-Vault-Namespace": namespace
+ }
+ },
+ gatewayDetails
+ );
return data.data.keys;
} catch (error) {
@@ -761,7 +799,8 @@ export const getHCVaultAuthMounts = async (
authType: HCVaultAuthType | undefined,
connection: THCVaultConnection,
gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
- gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
+ gatewayDetails?: TGatewayDetails
): Promise<THCVaultAuthMount[]> => {
const instanceUrl = await getHCVaultInstanceUrl(connection);
const accessToken = await getHCVaultAccessToken(connection, gatewayService, gatewayV2Service);
@@ -778,7 +817,8 @@ export const getHCVaultAuthMounts = async (
"X-Vault-Token": accessToken,
"X-Vault-Namespace": namespace
}
- }
+ },
+ gatewayDetails
);
const authMounts: THCVaultAuthMount[] = [];
diff --git a/backend/src/services/external-migration/external-migration-service.ts b/backend/src/services/external-migration/external-migration-service.ts
index 4f32a5bc01..ad41dd457d 100644
--- a/backend/src/services/external-migration/external-migration-service.ts
+++ b/backend/src/services/external-migration/external-migration-service.ts
@@ -5,12 +5,14 @@ import {
SecretApprovalEvent,
TAuditLogServiceFactory
} from "@app/ee/services/audit-log/audit-log-types";
+import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { crypto } from "@app/lib/crypto/cryptography";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { GatewayVersion } from "@app/lib/gateway/types";
import { OrgServiceActor } from "@app/lib/types";
import { AppConnection } from "../app-connection/app-connection-enums";
@@ -20,6 +22,7 @@ import {
convertVaultValueToString,
getHCVaultAuthMounts,
getHCVaultDatabaseRoles,
+ getHCVaultInstanceUrl,
getHCVaultKubernetesAuthRoles,
getHCVaultKubernetesRoles,
getHCVaultLdapRoles,
@@ -28,6 +31,7 @@ import {
listHCVaultMounts,
listHCVaultPolicies,
listHCVaultSecretPaths,
+ TGatewayDetails,
THCVaultConnection
} from "../app-connection/hc-vault";
import { TKmsServiceFactory } from "../kms/kms-service";
@@ -84,6 +88,55 @@ export const externalMigrationServiceFactory = ({
vaultExternalMigrationConfigDAL,
kmsService
}: TExternalMigrationServiceFactoryDep) => {
+ const getGatewayDetails = async (connection: THCVaultConnection) => {
+ let gatewayDetails: TGatewayDetails | undefined;
+
+ if (connection.gatewayId) {
+ const instanceUrl = await getHCVaultInstanceUrl(connection);
+ const url = new URL(instanceUrl);
+
+ const [targetHost] = await verifyHostInputValidity({
+ host: url.hostname,
+ isGateway: true,
+ isDynamicSecret: false
+ });
+
+ // eslint-disable-next-line no-nested-ternary
+ const targetPort = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80;
+
+ const gatewayV2Details = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
+ gatewayId: connection.gatewayId,
+ targetHost,
+ targetPort
+ });
+
+ if (gatewayV2Details) {
+ gatewayDetails = {
+ gatewayVersion: GatewayVersion.V2,
+ details: gatewayV2Details,
+ target: {
+ host: targetHost,
+ port: targetPort
+ }
+ };
+ } else {
+ const gatewayV1Details = await gatewayService.fnGetGatewayClientTlsByGatewayId(connection.gatewayId);
+ if (gatewayV1Details) {
+ gatewayDetails = {
+ gatewayVersion: GatewayVersion.V1,
+ details: gatewayV1Details,
+ target: {
+ host: targetHost,
+ port: targetPort
+ }
+ };
+ }
+ }
+ }
+
+ return gatewayDetails;
+ };
+
// Helper to verify admin permissions and get vault connection
const getVaultConnectionForNamespace = async (actor: OrgServiceActor, namespace: string, action: string) => {
const { hasRole } = await permissionService.getOrgPermission({
@@ -281,14 +334,30 @@ export const externalMigrationServiceFactory = ({
throw new BadRequestError({ message: "Namespace value does not match the namespace of the connection" });
}
+ const gatewayDetails = await getGatewayDetails(connection);
+
try {
- await listHCVaultPolicies(namespace, connection, gatewayService, gatewayV2Service);
- await getHCVaultAuthMounts(namespace, HCVaultAuthType.Kubernetes, connection, gatewayService, gatewayV2Service);
+ await listHCVaultPolicies(namespace, connection, gatewayService, gatewayV2Service, gatewayDetails);
+ await getHCVaultAuthMounts(
+ namespace,
+ HCVaultAuthType.Kubernetes,
+ connection,
+ gatewayService,
+ gatewayV2Service,
+ gatewayDetails
+ );
const mounts = await listHCVaultMounts(connection, gatewayService, gatewayV2Service);
const sampleKvMount = mounts.find((mount) => mount.type === "kv");
if (sampleKvMount) {
- await listHCVaultSecretPaths(namespace, connection, gatewayService, gatewayV2Service, sampleKvMount.path);
+ await listHCVaultSecretPaths(
+ namespace,
+ connection,
+ gatewayService,
+ gatewayV2Service,
+ sampleKvMount.path,
+ gatewayDetails
+ );
}
} catch (error) {
throw new BadRequestError({
@@ -323,6 +392,7 @@ export const externalMigrationServiceFactory = ({
});
try {
+ throw new BadRequestError({ message: "It worked" });
const config = await vaultExternalMigrationConfigDAL.create({
namespace,
connectionId,
@@ -436,12 +506,18 @@ export const externalMigrationServiceFactory = ({
const getVaultPolicies = async ({ actor, namespace }: { actor: OrgServiceActor; namespace: string }) => {
const connection = await getVaultConnectionForNamespace(actor, namespace, "view vault policies");
- return listHCVaultPolicies(namespace, connection, gatewayService, gatewayV2Service);
+
+ const gatewayDetails = await getGatewayDetails(connection);
+
+ return listHCVaultPolicies(namespace, connection, gatewayService, gatewayV2Service, gatewayDetails);
};
const getVaultMounts = async ({ actor, namespace }: { actor: OrgServiceActor; namespace: string }) => {
const connection = await getVaultConnectionForNamespace(actor, namespace, "view vault mounts");
- return listHCVaultMounts(connection, gatewayService, gatewayV2Service, namespace);
+
+ const gatewayDetails = await getGatewayDetails(connection);
+
+ return listHCVaultMounts(connection, gatewayService, gatewayV2Service, namespace, gatewayDetails);
};
const getVaultSecretPaths = async ({
@@ -454,7 +530,10 @@ export const externalMigrationServiceFactory = ({
mountPath: string;
}) => {
const connection = await getVaultConnectionForNamespace(actor, namespace, "view vault secret paths");
- return listHCVaultSecretPaths(namespace, connection, gatewayService, gatewayV2Service, mountPath);
+
+ const gatewayDetails = await getGatewayDetails(connection);
+
+ return listHCVaultSecretPaths(namespace, connection, gatewayService, gatewayV2Service, mountPath, gatewayDetails);
};
const importVaultSecrets = async ({