Skip to content

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

address some comments

Andrey28ff13fdf905
diff --git a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts
index 501a63d193..9fe232ec82 100644
--- a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts
+++ b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts
@@ -1,3 +1,4 @@
+import { Knex } from "knex";
import pLimit from "p-limit";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
@@ -81,6 +82,7 @@ type TPamAccountRotationServiceFactoryDep = {
| "findRotationCandidates"
| "reconcileRotationScheduleForAccount"
| "transaction"
+ | "primaryNode"
>;
pamDiscoverySourceDAL: Pick<TPamDiscoverySourceDALFactory, "find">;
projectDAL: Pick<TProjectDALFactory, "findById">;
@@ -819,14 +821,18 @@ export const pamAccountRotationServiceFactory = (deps: TPamAccountRotationServic
}
};
- const loadAccount = async (accountId: string, projectId: string): Promise<TPamAccountDetail> => {
- const account = await pamAccountDAL.findByIdWithDetails(accountId);
+ const loadAccount = async (accountId: string, projectId: string, tx?: Knex): Promise<TPamAccountDetail> => {
+ const account = await pamAccountDAL.findByIdWithDetails(accountId, tx);
if (!account || account.projectId !== projectId) {
throw new NotFoundError({ message: `Account with ID '${accountId}' not found` });
}
return account;
};
+ // discovery import binds and rotates an account right after creating it, before a read replica may have it
+ const loadAccountFromPrimary = (accountId: string, projectId: string) =>
+ loadAccount(accountId, projectId, pamAccountDAL.primaryNode());
+
const getRotation = async ({
accountId,
projectId,
@@ -896,7 +902,7 @@ export const pamAccountRotationServiceFactory = (deps: TPamAccountRotationServic
projectId,
...ctx
}: TSetPamRotationAccountDTO & TActorContext) => {
- const account = await loadAccount(accountId, projectId);
+ const account = await loadAccountFromPrimary(accountId, projectId);
await checkAccount(
accountId,
account.folderId,
@@ -1004,7 +1010,7 @@ export const pamAccountRotationServiceFactory = (deps: TPamAccountRotationServic
rotationStatus: string;
message?: string;
}> => {
- const account = await loadAccount(accountId, projectId);
+ const account = await loadAccountFromPrimary(accountId, projectId);
await checkAccount(
accountId,
account.folderId,
diff --git a/backend/src/ee/services/pam-account/pam-account-dal.ts b/backend/src/ee/services/pam-account/pam-account-dal.ts
index 16f2f897fa..b9b8c57115 100644
--- a/backend/src/ee/services/pam-account/pam-account-dal.ts
+++ b/backend/src/ee/services/pam-account/pam-account-dal.ts
@@ -565,6 +565,7 @@ export const pamAccountDALFactory = (db: TDbClient) => {
countByOrgId,
findRotationCandidates,
reconcileRotationScheduleForTemplate,
- reconcileRotationScheduleForAccount
+ reconcileRotationScheduleForAccount,
+ primaryNode: () => db.primaryNode()
};
};
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts b/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
index 35350e3418..82c4966e36 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-source-service.ts
@@ -1,3 +1,4 @@
+import { ForbiddenError } from "@casl/ability";
import pLimit from "p-limit";
import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";
@@ -90,6 +91,14 @@ const DISCOVERED_UPSERT_CONCURRENCY = 8;
const IMPORT_ROTATION_CONCURRENCY = 5;
+const toResultMessage = (err: unknown, fallback: string) =>
+ err instanceof BadRequestError ||
+ err instanceof NotFoundError ||
+ err instanceof ForbiddenRequestError ||
+ err instanceof ForbiddenError
+ ? err.message
+ : fallback;
+
const withScanTimeout = async <T>(scan: Promise<T>, controller: AbortController, timeoutMs: number): Promise<T> => {
let timer: ReturnType<typeof setTimeout> | undefined;
const timeout = new Promise<never>((_, reject) => {
@@ -766,7 +775,7 @@ export const pamDiscoverySourceServiceFactory = (deps: TPamDiscoverySourceServic
} catch (err) {
return {
rotationStatus: ROTATION_STATUS.Failed,
- rotationMessage: err instanceof Error ? err.message : "Failed to set the rotation account"
+ rotationMessage: toResultMessage(err, "Failed to set the rotation account")
};
}
await auditLogService.createAuditLog({
@@ -780,7 +789,7 @@ export const pamDiscoverySourceServiceFactory = (deps: TPamDiscoverySourceServic
.rotateNow({ accountId, projectId, ...ctx })
.catch((err: unknown) => ({
rotationStatus: ROTATION_STATUS.Failed,
- message: err instanceof Error ? err.message : "Rotation failed"
+ message: toResultMessage(err, "Rotation failed")
}));
await auditLogService.createAuditLog({
...auditLogInfo,
@@ -859,7 +868,7 @@ export const pamDiscoverySourceServiceFactory = (deps: TPamDiscoverySourceServic
return {
discoveredAccountId: item.discoveredAccountId,
status: PamDiscoveryImportStatus.Error,
- message: err instanceof Error ? err.message : "Import failed"
+ message: toResultMessage(err, "Import failed")
};
}
})