import { Form, useNavigation } from "@remix-run/react"; import { useEffect } from "react"; import { typedjson, useTypedActionData, useTypedFetcher, useTypedLoaderData, } from "remix-typedjson"; import { z } from "zod"; import { Button } from "~/components/primitives/Buttons"; import { Callout } from "~/components/primitives/Callout"; import { Header1, Header2 } from "~/components/primitives/Headers"; import { Paragraph } from "~/components/primitives/Paragraph"; import { TextArea } from "~/components/primitives/TextArea"; import { environmentEncryptionBackfill } from "~/services/environmentEncryption/backfillInstance.server"; import { dashboardAction, dashboardLoader } from "~/services/routeBuilders/dashboardBuilder"; import { adminWorker } from "~/v3/services/adminWorker.server"; const LEGACY_PAGE_ID = "legacy"; const STATUS_QUERY = "?status"; const operations = ["inspect", "enroll", "migrate", "verify"] as const; const operationLabels = { inspect: "Inspect", enroll: "Enroll missing", migrate: "Migrate", verify: "Verify", }; export const loader = dashboardLoader( { authorization: { requireSuper: true } }, async ({ request }) => { const statusOnly = new URL(request.url).searchParams.has("status"); const [job, verified, page] = await Promise.all([ environmentEncryptionBackfill.current(), environmentEncryptionBackfill.pageVerified(), statusOnly ? null : environmentEncryptionBackfill.page(), ]); return typedjson({ job, verified, page: page && { id: page.id, number: page.number, organizations: page.organizations, count: page.environments.length, first: page.environments.at(0) ?? null, last: page.environments.at(-1) ?? null, after: page.after, hasNext: page.nextCursor !== null, }, }); } ); const ActionSchema = z.discriminatedUnion("intent", [ z.object({ intent: z.literal("select"), pageId: z.string(), organizationIds: z.string().min(1).max(10_000), }), z.object({ intent: z.literal("next"), pageId: z.string().min(1) }), z.object({ intent: z.literal("stop"), pageId: z.string().min(1), jobId: z.string().min(1) }), z.object({ intent: z.enum(operations), pageId: z.string().min(1), backupConfirmed: z.string().optional(), }), ]); export const action = dashboardAction( { authorization: { requireSuper: true } }, async ({ request, user }) => { const parsed = ActionSchema.safeParse(Object.fromEntries(await request.formData())); if (!parsed.success) return typedjson({ error: "Invalid cohort or action" }, { status: 400 }); const data = parsed.data; try { switch (data.intent) { case "select": { await environmentEncryptionBackfill.selectPage( data.organizationIds.split(","), data.pageId ); return typedjson({ error: null }); } case "next": await environmentEncryptionBackfill.nextPage(data.pageId); return typedjson({ error: null }); case "stop": { const job = await environmentEncryptionBackfill.current(); if (job?.id !== data.jobId || job.pageId !== data.pageId) { throw new Error("The job changed. Reload and try again."); } await environmentEncryptionBackfill.stop(data.jobId); return typedjson({ error: null }); } } if ( (data.intent === "enroll" || data.intent === "migrate") && data.backupConfirmed !== "on" ) { return typedjson( { error: "Confirm the backup/restore procedure before making changes" }, { status: 400 } ); } const job = await environmentEncryptionBackfill.create(data.intent, data.pageId, user.id); try { await adminWorker.enqueue({ id: job.id, job: "admin.environmentEncryptionBackfillPage", payload: { jobId: job.id }, }); } catch { await environmentEncryptionBackfill.enqueueFailed(job.id); return typedjson({ error: "Could not enqueue job. Retry the operation." }, { status: 503 }); } return typedjson({ error: null }); } catch (error) { return typedjson( { error: error instanceof Error ? error.message : "Could not apply action" }, { status: 400 } ); } } ); export default function EnvironmentEncryptionAdmin() { const data = useTypedLoaderData(); return ( ); } function MigrationPage({ data }: { data: ReturnType> }) { const { page } = data; const actionData = useTypedActionData(); const navigation = useNavigation(); const { data: status, load, state } = useTypedFetcher(); const { job, verified } = status ?? data; const active = job?.status === "queued" || job?.status === "running"; const busy = navigation.state !== "idle"; const legacy = !page && job?.pageId === LEGACY_PAGE_ID; const pageJob = page && job?.pageId === page.id ? job : null; const pageVerified = verified && pageJob?.operation === "verify" && pageJob.id === data.job?.id && pageJob.status === "completed" && pageJob.progress.remaining === 0; useEffect(() => { if (!active || busy || state !== "idle") return; const timer = setTimeout(() => load(STATUS_QUERY), 3_000); return () => clearTimeout(timer); }, [active, busy, state, load]); return (
Environment encryption migration Temporary migration tool. Start with small internal orgs. Inspect, confirm backup/restore, enroll, migrate, then verify and observe. Migration overwrites ciphertext; the old keyring alone cannot undo it. Flags do not downgrade enrolled environments. Each page fixes up to 1,000 environments for all four operations, including archived and preview environments. One job runs globally, one environment at a time. Enrollment pauses 100 ms per environment; scans pause 100 ms between batches of 25 with no extra environment pause. Inspect and Verify decrypt without writing. Check cold serving-process access separately.