Skip to content

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

fix(pki): surface why a CA cannot be deleted

Claude03424a674438
diff --git a/backend/src/services/certificate-authority/certificate-authority-fns.ts b/backend/src/services/certificate-authority/certificate-authority-fns.ts
index f9e60d359b..611d313dc7 100644
--- a/backend/src/services/certificate-authority/certificate-authority-fns.ts
+++ b/backend/src/services/certificate-authority/certificate-authority-fns.ts
@@ -4,7 +4,8 @@ import RE2 from "re2";
import { crypto } from "@app/lib/crypto/cryptography";
import { derivePublicKeyFromSecret, getPqcCrypto, isPqcAlgorithm, PqcCryptoKey } from "@app/lib/crypto/pqc";
-import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { DatabaseErrorCode } from "@app/lib/error-codes";
+import { BadRequestError, DatabaseError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fns";
import { CertKeySource } from "@app/services/signer/signer-enums";
@@ -741,3 +742,21 @@ export const buildCrlDistributionPointUrls = (
return acc;
}, []);
};
+
+export const rethrowCaDeleteError = (error: unknown): never => {
+ if (error instanceof DatabaseError) {
+ const { code, constraint } = error.error as { code?: string; constraint?: string };
+ if (code === DatabaseErrorCode.ForeignKeyViolation) {
+ if (constraint === "pki_certificate_profiles_caid_foreign") {
+ throw new BadRequestError({
+ message:
+ "Cannot delete this CA because certificate profiles are associated with it. Delete those certificate profiles first."
+ });
+ }
+ throw new BadRequestError({
+ message: "Cannot delete this CA because it is referenced by another resource"
+ });
+ }
+ }
+ throw error;
+};
diff --git a/backend/src/services/certificate-authority/certificate-authority-service.ts b/backend/src/services/certificate-authority/certificate-authority-service.ts
index 4b89d55075..b54fa4f695 100644
--- a/backend/src/services/certificate-authority/certificate-authority-service.ts
+++ b/backend/src/services/certificate-authority/certificate-authority-service.ts
@@ -83,6 +83,7 @@ import {
} from "./azure-ad-cs/azure-ad-cs-certificate-authority-types";
import { TCertificateAuthorityDALFactory } from "./certificate-authority-dal";
import { CaType } from "./certificate-authority-enums";
+import { rethrowCaDeleteError } from "./certificate-authority-fns";
import { CERTIFICATE_AUTHORITIES_TYPE_MAP } from "./certificate-authority-maps";
import { assertCertificateAuthorityQuota, resolveEffectiveMaxCas } from "./certificate-authority-quota-fns";
import { TCertificateAuthoritySecretDALFactory } from "./certificate-authority-secret-dal";
@@ -944,7 +945,7 @@ export const certificateAuthorityServiceFactory = ({
});
}
- await certificateAuthorityDAL.deleteById(certificateAuthority.id);
+ await certificateAuthorityDAL.deleteById(certificateAuthority.id).catch(rethrowCaDeleteError);
if (type === CaType.INTERNAL) {
usageMeteringService.emitForProject(certificateAuthority.projectId, InternalCas.key);
@@ -1176,7 +1177,7 @@ export const certificateAuthorityServiceFactory = ({
});
}
- await certificateAuthorityDAL.deleteById(certificateAuthority.id);
+ await certificateAuthorityDAL.deleteById(certificateAuthority.id).catch(rethrowCaDeleteError);
if (type === CaType.INTERNAL) {
usageMeteringService.emitForProject(certificateAuthority.projectId, InternalCas.key);
diff --git a/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts b/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
index 4d1832f67e..ca017290b1 100644
--- a/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
+++ b/backend/src/services/certificate-authority/internal/internal-certificate-authority-service.ts
@@ -82,6 +82,7 @@ import {
getCaCertChains,
getCaSigner,
keyAlgorithmToAlgCfg,
+ rethrowCaDeleteError,
signatureAlgorithmToAlgCfg,
validateImportedCertificate
} from "../certificate-authority-fns";
@@ -749,7 +750,7 @@ export const internalCertificateAuthorityServiceFactory = ({
subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
);
- await certificateAuthorityDAL.deleteById(ca.id);
+ await certificateAuthorityDAL.deleteById(ca.id).catch(rethrowCaDeleteError);
return expandInternalCa(ca);
};