Skip to content

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

Merge remote-tracking branch 'origin/main' into PKI-367

Carlos Monastyrski6628f539e463
diff --cc backend/src/server/routes/v1/pki-sync-routers/gcp-certificate-manager-pki-sync-router.ts
index 41c3fa5185,0000000000..2a71fb068a
mode 100644,000000..100644
--- a/backend/src/server/routes/v1/pki-sync-routers/gcp-certificate-manager-pki-sync-router.ts
+++ b/backend/src/server/routes/v1/pki-sync-routers/gcp-certificate-manager-pki-sync-router.ts
@@@ -1,26 -1,0 +1,27 @@@
+import {
+ CreateGcpCertificateManagerPkiSyncSchema,
+ GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION,
+ GcpCertificateManagerPkiSyncSchema,
+ UpdateGcpCertificateManagerPkiSyncSchema
+} from "@app/services/pki-sync/gcp-certificate-manager";
+import { PkiSync } from "@app/services/pki-sync/pki-sync-enums";
+
+import { registerSyncPkiEndpoints } from "./pki-sync-endpoints";
+
+export const registerGcpCertificateManagerPkiSyncRouter = async (
+ server: FastifyZodProvider,
+ enableOperationId: boolean = true
+) =>
+ registerSyncPkiEndpoints({
+ destination: PkiSync.GcpCertificateManager,
+ server,
+ responseSchema: GcpCertificateManagerPkiSyncSchema,
+ createSchema: CreateGcpCertificateManagerPkiSyncSchema,
+ updateSchema: UpdateGcpCertificateManagerPkiSyncSchema,
+ syncOptions: {
+ canImportCertificates: GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION.canImportCertificates,
- canRemoveCertificates: GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION.canRemoveCertificates
++ canRemoveCertificates: GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION.canRemoveCertificates,
++ canRunHealthCheckCommand: GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION.canRunHealthCheckCommand
+ },
+ enableOperationId
+ });
diff --cc backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-constants.ts
index 319e511fa7,0000000000..6f98d7f32d
mode 100644,000000..100644
--- a/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-constants.ts
+++ b/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-constants.ts
@@@ -1,93 -1,0 +1,94 @@@
+import RE2 from "re2";
+
+import { AppConnection } from "@app/services/app-connection/app-connection-enums";
+import { RateLimitConfig } from "@app/services/connection-queue";
+import { PkiSync } from "@app/services/pki-sync/pki-sync-enums";
+
+import { GcpCertificateManagerAction, GcpCertificateManagerScope } from "./gcp-certificate-manager-pki-sync-enums";
+
+const GCP_CERTIFICATE_MANAGER_NAME_REGEX_SOURCE = "^[a-z][a-z0-9-]{0,62}$";
+
+export const GCP_CERTIFICATE_MANAGER_NAMING = {
+ NAME_PATTERN: new RE2(GCP_CERTIFICATE_MANAGER_NAME_REGEX_SOURCE),
+ FORBIDDEN_CHARACTERS: "!@#$%^&*()+=[]{}|\\:;\"'<>,.?/~`_ ",
+ MAX_NAME_LENGTH: 63,
+ MIN_NAME_LENGTH: 1,
+ ALLOWED_CHARACTER_PATTERN: GCP_CERTIFICATE_MANAGER_NAME_REGEX_SOURCE
+} as const;
+
+export const GCP_SCOPE_API_VALUES: Record<GcpCertificateManagerScope, string> = {
+ [GcpCertificateManagerScope.Default]: "DEFAULT",
+ [GcpCertificateManagerScope.EdgeCache]: "EDGE_CACHE",
+ [GcpCertificateManagerScope.AllRegions]: "ALL_REGIONS",
+ [GcpCertificateManagerScope.ClientAuth]: "CLIENT_AUTH"
+};
+
+export const GCP_PRIMARY_MATCHER = "PRIMARY";
+
+export const GCP_MAX_CERTIFICATES_PER_MAP_ENTRY = 4;
+
+export const GCP_NAME_HASH_LENGTH = 8;
+
+export const GCP_MAX_LIST_PAGES = 20;
+
+export const GCP_NAME_LEADING_LETTER_PATTERN = new RE2("^[a-z]");
+
+export const GCP_LOCATION_PATTERN = new RE2("^[a-z0-9-]{1,32}$");
+
+export const GCP_CERTIFICATE_MAP_NAME_PATTERN = new RE2(GCP_CERTIFICATE_MANAGER_NAME_REGEX_SOURCE);
+
+export const GCP_HOSTNAME_PATTERN = new RE2("^(\\*\\.)?([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z]{2,63}$", "i");
+
+export const GCP_CERTIFICATE_MANAGER_SUPPORTED_KEY_ALGORITHMS = [
+ "RSA-2048",
+ "RSA-3072",
+ "RSA-4096",
+ "ECDSA-P256",
+ "ECDSA-P384"
+] as const;
+
+// GCP allows 64 labels per resource and the sync writes two of its own.
+export const GCP_MAX_USER_LABELS = 62;
+
+export const GCP_LABEL_KEY_PATTERN = new RE2("^[a-z][a-z0-9_-]{0,62}$");
+export const GCP_LABEL_VALUE_PATTERN = new RE2("^[a-z0-9_-]{0,63}$");
+
+export const GCP_MANAGED_BY_LABEL_KEY = "managed-by";
+export const GCP_MANAGED_BY_LABEL_VALUE = "infisical";
+export const GCP_CERTIFICATE_ID_LABEL_KEY = "infisical-certificate-id";
+
+export const GCP_RESERVED_LABEL_KEYS = [GCP_MANAGED_BY_LABEL_KEY, GCP_CERTIFICATE_ID_LABEL_KEY] as const;
+
+// Certificate Manager allows 300 write requests per minute per project.
+export const GCP_RATE_LIMIT_CONFIG: RateLimitConfig = {
+ MAX_CONCURRENT_REQUESTS: 5,
+ BASE_DELAY: 1000,
+ MAX_DELAY: 30000,
+ MAX_RETRIES: 3,
+ RATE_LIMIT_STATUS_CODES: [429, 503]
+};
+
+export const GCP_OPERATION_POLL_TIMEOUT_MS = 120_000;
+export const GCP_OPERATION_POLL_BASE_DELAY_MS = 1000;
+export const GCP_OPERATION_POLL_MAX_DELAY_MS = 5000;
+
+export const GCP_CERTIFICATE_MANAGER_PKI_SYNC_LIST_OPTION = {
+ name: "GCP Certificate Manager" as const,
+ connection: AppConnection.GCP,
+ destination: PkiSync.GcpCertificateManager,
+ canImportCertificates: false,
+ canRemoveCertificates: true,
+ canRunPostSyncCommand: false,
++ canRunHealthCheckCommand: false,
+ defaultCertificateNameSchema: "infisical-{{certificateId}}",
+ forbiddenCharacters: GCP_CERTIFICATE_MANAGER_NAMING.FORBIDDEN_CHARACTERS,
+ allowedCharacterPattern: GCP_CERTIFICATE_MANAGER_NAMING.ALLOWED_CHARACTER_PATTERN,
+ maxCertificateNameLength: GCP_CERTIFICATE_MANAGER_NAMING.MAX_NAME_LENGTH,
+ minCertificateNameLength: GCP_CERTIFICATE_MANAGER_NAMING.MIN_NAME_LENGTH
+} as const;
+
+export const gcpCertificatePermission = (action: GcpCertificateManagerAction) => `certificatemanager.certs.${action}`;
+export const gcpCertificateMapPermission = (action: GcpCertificateManagerAction) =>
+ `certificatemanager.certmaps.${action}`;
+export const gcpCertificateMapEntryPermission = (action: GcpCertificateManagerAction) =>
+ `certificatemanager.certmapentries.${action}`;
diff --cc backend/src/services/pki-sync/pki-sync-queue.ts
index 84350d1558,d5c0d5fffc..0ca06e8332
--- a/backend/src/services/pki-sync/pki-sync-queue.ts
+++ b/backend/src/services/pki-sync/pki-sync-queue.ts
@@@ -435,52 -242,22 +242,23 @@@ export const pkiSyncQueueFactory = (
let isSynced = false;
let certSyncFailureCount = 0;
let syncMessage: string | null = null;
+ let partialFailure = false;
let postSyncCommandResult: TPostSyncCommandResult | undefined;
+ let healthCheckResult: THealthCheckCommandResult | undefined;
let isFinalAttempt = job.attemptsStarted === job.opts.attempts;
- const configuredPostSyncCommand = (pkiSync.syncOptions as { postSyncCommand?: string } | undefined)
- ?.postSyncCommand;
+ const configuredPostSyncCommand = getPostSyncCommand(pkiSync.syncOptions);
+ const configuredHealthCheckCommand = getHealthCheckCommand(pkiSync.syncOptions);
try {
- const {
- connection: { id: connectionId, orgId, projectId: appConnectionProjectId }
- } = pkiSync;
-
- const appConnection = await appConnectionDAL.findById(connectionId);
- if (!appConnection) {
- throw new Error(`App connection not found: ${connectionId}`);
- }
-
- const project = appConnectionProjectId ? await projectDAL.findById(appConnectionProjectId) : null;
-
- const credentials = await decryptAppConnectionCredentials({
- orgId,
- encryptedCredentials: appConnection.encryptedCredentials,
- kmsService,
- projectId: appConnectionProjectId
+ const pkiSyncWithCredentials = await hydratePkiSyncCredentials({
+ pkiSync,
+ appConnectionDAL,
+ projectDAL,
+ kmsService
});
- const syncCredentials = pkiSync.encryptedCredentials
- ? await decryptPkiSyncCredentials({
- orgId,
- projectId: pkiSync.projectId,
- encryptedCredentials: pkiSync.encryptedCredentials,
- kmsService
- })
- : undefined;
-
- const pkiSyncWithCredentials = {
- ...pkiSync,
- connection: {
- ...pkiSync.connection,
- credentials,
- projectType: project?.type
- },
- syncCredentials
- } as TPkiSyncWithCredentials;
-
- const { certificateMap, certificateMetadata } = await $getInfisicalCertificates(pkiSync);
+ const { certificateMap, certificateMetadata } = await $certificatesForSync(pkiSync);
const statusUpdates = Array.from(certificateMetadata.entries()).map(([, metadata]) => ({
pkiSyncId: pkiSync.id,
@@@ -624,14 -401,19 +402,21 @@@
}
postSyncCommandResult = syncResult.postSyncCommand;
+ partialFailure = Boolean(syncResult.partialFailureMessage);
- const reasons = [
- certSyncFailureCount > 0 ? `${certSyncFailureCount} certificate(s) failed to sync to the destination` : null,
- syncResult.partialFailureMessage ?? null,
- postSyncCommandResult?.status === PkiSyncStatus.Failed
- ? buildPostSyncCommandFailureMessage(postSyncCommandResult)
- : null
- ].filter(Boolean);
+ healthCheckResult = syncResult.healthCheck;
+
+ const reasons =
+ healthCheckResult && didHealthCheckFail(healthCheckResult)
+ ? [buildHealthCheckCommandFailureMessage(healthCheckResult)]
+ : [
+ certSyncFailureCount > 0
+ ? `${certSyncFailureCount} certificate(s) failed to sync to the destination`
+ : null,
++ syncResult.partialFailureMessage ?? null,
+ postSyncCommandResult?.status === PkiSyncStatus.Failed
+ ? buildPostSyncCommandFailureMessage(postSyncCommandResult)
+ : null
+ ].filter(Boolean);
if (reasons.length > 0) {
syncMessage = truncateSyncMessage(reasons.join(". "));
@@@ -666,7 -448,8 +451,12 @@@
} finally {
const ranAt = new Date();
const postSyncCommandFailed = postSyncCommandResult?.status === PkiSyncStatus.Failed;
- const fullySynced = isSynced && certSyncFailureCount === 0 && !postSyncCommandFailed && !partialFailure;
+ const fullySynced =
- isSynced && certSyncFailureCount === 0 && !postSyncCommandFailed && !didHealthCheckFail(healthCheckResult);
++ isSynced &&
++ certSyncFailureCount === 0 &&
++ !postSyncCommandFailed &&
++ !partialFailure &&
++ !didHealthCheckFail(healthCheckResult);
const syncStatus = fullySynced ? PkiSyncStatus.Succeeded : PkiSyncStatus.Failed;
await auditLogService.createAuditLog({
diff --cc backend/src/services/pki-sync/pki-sync-service.ts
index f56c9e1324,9535f174e9..4f1cda96fc
--- a/backend/src/services/pki-sync/pki-sync-service.ts
+++ b/backend/src/services/pki-sync/pki-sync-service.ts
@@@ -23,16 -25,26 +25,28 @@@ import { TSyncMetadata } from "../certi
import { certificateNameSchemaAllowsMultipleCertificates } from "./pki-sync-certificate-name-fns";
import { encryptPkiSyncCredentials } from "./pki-sync-credentials-fns";
import { TPkiSyncDALFactory } from "./pki-sync-dal";
- import { PkiSync, PkiSyncStatus } from "./pki-sync-enums";
+ import { HEALTH_CHECK_COMMAND_OPTION_KEY, PkiSync, PkiSyncStatus } from "./pki-sync-enums";
import { PkiSyncExportFormat } from "./pki-sync-export-fns";
import {
+ assertPkiSyncDestinationConfigAllowsCertificateCount,
enterprisePkiSyncCheck,
getPkiSyncMaxCertificates,
getPkiSyncProviderCapabilities,
- listPkiSyncOptions
+ listPkiSyncOptions,
+ resolvePkiSyncDestinationConfigUpdate
} from "./pki-sync-fns";
+ import {
+ applyHealthCheckCommandUpdate,
+ getHealthCheckCommand,
+ normalizeNewHealthCheckCommand,
+ toHealthCheckApiResult
+ } from "./pki-sync-health-check-command-fns";
+ import { TPkiSyncHealthCheckQueueFactory } from "./pki-sync-health-check-queue";
+ import {
+ findSingleCertificateHostCommandVariables,
+ formatHostCommandVariables,
+ HostCommandKind
+ } from "./pki-sync-host-command-fns";
import { PKI_SYNC_CONNECTION_MAP, PKI_SYNC_NAME_MAP } from "./pki-sync-maps";
import {
applyPostSyncCommandUpdate,
@@@ -514,39 -593,41 +602,35 @@@ export const pkiSyncServiceFactory = (
}
const effectiveSyncOptions = (resolvedSyncOptions ?? pkiSync.syncOptions) as Record<string, unknown> | undefined;
+ const effectiveDestinationConfig = (resolvedDestinationConfig ?? pkiSync.destinationConfig) as
+ | Record<string, unknown>
+ | undefined;
- await $assertMaySetPostSyncCommand(
- effectiveSyncOptions?.postSyncCommand,
- (pkiSync.syncOptions as Record<string, unknown> | undefined)?.postSyncCommand,
+ await $assertHostCommandWrite({
+ destination: pkiSync.destination,
+ nextSyncOptions: effectiveSyncOptions,
+ storedSyncOptions,
pkiSync,
- currentSubscriber?.name,
- actor
- );
-
- if (effectiveSyncOptions?.postSyncCommand) {
- $assertPostSyncCommandIsSupported(
- pkiSync.destination,
- effectiveSyncOptions,
+ subscriberName: currentSubscriber?.name,
+ actor,
+ isExecutionTargetChanging: Boolean(effectiveConnection) || isDestinationConfigChanging,
+ resolveConnection: async () =>
effectiveConnection ??
- (await appConnectionService.connectAppConnectionById(
- getDestinationAppType(pkiSync.destination),
- pkiSync.connectionId,
- actor
- ))
- );
- }
+ appConnectionService.connectAppConnectionById(
+ getDestinationAppType(pkiSync.destination),
+ pkiSync.connectionId,
+ actor
+ )
+ });
- if (certificateIds !== undefined) {
- if (certificateIds.length > 0) {
- assertWithinCertificateLimit(pkiSync.destination, certificateIds.length);
- await validateCertificatesForSync(certificateIds, pkiSync.projectId, pkiSync.applicationId);
- assertSyncOptionsAllowCertificateCount(effectiveSyncOptions, certificateIds.length);
- }
-
- await certificateSyncDAL.removeAllCertificatesFromSync(id);
- if (certificateIds.length > 0) {
- await certificateSyncDAL.addCertificates(
- id,
- certificateIds.map((certId) => ({ certificateId: certId }))
- );
- }
- } else if (syncOptions) {
+ if (syncOptions || destinationConfig) {
const existingCount = (await certificateSyncDAL.findByPkiSyncId(id)).length;
assertSyncOptionsAllowCertificateCount(effectiveSyncOptions, existingCount);
+ assertPkiSyncDestinationConfigAllowsCertificateCount(
+ pkiSync.destination,
+ effectiveDestinationConfig,
+ existingCount
+ );
}
if (
@@@ -561,7 -642,12 +645,12 @@@
? await encryptPkiSyncCredentials({ orgId: actor.orgId, projectId: pkiSync.projectId, credentials, kmsService })
: undefined;
+ const isHealthCheckBeingCleared =
+ resolvedSyncOptions !== undefined &&
+ !getHealthCheckCommand(resolvedSyncOptions) &&
+ Boolean(getHealthCheckCommand(storedSyncOptions));
+
- const updatedPkiSync = await pkiSyncDAL.updateById(id, {
+ const update = {
name,
description,
isAutoSyncEnabled,
@@@ -569,12 -655,11 +658,15 @@@
syncOptions: resolvedSyncOptions,
subscriberId,
connectionId,
- ...(encryptedCredentials ? { encryptedCredentials } : {})
+ ...(encryptedCredentials ? { encryptedCredentials } : {}),
+ ...(isHealthCheckBeingCleared
+ ? { lastHealthCheckRanAt: null, lastHealthCheckStatus: null, lastHealthCheckMessage: null }
+ : {})
- });
+ };
+
+ if (Object.values(update).every((value) => value === undefined)) return pkiSync as TPkiSync;
+
+ const updatedPkiSync = await pkiSyncDAL.updateById(id, update);
return updatedPkiSync as TPkiSync;
};