Skip to content

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

take into account global grants in additional privileges

adilson7e8ca18bc518
diff --git a/backend/e2e-test/routes/v1/access-approval-global-request-lifecycle.spec.ts b/backend/e2e-test/routes/v1/access-approval-global-request-lifecycle.spec.ts
index 4f84b8441a..b4fc325fe4 100644
--- a/backend/e2e-test/routes/v1/access-approval-global-request-lifecycle.spec.ts
+++ b/backend/e2e-test/routes/v1/access-approval-global-request-lifecycle.spec.ts
@@ -1160,3 +1160,116 @@ describe("Break-glass approvals on the global system", () => {
expect(state.status).toBe("pending");
});
});
+
+describe("Additional privilege list on the global system", () => {
+ const approvers: TApprover[] = [];
+ const legacyRequestIds: string[] = [];
+ let approver: TApprover;
+
+ const listUserPrivileges = async () => {
+ const membership = await getDb()(TableName.Membership)
+ .where({ scope: AccessScope.Project, scopeProjectId: seedData1.project.id, actorUserId: seedData1.id })
+ .first();
+ const res = await testServer.inject({
+ method: "GET",
+ url: `/api/v1/user-project-additional-privilege?projectMembershipId=${membership?.id}`,
+ headers: authHeaders()
+ });
+ expect(res.statusCode).toBe(200);
+ return res.json().privileges as {
+ id: string;
+ isLinkedToAccessApproval: boolean;
+ accessApprovalRequestId: string | null;
+ policyApproverUserIds: string[];
+ }[];
+ };
+
+ beforeAll(async () => {
+ initLogger();
+ await initEnvConfig(testHsmService, testKmsRootConfigDAL, testSuperAdminDAL, logger);
+ approver = await createApproverUser();
+ approvers.push(approver);
+ });
+
+ afterEach(async () => {
+ const db = getDb();
+ const globalIds = globalPolicyIds.splice(0);
+ const legacyIds = legacyRequestIds.splice(0);
+
+ if (legacyIds.length) {
+ await db(TableName.AccessApprovalRequest).whereIn("id", legacyIds).del();
+ }
+ await db(TableName.AdditionalPrivilege)
+ .where({ actorUserId: seedData1.id })
+ .whereLike("name", "requested-privilege-%")
+ .del();
+ await db(TableName.ApprovalRequestGrants)
+ .where({ projectId: seedData1.project.id, type: SECRET_ACCESS_TYPE })
+ .del();
+ if (globalIds.length) {
+ await db(TableName.AccessApprovalPolicyApprover).whereIn("policyId", globalIds).del();
+ await db(TableName.AccessApprovalPolicy).whereIn("id", globalIds).del();
+ await db(TableName.ApprovalRequests).whereIn("policyId", globalIds).del();
+ await db(TableName.ApprovalPolicies).whereIn("id", globalIds).del();
+ }
+ });
+
+ afterAll(async () => {
+ const db = getDb();
+ const userIds = approvers.map((el) => el.userId);
+ await db(TableName.AuthTokenSession).whereIn("userId", userIds).del();
+ await db(TableName.Membership).whereIn("actorUserId", userIds).del();
+ await db(TableName.Users).whereIn("id", userIds).del();
+ });
+
+ test("A privilege granted on the global system is listed as managed by its request", async () => {
+ const { requestId } = await createApprovedTemporaryRequest("/privilege-list-native");
+ const { privilege } = await getRequestState(requestId);
+
+ const listed = (await listUserPrivileges()).find((el) => el.id === privilege?.id);
+ expect(listed?.isLinkedToAccessApproval).toBe(true);
+ expect(listed?.accessApprovalRequestId).toBe(requestId);
+ expect(listed?.policyApproverUserIds).toEqual([seedData1.id]);
+ });
+
+ test("A migrated privilege lists the global policy's approvers after they change", async () => {
+ const db = getDb();
+ const secretPath = "/privilege-list-migrated";
+ const { policy, requestId } = await createApprovedTemporaryRequest(secretPath);
+ const { privilege } = await getRequestState(requestId);
+
+ const environment = await db(TableName.Environment)
+ .where({ projectId: seedData1.project.id, slug: seedData1.environment.slug })
+ .first();
+ await db(TableName.AccessApprovalPolicy).insert({
+ id: policy.id,
+ name: "privilege-list-migrated",
+ secretPath,
+ envId: environment?.id as string,
+ deletedAt: new Date()
+ });
+ await db(TableName.AccessApprovalPolicyApprover).insert({ policyId: policy.id, approverUserId: seedData1.id });
+ await db(TableName.AccessApprovalRequest).insert({
+ id: requestId,
+ policyId: policy.id,
+ privilegeId: privilege?.id,
+ requestedByUserId: seedData1.id,
+ isTemporary: true,
+ temporaryRange: "1h",
+ permissions: JSON.stringify([]),
+ status: "approved"
+ });
+ legacyRequestIds.push(requestId);
+
+ const res = await patchPolicy(policy.id, {
+ secretPath,
+ approvers: [{ type: ApproverType.User, id: approver.userId }]
+ });
+ expect(res.statusCode).toBe(200);
+
+ const listed = (await listUserPrivileges()).find((el) => el.id === privilege?.id);
+ expect(listed?.isLinkedToAccessApproval).toBe(true);
+ expect(listed?.accessApprovalRequestId).toBe(requestId);
+ expect(listed?.policyApproverUserIds).toEqual([approver.userId]);
+ });
+});
diff --git a/backend/src/services/additional-privilege/additional-privilege-dal.ts b/backend/src/services/additional-privilege/additional-privilege-dal.ts
index 845e12f3fe..8f90322c6a 100644
--- a/backend/src/services/additional-privilege/additional-privilege-dal.ts
+++ b/backend/src/services/additional-privilege/additional-privilege-dal.ts
@@ -10,6 +10,19 @@ import { ActorType } from "@app/services/auth/auth-type";
export type TAdditionalPrivilegeDALFactory = ReturnType<typeof additionalPrivilegeDALFactory>;
+const groupApproverUserIdsByPolicyId = (
+ rows: { policyId: string; approverUserId?: string | null; groupMemberUserId?: string | null }[]
+) =>
+ rows.reduce<Record<string, string[]>>((acc, row) => {
+ const uid = String(row.approverUserId || row.groupMemberUserId || "");
+ if (uid) {
+ const pid = String(row.policyId);
+ if (!acc[pid]) acc[pid] = [];
+ if (!acc[pid].includes(uid)) acc[pid].push(uid);
+ }
+ return acc;
+ }, {});
+
export const additionalPrivilegeDALFactory = (db: TDbClient) => {
const orm = ormify(db, TableName.AdditionalPrivilege);
@@ -23,21 +36,40 @@ export const additionalPrivilegeDALFactory = (db: TDbClient) => {
`${TableName.AdditionalPrivilege}.id`,
`${TableName.AccessApprovalRequest}.privilegeId`
)
+ .leftJoin(
+ TableName.ApprovalRequestGrants,
+ `${TableName.AdditionalPrivilege}.grantId`,
+ `${TableName.ApprovalRequestGrants}.id`
+ )
+ .leftJoin(
+ TableName.ApprovalRequests,
+ `${TableName.ApprovalRequestGrants}.requestId`,
+ `${TableName.ApprovalRequests}.id`
+ )
.select(selectAllTableCols(TableName.AdditionalPrivilege))
.select(
db.ref("id").withSchema(TableName.AccessApprovalRequest).as("accessApprovalRequestId"),
- db.ref("policyId").withSchema(TableName.AccessApprovalRequest).as("accessApprovalPolicyId")
+ db.ref("policyId").withSchema(TableName.AccessApprovalRequest).as("accessApprovalPolicyId"),
+ db.ref("id").withSchema(TableName.ApprovalRequests).as("approvalRequestId"),
+ db.ref("policyId").withSchema(TableName.ApprovalRequests).as("approvalPolicyId")
);
- const policyIds = [
- ...new Set(docs.map((d) => String(d.accessApprovalPolicyId)).filter((id) => id !== "null" && id !== "undefined"))
+ const legacyPolicyIds = [
+ ...new Set(
+ docs
+ .filter((doc) => !doc.grantId && doc.accessApprovalPolicyId)
+ .map((doc) => String(doc.accessApprovalPolicyId))
+ )
+ ];
+ const globalPolicyIds = [
+ ...new Set(docs.filter((doc) => doc.grantId && doc.approvalPolicyId).map((doc) => String(doc.approvalPolicyId)))
];
- let approversByPolicyId: Record<string, string[]> = {};
- if (policyIds.length > 0) {
+ let legacyApproversByPolicyId: Record<string, string[]> = {};
+ if (legacyPolicyIds.length > 0) {
const approverRows = await db
.replicaNode()(TableName.AccessApprovalPolicyApprover)
- .whereIn(`${TableName.AccessApprovalPolicyApprover}.policyId`, policyIds)
+ .whereIn(`${TableName.AccessApprovalPolicyApprover}.policyId`, legacyPolicyIds)
.leftJoin(
TableName.UserGroupMembership,
`${TableName.AccessApprovalPolicyApprover}.approverGroupId`,
@@ -49,24 +81,49 @@ export const additionalPrivilegeDALFactory = (db: TDbClient) => {
db.ref("userId").withSchema(TableName.UserGroupMembership).as("groupMemberUserId")
);
- approversByPolicyId = approverRows.reduce<Record<string, string[]>>((acc, row) => {
- const uid = String(row.approverUserId || row.groupMemberUserId || "");
- if (uid) {
- const pid = String(row.policyId);
- if (!acc[pid]) acc[pid] = [];
- if (!acc[pid].includes(uid)) acc[pid].push(uid);
- }
- return acc;
- }, {});
+ legacyApproversByPolicyId = groupApproverUserIdsByPolicyId(approverRows);
+ }
+
+ let globalApproversByPolicyId: Record<string, string[]> = {};
+ if (globalPolicyIds.length > 0) {
+ const approverRows = await db
+ .replicaNode()(TableName.ApprovalPolicySteps)
+ .whereIn(`${TableName.ApprovalPolicySteps}.policyId`, globalPolicyIds)
+ .join(
+ TableName.ApprovalPolicyStepApprovers,
+ `${TableName.ApprovalPolicySteps}.id`,
+ `${TableName.ApprovalPolicyStepApprovers}.policyStepId`
+ )
+ .leftJoin(
+ TableName.UserGroupMembership,
+ `${TableName.ApprovalPolicyStepApprovers}.groupId`,
+ `${TableName.UserGroupMembership}.groupId`
+ )
+ .select(
+ db.ref("policyId").withSchema(TableName.ApprovalPolicySteps),
+ db.ref("userId").withSchema(TableName.ApprovalPolicyStepApprovers).as("approverUserId"),
+ db.ref("userId").withSchema(TableName.UserGroupMembership).as("groupMemberUserId")
+ );
+
+ globalApproversByPolicyId = groupApproverUserIdsByPolicyId(approverRows);
}
- return docs.map((doc) => {
+ return docs.map(({ approvalRequestId, approvalPolicyId, ...doc }) => {
+ if (doc.grantId) {
+ return {
+ ...doc,
+ isLinkedToAccessApproval: Boolean(approvalRequestId),
+ accessApprovalRequestId: (approvalRequestId ?? null) as string | null,
+ policyApproverUserIds: approvalPolicyId ? (globalApproversByPolicyId[String(approvalPolicyId)] ?? []) : []
+ };
+ }
+
const pid = doc.accessApprovalPolicyId ? String(doc.accessApprovalPolicyId) : null;
return {
...doc,
isLinkedToAccessApproval: Boolean(doc.accessApprovalRequestId),
accessApprovalRequestId: (doc.accessApprovalRequestId ?? null) as string | null,
- policyApproverUserIds: pid ? (approversByPolicyId[pid] ?? []) : []
+ policyApproverUserIds: pid ? (legacyApproversByPolicyId[pid] ?? []) : []
};
});
};