Skip to content

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

misc: added transaction for cert template create and update

Sheen Capadngan0ab7a4e71345
diff --git a/backend/src/services/certificate-template/certificate-template-dal.ts b/backend/src/services/certificate-template/certificate-template-dal.ts
index a30c4890da..4db383951c 100644
--- a/backend/src/services/certificate-template/certificate-template-dal.ts
+++ b/backend/src/services/certificate-template/certificate-template-dal.ts
@@ -1,3 +1,5 @@
+import { Knex } from "knex";
+
import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
@@ -30,10 +32,9 @@ export const certificateTemplateDALFactory = (db: TDbClient) => {
}
};
- const getById = async (id: string) => {
+ const getById = async (id: string, tx?: Knex) => {
try {
- const certTemplate = await db
- .replicaNode()(TableName.CertificateTemplate)
+ const certTemplate = await (tx || db.replicaNode())(TableName.CertificateTemplate)
.join(
TableName.CertificateAuthority,
`${TableName.CertificateAuthority}.id`,
diff --git a/backend/src/services/certificate-template/certificate-template-service.ts b/backend/src/services/certificate-template/certificate-template-service.ts
index 31745e752e..04d5e98eb2 100644
--- a/backend/src/services/certificate-template/certificate-template-service.ts
+++ b/backend/src/services/certificate-template/certificate-template-service.ts
@@ -75,23 +75,28 @@ export const certificateTemplateServiceFactory = ({
ProjectPermissionSub.CertificateTemplates
);
- const { id } = await certificateTemplateDAL.create({
- caId,
- pkiCollectionId,
- name,
- commonName,
- subjectAlternativeName,
- ttl
- });
+ return certificateTemplateDAL.transaction(async (tx) => {
+ const { id } = await certificateTemplateDAL.create(
+ {
+ caId,
+ pkiCollectionId,
+ name,
+ commonName,
+ subjectAlternativeName,
+ ttl
+ },
+ tx
+ );
- const certificateTemplate = await certificateTemplateDAL.getById(id);
- if (!certificateTemplate) {
- throw new NotFoundError({
- message: "Certificate template not found"
- });
- }
+ const certificateTemplate = await certificateTemplateDAL.getById(id, tx);
+ if (!certificateTemplate) {
+ throw new NotFoundError({
+ message: "Certificate template not found"
+ });
+ }
- return certificateTemplate;
+ return certificateTemplate;
+ });
};
const updateCertTemplate = async ({
@@ -136,23 +141,29 @@ export const certificateTemplateServiceFactory = ({
}
}
- await certificateTemplateDAL.updateById(certTemplate.id, {
- caId,
- pkiCollectionId,
- commonName,
- subjectAlternativeName,
- name,
- ttl
- });
+ return certificateTemplateDAL.transaction(async (tx) => {
+ await certificateTemplateDAL.updateById(
+ certTemplate.id,
+ {
+ caId,
+ pkiCollectionId,
+ commonName,
+ subjectAlternativeName,
+ name,
+ ttl
+ },
+ tx
+ );
- const updatedTemplate = await certificateTemplateDAL.getById(id);
- if (!updatedTemplate) {
- throw new NotFoundError({
- message: "Certificate template not found"
- });
- }
+ const updatedTemplate = await certificateTemplateDAL.getById(id, tx);
+ if (!updatedTemplate) {
+ throw new NotFoundError({
+ message: "Certificate template not found"
+ });
+ }
- return updatedTemplate;
+ return updatedTemplate;
+ });
};
const deleteCertTemplate = async ({ id, actorId, actorAuthMethod, actor, actorOrgId }: TDeleteCertTemplateDTO) => {