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/render.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import type { Change, MailConfig } from '@couriercrow/core'
|
|
2
|
+
|
|
3
|
+
import type { CloudflareChange } from './cloudflare/cloudflare-state'
|
|
4
|
+
import type { AppResource, DeployPlan } from './plan'
|
|
5
|
+
import { printable } from './printable'
|
|
6
|
+
|
|
7
|
+
function compare(left: string, right: string): number {
|
|
8
|
+
return left < right ? -1 : left > right ? 1 : 0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function domainOf(address: string): string {
|
|
12
|
+
return address.slice(address.lastIndexOf('@') + 1)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cloudflareLine(change: CloudflareChange): string[] {
|
|
16
|
+
if (change._tag === 'RemoveRoutingRules') {
|
|
17
|
+
return change.rules.map((rule) => `- remove routing rule: ${rule.description}`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const line = singleLine(change)
|
|
21
|
+
|
|
22
|
+
return line === undefined ? [] : [line]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function singleLine(change: CloudflareChange): string | undefined {
|
|
26
|
+
switch (change._tag) {
|
|
27
|
+
case 'EnableRouting':
|
|
28
|
+
return '+ enable Email Routing'
|
|
29
|
+
case 'OnboardSending':
|
|
30
|
+
return '+ set up sending'
|
|
31
|
+
case 'RemoveCatchAll':
|
|
32
|
+
return `- stop receiving mail for ${change.domain}`
|
|
33
|
+
case 'RemoveRoutingRules':
|
|
34
|
+
return undefined
|
|
35
|
+
case 'ReplaceMxRecords':
|
|
36
|
+
return `- replace MX records pointing to ${change.records.map((record) => record.host).join(', ')}`
|
|
37
|
+
case 'SetCatchAll':
|
|
38
|
+
return '+ route all mail to the Courier Crow app'
|
|
39
|
+
case 'CloseUnsubscribePage':
|
|
40
|
+
case 'OpenUnsubscribePage':
|
|
41
|
+
case 'RemoveUnsubscribeBypass':
|
|
42
|
+
case 'UpdateAccessApplication':
|
|
43
|
+
case 'UpdateAccessPolicy':
|
|
44
|
+
return undefined
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The changes that belong to the app's Access setup, not to a domain.
|
|
49
|
+
function isAccessChange(
|
|
50
|
+
change: CloudflareChange
|
|
51
|
+
): change is Extract<
|
|
52
|
+
CloudflareChange,
|
|
53
|
+
{
|
|
54
|
+
_tag:
|
|
55
|
+
| 'CloseUnsubscribePage'
|
|
56
|
+
| 'OpenUnsubscribePage'
|
|
57
|
+
| 'RemoveUnsubscribeBypass'
|
|
58
|
+
| 'UpdateAccessApplication'
|
|
59
|
+
| 'UpdateAccessPolicy'
|
|
60
|
+
}
|
|
61
|
+
> {
|
|
62
|
+
return (
|
|
63
|
+
change._tag === 'CloseUnsubscribePage' ||
|
|
64
|
+
change._tag === 'OpenUnsubscribePage' ||
|
|
65
|
+
change._tag === 'RemoveUnsubscribeBypass' ||
|
|
66
|
+
change._tag === 'UpdateAccessApplication' ||
|
|
67
|
+
change._tag === 'UpdateAccessPolicy'
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function resourceLine(resource: AppResource): string {
|
|
72
|
+
const nouns = {
|
|
73
|
+
AccessApplication: 'Access application for',
|
|
74
|
+
Bucket: 'bucket',
|
|
75
|
+
Database: 'database',
|
|
76
|
+
Worker: 'Worker'
|
|
77
|
+
}
|
|
78
|
+
const what = `${nouns[resource._tag]} ${resource.name}`
|
|
79
|
+
const verb = { create: '+ create', replace: '~ replace existing', use: '~ use existing' }
|
|
80
|
+
|
|
81
|
+
return `${verb[resource.state]} ${what}`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type AppChange = Extract<
|
|
85
|
+
Change,
|
|
86
|
+
{
|
|
87
|
+
_tag:
|
|
88
|
+
| 'AddApp'
|
|
89
|
+
| 'RemoveApp'
|
|
90
|
+
| 'SetAppClient'
|
|
91
|
+
| 'SetAppSenders'
|
|
92
|
+
| 'SetAppSeries'
|
|
93
|
+
| 'SetAppViewers'
|
|
94
|
+
}
|
|
95
|
+
>
|
|
96
|
+
|
|
97
|
+
function isAppChange(change: Change): change is AppChange {
|
|
98
|
+
return (
|
|
99
|
+
change._tag === 'AddApp' ||
|
|
100
|
+
change._tag === 'RemoveApp' ||
|
|
101
|
+
change._tag === 'SetAppClient' ||
|
|
102
|
+
change._tag === 'SetAppSenders' ||
|
|
103
|
+
change._tag === 'SetAppSeries' ||
|
|
104
|
+
change._tag === 'SetAppViewers'
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const providerNames = { resend: 'Resend' } as const
|
|
109
|
+
|
|
110
|
+
// The line of the series provider, at the top of the Apps section.
|
|
111
|
+
function providerLines(plan: DeployPlan): string[] {
|
|
112
|
+
return plan.mail.flatMap((change) =>
|
|
113
|
+
change._tag !== 'SetSeriesProvider'
|
|
114
|
+
? []
|
|
115
|
+
: change.provider === null
|
|
116
|
+
? ['- stop sending series (no provider is set up)']
|
|
117
|
+
: [`+ send series through ${providerNames[change.provider]}`]
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The lines of the Apps section, app by app. A new app is one line with its
|
|
122
|
+
// senders; its own sender and viewer changes are part of that line. An app on
|
|
123
|
+
// a domain whose sending is not set up gets a note.
|
|
124
|
+
function appLines(plan: DeployPlan, config: MailConfig): string[] {
|
|
125
|
+
const changes = plan.mail.filter(isAppChange)
|
|
126
|
+
// An app that is there already and gets AddApp moves to another domain.
|
|
127
|
+
const moved = new Set(plan.movedApps ?? [])
|
|
128
|
+
const added = new Set(
|
|
129
|
+
changes.flatMap((change) =>
|
|
130
|
+
change._tag === 'AddApp' && !moved.has(change.name) ? [change.name] : []
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
const withoutSending = new Set<string>(
|
|
134
|
+
plan.cloudflare.flatMap((change) =>
|
|
135
|
+
change._tag === 'OnboardSending' ? [change.domain] : []
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
const configured = Object.entries(config.apps ?? {})
|
|
139
|
+
const unused = plan.unusedAppTokens ?? []
|
|
140
|
+
const movedHostname = plan.appsMovedHostname ?? []
|
|
141
|
+
const names = [
|
|
142
|
+
...new Set([
|
|
143
|
+
...plan.appsWithoutCredentials,
|
|
144
|
+
...movedHostname,
|
|
145
|
+
...changes.map((change) => change.name),
|
|
146
|
+
...unused.map((token) => token.app),
|
|
147
|
+
...configured
|
|
148
|
+
.filter(([, app]) => withoutSending.has(app.domain))
|
|
149
|
+
.map(([name]) => name)
|
|
150
|
+
])
|
|
151
|
+
].sort(compare)
|
|
152
|
+
|
|
153
|
+
return names.flatMap((name) => {
|
|
154
|
+
const domain = configured.find(([candidate]) => candidate === name)?.[1].domain
|
|
155
|
+
|
|
156
|
+
return [
|
|
157
|
+
...(plan.appsWithoutCredentials.includes(name)
|
|
158
|
+
? [`~ create new credentials for ${name} (the last deploy did not finish)`]
|
|
159
|
+
: []),
|
|
160
|
+
...(movedHostname.includes(name)
|
|
161
|
+
? [`~ give ${name} new credentials (the app moved to ${config.app.hostname})`]
|
|
162
|
+
: []),
|
|
163
|
+
...(unused.some((token) => token.app === name)
|
|
164
|
+
? [`- delete unused credentials of ${name}`]
|
|
165
|
+
: []),
|
|
166
|
+
...changeLines(
|
|
167
|
+
changes.filter((change) => change.name === name),
|
|
168
|
+
added
|
|
169
|
+
),
|
|
170
|
+
...(domain !== undefined && withoutSending.has(domain)
|
|
171
|
+
? [
|
|
172
|
+
`! sending is not set up for ${domain} yet; ${name} cannot send until it is`
|
|
173
|
+
]
|
|
174
|
+
: [])
|
|
175
|
+
]
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function changeLines(
|
|
180
|
+
changes: readonly AppChange[],
|
|
181
|
+
added: ReadonlySet<string>
|
|
182
|
+
): string[] {
|
|
183
|
+
return changes.flatMap((change) => {
|
|
184
|
+
switch (change._tag) {
|
|
185
|
+
case 'AddApp': {
|
|
186
|
+
if (!added.has(change.name)) {
|
|
187
|
+
return [`~ move ${change.name} to ${change.domain}`]
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const senders = changes.find(
|
|
191
|
+
(other) => other._tag === 'SetAppSenders' && other.name === change.name
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
return [
|
|
195
|
+
senders?._tag === 'SetAppSenders'
|
|
196
|
+
? `+ add app ${change.name} (sends as ${senders.senders.join(', ')})`
|
|
197
|
+
: `+ add app ${change.name}`
|
|
198
|
+
]
|
|
199
|
+
}
|
|
200
|
+
case 'RemoveApp':
|
|
201
|
+
return [`- remove app ${change.name} (its log is kept)`]
|
|
202
|
+
case 'SetAppClient':
|
|
203
|
+
return []
|
|
204
|
+
case 'SetAppSenders':
|
|
205
|
+
return added.has(change.name) ? [] : [`~ change senders of ${change.name}`]
|
|
206
|
+
case 'SetAppSeries':
|
|
207
|
+
return change.series === null
|
|
208
|
+
? [`- stop series of ${change.name} (enrolments left stop sending)`]
|
|
209
|
+
: [
|
|
210
|
+
`${added.has(change.name) ? '+' : '~'} ${change.name} sends series from ${change.series.from}, with "${change.series.postalAddress}" in the footer`
|
|
211
|
+
]
|
|
212
|
+
case 'SetAppViewers':
|
|
213
|
+
return added.has(change.name) ? [] : [`~ change viewers of ${change.name}`]
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
type MailChange = Exclude<Change, AppChange | { _tag: 'SetSeriesProvider' }>
|
|
219
|
+
|
|
220
|
+
// The domain a mail change belongs to, or undefined for the People section.
|
|
221
|
+
function mailDomain(change: MailChange): string | undefined {
|
|
222
|
+
switch (change._tag) {
|
|
223
|
+
case 'AddDomain':
|
|
224
|
+
case 'AddMailbox':
|
|
225
|
+
case 'RemoveDomain':
|
|
226
|
+
return change.domain
|
|
227
|
+
case 'ArchiveMailbox':
|
|
228
|
+
case 'RestoreMailbox':
|
|
229
|
+
return domainOf(change.address)
|
|
230
|
+
case 'GrantAccess':
|
|
231
|
+
case 'RevokeAccess':
|
|
232
|
+
return domainOf(change.mailbox)
|
|
233
|
+
case 'AddPerson':
|
|
234
|
+
case 'RemovePerson':
|
|
235
|
+
case 'RenamePerson':
|
|
236
|
+
return undefined
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function mailLine(
|
|
241
|
+
change: MailChange,
|
|
242
|
+
currentPeople: DeployPlan['currentPeople']
|
|
243
|
+
): string {
|
|
244
|
+
switch (change._tag) {
|
|
245
|
+
case 'AddDomain':
|
|
246
|
+
return '+ add domain'
|
|
247
|
+
case 'AddMailbox':
|
|
248
|
+
return `+ add mailbox ${change.address}`
|
|
249
|
+
case 'AddPerson':
|
|
250
|
+
return `+ add ${change.name} <${change.email}>`
|
|
251
|
+
case 'ArchiveMailbox':
|
|
252
|
+
return `- archive mailbox ${change.address} (mail is kept)`
|
|
253
|
+
case 'GrantAccess':
|
|
254
|
+
return `+ give ${change.person} access to ${change.mailbox}`
|
|
255
|
+
case 'RemoveDomain':
|
|
256
|
+
return '- remove domain'
|
|
257
|
+
case 'RemovePerson': {
|
|
258
|
+
const name = currentPeople.find(
|
|
259
|
+
(person) => person.email === change.email
|
|
260
|
+
)?.name
|
|
261
|
+
|
|
262
|
+
return name === undefined
|
|
263
|
+
? `- remove ${change.email}`
|
|
264
|
+
: `- remove ${name} <${change.email}>`
|
|
265
|
+
}
|
|
266
|
+
case 'RenamePerson':
|
|
267
|
+
return `~ rename ${change.email} to ${change.name}`
|
|
268
|
+
case 'RestoreMailbox':
|
|
269
|
+
return `~ restore mailbox ${change.address}`
|
|
270
|
+
case 'RevokeAccess':
|
|
271
|
+
return `- remove ${change.person}'s access to ${change.mailbox}`
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function section(title: string, lines: readonly string[]): string[] {
|
|
276
|
+
return lines.length === 0
|
|
277
|
+
? []
|
|
278
|
+
: [
|
|
279
|
+
[title, ...lines.map((line) => ` ${line}`)]
|
|
280
|
+
.map((line) => printable(line))
|
|
281
|
+
.join('\n')
|
|
282
|
+
]
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function renderPlan(plan: DeployPlan, config: MailConfig): string {
|
|
286
|
+
const domainLines = new Map<string, string[]>()
|
|
287
|
+
|
|
288
|
+
const addLine = (domain: string, line: string): void => {
|
|
289
|
+
domainLines.set(domain, [...(domainLines.get(domain) ?? []), line])
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
for (const change of plan.cloudflare) {
|
|
293
|
+
if (!isAccessChange(change)) {
|
|
294
|
+
for (const line of cloudflareLine(change)) {
|
|
295
|
+
addLine(change.domain, line)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (const address of plan.purgingMailboxes ?? []) {
|
|
301
|
+
addLine(
|
|
302
|
+
domainOf(address),
|
|
303
|
+
`! ${address} is being purged. Run bunx couriercrow purge ${address} again to finish.`
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const peopleLines: string[] = []
|
|
308
|
+
|
|
309
|
+
for (const change of plan.mail) {
|
|
310
|
+
if (isAppChange(change) || change._tag === 'SetSeriesProvider') {
|
|
311
|
+
continue
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const domain = mailDomain(change)
|
|
315
|
+
|
|
316
|
+
if (domain === undefined) {
|
|
317
|
+
peopleLines.push(mailLine(change, plan.currentPeople))
|
|
318
|
+
} else {
|
|
319
|
+
addLine(domain, mailLine(change, plan.currentPeople))
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const accessLines = plan.cloudflare.flatMap((change): string[] => {
|
|
324
|
+
switch (change._tag) {
|
|
325
|
+
case 'CloseUnsubscribePage':
|
|
326
|
+
return [`- close the unsubscribe page at ${change.hostname}/u/`]
|
|
327
|
+
case 'OpenUnsubscribePage':
|
|
328
|
+
return [`+ open the unsubscribe page at ${change.hostname}/u/ to everyone`]
|
|
329
|
+
// The coverage comes from Cloudflare, so it is made printable.
|
|
330
|
+
case 'RemoveUnsubscribeBypass':
|
|
331
|
+
return [
|
|
332
|
+
`- remove the open-to-everyone rule of the unsubscribe page at ${change.hostname}/u/ (its application covers ${printable(change.coverage)})`
|
|
333
|
+
]
|
|
334
|
+
case 'UpdateAccessApplication':
|
|
335
|
+
return [
|
|
336
|
+
`~ set the Access sign-in cookie to SameSite=Lax (now ${
|
|
337
|
+
change.sameSiteCookie === null || change.sameSiteCookie === ''
|
|
338
|
+
? 'not set'
|
|
339
|
+
: change.sameSiteCookie
|
|
340
|
+
})`
|
|
341
|
+
]
|
|
342
|
+
case 'UpdateAccessPolicy':
|
|
343
|
+
return [
|
|
344
|
+
...change.add.map((email) => `+ allow ${email}`),
|
|
345
|
+
...change.remove.map((email) => `- stop allowing ${email}`)
|
|
346
|
+
]
|
|
347
|
+
default:
|
|
348
|
+
return []
|
|
349
|
+
}
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
// What Courier Crow made for the hostname it moves from. The names come from
|
|
353
|
+
// Cloudflare, so they are made printable.
|
|
354
|
+
for (const application of plan.movesFrom?.applications ?? []) {
|
|
355
|
+
accessLines.push(`- remove the Access application of ${printable(application.hostname)}`)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
for (const token of [...(plan.movesFrom?.tokens ?? [])].sort((first, second) =>
|
|
359
|
+
compare(first.name, second.name)
|
|
360
|
+
)) {
|
|
361
|
+
accessLines.push(`- delete the service token ${printable(token.name)}`)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (plan.keepUnsubscribePageUntil !== undefined) {
|
|
365
|
+
accessLines.push(
|
|
366
|
+
`~ keep the unsubscribe page open until ${plan.keepUnsubscribePageUntil.slice(0, 10)} (mail already sent links to it)`
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const sections = [
|
|
371
|
+
...section(
|
|
372
|
+
'App',
|
|
373
|
+
plan.app === 'deploy'
|
|
374
|
+
? [
|
|
375
|
+
...plan.resources.map(resourceLine),
|
|
376
|
+
`+ deploy the Courier Crow app to ${config.app.hostname}`,
|
|
377
|
+
...(plan.authenticationHost === undefined
|
|
378
|
+
? []
|
|
379
|
+
: [authenticationHostLine(plan.authenticationHost.to)])
|
|
380
|
+
]
|
|
381
|
+
: []
|
|
382
|
+
),
|
|
383
|
+
...[...domainLines.keys()]
|
|
384
|
+
.sort(compare)
|
|
385
|
+
.flatMap((domain) => section(domain, domainLines.get(domain) ?? [])),
|
|
386
|
+
...section('People', peopleLines),
|
|
387
|
+
...section('Apps', [
|
|
388
|
+
...providerLines(plan),
|
|
389
|
+
...(plan.movesFrom === undefined || plan.movesFrom.isFinishing
|
|
390
|
+
? []
|
|
391
|
+
: [`~ apps get new credentials (the app moves to ${config.app.hostname})`]),
|
|
392
|
+
...appLines(plan, config)
|
|
393
|
+
]),
|
|
394
|
+
...section('Access to the app', accessLines)
|
|
395
|
+
]
|
|
396
|
+
|
|
397
|
+
return sections.length === 0
|
|
398
|
+
? 'No changes. Everything matches mail.config.ts.'
|
|
399
|
+
: sections.join('\n\n')
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function authenticationHostLine(to: string | null): string {
|
|
403
|
+
return to === null
|
|
404
|
+
? '~ stop reading SPF, DKIM and DMARC: app.authenticationHost is not set, so every message shows Not checked'
|
|
405
|
+
: `~ read SPF, DKIM and DMARC from the Authentication-Results header of ${printable(to)}`
|
|
406
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Effect, Option, Schema } from 'effect'
|
|
2
|
+
|
|
3
|
+
import { rotateAppToken } from './app-credentials'
|
|
4
|
+
import { MailboxApi, MailboxApiError } from './mailbox-api'
|
|
5
|
+
import { printable } from './printable'
|
|
6
|
+
import { Terminal } from './terminal'
|
|
7
|
+
|
|
8
|
+
export class RotateRefused extends Schema.TaggedError<RotateRefused>()(
|
|
9
|
+
'RotateRefused',
|
|
10
|
+
{ message: Schema.String }
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
export const rotateNeedsTerminalMessage =
|
|
14
|
+
'Rotating a secret stops the current one at once and needs to be confirmed by hand. Run it in a terminal.'
|
|
15
|
+
|
|
16
|
+
// Asks, then gives the app a new secret and prints it. The old secret stops
|
|
17
|
+
// working as soon as the API uses the new token.
|
|
18
|
+
export function rotateAppSecret(name: string, hostname: string) {
|
|
19
|
+
return Effect.gen(function* () {
|
|
20
|
+
const api = yield* MailboxApi
|
|
21
|
+
const terminal = yield* Terminal
|
|
22
|
+
|
|
23
|
+
if (!terminal.stdinIsTerminal || !terminal.stdoutIsTerminal) {
|
|
24
|
+
return yield* new RotateRefused({ message: rotateNeedsTerminalMessage })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const state = yield* api.currentState()
|
|
28
|
+
|
|
29
|
+
if (Option.isNone(state)) {
|
|
30
|
+
return yield* new MailboxApiError({
|
|
31
|
+
message: `The Courier Crow app at ${hostname} did not answer. Run bunx couriercrow deploy first.`
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!state.value.apps.some((app) => app.name === name)) {
|
|
36
|
+
return yield* new RotateRefused({
|
|
37
|
+
message: `There is no app named ${printable(name)} in the deployed app. Add it to apps in mail.config.ts and run bunx couriercrow deploy.`
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const answer = yield* terminal.ask(
|
|
42
|
+
`Rotate the secret of ${name}? The current secret stops working at once. (yes/no)`
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if (answer.trim().toLowerCase() !== 'yes') {
|
|
46
|
+
yield* terminal.print('Nothing was changed.')
|
|
47
|
+
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
yield* rotateAppToken(name, hostname, state.value.apps)
|
|
52
|
+
})
|
|
53
|
+
}
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The command line runs on Bun: it uses Bun's files, processes and its
|
|
2
|
+
// loading of mail.config.ts. bin/crow.js says so when Node runs it.
|
|
3
|
+
|
|
4
|
+
export const minimumBunVersion = '1.4.0'
|
|
5
|
+
|
|
6
|
+
export const nodeMessage =
|
|
7
|
+
"Courier Crow's command line needs Bun. Install it from https://bun.sh, then run bunx couriercrow."
|
|
8
|
+
|
|
9
|
+
function parts(version: string): number[] {
|
|
10
|
+
return (version.split('-')[0] ?? '').split('.').map((part) => {
|
|
11
|
+
const value = Number.parseInt(part, 10)
|
|
12
|
+
|
|
13
|
+
return Number.isNaN(value) ? 0 : value
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Undefined when this Bun is new enough. A pre-release counts as its version.
|
|
18
|
+
export function bunVersionProblem(version: string): string | undefined {
|
|
19
|
+
const have = parts(version)
|
|
20
|
+
const need = parts(minimumBunVersion)
|
|
21
|
+
|
|
22
|
+
for (let index = 0; index < need.length; index += 1) {
|
|
23
|
+
const difference = (have[index] ?? 0) - (need[index] ?? 0)
|
|
24
|
+
|
|
25
|
+
if (difference !== 0) {
|
|
26
|
+
return difference > 0
|
|
27
|
+
? undefined
|
|
28
|
+
: `Courier Crow needs Bun ${minimumBunVersion} or newer. You have ${version}. Run bun upgrade.`
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return undefined
|
|
33
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Effect, Schema } from 'effect'
|
|
2
|
+
|
|
3
|
+
import { Cloudflare, workerName } from './cloudflare/cloudflare-client'
|
|
4
|
+
import { maxInputCharacters, Terminal } from './terminal'
|
|
5
|
+
import { Wrangler } from './wrangler'
|
|
6
|
+
|
|
7
|
+
export class SetSecretRefused extends Schema.TaggedError<SetSecretRefused>()(
|
|
8
|
+
'SetSecretRefused',
|
|
9
|
+
{ message: Schema.String }
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
// The secrets set-secret takes. Only the series provider's key, for now.
|
|
13
|
+
export const secretNames = ['RESEND_API_KEY'] as const
|
|
14
|
+
|
|
15
|
+
export type SecretName = (typeof secretNames)[number]
|
|
16
|
+
|
|
17
|
+
// Control characters, and the line and paragraph separators.
|
|
18
|
+
const lineBreakPattern = /[\p{Cc}\p{Zl}\p{Zp}]/u
|
|
19
|
+
|
|
20
|
+
// Stores a secret in the deployed Worker. The value comes from a hidden
|
|
21
|
+
// prompt, or from stdin when it is piped. It is passed to wrangler on its
|
|
22
|
+
// stdin and is never printed, logged or written to disk.
|
|
23
|
+
export function setSecret(name: SecretName) {
|
|
24
|
+
return Effect.gen(function* () {
|
|
25
|
+
const cloudflare = yield* Cloudflare
|
|
26
|
+
const terminal = yield* Terminal
|
|
27
|
+
const wrangler = yield* Wrangler
|
|
28
|
+
|
|
29
|
+
if (!(yield* cloudflare.workerExists(workerName))) {
|
|
30
|
+
return yield* new SetSecretRefused({
|
|
31
|
+
message: `The Courier Crow app is not deployed yet, so it has nowhere to keep the key. Run bunx couriercrow deploy first, then run bunx couriercrow set-secret ${name} again.`
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const typed = terminal.stdinIsTerminal
|
|
36
|
+
? yield* terminal.askHidden(`Paste the value of ${name}. It is not shown.`)
|
|
37
|
+
: yield* terminal.readInput()
|
|
38
|
+
const value = typed.trim()
|
|
39
|
+
|
|
40
|
+
if (value === '') {
|
|
41
|
+
return yield* new SetSecretRefused({
|
|
42
|
+
message: `No value was given, so nothing was changed. Paste the key when asked, or pipe it in: bunx couriercrow set-secret ${name} < key.txt`
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (value.length > maxInputCharacters) {
|
|
47
|
+
return yield* new SetSecretRefused({
|
|
48
|
+
message:
|
|
49
|
+
'That value is too long for an API key, so nothing was changed. Check what you pasted and try again.'
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (lineBreakPattern.test(value)) {
|
|
54
|
+
return yield* new SetSecretRefused({
|
|
55
|
+
message:
|
|
56
|
+
'The value must be one line, so nothing was changed. Check what you pasted and try again.'
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
yield* wrangler.putSecret(name, value)
|
|
61
|
+
yield* terminal.print(`${name} is set. Series can send now.`)
|
|
62
|
+
})
|
|
63
|
+
}
|