|
| 1 | +import { type LoaderFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { type GetDeploymentBuildEnvVarsResponseBody } from "@trigger.dev/core/v3"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { prisma } from "~/db.server"; |
| 5 | +import { env } from "~/env.server"; |
| 6 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 7 | +import { logger } from "~/services/logger.server"; |
| 8 | +import { decryptSecret, EncryptedSecretValueSchema } from "~/services/secrets/secretStore.server"; |
| 9 | +import { FINAL_DEPLOYMENT_STATUSES } from "~/v3/services/failDeployment.server"; |
| 10 | + |
| 11 | +const ParamsSchema = z.object({ |
| 12 | + deploymentId: z.string(), |
| 13 | +}); |
| 14 | + |
| 15 | +// Returns the decrypted build-time env vars stored on a fromBundle deployment. |
| 16 | +// Deliberately separate from the main GET deployment endpoint: this is secret |
| 17 | +// material, and a dedicated route keeps access explicit and auditable. The vars |
| 18 | +// are cleared when the deployment reaches a terminal status, so this only ever |
| 19 | +// serves the active build window. |
| 20 | +export async function loader({ request, params }: LoaderFunctionArgs) { |
| 21 | + const parsedParams = ParamsSchema.safeParse(params); |
| 22 | + |
| 23 | + if (!parsedParams.success) { |
| 24 | + return json({ error: "Invalid params" }, { status: 400 }); |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + // Next authenticate the request |
| 29 | + const authenticationResult = await authenticateApiRequest(request); |
| 30 | + |
| 31 | + if (!authenticationResult) { |
| 32 | + logger.info("Invalid or missing api key", { url: request.url }); |
| 33 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 34 | + } |
| 35 | + |
| 36 | + const authenticatedEnv = authenticationResult.environment; |
| 37 | + |
| 38 | + const { deploymentId } = parsedParams.data; |
| 39 | + |
| 40 | + const deployment = await prisma.workerDeployment.findFirst({ |
| 41 | + where: { |
| 42 | + friendlyId: deploymentId, |
| 43 | + environmentId: authenticatedEnv.id, |
| 44 | + }, |
| 45 | + select: { |
| 46 | + id: true, |
| 47 | + status: true, |
| 48 | + buildEnvVars: true, |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + if (!deployment) { |
| 53 | + return json({ error: "Deployment not found" }, { status: 404 }); |
| 54 | + } |
| 55 | + |
| 56 | + logger.info("Build env vars read", { |
| 57 | + deploymentId, |
| 58 | + environmentId: authenticatedEnv.id, |
| 59 | + projectId: authenticatedEnv.projectId, |
| 60 | + status: deployment.status, |
| 61 | + hasVars: deployment.buildEnvVars !== null, |
| 62 | + }); |
| 63 | + |
| 64 | + // Terminal deployments have their vars cleared; even if a clear is still in |
| 65 | + // flight, never serve secrets for a build that is no longer active. |
| 66 | + if (FINAL_DEPLOYMENT_STATUSES.includes(deployment.status)) { |
| 67 | + return json({ variables: {} } satisfies GetDeploymentBuildEnvVarsResponseBody, { |
| 68 | + status: 200, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (!deployment.buildEnvVars) { |
| 73 | + return json({ variables: {} } satisfies GetDeploymentBuildEnvVarsResponseBody, { |
| 74 | + status: 200, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + const envelope = EncryptedSecretValueSchema.safeParse(deployment.buildEnvVars); |
| 79 | + |
| 80 | + if (!envelope.success) { |
| 81 | + logger.error("Stored build env vars are not a valid encrypted envelope", { |
| 82 | + deploymentId, |
| 83 | + environmentId: authenticatedEnv.id, |
| 84 | + }); |
| 85 | + return json({ variables: {} } satisfies GetDeploymentBuildEnvVarsResponseBody, { |
| 86 | + status: 200, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + let variables: Record<string, string>; |
| 91 | + |
| 92 | + try { |
| 93 | + const decrypted = await decryptSecret(env.ENCRYPTION_KEY, envelope.data); |
| 94 | + variables = z.record(z.string()).parse(JSON.parse(decrypted)); |
| 95 | + } catch (error) { |
| 96 | + logger.error("Failed to decrypt stored build env vars", { |
| 97 | + deploymentId, |
| 98 | + environmentId: authenticatedEnv.id, |
| 99 | + error, |
| 100 | + }); |
| 101 | + return json({ variables: {} } satisfies GetDeploymentBuildEnvVarsResponseBody, { |
| 102 | + status: 200, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + return json({ variables } satisfies GetDeploymentBuildEnvVarsResponseBody, { status: 200 }); |
| 107 | + } catch (error) { |
| 108 | + if (error instanceof Response) throw error; |
| 109 | + logger.error("Failed to load deployment build env vars", { error }); |
| 110 | + return json({ error: "Internal Server Error" }, { status: 500 }); |
| 111 | + } |
| 112 | +} |
0 commit comments