Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat: add migration to move data to access approval global system
adilson94200ca1bf1d
diff --git a/backend/src/db/migrations/20261013120000_migrate-access-approval-policies-to-global.ts b/backend/src/db/migrations/20261013120000_migrate-access-approval-policies-to-global.tsnew file mode 100644index 0000000000..dc390f1036--- /dev/null+++ b/backend/src/db/migrations/20261013120000_migrate-access-approval-policies-to-global.ts@@ -0,0 +1,592 @@+/* eslint-disable no-await-in-loop -- policies migrate one after another on purpose */+import crypto from "node:crypto";++import { Knex } from "knex";++import { verifyRequestedPermissions } from "@app/ee/services/access-approval-request/access-approval-request-fns";+import { isGlobSubsetOfGlob } from "@app/lib/casl/glob-subset";+import { chunkArray } from "@app/lib/fn";+import { ms } from "@app/lib/ms";++import { TableName } from "../schemas";+import {+ buildApprovalSteps,+ buildBypasserRows,+ buildGlobalPolicyRow,+ buildPolicyEnvironmentRows,+ buildPolicyStepRows,+ buildRequestStepRows,+ checkPendingApprovalState,+ checkPolicyEnvironments,+ checkRequestsAlreadyMigrated,+ checkReviewStatuses,+ INSERT_CHUNK_SIZE,+ insertChunked,+ insertGlobalPolicyRows,+ insertGlobalRequestRows,+ lockLegacyPolicy,+ logFailedChecks,+ POLICY_STATEMENT_TIMEOUT_MS,+ readApproverGroupMembers,+ readLegacyApprovers,+ readLegacyBypassers,+ readLegacyReviews,+ readPolicyEnvironments,+ readRequestIdsAlreadyOnGlobal,+ replayReviews,+ ReviewDecision,+ runLegacyPolicyMigration,+ TGlobalPolicyRows,+ TGlobalRequestRows,+ TGroupMember,+ TLegacyApprovalTables,+ TLegacyApprover,+ TLegacyBypasser,+ TLegacyReview,+ TMigrationFailure,+ TPolicyEnvironment,+ TPolicyOutcome+} from "./utils/approval-migration";++const LOG_PREFIX = "migrate-access-approval-policies-to-global";+const SECRET_ACCESS = "secret-access";+const SOFT_ENFORCEMENT = "soft";++const LEGACY_TABLES: TLegacyApprovalTables = {+ legacyPolicies: TableName.AccessApprovalPolicy,+ legacyApprovers: TableName.AccessApprovalPolicyApprover,+ legacyBypassers: TableName.AccessApprovalPolicyBypasser,+ legacyEnvironments: TableName.AccessApprovalPolicyEnvironment,+ legacyRequests: TableName.AccessApprovalRequest,+ legacyReviewers: TableName.AccessApprovalRequestReviewer+};++enum LegacyRequestStatus {+ Pending = "pending",+ Approved = "approved",+ Rejected = "rejected",+ Revoked = "revoked"+}++enum RequestStatus {+ Pending = "pending",+ Approved = "approved",+ Rejected = "rejected",+ Expired = "expired"+}++enum GrantStatus {+ Active = "active",+ Expired = "expired",+ Revoked = "revoked"+}++type TLegacyPolicy = {+ id: string;+ name: string;+ approvals: number;+ secretPath: string;+ enforcementLevel: string;+ allowedSelfApprovals: boolean;+ maxTimePeriod?: string | null;+ requestExpirationTime?: string | null;+ deletedAt?: Date | null;+ createdAt: Date;+ updatedAt: Date;+};++type TLegacyRequest = {+ id: string;+ requestedByUserId: string;+ status: string;+ permissions: unknown;+ isTemporary: boolean;+ temporaryRange?: string | null;+ note?: string | null;+ privilegeId?: string | null;+ privilegeDeletedAt?: Date | null;+ expiresAt?: Date | null;+ approvedAt?: Date | null;+ revokedAt?: Date | null;+ revokedByUserId?: string | null;+ bypassReason?: string | null;+ createdAt: Date;+ updatedAt: Date;+};++type TAdditionalPrivilege = {+ id: string;+ actorUserId?: string | null;+ projectId?: string | null;+ permissions?: unknown;+ isTemporary: boolean;+ temporaryAccessEndTime?: Date | null;+ grantId?: string | null;+};++type TRequester = {+ id: string;+ username: string;+ email?: string | null;+ firstName?: string | null;+ lastName?: string | null;+};++export type TPolicyData = {+ policy: TLegacyPolicy;+ environments: TPolicyEnvironment[];+ approvers: TLegacyApprover[];+ bypassers: TLegacyBypasser[];+ requests: TLegacyRequest[];+ reviews: TLegacyReview[];+ additionalPrivileges: TAdditionalPrivilege[];+ requesters: TRequester[];+ groupMembers: TGroupMember[];+ requestIdsAlreadyOnGlobal: string[];+};++type TRequestData = { permissions: unknown; isTemporary: boolean; temporaryRange: string | null };++type TGlobalRows = TGlobalPolicyRows &+ TGlobalRequestRows & {+ grants: Record<string, unknown>[];+ additionalPrivilegeGrants: { additionalPrivilegeId: string; grantId: string }[];+ };++const canonicalize = (value: unknown): unknown => {+ if (Array.isArray(value)) return value.map(canonicalize);+ if (value && typeof value === "object") {+ return Object.fromEntries(+ Object.keys(value as Record<string, unknown>)+ .sort()+ .map((key) => [key, canonicalize((value as Record<string, unknown>)[key])])+ );+ }+ return value;+};++const isSameJson = (a: unknown, b: unknown) => JSON.stringify(canonicalize(a)) === JSON.stringify(canonicalize(b));++const durationMs = (range?: string | null) => {+ if (!range) return null;+ try {+ const value = ms(range);+ return Number.isFinite(value) ? value : null;+ } catch {+ return null;+ }+};++const isPast = (date: Date | null | undefined, now: Date) => Boolean(date && new Date(date).getTime() <= now.getTime());++const toRequestStatus = (+ status: string,+ { isApprovedLike, isPendingLive }: { isApprovedLike: boolean; isPendingLive: boolean }+): RequestStatus => {+ if (isApprovedLike) return RequestStatus.Approved;+ if (status === LegacyRequestStatus.Rejected) return RequestStatus.Rejected;+ return isPendingLive ? RequestStatus.Pending : RequestStatus.Expired;+};++const checkPendingScope = (+ request: TLegacyRequest,+ { policy, environments }: Pick<TPolicyData, "policy" | "environments">,+ failures: TMigrationFailure[]+) => {+ const fail = (message: string) =>+ failures.push({ code: "pending-request-out-of-scope", requestId: request.id, message });++ let requested: { envSlug: string; secretPath: string };+ try {+ requested = verifyRequestedPermissions({ permissions: request.permissions });+ } catch (error) {+ fail(`The requested permissions are invalid: ${(error as Error).message}`);+ return;+ }++ if (!environments.some((environment) => environment.envSlug === requested.envSlug)) {+ fail(`The request is for environment '${requested.envSlug}', which the policy does not cover`);+ }+ if (!isGlobSubsetOfGlob(policy.secretPath, requested.secretPath)) {+ fail(+ `The request is for secret path '${requested.secretPath}', which policy path '${policy.secretPath}' does not cover`+ );+ }+};++// The legacy system treats any approval the requester gives on a soft policy as break-glass, and the+// reason is optional, so bypassReason alone under-reports it.+const isBreakGlassApproval = (policy: TLegacyPolicy, request: TLegacyRequest, reviews: TLegacyReview[]) =>+ Boolean(request.bypassReason) ||+ (policy.enforcementLevel === SOFT_ENFORCEMENT &&+ reviews.some(+ (review) => review.status === ReviewDecision.Approved && review.reviewerUserId === request.requestedByUserId+ ));++const checkAdditionalPrivilegeState = (+ request: TLegacyRequest,+ {+ additionalPrivilege,+ projectId,+ requestsByAdditionalPrivilegeId+ }: {+ additionalPrivilege?: TAdditionalPrivilege;+ projectId?: string;+ requestsByAdditionalPrivilegeId: Map<string, string[]>;+ },+ failures: TMigrationFailure[]+) => {+ if (!request.privilegeId) return;+ const fail = (message: string) => failures.push({ code: "privilege-state-mismatch", requestId: request.id, message });++ if (request.status !== LegacyRequestStatus.Approved) {+ fail(`A ${request.status} request still points to additional privilege ${request.privilegeId}`);+ }++ const sharedWith = (requestsByAdditionalPrivilegeId.get(request.privilegeId) ?? []).filter((id) => id !== request.id);+ if (sharedWith.length) {+ fail(`Additional privilege ${request.privilegeId} is shared with requests ${sharedWith.join(", ")}`);+ }++ if (!additionalPrivilege) {+ fail(`Additional privilege ${request.privilegeId} does not exist`);+ return;+ }+ if (additionalPrivilege.actorUserId !== request.requestedByUserId || additionalPrivilege.projectId !== projectId) {+ fail(`Additional privilege ${additionalPrivilege.id} belongs to another user or project`);+ }+ if (additionalPrivilege.grantId) {+ fail(`Additional privilege ${additionalPrivilege.id} is already linked to grant ${additionalPrivilege.grantId}`);+ }+ if (!isSameJson(additionalPrivilege.permissions, request.permissions)) {+ failures.push({+ code: "privilege-permissions-mismatch",+ requestId: request.id,+ message: `Additional privilege ${additionalPrivilege.id} grants different permissions than the request asked for`+ });+ }+};++export const translatePolicy = (data: TPolicyData, now: Date) => {+ const { policy, environments, bypassers, requests, reviews, additionalPrivileges, requesters } = data;+ const failures: TMigrationFailure[] = [];++ const { projectId, orgId } = checkPolicyEnvironments(environments, failures);+ const steps = buildApprovalSteps(data, failures);++ const rows: TGlobalRows = {+ policy: buildGlobalPolicyRow({+ policy,+ projectId,+ orgId,+ type: SECRET_ACCESS,+ conditions: { version: 1, conditions: {} },+ constraints: {+ version: 1,+ constraints: {+ allowedSelfApprovals: policy.allowedSelfApprovals,+ requestExpirationTime: policy.requestExpirationTime ?? null,+ maxTimePeriod: policy.maxTimePeriod ?? null+ }+ },+ bypassForMachineIdentities: false // always false for access approval policies+ }),+ secretEnvironments: buildPolicyEnvironmentRows(policy.id, environments, policy.secretPath),+ ...buildPolicyStepRows(policy.id, steps),+ bypassers: buildBypasserRows(policy.id, bypassers),+ requests: [],+ requestSteps: [],+ eligibleApprovers: [],+ approvals: [],+ grants: [],+ additionalPrivilegeGrants: []+ };++ checkRequestsAlreadyMigrated(data.requestIdsAlreadyOnGlobal, failures);++ const requesterById = new Map(requesters.map((requester) => [requester.id, requester]));+ const additionalPrivilegeById = new Map(+ additionalPrivileges.map((additionalPrivilege) => [additionalPrivilege.id, additionalPrivilege])+ );+ const reviewsByRequestId = new Map<string, TLegacyReview[]>();+ reviews.forEach((review) => {+ reviewsByRequestId.set(review.requestId, [...(reviewsByRequestId.get(review.requestId) ?? []), review]);+ });+ const requestsByAdditionalPrivilegeId = new Map<string, string[]>();+ requests.forEach(({ id, privilegeId }) => {+ if (privilegeId)+ requestsByAdditionalPrivilegeId.set(privilegeId, [+ ...(requestsByAdditionalPrivilegeId.get(privilegeId) ?? []),+ id+ ]);+ });++ requests.forEach((request) => {+ const requestReviews = reviewsByRequestId.get(request.id) ?? [];++ const knownStatuses = Object.values(LegacyRequestStatus) as string[];+ if (!knownStatuses.includes(request.status)) {+ failures.push({+ code: "unknown-request-status",+ requestId: request.id,+ message: `Status '${request.status}' has no equivalent on the global approval system`+ });+ }+ checkReviewStatuses(request.id, requestReviews, failures);++ const requester = requesterById.get(request.requestedByUserId);+ if (!requester) {+ failures.push({+ code: "requester-missing",+ requestId: request.id,+ message: `Requester ${request.requestedByUserId} no longer exists`+ });+ }++ const isApprovedLike =+ request.status === LegacyRequestStatus.Approved || request.status === LegacyRequestStatus.Revoked;+ const additionalPrivilege = request.privilegeId ? additionalPrivilegeById.get(request.privilegeId) : undefined;+ checkAdditionalPrivilegeState(+ request,+ { additionalPrivilege, projectId, requestsByAdditionalPrivilegeId },+ failures+ );++ const replay = replayReviews(steps, requestReviews);+ const isPendingLive = request.status === LegacyRequestStatus.Pending && !isPast(request.expiresAt, now);+ if (isPendingLive) {+ checkPendingScope(request, data, failures);+ checkPendingApprovalState(request.id, steps, requestReviews, replay, failures, {+ failWhenAllStepsSatisfied: true+ });+ }++ const requestData: TRequestData = {+ permissions: request.permissions,+ isTemporary: request.isTemporary,+ temporaryRange: request.temporaryRange || null+ };++ rows.requests.push({+ id: request.id,+ projectId,+ organizationId: orgId,+ policyId: policy.id,+ requesterId: request.requestedByUserId,+ requesterName: `${requester?.firstName ?? ""} ${requester?.lastName ?? ""}`.trim() || (requester?.username ?? ""),+ requesterEmail: requester?.email || (requester?.username ?? ""),+ type: SECRET_ACCESS,+ status: toRequestStatus(request.status, { isApprovedLike, isPendingLive }),+ justification: request.note ?? null,+ currentStep: isApprovedLike ? Math.max(steps.length, 1) : replay.currentIndex + 1,+ requestData: { version: 1, requestData },+ expiresAt: request.expiresAt ?? null,+ machineIdentityId: null,+ scopeType: null,+ scopeId: null,+ createdAt: request.createdAt,+ updatedAt: request.updatedAt+ });++ const { requestSteps, eligibleApprovers, approvals } = buildRequestStepRows({+ requestId: request.id,+ steps,+ replay,+ isApprovedLike,+ requestCreatedAt: request.createdAt,+ fallbackCompletedAt: request.approvedAt ?? request.updatedAt+ });+ rows.requestSteps.push(...requestSteps);+ rows.eligibleApprovers.push(...eligibleApprovers);+ rows.approvals.push(...approvals);++ if (!isApprovedLike) return;++ const rangeMs = durationMs(request.temporaryRange);+ const windowEnd =+ rangeMs !== null && request.approvedAt ? new Date(new Date(request.approvedAt).getTime() + rangeMs) : null;+ const grantId = crypto.randomUUID();+ const grant = {+ id: grantId,+ projectId,+ requestId: request.id,+ granteeUserId: request.requestedByUserId,+ type: SECRET_ACCESS,+ attributes: requestData,+ createdAt: request.approvedAt ?? request.updatedAt,+ isBreakGlass: isBreakGlassApproval(policy, request, requestReviews),+ bypassReason: request.bypassReason ?? null,+ revokedAt: null as Date | null,+ revokedByUserId: null as string | null+ };++ if (request.status === LegacyRequestStatus.Revoked) {+ rows.grants.push({+ ...grant,+ status: GrantStatus.Revoked,+ expiresAt: windowEnd,+ revokedAt: request.revokedAt ?? request.updatedAt,+ revokedByUserId: request.revokedByUserId ?? null+ });+ } else if (additionalPrivilege) {+ const expiresAt = additionalPrivilege.isTemporary ? (additionalPrivilege.temporaryAccessEndTime ?? null) : null;+ rows.grants.push({+ ...grant,+ status: isPast(expiresAt, now) ? GrantStatus.Expired : GrantStatus.Active,+ expiresAt+ });+ rows.additionalPrivilegeGrants.push({ additionalPrivilegeId: additionalPrivilege.id, grantId });+ } else {+ rows.grants.push({+ ...grant,+ status: GrantStatus.Expired,+ expiresAt: windowEnd ?? request.privilegeDeletedAt ?? request.updatedAt+ });+ }+ });++ return { failures, rows, projectId };+};++const linkAdditionalPrivilegesToGrants = async (tx: Knex, pairs: TGlobalRows["additionalPrivilegeGrants"]) => {+ for (const chunk of chunkArray(pairs, INSERT_CHUNK_SIZE)) {+ const values = chunk.map(() => "(?::uuid, ?::uuid)").join(", ");+ await tx.raw(+ `UPDATE ${TableName.AdditionalPrivilege} AS ap+ SET "grantId" = m.grant_id+ FROM (VALUES ${values}) AS m(additional_privilege_id, grant_id)+ WHERE ap.id = m.additional_privilege_id`,+ chunk.flatMap(({ additionalPrivilegeId, grantId }) => [additionalPrivilegeId, grantId])+ );+ }+};++const readPolicyData = async (tx: Knex, policy: TLegacyPolicy, requests: TLegacyRequest[]): Promise<TPolicyData> => {+ const { id: policyId } = policy;++ const additionalPrivileges = (await tx(TableName.AdditionalPrivilege)+ .join(+ TableName.AccessApprovalRequest,+ `${TableName.AccessApprovalRequest}.privilegeId`,+ `${TableName.AdditionalPrivilege}.id`+ )+ .where(`${TableName.AccessApprovalRequest}.policyId`, policyId)+ .forUpdate(TableName.AdditionalPrivilege)+ .select(+ tx.ref("id").withSchema(TableName.AdditionalPrivilege),+ tx.ref("actorUserId").withSchema(TableName.AdditionalPrivilege),+ tx.ref("projectId").withSchema(TableName.AdditionalPrivilege),+ tx.ref("permissions").withSchema(TableName.AdditionalPrivilege),+ tx.ref("isTemporary").withSchema(TableName.AdditionalPrivilege),+ tx.ref("temporaryAccessEndTime").withSchema(TableName.AdditionalPrivilege),+ tx.ref("grantId").withSchema(TableName.AdditionalPrivilege)+ )) as TAdditionalPrivilege[];++ const environments = await readPolicyEnvironments(tx, LEGACY_TABLES, policyId);+ const approvers = await readLegacyApprovers(tx, LEGACY_TABLES, policyId);+ const bypassers = await readLegacyBypassers(tx, LEGACY_TABLES, policyId);+ const reviews = await readLegacyReviews(tx, LEGACY_TABLES, policyId);++ const requesters = (await tx(TableName.Users)+ .whereIn("id", tx(TableName.AccessApprovalRequest).where({ policyId }).select("requestedByUserId"))+ .select("id", "username", "email", "firstName", "lastName")) as TRequester[];++ const groupMembers = await readApproverGroupMembers(tx, approvers);+ const requestIdsAlreadyOnGlobal = await readRequestIdsAlreadyOnGlobal(tx, LEGACY_TABLES, policyId);++ return {+ policy,+ environments,+ approvers,+ bypassers,+ requests,+ reviews,+ additionalPrivileges: [+ ...new Map(+ additionalPrivileges.map((additionalPrivilege) => [additionalPrivilege.id, additionalPrivilege])+ ).values()+ ],+ requesters,+ groupMembers,+ requestIdsAlreadyOnGlobal+ };+};++const migratePolicy = async (tx: Knex, policyId: string): Promise<TPolicyOutcome> => {+ const policy = await lockLegacyPolicy<TLegacyPolicy>(tx, LEGACY_TABLES, policyId);+ if (!policy) return "skipped";++ const requests = (await tx(TableName.AccessApprovalRequest)+ .where({ policyId })+ .orderBy("id", "asc")+ .forUpdate()+ .select("*")) as TLegacyRequest[];++ const data = await readPolicyData(tx, policy, requests);+ const { failures, rows, projectId } = translatePolicy(data, new Date());++ if (failures.length) {+ logFailedChecks({ logPrefix: LOG_PREFIX, policyId, projectId, failures });+ return "failedChecks";+ }++ await insertGlobalPolicyRows(tx, rows);+ await insertGlobalRequestRows(tx, rows);+ await insertChunked(tx, TableName.ApprovalRequestGrants, rows.grants);+ await linkAdditionalPrivilegesToGrants(tx, rows.additionalPrivilegeGrants);++ return "migrated";+};++export async function up(knex: Knex): Promise<void> {+ if (+ !(await knex.schema.hasTable(TableName.AccessApprovalPolicy)) ||+ !(await knex.schema.hasTable(TableName.ApprovalPolicySecretEnvironment))+ ) {+ return;+ }++ await runLegacyPolicyMigration(knex, { tables: LEGACY_TABLES, logPrefix: LOG_PREFIX, migratePolicy });+}++export async function down(knex: Knex): Promise<void> {+ if (+ !(await knex.schema.hasTable(TableName.AccessApprovalPolicy)) ||+ !(await knex.schema.hasTable(TableName.ApprovalPolicies))+ ) {+ return;+ }++ const policyIds = (await knex(TableName.ApprovalPolicies)+ .where({ type: SECRET_ACCESS })+ .whereExists((qb) => {+ void qb+ .select(knex.raw("1"))+ .from(TableName.AccessApprovalPolicy)+ .whereRaw(`"${TableName.AccessApprovalPolicy}"."id" = "${TableName.ApprovalPolicies}"."id"`);+ })+ .pluck("id")) as string[];++ for (const policyId of policyIds) {+ await knex.transaction(async (tx) => {+ await tx.raw(`SET LOCAL statement_timeout = ${POLICY_STATEMENT_TIMEOUT_MS}`);+ const migratedRequestIds = () =>+ tx(TableName.ApprovalRequests)+ .where({ policyId })+ .whereExists((qb) => {+ void qb+ .select(tx.raw("1"))+ .from(TableName.AccessApprovalRequest)+ .whereRaw(`"${TableName.AccessApprovalRequest}"."id" = "${TableName.ApprovalRequests}"."id"`);+ })+ .select("id");++ await tx(TableName.ApprovalRequestGrants).whereIn("requestId", migratedRequestIds()).delete();+ await tx(TableName.ApprovalRequests).whereIn("id", migratedRequestIds()).delete();+ await tx(TableName.ApprovalPolicies).where({ id: policyId }).delete();+ });+ }+}++const config = { transaction: false };+export { config };diff --git a/backend/src/db/migrations/utils/access-approval-migration-scenarios.test.ts b/backend/src/db/migrations/utils/access-approval-migration-scenarios.test.tsnew file mode 100644index 0000000000..1084b60de8--- /dev/null+++ b/backend/src/db/migrations/utils/access-approval-migration-scenarios.test.ts@@ -0,0 +1,647 @@+// Lives under utils/ because tsdown compiles every src/db/migrations/*.ts into dist/db/migrations/,+// where knex would load a test file as a migration.+import { describe, expect, test } from "vitest";++import { TPolicyData, translatePolicy } from "../20261013120000_migrate-access-approval-policies-to-global";+import { TLegacyApprover, TLegacyReview, TMigrationFailure } from "./approval-migration";++type TRequest = TPolicyData["requests"][number];+type TAdditionalPrivilege = TPolicyData["additionalPrivileges"][number];++const T0 = new Date("2026-01-01T00:00:00Z");+const at = (minutes: number) => new Date(T0.getTime() + minutes * 60_000);+const NOW = at(60);++const permissionsFor = (environment = "dev", secretPath = "/apps/api") => [+ ["read", "secrets", { environment, secretPath: { $glob: secretPath } }]+];++const approver = (approverUserId: string, opts: Partial<TLegacyApprover> = {}): TLegacyApprover => ({+ approverUserId,+ approverGroupId: null,+ sequence: 1,+ approvalsRequired: 1,+ ...opts+});++const groupApprover = (approverGroupId: string, opts: Partial<TLegacyApprover> = {}): TLegacyApprover => ({+ approverUserId: null,+ approverGroupId,+ sequence: 1,+ approvalsRequired: 1,+ ...opts+});++const env = (envSlug: string, projectId = "P") => ({ envId: `env-${envSlug}`, envSlug, projectId, orgId: "O" });++const scenario = (overrides: Partial<TPolicyData> = {}): TPolicyData => ({+ policy: {+ id: "pol-1",+ name: "Prod access",+ approvals: 1,+ secretPath: "/apps/**",+ enforcementLevel: "hard",+ allowedSelfApprovals: false,+ maxTimePeriod: "7d",+ requestExpirationTime: "1h",+ deletedAt: null,+ createdAt: at(0),+ updatedAt: at(1)+ },+ environments: [env("dev")],+ approvers: [approver("u1"), approver("u2")],+ bypassers: [],+ requests: [],+ reviews: [],+ additionalPrivileges: [],+ requesters: [{ id: "r1", username: "[email protected]", email: "[email protected]", firstName: "Ria", lastName: "Lee" }],+ groupMembers: [],+ requestIdsAlreadyOnGlobal: [],+ ...overrides+});++const request = (overrides: Partial<TRequest> = {}): TRequest => ({+ id: "req-1",+ requestedByUserId: "r1",+ status: "pending",+ permissions: permissionsFor(),+ isTemporary: false,+ temporaryRange: null,+ note: "need it",+ privilegeId: null,+ privilegeDeletedAt: null,+ expiresAt: at(120),+ approvedAt: null,+ revokedAt: null,+ revokedByUserId: null,+ bypassReason: null,+ createdAt: at(10),+ updatedAt: at(10),+ ...overrides+});++const approvedRequest = (overrides: Partial<TRequest> = {}) =>+ request({ status: "approved", approvedAt: at(20), updatedAt: at(20), privilegeId: "priv-1", ...overrides });++const additionalPrivilege = (overrides: Partial<TAdditionalPrivilege> = {}): TAdditionalPrivilege => ({+ id: "priv-1",+ actorUserId: "r1",+ projectId: "P",+ permissions: permissionsFor(),+ isTemporary: false,+ temporaryAccessEndTime: null,+ grantId: null,+ ...overrides+});++const review = (+ id: string,+ reviewerUserId: string,+ status: string,+ createdAt: Date,+ requestId = "req-1"+): TLegacyReview => ({ id, requestId, reviewerUserId, status, createdAt });++const run = (data: TPolicyData) => translatePolicy(data, NOW);+const codes = (failures: TMigrationFailure[]) => failures.map((failure) => failure.code);++describe("access approval migration scenarios that migrate", () => {+ test("translates a policy with two sequences, a group approver and bypassers", () => {+ const { failures, rows, projectId } = run(+ scenario({+ environments: [env("dev"), env("prod")],+ approvers: [approver("u1"), groupApprover("g1"), approver("u2", { sequence: 2 })],+ groupMembers: [{ groupId: "g1", userId: "u3" }],+ bypassers: [{ bypasserUserId: "b1" }, { bypasserUserId: "b1" }, { bypasserGroupId: "g2" }]+ })+ );++ expect(failures).toEqual([]);+ expect(projectId).toBe("P");+ expect(rows.policy).toEqual({+ id: "pol-1",+ projectId: "P",+ organizationId: "O",+ type: "secret-access",+ name: "Prod access",+ isActive: true,+ maxRequestTtl: null,+ conditions: { version: 1, conditions: {} },+ constraints: {+ version: 1,+ constraints: { allowedSelfApprovals: false, requestExpirationTime: "1h", maxTimePeriod: "7d" }+ },+ enforcementLevel: "hard",+ bypassForMachineIdentities: false,+ scopeType: null,+ scopeId: null,+ createdAt: at(0),+ updatedAt: at(1)+ });+ expect(rows.secretEnvironments).toEqual([+ { policyId: "pol-1", envId: "env-dev", secretPath: "/apps/**" },+ { policyId: "pol-1", envId: "env-prod", secretPath: "/apps/**" }+ ]);+ expect(rows.policySteps).toMatchObject([+ { policyId: "pol-1", stepNumber: 1, requiredApprovals: 1 },+ { policyId: "pol-1", stepNumber: 2, requiredApprovals: 1 }+ ]);+ expect(rows.policyStepApprovers).toEqual([+ { policyStepId: rows.policySteps[0].id, userId: "u1", groupId: null },+ { policyStepId: rows.policySteps[0].id, userId: null, groupId: "g1" },+ { policyStepId: rows.policySteps[1].id, userId: "u2", groupId: null }+ ]);+ expect(rows.bypassers).toEqual([+ { policyId: "pol-1", userId: "b1", groupId: null },+ { policyId: "pol-1", userId: null, groupId: "g2" }+ ]);+ expect(rows.requests).toEqual([]);+ expect(rows.grants).toEqual([]);+ });++ test("carries a live pending request over with its first step completed and the second in progress", () => {+ const { failures, rows } = run(+ scenario({+ approvers: [approver("u1"), approver("u2", { sequence: 2 })],+ requests: [request()],+ reviews: [review("rv1", "u1", "approved", at(15))]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.requests).toEqual([+ {+ id: "req-1",+ projectId: "P",+ organizationId: "O",+ policyId: "pol-1",+ requesterId: "r1",+ requesterName: "Ria Lee",+ requesterEmail: "[email protected]",+ type: "secret-access",+ status: "pending",+ justification: "need it",+ currentStep: 2,+ requestData: {+ version: 1,+ requestData: { permissions: permissionsFor(), isTemporary: false, temporaryRange: null }+ },+ expiresAt: at(120),+ machineIdentityId: null,+ scopeType: null,+ scopeId: null,+ createdAt: at(10),+ updatedAt: at(10)+ }+ ]);+ expect(rows.requestSteps).toEqual([+ expect.objectContaining({ stepNumber: 1, status: "completed", startedAt: at(10), completedAt: at(15) }),+ expect.objectContaining({ stepNumber: 2, status: "in-progress", startedAt: at(15), completedAt: null })+ ]);+ expect(rows.eligibleApprovers).toEqual([+ { stepId: rows.requestSteps[0].id, userId: "u1", groupId: null },+ { stepId: rows.requestSteps[1].id, userId: "u2", groupId: null }+ ]);+ expect(rows.approvals).toEqual([+ { stepId: rows.requestSteps[0].id, approverUserId: "u1", decision: "approved", comment: null, createdAt: at(15) }+ ]);+ expect(rows.grants).toEqual([]);+ });++ test("turns an approved request with a live additional privilege into an active grant linked to the additional privilege", () => {+ const { failures, rows } = run(+ scenario({+ requests: [approvedRequest()],+ additionalPrivileges: [additionalPrivilege()],+ reviews: [review("rv1", "u1", "approved", at(20))]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.requests[0]).toMatchObject({ status: "approved", currentStep: 1 });+ expect(rows.requestSteps).toEqual([+ expect.objectContaining({ status: "completed", startedAt: at(10), completedAt: at(20) })+ ]);+ expect(rows.grants).toEqual([+ {+ id: expect.any(String),+ projectId: "P",+ requestId: "req-1",+ granteeUserId: "r1",+ type: "secret-access",+ attributes: { permissions: permissionsFor(), isTemporary: false, temporaryRange: null },+ createdAt: at(20),+ isBreakGlass: false,+ bypassReason: null,+ revokedAt: null,+ revokedByUserId: null,+ status: "active",+ expiresAt: null+ }+ ]);+ expect(rows.additionalPrivilegeGrants).toEqual([{ additionalPrivilegeId: "priv-1", grantId: rows.grants[0].id }]);+ });++ test("takes the grant expiry from a temporary additional privilege and marks it expired once past", () => {+ const live = run(+ scenario({+ requests: [approvedRequest({ isTemporary: true, temporaryRange: "2h" })],+ additionalPrivileges: [additionalPrivilege({ isTemporary: true, temporaryAccessEndTime: at(90) })]+ })+ );+ const past = run(+ scenario({+ requests: [approvedRequest({ isTemporary: true, temporaryRange: "2h" })],+ additionalPrivileges: [additionalPrivilege({ isTemporary: true, temporaryAccessEndTime: at(40) })]+ })+ );++ expect(live.failures).toEqual([]);+ expect(live.rows.grants[0]).toMatchObject({ status: "active", expiresAt: at(90) });+ expect(past.failures).toEqual([]);+ expect(past.rows.grants[0]).toMatchObject({ status: "expired", expiresAt: at(40) });+ });++ test("writes an expired grant for an approved request whose additional privilege is already gone", () => {+ const withRange = run(+ scenario({+ requests: [+ approvedRequest({ privilegeId: null, privilegeDeletedAt: at(50), isTemporary: true, temporaryRange: "1h" })+ ]+ })+ );+ const withoutRange = run(+ scenario({ requests: [approvedRequest({ privilegeId: null, privilegeDeletedAt: at(50) })] })+ );++ expect(withRange.failures).toEqual([]);+ expect(withRange.rows.grants[0]).toMatchObject({ status: "expired", expiresAt: at(80) });+ expect(withRange.rows.additionalPrivilegeGrants).toEqual([]);+ expect(withoutRange.failures).toEqual([]);+ expect(withoutRange.rows.grants[0]).toMatchObject({ status: "expired", expiresAt: at(50) });+ });++ test("keeps a revoked request approved with a revoked grant", () => {+ const { failures, rows } = run(+ scenario({+ requests: [+ request({+ status: "revoked",+ approvedAt: at(20),+ revokedAt: at(30),+ revokedByUserId: "adm",+ isTemporary: true,+ temporaryRange: "1h",+ updatedAt: at(30)+ })+ ]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.requests[0]).toMatchObject({ status: "approved", currentStep: 1 });+ expect(rows.grants[0]).toMatchObject({+ status: "revoked",+ expiresAt: at(80),+ revokedAt: at(30),+ revokedByUserId: "adm"+ });+ expect(rows.additionalPrivilegeGrants).toEqual([]);+ });++ test("carries a rejected request over without a grant", () => {+ const { failures, rows } = run(+ scenario({ requests: [request({ status: "rejected" })], reviews: [review("rv1", "u1", "rejected", at(15))] })+ );++ expect(failures).toEqual([]);+ expect(rows.requests[0]).toMatchObject({ status: "rejected", currentStep: 1 });+ expect(rows.requestSteps[0]).toMatchObject({ status: "in-progress" });+ expect(rows.approvals[0]).toMatchObject({ decision: "rejected" });+ expect(rows.grants).toEqual([]);+ });++ test("expires a pending request past its deadline without running the live-request checks", () => {+ const { failures, rows } = run(+ scenario({ requests: [request({ expiresAt: at(30), permissions: permissionsFor("dev", "/other/x") })] })+ );++ expect(failures).toEqual([]);+ expect(rows.requests[0]).toMatchObject({ status: "expired", expiresAt: at(30) });+ });++ test("records a break-glass approval on the grant", () => {+ const { failures, rows } = run(+ scenario({+ requests: [approvedRequest({ bypassReason: "incident 42" })],+ additionalPrivileges: [additionalPrivilege()]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.grants[0]).toMatchObject({ isBreakGlass: true, bypassReason: "incident 42" });+ });+ test("treats a requester's own approval on a soft policy as break-glass even without a reason", () => {+ const { failures, rows } = run(+ scenario({+ policy: { ...scenario().policy, enforcementLevel: "soft" },+ requests: [approvedRequest()],+ additionalPrivileges: [additionalPrivilege()],+ reviews: [review("rv1", "r1", "approved", at(20))]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.grants[0]).toMatchObject({ isBreakGlass: true, bypassReason: null });+ });++ test("keeps a requester's own approval on a hard policy as a regular approval", () => {+ const { failures, rows } = run(+ scenario({+ policy: { ...scenario().policy, allowedSelfApprovals: true },+ approvers: [approver("r1"), approver("u2")],+ requests: [approvedRequest()],+ additionalPrivileges: [additionalPrivilege()],+ reviews: [review("rv1", "r1", "approved", at(20))]+ })+ );++ expect(failures).toEqual([]);+ expect(rows.grants[0]).toMatchObject({ isBreakGlass: false, bypassReason: null });+ });++ test("counts a user named directly and through a group once on a pending request", () => {+ const { failures } = run(+ scenario({+ approvers: [approver("u1", { approvalsRequired: 2 }), groupApprover("g1", { approvalsRequired: 2 })],+ groupMembers: [+ { groupId: "g1", userId: "u1" },+ { groupId: "g1", userId: "u2" }+ ],+ requests: [request()],+ reviews: [review("rv1", "u1", "approved", at(15))]+ })+ );++ expect(failures).toEqual([]);+ });+});++describe("access approval migration scenarios that fail the checks", () => {+ test("policy without environments", () => {+ const { failures, projectId } = run(scenario({ environments: [] }));++ expect(codes(failures)).toEqual(["no-environments"]);+ expect(projectId).toBeUndefined();+ });++ test("policy whose environments span projects", () => {+ const { failures } = run(scenario({ environments: [env("dev", "P1"), env("prod", "P2")] }));++ expect(codes(failures)).toEqual(["environments-span-projects"]);+ });++ test("policy without approvers", () => {+ expect(codes(run(scenario({ approvers: [] })).failures)).toEqual(["no-approvers"]);+ });++ test("approvers that disagree on the approvals a sequence needs", () => {+ const { failures } = run(+ scenario({ approvers: [approver("u1", { approvalsRequired: 2 }), approver("u2", { approvalsRequired: 1 })] })+ );++ expect(codes(failures)).toEqual(["step-approvals-disagree"]);+ });++ test("policy asking for several approvals while its first sequence has no approvalsRequired", () => {+ const { failures } = run(+ scenario({+ policy: { ...scenario().policy, approvals: 2 },+ approvers: [approver("u1", { approvalsRequired: null }), approver("u2", { approvalsRequired: null })]+ })+ );++ expect(codes(failures)).toEqual(["ambiguous-required-approvals"]);+ });++ test("sequence that needs more approvals than it has user approvers", () => {+ const { failures } = run(scenario({ approvers: [approver("u1", { approvalsRequired: 2 })] }));++ expect(codes(failures)).toEqual(["required-approvals-exceed-approvers"]);+ });++ test("sequence whose only group has no members", () => {+ expect(codes(run(scenario({ approvers: [groupApprover("g1")] })).failures)).toEqual([+ "step-without-eligible-approvers"+ ]);+ });++ test("request that already exists on the global system", () => {+ const { failures } = run(scenario({ requests: [request()], requestIdsAlreadyOnGlobal: ["req-1"] }));++ expect(codes(failures)).toEqual(["request-already-migrated"]);+ });++ test("request with a status the global system has no equivalent for", () => {+ const { failures } = run(scenario({ requests: [request({ status: "weird" })] }));++ expect(failures).toEqual([expect.objectContaining({ code: "unknown-request-status", requestId: "req-1" })]);+ });++ test("review with a status that is neither approved nor rejected", () => {+ const { failures } = run(scenario({ requests: [request()], reviews: [review("rv1", "u1", "pending", at(15))] }));++ expect(failures).toEqual([expect.objectContaining({ code: "unknown-review-status", requestId: "req-1" })]);+ });++ test("request whose requester no longer exists", () => {+ const { failures, rows } = run(scenario({ requests: [request({ requestedByUserId: "ghost" })] }));++ expect(failures).toEqual([expect.objectContaining({ code: "requester-missing", requestId: "req-1" })]);+ expect(rows.requests[0]).toMatchObject({ requesterName: "", requesterEmail: "" });+ });++ describe("additional privilege state", () => {+ const additionalPrivilegeFailures = (failures: TMigrationFailure[], requestId = "req-1") =>+ failures+ .filter((failure) => failure.code === "privilege-state-mismatch" && failure.requestId === requestId)+ .map((failure) => failure.message);++ test("pending request still pointing at a additional privilege", () => {+ const { failures } = run(+ scenario({ requests: [request({ privilegeId: "priv-1" })], additionalPrivileges: [additionalPrivilege()] })+ );++ expect(additionalPrivilegeFailures(failures)).toEqual([+ "A pending request still points to additional privilege priv-1"+ ]);+ });++ test("approved request whose additional privilege row is missing", () => {+ const { failures } = run(scenario({ requests: [approvedRequest()] }));++ expect(additionalPrivilegeFailures(failures)).toEqual(["Additional privilege priv-1 does not exist"]);+ });++ test("additional privilege held by another user", () => {+ const { failures } = run(+ scenario({+ requests: [approvedRequest()],+ additionalPrivileges: [additionalPrivilege({ actorUserId: "someone-else" })]+ })+ );++ expect(additionalPrivilegeFailures(failures)).toEqual([+ "Additional privilege priv-1 belongs to another user or project"+ ]);+ });++ test("additional privilege in another project", () => {+ const { failures } = run(+ scenario({ requests: [approvedRequest()], additionalPrivileges: [additionalPrivilege({ projectId: "P2" })] })+ );++ expect(additionalPrivilegeFailures(failures)).toEqual([+ "Additional privilege priv-1 belongs to another user or project"+ ]);+ });++ test("additional privilege already linked to a grant", () => {+ const { failures } = run(+ scenario({ requests: [approvedRequest()], additionalPrivileges: [additionalPrivilege({ grantId: "grant-x" })] })+ );++ expect(additionalPrivilegeFailures(failures)).toEqual([+ "Additional privilege priv-1 is already linked to grant grant-x"+ ]);+ });++ test("additional privilege shared by two requests flags both", () => {+ const { failures } = run(+ scenario({+ requests: [approvedRequest(), approvedRequest({ id: "req-2" })],+ additionalPrivileges: [additionalPrivilege()]+ })+ );++ expect(additionalPrivilegeFailures(failures, "req-1")).toEqual([+ "Additional privilege priv-1 is shared with requests req-2"+ ]);+ expect(additionalPrivilegeFailures(failures, "req-2")).toEqual([+ "Additional privilege priv-1 is shared with requests req-1"+ ]);+ expect(failures).toHaveLength(2);+ });++ test("additional privilege granting different permissions than the request asked for", () => {+ const { failures } = run(+ scenario({+ requests: [approvedRequest()],+ additionalPrivileges: [additionalPrivilege({ permissions: permissionsFor("dev", "/apps/other") })]+ })+ );++ expect(failures).toEqual([+ expect.objectContaining({ code: "privilege-permissions-mismatch", requestId: "req-1" })+ ]);+ });+ });++ describe("pending request scope", () => {+ const scopeFailures = (failures: TMigrationFailure[]) =>+ failures.filter((failure) => failure.code === "pending-request-out-of-scope").map((f) => f.message);++ test("request for an environment the policy does not cover", () => {+ const { failures } = run(scenario({ requests: [request({ permissions: permissionsFor("prod") })] }));++ expect(scopeFailures(failures)).toEqual([+ "The request is for environment 'prod', which the policy does not cover"+ ]);+ expect(failures).toHaveLength(1);+ });++ test("request for a secret path outside the policy path", () => {+ const { failures } = run(scenario({ requests: [request({ permissions: permissionsFor("dev", "/other/x") })] }));++ expect(scopeFailures(failures)).toEqual([+ "The request is for secret path '/other/x', which policy path '/apps/**' does not cover"+ ]);+ expect(failures).toHaveLength(1);+ });++ test("request whose permissions cannot be parsed", () => {+ const { failures } = run(+ scenario({ requests: [request({ permissions: [["read", "secrets", { environment: "dev" }]] })] })+ );++ expect(scopeFailures(failures)).toEqual([expect.stringMatching(/^The requested permissions are invalid:/)]);+ expect(failures).toHaveLength(1);+ });+ });++ describe("pending request state", () => {+ test("every step satisfied while the request is still pending", () => {+ const { failures } = run(scenario({ requests: [request()], reviews: [review("rv1", "u1", "approved", at(15))] }));++ expect(failures).toEqual([+ expect.objectContaining({+ code: "pending-request-state-mismatch",+ requestId: "req-1",+ message: "Every step already has enough approvals, yet the request is still pending"+ })+ ]);+ });++ test("approving reviewer who is an approver in two sequences", () => {+ const { failures } = run(+ scenario({+ approvers: [approver("u1"), approver("u1", { sequence: 2 }), approver("u2", { sequence: 2 })],+ requests: [request()],+ reviews: [review("rv1", "u1", "approved", at(15))]+ })+ );++ expect(failures).toContainEqual(+ expect.objectContaining({+ code: "pending-request-state-mismatch",+ requestId: "req-1",+ message: expect.stringContaining("is an approver in sequences 1, 2")+ })+ );+ });++ test("approval the global system would count but the legacy system does not", () => {+ const { failures } = run(+ scenario({+ approvers: [approver("u1"), approver("u2", { sequence: 2 })],+ requests: [request()],+ reviews: [review("rv1", "ux", "approved", at(15))]+ })+ );++ expect(codes(failures)).toEqual(["pending-request-state-mismatch", "pending-request-state-mismatch"]);+ expect(failures.map((failure) => failure.message)).toEqual([+ "The legacy approval system is waiting on sequence 1, the global approval system would wait on sequence 2",+ "Sequence 1 counts 0 approval(s) on the legacy approval system and 1 on the global approval system"+ ]);+ });+ });++ test("the ambiguous policy from the blackbox suite reports exactly its three failures", () => {+ const { failures } = run(+ scenario({+ policy: { ...scenario().policy, approvals: 2 },+ approvers: [approver("u1", { approvalsRequired: null }), approver("u2", { approvalsRequired: null })],+ requests: [request(), approvedRequest({ id: "req-2", privilegeId: "priv-2" })],+ reviews: [review("rv1", "u1", "approved", at(15))],+ additionalPrivileges: [additionalPrivilege({ id: "priv-2", actorUserId: "u1" })]+ })+ );++ expect(failures).toEqual([+ expect.objectContaining({ code: "ambiguous-required-approvals" }),+ expect.objectContaining({ code: "pending-request-state-mismatch", requestId: "req-1" }),+ expect.objectContaining({ code: "privilege-state-mismatch", requestId: "req-2" })+ ]);+ });+});diff --git a/backend/src/db/migrations/utils/approval-migration.test.ts b/backend/src/db/migrations/utils/approval-migration.test.tsnew file mode 100644index 0000000000..e3504c5111--- /dev/null+++ b/backend/src/db/migrations/utils/approval-migration.test.ts@@ -0,0 +1,711 @@+import { Knex } from "knex";+import { beforeEach, describe, expect, test, vi } from "vitest";++import { logger } from "@app/lib/logger";++import {+ buildApprovalSteps,+ buildBypasserRows,+ buildGlobalPolicyRow,+ buildPolicyEnvironmentRows,+ buildPolicyStepRows,+ buildRequestStepRows,+ checkPendingApprovalState,+ checkPolicyEnvironments,+ checkRequestsAlreadyMigrated,+ checkReviewStatuses,+ formatFailure,+ logFailedChecks,+ replayReviews,+ runLegacyPolicyMigration,+ TGroupMember,+ TLegacyApprover,+ TLegacyReview,+ TMigrationFailure,+ TPolicyEnvironment,+ TPolicyOutcome+} from "./approval-migration";++vi.mock("@app/lib/logger", () => ({+ initLogger: vi.fn(),+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }+}));++const T0 = new Date("2026-01-01T00:00:00Z");+const at = (minutes: number) => new Date(T0.getTime() + minutes * 60_000);++const approver = (approverUserId: string, opts: Partial<TLegacyApprover> = {}): TLegacyApprover => ({+ approverUserId,+ approverGroupId: null,+ sequence: 1,+ approvalsRequired: 1,+ ...opts+});++const groupApprover = (approverGroupId: string, opts: Partial<TLegacyApprover> = {}): TLegacyApprover => ({+ approverUserId: null,+ approverGroupId,+ sequence: 1,+ approvalsRequired: 1,+ ...opts+});++const review = (+ id: string,+ reviewerUserId: string,+ status: string,+ createdAt: Date,+ comment?: string | null+): TLegacyReview => ({ id, requestId: "req-1", reviewerUserId, status, createdAt, comment });++const env = (envId: string, projectId = "P", orgId = "O"): TPolicyEnvironment => ({+ envId,+ envSlug: envId,+ projectId,+ orgId+});++const codes = (failures: TMigrationFailure[]) => failures.map((failure) => failure.code);++const stepsOf = (approvers: TLegacyApprover[], groupMembers: TGroupMember[] = [], approvals = 1) => {+ const failures: TMigrationFailure[] = [];+ const steps = buildApprovalSteps({ policy: { approvals }, approvers, groupMembers }, failures);+ expect(failures).toEqual([]);+ return steps;+};++const legacyTables = {+ legacyPolicies: "legacy_policies",+ legacyApprovers: "legacy_approvers",+ legacyBypassers: "legacy_bypassers",+ legacyEnvironments: "legacy_environments",+ legacyRequests: "legacy_requests",+ legacyReviewers: "legacy_reviewers"+} as unknown as Parameters<typeof runLegacyPolicyMigration>[1]["tables"];++beforeEach(() => {+ vi.clearAllMocks();+});++describe("formatFailure", () => {+ test("prints the code and message", () => {+ expect(formatFailure({ code: "no-approvers", message: "none" })).toBe("no-approvers: none");+ });++ test("tags the request when the failure has one", () => {+ expect(formatFailure({ code: "requester-missing", message: "gone", requestId: "req-1" })).toBe(+ "requester-missing [requestId=req-1]: gone"+ );+ });+});++describe("logFailedChecks", () => {+ test("writes one warning carrying every reason in the searchable format", () => {+ const failures: TMigrationFailure[] = [+ { code: "a", message: "m1" },+ { code: "b", message: "m2", requestId: "r" }+ ];++ logFailedChecks({ logPrefix: "pfx", policyId: "p1", failures });++ expect(logger.warn).toHaveBeenCalledTimes(1);+ const [context, message] = vi.mocked(logger.warn).mock.calls[0] as [unknown, string];+ expect(context).toEqual({ policyId: "p1", projectId: undefined, failures });+ expect(message).toBe(+ "pfx: policy not migrated, it stays on the legacy approval system [policyId=p1] [projectId=unknown] [failedChecks=2] reasons=[a: m1; b [requestId=r]: m2]"+ );+ });++ test("prints the project when it is known", () => {+ logFailedChecks({ logPrefix: "pfx", policyId: "p1", projectId: "P", failures: [{ code: "a", message: "m" }] });++ expect(vi.mocked(logger.warn).mock.calls[0][1]).toContain("[projectId=P] [failedChecks=1]");+ });+});++describe("checkPolicyEnvironments", () => {+ test("returns the project and org of the policy's environments", () => {+ const failures: TMigrationFailure[] = [];++ expect(checkPolicyEnvironments([env("dev"), env("prod")], failures)).toEqual({ projectId: "P", orgId: "O" });+ expect(failures).toEqual([]);+ });++ test("fails a policy with no environments", () => {+ const failures: TMigrationFailure[] = [];++ expect(checkPolicyEnvironments([], failures)).toEqual({ projectId: undefined, orgId: undefined });+ expect(codes(failures)).toEqual(["no-environments"]);+ });++ test("fails a policy whose environments belong to several projects", () => {+ const failures: TMigrationFailure[] = [];++ checkPolicyEnvironments([env("dev", "P1"), env("prod", "P2")], failures);++ expect(codes(failures)).toEqual(["environments-span-projects"]);+ expect(failures[0].message).toContain("P1, P2");+ });+});++describe("buildApprovalSteps", () => {+ test("groups approvers by sequence in ascending order", () => {+ const steps = stepsOf([approver("u3", { sequence: 3 }), approver("u1"), approver("u2")]);++ expect(steps.map((step) => step.sequence)).toEqual([1, 3]);+ expect(steps[0].userIds).toEqual(["u1", "u2"]);+ expect(steps[1].userIds).toEqual(["u3"]);+ });++ test("treats a missing sequence as the first one", () => {+ const steps = stepsOf([approver("u1", { sequence: null }), approver("u2", { sequence: undefined })]);++ expect(steps).toHaveLength(1);+ expect(steps[0]).toMatchObject({ sequence: 1, requiredApprovals: 1, userIds: ["u1", "u2"] });+ });++ test("fails a policy without approvers", () => {+ const failures: TMigrationFailure[] = [];++ const steps = buildApprovalSteps({ policy: { approvals: 1 }, approvers: [], groupMembers: [] }, failures);++ expect(steps).toEqual([]);+ expect(codes(failures)).toEqual(["no-approvers"]);+ });++ test("fails when approvers of one sequence disagree on the approvals required", () => {+ const failures: TMigrationFailure[] = [];++ const steps = buildApprovalSteps(+ {+ policy: { approvals: 1 },+ approvers: [approver("u1", { approvalsRequired: 2 }), approver("u2", { approvalsRequired: null })],+ groupMembers: []+ },+ failures+ );++ expect(codes(failures)).toEqual(["step-approvals-disagree"]);+ expect(failures[0].message).toContain("(2, unset)");+ expect(steps[0].requiredApprovals).toBe(2);+ });++ test("fails when the first sequence has no approvalsRequired while the policy asks for more than one", () => {+ const failures: TMigrationFailure[] = [];++ const steps = buildApprovalSteps(+ {+ policy: { approvals: 2 },+ approvers: [approver("u1", { approvalsRequired: null }), approver("u2", { approvalsRequired: null })],+ groupMembers: []+ },+ failures+ );++ expect(codes(failures)).toEqual(["ambiguous-required-approvals"]);+ expect(steps[0].requiredApprovals).toBe(1);+ });++ test("does not treat a later sequence without approvalsRequired as ambiguous", () => {+ const steps = stepsOf(+ [+ approver("u1", { approvalsRequired: 2 }),+ approver("u2", { approvalsRequired: 2 }),+ approver("u3", { sequence: 2, approvalsRequired: null })+ ],+ [],+ 2+ );++ expect(steps.map((step) => step.requiredApprovals)).toEqual([2, 1]);+ });++ test("fails when a sequence of user approvers cannot reach its required approvals", () => {+ const failures: TMigrationFailure[] = [];++ buildApprovalSteps(+ { policy: { approvals: 1 }, approvers: [approver("u1", { approvalsRequired: 2 })], groupMembers: [] },+ failures+ );++ expect(codes(failures)).toEqual(["required-approvals-exceed-approvers"]);+ expect(failures[0].message).toContain("requires 2 approvals but has only 1 user approver");+ });++ test("lets a group cover the required approvals", () => {+ const steps = stepsOf(+ [approver("u1", { approvalsRequired: 2 }), groupApprover("g1", { approvalsRequired: 2 })],+ [{ groupId: "g1", userId: "u2" }]+ );++ expect(steps[0].eligibleUserIds).toEqual(new Set(["u1", "u2"]));+ });++ test("fails a sequence whose only approvers are groups without members", () => {+ const failures: TMigrationFailure[] = [];++ const steps = buildApprovalSteps(+ { policy: { approvals: 1 }, approvers: [groupApprover("g1")], groupMembers: [] },+ failures+ );++ expect(codes(failures)).toEqual(["step-without-eligible-approvers"]);+ expect(steps[0]).toMatchObject({ groupIds: ["g1"], eligibleUserIds: new Set() });+ });++ test("dedupes repeated users and groups and merges group members into the eligible set", () => {+ const steps = stepsOf(+ [approver("u1"), approver("u1"), groupApprover("g1"), groupApprover("g1")],+ [+ { groupId: "g1", userId: "u1" },+ { groupId: "g1", userId: "u2" }+ ]+ );++ expect(steps[0]).toMatchObject({+ userIds: ["u1"],+ groupIds: ["g1"],+ groupMemberIds: new Set(["u1", "u2"]),+ eligibleUserIds: new Set(["u1", "u2"])+ });+ });++ test("builds one step from approvers normalized to the policy's approvals, as a system without sequences does", () => {+ const steps = stepsOf(+ [+ approver("u1", { approvalsRequired: 3 }),+ approver("u2", { approvalsRequired: 3 }),+ groupApprover("g1", { approvalsRequired: 3 })+ ],+ [{ groupId: "g1", userId: "u3" }],+ 3+ );++ expect(steps).toHaveLength(1);+ expect(steps[0]).toMatchObject({ sequence: 1, requiredApprovals: 3, eligibleUserIds: new Set(["u1", "u2", "u3"]) });+ });+});++describe("replayReviews", () => {+ const twoSteps = () => stepsOf([approver("u1"), approver("u2", { sequence: 2 })]);++ test("starts at the first step and attributes nothing when there are no steps", () => {+ expect(replayReviews([], [review("r1", "u1", "approved", at(1))])).toEqual({+ approvedCounts: [],+ completedAt: [],+ attributed: [],+ currentIndex: 0+ });+ });++ test("advances to the next step once a step has enough approvals", () => {+ const replay = replayReviews(twoSteps(), [review("r1", "u1", "approved", at(1))]);++ expect(replay).toMatchObject({ approvedCounts: [1, 0], completedAt: [at(1), null], currentIndex: 1 });+ expect(replay.attributed).toEqual([{ review: expect.objectContaining({ id: "r1" }), stepIndex: 0 }]);+ });++ test("replays reviews in creation order, then by id, and stays on the last step when all are satisfied", () => {+ const replay = replayReviews(twoSteps(), [+ review("b", "u2", "approved", at(2)),+ review("a", "u1", "approved", at(1)),+ review("c", "u2", "rejected", at(2))+ ]);++ expect(replay.attributed.map(({ review: r, stepIndex }) => [r.id, stepIndex])).toEqual([+ ["a", 0],+ ["b", 1],+ ["c", 1]+ ]);+ expect(replay).toMatchObject({ approvedCounts: [1, 1], completedAt: [at(1), at(2)], currentIndex: 1 });+ });++ test("attributes an approval to the step the reviewer is eligible for", () => {+ const replay = replayReviews(twoSteps(), [review("r1", "u2", "approved", at(1))]);++ expect(replay.attributed[0].stepIndex).toBe(1);+ expect(replay).toMatchObject({ approvedCounts: [0, 1], currentIndex: 0 });+ });++ test("counts an approval from a reviewer eligible nowhere on the current step", () => {+ const replay = replayReviews(twoSteps(), [review("r1", "ux", "approved", at(1))]);++ expect(replay.attributed[0].stepIndex).toBe(0);+ expect(replay).toMatchObject({ approvedCounts: [1, 0], currentIndex: 1 });+ });++ test("attributes a rejection without counting it", () => {+ const replay = replayReviews(twoSteps(), [review("r1", "u1", "rejected", at(1))]);++ expect(replay.attributed[0].stepIndex).toBe(0);+ expect(replay).toMatchObject({ approvedCounts: [0, 0], completedAt: [null, null], currentIndex: 0 });+ });+});++describe("checkPendingApprovalState", () => {+ const check = (steps: ReturnType<typeof stepsOf>, reviews: TLegacyReview[], failWhenAllStepsSatisfied = true) => {+ const failures: TMigrationFailure[] = [];+ checkPendingApprovalState("req-1", steps, reviews, replayReviews(steps, reviews), failures, {+ failWhenAllStepsSatisfied+ });+ return failures;+ };++ test("reports a policy without steps as skipped", () => {+ const failures = check([], []);++ expect(failures).toEqual([+ { code: "pending-request-state-mismatch", requestId: "req-1", message: expect.stringMatching(/^skipped:/) }+ ]);+ });++ test("passes a pending request whose state both systems agree on", () => {+ const steps = stepsOf([approver("u1"), approver("u2", { sequence: 2 })]);++ expect(check(steps, [review("r1", "u1", "approved", at(1))])).toEqual([]);+ });++ test("fails when an approving reviewer is an approver in several sequences", () => {+ const steps = stepsOf([approver("u1"), approver("u1", { sequence: 2 }), approver("u2", { sequence: 2 })]);++ const failures = check(steps, [review("r1", "u1", "approved", at(1))]);++ expect(failures).toContainEqual(+ expect.objectContaining({+ message: expect.stringContaining("sequences 1, 2, so which step the approval counts toward is ambiguous")+ })+ );+ });++ test("fails a request that is still pending with every step satisfied, unless the caller allows it", () => {+ const steps = stepsOf([approver("u1")]);+ const reviews = [review("r1", "u1", "approved", at(1))];++ expect(check(steps, reviews, true).map((failure) => failure.message)).toEqual([+ "Every step already has enough approvals, yet the request is still pending"+ ]);+ expect(check(steps, reviews, false)).toEqual([]);+ });++ test("fails when an approval the global system would count is one the legacy system ignores", () => {+ const steps = stepsOf([approver("u1"), approver("u2", { sequence: 2 })]);++ const failures = check(steps, [review("r1", "ux", "approved", at(1))]);++ expect(failures.map((failure) => failure.message)).toEqual([+ "The legacy approval system is waiting on sequence 1, the global approval system would wait on sequence 2",+ "Sequence 1 counts 0 approval(s) on the legacy approval system and 1 on the global approval system"+ ]);+ });++ test("counts a user named directly and through a group once", () => {+ const steps = stepsOf(+ [approver("u1", { approvalsRequired: 2 }), groupApprover("g1", { approvalsRequired: 2 })],+ [+ { groupId: "g1", userId: "u1" },+ { groupId: "g1", userId: "u2" }+ ]+ );++ expect(check(steps, [review("r1", "u1", "approved", at(1))])).toEqual([]);+ });+});++describe("checkReviewStatuses", () => {+ test("accepts approved and rejected reviews", () => {+ const failures: TMigrationFailure[] = [];++ checkReviewStatuses(+ "req-1",+ [review("a", "u1", "approved", at(1)), review("b", "u2", "rejected", at(2))],+ failures+ );++ expect(failures).toEqual([]);+ });++ test("fails any other review status", () => {+ const failures: TMigrationFailure[] = [];++ checkReviewStatuses("req-1", [review("a", "u1", "pending", at(1))], failures);++ expect(failures).toEqual([+ { code: "unknown-review-status", requestId: "req-1", message: "Review by u1 has status 'pending'" }+ ]);+ });+});++describe("checkRequestsAlreadyMigrated", () => {+ test("passes when nothing is on the global system yet", () => {+ const failures: TMigrationFailure[] = [];++ checkRequestsAlreadyMigrated([], failures);++ expect(failures).toEqual([]);+ });++ test("names every request that already exists on the global system", () => {+ const failures: TMigrationFailure[] = [];++ checkRequestsAlreadyMigrated(["a", "b"], failures);++ expect(codes(failures)).toEqual(["request-already-migrated"]);+ expect(failures[0].message).toContain("Requests a, b already exist");+ });+});++describe("row builders", () => {+ const policy = { id: "pol-1", name: "Prod", enforcementLevel: "hard", createdAt: at(0), updatedAt: at(5) };++ test("buildGlobalPolicyRow fills the shared columns and takes the type-specific ones from the caller", () => {+ expect(+ buildGlobalPolicyRow({+ policy,+ projectId: "P",+ orgId: "O",+ type: "secret-change",+ conditions: { version: 1, conditions: [] },+ constraints: { version: 1, constraints: { allowedSelfApprovals: true } },+ bypassForMachineIdentities: true+ })+ ).toEqual({+ id: "pol-1",+ projectId: "P",+ organizationId: "O",+ type: "secret-change",+ name: "Prod",+ isActive: true,+ maxRequestTtl: null,+ conditions: { version: 1, conditions: [] },+ constraints: { version: 1, constraints: { allowedSelfApprovals: true } },+ enforcementLevel: "hard",+ bypassForMachineIdentities: true,+ scopeType: null,+ scopeId: null,+ createdAt: at(0),+ updatedAt: at(5)+ });+ });++ test("buildPolicyEnvironmentRows repeats the policy path for every environment", () => {+ expect(buildPolicyEnvironmentRows("pol-1", [env("dev"), env("prod")], "/apps/**")).toEqual([+ { policyId: "pol-1", envId: "dev", secretPath: "/apps/**" },+ { policyId: "pol-1", envId: "prod", secretPath: "/apps/**" }+ ]);+ });++ test("buildPolicyStepRows numbers steps and links approvers to their step", () => {+ const steps = stepsOf(+ [+ approver("u1"),+ groupApprover("g1"),+ approver("u2", { sequence: 5, approvalsRequired: 2 }),+ approver("u3", { sequence: 5, approvalsRequired: 2 })+ ],+ [{ groupId: "g1", userId: "u9" }]+ );++ const { policySteps, policyStepApprovers } = buildPolicyStepRows("pol-1", steps);++ expect(policySteps).toEqual([+ { id: expect.any(String), policyId: "pol-1", stepNumber: 1, requiredApprovals: 1 },+ { id: expect.any(String), policyId: "pol-1", stepNumber: 2, requiredApprovals: 2 }+ ]);+ expect(policyStepApprovers).toEqual([+ { policyStepId: policySteps[0].id, userId: "u1", groupId: null },+ { policyStepId: policySteps[0].id, userId: null, groupId: "g1" },+ { policyStepId: policySteps[1].id, userId: "u2", groupId: null },+ { policyStepId: policySteps[1].id, userId: "u3", groupId: null }+ ]);+ });++ test("buildBypasserRows dedupes and splits users from groups", () => {+ expect(+ buildBypasserRows("pol-1", [+ { bypasserUserId: "u1" },+ { bypasserUserId: "u1" },+ { bypasserGroupId: "g1" },+ { bypasserUserId: null, bypasserGroupId: null }+ ])+ ).toEqual([+ { policyId: "pol-1", userId: "u1", groupId: null },+ { policyId: "pol-1", userId: null, groupId: "g1" }+ ]);+ });++ describe("buildRequestStepRows", () => {+ const threeSteps = () =>+ stepsOf(+ [approver("u1"), groupApprover("g1", { sequence: 2 }), approver("u3", { sequence: 3 })],+ [{ groupId: "g1", userId: "u2" }]+ );++ test("completes every step of an approved request, chaining the timestamps", () => {+ const steps = threeSteps();+ const reviews = [review("r1", "u1", "approved", at(1))];++ const { requestSteps } = buildRequestStepRows({+ requestId: "req-1",+ steps,+ replay: replayReviews(steps, reviews),+ isApprovedLike: true,+ requestCreatedAt: at(0),+ fallbackCompletedAt: at(9)+ });++ expect(requestSteps.map(({ status, startedAt, completedAt }) => [status, startedAt, completedAt])).toEqual([+ ["completed", at(0), at(1)],+ ["completed", at(1), at(9)],+ ["completed", at(9), at(9)]+ ]);+ });++ test("marks the replayed step in progress and the rest pending on a live request", () => {+ const steps = threeSteps();+ const reviews = [review("r1", "u1", "approved", at(1))];++ const { requestSteps, eligibleApprovers } = buildRequestStepRows({+ requestId: "req-1",+ steps,+ replay: replayReviews(steps, reviews),+ isApprovedLike: false,+ requestCreatedAt: at(0),+ fallbackCompletedAt: at(9)+ });++ expect(requestSteps).toEqual([+ expect.objectContaining({+ requestId: "req-1",+ stepNumber: 1,+ status: "completed",+ startedAt: at(0),+ completedAt: at(1)+ }),+ expect.objectContaining({ stepNumber: 2, status: "in-progress", startedAt: at(1), completedAt: null }),+ expect.objectContaining({ stepNumber: 3, status: "pending", startedAt: null, completedAt: null })+ ]);+ expect(requestSteps[0]).toMatchObject({ name: null, requiredApprovals: 1, notifyApprovers: false });+ expect(eligibleApprovers).toEqual([+ { stepId: requestSteps[0].id, userId: "u1", groupId: null },+ { stepId: requestSteps[1].id, userId: null, groupId: "g1" },+ { stepId: requestSteps[2].id, userId: "u3", groupId: null }+ ]);+ });++ test("keeps a single step in progress when the request is not approved-like, even once satisfied", () => {+ const steps = stepsOf([approver("u1")]);+ const reviews = [review("r1", "u1", "approved", at(1), "looks good")];++ const { requestSteps, approvals } = buildRequestStepRows({+ requestId: "req-1",+ steps,+ replay: replayReviews(steps, reviews),+ isApprovedLike: false,+ requestCreatedAt: at(0),+ fallbackCompletedAt: at(9)+ });++ expect(requestSteps).toEqual([+ expect.objectContaining({ status: "in-progress", startedAt: at(0), completedAt: null })+ ]);+ expect(approvals).toEqual([+ {+ stepId: requestSteps[0].id,+ approverUserId: "u1",+ decision: "approved",+ comment: "looks good",+ createdAt: at(1)+ }+ ]);+ });++ test("maps approvals to the step they were attributed to and defaults the comment to null", () => {+ const steps = threeSteps();+ const reviews = [review("a", "u1", "approved", at(1)), review("b", "u2", "rejected", at(2))];++ const { requestSteps, approvals } = buildRequestStepRows({+ requestId: "req-1",+ steps,+ replay: replayReviews(steps, reviews),+ isApprovedLike: false,+ requestCreatedAt: at(0),+ fallbackCompletedAt: at(9)+ });++ expect(approvals).toEqual([+ { stepId: requestSteps[0].id, approverUserId: "u1", decision: "approved", comment: null, createdAt: at(1) },+ { stepId: requestSteps[1].id, approverUserId: "u2", decision: "rejected", comment: null, createdAt: at(2) }+ ]);+ });++ test("writes nothing for a policy without steps", () => {+ expect(+ buildRequestStepRows({+ requestId: "req-1",+ steps: [],+ replay: replayReviews([], [review("a", "u1", "approved", at(1))]),+ isApprovedLike: false,+ requestCreatedAt: at(0),+ fallbackCompletedAt: at(9)+ })+ ).toEqual({ requestSteps: [], eligibleApprovers: [], approvals: [] });+ });+ });+});++describe("runLegacyPolicyMigration", () => {+ const fakeKnex = (batches: string[][]) => {+ const pending = [...batches];+ const builder: Record<string, unknown> = {};+ ["whereNull", "whereNotExists", "modify", "orderBy", "limit"].forEach((method) => {+ builder[method] = () => builder;+ });+ builder.pluck = () => Promise.resolve(pending.shift() ?? []);+ return Object.assign(() => builder, {+ raw: (sql: string) => sql,+ transaction: (run: (tx: unknown) => Promise<TPolicyOutcome>) => run({})+ }) as unknown as Knex;+ };++ test("counts every outcome, keeps going past a locked or failing policy, and reports the totals", async () => {+ const outcomes: Record<string, () => Promise<TPolicyOutcome>> = {+ p1: () => Promise.resolve("migrated"),+ p2: () => Promise.reject(Object.assign(new Error("lock"), { code: "55P03" })),+ p3: () => Promise.reject(new Error("boom")),+ p4: () => Promise.resolve("failedChecks"),+ p5: () => Promise.resolve("skipped")+ };+ const migratePolicy = vi.fn((_tx: Knex, policyId: string) => outcomes[policyId]());++ await runLegacyPolicyMigration(+ fakeKnex([+ ["p1", "p2", "p3"],+ ["p4", "p5"]+ ]),+ {+ tables: legacyTables,+ logPrefix: "pfx",+ migratePolicy+ }+ );++ expect(migratePolicy.mock.calls.map(([, policyId]) => policyId)).toEqual(["p1", "p2", "p3", "p4", "p5"]);+ expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining("[policyId=p2] reasons=[locked:"));+ expect(logger.error).toHaveBeenCalledWith(+ expect.objectContaining({ message: "boom" }),+ expect.stringContaining("[policyId=p3] reasons=[unexpected error]")+ );+ expect(logger.info).toHaveBeenCalledWith(+ "pfx: migrated 1 policies, 1 failed checks, 1 were locked, 1 failed with an error, 1 were already migrated or deleted"+ );+ });++ test("does nothing when no legacy policy is left to migrate", async () => {+ const migratePolicy = vi.fn(() => Promise.resolve("migrated" as TPolicyOutcome));++ await runLegacyPolicyMigration(fakeKnex([]), { tables: legacyTables, logPrefix: "pfx", migratePolicy });++ expect(migratePolicy).not.toHaveBeenCalled();+ expect(logger.info).toHaveBeenCalledWith(expect.stringContaining("migrated 0 policies"));+ });+});diff --git a/backend/src/db/migrations/utils/approval-migration.ts b/backend/src/db/migrations/utils/approval-migration.tsnew file mode 100644index 0000000000..3a12365c5f--- /dev/null+++ b/backend/src/db/migrations/utils/approval-migration.ts@@ -0,0 +1,584 @@+/* eslint-disable no-await-in-loop -- policies migrate one after another on purpose */+import crypto from "node:crypto";++import { Knex } from "knex";++import { TableName } from "@app/db/schemas";+import { chunkArray } from "@app/lib/fn";+import { initLogger, logger } from "@app/lib/logger";++// Shared by every migration that moves a legacy approval system onto the global approval tables. Once a+// migration using it has shipped, change it only additively: a behaviour change here rewrites what an+// already-applied migration did.++export const INSERT_CHUNK_SIZE = 500;+export const POLICY_STATEMENT_TIMEOUT_MS = 60_000;+export const POLICY_LOCK_TIMEOUT_MS = 10_000;+const POLICY_BATCH_SIZE = 100;+const PG_LOCK_NOT_AVAILABLE = "55P03";++export enum ApprovalStepStatus {+ Pending = "pending",+ InProgress = "in-progress",+ Completed = "completed"+}++export enum ReviewDecision {+ Approved = "approved",+ Rejected = "rejected"+}++export type TLegacyApprovalTables = {+ legacyPolicies: TableName;+ legacyApprovers: TableName;+ legacyBypassers: TableName;+ legacyEnvironments: TableName;+ legacyRequests: TableName;+ legacyReviewers: TableName;+};++export type TMigrationFailure = { code: string; message: string; requestId?: string };++export type TPolicyEnvironment = { envId: string; envSlug: string; projectId: string; orgId: string };++export type TLegacyApprover = {+ approverUserId?: string | null;+ approverGroupId?: string | null;+ sequence?: number | null;+ approvalsRequired?: number | null;+};++export type TLegacyBypasser = { bypasserUserId?: string | null; bypasserGroupId?: string | null };++export type TLegacyReview = {+ id: string;+ requestId: string;+ reviewerUserId: string;+ status: string;+ createdAt: Date;+ comment?: string | null;+};++export type TGroupMember = { groupId: string; userId: string };++export type TApprovalStep = {+ sequence: number;+ requiredApprovals: number;+ userIds: string[];+ groupIds: string[];+ groupMemberIds: Set<string>;+ eligibleUserIds: Set<string>;+};++export type TGlobalPolicyRows = {+ policy: Record<string, unknown>;+ secretEnvironments: Record<string, unknown>[];+ policySteps: Record<string, unknown>[];+ policyStepApprovers: Record<string, unknown>[];+ bypassers: Record<string, unknown>[];+};++export type TGlobalRequestRows = {+ requests: Record<string, unknown>[];+ requestSteps: Record<string, unknown>[];+ eligibleApprovers: Record<string, unknown>[];+ approvals: Record<string, unknown>[];+};++export type TPolicyOutcome = "migrated" | "failedChecks" | "skipped";++const isId = (id: string | null | undefined): id is string => Boolean(id);++export const formatFailure = ({ code, message, requestId }: TMigrationFailure) =>+ `${code}${requestId ? ` [requestId=${requestId}]` : ""}: ${message}`;++export const logFailedChecks = ({+ logPrefix,+ policyId,+ projectId,+ failures+}: {+ logPrefix: string;+ policyId: string;+ projectId?: string;+ failures: TMigrationFailure[];+}) => {+ logger.warn(+ { policyId, projectId, failures },+ `${logPrefix}: policy not migrated, it stays on the legacy approval system [policyId=${policyId}] [projectId=${projectId ?? "unknown"}] [failedChecks=${failures.length}] reasons=[${failures.map(formatFailure).join("; ")}]`+ );+};++export const readPolicyEnvironments = async (tx: Knex, tables: TLegacyApprovalTables, policyId: string) =>+ (await tx(tables.legacyEnvironments)+ .join(TableName.Environment, `${TableName.Environment}.id`, `${tables.legacyEnvironments}.envId`)+ .join(TableName.Project, `${TableName.Project}.id`, `${TableName.Environment}.projectId`)+ .where(`${tables.legacyEnvironments}.policyId`, policyId)+ .select(+ tx.ref("envId").withSchema(tables.legacyEnvironments),+ tx.ref("slug").withSchema(TableName.Environment).as("envSlug"),+ tx.ref("projectId").withSchema(TableName.Environment),+ tx.ref("orgId").withSchema(TableName.Project)+ )) as TPolicyEnvironment[];++export const readLegacyApprovers = async (tx: Knex, tables: TLegacyApprovalTables, policyId: string) =>+ (await tx(tables.legacyApprovers)+ .where({ policyId })+ .orderBy([{ column: "createdAt" }, { column: "id" }])+ .select("*")) as TLegacyApprover[];++export const readLegacyBypassers = async (tx: Knex, tables: TLegacyApprovalTables, policyId: string) =>+ (await tx(tables.legacyBypassers)+ .where({ policyId })+ .select("bypasserUserId", "bypasserGroupId")) as TLegacyBypasser[];++export const readLegacyReviews = async (+ tx: Knex,+ tables: TLegacyApprovalTables,+ policyId: string,+ { withComment = false }: { withComment?: boolean } = {}+) => {+ const columns = ["id", "requestId", "reviewerUserId", "status", "createdAt", ...(withComment ? ["comment"] : [])];+ return (await tx(tables.legacyReviewers)+ .join(tables.legacyRequests, `${tables.legacyRequests}.id`, `${tables.legacyReviewers}.requestId`)+ .where(`${tables.legacyRequests}.policyId`, policyId)+ .select(...columns.map((column) => tx.ref(column).withSchema(tables.legacyReviewers)))) as TLegacyReview[];+};++export const readApproverGroupMembers = async (tx: Knex, approvers: TLegacyApprover[]) => {+ const groupIds = [...new Set(approvers.map((approver) => approver.approverGroupId).filter(isId))];+ if (!groupIds.length) return [] as TGroupMember[];+ return (await tx(TableName.UserGroupMembership)+ .whereIn("groupId", groupIds)+ .select("groupId", "userId")) as TGroupMember[];+};++export const readRequestIdsAlreadyOnGlobal = async (tx: Knex, tables: TLegacyApprovalTables, policyId: string) =>+ (await tx(TableName.ApprovalRequests)+ .whereIn("id", tx(tables.legacyRequests).where({ policyId }).select("id"))+ .pluck("id")) as string[];++export const checkPolicyEnvironments = (environments: TPolicyEnvironment[], failures: TMigrationFailure[]) => {+ if (!environments.length) {+ failures.push({ code: "no-environments", message: "The policy is not attached to any environment" });+ }++ // this is impossible to happen, but it is a good sanity check+ const projectIds = [...new Set(environments.map((environment) => environment.projectId))];+ if (projectIds.length > 1) {+ failures.push({+ code: "environments-span-projects",+ message: `The policy's environments belong to several projects (${projectIds.join(", ")})`+ });+ }+ return { projectId: environments[0]?.projectId, orgId: environments[0]?.orgId };+};++// A legacy system without sequences normalizes every approver to { sequence: 1, approvalsRequired:+// policy.approvals } before calling, so the two sequence checks cannot fire there.+export const buildApprovalSteps = (+ {+ policy,+ approvers,+ groupMembers+ }: { policy: { approvals: number }; approvers: TLegacyApprover[]; groupMembers: TGroupMember[] },+ failures: TMigrationFailure[]+): TApprovalStep[] => {+ if (!approvers.length) {+ failures.push({+ code: "no-approvers",+ message: "The policy has no approvers, and the global approval system refuses requests on a policy without steps"+ });+ }++ const membersByGroup = new Map<string, string[]>();+ groupMembers.forEach(({ groupId, userId }) => {+ membersByGroup.set(groupId, [...(membersByGroup.get(groupId) ?? []), userId]);+ });++ const approversBySequence = new Map<number, TLegacyApprover[]>();+ approvers.forEach((approver) => {+ const sequence = approver.sequence || 1;+ approversBySequence.set(sequence, [...(approversBySequence.get(sequence) ?? []), approver]);+ });++ return [...approversBySequence.keys()]+ .sort((a, b) => a - b)+ .map((sequence, index) => {+ const rows = approversBySequence.get(sequence) as TLegacyApprover[];+ const requiredValues = [...new Set(rows.map((row) => row.approvalsRequired ?? null))];+ if (requiredValues.length > 1) {+ failures.push({+ code: "step-approvals-disagree",+ message: `Approvers in sequence ${sequence} require different numbers of approvals (${requiredValues.map((value) => value ?? "unset").join(", ")})`+ });+ }++ const { approvalsRequired } = rows[0];+ if (index === 0 && !approvalsRequired && policy.approvals > 1) {+ failures.push({+ code: "ambiguous-required-approvals",+ message: `Sequence ${sequence} has no approvalsRequired, so the legacy approval system requires 1 approval there, while the policy is configured with ${policy.approvals} approvals`+ });+ }+ const requiredApprovals = approvalsRequired || 1;++ const userIds = [...new Set(rows.map((row) => row.approverUserId).filter(isId))];+ const groupIds = [...new Set(rows.map((row) => row.approverGroupId).filter(isId))];+ const groupMemberIds = new Set(groupIds.flatMap((groupId) => membersByGroup.get(groupId) ?? []));+ const eligibleUserIds = new Set([...userIds, ...groupMemberIds]);++ if (!groupIds.length && requiredApprovals > userIds.length) {+ failures.push({+ code: "required-approvals-exceed-approvers",+ message: `Sequence ${sequence} requires ${requiredApprovals} approvals but has only ${userIds.length} user approver${userIds.length === 1 ? "" : "s"}`+ });+ }++ if (!eligibleUserIds.size) {+ failures.push({+ code: "step-without-eligible-approvers",+ message: `Sequence ${sequence} has no user approvers and its groups have no members, so no one can approve it`+ });+ }++ return { sequence, requiredApprovals, userIds, groupIds, groupMemberIds, eligibleUserIds };+ });+};++export const checkRequestsAlreadyMigrated = (requestIds: string[], failures: TMigrationFailure[]) => {+ if (requestIds.length) {+ failures.push({+ code: "request-already-migrated",+ message: `Requests ${requestIds.join(", ")} already exist on the global approval system`+ });+ }+};++export const checkReviewStatuses = (requestId: string, reviews: TLegacyReview[], failures: TMigrationFailure[]) => {+ reviews+ .filter((review) => review.status !== ReviewDecision.Approved && review.status !== ReviewDecision.Rejected)+ .forEach((review) => {+ failures.push({+ code: "unknown-review-status",+ requestId,+ message: `Review by ${review.reviewerUserId} has status '${review.status}'`+ });+ });+};++// I need to check it more carefully+export const replayReviews = (steps: TApprovalStep[], reviews: TLegacyReview[]) => {+ const approvedCounts = steps.map(() => 0);+ const completedAt: (Date | null)[] = steps.map(() => null);+ const attributed: { review: TLegacyReview; stepIndex: number }[] = [];++ const currentIndex = () => {+ const index = steps.findIndex((step, i) => approvedCounts[i] < step.requiredApprovals);+ return index === -1 ? steps.length - 1 : index;+ };++ if (!steps.length) return { approvedCounts, completedAt, attributed, currentIndex: 0 };++ [...reviews]+ .sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime() || a.id.localeCompare(b.id))+ .forEach((review) => {+ let stepIndex = currentIndex();+ if (review.status === ReviewDecision.Approved) {+ if (!steps[stepIndex].eligibleUserIds.has(review.reviewerUserId)) {+ const eligibleIndex = steps.findIndex((step) => step.eligibleUserIds.has(review.reviewerUserId));+ if (eligibleIndex !== -1) stepIndex = eligibleIndex;+ }+ approvedCounts[stepIndex] += 1;+ if (approvedCounts[stepIndex] === steps[stepIndex].requiredApprovals) {+ completedAt[stepIndex] = review.createdAt;+ }+ }+ attributed.push({ review, stepIndex });+ });++ return { approvedCounts, completedAt, attributed, currentIndex: currentIndex() };+};++export type TReviewReplay = ReturnType<typeof replayReviews>;++// The legacy systems list each approver once per policy, so a user named directly and through a group+// counts a single time.+const legacyPendingState = (steps: TApprovalStep[], reviews: TLegacyReview[]) => {+ const approvedBy = new Set(+ reviews.filter((review) => review.status === ReviewDecision.Approved).map((review) => review.reviewerUserId)+ );+ const counts = steps.map((step) => [...step.eligibleUserIds].filter((userId) => approvedBy.has(userId)).length);+ return { counts, presentIndex: steps.findIndex((step, i) => counts[i] < step.requiredApprovals) };+};++export const checkPendingApprovalState = (+ requestId: string,+ steps: TApprovalStep[],+ reviews: TLegacyReview[],+ replay: TReviewReplay,+ failures: TMigrationFailure[],+ { failWhenAllStepsSatisfied }: { failWhenAllStepsSatisfied: boolean }+) => {+ const fail = (message: string) => failures.push({ code: "pending-request-state-mismatch", requestId, message });++ if (!steps.length) {+ fail("skipped: the policy has no approval steps to compare against");+ return;+ }++ [...new Set(reviews.filter((r) => r.status === ReviewDecision.Approved).map((r) => r.reviewerUserId))].forEach(+ (userId) => {+ const stepsOfUser = steps.filter((step) => step.eligibleUserIds.has(userId)).map((step) => step.sequence);+ if (stepsOfUser.length > 1) {+ fail(+ `Reviewer ${userId} approved and is an approver in sequences ${stepsOfUser.join(", ")}, so which step the approval counts toward is ambiguous`+ );+ }+ }+ );++ const legacy = legacyPendingState(steps, reviews);+ if (legacy.presentIndex === -1) {+ if (failWhenAllStepsSatisfied) {+ fail("Every step already has enough approvals, yet the request is still pending");+ return;+ }+ } else if (legacy.presentIndex !== replay.currentIndex) {+ fail(+ `The legacy approval system is waiting on sequence ${steps[legacy.presentIndex].sequence}, the global approval system would wait on sequence ${steps[replay.currentIndex].sequence}`+ );+ }+ steps.forEach((step, i) => {+ if (legacy.counts[i] !== replay.approvedCounts[i]) {+ fail(+ `Sequence ${step.sequence} counts ${legacy.counts[i]} approval(s) on the legacy approval system and ${replay.approvedCounts[i]} on the global approval system`+ );+ }+ });+};++export const buildGlobalPolicyRow = ({+ policy,+ projectId,+ orgId,+ type,+ conditions,+ constraints,+ bypassForMachineIdentities+}: {+ policy: { id: string; name: string; enforcementLevel: string; createdAt: Date; updatedAt: Date };+ projectId?: string;+ orgId?: string;+ type: string;+ conditions: unknown;+ constraints: unknown;+ bypassForMachineIdentities: boolean;+}): Record<string, unknown> => ({+ id: policy.id,+ projectId,+ organizationId: orgId,+ type,+ name: policy.name,+ isActive: true,+ maxRequestTtl: null,+ conditions,+ constraints,+ enforcementLevel: policy.enforcementLevel,+ bypassForMachineIdentities,+ scopeType: null,+ scopeId: null,+ createdAt: policy.createdAt,+ updatedAt: policy.updatedAt+});++export const buildPolicyEnvironmentRows = (policyId: string, environments: TPolicyEnvironment[], secretPath: string) =>+ environments.map(({ envId }) => ({ policyId, envId, secretPath }));++export const buildPolicyStepRows = (policyId: string, steps: TApprovalStep[]) => {+ const policySteps: Record<string, unknown>[] = [];+ const policyStepApprovers: Record<string, unknown>[] = [];+ steps.forEach((step, index) => {+ const stepId = crypto.randomUUID();+ policySteps.push({ id: stepId, policyId, stepNumber: index + 1, requiredApprovals: step.requiredApprovals });+ policyStepApprovers.push(+ ...step.userIds.map((userId) => ({ policyStepId: stepId, userId, groupId: null })),+ ...step.groupIds.map((groupId) => ({ policyStepId: stepId, userId: null, groupId }))+ );+ });+ return { policySteps, policyStepApprovers };+};++export const buildBypasserRows = (policyId: string, bypassers: TLegacyBypasser[]) => [+ ...[...new Set(bypassers.map((b) => b.bypasserUserId).filter(isId))].map((userId) => ({+ policyId,+ userId,+ groupId: null+ })),+ ...[...new Set(bypassers.map((b) => b.bypasserGroupId).filter(isId))].map((groupId) => ({+ policyId,+ userId: null,+ groupId+ }))+];++// A single step with isApprovedLike false stays in progress, which is what the global secret change+// bridge writes for every request.+export const buildRequestStepRows = ({+ requestId,+ steps,+ replay,+ isApprovedLike,+ requestCreatedAt,+ fallbackCompletedAt+}: {+ requestId: string;+ steps: TApprovalStep[];+ replay: TReviewReplay;+ isApprovedLike: boolean;+ requestCreatedAt: Date;+ fallbackCompletedAt: Date;+}) => {+ const stepIds = steps.map(() => crypto.randomUUID());+ const requestSteps: Record<string, unknown>[] = [];+ const eligibleApprovers: Record<string, unknown>[] = [];+ let previousCompletedAt: Date = requestCreatedAt;++ steps.forEach((step, index) => {+ let status = ApprovalStepStatus.Pending;+ let startedAt: Date | null = null;+ let completedAt: Date | null = null;+ if (isApprovedLike || index < replay.currentIndex) {+ status = ApprovalStepStatus.Completed;+ startedAt = previousCompletedAt;+ completedAt = replay.completedAt[index] ?? fallbackCompletedAt;+ previousCompletedAt = completedAt;+ } else if (index === replay.currentIndex) {+ status = ApprovalStepStatus.InProgress;+ startedAt = previousCompletedAt;+ }+ requestSteps.push({+ id: stepIds[index],+ requestId,+ stepNumber: index + 1,+ name: null,+ status,+ requiredApprovals: step.requiredApprovals,+ notifyApprovers: false,+ startedAt,+ completedAt+ });+ eligibleApprovers.push(+ ...step.userIds.map((userId) => ({ stepId: stepIds[index], userId, groupId: null })),+ ...step.groupIds.map((groupId) => ({ stepId: stepIds[index], userId: null, groupId }))+ );+ });++ const approvals: Record<string, unknown>[] = stepIds.length+ ? replay.attributed.map(({ review, stepIndex }) => ({+ stepId: stepIds[stepIndex],+ approverUserId: review.reviewerUserId,+ decision: review.status,+ comment: review.comment ?? null,+ createdAt: review.createdAt+ }))+ : [];++ return { requestSteps, eligibleApprovers, approvals };+};++export const insertChunked = async (tx: Knex, tableName: TableName, rows: Record<string, unknown>[]) => {+ for (const chunk of chunkArray(rows, INSERT_CHUNK_SIZE)) {+ await tx(tableName).insert(chunk as never);+ }+};++export const insertGlobalPolicyRows = async (tx: Knex, rows: TGlobalPolicyRows) => {+ await tx(TableName.ApprovalPolicies).insert(rows.policy as never);+ await insertChunked(tx, TableName.ApprovalPolicySecretEnvironment, rows.secretEnvironments);+ await insertChunked(tx, TableName.ApprovalPolicySteps, rows.policySteps);+ await insertChunked(tx, TableName.ApprovalPolicyStepApprovers, rows.policyStepApprovers);+ await insertChunked(tx, TableName.ApprovalPolicyBypassers, rows.bypassers);+};++export const insertGlobalRequestRows = async (tx: Knex, rows: TGlobalRequestRows) => {+ await insertChunked(tx, TableName.ApprovalRequests, rows.requests);+ await insertChunked(tx, TableName.ApprovalRequestSteps, rows.requestSteps);+ await insertChunked(tx, TableName.ApprovalRequestStepEligibleApprovers, rows.eligibleApprovers);+ await insertChunked(tx, TableName.ApprovalRequestApprovals, rows.approvals);+};++export const lockLegacyPolicy = async <T extends { id: string; deletedAt?: Date | null }>(+ tx: Knex,+ tables: TLegacyApprovalTables,+ policyId: string+): Promise<T | null> => {+ await tx.raw(`SET LOCAL statement_timeout = ${POLICY_STATEMENT_TIMEOUT_MS}`);+ await tx.raw(`SET LOCAL lock_timeout = ${POLICY_LOCK_TIMEOUT_MS}`);++ const policy = (await tx(tables.legacyPolicies).where({ id: policyId }).forUpdate().first()) as T | undefined;+ if (!policy || policy.deletedAt) return null;+ if (await tx(TableName.ApprovalPolicies).where({ id: policyId }).first("id")) return null;+ return policy;+};++export const runLegacyPolicyMigration = async (+ knex: Knex,+ {+ tables,+ logPrefix,+ migratePolicy+ }: {+ tables: TLegacyApprovalTables;+ logPrefix: string;+ migratePolicy: (tx: Knex, policyId: string) => Promise<TPolicyOutcome>;+ }+) => {+ initLogger();++ const counts = { migrated: 0, failedChecks: 0, skipped: 0, locked: 0, failedWithError: 0 };+ let lastId: string | undefined;++ for (;;) {+ const cursor = lastId;+ const policyIds = (await knex(tables.legacyPolicies)+ .whereNull("deletedAt")+ .whereNotExists((qb) => {+ void qb+ .select(knex.raw("1"))+ .from(TableName.ApprovalPolicies)+ .whereRaw(`"${TableName.ApprovalPolicies}"."id" = "${tables.legacyPolicies}"."id"`);+ })+ .modify((qb) => {+ if (cursor) void qb.where("id", ">", cursor);+ })+ .orderBy("id", "asc")+ .limit(POLICY_BATCH_SIZE)+ .pluck("id")) as string[];+ if (!policyIds.length) break;+ lastId = policyIds[policyIds.length - 1];++ for (const policyId of policyIds) {+ try {+ counts[await knex.transaction((tx) => migratePolicy(tx, policyId))] += 1;+ } catch (error) {+ if ((error as { code?: string })?.code === PG_LOCK_NOT_AVAILABLE) {+ counts.locked += 1;+ logger.warn(+ `${logPrefix}: policy not migrated, it stays on the legacy approval system [policyId=${policyId}] reasons=[locked: its rows were held by another transaction for over ${POLICY_LOCK_TIMEOUT_MS}ms, the next run retries it]`+ );+ } else {+ counts.failedWithError += 1;+ logger.error(+ error,+ `${logPrefix}: policy not migrated, it stays on the legacy approval system [policyId=${policyId}] reasons=[unexpected error]`+ );+ }+ }+ }+ }++ logger.info(+ `${logPrefix}: migrated ${counts.migrated} policies, ${counts.failedChecks} failed checks, ${counts.locked} were locked, ${counts.failedWithError} failed with an error, ${counts.skipped} were already migrated or deleted`+ );+};