Skip to content

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

fix: address pr review feedback on pam insights

bernie-ge581ae5fde04
diff --git a/backend/src/ee/routes/v1/pam-insights-router.ts b/backend/src/ee/routes/v1/pam-insights-router.ts
index 61a7104810..991484e5c5 100644
--- a/backend/src/ee/routes/v1/pam-insights-router.ts
+++ b/backend/src/ee/routes/v1/pam-insights-router.ts
@@ -151,7 +151,8 @@ export const registerPamInsightsRouter = async (server: FastifyZodProvider) => {
config: { rateLimit: readLimit },
schema: {
operationId: "getPamInsightsUpcomingRotations",
- description: "Get accounts due for credential rotation in the next 7 days",
+ description:
+ "Get accounts with a configured rotation schedule, ordered by next rotation time (capped at 100)",
security: [{ bearerAuth: [] }],
querystring: z.object({
projectId: z.string().trim()
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 4041d88ead..1f56dbe060 100644
--- a/backend/src/ee/services/pam-account/pam-account-dal.ts
+++ b/backend/src/ee/services/pam-account/pam-account-dal.ts
@@ -307,12 +307,18 @@ export const pamAccountDALFactory = (db: TDbClient) => {
projectId: string,
tx?: Knex
): Promise<{ resourceType: string; count: number }[]> => {
+ // leftJoin + COALESCE so domain accounts (resourceId IS NULL) are bucketed under
+ // a synthetic "domain" type instead of being silently dropped from the breakdown.
const rows = (await (tx || db.replicaNode())(TableName.PamAccount)
- .innerJoin(TableName.PamResource, `${TableName.PamAccount}.resourceId`, `${TableName.PamResource}.id`)
- .select(`${TableName.PamResource}.resourceType as resourceType`)
+ .leftJoin(TableName.PamResource, `${TableName.PamAccount}.resourceId`, `${TableName.PamResource}.id`)
+ .select(
+ db.raw(`COALESCE("${TableName.PamResource}"."resourceType", 'domain') as "resourceType"`)
+ )
.count(`${TableName.PamAccount}.id as count`)
.where(`${TableName.PamAccount}.projectId`, projectId)
- .groupBy(`${TableName.PamResource}.resourceType`)) as unknown as {
+ .groupByRaw(
+ `COALESCE("${TableName.PamResource}"."resourceType", 'domain')`
+ )) as unknown as {
resourceType: string;
count: string | number;
}[];
diff --git a/backend/src/ee/services/pam-insights/pam-insights-service.ts b/backend/src/ee/services/pam-insights/pam-insights-service.ts
index 23a4313fde..2d4f25e762 100644
--- a/backend/src/ee/services/pam-insights/pam-insights-service.ts
+++ b/backend/src/ee/services/pam-insights/pam-insights-service.ts
@@ -224,7 +224,9 @@ export const pamInsightsServiceFactory = ({
nextRotationAt: Date;
};
- const upcoming: TUpcomingRotation[] = [];
+ // Sort by raw nextRotationMs so the most-overdue rotations stay at the top
+ // even after clamping the displayed timestamp to "now" for past-due entries.
+ const candidatesWithRotation: { row: TUpcomingRotation; nextRotationMs: number }[] = [];
for (const candidate of candidates) {
const rules = rulesByResource[candidate.resourceId];
// eslint-disable-next-line no-continue
@@ -239,22 +241,25 @@ export const pamInsightsServiceFactory = ({
: candidate.createdAt.getTime();
const nextRotationMs = lastRotated + matchedRule.intervalSeconds * 1000;
- upcoming.push({
- accountId: candidate.id,
- accountName: candidate.name,
- resourceId: candidate.resourceId,
- resourceName: candidate.resourceName,
- resourceType: candidate.resourceType,
- intervalSeconds: matchedRule.intervalSeconds,
- nextRotationAt: new Date(Math.max(nextRotationMs, now))
+ candidatesWithRotation.push({
+ row: {
+ accountId: candidate.id,
+ accountName: candidate.name,
+ resourceId: candidate.resourceId,
+ resourceName: candidate.resourceName,
+ resourceType: candidate.resourceType,
+ intervalSeconds: matchedRule.intervalSeconds,
+ nextRotationAt: new Date(Math.max(nextRotationMs, now))
+ },
+ nextRotationMs
});
}
- upcoming.sort((a, b) => a.nextRotationAt.getTime() - b.nextRotationAt.getTime());
+ candidatesWithRotation.sort((a, b) => a.nextRotationMs - b.nextRotationMs);
return {
- rotations: upcoming.slice(0, UPCOMING_ROTATIONS_LIMIT),
- totalScheduled: upcoming.length
+ rotations: candidatesWithRotation.slice(0, UPCOMING_ROTATIONS_LIMIT).map((c) => c.row),
+ totalScheduled: candidatesWithRotation.length
};
},
reviver: (parsed) => {
diff --git a/backend/src/ee/services/pam-session/pam-session-dal.ts b/backend/src/ee/services/pam-session/pam-session-dal.ts
index bdd2d4fa80..cf963e3569 100644
--- a/backend/src/ee/services/pam-session/pam-session-dal.ts
+++ b/backend/src/ee/services/pam-session/pam-session-dal.ts
@@ -98,12 +98,14 @@ export const pamSessionDALFactory = (db: TDbClient) => {
tx?: Knex
): Promise<{ date: string; count: number }[]> => {
const rows = (await (tx || db.replicaNode())(TableName.PamSession)
- .select(db.raw(`to_char("createdAt"::date, 'YYYY-MM-DD') as date`))
+ .select(db.raw(`to_char(("createdAt" AT TIME ZONE 'UTC')::date, 'YYYY-MM-DD') as date`))
.count("id as count")
.where("projectId", projectId)
.where("createdAt", ">=", startDate)
- .groupByRaw(`"createdAt"::date`)
- .orderByRaw(`"createdAt"::date asc`)) as unknown as { date: string; count: string | number }[];
+ .groupByRaw(`("createdAt" AT TIME ZONE 'UTC')::date`)
+ .orderByRaw(
+ `("createdAt" AT TIME ZONE 'UTC')::date asc`
+ )) as unknown as { date: string; count: string | number }[];
return rows.map((row) => ({ date: String(row.date), count: Number(row.count) }));
};