Skip to content

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

Merge branch 'feature/certificate-template' into feature/est-simpleenroll

Sheen Capadngan3a4f8c2e5483
diff --cc backend/package-lock.json
index 3863cc25d6,18b1548f17..dd7f69b223
--- a/backend/package-lock.json
+++ b/backend/package-lock.json
@@@ -73,9 -74,9 +74,10 @@@
"pg-query-stream": "^4.5.3",
"picomatch": "^3.0.1",
"pino": "^8.16.2",
+ "pkijs": "^3.2.4",
"posthog-node": "^3.6.2",
"probot": "^13.0.0",
+ "safe-regex": "^2.1.1",
"smee-client": "^2.0.0",
"tedious": "^18.2.1",
"tweetnacl": "^1.0.3",
diff --cc backend/package.json
index 6e265acdb0,fbd02365b5..ee70d9c9f3
--- a/backend/package.json
+++ b/backend/package.json
@@@ -169,9 -171,9 +171,10 @@@
"pg-query-stream": "^4.5.3",
"picomatch": "^3.0.1",
"pino": "^8.16.2",
+ "pkijs": "^3.2.4",
"posthog-node": "^3.6.2",
"probot": "^13.0.0",
+ "safe-regex": "^2.1.1",
"smee-client": "^2.0.0",
"tedious": "^18.2.1",
"tweetnacl": "^1.0.3",
diff --cc backend/src/server/routes/index.ts
index f0c3cbd5b0,d49c7aace6..d5735ff9f9
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@@ -190,8 -192,8 +193,9 @@@ import { webhookServiceFactory } from "
import { injectAuditLogInfo } from "../plugins/audit-log";
import { injectIdentity } from "../plugins/auth/inject-identity";
import { injectPermission } from "../plugins/auth/inject-permission";
+ import { injectRateLimits } from "../plugins/inject-rate-limits";
import { registerSecretScannerGhApp } from "../plugins/secret-scanner";
+import { registerCertificateEstRouter } from "./est/certificate-est-router";
import { registerV1Routes } from "./v1";
import { registerV2Routes } from "./v2";
import { registerV3Routes } from "./v3";
@@@ -586,7 -589,7 +591,8 @@@ export const registerRoutes = async
const certificateAuthorityCertDAL = certificateAuthorityCertDALFactory(db);
const certificateAuthoritySecretDAL = certificateAuthoritySecretDALFactory(db);
const certificateAuthorityCrlDAL = certificateAuthorityCrlDALFactory(db);
+ const certificateAuthorityEstConfigDAL = certificateAuthorityEstConfigDALFactory(db);
+ const certificateTemplateDAL = certificateTemplateDALFactory(db);
const certificateDAL = certificateDALFactory(db);
const certificateBodyDAL = certificateBodyDALFactory(db);
@@@ -625,10 -629,11 +632,12 @@@
certificateAuthorityQueue,
certificateDAL,
certificateBodyDAL,
+ pkiCollectionDAL,
+ pkiCollectionItemDAL,
projectDAL,
kmsService,
- permissionService
+ permissionService,
+ certificateAuthorityEstConfigDAL
});
const certificateAuthorityCrlService = certificateAuthorityCrlServiceFactory({
diff --cc backend/src/services/certificate-authority/certificate-authority-service.ts
index dfbb895f9e,32dbc8fd00..ddbade5f5c
--- a/backend/src/services/certificate-authority/certificate-authority-service.ts
+++ b/backend/src/services/certificate-authority/certificate-authority-service.ts
@@@ -6,10 -5,9 +6,11 @@@ import crypto, { KeyObject } from "cryp
import ms from "ms";
import { z } from "zod";
+ import { TCertificateAuthorities, TCertificateTemplates } from "@app/db/schemas";
+import { TCertificateAuthorityEstConfigsUpdate } from "@app/db/schemas/certificate-authority-est-configs";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service";
import { ProjectPermissionActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
+import { getConfig } from "@app/lib/config/env";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { TCertificateBodyDALFactory } from "@app/services/certificate/certificate-body-dal";
import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";
@@@ -19,9 -19,10 +22,11 @@@ import { getProjectKmsCertificateKeyId
import { TCertificateAuthorityCrlDALFactory } from "../../ee/services/certificate-authority-crl/certificate-authority-crl-dal";
import { CertKeyAlgorithm, CertStatus } from "../certificate/certificate-types";
+ import { TCertificateTemplateDALFactory } from "../certificate-template/certificate-template-dal";
+ import { validateCertificateDetailsAgainstTemplate } from "../certificate-template/certificate-template-fns";
import { TCertificateAuthorityCertDALFactory } from "./certificate-authority-cert-dal";
import { TCertificateAuthorityDALFactory } from "./certificate-authority-dal";
+import { TCertificateAuthorityEstConfigDALFactory } from "./certificate-authority-est-config-dal";
import {
createDistinguishedName,
getCaCertChain, // TODO: consider rename
@@@ -58,13 -56,18 +63,19 @@@ type TCertificateAuthorityServiceFactor
TCertificateAuthorityDALFactory,
"transaction" | "create" | "findById" | "updateById" | "deleteById" | "findOne"
>;
- certificateAuthorityCertDAL: Pick<TCertificateAuthorityCertDALFactory, "create" | "findOne" | "transaction" | "find">;
+ certificateAuthorityCertDAL: Pick<
+ TCertificateAuthorityCertDALFactory,
+ "create" | "findOne" | "transaction" | "find" | "findById"
+ >;
certificateAuthoritySecretDAL: Pick<TCertificateAuthoritySecretDALFactory, "create" | "findOne">;
certificateAuthorityCrlDAL: Pick<TCertificateAuthorityCrlDALFactory, "create" | "findOne" | "update">;
+ certificateTemplateDAL: Pick<TCertificateTemplateDALFactory, "getById">;
certificateAuthorityQueue: TCertificateAuthorityQueueFactory; // TODO: Pick
+ certificateAuthorityEstConfigDAL: Pick<TCertificateAuthorityEstConfigDALFactory, "updateById" | "create" | "findOne">;
certificateDAL: Pick<TCertificateDALFactory, "transaction" | "create" | "find">;
certificateBodyDAL: Pick<TCertificateBodyDALFactory, "create">;
+ pkiCollectionDAL: Pick<TPkiCollectionDALFactory, "findById">;
+ pkiCollectionItemDAL: Pick<TPkiCollectionItemDALFactory, "create">;
projectDAL: Pick<TProjectDALFactory, "findProjectBySlug" | "findOne" | "updateById" | "findById" | "transaction">;
kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey" | "decryptWithKmsKey">;
permissionService: Pick<TPermissionServiceFactory, "getProjectPermission">;
@@@ -76,10 -79,12 +87,13 @@@ export const certificateAuthorityServic
certificateAuthorityDAL,
certificateAuthorityCertDAL,
certificateAuthoritySecretDAL,
+ certificateAuthorityEstConfigDAL,
certificateAuthorityCrlDAL,
+ certificateTemplateDAL,
certificateDAL,
certificateBodyDAL,
+ pkiCollectionDAL,
+ pkiCollectionItemDAL,
projectDAL,
kmsService,
permissionService
@@@ -1214,31 -1275,58 +1284,62 @@@
* Return new leaf certificate issued by CA with id [caId].
* Note: CSR is generated externally and submitted to Infisical.
*/
- const signCertFromCa = async ({
- caId,
- certificateTemplateId,
- csr,
- pkiCollectionId,
- friendlyName,
- commonName,
- altNames,
- ttl,
- notBefore,
- notAfter,
- actorId,
- actorAuthMethod,
- actor,
- actorOrgId
- }: TSignCertFromCaDTO) => {
+ const signCertFromCa = async (dto: TSignCertFromCaDTO) => {
- const ca = await certificateAuthorityDAL.findById(dto.caId);
- if (!ca) throw new BadRequestError({ message: "CA not found" });
+ let ca: TCertificateAuthorities | undefined;
+ let certificateTemplate: TCertificateTemplates | undefined;
++
++ const {
++ caId,
++ certificateTemplateId,
++ csr,
++ pkiCollectionId,
++ friendlyName,
++ commonName,
++ altNames,
++ ttl,
++ notBefore,
++ notAfter
++ } = dto;
++
+ let collectionId = pkiCollectionId;
+
+ if (caId) {
+ ca = await certificateAuthorityDAL.findById(caId);
+ } else if (certificateTemplateId) {
+ certificateTemplate = await certificateTemplateDAL.getById(certificateTemplateId);
+ if (!certificateTemplate) {
+ throw new NotFoundError({
+ message: "Certificate template not found"
+ });
+ }
+
+ collectionId = certificateTemplate.pkiCollectionId as string;
+ ca = await certificateAuthorityDAL.findById(certificateTemplate.caId);
+ }
+
+ if (!ca) {
+ throw new BadRequestError({ message: "CA not found" });
+ }
- const { permission } = await permissionService.getProjectPermission(
- actor,
- actorId,
- ca.projectId,
- actorAuthMethod,
- actorOrgId
- );
+ if (!dto.isInternal) {
+ const { permission } = await permissionService.getProjectPermission(
+ dto.actor,
+ dto.actorId,
+ ca.projectId,
+ dto.actorAuthMethod,
+ dto.actorOrgId
+ );
- ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Create, ProjectPermissionSub.Certificates);
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionActions.Create,
+ ProjectPermissionSub.Certificates
+ );
+ }
- const { csr, friendlyName, commonName, altNames, ttl, notBefore, notAfter } = dto;
-
if (ca.status === CaStatus.DISABLED) throw new BadRequestError({ message: "CA is disabled" });
+ if (!ca.activeCaCertId) throw new BadRequestError({ message: "CA does not have a certificate installed" });
- const [caCert] = await certificateAuthorityCertDAL.find({ caId: ca.id }, { sort: [["version", "desc"]] });
- if (!caCert) throw new BadRequestError({ message: "CA does not have a certificate installed" });
+ const caCert = await certificateAuthorityCertDAL.findById(ca.activeCaCertId);
if (ca.notAfter && new Date() > new Date(ca.notAfter)) {
throw new BadRequestError({ message: "CA is expired" });
diff --cc backend/src/services/certificate-authority/certificate-authority-types.ts
index 2f0d789bb7,c6bdeae301..9c2feea375
--- a/backend/src/services/certificate-authority/certificate-authority-types.ts
+++ b/backend/src/services/certificate-authority/certificate-authority-types.ts
@@@ -95,29 -97,18 +97,33 @@@ export type TIssueCertFromCaDTO =
notAfter?: string;
} & Omit<TProjectPermission, "projectId">;
-export type TSignCertFromCaDTO = {
- caId?: string;
- csr: string;
- certificateTemplateId?: string;
- pkiCollectionId?: string;
- friendlyName?: string;
- commonName?: string;
- altNames: string;
- ttl: string;
- notBefore?: string;
- notAfter?: string;
-} & Omit<TProjectPermission, "projectId">;
+export type TSignCertFromCaDTO =
+ | {
+ isInternal: true;
- caId: string;
++ caId?: string;
+ csr: string;
++ certificateTemplateId?: string;
++ pkiCollectionId?: string;
+ friendlyName?: string;
+ commonName?: string;
+ altNames: string;
+ ttl: string;
+ notBefore?: string;
+ notAfter?: string;
+ }
+ | ({
+ isInternal: false;
- caId: string;
++ caId?: string;
+ csr: string;
++ certificateTemplateId?: string;
++ pkiCollectionId?: string;
+ friendlyName?: string;
+ commonName?: string;
+ altNames: string;
+ ttl: string;
+ notBefore?: string;
+ notAfter?: string;
+ } & Omit<TProjectPermission, "projectId">);
export type TDNParts = {
commonName?: string;