@crombolo/server 0.43.1 → 0.44.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/package.json +13 -13
- package/src/config.ts +42 -2
- package/src/core.ts +5 -1
- package/src/form-submit.ts +32 -5
- package/src/form-workflows.ts +7 -1
- package/src/index.ts +1 -0
- package/src/no-nodes.ts +1 -1
- package/src/remote-ci.ts +334 -0
- package/src/remote.ts +459 -11
- package/src/schema.ts +7 -0
- package/src/seed/compile.ts +17 -15
- package/src/seed/export.ts +16 -9
- package/src/seed/read.ts +43 -13
- package/src/server.ts +57 -4
- package/src/settings.ts +13 -4
package/src/remote.ts
CHANGED
|
@@ -18,14 +18,36 @@ import { join, sep } from 'node:path'
|
|
|
18
18
|
import { hasSection, type Principal } from '@crombolo/api-management/domain'
|
|
19
19
|
import { DEVICE_CODE_GRANT, REMOTE_CLIENT_ID } from '@crombolo/auth'
|
|
20
20
|
import { managementApiPath, RemotePermissions, type RemoteVerb } from '@crombolo/core'
|
|
21
|
-
import {
|
|
22
|
-
|
|
21
|
+
import {
|
|
22
|
+
AuditLog,
|
|
23
|
+
auditRecord,
|
|
24
|
+
type Db,
|
|
25
|
+
type RemoteJob,
|
|
26
|
+
RemoteJobRepository,
|
|
27
|
+
RemoteTrustRepository,
|
|
28
|
+
TransferRunRepository,
|
|
29
|
+
type TrustRule,
|
|
30
|
+
} from '@crombolo/data'
|
|
31
|
+
import {
|
|
32
|
+
type ContentSet,
|
|
33
|
+
isResolution,
|
|
34
|
+
type NodeResolutions,
|
|
35
|
+
type Resolution,
|
|
36
|
+
revertRun,
|
|
37
|
+
} from '@crombolo/transfer'
|
|
23
38
|
import { readAuditPageQuery, recordAudit } from './adapters/audit.ts'
|
|
24
|
-
import {
|
|
25
|
-
|
|
39
|
+
import {
|
|
40
|
+
checkBundleContent,
|
|
41
|
+
installBundleContent,
|
|
42
|
+
readBundle,
|
|
43
|
+
revertSections,
|
|
44
|
+
} from './bundle-install.ts'
|
|
45
|
+
import { carriesSections, swappedSections } from './bundle-sections.ts'
|
|
46
|
+
import { type CromboloConfig, REMOTE_ACCESS_LEVELS, type RemoteAccess } from './config.ts'
|
|
26
47
|
import { answerContentQuery, contentQueryScope } from './content-query.ts'
|
|
27
48
|
import { mediaStoreFor } from './media-files.ts'
|
|
28
49
|
import { type MediaStore, streamOf } from './media-store.ts'
|
|
50
|
+
import { createCiSignIn, readRuleInput, trustedIssuers } from './remote-ci.ts'
|
|
29
51
|
import { openSecret, sealSecret, usableSecretKey } from './secrets.ts'
|
|
30
52
|
import { exportSite } from './site-file.ts'
|
|
31
53
|
import { replaceSummary } from './site-replace.ts'
|
|
@@ -61,6 +83,11 @@ export interface Remote {
|
|
|
61
83
|
handle(request: Request, url: URL): Promise<Response | undefined>
|
|
62
84
|
/** Whether a request is an upload the remote takes beyond the usual body limit. */
|
|
63
85
|
isUpload(request: Request, url: URL): boolean
|
|
86
|
+
/**
|
|
87
|
+
* The trust rules (story 160), for the back office's CI access screen, which
|
|
88
|
+
* calls them under its own path: `rest` is `trust` or `trust/<key>[/<verb>]`.
|
|
89
|
+
*/
|
|
90
|
+
trust(request: Request, principal: Principal, rest: string): Promise<Response>
|
|
64
91
|
}
|
|
65
92
|
|
|
66
93
|
interface Problem {
|
|
@@ -119,8 +146,25 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
119
146
|
id: settings.environment,
|
|
120
147
|
name: settings.environment,
|
|
121
148
|
protected: settings.protected,
|
|
149
|
+
access: level,
|
|
122
150
|
})
|
|
123
151
|
|
|
152
|
+
/** What the site takes (story 167): each level the ones before it and more. */
|
|
153
|
+
const level: RemoteAccess = settings.access ?? (settings.enabled ? 'all' : 'off')
|
|
154
|
+
const atLeast = (wanted: RemoteAccess) =>
|
|
155
|
+
REMOTE_ACCESS_LEVELS.indexOf(level) >= REMOTE_ACCESS_LEVELS.indexOf(wanted)
|
|
156
|
+
const notAtLevel = (wanted: RemoteAccess, what: string) =>
|
|
157
|
+
problem(403, {
|
|
158
|
+
code: 'access-level',
|
|
159
|
+
title: `This site does not take ${what}`,
|
|
160
|
+
detail: `Its CROMBOLO_REMOTE_ACCESS is ${level}; ${what} need ${wanted}.`,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
/** Who may change this site's content: Push, and PushProtected where it is protected. */
|
|
164
|
+
const mayChange = (principal: Principal) =>
|
|
165
|
+
may(principal, RemotePermissions.Push) &&
|
|
166
|
+
(!settings.protected || may(principal, RemotePermissions.PushProtected))
|
|
167
|
+
|
|
124
168
|
// Under the back office, as the editor's own API calls are; a command line
|
|
125
169
|
// follows these addresses rather than building its own.
|
|
126
170
|
const managementApi = managementApiPath(config.backOfficePath.replace(/\/+$/, ''))
|
|
@@ -134,8 +178,12 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
134
178
|
token: `${origin}${managementApi}/security/back-office/token`,
|
|
135
179
|
clientId: REMOTE_CLIENT_ID,
|
|
136
180
|
grantType: DEVICE_CODE_GRANT,
|
|
181
|
+
// Story 160: a CI job's identity token, exchanged for a token acting as a trust rule.
|
|
182
|
+
ci: ci.discovery(origin, `${origin}${REMOTE_API_PATH}`),
|
|
137
183
|
},
|
|
138
184
|
capabilities: [
|
|
185
|
+
...(atLeast('content') ? ['bundle-apply'] : []),
|
|
186
|
+
'ci-sign-in',
|
|
139
187
|
'media-fetch',
|
|
140
188
|
'push-check',
|
|
141
189
|
'audit',
|
|
@@ -145,6 +193,18 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
145
193
|
environments: [environment()],
|
|
146
194
|
})
|
|
147
195
|
|
|
196
|
+
/** Where a CI principal's run was, for its audit rows: repository, ref and run. */
|
|
197
|
+
const ciRun = (principal: Principal) => {
|
|
198
|
+
const c = principal.ci?.claims
|
|
199
|
+
if (!c) return ''
|
|
200
|
+
const ref = c.ref
|
|
201
|
+
? ` ${c.refType ?? 'ref'} ${c.ref}`
|
|
202
|
+
: c.environment
|
|
203
|
+
? ` environment ${c.environment}`
|
|
204
|
+
: ''
|
|
205
|
+
return ` (CI: ${c.repository}${ref}${c.run ? `, run ${c.run}` : ''})`
|
|
206
|
+
}
|
|
207
|
+
|
|
148
208
|
const audit = (principal: Principal, request: Request, action: string, detail: string) =>
|
|
149
209
|
recordAudit(db, {
|
|
150
210
|
category: 'system',
|
|
@@ -156,9 +216,124 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
156
216
|
entityName: settings.environment,
|
|
157
217
|
ipAddress: null,
|
|
158
218
|
forwardedFor: request.headers.get('x-forwarded-for'),
|
|
159
|
-
detail
|
|
219
|
+
detail: `${detail}${ciRun(principal)}`,
|
|
160
220
|
})
|
|
161
221
|
|
|
222
|
+
const trust = new RemoteTrustRepository(db)
|
|
223
|
+
const ci = createCiSignIn({
|
|
224
|
+
db,
|
|
225
|
+
issuers: trustedIssuers(settings.oidcIssuers),
|
|
226
|
+
onExchange: (outcome, request) =>
|
|
227
|
+
recordAudit(db, {
|
|
228
|
+
category: 'system',
|
|
229
|
+
action: outcome.ok ? 'RemoteCiSignIn' : 'RemoteCiSignInRefused',
|
|
230
|
+
source: 'request',
|
|
231
|
+
userKey: outcome.ok ? outcome.rule.key : null,
|
|
232
|
+
userName: outcome.ok ? `CI: ${outcome.rule.name}` : null,
|
|
233
|
+
entityType: 'RemoteEnvironment',
|
|
234
|
+
entityName: settings.environment,
|
|
235
|
+
ipAddress: null,
|
|
236
|
+
forwardedFor: request.headers.get('x-forwarded-for'),
|
|
237
|
+
detail: outcome.ok
|
|
238
|
+
? `${outcome.claims.repository} ${outcome.claims.refType ?? ''} ${outcome.claims.ref ?? outcome.claims.environment ?? ''}${outcome.claims.run ? `, run ${outcome.claims.run}` : ''}`.replace(
|
|
239
|
+
/\s+/g,
|
|
240
|
+
' ',
|
|
241
|
+
)
|
|
242
|
+
: `Refused: ${outcome.reason}${outcome.repository ? ` (${outcome.repository})` : ''}`,
|
|
243
|
+
}),
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
/** A rule as the trust endpoints show it. */
|
|
247
|
+
const ruleView = (rule: TrustRule) => ({
|
|
248
|
+
key: rule.key,
|
|
249
|
+
name: rule.name,
|
|
250
|
+
issuer: rule.issuer,
|
|
251
|
+
repository: rule.repository,
|
|
252
|
+
refKind: rule.refKind,
|
|
253
|
+
refValue: rule.refValue,
|
|
254
|
+
permissions: rule.permissions,
|
|
255
|
+
disabled: rule.disabled,
|
|
256
|
+
createdBy: rule.createdBy,
|
|
257
|
+
createDate: rule.createDate.toISOString(),
|
|
258
|
+
lastUsedDate: rule.lastUsedDate?.toISOString() ?? null,
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
/** The trust rules, for administrators: a person, never CI, and never over a cross-site write. */
|
|
262
|
+
async function trustRoute(
|
|
263
|
+
request: Request,
|
|
264
|
+
principal: Principal,
|
|
265
|
+
rest: string,
|
|
266
|
+
): Promise<Response> {
|
|
267
|
+
if (!principal.isAdmin || principal.ci)
|
|
268
|
+
return problem(403, { code: 'forbidden', title: 'Only an administrator manages CI access' })
|
|
269
|
+
if (request.method !== 'GET' && !fromThisSiteOrBearer(request))
|
|
270
|
+
return new Response('Forbidden', { status: 403 })
|
|
271
|
+
const changed = (action: string, rule: TrustRule) =>
|
|
272
|
+
audit(
|
|
273
|
+
principal,
|
|
274
|
+
request,
|
|
275
|
+
action,
|
|
276
|
+
`${rule.name}: ${rule.repository} ${rule.refKind} ${rule.refValue}, may ${rule.permissions.join(', ')}`,
|
|
277
|
+
)
|
|
278
|
+
if (rest === 'trust') {
|
|
279
|
+
if (request.method === 'GET')
|
|
280
|
+
return Response.json({
|
|
281
|
+
// Rules can be made before the site answers as a remote; the screen says so.
|
|
282
|
+
remoteEnabled: settings.enabled,
|
|
283
|
+
issuers: ci.issuers,
|
|
284
|
+
permissions: Object.values(RemotePermissions),
|
|
285
|
+
items: (await trust.all()).map(ruleView),
|
|
286
|
+
})
|
|
287
|
+
if (request.method === 'POST') {
|
|
288
|
+
const { input, problems } = readRuleInput(
|
|
289
|
+
await request.json().catch(() => ({})),
|
|
290
|
+
ci.issuers,
|
|
291
|
+
)
|
|
292
|
+
if (!input)
|
|
293
|
+
return problem(400, {
|
|
294
|
+
code: 'invalid-rule',
|
|
295
|
+
title: 'That rule cannot be saved',
|
|
296
|
+
items: problems,
|
|
297
|
+
})
|
|
298
|
+
const rule = await trust.create(input, principal.name)
|
|
299
|
+
await changed('RemoteTrustRuleAdded', rule)
|
|
300
|
+
return Response.json(ruleView(rule), { status: 201 })
|
|
301
|
+
}
|
|
302
|
+
return new Response(null, { status: 405 })
|
|
303
|
+
}
|
|
304
|
+
const one = /^trust\/([0-9a-f-]{36})(?:\/(disable|enable))?$/.exec(rest)
|
|
305
|
+
if (!one) return problem(404, { code: 'not-found', title: 'There is no such rule' })
|
|
306
|
+
const [, key, action] = one as unknown as [string, string, string | undefined]
|
|
307
|
+
const existing = await trust.byKey(key)
|
|
308
|
+
if (!existing) return problem(404, { code: 'not-found', title: 'There is no such rule' })
|
|
309
|
+
if (action && request.method === 'POST') {
|
|
310
|
+
const rule = (await trust.setDisabled(key, action === 'disable')) as TrustRule
|
|
311
|
+
await changed(
|
|
312
|
+
action === 'disable' ? 'RemoteTrustRuleDisabled' : 'RemoteTrustRuleEnabled',
|
|
313
|
+
rule,
|
|
314
|
+
)
|
|
315
|
+
return Response.json(ruleView(rule))
|
|
316
|
+
}
|
|
317
|
+
if (!action && request.method === 'PUT') {
|
|
318
|
+
const { input, problems } = readRuleInput(await request.json().catch(() => ({})), ci.issuers)
|
|
319
|
+
if (!input)
|
|
320
|
+
return problem(400, {
|
|
321
|
+
code: 'invalid-rule',
|
|
322
|
+
title: 'That rule cannot be saved',
|
|
323
|
+
items: problems,
|
|
324
|
+
})
|
|
325
|
+
const rule = (await trust.update(key, input)) as TrustRule
|
|
326
|
+
await changed('RemoteTrustRuleChanged', rule)
|
|
327
|
+
return Response.json(ruleView(rule))
|
|
328
|
+
}
|
|
329
|
+
if (!action && request.method === 'DELETE') {
|
|
330
|
+
await trust.remove(key)
|
|
331
|
+
await changed('RemoteTrustRuleDeleted', existing)
|
|
332
|
+
return new Response(null, { status: 204 })
|
|
333
|
+
}
|
|
334
|
+
return new Response(null, { status: 405 })
|
|
335
|
+
}
|
|
336
|
+
|
|
162
337
|
/** A job as the protocol shows it; a running one gone quiet is reported as failed. */
|
|
163
338
|
const describe = async (job: RemoteJob, origin: string) => {
|
|
164
339
|
let current = job
|
|
@@ -433,6 +608,223 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
433
608
|
return { sha256: hasher.digest('hex'), size }
|
|
434
609
|
}
|
|
435
610
|
|
|
611
|
+
/** The options a bundle apply was sent with, in `Crombolo-Options` (story 167). */
|
|
612
|
+
const applyOptions = (request: Request) => {
|
|
613
|
+
let raw: Record<string, unknown> = {}
|
|
614
|
+
try {
|
|
615
|
+
raw = JSON.parse(request.headers.get('crombolo-options') ?? '{}') as Record<string, unknown>
|
|
616
|
+
} catch {
|
|
617
|
+
raw = {}
|
|
618
|
+
}
|
|
619
|
+
const resolutions =
|
|
620
|
+
raw.resolutions && typeof raw.resolutions === 'object'
|
|
621
|
+
? (raw.resolutions as Record<string, NodeResolutions>)
|
|
622
|
+
: undefined
|
|
623
|
+
const resolveAll =
|
|
624
|
+
typeof raw.resolveAll === 'string' && isResolution(raw.resolveAll)
|
|
625
|
+
? (raw.resolveAll as Resolution)
|
|
626
|
+
: undefined
|
|
627
|
+
return {
|
|
628
|
+
dryRun: raw.dryRun === true,
|
|
629
|
+
publish: raw.publish === true,
|
|
630
|
+
label: typeof raw.label === 'string' ? raw.label.slice(0, 200) : undefined,
|
|
631
|
+
resolutions,
|
|
632
|
+
resolveAll,
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* A bundle applied here (story 167): checked, and unless a dry run backed up
|
|
638
|
+
* and applied in one transaction as `bundle apply` does on a site's own
|
|
639
|
+
* machine. Content needs the `content` level and the right to change this
|
|
640
|
+
* site; schema, views or code need `all` and Crombolo.Remote.ApplyStructure.
|
|
641
|
+
*/
|
|
642
|
+
async function applyBundle(request: Request, principal: Principal): Promise<Response> {
|
|
643
|
+
if (!atLeast('content')) return notAtLevel('content', 'bundles')
|
|
644
|
+
if (!mayChange(principal))
|
|
645
|
+
return problem(403, { code: 'forbidden', title: 'You may not change this environment' })
|
|
646
|
+
const declared = request.headers.get('crombolo-sha256')?.toLowerCase()
|
|
647
|
+
if (!declared || !/^[0-9a-f]{64}$/.test(declared))
|
|
648
|
+
return problem(400, { code: 'hash-required', title: 'Crombolo-Sha256 is required' })
|
|
649
|
+
const asked = applyOptions(request)
|
|
650
|
+
const id = crypto.randomUUID()
|
|
651
|
+
const local = join(tmpdir(), `crombolo-remote-bundle-${id}.tgz`)
|
|
652
|
+
const dir = join(tmpdir(), `crombolo-remote-bundle-${id}`)
|
|
653
|
+
try {
|
|
654
|
+
let received: { sha256: string; size: number } | undefined
|
|
655
|
+
try {
|
|
656
|
+
received = await receive(request, local)
|
|
657
|
+
} catch {
|
|
658
|
+
return problem(413, {
|
|
659
|
+
code: 'too-large',
|
|
660
|
+
title: 'The bundle is larger than this remote takes',
|
|
661
|
+
})
|
|
662
|
+
}
|
|
663
|
+
if (!received)
|
|
664
|
+
return problem(400, { code: 'body-required', title: 'The request carried no bundle' })
|
|
665
|
+
if (received.sha256 !== declared)
|
|
666
|
+
return problem(400, {
|
|
667
|
+
code: 'hash-mismatch',
|
|
668
|
+
title: 'The bundle is not the one described',
|
|
669
|
+
detail:
|
|
670
|
+
'Its SHA-256 does not match Crombolo-Sha256: it was changed or cut short on the way.',
|
|
671
|
+
})
|
|
672
|
+
await extractTarball(local, dir)
|
|
673
|
+
let structural: boolean
|
|
674
|
+
try {
|
|
675
|
+
structural = carriesSections(readBundle(dir, config.siteDir).carried)
|
|
676
|
+
} catch (error) {
|
|
677
|
+
return problem(400, {
|
|
678
|
+
code: 'not-a-bundle',
|
|
679
|
+
title: 'That is not a bundle this site can read',
|
|
680
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
681
|
+
})
|
|
682
|
+
}
|
|
683
|
+
if (structural && !atLeast('all'))
|
|
684
|
+
return notAtLevel('all', 'bundles that change content types, views or code')
|
|
685
|
+
if (structural && !may(principal, RemotePermissions.ApplyStructure))
|
|
686
|
+
return problem(403, {
|
|
687
|
+
code: 'forbidden-structure',
|
|
688
|
+
title: "You may not change this site's structure",
|
|
689
|
+
detail:
|
|
690
|
+
'The bundle carries content types, views or code, which needs Crombolo.Remote.ApplyStructure.',
|
|
691
|
+
})
|
|
692
|
+
const input = {
|
|
693
|
+
bundle: dir,
|
|
694
|
+
publish: asked.publish,
|
|
695
|
+
resolutions: asked.resolutions,
|
|
696
|
+
resolveAll: asked.resolveAll,
|
|
697
|
+
by: principal.name,
|
|
698
|
+
}
|
|
699
|
+
const report = (check: {
|
|
700
|
+
counts: unknown
|
|
701
|
+
findings: Array<{ kind: string; code: string; key?: string; message: string }>
|
|
702
|
+
outstanding: unknown[]
|
|
703
|
+
}) => ({
|
|
704
|
+
counts: check.counts,
|
|
705
|
+
findings: check.findings.map((f) => ({
|
|
706
|
+
kind: f.kind,
|
|
707
|
+
code: f.code,
|
|
708
|
+
key: f.key ?? null,
|
|
709
|
+
message: f.message,
|
|
710
|
+
})),
|
|
711
|
+
outstanding: check.outstanding.length,
|
|
712
|
+
})
|
|
713
|
+
if (asked.dryRun) {
|
|
714
|
+
const checked = await checkBundleContent(db, config, input)
|
|
715
|
+
return Response.json({
|
|
716
|
+
dryRun: true,
|
|
717
|
+
structural,
|
|
718
|
+
bundle: checked.bundle,
|
|
719
|
+
...report(checked.check),
|
|
720
|
+
clean: checked.clean,
|
|
721
|
+
problems: checked.problems,
|
|
722
|
+
})
|
|
723
|
+
}
|
|
724
|
+
// A backup wherever this server can take one; on Postgres that needs CROMBOLO_PG_DUMP.
|
|
725
|
+
const canBackUp = config.dialect === 'sqlite' || Boolean(config.pgDump)
|
|
726
|
+
const result = await installBundleContent(db, config, {
|
|
727
|
+
...input,
|
|
728
|
+
backupTaken: !canBackUp,
|
|
729
|
+
label: asked.label ?? `bundle from ${principal.name}`,
|
|
730
|
+
})
|
|
731
|
+
if (result.runId) {
|
|
732
|
+
await audit(
|
|
733
|
+
principal,
|
|
734
|
+
request,
|
|
735
|
+
'RemoteBundleApplied',
|
|
736
|
+
`${result.bundle.id} as run ${result.runId}${structural ? ', with structure' : ''}`,
|
|
737
|
+
)
|
|
738
|
+
options.onApplied?.()
|
|
739
|
+
}
|
|
740
|
+
return Response.json(
|
|
741
|
+
{
|
|
742
|
+
dryRun: false,
|
|
743
|
+
structural,
|
|
744
|
+
bundle: result.bundle,
|
|
745
|
+
runId: result.runId ?? null,
|
|
746
|
+
...report(result.check),
|
|
747
|
+
problems: result.problems,
|
|
748
|
+
published: result.published,
|
|
749
|
+
notPublished: result.publishFailures,
|
|
750
|
+
},
|
|
751
|
+
{ status: result.runId ? 200 : 409 },
|
|
752
|
+
)
|
|
753
|
+
} finally {
|
|
754
|
+
await rm(local, { force: true })
|
|
755
|
+
await rm(dir, { recursive: true, force: true })
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** The applies on this site, newest first: for `bundle runs --remote`. */
|
|
760
|
+
async function listRuns(url: URL, principal: Principal): Promise<Response> {
|
|
761
|
+
if (!atLeast('read')) return notAtLevel('read', 'reads')
|
|
762
|
+
if (!may(principal, RemotePermissions.Pull) && !mayChange(principal))
|
|
763
|
+
return problem(403, { code: 'forbidden', title: 'You may not read this environment' })
|
|
764
|
+
const take = Math.min(Number(url.searchParams.get('limit') ?? 20) || 20, 200)
|
|
765
|
+
const skip = Number(url.searchParams.get('offset') ?? 0) || 0
|
|
766
|
+
const runs = new TransferRunRepository(db)
|
|
767
|
+
const [items, total] = await Promise.all([runs.recent(take, skip), runs.count()])
|
|
768
|
+
return Response.json({
|
|
769
|
+
total,
|
|
770
|
+
items: items.map((run) => ({
|
|
771
|
+
id: run.id,
|
|
772
|
+
startedAt: run.startedAt.toISOString(),
|
|
773
|
+
direction: run.direction,
|
|
774
|
+
status: run.status,
|
|
775
|
+
nodeCount: run.nodeCount,
|
|
776
|
+
bundleId: run.bundleId,
|
|
777
|
+
bundleLabel: run.bundleLabel ?? null,
|
|
778
|
+
appliedBy: run.appliedBy ?? null,
|
|
779
|
+
})),
|
|
780
|
+
})
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/** Undoes an apply (story 167), as `bundle revert` does here; files it replaced come back too. */
|
|
784
|
+
async function revertBundleRun(
|
|
785
|
+
request: Request,
|
|
786
|
+
principal: Principal,
|
|
787
|
+
runId: string,
|
|
788
|
+
): Promise<Response> {
|
|
789
|
+
if (!atLeast('content')) return notAtLevel('content', 'reverts')
|
|
790
|
+
if (!mayChange(principal))
|
|
791
|
+
return problem(403, { code: 'forbidden', title: 'You may not change this environment' })
|
|
792
|
+
if (!fromThisSiteOrBearer(request)) return new Response('Forbidden', { status: 403 })
|
|
793
|
+
const body = (await request.json().catch(() => ({}))) as { force?: unknown }
|
|
794
|
+
// A run that replaced files changes structure back, which needs the same as it did.
|
|
795
|
+
const swapped = swappedSections(config, runId)
|
|
796
|
+
if (
|
|
797
|
+
swapped.length > 0 &&
|
|
798
|
+
(!atLeast('all') || !may(principal, RemotePermissions.ApplyStructure))
|
|
799
|
+
)
|
|
800
|
+
return problem(403, {
|
|
801
|
+
code: 'forbidden-structure',
|
|
802
|
+
title: "Reverting that run changes this site's structure",
|
|
803
|
+
detail: 'It needs CROMBOLO_REMOTE_ACCESS=all and Crombolo.Remote.ApplyStructure.',
|
|
804
|
+
})
|
|
805
|
+
const result = await revertRun(db, runId, {
|
|
806
|
+
force: body.force === true,
|
|
807
|
+
nodeId: config.nodeId,
|
|
808
|
+
backOfficePath: config.backOfficePath,
|
|
809
|
+
})
|
|
810
|
+
if (!result.runId)
|
|
811
|
+
return problem(409, {
|
|
812
|
+
code: 'revert-refused',
|
|
813
|
+
title: 'The revert was refused; nothing was changed',
|
|
814
|
+
items: result.check.findings.map((f) => f.message),
|
|
815
|
+
})
|
|
816
|
+
const files = await revertSections(db, config, runId)
|
|
817
|
+
await audit(principal, request, 'RemoteBundleReverted', `${runId} as run ${result.runId}`)
|
|
818
|
+
options.onApplied?.()
|
|
819
|
+
return Response.json({
|
|
820
|
+
runId: result.runId,
|
|
821
|
+
reverted: runId,
|
|
822
|
+
restored: result.restored,
|
|
823
|
+
recycled: result.recycled,
|
|
824
|
+
files,
|
|
825
|
+
})
|
|
826
|
+
}
|
|
827
|
+
|
|
436
828
|
async function startPush(request: Request, url: URL, principal: Principal): Promise<Response> {
|
|
437
829
|
if (!may(principal, RemotePermissions.Push))
|
|
438
830
|
return problem(403, { code: 'forbidden', title: 'You may not push to this environment' })
|
|
@@ -513,6 +905,9 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
513
905
|
approve: boolean,
|
|
514
906
|
): Promise<Response> {
|
|
515
907
|
if (!fromThisSiteOrBearer(request)) return new Response('Forbidden', { status: 403 })
|
|
908
|
+
// CI never approves, whatever its rule holds: a second person is the point.
|
|
909
|
+
if (principal.ci)
|
|
910
|
+
return problem(403, { code: 'ci-cannot-approve', title: 'CI cannot approve pushes' })
|
|
516
911
|
const job = await jobs.get(id)
|
|
517
912
|
if (job?.kind !== 'push')
|
|
518
913
|
return problem(404, { code: 'not-found', title: 'There is no such push' })
|
|
@@ -710,10 +1105,16 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
710
1105
|
}
|
|
711
1106
|
|
|
712
1107
|
return {
|
|
1108
|
+
trust: trustRoute,
|
|
1109
|
+
|
|
713
1110
|
isUpload(request, url) {
|
|
714
1111
|
if (!settings.enabled || request.method !== 'PUT') return false
|
|
715
1112
|
const prefix = `${REMOTE_API_PATH}/environments/${settings.environment}/`
|
|
716
|
-
return
|
|
1113
|
+
return (
|
|
1114
|
+
url.pathname === `${prefix}pushes` ||
|
|
1115
|
+
url.pathname === `${prefix}bundles` ||
|
|
1116
|
+
url.pathname.startsWith(`${prefix}media/`)
|
|
1117
|
+
)
|
|
717
1118
|
},
|
|
718
1119
|
|
|
719
1120
|
async handle(request, url) {
|
|
@@ -727,7 +1128,12 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
727
1128
|
if (!pathname.startsWith(`${REMOTE_API_PATH}/`)) return undefined
|
|
728
1129
|
if (!settings.enabled)
|
|
729
1130
|
return problem(404, { code: 'not-enabled', title: 'This site is not a remote' })
|
|
730
|
-
|
|
1131
|
+
if (pathname === `${REMOTE_API_PATH}/token`)
|
|
1132
|
+
return request.method === 'POST'
|
|
1133
|
+
? ci.exchange(request, url, request.headers.get('x-forwarded-for')?.split(',')[0]?.trim())
|
|
1134
|
+
: new Response(null, { status: 405 })
|
|
1135
|
+
// A CI token is good here and nowhere else; a person's is the back office's own.
|
|
1136
|
+
const principal = (await ci.principalOf(request)) ?? (await options.authenticate(request))
|
|
731
1137
|
if (!principal)
|
|
732
1138
|
return problem(401, {
|
|
733
1139
|
code: 'sign-in-required',
|
|
@@ -736,16 +1142,33 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
736
1142
|
})
|
|
737
1143
|
const rest = pathname.slice(REMOTE_API_PATH.length + 1)
|
|
738
1144
|
|
|
1145
|
+
// Who this is, for `crombolo whoami`: a person, or CI acting by a rule.
|
|
1146
|
+
if (rest === 'me' && request.method === 'GET')
|
|
1147
|
+
return Response.json({
|
|
1148
|
+
name: principal.name,
|
|
1149
|
+
kind: principal.ci ? 'ci' : 'person',
|
|
1150
|
+
...(principal.ci ? { rule: principal.ci.rule, run: principal.ci.claims } : {}),
|
|
1151
|
+
permissions: Object.values(RemotePermissions).filter((verb) => may(principal, verb)),
|
|
1152
|
+
})
|
|
1153
|
+
if (rest === 'trust' || rest.startsWith('trust/')) return trustRoute(request, principal, rest)
|
|
1154
|
+
|
|
739
1155
|
if (rest === 'environments' && request.method === 'GET')
|
|
740
1156
|
return Response.json([
|
|
741
1157
|
{
|
|
742
1158
|
...environment(),
|
|
743
1159
|
may: {
|
|
744
|
-
pull: may(principal, RemotePermissions.Pull),
|
|
745
|
-
pullFull: may(principal, RemotePermissions.PullFull),
|
|
1160
|
+
pull: atLeast('read') && may(principal, RemotePermissions.Pull),
|
|
1161
|
+
pullFull: atLeast('read') && may(principal, RemotePermissions.PullFull),
|
|
1162
|
+
// A whole-site push changes structure, so it needs that right and the top level.
|
|
746
1163
|
push:
|
|
747
|
-
|
|
748
|
-
(
|
|
1164
|
+
atLeast('all') &&
|
|
1165
|
+
mayChange(principal) &&
|
|
1166
|
+
may(principal, RemotePermissions.ApplyStructure),
|
|
1167
|
+
apply: atLeast('content') && mayChange(principal),
|
|
1168
|
+
applyStructure:
|
|
1169
|
+
atLeast('all') &&
|
|
1170
|
+
mayChange(principal) &&
|
|
1171
|
+
may(principal, RemotePermissions.ApplyStructure),
|
|
749
1172
|
audit: mayReadAudit(principal),
|
|
750
1173
|
},
|
|
751
1174
|
},
|
|
@@ -790,6 +1213,31 @@ export function createRemote(options: RemoteOptions): Remote {
|
|
|
790
1213
|
await request.json().catch(() => undefined),
|
|
791
1214
|
)
|
|
792
1215
|
}
|
|
1216
|
+
// Story 167: the site's level first, then the person's permissions.
|
|
1217
|
+
const pulling =
|
|
1218
|
+
what === 'pulls' ||
|
|
1219
|
+
what === 'media/links' ||
|
|
1220
|
+
(what.startsWith('media/') && request.method === 'GET')
|
|
1221
|
+
const pushing =
|
|
1222
|
+
what === 'pushes' ||
|
|
1223
|
+
what === 'pushes/check' ||
|
|
1224
|
+
what === 'media/have' ||
|
|
1225
|
+
(what.startsWith('media/') && request.method === 'PUT')
|
|
1226
|
+
if (pulling && !atLeast('read')) return notAtLevel('read', 'pulls')
|
|
1227
|
+
if (pushing && !atLeast('all')) return notAtLevel('all', 'whole-site pushes')
|
|
1228
|
+
// Push and protection are refused by their own routes first, as before story 167.
|
|
1229
|
+
if (pushing && mayChange(principal) && !may(principal, RemotePermissions.ApplyStructure))
|
|
1230
|
+
return problem(403, {
|
|
1231
|
+
code: 'forbidden-structure',
|
|
1232
|
+
title: 'You may not push a whole site here',
|
|
1233
|
+
detail:
|
|
1234
|
+
"A push replaces the site's content types, views and code, which needs Crombolo.Remote.ApplyStructure.",
|
|
1235
|
+
})
|
|
1236
|
+
if (what === 'bundles' && request.method === 'PUT') return applyBundle(request, principal)
|
|
1237
|
+
if (what === 'runs' && request.method === 'GET') return listRuns(url, principal)
|
|
1238
|
+
const revert = /^runs\/([0-9a-f-]{36})\/revert$/.exec(what)
|
|
1239
|
+
if (revert && request.method === 'POST')
|
|
1240
|
+
return revertBundleRun(request, principal, revert[1] as string)
|
|
793
1241
|
if (what === 'pulls' && request.method === 'POST') return startPull(request, url, principal)
|
|
794
1242
|
if (what === 'pushes' && request.method === 'PUT') return startPush(request, url, principal)
|
|
795
1243
|
if (what === 'pushes/check' && request.method === 'POST') {
|
package/src/schema.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
contractsLedgeredAfter,
|
|
14
14
|
currentSchemaState,
|
|
15
15
|
type Db,
|
|
16
|
+
ensureFormLibraryTypes,
|
|
16
17
|
ensureSystemContentTypes,
|
|
17
18
|
ensureSystemMemberTypes,
|
|
18
19
|
type Finding,
|
|
@@ -127,6 +128,11 @@ export async function syncSchemaDirectory(
|
|
|
127
128
|
// sync never applies a snapshot that a later backoffice change has overtaken.
|
|
128
129
|
return db.locks.withLock(Locks.Schema, async () => {
|
|
129
130
|
const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
|
|
131
|
+
// The types made from forms follow the forms whether or not the files changed.
|
|
132
|
+
// Before the sync, not after: nothing may come between the sync recording a
|
|
133
|
+
// new schema state and its caller moving the node to it, or the poller can
|
|
134
|
+
// see the database ahead of the node and take it out of service.
|
|
135
|
+
await ensureFormLibraryTypes(db, loaded.set.forms ?? [])
|
|
130
136
|
const report = await syncSchema(
|
|
131
137
|
db,
|
|
132
138
|
loaded,
|
|
@@ -153,6 +159,7 @@ export async function bootSchema(db: Db, config: CromboloConfig): Promise<Schema
|
|
|
153
159
|
await touchServer(db, { nodeId, ...nodeState, role: config.role, cromboloVersion: VERSION })
|
|
154
160
|
await ensureSystemMemberTypes(db)
|
|
155
161
|
await ensureSystemContentTypes(db)
|
|
162
|
+
await ensureFormLibraryTypes(db, [])
|
|
156
163
|
return { nodeState, nodeId, report: undefined, compatibilityMode: false }
|
|
157
164
|
}
|
|
158
165
|
const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
|