Skip to content

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

fix(pki): keep imperva links when a website is unreachable

bernie-g7b744adf0a08
diff --git a/backend/src/services/app-connection/imperva/imperva-connection-fns.ts b/backend/src/services/app-connection/imperva/imperva-connection-fns.ts
index 175d3e8169..998081cc35 100644
--- a/backend/src/services/app-connection/imperva/imperva-connection-fns.ts
+++ b/backend/src/services/app-connection/imperva/imperva-connection-fns.ts
@@ -1,4 +1,5 @@
import { AxiosError, AxiosRequestConfig } from "axios";
+import https from "https";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { getConfig } from "@app/lib/config/env";
@@ -9,7 +10,6 @@ import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { GatewayProxyProtocol } from "@app/lib/gateway-v2/types";
import { logger } from "@app/lib/logger";
import { safeRequest } from "@app/lib/validator";
-import { getSharedHttpsAgent } from "@app/lib/validator/safe-request";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
import { ImpervaConnectionMethod } from "./imperva-connection-enums";
@@ -169,6 +169,12 @@ export const withImpervaOperation = async <T>(
// withGatewayV2Proxy replaces the thrown error with one of its own, so Imperva's status and
// message are kept here and rethrown once the proxies have been torn down.
let impervaError: Error | undefined;
+ // Pooled per host for the operation, then destroyed: the proxy's port goes away with it, so a
+ // socket kept alive past the teardown points at nothing.
+ const agents: Record<string, https.Agent> = {
+ [IMPERVA_PROV_HOST]: new https.Agent({ keepAlive: true, servername: IMPERVA_PROV_HOST }),
+ [IMPERVA_REST_HOST]: new https.Agent({ keepAlive: true, servername: IMPERVA_REST_HOST })
+ };
try {
return await withGatewayV2Proxy(
@@ -181,7 +187,7 @@ export const withImpervaOperation = async <T>(
...requestCfg,
headers: { ...getImpervaAuthHeaders(credentials), ...requestCfg.headers, Host: host },
url: `https://localhost:${host === IMPERVA_PROV_HOST ? provPort : restPort}${path}`,
- httpsAgent: getSharedHttpsAgent({ servername: host, rejectUnauthorized: true }),
+ httpsAgent: agents[host],
timeout: IMPERVA_REQUEST_TIMEOUT_MS,
maxContentLength: IMPERVA_MAX_RESPONSE_BYTES,
maxBodyLength: IMPERVA_MAX_RESPONSE_BYTES
@@ -202,6 +208,8 @@ export const withImpervaOperation = async <T>(
} catch (error) {
if (impervaError) throw impervaError;
throw error;
+ } finally {
+ Object.values(agents).forEach((agent) => agent.destroy());
}
};
diff --git a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
index 8d1c086796..2c841cecf4 100644
--- a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
+++ b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.test.ts
@@ -106,10 +106,9 @@ vi.mock("@app/services/app-connection/imperva/imperva-connection-fns", async (im
describe("Imperva upload failures", () => {
test("explains what Imperva rejects when it answers a bare 400", async () => {
const { describeImpervaUploadFailure } = await import("./imperva-pki-sync-fns");
- const { AxiosError } = await import("axios");
+ const { ImpervaRequestError } = await import("@app/services/app-connection/imperva/imperva-connection-fns");
- const rejected = new AxiosError("Request failed");
- rejected.response = { status: 400, data: { message: "Invalid input" } } as never;
+ const rejected = new ImpervaRequestError({ message: "Invalid input", status: 400 });
expect(describeImpervaUploadFailure(rejected)).toBe(
"Invalid input. Imperva rejected the certificate. It has to cover every domain the website serves, and its chain has to be complete and ordered leaf first"
@@ -118,10 +117,12 @@ describe("Imperva upload failures", () => {
test("leaves a message that is not a rejection alone", async () => {
const { describeImpervaUploadFailure } = await import("./imperva-pki-sync-fns");
- const { AxiosError } = await import("axios");
+ const { ImpervaRequestError } = await import("@app/services/app-connection/imperva/imperva-connection-fns");
- const rateLimited = new AxiosError("Request failed");
- rateLimited.response = { status: 429, data: {} } as never;
+ const rateLimited = new ImpervaRequestError({
+ message: "Imperva rate limited the request, try again shortly",
+ status: 429
+ });
expect(describeImpervaUploadFailure(rateLimited)).toBe("Imperva rate limited the request, try again shortly");
});
diff --git a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
index 760c492aa2..c8ef7d44cb 100644
--- a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts
@@ -1,6 +1,4 @@
/* eslint-disable no-await-in-loop */
-import { AxiosError } from "axios";
-
import { TCertificateSyncs } from "@app/db/schemas";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
@@ -62,7 +60,7 @@ const IMPERVA_UPLOAD_REJECTION_HINT =
export const describeImpervaUploadFailure = (error: unknown): string => {
const message = extractImpervaErrorMessage(error, "Unknown error");
- const status = error instanceof AxiosError ? error.response?.status : undefined;
+ const status = error instanceof ImpervaRequestError ? error.status : undefined;
return status === 400 ? `${message}. ${IMPERVA_UPLOAD_REJECTION_HINT}` : message;
};
@@ -566,12 +564,12 @@ export const impervaPkiSyncFactory = ({
`Imperva PKI sync [syncId=${pkiSync.id}]: removed ${authType} custom certificate from site ${recordSiteLabel}`
);
} catch (error: unknown) {
- // A slot Imperva no longer has is already in the state the caller asked for. Decided on
- // the status, since the message is Imperva's own prose.
+ // Deleting an empty slot on a reachable site answers 200, so a 404 means the website
+ // is gone or the key lost access, and the certificate may still be installed.
if (error instanceof ImpervaRequestError && error.status === 404) {
- certificateIdsToClean.push(...certificateIds);
- // eslint-disable-next-line no-continue
- continue;
+ throw new PkiSyncError({
+ message: `Imperva website ${recordSiteLabel} is unknown to this connection, so its ${authType} custom certificate was left in place. Check that the website still exists and that the API key can reach its account.`
+ });
}
throw new PkiSyncError({
diff --git a/docs/documentation/platform/pki/applications/certificate-syncs/imperva.mdx b/docs/documentation/platform/pki/applications/certificate-syncs/imperva.mdx
index b1d8d904c1..c9622a3e0b 100644
--- a/docs/documentation/platform/pki/applications/certificate-syncs/imperva.mdx
+++ b/docs/documentation/platform/pki/applications/certificate-syncs/imperva.mdx
@@ -32,17 +32,17 @@ Imperva stores one custom certificate per website for each key type, so a sync t
2. Select the **Imperva** option.
3. Configure the **Destination**:
- - **Imperva Connection**: The connection to authenticate with.
- - **Website**: The website that receives the certificate, selected from the websites in the Imperva account.
+ - **Imperva Connection**: the connection to authenticate with
+ - **Website**: the website that receives the certificate, chosen from the websites in the Imperva account
4. Configure the **Sync Options**:
- - **Remove certificates from destination**: Remove the custom certificate from the website once this sync stops holding it. The website then falls back to whichever certificate Imperva would otherwise serve.
- - **Include root CA in certificate chain**: Leave this off. Imperva recommends uploading trusted certificates without the root for a faster TLS handshake.
- - **Auto-sync on change**: Automatically sync certificates when changes occur.
+ - **Remove certificates from destination**: remove the custom certificate from the website once this sync stops holding it
+ - **Include root CA in certificate chain**: leave this off, since Imperva recommends uploading trusted certificates without the root
+ - **Auto-sync on change**: sync certificates automatically when they change
5. Configure the **Details**:
- - **Name**: The name of your sync (slug-friendly).
- - **Description**: Optional description.
+ - **Name**: the name of your sync, slug-friendly
+ - **Description**: an optional description
6. Select the certificate to sync.
@@ -115,10 +115,10 @@ Imperva stores one custom certificate per website for each key type, so a sync t
Infisical uploads the certificate and its private key to the website as a custom certificate:
-- The leaf certificate and its intermediate CA certificates are sent as one chain, so visitors receive a complete trust path.
-- The key type is read from the certificate, and Imperva stores it in the matching RSA or ECDSA slot on the website.
-- A renewal overwrites the certificate in that slot, so the website keeps serving without a binding step.
-- Imperva serves a custom certificate to clients that support SNI. To cover clients that don't, add the domain to an Imperva SAN certificate as well.
+- The leaf certificate and its intermediate CA certificates are sent as one chain, giving visitors a complete trust path
+- The key type is read from the certificate, and Imperva stores it in the matching RSA or ECDSA slot
+- A renewal overwrites the certificate in that slot, and the website keeps serving without a binding step
+- Imperva serves a custom certificate to clients that support SNI, so cover other clients by adding the domain to an Imperva SAN certificate
## Removing certificates
diff --git a/docs/integrations/app-connections/imperva.mdx b/docs/integrations/app-connections/imperva.mdx
index c00a30056c..6520210b84 100644
--- a/docs/integrations/app-connections/imperva.mdx
+++ b/docs/integrations/app-connections/imperva.mdx
@@ -32,11 +32,11 @@ Infisical connects to Imperva Cloud WAF with an account API key, which it uses t
Select the **Imperva** option from the list of available connections.
</Step>
<Step title="Fill in Connection Details">
- - **API ID**: The API ID from the Imperva Cloud Security Console.
- - **API Key**: The API key paired with that ID.
- - **Account ID** (Optional): The numeric ID of the sub-account whose websites this connection manages. Leave it empty to use the account the key belongs to.
+ - **API ID**: the API ID from the Imperva Cloud Security Console
+ - **API Key**: the API key paired with that ID
+ - **Account ID** (Optional): the numeric ID of the sub-account whose websites this connection manages, left empty to use the account the key belongs to
- Optionally select a **Gateway** to send the Imperva API calls through an Infisical Gateway, so outbound traffic leaves from a network you control.
+ Optionally select a **Gateway** to send the Imperva API calls through an [Infisical Gateway](/documentation/platform/gateways/overview), which makes outbound traffic leave from a network you control.
Select **Connect to Imperva** to validate and save your connection.
</Step>