easy-devops 1.2.0 → 1.3.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 +97 -6
- package/dist/ui/assets/index-B6zdwEnG.css +1 -0
- package/dist/ui/assets/index-Cy0oNmul.js +442 -0
- package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
- package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
- package/dist/ui/brand/apple-touch-icon.png +0 -0
- package/dist/ui/brand/favicon-16.png +0 -0
- package/dist/ui/brand/favicon-32.png +0 -0
- package/dist/ui/brand/favicon-light-16.png +0 -0
- package/dist/ui/brand/favicon-light-32.png +0 -0
- package/dist/ui/brand/favicon.ico +0 -0
- package/dist/ui/brand/icon-192.png +0 -0
- package/dist/ui/brand/icon-48.png +0 -0
- package/dist/ui/brand/icon-512.png +0 -0
- package/dist/ui/brand/icon-64-light.png +0 -0
- package/dist/ui/brand/icon-64.png +0 -0
- package/dist/ui/brand/logo-dark.png +0 -0
- package/dist/ui/brand/logo-light.png +0 -0
- package/dist/ui/brand/mark-dark.png +0 -0
- package/dist/ui/brand/mark-light.png +0 -0
- package/dist/ui/index.html +71 -0
- package/install.ps1 +188 -1
- package/install.sh +254 -29
- package/package.json +17 -5
- package/src/cli/dash.js +110 -0
- package/src/cli/index.js +237 -106
- package/src/cli/managers/domain-manager.js +60 -40
- package/src/cli/managers/ssl-manager.js +47 -330
- package/src/cli/menu.js +156 -0
- package/src/cli/menus/cleanup.js +205 -0
- package/src/cli/menus/dashboard.js +93 -2
- package/src/cli/menus/runtime.js +236 -0
- package/src/cli/menus/service.js +764 -0
- package/src/cli/menus/settings.js +176 -8
- package/src/cli/menus/uninstall.js +167 -0
- package/src/cli/menus/update.js +221 -65
- package/src/core/acme-challenge.js +273 -0
- package/src/core/cert-operations.js +189 -0
- package/src/{dashboard/lib → core}/cert-reader.js +2 -2
- package/src/core/cert-renewal.js +494 -0
- package/src/core/channels/discord.js +77 -0
- package/src/core/channels/telegram.js +120 -0
- package/src/core/cleanup.js +600 -0
- package/src/core/config.js +169 -2
- package/src/core/daemon/ipc.js +292 -0
- package/src/core/daemon/lock.js +294 -0
- package/src/core/daemon/run.js +241 -0
- package/src/core/daemon/stopRequest.js +162 -0
- package/src/core/db.js +44 -8
- package/src/core/deploy/apps.js +491 -0
- package/src/core/deploy/domainLink.js +200 -0
- package/src/core/deploy/engine.js +973 -0
- package/src/core/deploy/ezfile.js +361 -0
- package/src/core/deploy/github.js +239 -0
- package/src/core/deploy/githubApi.js +261 -0
- package/src/core/deploy/hookServer.js +325 -0
- package/src/core/deploy/hubCommands.js +267 -0
- package/src/core/deploy/launcher.js +57 -0
- package/src/core/deploy/status.js +91 -0
- package/src/core/deploy/store.js +398 -0
- package/src/core/domainNotifier.js +364 -0
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +222 -0
- package/src/core/hub/agent.js +842 -0
- package/src/core/hubLink.js +306 -0
- package/src/core/hubPermissions.js +430 -0
- package/src/core/installSource.js +162 -0
- package/src/core/jobs/bus.js +120 -0
- package/src/core/jobs/index.js +371 -0
- package/src/core/jobs/monitors.js +368 -0
- package/src/core/jobs/owner.js +217 -0
- package/src/core/jobs/revisionWatcher.js +106 -0
- package/src/core/nginx-apply.js +213 -0
- package/src/core/nginx-conf-generator.js +253 -17
- package/src/{dashboard/lib → core}/nginx-service.js +18 -9
- package/src/core/notifications/claim.js +214 -0
- package/src/core/notifications/dispatch.js +154 -0
- package/src/core/notifier.js +103 -0
- package/src/core/passwords.js +172 -0
- package/src/core/permissions.js +8 -4
- package/src/core/platform.js +709 -2
- package/src/core/probeCache.js +258 -0
- package/src/core/purge.js +372 -0
- package/src/core/runner.js +488 -0
- package/src/core/service/acl.js +226 -0
- package/src/core/service/dataDir.js +345 -0
- package/src/core/service/index.js +1128 -0
- package/src/core/service/legacy.js +218 -0
- package/src/core/service/runtime.js +446 -0
- package/src/core/service/runtimeUpdate.js +800 -0
- package/src/core/service/systemd.js +458 -0
- package/src/core/service/windowsPersonal.js +530 -0
- package/src/core/service/windowsService.js +576 -0
- package/src/core/service/winsw.js +260 -0
- package/src/core/setupCode.js +331 -0
- package/src/core/shell.js +128 -13
- package/src/core/ssl-issuer.js +627 -0
- package/src/core/updater.js +295 -0
- package/src/core/validators.js +99 -0
- package/src/dashboard/controllers/authController.js +316 -0
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +403 -0
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +146 -0
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +503 -0
- package/src/dashboard/controllers/sslController.js +452 -0
- package/src/dashboard/lib/daemonBridge.js +163 -0
- package/src/dashboard/lib/localRequest.js +235 -0
- package/src/dashboard/lib/loginRateLimit.js +119 -0
- package/src/dashboard/lib/requireAuth.js +80 -0
- package/src/dashboard/public/img/icon.png +0 -0
- package/src/dashboard/public/img/icon_b.png +0 -0
- package/src/dashboard/public/js/app.js +595 -19
- package/src/dashboard/routes/auth.js +14 -33
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +20 -363
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +22 -146
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +25 -232
- package/src/dashboard/routes/ssl.js +18 -276
- package/src/dashboard/server.js +279 -165
- package/src/dashboard/socketManager.js +246 -0
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/domain-form.ejs +64 -1
- package/src/dashboard/views/partials/login.ejs +69 -8
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +549 -118
- package/src/dashboard/views/partials/sidebar.ejs +3 -1
- package/src/dashboard/lib/domains-db.js +0 -51
- /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/githubApi.js
|
|
3
|
+
*
|
|
4
|
+
* Does the GitHub side of the setup for the user: stores the two repository
|
|
5
|
+
* secrets, commits the workflow file, and adds the deploy key. The manual
|
|
6
|
+
* steps in the dashboard remain; this is the same result without the copying.
|
|
7
|
+
*
|
|
8
|
+
* ── The two dependencies ─────────────────────────────────────────────────────
|
|
9
|
+
*
|
|
10
|
+
* GitHub only accepts a secret encrypted to the repository's public key with
|
|
11
|
+
* a libsodium "sealed box": X25519 + XSalsa20-Poly1305, with a nonce derived
|
|
12
|
+
* by BLAKE2b. Node's crypto has neither XSalsa20 nor a BLAKE2b with a 24-byte
|
|
13
|
+
* digest, so this is the one place the package depends on `tweetnacl` and
|
|
14
|
+
* `blakejs` (an explicit decision, CLAUDE.md rule 7). Both are small, pure
|
|
15
|
+
* JavaScript and have no dependencies of their own.
|
|
16
|
+
*
|
|
17
|
+
* ── The token ────────────────────────────────────────────────────────────────
|
|
18
|
+
*
|
|
19
|
+
* The GitHub token used here needs more than reading the repository: writing
|
|
20
|
+
* Actions secrets, writing contents (and workflows), and — for a deploy key —
|
|
21
|
+
* repository administration. It is used for these calls and, when it was typed
|
|
22
|
+
* in for the occasion, is not stored.
|
|
23
|
+
*
|
|
24
|
+
* `fetch` is injectable, so every request and every error path is tested
|
|
25
|
+
* without touching the network.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { createRequire } from 'module';
|
|
29
|
+
|
|
30
|
+
const require = createRequire(import.meta.url);
|
|
31
|
+
const nacl = require('tweetnacl');
|
|
32
|
+
const { blake2b } = require('blakejs');
|
|
33
|
+
|
|
34
|
+
const API = 'https://api.github.com';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Encrypts a secret for GitHub: a libsodium sealed box.
|
|
38
|
+
*
|
|
39
|
+
* ephemeral key pair (epk, esk)
|
|
40
|
+
* nonce = BLAKE2b-24(epk ‖ recipient public key)
|
|
41
|
+
* box = crypto_box(message, nonce, recipient public key, esk)
|
|
42
|
+
* out = epk ‖ box
|
|
43
|
+
*
|
|
44
|
+
* @param {string} value The secret
|
|
45
|
+
* @param {string} publicKeyBase64 The repository's public key, as GitHub returns it
|
|
46
|
+
* @returns {string} Base64 ciphertext for `encrypted_value`
|
|
47
|
+
*/
|
|
48
|
+
export function sealSecret(value, publicKeyBase64) {
|
|
49
|
+
const recipient = new Uint8Array(Buffer.from(publicKeyBase64, 'base64'));
|
|
50
|
+
if (recipient.length !== nacl.box.publicKeyLength) {
|
|
51
|
+
throw new Error('The repository public key has an unexpected length.');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const ephemeral = nacl.box.keyPair();
|
|
55
|
+
const nonceInput = new Uint8Array(ephemeral.publicKey.length + recipient.length);
|
|
56
|
+
nonceInput.set(ephemeral.publicKey, 0);
|
|
57
|
+
nonceInput.set(recipient, ephemeral.publicKey.length);
|
|
58
|
+
const nonce = blake2b(nonceInput, undefined, nacl.box.nonceLength);
|
|
59
|
+
|
|
60
|
+
const message = new Uint8Array(Buffer.from(String(value), 'utf8'));
|
|
61
|
+
const box = nacl.box(message, nonce, recipient, ephemeral.secretKey);
|
|
62
|
+
|
|
63
|
+
const out = new Uint8Array(ephemeral.publicKey.length + box.length);
|
|
64
|
+
out.set(ephemeral.publicKey, 0);
|
|
65
|
+
out.set(box, ephemeral.publicKey.length);
|
|
66
|
+
return Buffer.from(out).toString('base64');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Opens a sealed box. Not used in production — GitHub does the opening — but
|
|
71
|
+
* it is what lets the test prove sealSecret() produces something a holder of
|
|
72
|
+
* the private key can read.
|
|
73
|
+
*
|
|
74
|
+
* @returns {string|null} The plaintext, or null when it does not open
|
|
75
|
+
*/
|
|
76
|
+
export function openSealedSecret(sealedBase64, publicKey, secretKey) {
|
|
77
|
+
const sealed = new Uint8Array(Buffer.from(sealedBase64, 'base64'));
|
|
78
|
+
const epk = sealed.subarray(0, nacl.box.publicKeyLength);
|
|
79
|
+
const box = sealed.subarray(nacl.box.publicKeyLength);
|
|
80
|
+
|
|
81
|
+
const nonceInput = new Uint8Array(epk.length + publicKey.length);
|
|
82
|
+
nonceInput.set(epk, 0);
|
|
83
|
+
nonceInput.set(publicKey, epk.length);
|
|
84
|
+
const nonce = blake2b(nonceInput, undefined, nacl.box.nonceLength);
|
|
85
|
+
|
|
86
|
+
const opened = nacl.box.open(box, nonce, epk, secretKey);
|
|
87
|
+
return opened ? Buffer.from(opened).toString('utf8') : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Owner and repository from a github.com URL, in any of the forms an app may
|
|
92
|
+
* be configured with.
|
|
93
|
+
*
|
|
94
|
+
* @returns {{ owner: string, repo: string }|null} null for any other host
|
|
95
|
+
*/
|
|
96
|
+
export function parseGithubRepo(repoUrl) {
|
|
97
|
+
const url = String(repoUrl ?? '');
|
|
98
|
+
const m =
|
|
99
|
+
/^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?\/?$/.exec(url) ||
|
|
100
|
+
/^git@github\.com:([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?$/.exec(url) ||
|
|
101
|
+
/^ssh:\/\/git@github\.com(?::22)?\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?$/.exec(url);
|
|
102
|
+
return m ? { owner: m[1], repo: m[2] } : null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A GitHub token, checked before it is put in a header. */
|
|
106
|
+
export function validateGithubToken(token) {
|
|
107
|
+
return typeof token === 'string' && /^[A-Za-z0-9_.-]{10,255}$/.test(token);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function makeClient(token, fetchImpl) {
|
|
111
|
+
return async (method, path, body) => {
|
|
112
|
+
let response;
|
|
113
|
+
try {
|
|
114
|
+
response = await fetchImpl(`${API}${path}`, {
|
|
115
|
+
method,
|
|
116
|
+
headers: {
|
|
117
|
+
Authorization: `Bearer ${token}`,
|
|
118
|
+
Accept: 'application/vnd.github+json',
|
|
119
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
120
|
+
'User-Agent': 'easy-devops',
|
|
121
|
+
...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),
|
|
122
|
+
},
|
|
123
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
124
|
+
});
|
|
125
|
+
} catch (err) {
|
|
126
|
+
return { ok: false, status: 0, json: null, message: `Could not reach GitHub: ${err.message}` };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let json = null;
|
|
130
|
+
try { json = await response.json(); } catch { /* 204, or not JSON */ }
|
|
131
|
+
return {
|
|
132
|
+
ok: response.status >= 200 && response.status < 300,
|
|
133
|
+
status: response.status,
|
|
134
|
+
json,
|
|
135
|
+
message: typeof json?.message === 'string' ? json.message : null,
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Why a call failed, in words that say what to change on the token. */
|
|
141
|
+
function explain(result, needs) {
|
|
142
|
+
if (result.status === 0) return result.message;
|
|
143
|
+
if (result.status === 401) return 'GitHub rejected the token. It may be mistyped, expired or revoked.';
|
|
144
|
+
if (result.status === 403 || result.status === 404) {
|
|
145
|
+
return `GitHub refused (${result.status}${result.message ? `: ${result.message}` : ''}). The token needs ${needs} on this repository.`;
|
|
146
|
+
}
|
|
147
|
+
if (result.status === 422) return `GitHub did not accept the request${result.message ? `: ${result.message}` : '.'}`;
|
|
148
|
+
return `GitHub answered ${result.status}${result.message ? `: ${result.message}` : ''}.`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Applies the setup to a repository.
|
|
153
|
+
*
|
|
154
|
+
* Each requested part is attempted independently and reported on its own, so
|
|
155
|
+
* a token that may write secrets but not workflows still gets the secrets in
|
|
156
|
+
* place, and the answer says exactly which manual step is left.
|
|
157
|
+
*
|
|
158
|
+
* @param {Object} opts
|
|
159
|
+
* @param {string} opts.repoUrl
|
|
160
|
+
* @param {string} opts.branch
|
|
161
|
+
* @param {string} opts.token GitHub token
|
|
162
|
+
* @param {Object<string, string>|null} [opts.secrets] name → value, or null to skip
|
|
163
|
+
* @param {{ path: string, content: string }|null} [opts.workflow]
|
|
164
|
+
* @param {{ title: string, key: string }|null} [opts.deployKey]
|
|
165
|
+
* @param {Function} [opts.fetchImpl]
|
|
166
|
+
* @returns {Promise<{ ok: boolean, repo: string|null, error: string|null,
|
|
167
|
+
* steps: Array<{ id: string, ok: boolean, detail: string }> }>}
|
|
168
|
+
*/
|
|
169
|
+
export async function applyGithubSetup({
|
|
170
|
+
repoUrl, branch, token, secrets = null, workflow = null, deployKey = null, fetchImpl = globalThis.fetch,
|
|
171
|
+
}) {
|
|
172
|
+
const repo = parseGithubRepo(repoUrl);
|
|
173
|
+
if (!repo) {
|
|
174
|
+
return { ok: false, repo: null, error: 'Automatic setup works with github.com repositories only.', steps: [] };
|
|
175
|
+
}
|
|
176
|
+
if (!validateGithubToken(token)) {
|
|
177
|
+
return { ok: false, repo: `${repo.owner}/${repo.repo}`, error: 'A GitHub token is required.', steps: [] };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const gh = makeClient(token, fetchImpl);
|
|
181
|
+
const base = `/repos/${repo.owner}/${repo.repo}`;
|
|
182
|
+
const full = `${repo.owner}/${repo.repo}`;
|
|
183
|
+
const steps = [];
|
|
184
|
+
|
|
185
|
+
// Can the token see the repository at all? One clear answer here beats
|
|
186
|
+
// three confusing ones below.
|
|
187
|
+
const seen = await gh('GET', base);
|
|
188
|
+
if (!seen.ok) {
|
|
189
|
+
return { ok: false, repo: full, error: explain(seen, 'access'), steps };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ── secrets ───────────────────────────────────────────────────────────────
|
|
193
|
+
if (secrets) {
|
|
194
|
+
const key = await gh('GET', `${base}/actions/secrets/public-key`);
|
|
195
|
+
if (!key.ok || !key.json?.key || !key.json?.key_id) {
|
|
196
|
+
steps.push({ id: 'secrets', ok: false, detail: explain(key, '“Secrets: read and write”') });
|
|
197
|
+
} else {
|
|
198
|
+
const failed = [];
|
|
199
|
+
for (const [name, value] of Object.entries(secrets)) {
|
|
200
|
+
let encrypted;
|
|
201
|
+
try {
|
|
202
|
+
encrypted = sealSecret(value, key.json.key);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
failed.push(`${name}: ${err.message}`);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const put = await gh('PUT', `${base}/actions/secrets/${name}`, {
|
|
208
|
+
encrypted_value: encrypted,
|
|
209
|
+
key_id: key.json.key_id,
|
|
210
|
+
});
|
|
211
|
+
if (!put.ok) failed.push(`${name}: ${explain(put, '“Secrets: read and write”')}`);
|
|
212
|
+
}
|
|
213
|
+
steps.push(failed.length === 0
|
|
214
|
+
? { id: 'secrets', ok: true, detail: `Stored ${Object.keys(secrets).join(' and ')}.` }
|
|
215
|
+
: { id: 'secrets', ok: false, detail: failed.join(' ') });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ── workflow file ─────────────────────────────────────────────────────────
|
|
220
|
+
if (workflow) {
|
|
221
|
+
const filePath = workflow.path.split('/').map(encodeURIComponent).join('/');
|
|
222
|
+
const existing = await gh('GET', `${base}/contents/${filePath}?ref=${encodeURIComponent(branch)}`);
|
|
223
|
+
const wanted = Buffer.from(workflow.content, 'utf8').toString('base64');
|
|
224
|
+
|
|
225
|
+
if (existing.ok && typeof existing.json?.content === 'string'
|
|
226
|
+
&& existing.json.content.replace(/\s/g, '') === wanted) {
|
|
227
|
+
steps.push({ id: 'workflow', ok: true, detail: `${workflow.path} is already up to date.` });
|
|
228
|
+
} else if (!existing.ok && existing.status !== 404) {
|
|
229
|
+
steps.push({ id: 'workflow', ok: false, detail: explain(existing, '“Contents: read and write”') });
|
|
230
|
+
} else {
|
|
231
|
+
const put = await gh('PUT', `${base}/contents/${filePath}`, {
|
|
232
|
+
message: existing.ok ? 'Update the Easy DevOps deploy workflow' : 'Add the Easy DevOps deploy workflow',
|
|
233
|
+
content: wanted,
|
|
234
|
+
branch,
|
|
235
|
+
...(existing.ok && existing.json?.sha ? { sha: existing.json.sha } : {}),
|
|
236
|
+
});
|
|
237
|
+
steps.push(put.ok
|
|
238
|
+
? { id: 'workflow', ok: true, detail: `Committed ${workflow.path} to ${branch}. That push starts the first deployment.` }
|
|
239
|
+
: { id: 'workflow', ok: false, detail: explain(put, '“Contents: read and write” and “Workflows: read and write”') });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ── deploy key ────────────────────────────────────────────────────────────
|
|
244
|
+
if (deployKey) {
|
|
245
|
+
// Compare the key material only: GitHub drops the trailing comment.
|
|
246
|
+
const material = deployKey.key.split(/\s+/).slice(0, 2).join(' ');
|
|
247
|
+
const list = await gh('GET', `${base}/keys?per_page=100`);
|
|
248
|
+
if (!list.ok) {
|
|
249
|
+
steps.push({ id: 'deployKey', ok: false, detail: explain(list, '“Administration: read and write”') });
|
|
250
|
+
} else if (Array.isArray(list.json) && list.json.some((k) => String(k.key ?? '').trim() === material)) {
|
|
251
|
+
steps.push({ id: 'deployKey', ok: true, detail: 'The deploy key is already on the repository.' });
|
|
252
|
+
} else {
|
|
253
|
+
const add = await gh('POST', `${base}/keys`, { title: deployKey.title, key: deployKey.key, read_only: true });
|
|
254
|
+
steps.push(add.ok
|
|
255
|
+
? { id: 'deployKey', ok: true, detail: 'Added the deploy key, read-only.' }
|
|
256
|
+
: { id: 'deployKey', ok: false, detail: explain(add, '“Administration: read and write”') });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { ok: steps.length > 0 && steps.every((s) => s.ok), repo: full, error: null, steps };
|
|
261
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/hookServer.js
|
|
3
|
+
*
|
|
4
|
+
* The deploy hook: a small HTTP listener that lets a CI job say "deploy this
|
|
5
|
+
* commit" — the trigger for a GitHub Action that has no Hub to go through.
|
|
6
|
+
*
|
|
7
|
+
* ── Why this is its own listener, and not a dashboard route ──────────────────
|
|
8
|
+
*
|
|
9
|
+
* The dashboard is started on demand; a deployment trigger has to be there at
|
|
10
|
+
* three in the morning. So it runs with the background jobs, in whichever
|
|
11
|
+
* process owns them (normally the daemon).
|
|
12
|
+
*
|
|
13
|
+
* That reverses a deliberate property of the daemon — it used to open no port
|
|
14
|
+
* at all — so it is held to a narrow shape:
|
|
15
|
+
*
|
|
16
|
+
* - OFF by default. Nothing listens until `deployHook.enabled` is set.
|
|
17
|
+
* - Loopback by default, reached through one nginx location on a domain the
|
|
18
|
+
* operator picks (HTTPS, if the domain has a certificate). Binding it to
|
|
19
|
+
* every interface is possible and is plain HTTP — the UI says so.
|
|
20
|
+
* - It serves exactly two things, both under /.ez-deploy/: start a
|
|
21
|
+
* deployment, and read that deployment's status. No dashboard API, no
|
|
22
|
+
* static files, nothing else.
|
|
23
|
+
* - Each app has its own token (256 random bits, stored as a hash). A token
|
|
24
|
+
* can start a deployment of ITS app from the app's configured repository,
|
|
25
|
+
* and nothing more: it cannot choose the repository, the commands or the
|
|
26
|
+
* environment.
|
|
27
|
+
* - Failed attempts are rate-limited per client, and an unknown app is
|
|
28
|
+
* indistinguishable from a wrong token.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import http from 'http';
|
|
32
|
+
|
|
33
|
+
import { loadConfig } from '../config.js';
|
|
34
|
+
import { DEPLOY_HOOK_PATH } from '../nginx-conf-generator.js';
|
|
35
|
+
import { getApp, verifyHookToken } from './apps.js';
|
|
36
|
+
import { startDeployment } from './engine.js';
|
|
37
|
+
import { readRun, isId } from './store.js';
|
|
38
|
+
|
|
39
|
+
const MAX_BODY_BYTES = 16 * 1024;
|
|
40
|
+
const FAILURE_WINDOW_MS = 15 * 60 * 1000;
|
|
41
|
+
const MAX_FAILURES = 10;
|
|
42
|
+
/** How often the manager re-reads the setting. */
|
|
43
|
+
const SETTINGS_POLL_MS = 10_000;
|
|
44
|
+
|
|
45
|
+
function isLoopback(address) {
|
|
46
|
+
const a = String(address ?? '');
|
|
47
|
+
return a === '127.0.0.1' || a === '::1' || a === '::ffff:127.0.0.1';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Who is calling.
|
|
52
|
+
*
|
|
53
|
+
* X-Forwarded-For is believed only when the connection itself comes from
|
|
54
|
+
* loopback — that is, from the nginx location, which overwrites the header
|
|
55
|
+
* with the real peer. From anywhere else the header is the caller's own
|
|
56
|
+
* claim and is ignored.
|
|
57
|
+
*/
|
|
58
|
+
function clientAddress(req) {
|
|
59
|
+
const peer = req.socket?.remoteAddress ?? '';
|
|
60
|
+
if (isLoopback(peer)) {
|
|
61
|
+
const forwarded = String(req.headers['x-forwarded-for'] ?? '').split(',')[0].trim();
|
|
62
|
+
if (forwarded) return forwarded;
|
|
63
|
+
}
|
|
64
|
+
return peer || 'unknown';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function send(res, status, body) {
|
|
68
|
+
const text = JSON.stringify(body);
|
|
69
|
+
res.writeHead(status, {
|
|
70
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
71
|
+
'Content-Length': Buffer.byteLength(text),
|
|
72
|
+
'Cache-Control': 'no-store',
|
|
73
|
+
});
|
|
74
|
+
res.end(text);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readBody(req) {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
let size = 0;
|
|
80
|
+
const chunks = [];
|
|
81
|
+
req.on('data', (chunk) => {
|
|
82
|
+
size += chunk.length;
|
|
83
|
+
if (size > MAX_BODY_BYTES) {
|
|
84
|
+
reject(Object.assign(new Error('Request body too large'), { status: 413 }));
|
|
85
|
+
req.destroy();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
chunks.push(chunk);
|
|
89
|
+
});
|
|
90
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
91
|
+
req.on('error', reject);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function bearer(req) {
|
|
96
|
+
const m = /^Bearer\s+(\S+)$/i.exec(String(req.headers.authorization ?? ''));
|
|
97
|
+
return m ? m[1] : null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Builds the request handler. Separate from the listener so it can be tested
|
|
102
|
+
* without binding a port policy to it.
|
|
103
|
+
*
|
|
104
|
+
* @param {Object} [opts]
|
|
105
|
+
* @param {Function} [opts.deploy] startDeployment, injectable for tests
|
|
106
|
+
* @param {() => number} [opts.now]
|
|
107
|
+
* @returns {(req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>}
|
|
108
|
+
*/
|
|
109
|
+
export function createHookHandler({ deploy = startDeployment, now = () => Date.now() } = {}) {
|
|
110
|
+
/** @type {Map<string, number[]>} client → timestamps of recent failures */
|
|
111
|
+
const failures = new Map();
|
|
112
|
+
|
|
113
|
+
const recentFailures = (client) => {
|
|
114
|
+
const cutoff = now() - FAILURE_WINDOW_MS;
|
|
115
|
+
const list = (failures.get(client) ?? []).filter((t) => t > cutoff);
|
|
116
|
+
if (list.length > 0) failures.set(client, list); else failures.delete(client);
|
|
117
|
+
return list;
|
|
118
|
+
};
|
|
119
|
+
const recordFailure = (client) => {
|
|
120
|
+
const list = recentFailures(client);
|
|
121
|
+
list.push(now());
|
|
122
|
+
failures.set(client, list);
|
|
123
|
+
// A flood of distinct clients must not grow this map without bound.
|
|
124
|
+
if (failures.size > 5000) failures.delete(failures.keys().next().value);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** Authenticates a request for an app. Sends the refusal itself. */
|
|
128
|
+
const authenticate = (req, res, appId) => {
|
|
129
|
+
const client = clientAddress(req);
|
|
130
|
+
|
|
131
|
+
if (recentFailures(client).length >= MAX_FAILURES) {
|
|
132
|
+
send(res, 429, { error: 'Too many failed attempts. Try again later.' });
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const app = /^[a-z0-9][a-z0-9-]{0,39}$/.test(appId) ? getApp(appId) : null;
|
|
137
|
+
// One answer for "no such app", "hook not set up for it" and "wrong
|
|
138
|
+
// token": the difference would tell a stranger which app names exist.
|
|
139
|
+
if (!app || !verifyHookToken(app, bearer(req) ?? '')) {
|
|
140
|
+
recordFailure(client);
|
|
141
|
+
send(res, 401, { error: 'Unauthorized' });
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
return app;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return async function handle(req, res) {
|
|
148
|
+
try {
|
|
149
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
150
|
+
const pathname = url.pathname;
|
|
151
|
+
|
|
152
|
+
if (!pathname.startsWith(DEPLOY_HOOK_PATH)) return send(res, 404, { error: 'Not found' });
|
|
153
|
+
const parts = pathname.slice(DEPLOY_HOOK_PATH.length).split('/').filter(Boolean);
|
|
154
|
+
|
|
155
|
+
// Liveness, for the dashboard's "is the hook reachable" check. Says
|
|
156
|
+
// nothing about apps.
|
|
157
|
+
if (req.method === 'GET' && parts.length === 1 && parts[0] === 'health') {
|
|
158
|
+
return send(res, 200, { ok: true });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// POST /.ez-deploy/<app>
|
|
162
|
+
if (req.method === 'POST' && parts.length === 1) {
|
|
163
|
+
const app = authenticate(req, res, parts[0]);
|
|
164
|
+
if (!app) return undefined;
|
|
165
|
+
|
|
166
|
+
let ref = null;
|
|
167
|
+
const raw = await readBody(req);
|
|
168
|
+
if (raw.trim()) {
|
|
169
|
+
let body;
|
|
170
|
+
try { body = JSON.parse(raw); } catch { return send(res, 400, { error: 'The body must be JSON.' }); }
|
|
171
|
+
if (body && typeof body.ref === 'string' && body.ref.trim()) ref = body.ref.trim();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const started = deploy(app.id, { trigger: 'webhook', ref, actor: 'deploy hook' });
|
|
175
|
+
if (!started.ok) {
|
|
176
|
+
return send(res, started.status ?? 500, { error: started.error, runId: started.runId ?? null });
|
|
177
|
+
}
|
|
178
|
+
return send(res, 202, {
|
|
179
|
+
runId: started.runId,
|
|
180
|
+
status: 'running',
|
|
181
|
+
statusPath: `${DEPLOY_HOOK_PATH}${app.id}/runs/${started.runId}`,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// GET /.ez-deploy/<app>/runs/<runId>
|
|
186
|
+
if (req.method === 'GET' && parts.length === 3 && parts[1] === 'runs') {
|
|
187
|
+
const app = authenticate(req, res, parts[0]);
|
|
188
|
+
if (!app) return undefined;
|
|
189
|
+
|
|
190
|
+
const run = isId(parts[2]) ? readRun(app.id, parts[2]) : null;
|
|
191
|
+
if (!run) return send(res, 404, { error: 'No such run.' });
|
|
192
|
+
return send(res, 200, {
|
|
193
|
+
runId: run.id,
|
|
194
|
+
status: run.status,
|
|
195
|
+
step: run.step,
|
|
196
|
+
sha: run.sha,
|
|
197
|
+
error: run.error,
|
|
198
|
+
rolledBackTo: run.rolledBackTo ?? null,
|
|
199
|
+
startedAt: run.startedAt,
|
|
200
|
+
finishedAt: run.finishedAt,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return send(res, 404, { error: 'Not found' });
|
|
205
|
+
} catch (err) {
|
|
206
|
+
if (!res.headersSent) send(res, err?.status ?? 500, { error: err?.status ? err.message : 'Internal error' });
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Starts the listener.
|
|
214
|
+
* @returns {Promise<{ port: number, bind: string, close: () => Promise<void> }>}
|
|
215
|
+
*/
|
|
216
|
+
export function startHookServer({ port, bind = '127.0.0.1', handler = createHookHandler() } = {}) {
|
|
217
|
+
return new Promise((resolve, reject) => {
|
|
218
|
+
const server = http.createServer((req, res) => { handler(req, res); });
|
|
219
|
+
// A CI job that stalls must not hold a socket open for ever.
|
|
220
|
+
server.requestTimeout = 30_000;
|
|
221
|
+
server.headersTimeout = 15_000;
|
|
222
|
+
|
|
223
|
+
server.once('error', reject);
|
|
224
|
+
server.listen(port, bind, () => {
|
|
225
|
+
server.removeListener('error', reject);
|
|
226
|
+
server.on('error', (err) => console.error('[deploy hook] server error:', err.message));
|
|
227
|
+
resolve({
|
|
228
|
+
port: server.address().port,
|
|
229
|
+
bind,
|
|
230
|
+
close: () => new Promise((done) => {
|
|
231
|
+
server.close(() => done());
|
|
232
|
+
// Idle keep-alive sockets would otherwise keep close() waiting.
|
|
233
|
+
if (typeof server.closeAllConnections === 'function') server.closeAllConnections();
|
|
234
|
+
}),
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The hook settings in force, with anything unusable replaced by a safe value. */
|
|
241
|
+
export function hookSettings(config = loadConfig()) {
|
|
242
|
+
const raw = config?.deployHook ?? {};
|
|
243
|
+
const port = Number(raw.port);
|
|
244
|
+
return {
|
|
245
|
+
enabled: raw.enabled === true,
|
|
246
|
+
port: Number.isInteger(port) && port > 0 && port < 65536 ? port : 6445,
|
|
247
|
+
bind: raw.bind === '0.0.0.0' ? '0.0.0.0' : '127.0.0.1',
|
|
248
|
+
domain: typeof raw.domain === 'string' && raw.domain ? raw.domain : null,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Keeps the listener in step with the setting.
|
|
254
|
+
*
|
|
255
|
+
* The setting is changed from the dashboard, which is usually a different
|
|
256
|
+
* process from the one running this. Re-reading it on a timer is the same
|
|
257
|
+
* mechanism the revision watcher uses, and it means turning the hook on or
|
|
258
|
+
* off never needs a service restart.
|
|
259
|
+
*
|
|
260
|
+
* @param {Object} [opts]
|
|
261
|
+
* @param {number} [opts.pollMs]
|
|
262
|
+
* @param {(line: string) => void} [opts.log]
|
|
263
|
+
* @returns {{ stop: () => Promise<void>, state: () => Object, check: () => Promise<void> }}
|
|
264
|
+
*/
|
|
265
|
+
export function startDeployHookManager({ pollMs = SETTINGS_POLL_MS, log = (l) => console.log(l) } = {}) {
|
|
266
|
+
let server = null;
|
|
267
|
+
let active = null; // the settings the running server was started with
|
|
268
|
+
let lastError = null;
|
|
269
|
+
let stopped = false;
|
|
270
|
+
let busy = false;
|
|
271
|
+
|
|
272
|
+
const check = async () => {
|
|
273
|
+
if (stopped || busy) return;
|
|
274
|
+
busy = true;
|
|
275
|
+
try {
|
|
276
|
+
let wanted;
|
|
277
|
+
try { wanted = hookSettings(); } catch { return; }
|
|
278
|
+
|
|
279
|
+
const same = active && wanted.enabled && active.port === wanted.port && active.bind === wanted.bind;
|
|
280
|
+
if (same) return;
|
|
281
|
+
|
|
282
|
+
if (server) {
|
|
283
|
+
await server.close();
|
|
284
|
+
log(`[deploy hook] stopped listening on ${active.bind}:${active.port}`);
|
|
285
|
+
server = null;
|
|
286
|
+
active = null;
|
|
287
|
+
}
|
|
288
|
+
if (!wanted.enabled) { lastError = null; return; }
|
|
289
|
+
|
|
290
|
+
try {
|
|
291
|
+
server = await startHookServer({ port: wanted.port, bind: wanted.bind });
|
|
292
|
+
active = { port: wanted.port, bind: wanted.bind };
|
|
293
|
+
lastError = null;
|
|
294
|
+
log(`[deploy hook] listening on ${wanted.bind}:${wanted.port}`);
|
|
295
|
+
} catch (err) {
|
|
296
|
+
// Said once per distinct failure, then retried quietly on each poll:
|
|
297
|
+
// a port that is busy now may be free in a minute.
|
|
298
|
+
const message = err.code === 'EADDRINUSE'
|
|
299
|
+
? `port ${wanted.port} is already in use`
|
|
300
|
+
: err.message;
|
|
301
|
+
if (lastError !== message) log(`[deploy hook] could not listen on ${wanted.bind}:${wanted.port}: ${message}`);
|
|
302
|
+
lastError = message;
|
|
303
|
+
}
|
|
304
|
+
} finally {
|
|
305
|
+
busy = false;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const timer = setInterval(() => { check(); }, pollMs);
|
|
310
|
+
check();
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
check,
|
|
314
|
+
state: () => ({ listening: Boolean(server), ...(active ?? {}), error: lastError }),
|
|
315
|
+
async stop() {
|
|
316
|
+
stopped = true;
|
|
317
|
+
clearInterval(timer);
|
|
318
|
+
if (server) {
|
|
319
|
+
try { await server.close(); } catch { /* ignore */ }
|
|
320
|
+
server = null;
|
|
321
|
+
active = null;
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|