couriercrow 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +19 -2
- package/bin/couriercrow.js +5 -0
- package/bin/crow.js +14 -0
- package/bundle/migrations/0001_initial.sql +463 -0
- package/bundle/version.json +3 -0
- package/bundle/web/_headers +6 -0
- package/bundle/web/assets/index-Bim8rHvG.css +2 -0
- package/bundle/web/assets/index-YpJn_3e5.js +13 -0
- package/bundle/web/index.html +14 -0
- package/bundle/worker/README.md +1 -0
- package/bundle/worker/worker.js +57841 -0
- package/bundle/wrangler.jsonc +64 -0
- package/package.json +61 -4
- package/src/app-credentials.ts +413 -0
- package/src/bin.ts +578 -0
- package/src/build-config.ts +264 -0
- package/src/build-id.ts +39 -0
- package/src/cloudflare/cloudflare-client.ts +361 -0
- package/src/cloudflare/cloudflare-live-access.ts +454 -0
- package/src/cloudflare/cloudflare-live-account.ts +95 -0
- package/src/cloudflare/cloudflare-live-api.ts +87 -0
- package/src/cloudflare/cloudflare-live-checks.ts +151 -0
- package/src/cloudflare/cloudflare-live-zone.ts +363 -0
- package/src/cloudflare/cloudflare-live.ts +270 -0
- package/src/cloudflare/cloudflare-state.ts +152 -0
- package/src/cloudflare/first-run.ts +322 -0
- package/src/credentials-show.ts +49 -0
- package/src/deploy.ts +807 -0
- package/src/doctor.ts +504 -0
- package/src/environment.ts +141 -0
- package/src/index.ts +4 -0
- package/src/init.ts +332 -0
- package/src/load-config.ts +47 -0
- package/src/mailbox-api.ts +345 -0
- package/src/permissions.ts +133 -0
- package/src/plan.ts +517 -0
- package/src/printable.ts +8 -0
- package/src/product.ts +63 -0
- package/src/purge.ts +93 -0
- package/src/render.ts +406 -0
- package/src/rotate-app-secret.ts +53 -0
- package/src/runtime.ts +33 -0
- package/src/set-secret.ts +63 -0
- package/src/terminal.ts +163 -0
- package/src/wrangler.ts +168 -0
package/src/deploy.ts
ADDED
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
import { desiredState, maxChangesPerRequest } from '@couriercrow/core'
|
|
2
|
+
import type { Change, DomainName, MailConfig } from '@couriercrow/core'
|
|
3
|
+
import { Effect, Option } from 'effect'
|
|
4
|
+
|
|
5
|
+
import { appNeedsTerminalMessage, applyAppCredentials, printCredentials } from './app-credentials'
|
|
6
|
+
import type { AppCredentials } from './app-credentials'
|
|
7
|
+
import { appVersionFor, checkBundle, readAppVersion, writeBuildConfig } from './build-config'
|
|
8
|
+
import {
|
|
9
|
+
Cloudflare,
|
|
10
|
+
CloudflareError,
|
|
11
|
+
coversExactly,
|
|
12
|
+
describeCoverage,
|
|
13
|
+
unsubscribeDomain,
|
|
14
|
+
unsubscribePolicyName
|
|
15
|
+
} from './cloudflare/cloudflare-client'
|
|
16
|
+
import type { Zone } from './cloudflare/cloudflare-client'
|
|
17
|
+
import { paidPlanMessage } from './cloudflare/cloudflare-live'
|
|
18
|
+
import {
|
|
19
|
+
DeployBlocked,
|
|
20
|
+
notAZoneMessage
|
|
21
|
+
} from './cloudflare/cloudflare-state'
|
|
22
|
+
import type { CloudflareChange } from './cloudflare/cloudflare-state'
|
|
23
|
+
import {
|
|
24
|
+
ciCredentialsMessage,
|
|
25
|
+
credentialsMismatchMessage,
|
|
26
|
+
credentialsMissingMessage,
|
|
27
|
+
DeployFailed,
|
|
28
|
+
ensureIgnored,
|
|
29
|
+
prepareApp,
|
|
30
|
+
setPeoplePolicy
|
|
31
|
+
} from './cloudflare/first-run'
|
|
32
|
+
import { accountChecks, failedLines, formatCheck, hasFailed } from './doctor'
|
|
33
|
+
import { MailboxApi, MailboxApiError } from './mailbox-api'
|
|
34
|
+
import {
|
|
35
|
+
buildPlan,
|
|
36
|
+
missingPermissionMessage,
|
|
37
|
+
purgingInConfigMessage
|
|
38
|
+
} from './plan'
|
|
39
|
+
import type { DeployPlan } from './plan'
|
|
40
|
+
import { accountPermissions } from './permissions'
|
|
41
|
+
import { printable } from './printable'
|
|
42
|
+
import { credentialsPath } from './product'
|
|
43
|
+
import { renderPlan } from './render'
|
|
44
|
+
import { Terminal } from './terminal'
|
|
45
|
+
import { Wrangler, WranglerError } from './wrangler'
|
|
46
|
+
|
|
47
|
+
// The API takes at most this many changes in one post, so a post stays within
|
|
48
|
+
// D1's queries per Worker invocation.
|
|
49
|
+
export const mailBatchSize = maxChangesPerRequest
|
|
50
|
+
|
|
51
|
+
export { ciCredentialsMessage }
|
|
52
|
+
|
|
53
|
+
export const needsYesMessage =
|
|
54
|
+
'Run deploy with --yes to apply changes without a prompt.'
|
|
55
|
+
|
|
56
|
+
export const nothingChangedMessage = 'Nothing was changed.'
|
|
57
|
+
|
|
58
|
+
export const skippedChecksMessage = 'Skipped the checks. Run bunx couriercrow doctor to see them.'
|
|
59
|
+
|
|
60
|
+
export function seriesKeyMissingMessage(app: string): string {
|
|
61
|
+
return `Series for ${app} will not send until you run bunx couriercrow set-secret RESEND_API_KEY.`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const unsubscribeApplicationRemovedMessage =
|
|
65
|
+
'The unsubscribe page application was removed in Cloudflare while deploying. Run deploy again.'
|
|
66
|
+
|
|
67
|
+
export const unsubscribeBypassRemovedMessage =
|
|
68
|
+
'The unsubscribe page application covered more than /u/. Its open-to-everyone rule was removed. Run deploy again to recreate it.'
|
|
69
|
+
|
|
70
|
+
export const credentialsRecreatedMessage =
|
|
71
|
+
'Deploy credentials were recreated, so the plan was computed again:'
|
|
72
|
+
|
|
73
|
+
export interface DeployOptions {
|
|
74
|
+
// The Cloudflare account, for links into the dashboard.
|
|
75
|
+
accountId: string
|
|
76
|
+
// How long to wait for a freshly deployed app to answer.
|
|
77
|
+
appWait?: { everyMilliseconds: number; upToMilliseconds: number }
|
|
78
|
+
// Where the app's files (the bundle) are.
|
|
79
|
+
bundleDirectory: string
|
|
80
|
+
// The folder of the config file. .courier-crow/ is created in it.
|
|
81
|
+
directory: string
|
|
82
|
+
// The variables of the run. COURIER_CROW_DEPLOY_CLIENT_ID and _SECRET win
|
|
83
|
+
// over the credentials file. None when not given.
|
|
84
|
+
environment?: Readonly<Record<string, string | undefined>>
|
|
85
|
+
// Leaves out the checks of crow doctor that run first.
|
|
86
|
+
skipChecks?: boolean
|
|
87
|
+
takeOver: boolean
|
|
88
|
+
// Skips the question before anything is applied.
|
|
89
|
+
yes: boolean
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface DeployResult {
|
|
93
|
+
// False when nothing was applied: no changes, or the person said no.
|
|
94
|
+
applied: boolean
|
|
95
|
+
plan: DeployPlan
|
|
96
|
+
// Things that did not work but do not stop the deploy.
|
|
97
|
+
warnings: readonly string[]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type DeployError =
|
|
101
|
+
| CloudflareError
|
|
102
|
+
| DeployBlocked
|
|
103
|
+
| DeployFailed
|
|
104
|
+
| MailboxApiError
|
|
105
|
+
| WranglerError
|
|
106
|
+
|
|
107
|
+
const defaultAppWait = { everyMilliseconds: 5000, upToMilliseconds: 120_000 }
|
|
108
|
+
|
|
109
|
+
function isEmpty(plan: DeployPlan): boolean {
|
|
110
|
+
return (
|
|
111
|
+
plan.app === 'unchanged' &&
|
|
112
|
+
plan.appsWithoutCredentials.length === 0 &&
|
|
113
|
+
(plan.appsMovedHostname ?? []).length === 0 &&
|
|
114
|
+
(plan.unusedAppTokens ?? []).length === 0 &&
|
|
115
|
+
plan.cloudflare.length === 0 &&
|
|
116
|
+
plan.mail.length === 0 &&
|
|
117
|
+
plan.movesFrom === undefined
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Whether apps get or lose service tokens in this plan.
|
|
122
|
+
function changesAppTokens(plan: DeployPlan): boolean {
|
|
123
|
+
return (
|
|
124
|
+
plan.appsWithoutCredentials.length > 0 ||
|
|
125
|
+
(plan.appsMovedHostname ?? []).length > 0 ||
|
|
126
|
+
(plan.unusedAppTokens ?? []).length > 0 ||
|
|
127
|
+
plan.movesFrom !== undefined ||
|
|
128
|
+
plan.mail.some(
|
|
129
|
+
(change) => change._tag === 'AddApp' || change._tag === 'RemoveApp'
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function batches(changes: readonly Change[]): Change[][] {
|
|
135
|
+
const result: Change[][] = []
|
|
136
|
+
|
|
137
|
+
for (let start = 0; start < changes.length; start += mailBatchSize) {
|
|
138
|
+
result.push(changes.slice(start, start + mailBatchSize))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return result
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// A mailbox in the config that is being purged can neither be restored nor
|
|
145
|
+
// finished by a deploy. Nothing is changed until the person decides.
|
|
146
|
+
function blockPurgingInConfig(plan: DeployPlan, config: MailConfig) {
|
|
147
|
+
const wanted = new Set<string>(
|
|
148
|
+
desiredState(config).mailboxes.map((mailbox) => mailbox.address)
|
|
149
|
+
)
|
|
150
|
+
const blocked = (plan.purgingMailboxes ?? []).find((address) =>
|
|
151
|
+
wanted.has(address)
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
return blocked === undefined
|
|
155
|
+
? Effect.void
|
|
156
|
+
: Effect.fail(new DeployBlocked({ message: purgingInConfigMessage(blocked) }))
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Changes that take mail access away.
|
|
160
|
+
function isRevocation(change: Change): boolean {
|
|
161
|
+
return change._tag === 'RemovePerson' || change._tag === 'RevokeAccess'
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function removeMxFailed(domain: DomainName): string {
|
|
165
|
+
return `The Cloudflare API token cannot remove the old MX records of ${domain}. Add the permission Zone > DNS > Edit to the token and run deploy again.`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function changedWhileConfirming(domain: DomainName): string {
|
|
169
|
+
return `${domain} changed while you were confirming: new MX records or routing rules appeared. Run deploy --take-over again to see and confirm them.`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// `reason` is the text of Cloudflare's error, when the cause is not a missing
|
|
173
|
+
// permission.
|
|
174
|
+
function sessionsNotEnded(email: string, reason: string | undefined): string {
|
|
175
|
+
const needs =
|
|
176
|
+
reason === undefined
|
|
177
|
+
? ` (the API token needs ${accountPermissions.accessOrganization})`
|
|
178
|
+
: `: ${printable(reason)}`
|
|
179
|
+
|
|
180
|
+
return `Could not end ${printable(email)}'s open sessions${needs}. They lose access to mail now, but stay signed in to the app until their session expires.`
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function sendingNotSetUp(domain: DomainName, accountId: string): string {
|
|
184
|
+
return `Sending is not set up for ${domain} yet. Receiving works. Finish the step at https://dash.cloudflare.com/${accountId}/${domain}/email/sending, then run deploy again.`
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Waits until the deployed app answers with the build id of the bundle, at
|
|
188
|
+
// most until the deadline, however long single answers take.
|
|
189
|
+
function waitForApp(
|
|
190
|
+
hostname: string,
|
|
191
|
+
appVersion: string,
|
|
192
|
+
wait: { everyMilliseconds: number; upToMilliseconds: number }
|
|
193
|
+
) {
|
|
194
|
+
return Effect.gen(function* () {
|
|
195
|
+
const api = yield* MailboxApi
|
|
196
|
+
|
|
197
|
+
const poll = Effect.gen(function* () {
|
|
198
|
+
for (;;) {
|
|
199
|
+
// A failure is the same as no answer yet: a new hostname and a new
|
|
200
|
+
// Access token take a moment to work.
|
|
201
|
+
const version = yield* api
|
|
202
|
+
.version()
|
|
203
|
+
.pipe(Effect.catch(() => Effect.succeed(Option.none<string>())))
|
|
204
|
+
|
|
205
|
+
if (Option.isSome(version) && version.value === appVersion) {
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
yield* Effect.sleep(`${wait.everyMilliseconds} millis`)
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
yield* poll.pipe(
|
|
214
|
+
Effect.timeoutOrElse({
|
|
215
|
+
duration: `${wait.upToMilliseconds} millis`,
|
|
216
|
+
orElse: () =>
|
|
217
|
+
Effect.fail(
|
|
218
|
+
new MailboxApiError({
|
|
219
|
+
message: `The Courier Crow app was deployed but is not answering at ${hostname} yet. DNS for a new hostname can take a few minutes. Run deploy again.`
|
|
220
|
+
})
|
|
221
|
+
)
|
|
222
|
+
})
|
|
223
|
+
)
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Builds the plan, shows it, and applies it. Every step is safe to run again
|
|
228
|
+
// after a failure: steps that are done are found and skipped.
|
|
229
|
+
export function deploy(
|
|
230
|
+
config: MailConfig,
|
|
231
|
+
options: DeployOptions
|
|
232
|
+
): Effect.Effect<
|
|
233
|
+
DeployResult,
|
|
234
|
+
DeployError,
|
|
235
|
+
Cloudflare | MailboxApi | Terminal | Wrangler
|
|
236
|
+
> {
|
|
237
|
+
return Effect.gen(function* () {
|
|
238
|
+
const cloudflare = yield* Cloudflare
|
|
239
|
+
const api = yield* MailboxApi
|
|
240
|
+
const terminal = yield* Terminal
|
|
241
|
+
const wrangler = yield* Wrangler
|
|
242
|
+
const hostname = config.app.hostname
|
|
243
|
+
const appWait = options.appWait ?? defaultAppWait
|
|
244
|
+
const warnings: string[] = []
|
|
245
|
+
// A run without a terminal (CI) creates no secret it would have to
|
|
246
|
+
// print or keep.
|
|
247
|
+
const interactive = terminal.stdinIsTerminal && terminal.stdoutIsTerminal
|
|
248
|
+
|
|
249
|
+
// A warning is printed when it happens, so a later failure cannot lose
|
|
250
|
+
// it. The result carries them too.
|
|
251
|
+
const warn = (text: string) =>
|
|
252
|
+
Effect.gen(function* () {
|
|
253
|
+
warnings.push(text)
|
|
254
|
+
yield* terminal.warn(text)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
// The checks of crow doctor first: when one fails, nothing is changed and
|
|
258
|
+
// deploy stops with the same lines. A check that could not tell is a
|
|
259
|
+
// warning. --skip-checks leaves them all out.
|
|
260
|
+
if (options.skipChecks === true) {
|
|
261
|
+
yield* warn(skippedChecksMessage)
|
|
262
|
+
} else {
|
|
263
|
+
const checks = yield* accountChecks(config, { forDeploy: true })
|
|
264
|
+
|
|
265
|
+
if (hasFailed(checks)) {
|
|
266
|
+
return yield* new DeployBlocked({ message: failedLines(checks).join('\n') })
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
for (const check of checks.filter((candidate) => candidate.mark === 'unknown')) {
|
|
270
|
+
yield* warn(formatCheck(check))
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// The question needs a person: stdin and stdout are terminals.
|
|
275
|
+
const confirmed = Effect.gen(function* () {
|
|
276
|
+
if (options.yes) {
|
|
277
|
+
return true
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (!terminal.stdoutIsTerminal || !terminal.stdinIsTerminal) {
|
|
281
|
+
return yield* new DeployBlocked({ message: needsYesMessage })
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const answer = yield* terminal.ask('Apply these changes? (yes/no)')
|
|
285
|
+
|
|
286
|
+
return answer.trim().toLowerCase() === 'yes'
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
// Series need RESEND_API_KEY in the Worker. Deploy cannot set it, so it
|
|
290
|
+
// says so for every app that has series while the key is missing.
|
|
291
|
+
const warnAboutSeriesKey = Effect.gen(function* () {
|
|
292
|
+
const withSeries = desiredState(config)
|
|
293
|
+
.apps.filter((app) => app.series !== null)
|
|
294
|
+
.map((app) => app.name)
|
|
295
|
+
|
|
296
|
+
if (withSeries.length === 0) {
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const hasKey = yield* api
|
|
301
|
+
.hasSeriesProviderKey()
|
|
302
|
+
.pipe(Effect.catch(() => Effect.succeed(Option.none<boolean>())))
|
|
303
|
+
|
|
304
|
+
if (Option.isSome(hasKey) && !hasKey.value) {
|
|
305
|
+
for (const name of withSeries) {
|
|
306
|
+
yield* warn(seriesKeyMissingMessage(name))
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
const appVersion = appVersionFor(
|
|
312
|
+
yield* readAppVersion(options.bundleDirectory, 'deploy'),
|
|
313
|
+
config.app
|
|
314
|
+
)
|
|
315
|
+
const planOptions = { appVersion, takeOver: options.takeOver }
|
|
316
|
+
const first = yield* buildPlan(config, planOptions)
|
|
317
|
+
|
|
318
|
+
yield* terminal.print(renderPlan(first, config))
|
|
319
|
+
yield* blockPurgingInConfig(first, config)
|
|
320
|
+
|
|
321
|
+
if (isEmpty(first)) {
|
|
322
|
+
yield* warnAboutSeriesKey
|
|
323
|
+
|
|
324
|
+
return { applied: false, plan: first, warnings }
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (!(yield* confirmed)) {
|
|
328
|
+
yield* terminal.print(nothingChangedMessage)
|
|
329
|
+
|
|
330
|
+
return { applied: false, plan: first, warnings }
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let plan = first
|
|
334
|
+
let accessPolicyApplied = false
|
|
335
|
+
let appAnswered = false
|
|
336
|
+
let revocationsPosted = false
|
|
337
|
+
|
|
338
|
+
const emails = desiredState(config).people.map((person) => person.email)
|
|
339
|
+
|
|
340
|
+
// Posts a list of changes in batches. A failed batch is retried by running
|
|
341
|
+
// deploy again: the API applies every change idempotently.
|
|
342
|
+
const postMail = (changes: readonly Change[]) =>
|
|
343
|
+
Effect.gen(function* () {
|
|
344
|
+
const parts = batches(changes)
|
|
345
|
+
|
|
346
|
+
for (const [index, part] of parts.entries()) {
|
|
347
|
+
yield* api.applyChanges(part).pipe(
|
|
348
|
+
Effect.mapError(
|
|
349
|
+
(error) =>
|
|
350
|
+
new MailboxApiError({
|
|
351
|
+
message: `The mail changes could not be applied (batch ${index + 1} of ${parts.length}). ${error.message} Run deploy again. Steps already applied are skipped.`
|
|
352
|
+
})
|
|
353
|
+
)
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
// People who lose access stop reading mail before anything else changes.
|
|
359
|
+
// Their open Access sessions end too, as far as the token allows it.
|
|
360
|
+
// Used for the first plan and for a plan that was computed again.
|
|
361
|
+
const revokeRemovedPeople = Effect.gen(function* () {
|
|
362
|
+
revocationsPosted = false
|
|
363
|
+
|
|
364
|
+
const removed = plan.mail.flatMap((change) =>
|
|
365
|
+
change._tag === 'RemovePerson' ? [change.email] : []
|
|
366
|
+
)
|
|
367
|
+
const revocations = plan.mail.filter(isRevocation)
|
|
368
|
+
|
|
369
|
+
for (const email of removed) {
|
|
370
|
+
yield* cloudflare.revokeAccessSessions(email).pipe(
|
|
371
|
+
Effect.catch((error) =>
|
|
372
|
+
warn(
|
|
373
|
+
sessionsNotEnded(
|
|
374
|
+
email,
|
|
375
|
+
error.kind === 'forbidden' ? undefined : error.message
|
|
376
|
+
)
|
|
377
|
+
)
|
|
378
|
+
)
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (revocations.length === 0) {
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Only an app that is deployed and accepts the credentials has people
|
|
387
|
+
// to revoke.
|
|
388
|
+
const version = yield* api
|
|
389
|
+
.version()
|
|
390
|
+
.pipe(Effect.catch(() => Effect.succeed(Option.none<string>())))
|
|
391
|
+
|
|
392
|
+
if (Option.isSome(version)) {
|
|
393
|
+
yield* postMail(revocations)
|
|
394
|
+
|
|
395
|
+
revocationsPosted = true
|
|
396
|
+
}
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
// The Access policy: exactly the people of the config.
|
|
400
|
+
const updateAccessPolicy = Effect.gen(function* () {
|
|
401
|
+
const application = yield* cloudflare.findAccessApplication(hostname)
|
|
402
|
+
|
|
403
|
+
if (Option.isNone(application)) {
|
|
404
|
+
return yield* new DeployFailed({
|
|
405
|
+
message: `The Access application for ${hostname} was not found. Delete ${credentialsPath} and run deploy again to create it.`
|
|
406
|
+
})
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
yield* setPeoplePolicy(application.value.id, emails)
|
|
410
|
+
|
|
411
|
+
accessPolicyApplied = true
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// The app: database, bucket, Access, then the Worker itself.
|
|
415
|
+
if (plan.app === 'deploy') {
|
|
416
|
+
yield* checkBundle(options.bundleDirectory, 'deploy')
|
|
417
|
+
yield* ensureIgnored(options.directory)
|
|
418
|
+
yield* terminal.print('Setting up the database, bucket and Access.')
|
|
419
|
+
|
|
420
|
+
const setup = yield* prepareApp(config, options.directory, {
|
|
421
|
+
canCreateCredentials: interactive,
|
|
422
|
+
environment: options.environment ?? {}
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
accessPolicyApplied = setup.accessPolicyApplied
|
|
426
|
+
|
|
427
|
+
if (setup.credentialsReplaced === 'missing') {
|
|
428
|
+
yield* terminal.print(credentialsMissingMessage)
|
|
429
|
+
} else if (setup.credentialsReplaced === 'mismatch') {
|
|
430
|
+
yield* terminal.print(credentialsMismatchMessage)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
yield* revokeRemovedPeople
|
|
434
|
+
|
|
435
|
+
const configPath = yield* writeBuildConfig(
|
|
436
|
+
options.directory,
|
|
437
|
+
options.bundleDirectory,
|
|
438
|
+
{
|
|
439
|
+
accessAudience: setup.accessAudience,
|
|
440
|
+
accessTeamDomain: setup.accessTeamDomain,
|
|
441
|
+
appVersion,
|
|
442
|
+
authenticationHost: config.app.authenticationHost,
|
|
443
|
+
branding: config.app.branding,
|
|
444
|
+
databaseId: setup.databaseId,
|
|
445
|
+
deployClientId: setup.deployClientId,
|
|
446
|
+
hostname
|
|
447
|
+
}
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
yield* terminal.print('Applying database migrations.')
|
|
451
|
+
yield* wrangler.applyMigrations(configPath)
|
|
452
|
+
yield* terminal.print(`Deploying the Courier Crow app to ${hostname}.`)
|
|
453
|
+
yield* wrangler.deploy(configPath)
|
|
454
|
+
|
|
455
|
+
// The plan was computed against an app that did not accept the old
|
|
456
|
+
// credentials, so it saw an empty state. Look again with the new ones.
|
|
457
|
+
if (setup.credentialsReplaced !== undefined) {
|
|
458
|
+
yield* waitForApp(hostname, appVersion, appWait)
|
|
459
|
+
|
|
460
|
+
appAnswered = true
|
|
461
|
+
plan = yield* buildPlan(config, planOptions)
|
|
462
|
+
|
|
463
|
+
yield* terminal.print(credentialsRecreatedMessage)
|
|
464
|
+
yield* terminal.print(renderPlan(plan, config))
|
|
465
|
+
yield* blockPurgingInConfig(plan, config)
|
|
466
|
+
|
|
467
|
+
if (!(yield* confirmed)) {
|
|
468
|
+
yield* terminal.print(
|
|
469
|
+
'Stopped. The app was deployed, but nothing else was changed. Run deploy again to continue.'
|
|
470
|
+
)
|
|
471
|
+
|
|
472
|
+
return { applied: false, plan, warnings }
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// The same removal steps as for a first plan, before anything else
|
|
476
|
+
// of this plan changes.
|
|
477
|
+
yield* revokeRemovedPeople
|
|
478
|
+
}
|
|
479
|
+
} else {
|
|
480
|
+
// The app is there: access changes first.
|
|
481
|
+
if (plan.cloudflare.some((change) => change._tag === 'UpdateAccessPolicy')) {
|
|
482
|
+
yield* updateAccessPolicy
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
yield* revokeRemovedPeople
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Email Routing, sending and Access, in plan order.
|
|
489
|
+
const zones = new Map<string, Zone>()
|
|
490
|
+
|
|
491
|
+
const zoneOf = (domain: DomainName) =>
|
|
492
|
+
Effect.gen(function* () {
|
|
493
|
+
const known = zones.get(domain)
|
|
494
|
+
|
|
495
|
+
if (known !== undefined) {
|
|
496
|
+
return known
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const setup = yield* cloudflare.domainSetup(domain).pipe(
|
|
500
|
+
Effect.mapError((error) =>
|
|
501
|
+
error.kind === 'forbidden'
|
|
502
|
+
? new CloudflareError({
|
|
503
|
+
kind: 'forbidden',
|
|
504
|
+
message: missingPermissionMessage(domain)
|
|
505
|
+
})
|
|
506
|
+
: error
|
|
507
|
+
)
|
|
508
|
+
)
|
|
509
|
+
|
|
510
|
+
if (setup.zoneId === null) {
|
|
511
|
+
return yield* new DeployBlocked({ message: notAZoneMessage(domain) })
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const zone = { domain, id: setup.zoneId }
|
|
515
|
+
|
|
516
|
+
zones.set(domain, zone)
|
|
517
|
+
|
|
518
|
+
return zone
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
// A forbidden answer on a zone call names the missing permission.
|
|
522
|
+
const inZone = <Value>(
|
|
523
|
+
domain: DomainName,
|
|
524
|
+
effect: Effect.Effect<Value, CloudflareError>,
|
|
525
|
+
message: string = missingPermissionMessage(domain)
|
|
526
|
+
) =>
|
|
527
|
+
effect.pipe(
|
|
528
|
+
Effect.mapError((error) =>
|
|
529
|
+
error.kind === 'forbidden'
|
|
530
|
+
? new CloudflareError({ kind: 'forbidden', message })
|
|
531
|
+
: error
|
|
532
|
+
)
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
// Something that is gone already does not need deleting.
|
|
536
|
+
const skipGone = (effect: Effect.Effect<void, CloudflareError>) =>
|
|
537
|
+
effect.pipe(
|
|
538
|
+
Effect.catchIf(
|
|
539
|
+
(error) => error.kind === 'notFound',
|
|
540
|
+
() => Effect.void
|
|
541
|
+
)
|
|
542
|
+
)
|
|
543
|
+
|
|
544
|
+
const applyCloudflareChanges = Effect.gen(function* () {
|
|
545
|
+
// Domains where --take-over deleted records. Before such a domain
|
|
546
|
+
// receives mail, nothing foreign may be left.
|
|
547
|
+
const tookOver = new Set<string>(
|
|
548
|
+
plan.cloudflare.flatMap((change) =>
|
|
549
|
+
change._tag === 'ReplaceMxRecords' ||
|
|
550
|
+
change._tag === 'RemoveRoutingRules'
|
|
551
|
+
? [change.domain]
|
|
552
|
+
: []
|
|
553
|
+
)
|
|
554
|
+
)
|
|
555
|
+
const checked = new Set<string>()
|
|
556
|
+
|
|
557
|
+
// Only what the plan showed was deleted. Something that appeared since
|
|
558
|
+
// then is left alone and stops the deploy.
|
|
559
|
+
const ensureNothingNew = (domain: DomainName) =>
|
|
560
|
+
Effect.gen(function* () {
|
|
561
|
+
if (!tookOver.has(domain) || checked.has(domain)) {
|
|
562
|
+
return
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
checked.add(domain)
|
|
566
|
+
|
|
567
|
+
const zone = yield* zoneOf(domain)
|
|
568
|
+
const mx = yield* inZone(domain, cloudflare.foreignMxRecords(zone))
|
|
569
|
+
const rules = yield* inZone(domain, cloudflare.routingRules(zone))
|
|
570
|
+
|
|
571
|
+
if (mx.length > 0 || rules.length > 0) {
|
|
572
|
+
return yield* new DeployBlocked({
|
|
573
|
+
message: changedWhileConfirming(domain)
|
|
574
|
+
})
|
|
575
|
+
}
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
const apply = (change: CloudflareChange) =>
|
|
579
|
+
Effect.gen(function* () {
|
|
580
|
+
switch (change._tag) {
|
|
581
|
+
case 'ReplaceMxRecords': {
|
|
582
|
+
const zone = yield* zoneOf(change.domain)
|
|
583
|
+
|
|
584
|
+
for (const record of change.records) {
|
|
585
|
+
yield* skipGone(
|
|
586
|
+
inZone(
|
|
587
|
+
change.domain,
|
|
588
|
+
cloudflare.deleteDnsRecord(zone, record.id),
|
|
589
|
+
removeMxFailed(change.domain)
|
|
590
|
+
)
|
|
591
|
+
)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
case 'RemoveRoutingRules': {
|
|
598
|
+
const zone = yield* zoneOf(change.domain)
|
|
599
|
+
|
|
600
|
+
for (const rule of change.rules) {
|
|
601
|
+
yield* skipGone(
|
|
602
|
+
inZone(
|
|
603
|
+
change.domain,
|
|
604
|
+
cloudflare.deleteRoutingRule(zone, rule.id)
|
|
605
|
+
)
|
|
606
|
+
)
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
case 'EnableRouting':
|
|
613
|
+
yield* ensureNothingNew(change.domain)
|
|
614
|
+
|
|
615
|
+
return yield* inZone(
|
|
616
|
+
change.domain,
|
|
617
|
+
cloudflare.enableRouting(yield* zoneOf(change.domain))
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
case 'SetCatchAll':
|
|
621
|
+
yield* ensureNothingNew(change.domain)
|
|
622
|
+
|
|
623
|
+
return yield* inZone(
|
|
624
|
+
change.domain,
|
|
625
|
+
cloudflare.setCatchAll(yield* zoneOf(change.domain))
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
case 'OnboardSending': {
|
|
629
|
+
const zone = yield* zoneOf(change.domain)
|
|
630
|
+
|
|
631
|
+
// Receiving works without sending, so whatever goes wrong here
|
|
632
|
+
// is a warning and the deploy goes on.
|
|
633
|
+
const outcome = yield* inZone(
|
|
634
|
+
change.domain,
|
|
635
|
+
cloudflare.onboardSending(zone)
|
|
636
|
+
).pipe(
|
|
637
|
+
Effect.as('onboarded' as const),
|
|
638
|
+
Effect.catch((error) =>
|
|
639
|
+
Effect.succeed(
|
|
640
|
+
error.kind === 'alreadyExists'
|
|
641
|
+
? ('exists' as const)
|
|
642
|
+
: error.kind === 'paidPlanRequired'
|
|
643
|
+
? ('needsPaidPlan' as const)
|
|
644
|
+
: ('failed' as const)
|
|
645
|
+
)
|
|
646
|
+
)
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
if (outcome === 'needsPaidPlan') {
|
|
650
|
+
yield* warn(paidPlanMessage)
|
|
651
|
+
} else if (outcome === 'failed') {
|
|
652
|
+
yield* warn(sendingNotSetUp(change.domain, options.accountId))
|
|
653
|
+
} else if (outcome === 'onboarded') {
|
|
654
|
+
yield* terminal.print(
|
|
655
|
+
`Sending for ${change.domain} is set up. DNS changes can take 5 to 15 minutes before sending works.`
|
|
656
|
+
)
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
case 'RemoveCatchAll':
|
|
663
|
+
return yield* inZone(
|
|
664
|
+
change.domain,
|
|
665
|
+
cloudflare.disableCatchAll(yield* zoneOf(change.domain))
|
|
666
|
+
)
|
|
667
|
+
|
|
668
|
+
case 'UpdateAccessApplication':
|
|
669
|
+
return yield* cloudflare.updateAccessApplication(
|
|
670
|
+
change.applicationId
|
|
671
|
+
)
|
|
672
|
+
|
|
673
|
+
// The application first, then its bypass policy. A run that
|
|
674
|
+
// stops in between leaves an application that lets nobody in;
|
|
675
|
+
// the next plan sees it and adds the policy.
|
|
676
|
+
case 'OpenUnsubscribePage': {
|
|
677
|
+
const isNew = change.applicationId === null
|
|
678
|
+
const applicationId =
|
|
679
|
+
change.applicationId ??
|
|
680
|
+
(yield* cloudflare.createUnsubscribeApplication(change.hostname)).id
|
|
681
|
+
const coverage = yield* cloudflare.accessApplicationCoverage(applicationId)
|
|
682
|
+
|
|
683
|
+
if (Option.isNone(coverage)) {
|
|
684
|
+
return yield* new DeployFailed({ message: unsubscribeApplicationRemovedMessage })
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// The bypass policy opens whatever the application covers, so
|
|
688
|
+
// it goes only on an application for exactly <hostname>/u/*:
|
|
689
|
+
// its domain, and its destinations and self-hosted domains when
|
|
690
|
+
// it has them, hold that path and nothing else.
|
|
691
|
+
if (!coversExactly(coverage.value, unsubscribeDomain(change.hostname))) {
|
|
692
|
+
if (isNew) {
|
|
693
|
+
yield* skipGone(cloudflare.deleteAccessApplication(applicationId))
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
return yield* new DeployFailed({
|
|
697
|
+
message: `Cloudflare did not create the unsubscribe page as a path-only application (got ${printable(describeCoverage(coverage.value))}). Nothing was opened. This needs a fix in Courier Crow; series can still unsubscribe by mail.`
|
|
698
|
+
})
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return yield* cloudflare
|
|
702
|
+
.createAccessPolicy(applicationId, {
|
|
703
|
+
decision: 'bypass',
|
|
704
|
+
include: [{ _tag: 'Everyone' }],
|
|
705
|
+
name: unsubscribePolicyName
|
|
706
|
+
})
|
|
707
|
+
.pipe(
|
|
708
|
+
Effect.catchIf(
|
|
709
|
+
(error) => error.kind === 'notFound',
|
|
710
|
+
() => Effect.fail(new DeployFailed({ message: unsubscribeApplicationRemovedMessage }))
|
|
711
|
+
)
|
|
712
|
+
)
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
case 'CloseUnsubscribePage':
|
|
716
|
+
return yield* skipGone(
|
|
717
|
+
cloudflare.deleteAccessApplication(change.applicationId)
|
|
718
|
+
)
|
|
719
|
+
|
|
720
|
+
// The page is open to more than /u/: the bypass goes at once,
|
|
721
|
+
// and the deploy stops there.
|
|
722
|
+
case 'RemoveUnsubscribeBypass':
|
|
723
|
+
yield* skipGone(
|
|
724
|
+
cloudflare.deleteAccessPolicy(change.applicationId, change.policyId)
|
|
725
|
+
)
|
|
726
|
+
|
|
727
|
+
return yield* new DeployFailed({ message: unsubscribeBypassRemovedMessage })
|
|
728
|
+
|
|
729
|
+
case 'UpdateAccessPolicy':
|
|
730
|
+
// First-run setup, or the step above, did this already.
|
|
731
|
+
if (!accessPolicyApplied) {
|
|
732
|
+
return yield* updateAccessPolicy
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
return
|
|
736
|
+
}
|
|
737
|
+
})
|
|
738
|
+
|
|
739
|
+
for (const change of plan.cloudflare) {
|
|
740
|
+
yield* apply(change)
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// A domain that needed no routing or catch-all change is checked too.
|
|
744
|
+
for (const domain of tookOver) {
|
|
745
|
+
yield* ensureNothingNew(domain as DomainName)
|
|
746
|
+
}
|
|
747
|
+
})
|
|
748
|
+
|
|
749
|
+
if (plan.cloudflare.length > 0) {
|
|
750
|
+
yield* terminal.print('Applying the Cloudflare changes.')
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
yield* applyCloudflareChanges
|
|
754
|
+
|
|
755
|
+
// The mail changes, in plan order. Revocations went first, if they could.
|
|
756
|
+
const remaining = revocationsPosted
|
|
757
|
+
? plan.mail.filter((change) => !isRevocation(change))
|
|
758
|
+
: plan.mail
|
|
759
|
+
|
|
760
|
+
if (remaining.length > 0) {
|
|
761
|
+
if (first.app === 'deploy' && !appAnswered) {
|
|
762
|
+
yield* waitForApp(hostname, appVersion, appWait)
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
yield* postMail(remaining)
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// Apps get their tokens once the API knows them. The secrets are printed
|
|
769
|
+
// last, in a block of their own, so they are not lost in the output.
|
|
770
|
+
let credentials: readonly AppCredentials[] = []
|
|
771
|
+
|
|
772
|
+
if (changesAppTokens(plan)) {
|
|
773
|
+
if (remaining.length === 0 && first.app === 'deploy' && !appAnswered) {
|
|
774
|
+
yield* waitForApp(hostname, appVersion, appWait)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
yield* terminal.print('Applying credentials for apps.')
|
|
778
|
+
|
|
779
|
+
const applied = yield* applyAppCredentials(hostname, interactive)
|
|
780
|
+
|
|
781
|
+
credentials = applied.saved
|
|
782
|
+
|
|
783
|
+
for (const app of applied.needTerminal) {
|
|
784
|
+
yield* warn(appNeedsTerminalMessage(app))
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// The app answers at its new hostname and its apps have credentials
|
|
789
|
+
// there: what Courier Crow made for the old hostname goes, exactly what the
|
|
790
|
+
// plan listed. Something gone already is fine.
|
|
791
|
+
if (plan.movesFrom !== undefined) {
|
|
792
|
+
for (const application of plan.movesFrom.applications) {
|
|
793
|
+
yield* skipGone(cloudflare.deleteAccessApplication(application.id))
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
for (const token of plan.movesFrom.tokens) {
|
|
797
|
+
yield* skipGone(cloudflare.deleteServiceToken(token.id))
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
yield* warnAboutSeriesKey
|
|
802
|
+
yield* terminal.print('Deploy finished.')
|
|
803
|
+
yield* printCredentials(hostname, credentials)
|
|
804
|
+
|
|
805
|
+
return { applied: true, plan, warnings }
|
|
806
|
+
})
|
|
807
|
+
}
|