Skip to content

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

Fix missing gateway-v2 handling on venafi tpp

Carlos Monastyrski3834d5e7d21e
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 68327ec00b..4ae15fd22a 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -2485,7 +2485,9 @@ export const registerRoutes = async (
pkiSyncDAL,
pkiSyncQueue,
certificateRequestDAL,
- resourceMetadataDAL
+ resourceMetadataDAL,
+ gatewayService,
+ gatewayV2Service
});
const certificateEstService = certificateEstServiceFactory({
@@ -2568,7 +2570,9 @@ export const registerRoutes = async (
certificateRequestService,
certificateRequestDAL,
resourceMetadataDAL,
- pkiAlertV2Queue
+ pkiAlertV2Queue,
+ gatewayService,
+ gatewayV2Service
});
const certificateApprovalService = certificateApprovalServiceFactory({
diff --git a/backend/src/services/app-connection/venafi-tpp/venafi-tpp-connection-fns.ts b/backend/src/services/app-connection/venafi-tpp/venafi-tpp-connection-fns.ts
index bdeee09456..bdb4d9946a 100644
--- a/backend/src/services/app-connection/venafi-tpp/venafi-tpp-connection-fns.ts
+++ b/backend/src/services/app-connection/venafi-tpp/venafi-tpp-connection-fns.ts
@@ -1,8 +1,14 @@
-import { AxiosError } from "axios";
+import { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
+import https from "https";
import RE2 from "re2";
+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 { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
+import { GatewayProxyProtocol, withGatewayProxy } from "@app/lib/gateway";
+import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { logger } from "@app/lib/logger";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator/validate-url";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
@@ -34,25 +40,146 @@ const normalizeTppUrl = (tppUrl: string): string => {
return tppUrl.replace(new RE2("\\/+$"), "");
};
+/**
+ * Issues an HTTP request to the Venafi TPP server, routing through the configured
+ * gateway transport when the connection has a gatewayId set.
+ */
+export const requestWithVenafiTppGateway = async <T>(
+ appConnection: { gatewayId?: string | null },
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
+ requestConfig: AxiosRequestConfig
+): Promise<AxiosResponse<T>> => {
+ const { gatewayId } = appConnection;
+
+ const url = new URL(requestConfig.url as string);
+ await blockLocalAndPrivateIpAddresses(url.toString(), Boolean(gatewayId));
+
+ if (!gatewayId) {
+ return request.request(requestConfig);
+ }
+
+ 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 gatewayConnectionDetailsV2 = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
+ gatewayId,
+ targetHost,
+ targetPort
+ });
+
+ if (gatewayConnectionDetailsV2) {
+ return withGatewayV2Proxy(
+ async (proxyPort) => {
+ const isHttps = url.protocol === "https:";
+ url.host = `localhost:${proxyPort}`;
+
+ const finalRequestConfig: AxiosRequestConfig = {
+ ...requestConfig,
+ url: url.toString(),
+ headers: {
+ ...requestConfig.headers,
+ Host: targetHost
+ },
+ ...(isHttps && {
+ httpsAgent: new https.Agent({
+ servername: targetHost
+ })
+ })
+ };
+
+ try {
+ return await request.request(finalRequestConfig);
+ } catch (error) {
+ if (error instanceof AxiosError) {
+ logger.error(
+ { message: error.message, data: (error.response as undefined | { data: unknown })?.data },
+ "Error during Venafi TPP gateway request:"
+ );
+ }
+ throw error;
+ }
+ },
+ {
+ protocol: GatewayProxyProtocol.Tcp,
+ relayHost: gatewayConnectionDetailsV2.relayHost,
+ gateway: gatewayConnectionDetailsV2.gateway,
+ relay: gatewayConnectionDetailsV2.relay
+ }
+ );
+ }
+
+ const gatewayConnectionDetailsV1 = await gatewayService.fnGetGatewayClientTlsByGatewayId(gatewayId);
+
+ return withGatewayProxy(
+ async (proxyPort) => {
+ const httpsAgent = new https.Agent({
+ servername: targetHost
+ });
+
+ url.protocol = "https:";
+ url.host = `localhost:${proxyPort}`;
+
+ const finalRequestConfig: AxiosRequestConfig = {
+ ...requestConfig,
+ url: url.toString(),
+ httpsAgent,
+ headers: {
+ ...requestConfig.headers,
+ Host: targetHost
+ }
+ };
+
+ try {
+ return await request.request(finalRequestConfig);
+ } catch (error) {
+ if (error instanceof AxiosError) {
+ logger.error(
+ { message: error.message, data: (error.response as undefined | { data: unknown })?.data },
+ "Error during Venafi TPP gateway request:"
+ );
+ }
+ throw error;
+ }
+ },
+ {
+ relayDetails: gatewayConnectionDetailsV1,
+ protocol: GatewayProxyProtocol.Tcp,
+ targetHost,
+ targetPort
+ }
+ );
+};
+
/**
* Authenticates with Venafi TPP via OAuth and returns an access token.
*/
-export const authenticateVenafiTpp = async ({
- tppUrl,
- clientId,
- username,
- password
-}: TVenafiTppCredentials): Promise<TVenafiTppOAuthResponse> => {
+export const authenticateVenafiTpp = async (
+ { credentials, ...appConnection }: { gatewayId?: string | null; credentials: TVenafiTppCredentials },
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
+): Promise<TVenafiTppOAuthResponse> => {
+ const { tppUrl, clientId, username, password } = credentials;
const baseUrl = normalizeTppUrl(tppUrl);
logger.info("Venafi TPP: Authenticating via OAuth token endpoint");
- const { data } = await request.post<TVenafiTppOAuthResponse>(`${baseUrl}/vedauth/authorize/oauth`, {
- client_id: clientId,
- username,
- password,
- scope: "certificate:manage,discover,revoke;configuration"
- });
+ const { data } = await requestWithVenafiTppGateway<TVenafiTppOAuthResponse>(
+ appConnection,
+ gatewayService,
+ gatewayV2Service,
+ {
+ method: "POST",
+ url: `${baseUrl}/vedauth/authorize/oauth`,
+ data: {
+ client_id: clientId,
+ username,
+ password,
+ scope: "certificate:manage,discover,revoke;configuration"
+ }
+ }
+ );
logger.info("Venafi TPP: Successfully obtained access token");
@@ -62,11 +189,18 @@ export const authenticateVenafiTpp = async ({
/**
* Revokes a Venafi TPP access token.
*/
-export const revokeVenafiTppToken = async (tppUrl: string, accessToken: string): Promise<void> => {
- const baseUrl = normalizeTppUrl(tppUrl);
+export const revokeVenafiTppToken = async (
+ { credentials, ...appConnection }: { gatewayId?: string | null; credentials: { tppUrl: string } },
+ accessToken: string,
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
+): Promise<void> => {
+ const baseUrl = normalizeTppUrl(credentials.tppUrl);
try {
- await request.get(`${baseUrl}/vedauth/revoke/token`, {
+ await requestWithVenafiTppGateway(appConnection, gatewayService, gatewayV2Service, {
+ method: "GET",
+ url: `${baseUrl}/vedauth/revoke/token`,
headers: {
Authorization: `Bearer ${accessToken}`
}
@@ -90,23 +224,23 @@ export const getVenafiTppConnectionListItem = () => {
};
};
-export const validateVenafiTppConnectionCredentials = async (config: TVenafiTppConnectionConfig) => {
- const { tppUrl, clientId, username, password } = config.credentials as TVenafiTppCredentials;
- const hasGateway = Boolean(config.gatewayId);
-
- const normalizedUrl = normalizeTppUrl(tppUrl);
- try {
- await blockLocalAndPrivateIpAddresses(normalizedUrl, hasGateway);
- } catch (error) {
- logger.error({ tppUrl, hasGateway }, "Venafi TPP: URL blocked by SSRF protection");
- throw error;
- }
+export const validateVenafiTppConnectionCredentials = async (
+ config: TVenafiTppConnectionConfig,
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
+) => {
+ const credentials = config.credentials as TVenafiTppCredentials;
+ const { tppUrl } = credentials;
logger.info({ tppUrl }, "Venafi TPP: Validating connection credentials");
let accessToken: string | undefined;
try {
- const authResponse = await authenticateVenafiTpp({ tppUrl, clientId, username, password });
+ const authResponse = await authenticateVenafiTpp(
+ { gatewayId: config.gatewayId, credentials },
+ gatewayService,
+ gatewayV2Service
+ );
accessToken = authResponse.access_token;
logger.info(
@@ -142,7 +276,12 @@ export const validateVenafiTppConnectionCredentials = async (config: TVenafiTppC
});
} finally {
if (accessToken) {
- await revokeVenafiTppToken(tppUrl, accessToken);
+ await revokeVenafiTppToken(
+ { gatewayId: config.gatewayId, credentials },
+ accessToken,
+ gatewayService,
+ gatewayV2Service
+ );
}
}
diff --git a/backend/src/services/certificate-authority/certificate-authority-service.ts b/backend/src/services/certificate-authority/certificate-authority-service.ts
index 7afaa78ae0..037ad1a339 100644
--- a/backend/src/services/certificate-authority/certificate-authority-service.ts
+++ b/backend/src/services/certificate-authority/certificate-authority-service.ts
@@ -1,6 +1,8 @@
import { ForbiddenError, subject } from "@casl/ability";
import { ActionProjectType, TableName } from "@app/db/schemas";
+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 {
ProjectPermissionCertificateActions,
@@ -122,6 +124,8 @@ type TCertificateAuthorityServiceFactoryDep = {
"findById" | "updateById" | "updateStatus" | "attachCertificate"
>;
resourceMetadataDAL: Pick<TResourceMetadataDALFactory, "find" | "insertMany">;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
};
export type TCertificateAuthorityServiceFactory = ReturnType<typeof certificateAuthorityServiceFactory>;
@@ -143,7 +147,9 @@ export const certificateAuthorityServiceFactory = ({
pkiSyncQueue,
certificateProfileDAL,
certificateRequestDAL,
- resourceMetadataDAL
+ resourceMetadataDAL,
+ gatewayService,
+ gatewayV2Service
}: TCertificateAuthorityServiceFactoryDep) => {
const acmeFns = AcmeCertificateAuthorityFns({
appConnectionDAL,
@@ -187,7 +193,9 @@ export const certificateAuthorityServiceFactory = ({
certificateSecretDAL,
kmsService,
projectDAL,
- certificateProfileDAL
+ certificateProfileDAL,
+ gatewayService,
+ gatewayV2Service
});
const awsPcaFns = AwsPcaCertificateAuthorityFns({
diff --git a/backend/src/services/certificate-authority/certificate-issuance-queue.ts b/backend/src/services/certificate-authority/certificate-issuance-queue.ts
index 6f4d549627..8d092d9ea8 100644
--- a/backend/src/services/certificate-authority/certificate-issuance-queue.ts
+++ b/backend/src/services/certificate-authority/certificate-issuance-queue.ts
@@ -1,6 +1,8 @@
import acme from "acme-client";
import { UnrecoverableError } from "bullmq";
+import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { crypto } from "@app/lib/crypto/cryptography";
import { NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
@@ -121,6 +123,8 @@ type TCertificateIssuanceQueueFactoryDep = {
certificateRequestDAL?: Pick<TCertificateRequestDALFactory, "updateById">;
resourceMetadataDAL: Pick<TResourceMetadataDALFactory, "find" | "insertMany">;
pkiAlertV2Queue?: Pick<TPkiAlertV2QueueServiceFactory, "queueCertificateEvent">;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
};
export type TCertificateIssuanceQueueFactory = ReturnType<typeof certificateIssuanceQueueFactory>;
@@ -143,7 +147,9 @@ export const certificateIssuanceQueueFactory = ({
certificateRequestService,
certificateRequestDAL,
resourceMetadataDAL,
- pkiAlertV2Queue
+ pkiAlertV2Queue,
+ gatewayService,
+ gatewayV2Service
}: TCertificateIssuanceQueueFactoryDep) => {
const acmeFns = AcmeCertificateAuthorityFns({
appConnectionDAL,
@@ -225,7 +231,9 @@ export const certificateIssuanceQueueFactory = ({
certificateSecretDAL,
kmsService,
projectDAL,
- certificateProfileDAL
+ certificateProfileDAL,
+ gatewayService,
+ gatewayV2Service
});
/**
diff --git a/backend/src/services/certificate-authority/venafi-tpp/venafi-tpp-certificate-authority-fns.ts b/backend/src/services/certificate-authority/venafi-tpp/venafi-tpp-certificate-authority-fns.ts
index 925b86c1a7..acb6ab81ae 100644
--- a/backend/src/services/certificate-authority/venafi-tpp/venafi-tpp-certificate-authority-fns.ts
+++ b/backend/src/services/certificate-authority/venafi-tpp/venafi-tpp-certificate-authority-fns.ts
@@ -4,7 +4,8 @@ import { AxiosError } from "axios";
import RE2 from "re2";
import { TableName } from "@app/db/schemas";
-import { request } from "@app/lib/config/request";
+import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { ProcessedPermissionRules } from "@app/lib/knex/permission-filter-utils";
@@ -17,6 +18,7 @@ import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-c
import {
authenticateVenafiTpp,
getVenafiTppHeaders,
+ requestWithVenafiTppGateway,
revokeVenafiTppToken
} from "@app/services/app-connection/venafi-tpp/venafi-tpp-connection-fns";
import { TCertificateBodyDALFactory } from "@app/services/certificate/certificate-body-dal";
@@ -152,7 +154,7 @@ const parseSanToVenafiFormat = (san: string): { Type: number; Name: string } =>
return { Type: VENAFI_SAN_TYPE_BY_NORMALIZED[type], Name: value };
};
-const getVenafiTppConnectionCredentials = async (
+const getVenafiTppConnection = async (
appConnectionId: string,
appConnectionDAL: Pick<TAppConnectionDALFactory, "findById">,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
@@ -190,18 +192,22 @@ const getVenafiTppConnectionCredentials = async (
password: string;
};
- return credentials;
+ return { credentials, gatewayId: appConnection.gatewayId ?? null };
};
const submitCertificateToTpp = async ({
+ appConnection,
baseUrl,
accessToken,
policyDN,
csrPem,
objectName,
altNames,
- workToDoTimeout = 30
+ workToDoTimeout = 30,
+ gatewayService,
+ gatewayV2Service
}: {
+ appConnection: { gatewayId?: string | null };
baseUrl: string;
accessToken: string;
policyDN: string;
@@ -209,6 +215,8 @@ const submitCertificateToTpp = async ({
objectName: string;
altNames?: string[];
workToDoTimeout?: number;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
}): Promise<TVenafiTppRequestResponse> => {
const requestBody: TVenafiTppCertificateRequest = {
PolicyDN: policyDN,
@@ -232,10 +240,14 @@ const submitCertificateToTpp = async ({
"Venafi TPP: Submitting certificate request"
);
- const { data, status } = await request.post<TVenafiTppRequestResponse>(
- `${baseUrl}/vedsdk/certificates/request`,
- requestBody,
+ const { data, status } = await requestWithVenafiTppGateway<TVenafiTppRequestResponse>(
+ appConnection,
+ gatewayService,
+ gatewayV2Service,
{
+ method: "POST",
+ url: `${baseUrl}/vedsdk/certificates/request`,
+ data: requestBody,
headers: getVenafiTppHeaders(accessToken),
validateStatus: (s) => s === 200 || s === 202
}
@@ -255,28 +267,38 @@ const submitCertificateToTpp = async ({
};
const retrieveCertificateFromTpp = async ({
+ appConnection,
baseUrl,
accessToken,
certificateDN,
- includeChain = true
+ includeChain = true,
+ gatewayService,
+ gatewayV2Service
}: {
+ appConnection: { gatewayId?: string | null };
baseUrl: string;
accessToken: string;
certificateDN: string;
includeChain?: boolean;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
}): Promise<{ certificate: string; chain: string }> => {
logger.info({ certificateDN, includeChain }, "Venafi TPP: Retrieving certificate");
- const { data, status } = await request.post<TVenafiTppRetrieveResponse>(
- `${baseUrl}/vedsdk/certificates/retrieve`,
- {
- CertificateDN: certificateDN,
- Format: "Base64",
- IncludeChain: includeChain,
- IncludePrivateKey: false,
- RootFirstOrder: false
- },
+ const { data, status } = await requestWithVenafiTppGateway<TVenafiTppRetrieveResponse>(
+ appConnection,
+ gatewayService,
+ gatewayV2Service,
{
+ method: "POST",
+ url: `${baseUrl}/vedsdk/certificates/retrieve`,
+ data: {
+ CertificateDN: certificateDN,
+ Format: "Base64",
+ IncludeChain: includeChain,
+ IncludePrivateKey: false,
+ RootFirstOrder: false
+ },
headers: getVenafiTppHeaders(accessToken),
validateStatus: (s) => s === 200 || s === 202
}
@@ -368,6 +390,8 @@ type TVenafiTppCertificateAuthorityFnsDeps = {
kmsService: Pick<TKmsServiceFactory, "encryptWithKmsKey" | "generateKmsKey" | "createCipherPairWithDataKey">;
projectDAL: Pick<TProjectDALFactory, "findById" | "findOne" | "updateById" | "transaction">;
certificateProfileDAL?: Pick<TCertificateProfileDALFactory, "findById">;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
};
export const VenafiTppCertificateAuthorityFns = ({
@@ -380,7 +404,9 @@ export const VenafiTppCertificateAuthorityFns = ({
certificateSecretDAL,
kmsService,
projectDAL,
- certificateProfileDAL
+ certificateProfileDAL,
+ gatewayService,
+ gatewayV2Service
}: TVenafiTppCertificateAuthorityFnsDeps) => {
const createCertificateAuthority = async ({
name,
@@ -627,13 +653,14 @@ export const VenafiTppCertificateAuthorityFns = ({
kmsId: certificateManagerKmsId
});
- const credentials = await getVenafiTppConnectionCredentials(
+ const { credentials, gatewayId } = await getVenafiTppConnection(
venafiCa.configuration.appConnectionId,
appConnectionDAL,
kmsService
);
const baseUrl = normalizeTppUrl(credentials.tppUrl);
+ const appConnection = { gatewayId };
let csrPem: string;
let skLeaf: string | undefined;
@@ -713,22 +740,20 @@ export const VenafiTppCertificateAuthorityFns = ({
let accessToken: string | undefined;
try {
- const authResponse = await authenticateVenafiTpp({
- tppUrl: credentials.tppUrl,
- clientId: credentials.clientId,
- username: credentials.username,
- password: credentials.password
- });
+ const authResponse = await authenticateVenafiTpp({ gatewayId, credentials }, gatewayService, gatewayV2Service);
accessToken = authResponse.access_token;
const requestResponse = await submitCertificateToTpp({
+ appConnection,
baseUrl,
accessToken,
policyDN: venafiCa.configuration.policyDN,
csrPem,
objectName: commonName,
altNames,
- workToDoTimeout: 60
+ workToDoTimeout: 60,
+ gatewayService,
+ gatewayV2Service
});
let certificateResult: { certificate: string; chain: string } | undefined;
@@ -755,10 +780,13 @@ export const VenafiTppCertificateAuthorityFns = ({
for (let attempt = 0; attempt < maxRetries; attempt += 1) {
try {
certificateResult = await retrieveCertificateFromTpp({
+ appConnection,
baseUrl,
accessToken,
certificateDN: requestResponse.CertificateDN,
- includeChain: true
+ includeChain: true,
+ gatewayService,
+ gatewayV2Service
});
break;
} catch (error) {
@@ -961,7 +989,7 @@ export const VenafiTppCertificateAuthorityFns = ({
});
} finally {
if (accessToken) {
- await revokeVenafiTppToken(baseUrl, accessToken);
+ await revokeVenafiTppToken({ gatewayId, credentials }, accessToken, gatewayService, gatewayV2Service);
}
}
};