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
/* eslint-disable no-await-in-loop */
import { Knex } from "knex";
import net from "net";
import pLimit from "p-limit";
import { TCertificatesInsert, TPkiCertificateInstallationsInsert } from "@app/db/schemas";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { hasPostgresErrorCode } from "@app/lib/errors/postgres";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { GatewayProxyProtocol } from "@app/lib/gateway-v2/types";
import { logger } from "@app/lib/logger";
import { TCertificateBodyDALFactory } from "@app/services/certificate/certificate-body-dal";
import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";
import { CertStatus } from "@app/services/certificate/certificate-types";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TProjectDALFactory } from "@app/services/project/project-dal";
import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fns";
import { TTelemetryServiceFactory } from "@app/services/telemetry/telemetry-service";
import { TPkiCertificateInstallationCertDALFactory } from "./pki-certificate-installation-cert-dal";
import { TPkiCertificateInstallationDALFactory } from "./pki-certificate-installation-dal";
import { TPkiDiscoveryConfigDALFactory } from "./pki-discovery-config-dal";
import {
computeLocationFingerprint,
getPrivateIpRangeError,
resolveDomain,
resolveTargets,
scanEndpoint
} from "./pki-discovery-fns";
import { TPkiDiscoveryInstallationDALFactory } from "./pki-discovery-installation-dal";
import { TPkiDiscoveryScanHistoryDALFactory } from "./pki-discovery-scan-history-dal";
import {
DB_SHORT_VARCHAR_LIMIT,
DB_VARCHAR_LIMIT,
discardScanRunIfDeleted,
failScanRun,
finishScanRun,
sendScanCompletedTelemetry,
startScanRun,
truncateString
} from "./pki-discovery-scan-run-fns";
import {
CertificateSource,
PkiDiscoveryScanStatus,
PkiDiscoveryType,
PkiInstallationLocationType,
PkiInstallationType,
ScanEndpointFailureReason,
TNetworkTargetConfig,
TScanCertificateResult,
TScanEndpointResult
} from "./pki-discovery-types";
const SCAN_CONCURRENCY = 20;
const DEFAULT_SCAN_TIMEOUT = 10000;
const GATEWAY_CONSECUTIVE_FAILURE_LIMIT = 10;
type TExecuteScanDeps = {
pkiDiscoveryConfigDAL: TPkiDiscoveryConfigDALFactory;
pkiDiscoveryScanHistoryDAL: TPkiDiscoveryScanHistoryDALFactory;
pkiCertificateInstallationDAL: TPkiCertificateInstallationDALFactory;
pkiDiscoveryInstallationDAL: TPkiDiscoveryInstallationDALFactory;
pkiCertificateInstallationCertDAL: TPkiCertificateInstallationCertDALFactory;
certificateDAL: TCertificateDALFactory;
certificateBodyDAL: TCertificateBodyDALFactory;
projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">;
kmsService: Pick<TKmsServiceFactory, "encryptWithKmsKey" | "generateKmsKey">;
gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
gatewayV2DAL?: Pick<TGatewayV2DALFactory, "findById">;
gatewayPoolService?: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">;
telemetryService?: Pick<TTelemetryServiceFactory, "sendPostHogEvents">;
};
export const findOrCreateNetworkInstallation = async (
deps: Pick<TExecuteScanDeps, "pkiCertificateInstallationDAL">,
tx: Knex,
data: {
projectId: string;
locationDetails: TPkiCertificateInstallationsInsert["locationDetails"];
locationFingerprint: string;
name: string;
scanTime: Date;
}
) => {
const { pkiCertificateInstallationDAL } = deps;
const { projectId, locationDetails, locationFingerprint, name, scanTime } = data;
const existing = await pkiCertificateInstallationDAL.findByFingerprint(projectId, locationFingerprint, tx);
if (existing) {
await pkiCertificateInstallationDAL.updateById(existing.id, { lastSeenAt: scanTime }, tx);
return existing;
}
try {
return await tx.transaction((sp) =>
pkiCertificateInstallationDAL.create(
{
projectId,
locationType: PkiInstallationLocationType.Network,
locationDetails,
locationFingerprint,
name,
type: PkiInstallationType.Unknown,
lastSeenAt: scanTime
},
sp
)
);
} catch (error) {
if (!hasPostgresErrorCode(error, DatabaseErrorCode.UniqueViolation)) throw error;
const raced = await pkiCertificateInstallationDAL.findByFingerprint(projectId, locationFingerprint, tx);
if (!raced) throw error;
await pkiCertificateInstallationDAL.updateById(raced.id, { lastSeenAt: scanTime }, tx);
return raced;
}
};
export const executeScan = async (discoveryId: string, deps: TExecuteScanDeps): Promise<void> => {
const { pkiDiscoveryConfigDAL, projectDAL, kmsService, gatewayV2Service, gatewayPoolService, telemetryService } =
deps;
const startedAt = new Date();
let scanHistoryId: string | undefined;
try {
const discoveryConfig = await pkiDiscoveryConfigDAL.findById(discoveryId);
if (!discoveryConfig) {
logger.error({ discoveryId }, "Discovery config not found");
throw new Error(`Discovery config not found: ${discoveryId}`);
}
logger.info(
{ discoveryId, name: discoveryConfig.name, projectId: discoveryConfig.projectId },
"PKI discovery scan starting"
);
const certificateKmsKeyId = await getProjectKmsCertificateKeyId({
projectId: discoveryConfig.projectId,
projectDAL,
kmsService
});
const kmsEncryptor = await kmsService.encryptWithKmsKey({
kmsId: certificateKmsKeyId
});
scanHistoryId = await startScanRun(discoveryId, startedAt, deps);
const effectiveGatewayId = gatewayPoolService
? await gatewayPoolService.resolveEffectiveGatewayId({
gatewayId: discoveryConfig.gatewayId,
gatewayPoolId: discoveryConfig.gatewayPoolId
})
: (discoveryConfig.gatewayId ?? null);
let gatewayName: string | undefined;
if (effectiveGatewayId && deps.gatewayV2DAL) {
const gw = await deps.gatewayV2DAL.findById(effectiveGatewayId);
gatewayName = gw?.name;
}
const targetConfig = discoveryConfig.targetConfig as TNetworkTargetConfig;
const hasGateway = !!effectiveGatewayId;
// The gateway FK is ON DELETE SET NULL, so a config saved with private ranges can lose its gateway later.
if (getPrivateIpRangeError(targetConfig.ipRanges, hasGateway)) {
throw new Error(
"This job targets private IP addresses but has no gateway attached. Attach a gateway or gateway pool to scan private networks."
);
}
const targets = await resolveTargets(targetConfig, hasGateway);
const uniqueHosts = new Set(targets.map((t) => t.host)).size;
const uniquePorts = new Set(targets.map((t) => t.port)).size;
logger.info(
{
discoveryId,
totalTargets: targets.length,
uniqueHosts,
uniquePorts,
ipRanges: targetConfig.ipRanges?.length || 0,
domains: targetConfig.domains?.length || 0,
ports: targetConfig.ports
},
"Targets resolved, starting TLS scans"
);
const limit = pLimit(SCAN_CONCURRENCY);
let results: TScanEndpointResult[];
if (effectiveGatewayId && gatewayV2Service) {
logger.info(
{ discoveryId, gatewayId: effectiveGatewayId, gatewayPoolId: discoveryConfig.gatewayPoolId ?? undefined },
"Scanning via gateway proxy"
);
results = [];
let consecutiveGwFailures = 0;
let gatewayCircuitBroken = false;
// Gateway scans must be sequential - each target needs its own proxy connection
for (const target of targets) {
let targetSuccess = false;
try {
const targetGatewayDetails = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
gatewayId: effectiveGatewayId,
targetHost: target.host,
targetPort: target.port
});
if (!targetGatewayDetails) {
consecutiveGwFailures += 1;
results.push({
host: target.host,
port: target.port,
success: false,
failureReason: ScanEndpointFailureReason.ConnectionFailed,
error: "Gateway failed to establish proxy connection to target",
sniHostname: target.sniHostname
});
} else {
const scanResult = await withGatewayV2Proxy(
async (proxyPort) => {
const sniHostname = target.sniHostname || target.host;
return scanEndpoint("localhost", proxyPort, DEFAULT_SCAN_TIMEOUT, sniHostname);
},
{
...targetGatewayDetails,
protocol: GatewayProxyProtocol.Tcp
}
);
results.push({
...scanResult,
host: target.host,
port: target.port,
sniHostname: target.sniHostname
});
if (scanResult.failureReason !== ScanEndpointFailureReason.ConnectionFailed) {
targetSuccess = true;
}
}
} catch (error) {
consecutiveGwFailures += 1;
const gatewayError = error instanceof Error ? error.message : "Unknown gateway error";
results.push({
host: target.host,
port: target.port,
success: false,
failureReason: ScanEndpointFailureReason.ConnectionFailed,
error: `Gateway proxy error: ${gatewayError}`,
sniHostname: target.sniHostname
});
}
if (targetSuccess) {
consecutiveGwFailures = 0;
}
if (consecutiveGwFailures >= GATEWAY_CONSECUTIVE_FAILURE_LIMIT) {
logger.warn(
{ discoveryId, consecutiveGwFailures, scannedSoFar: results.length, totalTargets: targets.length },
"Gateway circuit breaker triggered — too many consecutive proxy failures, aborting scan"
);
gatewayCircuitBroken = true;
break;
}
}
if (gatewayCircuitBroken) {
const completedAt = new Date();
const lastFailedResult = results[results.length - 1];
const lastError = lastFailedResult?.error ? ` Last error: ${lastFailedResult.error}` : "";
const cbErrorMessage = `Gateway connection failed to reach target network. Check that the gateway is online and can reach the target network.${lastError}`;
await finishScanRun(
{
discoveryId,
scanHistoryId,
status: PkiDiscoveryScanStatus.Failed,
completedAt,
targetsScannedCount: results.length,
certificatesFoundCount: 0,
installationsFoundCount: 0,
errorMessage: cbErrorMessage
},
deps
);
return;
}
} else {
const scanPromises = targets.map((target) =>
limit(async (): Promise<TScanEndpointResult> => {
const result = await scanEndpoint(target.host, target.port, DEFAULT_SCAN_TIMEOUT, target.sniHostname);
return {
...result,
sniHostname: target.sniHostname
};
})
);
results = await Promise.all(scanPromises);
}
logger.info({ discoveryId, scannedCount: results.length }, "TLS scans completed, processing results");
if (await discardScanRunIfDeleted(discoveryId, scanHistoryId, deps)) return;
const uniqueCertificateIds = new Set<string>();
const uniqueInstallationIds = new Set<string>();
let successfulScans = 0;
let certParseErrors = 0;
const scanErrors: string[] = [];
const now = new Date();
const domainCertMap = new Map<string, Set<string>>();
for (const result of results) {
if (result.success && result.certificates && result.certificates.length > 0) {
successfulScans += 1;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const processedCertIds = await processEndpointResult(
result,
discoveryConfig.projectId,
discoveryId,
now,
deps,
kmsEncryptor,
effectiveGatewayId ?? undefined,
gatewayName,
discoveryConfig.gatewayPoolId ?? effectiveGatewayId ?? undefined
);
if (processedCertIds.installationId) {
uniqueInstallationIds.add(processedCertIds.installationId);
}
for (const certId of processedCertIds.certificateIds) {
uniqueCertificateIds.add(certId);
}
if (processedCertIds.sniHostname && processedCertIds.certificateIds.length > 0) {
const domainKey = `${processedCertIds.sniHostname}:${result.port}`;
if (!domainCertMap.has(domainKey)) {
domainCertMap.set(domainKey, new Set());
}
const certSet = domainCertMap.get(domainKey)!;
for (const certId of processedCertIds.certificateIds) {
certSet.add(certId);
}
}
} else if (!result.success && result.failureReason === ScanEndpointFailureReason.CertificateParseError) {
certParseErrors += 1;
scanErrors.push(`${result.host}:${result.port}: ${result.error}`);
logger.warn(
{ host: result.host, port: result.port, error: result.error },
"Failed to parse certificate from TLS endpoint"
);
}
}
if (domainCertMap.size > 0) {
for (const [domainKey, certIds] of domainCertMap) {
const [domain, portStr] = domainKey.split(":");
const port = parseInt(portStr, 10);
const domainLocationDetails = {
fqdn: domain,
port,
protocol: "tls",
...(gatewayName && { gatewayName })
};
const domainFingerprint = computeLocationFingerprint(
PkiInstallationLocationType.Network,
domainLocationDetails,
discoveryConfig.gatewayPoolId ?? effectiveGatewayId ?? undefined
);
const domainInstallationId = await deps.pkiDiscoveryConfigDAL.transaction(async (tx) => {
const domainInstallation = await findOrCreateNetworkInstallation(deps, tx, {
projectId: discoveryConfig.projectId,
locationDetails: domainLocationDetails,
locationFingerprint: domainFingerprint,
name: domain,
scanTime: now
});
const domainLink = await deps.pkiDiscoveryInstallationDAL.upsertLink(
discoveryId,
domainInstallation.id,
now,
tx
);
if (!domainLink) return null;
for (const certId of certIds) {
await deps.pkiCertificateInstallationCertDAL.upsertCertLink(
domainInstallation.id,
certId,
{ lastSeenAt: now },
tx
);
}
return domainInstallation.id;
});
if (!domainInstallationId) continue; // eslint-disable-line no-continue
uniqueInstallationIds.add(domainInstallationId);
}
}
logger.info(
{
discoveryId,
successfulScans,
certParseErrors,
totalTargets: targets.length,
certificatesFoundCount: uniqueCertificateIds.size,
installationsFoundCount: uniqueInstallationIds.size
},
"Finished processing scan results"
);
const completedAt = new Date();
const finalStatus = PkiDiscoveryScanStatus.Completed;
let errorMessage: string | null = null;
if (certParseErrors > 0) {
errorMessage = `${certParseErrors} certificate(s) could not be parsed: ${scanErrors.join("; ")}`;
}
await finishScanRun(
{
discoveryId,
scanHistoryId,
status: finalStatus,
completedAt,
targetsScannedCount: targets.length,
certificatesFoundCount: uniqueCertificateIds.size,
installationsFoundCount: uniqueInstallationIds.size,
errorMessage
},
deps
);
const durationMs = completedAt.getTime() - startedAt.getTime();
const durationSec = (durationMs / 1000).toFixed(1);
const project = telemetryService
? await projectDAL.findOne({ id: discoveryConfig.projectId }).catch(() => undefined)
: undefined;
if (project) {
await sendScanCompletedTelemetry(telemetryService, {
discoveryId,
discoveryType: PkiDiscoveryType.Network,
projectId: discoveryConfig.projectId,
orgId: project.orgId,
status: finalStatus,
certificatesFound: uniqueCertificateIds.size,
installationsFound: uniqueInstallationIds.size,
durationMs
});
}
logger.info(
{
discoveryId,
name: discoveryConfig.name,
status: finalStatus,
durationSec,
targetsScanned: targets.length,
successfulScans,
certParseErrors,
certificatesFound: uniqueCertificateIds.size,
installationsFound: uniqueInstallationIds.size
},
`PKI discovery scan completed in ${durationSec}s - found ${uniqueCertificateIds.size} certificate(s) at ${uniqueInstallationIds.size} installation(s)`
);
} catch (error) {
const durationMs = new Date().getTime() - startedAt.getTime();
const durationSec = (durationMs / 1000).toFixed(1);
logger.error(
{
discoveryId,
durationSec,
error: error instanceof Error ? error.message : "Unknown error"
},
`PKI discovery scan failed after ${durationSec}s`
);
await failScanRun(discoveryId, scanHistoryId, error, deps);
throw error;
}
};
export type TKmsEncryptor = (data: { plainText: Buffer }) => Promise<{ cipherTextBlob: Buffer }>;
const processEndpointResult = async (
result: TScanEndpointResult,
projectId: string,
discoveryId: string,
scanTime: Date,
deps: TExecuteScanDeps,
kmsEncryptor: TKmsEncryptor,
gatewayId?: string,
gatewayName?: string,
fingerprintGatewayKey?: string
): Promise<{ installationId?: string; certificateIds: string[]; sniHostname?: string }> => {
const { pkiDiscoveryInstallationDAL, pkiCertificateInstallationCertDAL, certificateDAL, certificateBodyDAL } = deps;
const certificateIds: string[] = [];
let ipAddress: string | null = null;
const isIp = net.isIP(result.host) !== 0;
if (isIp) {
ipAddress = result.host;
} else {
try {
const resolved = await resolveDomain(result.host);
if (resolved.length > 0) {
[ipAddress] = resolved;
}
} catch {
// do nothing
}
}
const locationDetails = {
ipAddress: ipAddress || result.host,
port: result.port,
protocol: "tls",
...(gatewayName && { gatewayName })
};
const locationFingerprint = computeLocationFingerprint(
PkiInstallationLocationType.Network,
locationDetails,
fingerprintGatewayKey ?? gatewayId
);
const displayName = ipAddress || result.host;
if (result.certificates) {
for (const certResult of result.certificates) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const certId = await processDiscoveredCertificate(
certResult,
projectId,
{ certificateDAL, certificateBodyDAL, kmsEncryptor },
{
discoveredBy: discoveryId,
host: result.host,
port: result.port,
sniHostname: result.sniHostname,
discoveredAt: scanTime.toISOString(),
issuerCommonName: certResult.issuerCommonName,
issuerOrganization: certResult.issuerOrganization
}
);
if (certId) {
certificateIds.push(certId);
}
}
}
const installationId = await deps.pkiDiscoveryConfigDAL.transaction(async (tx) => {
const installation = await findOrCreateNetworkInstallation(deps, tx, {
projectId,
locationDetails,
locationFingerprint,
name: displayName,
scanTime
});
const link = await pkiDiscoveryInstallationDAL.upsertLink(discoveryId, installation.id, scanTime, tx);
if (!link) {
return null;
}
for (const certId of certificateIds) {
await pkiCertificateInstallationCertDAL.upsertCertLink(installation.id, certId, { lastSeenAt: scanTime }, tx);
}
return installation.id;
});
if (!installationId) {
return { certificateIds: [] };
}
return { installationId, certificateIds, sniHostname: result.sniHostname };
};
type TProcessDiscoveredCertDeps = {
certificateDAL: TCertificateDALFactory;
certificateBodyDAL: TCertificateBodyDALFactory;
kmsEncryptor: TKmsEncryptor;
};
export const processDiscoveredCertificate = async (
certResult: TScanCertificateResult,
projectId: string,
deps: TProcessDiscoveredCertDeps,
discoveryMetadata?: Record<string, unknown>
): Promise<string | null> => {
const { certificateDAL, certificateBodyDAL, kmsEncryptor } = deps;
try {
const existingCerts = await certificateDAL.find({
projectId,
fingerprintSha256: certResult.fingerprint
});
if (existingCerts.length > 0) {
return existingCerts[0].id;
}
const friendlyName =
truncateString(certResult.commonName, DB_SHORT_VARCHAR_LIMIT) ||
`Discovered: ${certResult.fingerprint.substring(0, 16)}`;
const serialNumber = certResult.serialNumber || certResult.fingerprint.substring(0, 40);
// Deliberately not gated on the certificate quota: this is a background scan, and every PKI
// license gate blocks creation only. A scan can push an org past its cap, after which new
// issuance is refused until it upgrades or cleans up.
const certData: Omit<TCertificatesInsert, "quotaKey"> = {
projectId,
status: CertStatus.ACTIVE,
serialNumber: truncateString(serialNumber, DB_SHORT_VARCHAR_LIMIT) || "unknown",
friendlyName,
commonName: truncateString(certResult.commonName, DB_SHORT_VARCHAR_LIMIT) || "Unknown",
altNames: truncateString(certResult.altNames, DB_VARCHAR_LIMIT),
notBefore: certResult.notBefore,
notAfter: certResult.notAfter,
fingerprintSha256: certResult.fingerprint,
fingerprintSha1: certResult.fingerprintSha1,
subjectOrganization: truncateString(certResult.subjectOrganization, DB_SHORT_VARCHAR_LIMIT),
subjectOrganizationalUnit: truncateString(certResult.subjectOrganizationalUnit, DB_SHORT_VARCHAR_LIMIT),
subjectCountry: truncateString(certResult.subjectCountry, DB_SHORT_VARCHAR_LIMIT),
subjectState: truncateString(certResult.subjectState, DB_SHORT_VARCHAR_LIMIT),
subjectLocality: truncateString(certResult.subjectLocality, DB_SHORT_VARCHAR_LIMIT),
keyAlgorithm: truncateString(certResult.keyAlgorithm, DB_SHORT_VARCHAR_LIMIT),
signatureAlgorithm: truncateString(certResult.signatureAlgorithm, DB_SHORT_VARCHAR_LIMIT),
keyUsages: certResult.keyUsages,
extendedKeyUsages: certResult.extendedKeyUsages,
isCA: certResult.isCA,
pathLength: certResult.pathLength,
source: CertificateSource.Discovered,
discoveryMetadata: discoveryMetadata || null
};
let encryptedCertificate: Buffer | undefined;
let encryptedCertificateChain: Buffer | undefined;
if (certResult.pemChain && certResult.pemChain.length > 0) {
const certificatePem = certResult.pemChain[0];
const chainPem = certResult.pemChain.slice(1).join("\n");
({ cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
plainText: Buffer.from(certificatePem)
}));
if (chainPem) {
({ cipherTextBlob: encryptedCertificateChain } = await kmsEncryptor({
plainText: Buffer.from(chainPem)
}));
}
}
const newCert = await certificateDAL.transaction(async (tx) => {
const cert = await certificateDAL.create(certData, tx);
if (encryptedCertificate) {
await certificateBodyDAL.create(
{
certId: cert.id,
encryptedCertificate,
encryptedCertificateChain
},
tx
);
}
return cert;
});
return newCert.id;
} catch (error) {
const dbError = error as { code?: string };
// PG unique constraint violation — another concurrent scan already inserted this certificate
if (dbError.code === "23505") {
const existingCerts = await certificateDAL.find({
projectId,
fingerprintSha256: certResult.fingerprint
});
if (existingCerts.length > 0) {
logger.debug({ fingerprint: certResult.fingerprint }, "Certificate already exists (concurrent insert)");
return existingCerts[0].id;
}
}
logger.error({ error, fingerprint: certResult.fingerprint, projectId }, "Failed to process discovered certificate");
return null;
}
};