Skip to content

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

reduce memory footprint

x032205d45e39012e07
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 33dbc1a884..fedf2ac2ab 100644
--- a/backend/src/ee/services/pam-account/pam-account-dal.ts
+++ b/backend/src/ee/services/pam-account/pam-account-dal.ts
@@ -170,10 +170,33 @@ export const pamAccountDALFactory = (db: TDbClient) => {
return byAccountId;
};
+ const findRotationCandidates = async (resourceIds: string[], minIntervalSeconds: number, tx?: Knex) => {
+ if (!resourceIds.length) return [];
+
+ try {
+ const cutoff = new Date(Date.now() - minIntervalSeconds * 1000);
+
+ return await (tx || db.replicaNode())(TableName.PamAccount)
+ .whereIn("resourceId", resourceIds)
+ .where((qb) => {
+ void qb.whereNot("rotationStatus", "rotating").orWhereNull("rotationStatus");
+ })
+ .where((qb) => {
+ void qb.where("lastRotatedAt", "<", cutoff).orWhere((inner) => {
+ void inner.whereNull("lastRotatedAt").andWhere("createdAt", "<", cutoff);
+ });
+ })
+ .select(selectAllTableCols(TableName.PamAccount));
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Find rotation candidates" });
+ }
+ };
+
return {
...orm,
findByProjectIdWithResourceDetails,
findByIdWithResourceDetails,
- findMetadataByAccountIds
+ findMetadataByAccountIds,
+ findRotationCandidates
};
};
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 5ad7946447..ef629b3021 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -1051,16 +1051,21 @@ export const pamAccountServiceFactory = ({
const allRules = await pamResourceRotationRulesDAL.findByResourceIds(resourceIds);
if (!allRules.length) return [];
- // Group rules by resource
+ // Group rules by resource, compute minimum interval for DB pre-filter
const rulesByResource: Record<string, typeof allRules> = {};
+ let minIntervalSeconds = Infinity;
for (const rule of allRules) {
if (!rulesByResource[rule.resourceId]) rulesByResource[rule.resourceId] = [];
rulesByResource[rule.resourceId].push(rule);
+ if (rule.enabled && rule.intervalSeconds) {
+ minIntervalSeconds = Math.min(minIntervalSeconds, rule.intervalSeconds);
+ }
}
- // Get accounts for resources that have rules
+ if (minIntervalSeconds === Infinity) return [];
+
const resourceIdsWithRules = Object.keys(rulesByResource);
- const accounts = await pamAccountDAL.find({ $in: { resourceId: resourceIdsWithRules } });
+ const accounts = await pamAccountDAL.findRotationCandidates(resourceIdsWithRules, minIntervalSeconds);
const now = Date.now();
const dueAccounts: TPamAccounts[] = [];
@@ -1076,10 +1081,6 @@ export const pamAccountServiceFactory = ({
// eslint-disable-next-line no-continue
if (!matchedRule || !matchedRule.enabled || !matchedRule.intervalSeconds) continue;
- // Skip if already rotating
- // eslint-disable-next-line no-continue
- if (account.rotationStatus === PamAccountRotationStatus.Rotating) continue;
-
// Check if interval has elapsed
const lastRotated = account.lastRotatedAt
? new Date(account.lastRotatedAt).getTime()