canopycms-cdk 0.0.68-int.98 → 0.1.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/README.md +22 -15
- package/dist/constructs/asset-support.d.ts +104 -84
- package/dist/constructs/asset-support.js +193 -195
- package/dist/constructs/cms-distribution.d.ts +2 -3
- package/dist/constructs/cms-distribution.js +5 -6
- package/dist/constructs/cms-service.d.ts +150 -18
- package/dist/constructs/cms-service.js +498 -94
- package/dist/constructs/editor-routing.d.ts +2 -2
- package/dist/constructs/worker-bundle.d.ts +42 -0
- package/dist/constructs/worker-bundle.js +118 -0
- package/dist/constructs/worker-lifecycle.d.ts +70 -0
- package/dist/constructs/worker-lifecycle.js +111 -0
- package/dist/constructs/worker-units.d.ts +99 -0
- package/dist/constructs/worker-units.js +398 -0
- package/dist/index.d.ts +2 -1
- package/lambda/asset-transform/dist/handler.js +813 -30360
- package/package.json +11 -6
- package/worker/canopy-github.service +81 -0
- package/worker/canopy-github.socket +16 -0
- package/worker/canopy-worker.service +76 -15
- package/worker/dist/index.js +77784 -40975
- package/worker/dist/index.js.contract +1 -0
- package/worker/dist/index.js.sha256 +1 -0
- package/worker/dist/instance-files.sh +365 -0
- package/worker/gateway.env.example +25 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker instance's systemd units and the `canopy-inspect` sandbox. `build:worker` packs them
|
|
3
|
+
* into `worker/dist/instance-files.sh` (`instanceFilesScript`), which user data (cms-service.ts)
|
|
4
|
+
* downloads, checks against its sha256 and installs from, by layout. The split's three units are
|
|
5
|
+
* also checked in under worker/, byte for byte, for a hand install; worker-units.test.ts holds the
|
|
6
|
+
* copies together.
|
|
7
|
+
*
|
|
8
|
+
* Two layouts, chosen at boot by the bundle's contract banner (WORKER_CONTRACT_BANNER_PREFIX):
|
|
9
|
+
* - split, for a bundle at GATEWAY_SPLIT_CONTRACT or later: `canopy-worker.service` with no
|
|
10
|
+
* network and no secret, `canopy-github.service` holding every secret with no view of EFS, and
|
|
11
|
+
* `canopy-github.socket` between them;
|
|
12
|
+
* - single-process, for an older bundle: one `canopy-worker.service` holding the secrets, as
|
|
13
|
+
* before the split.
|
|
14
|
+
*/
|
|
15
|
+
import { EXIT_DRAINED_FOR_TERMINATION, GATEWAY_SPLIT_CONTRACT, GITHUB_GATEWAY_SOCKET_ENV, GITHUB_GATEWAY_SOCKET_PATH, WORKER_CONTRACT_ENV, WORKER_CONTRACT_VERSION, } from './worker-lifecycle.js';
|
|
16
|
+
/**
|
|
17
|
+
* Where the Lambda AND the worker mount the `WorkspaceAP` access point (rooted
|
|
18
|
+
* at EFS:/workspace), and so the workspace root of both.
|
|
19
|
+
*
|
|
20
|
+
* One path string, not merely one directory: git records absolute paths on
|
|
21
|
+
* EFS (a workspace clone's `origin` is `<root>/remote.git`), and a path written
|
|
22
|
+
* by one process must resolve for the other.
|
|
23
|
+
*/
|
|
24
|
+
export const EFS_MOUNT_PATH = '/mnt/efs';
|
|
25
|
+
/** The worker's code, env files and the inspect sandbox's scripts: root-owned. */
|
|
26
|
+
export const WORKER_INSTALL_DIR = '/opt/canopy-worker';
|
|
27
|
+
/** The GitHub gateway's system user (D8 of the split's plan): a static, non-login account. */
|
|
28
|
+
export const GATEWAY_USER = 'canopy-github';
|
|
29
|
+
/** The worker's user; `canopy-github.socket` belongs to it, so only it (and root) can connect. */
|
|
30
|
+
export const WORKER_USER = 'ec2-user';
|
|
31
|
+
/** The worker contract the single-process unit provides: the last before the split. */
|
|
32
|
+
export const SINGLE_PROCESS_UNIT_CONTRACT = GATEWAY_SPLIT_CONTRACT - 1;
|
|
33
|
+
/**
|
|
34
|
+
* The sandbox both services share. Node and git need none of what these take away;
|
|
35
|
+
* `MemoryDenyWriteExecute` is left out because V8's JIT writes executable memory.
|
|
36
|
+
*/
|
|
37
|
+
export const WORKER_SANDBOX_DIRECTIVES = [
|
|
38
|
+
'NoNewPrivileges=yes',
|
|
39
|
+
'ProtectSystem=strict',
|
|
40
|
+
'ProtectHome=tmpfs',
|
|
41
|
+
'PrivateTmp=yes',
|
|
42
|
+
'PrivateDevices=yes',
|
|
43
|
+
'ProtectProc=invisible',
|
|
44
|
+
'ProtectKernelTunables=yes',
|
|
45
|
+
'ProtectKernelModules=yes',
|
|
46
|
+
'ProtectKernelLogs=yes',
|
|
47
|
+
'ProtectControlGroups=yes',
|
|
48
|
+
'ProtectClock=yes',
|
|
49
|
+
'ProtectHostname=yes',
|
|
50
|
+
'RestrictNamespaces=yes',
|
|
51
|
+
'RestrictSUIDSGID=yes',
|
|
52
|
+
'RestrictRealtime=yes',
|
|
53
|
+
'LockPersonality=yes',
|
|
54
|
+
'SystemCallArchitectures=native',
|
|
55
|
+
'CapabilityBoundingSet=',
|
|
56
|
+
];
|
|
57
|
+
/**
|
|
58
|
+
* Hidden from both services under the split. The journal because the worker's user's groups can
|
|
59
|
+
* read it (and a crash's environment is journaled there); `/dev/shm` because it would otherwise be
|
|
60
|
+
* the one directory both services and root can write outside EFS and `PrivateTmp`; the system bus
|
|
61
|
+
* because neither service has a use for it. A `-` covers only the path it prefixes, and makes a
|
|
62
|
+
* missing one (no persistent journal) no error.
|
|
63
|
+
*/
|
|
64
|
+
const HIDDEN_PATHS = [
|
|
65
|
+
'InaccessiblePaths=-/run/dbus',
|
|
66
|
+
'InaccessiblePaths=-/dev/shm',
|
|
67
|
+
'InaccessiblePaths=-/var/log/journal',
|
|
68
|
+
'InaccessiblePaths=-/run/log/journal',
|
|
69
|
+
];
|
|
70
|
+
/**
|
|
71
|
+
* The split worker's sandbox, which `canopy-inspect` applies too, so an operator's command on EFS
|
|
72
|
+
* runs exactly as confined as the worker: the shared sandbox, the workspace writable, and the cut.
|
|
73
|
+
* - `PrivateNetwork=yes`: only `lo`, so no route to the instance metadata service.
|
|
74
|
+
* - `IPAddressDeny=any`: a second layer, which needs cgroup BPF (silently absent without it).
|
|
75
|
+
* - `RestrictAddressFamilies=AF_UNIX`: `socket()` only; the gateway's socket is a file.
|
|
76
|
+
* - `PrivateIPC=yes`, the hidden paths, `SystemCallFilter=@system-service` (node and git need
|
|
77
|
+
* nothing outside it), and `LimitCORE=0`.
|
|
78
|
+
*/
|
|
79
|
+
export const WORKER_SPLIT_SANDBOX = [
|
|
80
|
+
...WORKER_SANDBOX_DIRECTIVES,
|
|
81
|
+
`ReadWritePaths=${EFS_MOUNT_PATH}`,
|
|
82
|
+
'PrivateNetwork=yes',
|
|
83
|
+
'IPAddressDeny=any',
|
|
84
|
+
'RestrictAddressFamilies=AF_UNIX',
|
|
85
|
+
'PrivateIPC=yes',
|
|
86
|
+
...HIDDEN_PATHS,
|
|
87
|
+
'SystemCallFilter=@system-service',
|
|
88
|
+
'LimitCORE=0',
|
|
89
|
+
];
|
|
90
|
+
/**
|
|
91
|
+
* The gateway's sandbox: the shared one, no view of EFS, the network it needs (`AF_NETLINK` for
|
|
92
|
+
* glibc's `getaddrinfo`), and the rest of the worker's additions.
|
|
93
|
+
*/
|
|
94
|
+
export const GATEWAY_SANDBOX = [
|
|
95
|
+
...WORKER_SANDBOX_DIRECTIVES,
|
|
96
|
+
`InaccessiblePaths=${EFS_MOUNT_PATH}`,
|
|
97
|
+
'RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK',
|
|
98
|
+
'PrivateIPC=yes',
|
|
99
|
+
...HIDDEN_PATHS,
|
|
100
|
+
'SystemCallFilter=@system-service',
|
|
101
|
+
'LimitCORE=0',
|
|
102
|
+
];
|
|
103
|
+
/** The worker's log; its directory and the file are root-owned. */
|
|
104
|
+
export const WORKER_LOG_DIR = '/var/log/canopy-worker';
|
|
105
|
+
export const WORKER_LOG = `${WORKER_LOG_DIR}/worker.log`;
|
|
106
|
+
/** The gateway's log; its directory and the file are root-owned. */
|
|
107
|
+
export const GATEWAY_LOG_DIR = '/var/log/canopy-github';
|
|
108
|
+
export const GATEWAY_LOG = `${GATEWAY_LOG_DIR}/gateway.log`;
|
|
109
|
+
const LOG_RULE = [
|
|
110
|
+
'# File output, not journal: the CloudWatch agent cannot read journald,',
|
|
111
|
+
'# so it tails this file instead.',
|
|
112
|
+
'# systemd opens append: targets as root, following symlinks, before it',
|
|
113
|
+
'# applies the sandbox or drops to User=. So the service must own neither',
|
|
114
|
+
'# the file nor its directory: one that could swap the file for a symlink',
|
|
115
|
+
'# would have its output appended to any file on the host as root.',
|
|
116
|
+
'# Hence no LogsDirectory=, which chowns the directory to User=. Both are',
|
|
117
|
+
'# created root-owned before the first start (user-data does), since',
|
|
118
|
+
'# without the directory the start fails with 209/STDOUT. The service',
|
|
119
|
+
'# writes through the descriptor systemd hands it.',
|
|
120
|
+
];
|
|
121
|
+
/**
|
|
122
|
+
* `canopy-worker.service`. Split: no network, no secret, the gateway's socket. Single-process:
|
|
123
|
+
* the unit as it was before the split, holding the secrets, for a bundle older than the split.
|
|
124
|
+
*/
|
|
125
|
+
export function workerUnit(layout) {
|
|
126
|
+
const split = layout === 'split';
|
|
127
|
+
return [
|
|
128
|
+
'[Unit]',
|
|
129
|
+
'Description=CanopyCMS Worker Daemon',
|
|
130
|
+
'After=network.target',
|
|
131
|
+
...(split
|
|
132
|
+
? [
|
|
133
|
+
'# Stopped before the gateway, so its drain runs while the gateway is up.',
|
|
134
|
+
'After=canopy-github.socket canopy-github.service',
|
|
135
|
+
'Requires=canopy-github.socket',
|
|
136
|
+
'Wants=canopy-github.service',
|
|
137
|
+
]
|
|
138
|
+
: []),
|
|
139
|
+
'# Never run against an unmounted /mnt/efs (user-data adds the fstab entry).',
|
|
140
|
+
`RequiresMountsFor=${EFS_MOUNT_PATH}`,
|
|
141
|
+
'',
|
|
142
|
+
'[Service]',
|
|
143
|
+
'Type=simple',
|
|
144
|
+
`User=${WORKER_USER}`,
|
|
145
|
+
`WorkingDirectory=${WORKER_INSTALL_DIR}`,
|
|
146
|
+
'# Namespaced binary, not bare `node`: AL2023 points /usr/bin/node at',
|
|
147
|
+
'# an installed version through `alternatives`, and AWS documents that',
|
|
148
|
+
'# selection as able to change at any time. node-22 always means 22.',
|
|
149
|
+
'ExecStart=/usr/bin/node-22 index.js',
|
|
150
|
+
'Restart=always',
|
|
151
|
+
'RestartSec=5',
|
|
152
|
+
'TimeoutStartSec=300',
|
|
153
|
+
'# Stopping drains: SIGTERM reaches node ONLY (mixed), so the git',
|
|
154
|
+
'# children it waits for keep running. Whatever is left when node',
|
|
155
|
+
'# exits, or at TimeoutStopSec, is SIGKILLed; 120s outlasts the 90s',
|
|
156
|
+
'# drain and its abort grace.',
|
|
157
|
+
'KillMode=mixed',
|
|
158
|
+
'TimeoutStopSec=120',
|
|
159
|
+
'# A worker that drained for an instance termination exits with this;',
|
|
160
|
+
'# restarting it would start work on an instance about to disappear.',
|
|
161
|
+
`RestartPreventExitStatus=${EXIT_DRAINED_FOR_TERMINATION}`,
|
|
162
|
+
`SuccessExitStatus=${EXIT_DRAINED_FOR_TERMINATION}`,
|
|
163
|
+
...LOG_RULE,
|
|
164
|
+
`StandardOutput=append:${WORKER_LOG}`,
|
|
165
|
+
`StandardError=append:${WORKER_LOG}`,
|
|
166
|
+
...(split
|
|
167
|
+
? ['# The worker requires it; it holds nothing secret.']
|
|
168
|
+
: [
|
|
169
|
+
'# The private GitHub mirror, /var/lib/canopy-worker on the root volume:',
|
|
170
|
+
'# the only git repository the GitHub credential is used in. The worker',
|
|
171
|
+
'# does not start without it.',
|
|
172
|
+
]),
|
|
173
|
+
'StateDirectory=canopy-worker',
|
|
174
|
+
`EnvironmentFile=${WORKER_INSTALL_DIR}/.env`,
|
|
175
|
+
...(split ? [] : [`EnvironmentFile=${WORKER_INSTALL_DIR}/gateway.env`]),
|
|
176
|
+
'# What this unit provides; a bundle needing more refuses to start.',
|
|
177
|
+
`Environment=${WORKER_CONTRACT_ENV}=${split ? WORKER_CONTRACT_VERSION : SINGLE_PROCESS_UNIT_CONTRACT}`,
|
|
178
|
+
...(split
|
|
179
|
+
? [
|
|
180
|
+
'# The GitHub gateway, its only way to GitHub, the Clerk API and the',
|
|
181
|
+
'# instance role.',
|
|
182
|
+
`Environment=${GITHUB_GATEWAY_SOCKET_ENV}=${GITHUB_GATEWAY_SOCKET_PATH}`,
|
|
183
|
+
]
|
|
184
|
+
: []),
|
|
185
|
+
'# Sandbox. Writable: the workspace, the state dir (StateDirectory=) and a',
|
|
186
|
+
'# private /tmp.',
|
|
187
|
+
'# ProtectHome=tmpfs, not yes: every git call reads per-user files under',
|
|
188
|
+
'# $HOME (~/.config/git/attributes, ignore), and an inaccessible /home',
|
|
189
|
+
'# makes each read warn "Permission denied"; an empty one is a silent',
|
|
190
|
+
'# ENOENT. It also hides /run/user, so the worker cannot reach a user',
|
|
191
|
+
'# manager of an operator logged in as its user.',
|
|
192
|
+
...(split
|
|
193
|
+
? [
|
|
194
|
+
'# Then the cut: no network but lo, no route to the instance metadata',
|
|
195
|
+
'# service, only unix sockets, the journal and /dev/shm hidden, and no',
|
|
196
|
+
'# core dumps. The worker checks at boot that the network cut took',
|
|
197
|
+
'# effect (only lo, no route to the metadata service), and ProtectProc',
|
|
198
|
+
'# and ProtectHome.',
|
|
199
|
+
...WORKER_SPLIT_SANDBOX,
|
|
200
|
+
]
|
|
201
|
+
: [...WORKER_SANDBOX_DIRECTIVES, `ReadWritePaths=${EFS_MOUNT_PATH}`]),
|
|
202
|
+
'',
|
|
203
|
+
'[Install]',
|
|
204
|
+
'WantedBy=multi-user.target',
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
/** `canopy-github.service`: every secret, the network, no view of EFS. */
|
|
208
|
+
export function gatewayUnit() {
|
|
209
|
+
return [
|
|
210
|
+
'[Unit]',
|
|
211
|
+
'Description=CanopyCMS GitHub gateway',
|
|
212
|
+
'After=network.target canopy-github.socket',
|
|
213
|
+
'Requires=canopy-github.socket',
|
|
214
|
+
'# Mounted before this unit sets up its namespace, so the mount is what',
|
|
215
|
+
'# InaccessiblePaths= hides.',
|
|
216
|
+
`RequiresMountsFor=${EFS_MOUNT_PATH}`,
|
|
217
|
+
'# A boot or prepare() that fails holds its cause for the worker for 60s,',
|
|
218
|
+
'# then exits 1. Restarting it is how a transient cause recovers, so',
|
|
219
|
+
'# systemd never gives up on it. systemd 252 has no growing RestartSec',
|
|
220
|
+
'# (no RestartSteps=); the hold paces it to one attempt every 72s or so.',
|
|
221
|
+
'StartLimitIntervalSec=0',
|
|
222
|
+
'',
|
|
223
|
+
'[Service]',
|
|
224
|
+
'Type=simple',
|
|
225
|
+
`User=${GATEWAY_USER}`,
|
|
226
|
+
`Group=${GATEWAY_USER}`,
|
|
227
|
+
`WorkingDirectory=${WORKER_INSTALL_DIR}`,
|
|
228
|
+
'ExecStart=/usr/bin/node-22 index.js gateway',
|
|
229
|
+
'# Descriptor 3, owned by the worker user, so only it and root connect.',
|
|
230
|
+
'Sockets=canopy-github.socket',
|
|
231
|
+
'Restart=always',
|
|
232
|
+
'RestartSec=5',
|
|
233
|
+
'# Stopping drains: answers new requests "stopping", lets those under way',
|
|
234
|
+
'# finish for up to 60s, then exits. SIGTERM reaches node only (mixed);',
|
|
235
|
+
'# a running fetch job is SIGKILLed with the cgroup when node exits.',
|
|
236
|
+
'KillMode=mixed',
|
|
237
|
+
'TimeoutStopSec=150',
|
|
238
|
+
...LOG_RULE,
|
|
239
|
+
`StandardOutput=append:${GATEWAY_LOG}`,
|
|
240
|
+
`StandardError=append:${GATEWAY_LOG}`,
|
|
241
|
+
'# The private GitHub mirror and the per-command credential files. 0700:',
|
|
242
|
+
"# the default 0755 would let the worker's user read both.",
|
|
243
|
+
'StateDirectory=canopy-github',
|
|
244
|
+
'StateDirectoryMode=0700',
|
|
245
|
+
'# 0640 root:canopy-github: the secrets, unreadable to the worker.',
|
|
246
|
+
`EnvironmentFile=${WORKER_INSTALL_DIR}/gateway.env`,
|
|
247
|
+
'# What this unit provides; a bundle needing more refuses to start.',
|
|
248
|
+
`Environment=${WORKER_CONTRACT_ENV}=${WORKER_CONTRACT_VERSION}`,
|
|
249
|
+
'# Sandbox. Writable: the state dir and a private /tmp. No view of EFS,',
|
|
250
|
+
'# which the gateway checks at boot, as it checks that cores are not',
|
|
251
|
+
'# piped to a helper and that its core limit is zero.',
|
|
252
|
+
...GATEWAY_SANDBOX,
|
|
253
|
+
'',
|
|
254
|
+
'[Install]',
|
|
255
|
+
'WantedBy=multi-user.target',
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
/** `canopy-github.socket`: the worker's only way to the gateway. */
|
|
259
|
+
export function gatewaySocketUnit() {
|
|
260
|
+
return [
|
|
261
|
+
'[Unit]',
|
|
262
|
+
'Description=CanopyCMS GitHub gateway socket',
|
|
263
|
+
'',
|
|
264
|
+
'[Socket]',
|
|
265
|
+
`ListenStream=${GITHUB_GATEWAY_SOCKET_PATH}`,
|
|
266
|
+
'# Only the worker user (and root) can connect. The gateway refuses to',
|
|
267
|
+
"# run as this socket's owner, or with group or other access to it.",
|
|
268
|
+
`SocketUser=${WORKER_USER}`,
|
|
269
|
+
`SocketGroup=${WORKER_USER}`,
|
|
270
|
+
'SocketMode=0600',
|
|
271
|
+
'# Root-owned: systemd chowns the socket by name, following symlinks,',
|
|
272
|
+
'# which is safe only in a directory nobody else can write.',
|
|
273
|
+
'DirectoryMode=0755',
|
|
274
|
+
'',
|
|
275
|
+
'[Install]',
|
|
276
|
+
'WantedBy=sockets.target',
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
/** The `canopy-inspect` scripts' paths. */
|
|
280
|
+
export const CANOPY_INSPECT = `${WORKER_INSTALL_DIR}/canopy-inspect`;
|
|
281
|
+
export const INSPECT_CHECKS = `${WORKER_INSTALL_DIR}/inspect-checks`;
|
|
282
|
+
export const INSPECT_RC = `${WORKER_INSTALL_DIR}/inspect.rc`;
|
|
283
|
+
/** A shell word for `value`: single-quoted, so the shell expands nothing in it. */
|
|
284
|
+
function shellWord(value) {
|
|
285
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* `canopy-inspect`: run one command (or, with `--interactive`, a shell) as the worker's user,
|
|
289
|
+
* inside exactly the worker's sandbox, so a hostile repository on EFS reaches only what the
|
|
290
|
+
* worker reaches. `--interactive` relays the sandbox's terminal raw, and the worker can reach its
|
|
291
|
+
* pty (safeinsights/canopycms#604). Run as root. Nothing after `--` is composed by a shell
|
|
292
|
+
* outside the sandbox: inspect-checks runs its assertions, then execs the command.
|
|
293
|
+
*/
|
|
294
|
+
export function canopyInspectScript() {
|
|
295
|
+
return [
|
|
296
|
+
'#!/bin/bash',
|
|
297
|
+
'# canopy-inspect: run a command on EFS as the worker does, never as root.',
|
|
298
|
+
'# canopy-inspect <command> [args...]',
|
|
299
|
+
'# canopy-inspect --interactive',
|
|
300
|
+
"# Run as root. Each command runs as uid 1000 inside the worker unit's",
|
|
301
|
+
'# sandbox (no network, no secret), after inspect-checks has proved it.',
|
|
302
|
+
'set -euo pipefail',
|
|
303
|
+
'props=(',
|
|
304
|
+
...WORKER_SPLIT_SANDBOX.map((directive) => ` -p ${shellWord(directive)}`),
|
|
305
|
+
')',
|
|
306
|
+
'run=(/usr/bin/systemd-run --quiet --wait --collect',
|
|
307
|
+
` --uid=${WORKER_USER} --gid=${WORKER_USER} "\${props[@]}")`,
|
|
308
|
+
'usage() {',
|
|
309
|
+
' echo "usage: canopy-inspect <command> [args...] | canopy-inspect --interactive" >&2',
|
|
310
|
+
' exit 2',
|
|
311
|
+
'}',
|
|
312
|
+
'[ "$#" -ge 1 ] || usage',
|
|
313
|
+
'if [ "$1" = --interactive ]; then',
|
|
314
|
+
' [ "$#" -eq 1 ] || usage',
|
|
315
|
+
" echo 'canopy-inspect: this terminal relays escape sequences both ways;' >&2",
|
|
316
|
+
" echo 'paste nothing from it into a root shell.' >&2",
|
|
317
|
+
` exec "\${run[@]}" --pty -- ${INSPECT_CHECKS} /bin/bash --noprofile --rcfile ${INSPECT_RC} -i`,
|
|
318
|
+
'fi',
|
|
319
|
+
'# stdin from /dev/null, stderr into stdout, and both through cat -v: the',
|
|
320
|
+
"# sandbox's output never reaches the terminal raw, and it reads nothing",
|
|
321
|
+
"# from the operator's.",
|
|
322
|
+
`"\${run[@]}" --pipe -- ${INSPECT_CHECKS} "$@" </dev/null 2>&1 | cat -v`,
|
|
323
|
+
];
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* `inspect-checks`: inside the sandbox, prove it (the worker's own boot self-checks, then what a
|
|
327
|
+
* shell can see), then exec the operator's command in the same process.
|
|
328
|
+
*/
|
|
329
|
+
export function inspectChecksScript() {
|
|
330
|
+
return [
|
|
331
|
+
'#!/bin/bash',
|
|
332
|
+
"# inspect-checks: canopy-inspect's sandbox proves itself, then runs the",
|
|
333
|
+
'# command. Any failure refuses, and the command never runs.',
|
|
334
|
+
'set -euo pipefail',
|
|
335
|
+
'refuse() {',
|
|
336
|
+
' echo "canopy-inspect: refusing to run: $*" >&2',
|
|
337
|
+
' exit 1',
|
|
338
|
+
'}',
|
|
339
|
+
`/usr/bin/node-22 ${WORKER_INSTALL_DIR}/index.js self-check ||`,
|
|
340
|
+
' refuse "the worker\'s self-checks failed"',
|
|
341
|
+
'[ "$(id -u)" = 1000 ] || refuse "not running as uid 1000"',
|
|
342
|
+
"grep -Eq '^NoNewPrivs:[[:space:]]+1$' /proc/self/status ||",
|
|
343
|
+
' refuse "NoNewPrivileges is not in effect"',
|
|
344
|
+
'grep -Eq \'^Seccomp:[[:space:]]+2$\' /proc/self/status || refuse "no seccomp filter"',
|
|
345
|
+
"# ProtectHome=tmpfs: /run/user is a tmpfs of the sandbox's own, empty, so no",
|
|
346
|
+
"# operator's user manager is reachable. (systemd 252 mounts it writable.)",
|
|
347
|
+
'awk \'$5 == "/run/user" { for (i = 7; i < NF; i++) if ($i == "-") t = $(i + 1) }',
|
|
348
|
+
' END { exit t != "tmpfs" }\' /proc/self/mountinfo && [ -z "$(ls -A /run/user)" ] ||',
|
|
349
|
+
' refuse "/run/user is not an empty tmpfs of its own"',
|
|
350
|
+
'for hidden in /dev/shm /var/log/journal /run/log/journal; do',
|
|
351
|
+
' if ls "$hidden" >/dev/null 2>&1; then refuse "$hidden is accessible"; fi',
|
|
352
|
+
'done',
|
|
353
|
+
'if sudo -n true >/dev/null 2>&1; then refuse "sudo works"; fi',
|
|
354
|
+
'exec -- "$@"',
|
|
355
|
+
];
|
|
356
|
+
}
|
|
357
|
+
/** The interactive shell's rc file: a prompt that says where it is. */
|
|
358
|
+
export function inspectRc() {
|
|
359
|
+
return [
|
|
360
|
+
"# canopy-inspect --interactive's shell. Root-owned, like everything here.",
|
|
361
|
+
"PS1='canopy-inspect:\\w\\$ '",
|
|
362
|
+
`cd ${EFS_MOUNT_PATH}`,
|
|
363
|
+
];
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Where user-data unpacks the instance files (root-only), before installing those its layout
|
|
367
|
+
* needs.
|
|
368
|
+
*/
|
|
369
|
+
export const INSTANCE_FILES_DIR = '/run/canopy-instance-files';
|
|
370
|
+
/** The instance files, by their name in {@link INSTANCE_FILES_DIR}. */
|
|
371
|
+
export function instanceFiles() {
|
|
372
|
+
return {
|
|
373
|
+
'canopy-worker.service': workerUnit('split'),
|
|
374
|
+
'canopy-worker-single-process.service': workerUnit('single-process'),
|
|
375
|
+
'canopy-github.service': gatewayUnit(),
|
|
376
|
+
'canopy-github.socket': gatewaySocketUnit(),
|
|
377
|
+
'canopy-inspect': canopyInspectScript(),
|
|
378
|
+
'inspect-checks': inspectChecksScript(),
|
|
379
|
+
'inspect.rc': inspectRc(),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const INSTANCE_FILE_DELIMITER = 'CANOPYFILEEOF';
|
|
383
|
+
/**
|
|
384
|
+
* A script that writes {@link instanceFiles} into its working directory. `build:worker` writes it
|
|
385
|
+
* beside the bundle as `worker/dist/instance-files.sh`, and the construct ships it as an asset
|
|
386
|
+
* checked against its sha256, as the bundle is: EC2 caps user data at 16 KiB, which the files
|
|
387
|
+
* alone would nearly fill.
|
|
388
|
+
*/
|
|
389
|
+
export function instanceFilesScript() {
|
|
390
|
+
return `set -eu\n${Object.entries(instanceFiles())
|
|
391
|
+
.flatMap(([name, lines]) => {
|
|
392
|
+
if (lines.includes(INSTANCE_FILE_DELIMITER)) {
|
|
393
|
+
throw new Error(`${name} holds the line ${INSTANCE_FILE_DELIMITER}`);
|
|
394
|
+
}
|
|
395
|
+
return [`cat > ${name} << '${INSTANCE_FILE_DELIMITER}'`, ...lines, INSTANCE_FILE_DELIMITER];
|
|
396
|
+
})
|
|
397
|
+
.join('\n')}\n`;
|
|
398
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { CmsWorker } from './worker.js';
|
|
2
2
|
export type { CmsWorkerConfig } from './worker.js';
|
|
3
3
|
export { CanopyCmsService } from './constructs/cms-service.js';
|
|
4
|
-
export type { CanopyCmsServiceProps } from './constructs/cms-service.js';
|
|
4
|
+
export type { CanopyCmsServiceProps, WorkerCapacity } from './constructs/cms-service.js';
|
|
5
|
+
export type { WorkerCode } from './constructs/worker-bundle.js';
|
|
5
6
|
export type { CanopyCmsAttachOptions } from './constructs/editor-routing.js';
|
|
6
7
|
export { CanopyCmsDistribution } from './constructs/cms-distribution.js';
|
|
7
8
|
export type { CanopyCmsDistributionProps } from './constructs/cms-distribution.js';
|