@prenta/cli 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +51 -52
- package/CHANGELOG.md +19 -0
- package/dist/backup/workflow-template.d.ts +1 -1
- package/dist/backup/workflow-template.js +1 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +18 -6
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/seed.d.ts.map +1 -1
- package/dist/commands/seed.js.map +1 -1
- package/dist/deployment/diagnostics.d.ts +26 -1
- package/dist/deployment/diagnostics.d.ts.map +1 -1
- package/dist/deployment/diagnostics.js +78 -2
- package/dist/deployment/diagnostics.js.map +1 -1
- package/dist/deployment/health-check.d.ts +15 -0
- package/dist/deployment/health-check.d.ts.map +1 -0
- package/dist/deployment/health-check.js +85 -0
- package/dist/deployment/health-check.js.map +1 -0
- package/dist/vercel/env-matrix.d.ts +11 -1
- package/dist/vercel/env-matrix.d.ts.map +1 -1
- package/dist/vercel/env-matrix.js +6 -1
- package/dist/vercel/env-matrix.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/rate-limit-backend.test.ts +259 -0
- package/src/backup/workflow-template.ts +1 -1
- package/src/commands/doctor.ts +24 -5
- package/src/commands/seed.ts +1 -2
- package/src/deployment/diagnostics.ts +95 -2
- package/src/deployment/health-check.ts +114 -0
- package/src/vercel/env-matrix.ts +22 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #1261: a production deploy without a rate-limit store refuses sign-in,
|
|
3
|
+
* setup and saves. `prenta doctor` / `deploy:check` must say so before the
|
|
4
|
+
* deploy, and `prenta verify` must not pass a deploy whose /health says
|
|
5
|
+
* "unhealthy".
|
|
6
|
+
*/
|
|
7
|
+
import { Command } from 'commander'
|
|
8
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
9
|
+
|
|
10
|
+
import { registerVerifyCommand } from '../commands/doctor.js'
|
|
11
|
+
import {
|
|
12
|
+
createDiagnosticReport,
|
|
13
|
+
detectRateLimitBackend,
|
|
14
|
+
VERCEL_MATRIX_OPTIONAL,
|
|
15
|
+
VERCEL_MATRIX_REQUIRED,
|
|
16
|
+
VERCEL_MATRIX_REQUIRED_ONE_OF,
|
|
17
|
+
} from '../deployment/diagnostics.js'
|
|
18
|
+
import { checkHealthResponse } from '../deployment/health-check.js'
|
|
19
|
+
import type { VercelEnvVar } from '../vercel/client.js'
|
|
20
|
+
import { buildEnvMatrix } from '../vercel/env-matrix.js'
|
|
21
|
+
|
|
22
|
+
const BASE_ENV = {
|
|
23
|
+
DATABASE_URL: 'postgresql://example',
|
|
24
|
+
DIRECT_DATABASE_URL: 'postgresql://example',
|
|
25
|
+
CMS_SECRET: 'x'.repeat(40),
|
|
26
|
+
CMS_ENCRYPTION_KEY: 'ab'.repeat(32),
|
|
27
|
+
NEXT_PUBLIC_SITE_URL: 'https://example.com',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function rateLimitCheck(env: Record<string, string | undefined>, mode?: 'doctor' | 'deploy') {
|
|
31
|
+
const report = createDiagnosticReport({
|
|
32
|
+
schemaModels: new Set(),
|
|
33
|
+
env,
|
|
34
|
+
packageManager: 'pnpm',
|
|
35
|
+
schemaPath: 'prisma/schema.prisma',
|
|
36
|
+
mode,
|
|
37
|
+
})
|
|
38
|
+
return { report, check: report.checks.find((c) => c.id === 'rate-limit-backend') }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('rate-limit store check (doctor / deploy:check)', () => {
|
|
42
|
+
it('fails deploy:check when neither name pair is set, and names both', () => {
|
|
43
|
+
const { report, check } = rateLimitCheck(BASE_ENV, 'deploy')
|
|
44
|
+
expect(check?.status).toBe('fail')
|
|
45
|
+
expect(report.status).toBe('fail')
|
|
46
|
+
expect(check?.message).toContain('UPSTASH_REDIS_REST_URL')
|
|
47
|
+
expect(check?.message).toContain('KV_REST_API_URL')
|
|
48
|
+
expect(check?.message).toContain('503')
|
|
49
|
+
expect(check?.fix).toContain('Vercel Marketplace')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('fails doctor when the environment is production', () => {
|
|
53
|
+
expect(rateLimitCheck({ ...BASE_ENV, NODE_ENV: 'production' }, 'doctor').check?.status).toBe(
|
|
54
|
+
'fail',
|
|
55
|
+
)
|
|
56
|
+
expect(rateLimitCheck({ ...BASE_ENV, VERCEL_ENV: 'production' }, 'doctor').check?.status).toBe(
|
|
57
|
+
'fail',
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('only warns in local doctor runs, where an in-memory limiter is used', () => {
|
|
62
|
+
expect(rateLimitCheck(BASE_ENV, 'doctor').check?.status).toBe('warn')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('passes with the Vercel Marketplace KV_REST_API_* names', () => {
|
|
66
|
+
const { check } = rateLimitCheck(
|
|
67
|
+
{ ...BASE_ENV, KV_REST_API_URL: 'https://kv.example.com', KV_REST_API_TOKEN: 't' },
|
|
68
|
+
'deploy',
|
|
69
|
+
)
|
|
70
|
+
expect(check?.status).toBe('pass')
|
|
71
|
+
expect(check?.message).toContain('KV_REST_API_URL')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('passes with the Upstash console names', () => {
|
|
75
|
+
const { check } = rateLimitCheck(
|
|
76
|
+
{
|
|
77
|
+
...BASE_ENV,
|
|
78
|
+
UPSTASH_REDIS_REST_URL: 'https://u.example.com',
|
|
79
|
+
UPSTASH_REDIS_REST_TOKEN: 't',
|
|
80
|
+
},
|
|
81
|
+
'deploy',
|
|
82
|
+
)
|
|
83
|
+
expect(check?.status).toBe('pass')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('names the missing half of a half-set pair', () => {
|
|
87
|
+
const { check } = rateLimitCheck({ ...BASE_ENV, KV_REST_API_URL: 'https://kv' }, 'deploy')
|
|
88
|
+
expect(check?.status).toBe('fail')
|
|
89
|
+
expect(check?.message).toContain('UPSTASH_REDIS_REST_TOKEN (or KV_REST_API_TOKEN)')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('mixes names per variable, like the core does', () => {
|
|
93
|
+
expect(
|
|
94
|
+
detectRateLimitBackend({ UPSTASH_REDIS_REST_URL: 'https://u', KV_REST_API_TOKEN: 't' }).state,
|
|
95
|
+
).toBe('ok')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('deploy:check --vercel matrix', () => {
|
|
100
|
+
const env = (key: string, target: VercelEnvVar['target']): VercelEnvVar => ({ key, target })
|
|
101
|
+
const required = VERCEL_MATRIX_REQUIRED.map((key) =>
|
|
102
|
+
env(key, ['production', 'preview', 'development']),
|
|
103
|
+
)
|
|
104
|
+
const matrix = (vars: VercelEnvVar[]) =>
|
|
105
|
+
buildEnvMatrix(
|
|
106
|
+
[...required, ...vars],
|
|
107
|
+
VERCEL_MATRIX_REQUIRED,
|
|
108
|
+
VERCEL_MATRIX_OPTIONAL,
|
|
109
|
+
VERCEL_MATRIX_REQUIRED_ONE_OF,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
it('blocks a project with no rate-limit store on production or preview', () => {
|
|
113
|
+
expect(matrix([]).missingCritical).toEqual([
|
|
114
|
+
{
|
|
115
|
+
key: expect.stringContaining('KV_REST_API_URL'),
|
|
116
|
+
targets: ['production', 'preview'],
|
|
117
|
+
},
|
|
118
|
+
])
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('accepts the Marketplace KV names', () => {
|
|
122
|
+
const all: VercelEnvVar['target'] = ['production', 'preview']
|
|
123
|
+
expect(
|
|
124
|
+
matrix([env('KV_REST_API_URL', all), env('KV_REST_API_TOKEN', all)]).missingCritical,
|
|
125
|
+
).toEqual([])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('does not accept a pair split across targets', () => {
|
|
129
|
+
const result = matrix([
|
|
130
|
+
env('KV_REST_API_URL', ['production', 'preview']),
|
|
131
|
+
env('KV_REST_API_TOKEN', ['production']),
|
|
132
|
+
])
|
|
133
|
+
expect(result.missingCritical).toEqual([
|
|
134
|
+
{ key: expect.stringContaining('rate-limit'), targets: ['preview'] },
|
|
135
|
+
])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('shows both name pairs in the matrix', () => {
|
|
139
|
+
const keys = matrix([]).rows.map((r) => r.key)
|
|
140
|
+
expect(keys).toEqual(
|
|
141
|
+
expect.arrayContaining([
|
|
142
|
+
'UPSTASH_REDIS_REST_URL',
|
|
143
|
+
'UPSTASH_REDIS_REST_TOKEN',
|
|
144
|
+
'KV_REST_API_URL',
|
|
145
|
+
'KV_REST_API_TOKEN',
|
|
146
|
+
]),
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
function healthResponse(data: unknown, status: number): Response {
|
|
152
|
+
return new Response(JSON.stringify({ data }), {
|
|
153
|
+
status,
|
|
154
|
+
headers: { 'Content-Type': 'application/json' },
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const URL_ = 'https://example.com/api/cms/health'
|
|
159
|
+
|
|
160
|
+
describe('checkHealthResponse', () => {
|
|
161
|
+
it('fails an unhealthy deploy that answers 200 (cores before #1261)', async () => {
|
|
162
|
+
const result = await checkHealthResponse(
|
|
163
|
+
healthResponse({ status: 'unhealthy', rateLimitBackendReady: false }, 200),
|
|
164
|
+
URL_,
|
|
165
|
+
)
|
|
166
|
+
expect(result.verdict).toBe('fail')
|
|
167
|
+
expect(result.lines.join('\n')).toContain('KV_REST_API_URL')
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('fails an unhealthy deploy that answers 503 and says why', async () => {
|
|
171
|
+
const result = await checkHealthResponse(
|
|
172
|
+
healthResponse(
|
|
173
|
+
{
|
|
174
|
+
status: 'unhealthy',
|
|
175
|
+
rateLimitBackendReady: true,
|
|
176
|
+
trustedIpSource: false,
|
|
177
|
+
trustProxyHint: 'Set PRENTA_TRUST_PROXY=1 behind a trusted reverse proxy.',
|
|
178
|
+
env: { checks: [{ name: 'CMS_SECRET', status: 'error', message: 'too short' }] },
|
|
179
|
+
},
|
|
180
|
+
503,
|
|
181
|
+
),
|
|
182
|
+
URL_,
|
|
183
|
+
)
|
|
184
|
+
expect(result.verdict).toBe('fail')
|
|
185
|
+
expect(result.lines).toEqual(
|
|
186
|
+
expect.arrayContaining([
|
|
187
|
+
expect.stringContaining('PRENTA_TRUST_PROXY'),
|
|
188
|
+
'CMS_SECRET: too short',
|
|
189
|
+
]),
|
|
190
|
+
)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('passes a healthy deploy', async () => {
|
|
194
|
+
expect(
|
|
195
|
+
(await checkHealthResponse(healthResponse({ status: 'healthy' }, 200), URL_)).verdict,
|
|
196
|
+
).toBe('pass')
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
it('warns on degraded', async () => {
|
|
200
|
+
const result = await checkHealthResponse(
|
|
201
|
+
healthResponse({ status: 'degraded', databaseConnected: false }, 200),
|
|
202
|
+
URL_,
|
|
203
|
+
)
|
|
204
|
+
expect(result.verdict).toBe('warn')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('warns, not passes, when Deployment Protection hides the report', async () => {
|
|
208
|
+
expect((await checkHealthResponse(new Response('', { status: 401 }), URL_)).verdict).toBe(
|
|
209
|
+
'warn',
|
|
210
|
+
)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('fails a 5xx without a report', async () => {
|
|
214
|
+
expect((await checkHealthResponse(new Response('oops', { status: 500 }), URL_)).verdict).toBe(
|
|
215
|
+
'fail',
|
|
216
|
+
)
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
describe('prenta verify --url', () => {
|
|
221
|
+
afterEach(() => {
|
|
222
|
+
vi.unstubAllGlobals()
|
|
223
|
+
vi.restoreAllMocks()
|
|
224
|
+
process.exitCode = undefined
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
async function verify(health: () => Response): Promise<{ exitCode: unknown; out: string }> {
|
|
228
|
+
vi.stubGlobal(
|
|
229
|
+
'fetch',
|
|
230
|
+
vi.fn(async (url: string) =>
|
|
231
|
+
url.endsWith('/api/cms/health') ? health() : new Response('', { status: 302 }),
|
|
232
|
+
),
|
|
233
|
+
)
|
|
234
|
+
const lines: string[] = []
|
|
235
|
+
vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => {
|
|
236
|
+
lines.push(args.join(' '))
|
|
237
|
+
})
|
|
238
|
+
const program = new Command()
|
|
239
|
+
program.exitOverride()
|
|
240
|
+
registerVerifyCommand(program)
|
|
241
|
+
await program.parseAsync(['node', 'prenta', 'verify', '--full', '--url', 'https://example.com'])
|
|
242
|
+
return { exitCode: process.exitCode, out: lines.join('\n') }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
it('fails when /health reports unhealthy, even with HTTP 200', async () => {
|
|
246
|
+
const { exitCode, out } = await verify(() =>
|
|
247
|
+
healthResponse({ status: 'unhealthy', rateLimitBackendReady: false }, 200),
|
|
248
|
+
)
|
|
249
|
+
expect(exitCode).toBe(1)
|
|
250
|
+
expect(out).toContain('FAIL')
|
|
251
|
+
expect(out).toContain('unhealthy')
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
it('passes when /health reports healthy', async () => {
|
|
255
|
+
const { exitCode, out } = await verify(() => healthResponse({ status: 'healthy' }, 200))
|
|
256
|
+
expect(exitCode).toBeUndefined()
|
|
257
|
+
expect(out).toContain('PASS Health route')
|
|
258
|
+
})
|
|
259
|
+
})
|
|
@@ -27,7 +27,7 @@ jobs:
|
|
|
27
27
|
- uses: actions/checkout@v6
|
|
28
28
|
- uses: pnpm/action-setup@v6
|
|
29
29
|
- uses: actions/setup-node@v6
|
|
30
|
-
with: { node-version:
|
|
30
|
+
with: { node-version: 24, cache: pnpm }
|
|
31
31
|
- name: Install PostgreSQL 17 client
|
|
32
32
|
run: |
|
|
33
33
|
sudo install -d /usr/share/postgresql-common/pgdg
|
package/src/commands/doctor.ts
CHANGED
|
@@ -10,8 +10,10 @@ import {
|
|
|
10
10
|
type ProjectFileSignals,
|
|
11
11
|
VERCEL_MATRIX_OPTIONAL,
|
|
12
12
|
VERCEL_MATRIX_REQUIRED,
|
|
13
|
+
VERCEL_MATRIX_REQUIRED_ONE_OF,
|
|
13
14
|
type DiagnosticReport,
|
|
14
15
|
} from '../deployment/diagnostics.js'
|
|
16
|
+
import { checkHealthResponse } from '../deployment/health-check.js'
|
|
15
17
|
import {
|
|
16
18
|
createVercelClient,
|
|
17
19
|
readVercelLink,
|
|
@@ -231,7 +233,12 @@ async function runVercelMatrix(opts: {
|
|
|
231
233
|
const client = createVercelClient({ token, teamId: link.orgId ?? opts.org })
|
|
232
234
|
try {
|
|
233
235
|
const envVars = await client.listProjectEnv(link.projectId)
|
|
234
|
-
const matrix = buildEnvMatrix(
|
|
236
|
+
const matrix = buildEnvMatrix(
|
|
237
|
+
envVars,
|
|
238
|
+
VERCEL_MATRIX_REQUIRED,
|
|
239
|
+
VERCEL_MATRIX_OPTIONAL,
|
|
240
|
+
VERCEL_MATRIX_REQUIRED_ONE_OF,
|
|
241
|
+
)
|
|
235
242
|
if (!opts.json) {
|
|
236
243
|
printEnvMatrix(matrix)
|
|
237
244
|
if (matrix.missingCritical.length > 0) {
|
|
@@ -336,10 +343,22 @@ export function registerVerifyCommand(program: Command): void {
|
|
|
336
343
|
const adminUrl = `${origin}${manifest.routes.adminDefault}`
|
|
337
344
|
let failed = false
|
|
338
345
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
346
|
+
try {
|
|
347
|
+
const health = await checkHealthResponse(
|
|
348
|
+
await fetch(healthUrl, { method: 'GET' }),
|
|
349
|
+
healthUrl,
|
|
350
|
+
)
|
|
351
|
+
if (health.verdict === 'fail') failed = true
|
|
352
|
+
const [summary, ...details] = health.lines
|
|
353
|
+
console.log(`${health.verdict.toUpperCase()} ${summary}`)
|
|
354
|
+
for (const line of details) console.log(chalk.dim(` ${line}`))
|
|
355
|
+
} catch (error) {
|
|
356
|
+
failed = true
|
|
357
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
358
|
+
console.log(`FAIL Health route could not be reached at ${healthUrl}: ${message}`)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
for (const [label, url] of [['Admin', adminUrl]] as const) {
|
|
343
362
|
try {
|
|
344
363
|
const response = await fetch(url, { method: 'GET' })
|
|
345
364
|
if (response.ok || response.status === 302 || response.status === 401) {
|
package/src/commands/seed.ts
CHANGED
|
@@ -462,8 +462,7 @@ interface SeedSanitizers {
|
|
|
462
462
|
) => Record<string, unknown>
|
|
463
463
|
sanitizeLayoutTree: (layout: unknown) => unknown
|
|
464
464
|
getPrentaConfig: () =>
|
|
465
|
-
|
|
466
|
-
| undefined
|
|
465
|
+
{ collections?: Record<string, { fields?: Record<string, unknown> } | undefined> } | undefined
|
|
467
466
|
}
|
|
468
467
|
|
|
469
468
|
/**
|
|
@@ -51,11 +51,29 @@ export const VERCEL_MATRIX_REQUIRED = [
|
|
|
51
51
|
/** Vars the matrix surfaces as advisory (feature-gated, not deploy-blocking). */
|
|
52
52
|
export const VERCEL_MATRIX_OPTIONAL = [
|
|
53
53
|
'BLOB_READ_WRITE_TOKEN',
|
|
54
|
+
// The rate-limit store: either pair. Shown here so both names appear; the
|
|
55
|
+
// either-or requirement is `VERCEL_MATRIX_REQUIRED_ONE_OF`.
|
|
54
56
|
'UPSTASH_REDIS_REST_URL',
|
|
55
57
|
'UPSTASH_REDIS_REST_TOKEN',
|
|
58
|
+
'KV_REST_API_URL',
|
|
59
|
+
'KV_REST_API_TOKEN',
|
|
56
60
|
'RESEND_API_KEY',
|
|
57
61
|
] as const
|
|
58
62
|
|
|
63
|
+
/** The two names for the shared rate-limit store; either pair is enough. */
|
|
64
|
+
export const RATE_LIMIT_ENV_PAIRS = [
|
|
65
|
+
['UPSTASH_REDIS_REST_URL', 'UPSTASH_REDIS_REST_TOKEN'],
|
|
66
|
+
['KV_REST_API_URL', 'KV_REST_API_TOKEN'],
|
|
67
|
+
] as const
|
|
68
|
+
|
|
69
|
+
/** Deploy-blocking requirements any one of several variable sets satisfies. */
|
|
70
|
+
export const VERCEL_MATRIX_REQUIRED_ONE_OF = [
|
|
71
|
+
{
|
|
72
|
+
label: 'UPSTASH_REDIS_REST_URL/TOKEN or KV_REST_API_URL/TOKEN (rate-limit store)',
|
|
73
|
+
alternatives: RATE_LIMIT_ENV_PAIRS,
|
|
74
|
+
},
|
|
75
|
+
] as const
|
|
76
|
+
|
|
59
77
|
export interface DiagnosticInput {
|
|
60
78
|
schemaModels: Set<string>
|
|
61
79
|
schemaContent?: string
|
|
@@ -133,6 +151,79 @@ export function detectBlobLinkState(
|
|
|
133
151
|
return hasPartialSignals ? 'partial' : 'none'
|
|
134
152
|
}
|
|
135
153
|
|
|
154
|
+
export type RateLimitBackendState =
|
|
155
|
+
| { state: 'ok'; pair: readonly [string, string] }
|
|
156
|
+
| { state: 'partial'; missing: string[] }
|
|
157
|
+
| { state: 'none' }
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whether the shared rate-limit store is configured, under either the Upstash
|
|
161
|
+
* console names or the names Vercel Marketplace Upstash provisions. Mirrors
|
|
162
|
+
* `resolveUpstashCredentials` in `@prenta/core`, which takes `UPSTASH_*` first
|
|
163
|
+
* and falls back to `KV_REST_API_*` per variable.
|
|
164
|
+
*/
|
|
165
|
+
export function detectRateLimitBackend(
|
|
166
|
+
env: Record<string, string | undefined>,
|
|
167
|
+
): RateLimitBackendState {
|
|
168
|
+
const url = env.UPSTASH_REDIS_REST_URL || env.KV_REST_API_URL
|
|
169
|
+
const token = env.UPSTASH_REDIS_REST_TOKEN || env.KV_REST_API_TOKEN
|
|
170
|
+
if (url && token) {
|
|
171
|
+
const pair = RATE_LIMIT_ENV_PAIRS.find(([u, t]) => env[u] && env[t]) ?? RATE_LIMIT_ENV_PAIRS[0]
|
|
172
|
+
return { state: 'ok', pair }
|
|
173
|
+
}
|
|
174
|
+
if (!url && !token) return { state: 'none' }
|
|
175
|
+
return {
|
|
176
|
+
state: 'partial',
|
|
177
|
+
missing: url
|
|
178
|
+
? ['UPSTASH_REDIS_REST_TOKEN (or KV_REST_API_TOKEN)']
|
|
179
|
+
: ['UPSTASH_REDIS_REST_URL (or KV_REST_API_URL)'],
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isProductionTarget(input: DiagnosticInput): boolean {
|
|
184
|
+
return (
|
|
185
|
+
input.mode === 'deploy' ||
|
|
186
|
+
input.env.NODE_ENV === 'production' ||
|
|
187
|
+
input.env.VERCEL_ENV === 'production'
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* In production every fail-closed limiter (writes, sign-in, setup, admin reads)
|
|
193
|
+
* refuses all requests with a 503 when no shared store is configured, so a
|
|
194
|
+
* deploy without one cannot be used (#1261). Locally it is only a warning: dev
|
|
195
|
+
* falls back to an in-memory counter.
|
|
196
|
+
*/
|
|
197
|
+
function rateLimitBackendCheck(input: DiagnosticInput): DiagnosticCheck {
|
|
198
|
+
const backend = detectRateLimitBackend(input.env)
|
|
199
|
+
const label = 'Rate-limit store (Upstash Redis)'
|
|
200
|
+
const docs = 'https://prentacms.dev/docs/deployment'
|
|
201
|
+
const fix =
|
|
202
|
+
'Add Upstash from the Vercel Marketplace (Storage tab, sets KV_REST_API_URL and KV_REST_API_TOKEN), or set UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN from the Upstash console, then redeploy.'
|
|
203
|
+
if (backend.state === 'ok') {
|
|
204
|
+
return {
|
|
205
|
+
id: 'rate-limit-backend',
|
|
206
|
+
label,
|
|
207
|
+
status: 'pass',
|
|
208
|
+
message: `Shared rate-limit store configured (${backend.pair.join(' + ')}).`,
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const production = isProductionTarget(input)
|
|
212
|
+
const consequence =
|
|
213
|
+
'In production every write, sign-in, setup request and admin read is refused with HTTP 503 (rate_limit_backend_missing) until it is set.'
|
|
214
|
+
return {
|
|
215
|
+
id: 'rate-limit-backend',
|
|
216
|
+
label,
|
|
217
|
+
status: production ? 'fail' : 'warn',
|
|
218
|
+
message:
|
|
219
|
+
backend.state === 'partial'
|
|
220
|
+
? `Rate-limit store is half configured: missing ${backend.missing.join(', ')}. ${consequence}`
|
|
221
|
+
: `Neither UPSTASH_REDIS_REST_URL + UPSTASH_REDIS_REST_TOKEN nor KV_REST_API_URL + KV_REST_API_TOKEN is set. ${consequence}${production ? '' : ' Local development uses an in-memory limiter.'}`,
|
|
222
|
+
fix,
|
|
223
|
+
docs,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
136
227
|
export function detectPackageManager(lockfiles: Set<string>): string {
|
|
137
228
|
if (lockfiles.has('pnpm-lock.yaml')) return 'pnpm'
|
|
138
229
|
if (lockfiles.has('yarn.lock')) return 'yarn'
|
|
@@ -200,6 +291,8 @@ export function createDiagnosticReport(input: DiagnosticInput): DiagnosticReport
|
|
|
200
291
|
docs: 'https://prentacms.dev/docs/environment-variables',
|
|
201
292
|
})
|
|
202
293
|
|
|
294
|
+
checks.push(rateLimitBackendCheck(input))
|
|
295
|
+
|
|
203
296
|
const blobState = detectBlobLinkState(input.env, input.configContent)
|
|
204
297
|
checks.push({
|
|
205
298
|
id: 'blob-storage',
|
|
@@ -493,10 +586,10 @@ export function buildDeploymentManifest() {
|
|
|
493
586
|
'CMS_ADMIN_PASSWORD',
|
|
494
587
|
'CRON_SECRET',
|
|
495
588
|
'BLOB_READ_WRITE_TOKEN',
|
|
496
|
-
'UPSTASH_REDIS_REST_URL',
|
|
497
|
-
'UPSTASH_REDIS_REST_TOKEN',
|
|
498
589
|
'RESEND_API_KEY',
|
|
499
590
|
],
|
|
591
|
+
/** Required in production; either pair satisfies it. */
|
|
592
|
+
rateLimitEnv: RATE_LIMIT_ENV_PAIRS.map((pair) => [...pair]),
|
|
500
593
|
routes: {
|
|
501
594
|
apiHealth: '/api/cms/health',
|
|
502
595
|
apiBase: '/api/cms',
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a deployed `/api/cms/health` response for `prenta verify`.
|
|
3
|
+
*
|
|
4
|
+
* Answering is not the same as working: a deploy without a rate-limit store
|
|
5
|
+
* answers `/health` with `status: "unhealthy"` while refusing every sign-in
|
|
6
|
+
* and save (#1261), so the verdict comes from the body, not only the status.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type HealthVerdict = 'pass' | 'warn' | 'fail'
|
|
10
|
+
|
|
11
|
+
export interface HealthCheckResult {
|
|
12
|
+
verdict: HealthVerdict
|
|
13
|
+
/** One line per finding, the first being the summary. */
|
|
14
|
+
lines: string[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface HealthEnvCheck {
|
|
18
|
+
name?: string
|
|
19
|
+
status?: string
|
|
20
|
+
message?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface HealthBody {
|
|
24
|
+
status?: string
|
|
25
|
+
rateLimitBackendReady?: boolean
|
|
26
|
+
trustedIpSource?: boolean
|
|
27
|
+
trustProxyHint?: string
|
|
28
|
+
databaseConnected?: boolean
|
|
29
|
+
env?: { checks?: HealthEnvCheck[] }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readBody(json: unknown): HealthBody | null {
|
|
33
|
+
if (!json || typeof json !== 'object') return null
|
|
34
|
+
const data = (json as { data?: unknown }).data
|
|
35
|
+
if (!data || typeof data !== 'object') return null
|
|
36
|
+
return data as HealthBody
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Why the deploy reports itself unhealthy or degraded, as far as the body says. */
|
|
40
|
+
function reasons(body: HealthBody): string[] {
|
|
41
|
+
const out: string[] = []
|
|
42
|
+
if (body.rateLimitBackendReady === false) {
|
|
43
|
+
out.push(
|
|
44
|
+
'No rate-limit store: set UPSTASH_REDIS_REST_URL/TOKEN or add Upstash from the Vercel Marketplace (KV_REST_API_URL/TOKEN). Until then sign-in, setup and saves are refused with 503.',
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
if (body.trustedIpSource === false) {
|
|
48
|
+
out.push(body.trustProxyHint ?? 'No trusted client-IP source: set PRENTA_TRUST_PROXY=1.')
|
|
49
|
+
}
|
|
50
|
+
for (const check of body.env?.checks ?? []) {
|
|
51
|
+
if (check.status === 'error' || check.status === 'missing') {
|
|
52
|
+
out.push(`${check.name ?? 'env'}: ${check.message ?? check.status}`)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (body.databaseConnected === false) out.push('The database is not reachable.')
|
|
56
|
+
return out
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function checkHealthResponse(
|
|
60
|
+
response: Response,
|
|
61
|
+
url: string,
|
|
62
|
+
): Promise<HealthCheckResult> {
|
|
63
|
+
// Deployment Protection or an auth gate in front of the site: the route is
|
|
64
|
+
// there, but its report cannot be read.
|
|
65
|
+
if (response.status === 401 || response.status === 302) {
|
|
66
|
+
return {
|
|
67
|
+
verdict: 'warn',
|
|
68
|
+
lines: [
|
|
69
|
+
`Health route at ${url} answered HTTP ${response.status}, so its status could not be read (Deployment Protection?).`,
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let body: HealthBody | null = null
|
|
75
|
+
try {
|
|
76
|
+
body = readBody(await response.json())
|
|
77
|
+
} catch {
|
|
78
|
+
body = null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!body?.status) {
|
|
82
|
+
return response.ok
|
|
83
|
+
? {
|
|
84
|
+
verdict: 'warn',
|
|
85
|
+
lines: [
|
|
86
|
+
`Health route at ${url} answered HTTP ${response.status} without a health report.`,
|
|
87
|
+
],
|
|
88
|
+
}
|
|
89
|
+
: { verdict: 'fail', lines: [`Health route returned HTTP ${response.status} at ${url}.`] }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (body.status === 'unhealthy') {
|
|
93
|
+
return {
|
|
94
|
+
verdict: 'fail',
|
|
95
|
+
lines: [
|
|
96
|
+
`Health route at ${url} reports "unhealthy" (HTTP ${response.status}).`,
|
|
97
|
+
...reasons(body),
|
|
98
|
+
],
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
return {
|
|
103
|
+
verdict: 'fail',
|
|
104
|
+
lines: [`Health route returned HTTP ${response.status} at ${url} (status "${body.status}").`],
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (body.status === 'degraded') {
|
|
108
|
+
return {
|
|
109
|
+
verdict: 'warn',
|
|
110
|
+
lines: [`Health route at ${url} reports "degraded".`, ...reasons(body)],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { verdict: 'pass', lines: [`Health route at ${url} reports "${body.status}".`] }
|
|
114
|
+
}
|
package/src/vercel/env-matrix.ts
CHANGED
|
@@ -40,10 +40,22 @@ export interface EnvMatrix {
|
|
|
40
40
|
/** Production and preview are the deploy-blocking targets; development is local-only. */
|
|
41
41
|
const DEPLOY_TARGETS: VercelTarget[] = ['production', 'preview']
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* A requirement any one of several sets of variables satisfies, such as the
|
|
45
|
+
* rate-limit store, which reads either the Upstash console names or the names
|
|
46
|
+
* Vercel Marketplace Upstash provisions.
|
|
47
|
+
*/
|
|
48
|
+
export interface RequiredOneOf {
|
|
49
|
+
/** Shown in `missingCritical` when no alternative is complete on a target. */
|
|
50
|
+
label: string
|
|
51
|
+
alternatives: readonly (readonly string[])[]
|
|
52
|
+
}
|
|
53
|
+
|
|
43
54
|
export function buildEnvMatrix(
|
|
44
55
|
envVars: VercelEnvVar[],
|
|
45
56
|
requiredKeys: readonly string[],
|
|
46
57
|
optionalKeys: readonly string[] = [],
|
|
58
|
+
requiredOneOf: readonly RequiredOneOf[] = [],
|
|
47
59
|
): EnvMatrix {
|
|
48
60
|
const rows: EnvMatrixRow[] = []
|
|
49
61
|
const seen = new Set<string>()
|
|
@@ -63,6 +75,16 @@ export function buildEnvMatrix(
|
|
|
63
75
|
rows.push({ key, required: false, presence: presenceForKey(envVars, key) })
|
|
64
76
|
}
|
|
65
77
|
|
|
78
|
+
for (const requirement of requiredOneOf) {
|
|
79
|
+
const missing = DEPLOY_TARGETS.filter(
|
|
80
|
+
(target) =>
|
|
81
|
+
!requirement.alternatives.some((keys) =>
|
|
82
|
+
keys.every((key) => presenceForKey(envVars, key)[target]),
|
|
83
|
+
),
|
|
84
|
+
)
|
|
85
|
+
if (missing.length > 0) missingCritical.push({ key: requirement.label, targets: missing })
|
|
86
|
+
}
|
|
87
|
+
|
|
66
88
|
return { rows, missingCritical }
|
|
67
89
|
}
|
|
68
90
|
|