Skip to content

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

use transactions

x0322051a0ee7d94b5f
diff --git a/backend/src/ee/services/pam-discovery/active-directory/active-directory-discovery-factory.ts b/backend/src/ee/services/pam-discovery/active-directory/active-directory-discovery-factory.ts
index 9aab44d94b..4f8835984a 100644
--- a/backend/src/ee/services/pam-discovery/active-directory/active-directory-discovery-factory.ts
+++ b/backend/src/ee/services/pam-discovery/active-directory/active-directory-discovery-factory.ts
@@ -292,16 +292,9 @@ const upsertAdServerResource = async (
configuration: TAdDiscoveryConfiguration,
gatewayId: string,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">,
- pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find">
+ pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "transaction">
) => {
- const existing = await pamResourceDAL.find({
- projectId,
- resourceType: PamResource.ActiveDirectory
- });
-
const domainResourceName = toSlugName(configuration.domainFQDN);
- const matched = existing.find((r) => r.name === domainResourceName);
- if (matched) return { resource: matched, isNew: false };
const encryptedConnectionDetails = await encryptResourceConnectionDetails({
projectId,
@@ -313,15 +306,31 @@ const upsertAdServerResource = async (
kmsService
});
- const resource = await pamResourceDAL.create({
- projectId,
- name: domainResourceName,
- resourceType: PamResource.ActiveDirectory,
- gatewayId,
- encryptedConnectionDetails
- });
+ return pamResourceDAL.transaction(async (tx) => {
+ const existing = await pamResourceDAL.find(
+ {
+ projectId,
+ resourceType: PamResource.ActiveDirectory
+ },
+ { tx }
+ );
+
+ const matched = existing.find((r) => r.name === domainResourceName);
+ if (matched) return { resource: matched, isNew: false };
+
+ const resource = await pamResourceDAL.create(
+ {
+ projectId,
+ name: domainResourceName,
+ resourceType: PamResource.ActiveDirectory,
+ gatewayId,
+ encryptedConnectionDetails
+ },
+ tx
+ );
- return { resource, isNew: true };
+ return { resource, isNew: true };
+ });
};
const upsertWindowsServerResource = async (
@@ -330,19 +339,11 @@ const upsertWindowsServerResource = async (
adServerResourceId: string,
gatewayId: string,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">,
- pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find">
+ pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "transaction">
) => {
const hostname = computer.dNSHostName || computer.cn;
const resourceName = toSlugName(hostname);
- const existing = await pamResourceDAL.find({
- projectId,
- resourceType: PamResource.Windows,
- name: resourceName
- });
-
- if (existing.length > 0) return { resource: existing[0], isNew: false };
-
const [encryptedConnectionDetails, encryptedResourceMetadata] = await Promise.all([
encryptResourceConnectionDetails({
projectId,
@@ -363,17 +364,33 @@ const upsertWindowsServerResource = async (
})
]);
- const resource = await pamResourceDAL.create({
- projectId,
- name: resourceName,
- resourceType: PamResource.Windows,
- gatewayId,
- encryptedConnectionDetails,
- encryptedResourceMetadata,
- adServerResourceId
- });
+ return pamResourceDAL.transaction(async (tx) => {
+ const existing = await pamResourceDAL.find(
+ {
+ projectId,
+ resourceType: PamResource.Windows,
+ name: resourceName
+ },
+ { tx }
+ );
+
+ if (existing.length > 0) return { resource: existing[0], isNew: false };
- return { resource, isNew: true };
+ const resource = await pamResourceDAL.create(
+ {
+ projectId,
+ name: resourceName,
+ resourceType: PamResource.Windows,
+ gatewayId,
+ encryptedConnectionDetails,
+ encryptedResourceMetadata,
+ adServerResourceId
+ },
+ tx
+ );
+
+ return { resource, isNew: true };
+ });
};
const upsertDomainAccount = async (
@@ -381,18 +398,11 @@ const upsertDomainAccount = async (
user: TLdapUser,
adServerResourceId: string,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">,
- pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find">
+ pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find" | "transaction">
) => {
const accountName = user.sAMAccountName;
const accountType = isServiceAccount(user) ? "service" : "user";
- const existing = await pamAccountDAL.find({
- resourceId: adServerResourceId,
- name: accountName
- });
-
- if (existing.length > 0) return { account: existing[0], isNew: false };
-
const encryptedCredentials = await encryptAccountCredentials({
projectId,
credentials: {
@@ -420,15 +430,30 @@ const upsertDomainAccount = async (
lastLogonTimestamp: user.lastLogonTimestamp || undefined
} as TActiveDirectoryAccountMetadata;
- const account = await pamAccountDAL.create({
- projectId,
- resourceId: adServerResourceId,
- name: accountName,
- encryptedCredentials,
- metadata
- });
+ return pamAccountDAL.transaction(async (tx) => {
+ const existing = await pamAccountDAL.find(
+ {
+ resourceId: adServerResourceId,
+ name: accountName
+ },
+ { tx }
+ );
+
+ if (existing.length > 0) return { account: existing[0], isNew: false };
- return { account, isNew: true };
+ const account = await pamAccountDAL.create(
+ {
+ projectId,
+ resourceId: adServerResourceId,
+ name: accountName,
+ encryptedCredentials,
+ metadata
+ },
+ tx
+ );
+
+ return { account, isNew: true };
+ });
};
export const activeDirectoryDiscoveryFactory: TPamDiscoveryFactory<
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-factory.ts b/backend/src/ee/services/pam-discovery/pam-discovery-factory.ts
index 47b4aa162a..1b4d72bab2 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-factory.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-factory.ts
@@ -27,8 +27,8 @@ export type TPamDiscoveryScanDeps = {
"upsertJunction" | "markStaleForRun"
>;
pamAccountDependenciesDAL: Pick<TPamAccountDependenciesDALFactory, "upsertDependency">;
- pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "findById">;
- pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find">;
+ pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "findById" | "transaction">;
+ pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find" | "transaction">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
gatewayV2DAL: Pick<TGatewayV2DALFactory, "findById">;
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-queue.ts b/backend/src/ee/services/pam-discovery/pam-discovery-queue.ts
index d13e49f3df..dcc5988138 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-queue.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-queue.ts
@@ -27,8 +27,8 @@ type TPamDiscoveryQueueFactoryDep = {
"upsertJunction" | "markStaleForRun"
>;
pamAccountDependenciesDAL: Pick<TPamAccountDependenciesDALFactory, "upsertDependency">;
- pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "findById">;
- pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find">;
+ pamResourceDAL: Pick<TPamResourceDALFactory, "create" | "find" | "findById" | "transaction">;
+ pamAccountDAL: Pick<TPamAccountDALFactory, "create" | "find" | "transaction">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
queueService: TQueueServiceFactory;
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;