easy-devops 1.2.1 → 1.3.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/README.md +97 -6
- package/dist/ui/assets/index-CMRzkIKj.css +1 -0
- package/dist/ui/assets/index-DEouL3IK.js +452 -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 +15 -4
- 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 +92 -1
- 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 +222 -47
- 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/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 +37 -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 +94 -28
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +94 -2
- 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 +31 -1
- 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 +70 -7
- 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 +302 -15
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +80 -61
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +21 -3
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +180 -12
- package/src/dashboard/controllers/sslController.js +288 -10
- 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/js/app.js +489 -19
- package/src/dashboard/routes/auth.js +10 -10
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +9 -0
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +9 -0
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +9 -0
- package/src/dashboard/routes/ssl.js +10 -0
- package/src/dashboard/server.js +288 -21
- package/src/dashboard/socketManager.js +171 -207
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/login.ejs +68 -7
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +287 -0
- package/src/dashboard/views/partials/sidebar.ejs +2 -0
- package/src/dashboard/lib/domains-db.js +0 -51
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/ssl-issuer.js
|
|
3
|
+
*
|
|
4
|
+
* The one place certificates are issued. The CLI, the dashboard API and the
|
|
5
|
+
* Hub command path all call issueCertificate(), so the challenge strategy,
|
|
6
|
+
* the preflight gate and the nginx-state guarantees cannot drift apart.
|
|
7
|
+
*
|
|
8
|
+
* Proxy mode (the default) keeps nginx running: each port-80 server block
|
|
9
|
+
* proxies /.well-known/acme-challenge/ to a loopback listener this process
|
|
10
|
+
* starts for the duration of the issuance. Only a graceful reload is used.
|
|
11
|
+
*
|
|
12
|
+
* Standalone mode is the fallback for machines where nginx is not installed
|
|
13
|
+
* or not running. It stops nginx, and a finally block always restores nginx
|
|
14
|
+
* to the state it was found in.
|
|
15
|
+
*
|
|
16
|
+
* The choice between them is made from a LOCAL preflight only, never from a
|
|
17
|
+
* Let's Encrypt failure, so a misconfiguration cannot burn rate limits.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from 'fs/promises';
|
|
21
|
+
import path from 'path';
|
|
22
|
+
import os from 'os';
|
|
23
|
+
import * as acme from 'acme-client';
|
|
24
|
+
|
|
25
|
+
import { loadConfig } from './config.js';
|
|
26
|
+
import { run as defaultRun } from './shell.js';
|
|
27
|
+
import {
|
|
28
|
+
isWindows,
|
|
29
|
+
isDevDataDir,
|
|
30
|
+
nginxServiceStartCmd,
|
|
31
|
+
nginxServiceStopCmd,
|
|
32
|
+
nginxServiceReloadCmd,
|
|
33
|
+
nginxIsRunningCmd,
|
|
34
|
+
isNginxRunning,
|
|
35
|
+
nginxTestCmd,
|
|
36
|
+
isNginxTestOk,
|
|
37
|
+
combineOutput,
|
|
38
|
+
} from './platform.js';
|
|
39
|
+
import {
|
|
40
|
+
getConfDDir,
|
|
41
|
+
hasChallengeLocation,
|
|
42
|
+
parseListenAddresses,
|
|
43
|
+
DEFAULT_ACME_CHALLENGE_PORT,
|
|
44
|
+
} from './nginx-conf-generator.js';
|
|
45
|
+
import { withChallengeServer, preflight, resolveProbeHosts } from './acme-challenge.js';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Written next to fullchain.pem after every successful issuance. It records
|
|
49
|
+
* HOW the certificate was obtained, which the certificate itself cannot say:
|
|
50
|
+
* a single-name certificate validated over DNS-01 looks exactly like one
|
|
51
|
+
* validated over HTTP-01. core/cert-renewal.js reads it to decide whether a
|
|
52
|
+
* certificate can be renewed unattended.
|
|
53
|
+
*/
|
|
54
|
+
export const CERT_META_FILE = 'ez-cert.json';
|
|
55
|
+
|
|
56
|
+
export const CHALLENGE_MODES = Object.freeze({
|
|
57
|
+
AUTO: 'auto',
|
|
58
|
+
PROXY: 'proxy',
|
|
59
|
+
STANDALONE: 'standalone',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/** Advice shown when neither mode could complete the challenge. */
|
|
63
|
+
const EXTERNAL_CAUSE_HINT = [
|
|
64
|
+
'Both challenge modes failed locally, which usually means the cause is outside Easy DevOps:',
|
|
65
|
+
' • the domain\'s DNS A record does not point at this server',
|
|
66
|
+
' • port 80 is blocked by a firewall or security group',
|
|
67
|
+
' • an AAAA/IPv6 record points somewhere else (Let\'s Encrypt prefers IPv6)',
|
|
68
|
+
' • a CDN or proxy in front of the server forces HTTPS or intercepts the path',
|
|
69
|
+
].join('\n');
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* One-line human summary of how an issuance went, for the CLI, the dashboard
|
|
73
|
+
* toast and the Hub command result — so all three report the same thing.
|
|
74
|
+
*
|
|
75
|
+
* @param {Object} result Value returned by issueCertificate()
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
78
|
+
export function describeIssuance(result) {
|
|
79
|
+
if (!result) return '';
|
|
80
|
+
|
|
81
|
+
const parts = [];
|
|
82
|
+
if (result.staging) parts.push('Let\'s Encrypt STAGING (not a trusted certificate)');
|
|
83
|
+
|
|
84
|
+
if (result.mode === CHALLENGE_MODES.PROXY) {
|
|
85
|
+
parts.push('nginx kept running (proxy challenge)');
|
|
86
|
+
} else if (result.mode === CHALLENGE_MODES.STANDALONE) {
|
|
87
|
+
const secs = (result.nginxDownMs / 1000).toFixed(1);
|
|
88
|
+
parts.push(
|
|
89
|
+
result.fellBack
|
|
90
|
+
? `fell back to standalone — nginx was stopped for ${secs}s`
|
|
91
|
+
: `standalone mode — nginx was stopped for ${secs}s`
|
|
92
|
+
);
|
|
93
|
+
if (result.nginxRestored === false) parts.push('nginx did NOT come back up');
|
|
94
|
+
} else if (result.mode === 'dns-01') {
|
|
95
|
+
parts.push('DNS-01 challenge (nginx untouched)');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (result.configRegenerated) parts.push('the nginx config was regenerated to add the challenge location');
|
|
99
|
+
|
|
100
|
+
return parts.join('; ');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** ACME directory for the configured environment. */
|
|
104
|
+
function directoryUrl(staging) {
|
|
105
|
+
return staging ? acme.directory.letsencrypt.staging : acme.directory.letsencrypt.production;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Staging is used when explicitly enabled, and always in dev data mode, so
|
|
110
|
+
* development and testing can never consume production rate limits.
|
|
111
|
+
*
|
|
112
|
+
* Exported so the decision can be asserted directly. That matters more than it
|
|
113
|
+
* looks: `isDevDataDir()` used to be true whenever `EASY_DEVOPS_DATA_DIR` was
|
|
114
|
+
* set, and every installed service sets it — so a real install silently issued
|
|
115
|
+
* **staging certificates**, which browsers reject, on the user's real domains.
|
|
116
|
+
* A test that pins "service data directory ⇒ production ACME" is the guard
|
|
117
|
+
* against that coming back.
|
|
118
|
+
*
|
|
119
|
+
* @param {Object} config
|
|
120
|
+
* @param {boolean} [override]
|
|
121
|
+
* @returns {boolean}
|
|
122
|
+
*/
|
|
123
|
+
export function useStaging(config, override) {
|
|
124
|
+
if (typeof override === 'boolean') return override;
|
|
125
|
+
if (isDevDataDir()) return true;
|
|
126
|
+
return Boolean(config.acmeStaging);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function nginxRunning(run) {
|
|
130
|
+
try {
|
|
131
|
+
return isNginxRunning(await run(nginxIsRunningCmd()));
|
|
132
|
+
} catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Reads the current config file for a domain, or null when absent. */
|
|
138
|
+
async function readDomainConf(nginxDir, domainName) {
|
|
139
|
+
const confPath = path.join(getConfDDir(nginxDir), `${domainName}.conf`);
|
|
140
|
+
try {
|
|
141
|
+
return { confPath, text: await fs.readFile(confPath, 'utf8') };
|
|
142
|
+
} catch {
|
|
143
|
+
return { confPath, text: null };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Graceful reload. Never stops nginx; validity is checked with isNginxTestOk().
|
|
149
|
+
* @returns {Promise<{ ok: boolean, output: string }>}
|
|
150
|
+
*/
|
|
151
|
+
export async function reloadNginx(run = defaultRun) {
|
|
152
|
+
const { nginxDir } = loadConfig();
|
|
153
|
+
|
|
154
|
+
const testResult = await run(nginxTestCmd(nginxDir), { cwd: nginxDir });
|
|
155
|
+
if (!isNginxTestOk(testResult)) {
|
|
156
|
+
return { ok: false, output: combineOutput(testResult) };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const result = await run(nginxServiceReloadCmd(nginxDir), { cwd: nginxDir });
|
|
160
|
+
return { ok: result.success, output: combineOutput(result) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Ensures the certificate directory exists, with the right ownership. */
|
|
164
|
+
async function ensureCertDir(certDir, run) {
|
|
165
|
+
if (isWindows) {
|
|
166
|
+
await fs.mkdir(certDir, { recursive: true });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const user = os.userInfo().username;
|
|
170
|
+
await run(`sudo -n mkdir -p "${certDir}"`);
|
|
171
|
+
await run(`sudo -n chown ${user}:${user} "${certDir}"`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Loads or creates the persistent ACME account key. */
|
|
175
|
+
async function getAccountKey(sslDir) {
|
|
176
|
+
const keyPath = path.join(sslDir, '.account', 'account.key');
|
|
177
|
+
await fs.mkdir(path.dirname(keyPath), { recursive: true });
|
|
178
|
+
try {
|
|
179
|
+
return await fs.readFile(keyPath);
|
|
180
|
+
} catch {
|
|
181
|
+
const key = await acme.crypto.createPrivateKey();
|
|
182
|
+
await fs.writeFile(keyPath, key);
|
|
183
|
+
return key;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function buildError(step, cause, consequence, extra = {}) {
|
|
188
|
+
return { step, cause, consequence, ...extra };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Makes sure nginx can answer an HTTP-01 challenge for a domain before any
|
|
193
|
+
* certificate is requested.
|
|
194
|
+
*
|
|
195
|
+
* Covers both flows:
|
|
196
|
+
* - new domain: no config exists yet, so an HTTP-only one is written first
|
|
197
|
+
* (there is no certificate to reference yet, so a 443 block would fail
|
|
198
|
+
* `nginx -t`)
|
|
199
|
+
* - existing domain whose config predates this feature and therefore has no
|
|
200
|
+
* challenge location: it is regenerated
|
|
201
|
+
*
|
|
202
|
+
* In both cases the config is written through writeConfWithTest(), so a bad
|
|
203
|
+
* config is rolled back, and nginx is only ever reloaded — never stopped.
|
|
204
|
+
*
|
|
205
|
+
* @param {Object} domain Domain object (v2 schema)
|
|
206
|
+
* @param {Function} [run] Injected shell runner (testing only)
|
|
207
|
+
* @returns {Promise<{ ok: boolean, regenerated: boolean, reason: string, error: string|null }>}
|
|
208
|
+
*/
|
|
209
|
+
export async function prepareDomainForHttpChallenge(domain, run = defaultRun) {
|
|
210
|
+
const { nginxDir } = loadConfig();
|
|
211
|
+
const { confPath, text } = await readDomainConf(nginxDir, domain.name);
|
|
212
|
+
|
|
213
|
+
const missing = text === null;
|
|
214
|
+
const stale = text !== null && !hasChallengeLocation(text);
|
|
215
|
+
|
|
216
|
+
if (!missing && !stale) {
|
|
217
|
+
return { ok: true, regenerated: false, reason: 'config already serves the challenge', error: null };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Write an HTTP-only variant: at this point the certificate may not exist,
|
|
221
|
+
// and a 443 block pointing at a missing certificate fails nginx -t.
|
|
222
|
+
const httpOnly = { ...domain, ssl: { ...(domain.ssl ?? {}), enabled: false } };
|
|
223
|
+
|
|
224
|
+
const { writeConfWithTest } = await import('./nginx-apply.js');
|
|
225
|
+
const applied = await writeConfWithTest(httpOnly);
|
|
226
|
+
|
|
227
|
+
if (!applied.ok) {
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
regenerated: false,
|
|
231
|
+
reason: missing ? 'no config existed' : 'config had no challenge location',
|
|
232
|
+
error: applied.error ?? `nginx rejected the temporary HTTP config for ${domain.name}`,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const reloaded = await reloadNginx(run);
|
|
237
|
+
if (!reloaded.ok) {
|
|
238
|
+
return {
|
|
239
|
+
ok: false,
|
|
240
|
+
regenerated: true,
|
|
241
|
+
reason: 'config written but nginx reload failed',
|
|
242
|
+
error: reloaded.output,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
ok: true,
|
|
248
|
+
regenerated: true,
|
|
249
|
+
reason: missing
|
|
250
|
+
? `wrote a temporary HTTP-only config for ${domain.name} (${confPath})`
|
|
251
|
+
: `regenerated ${confPath} to add the ACME challenge location`,
|
|
252
|
+
error: null,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Issues or renews a certificate.
|
|
258
|
+
*
|
|
259
|
+
* @param {string} domainName
|
|
260
|
+
* @param {Object} [options]
|
|
261
|
+
* @param {boolean} [options.www]
|
|
262
|
+
* @param {boolean} [options.wildcard]
|
|
263
|
+
* @param {'http'|'dns'} [options.validationMethod]
|
|
264
|
+
* @param {string|null} [options.email]
|
|
265
|
+
* @param {Function|null} [options.onDnsChallenge]
|
|
266
|
+
* @param {'auto'|'proxy'|'standalone'} [options.mode] Overrides sslChallengeMode
|
|
267
|
+
* @param {boolean} [options.staging] Overrides the staging setting
|
|
268
|
+
* @param {Function} [options.run] Injected shell runner (testing only)
|
|
269
|
+
* @returns {Promise<{
|
|
270
|
+
* success: boolean,
|
|
271
|
+
* certPath: string|null,
|
|
272
|
+
* keyPath: string|null,
|
|
273
|
+
* mode: string|null,
|
|
274
|
+
* fellBack: boolean,
|
|
275
|
+
* nginxDownMs: number,
|
|
276
|
+
* staging: boolean,
|
|
277
|
+
* preflight: { ok: boolean, detail: string }|null,
|
|
278
|
+
* configRegenerated: boolean,
|
|
279
|
+
* error: object|null
|
|
280
|
+
* }>}
|
|
281
|
+
*/
|
|
282
|
+
export async function issueCertificate(domainName, options = {}) {
|
|
283
|
+
const {
|
|
284
|
+
www = false,
|
|
285
|
+
wildcard = false,
|
|
286
|
+
validationMethod = 'http',
|
|
287
|
+
email = null,
|
|
288
|
+
onDnsChallenge = null,
|
|
289
|
+
mode: modeOverride = null,
|
|
290
|
+
staging: stagingOverride,
|
|
291
|
+
run = defaultRun,
|
|
292
|
+
} = options;
|
|
293
|
+
|
|
294
|
+
const config = loadConfig();
|
|
295
|
+
const sslDir = config.sslDir;
|
|
296
|
+
const nginxDir = config.nginxDir;
|
|
297
|
+
const challengePort = Number(config.acmeChallengePort) || DEFAULT_ACME_CHALLENGE_PORT;
|
|
298
|
+
const staging = useStaging(config, stagingOverride);
|
|
299
|
+
|
|
300
|
+
const base = {
|
|
301
|
+
success: false,
|
|
302
|
+
certPath: null,
|
|
303
|
+
keyPath: null,
|
|
304
|
+
mode: null,
|
|
305
|
+
fellBack: false,
|
|
306
|
+
nginxDownMs: 0,
|
|
307
|
+
staging,
|
|
308
|
+
preflight: null,
|
|
309
|
+
configRegenerated: false,
|
|
310
|
+
error: null,
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const acmeEmail = email || config.acmeEmail;
|
|
314
|
+
if (!acmeEmail) {
|
|
315
|
+
return {
|
|
316
|
+
...base,
|
|
317
|
+
error: buildError(
|
|
318
|
+
'email configuration',
|
|
319
|
+
"No email address configured. Let's Encrypt requires an email for account creation.",
|
|
320
|
+
'No certificate was issued. Set acmeEmail in Settings or pass an email.',
|
|
321
|
+
{ nginxRunning: true, configSaved: false }
|
|
322
|
+
),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (wildcard && validationMethod === 'http') {
|
|
327
|
+
return {
|
|
328
|
+
...base,
|
|
329
|
+
error: buildError(
|
|
330
|
+
'validation method',
|
|
331
|
+
'Wildcard certificates require DNS-01 validation, not HTTP-01.',
|
|
332
|
+
'No certificate was issued. Use the DNS validation method.',
|
|
333
|
+
{ nginxRunning: true, configSaved: false }
|
|
334
|
+
),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const certDir = path.join(sslDir, domainName);
|
|
339
|
+
await ensureCertDir(certDir, run);
|
|
340
|
+
|
|
341
|
+
const accountKey = await getAccountKey(sslDir);
|
|
342
|
+
const client = new acme.Client({ directoryUrl: directoryUrl(staging), accountKey });
|
|
343
|
+
|
|
344
|
+
const altNames = wildcard
|
|
345
|
+
? [domainName, `*.${domainName}`]
|
|
346
|
+
: (www ? [domainName, `www.${domainName}`] : [domainName]);
|
|
347
|
+
const [domainKey, csr] = await acme.crypto.createCsr({ commonName: domainName, altNames });
|
|
348
|
+
|
|
349
|
+
const writeCert = async (cert) => {
|
|
350
|
+
const certPath = path.join(certDir, 'fullchain.pem');
|
|
351
|
+
const keyPath = path.join(certDir, 'privkey.pem');
|
|
352
|
+
await fs.writeFile(keyPath, domainKey);
|
|
353
|
+
await fs.writeFile(certPath, cert);
|
|
354
|
+
|
|
355
|
+
// Best effort: a certificate without this file still renews — the names
|
|
356
|
+
// are read back out of the certificate — so a failed write must never
|
|
357
|
+
// turn a successful issuance into a reported failure.
|
|
358
|
+
try {
|
|
359
|
+
await fs.writeFile(
|
|
360
|
+
path.join(certDir, CERT_META_FILE),
|
|
361
|
+
JSON.stringify({
|
|
362
|
+
domain: domainName,
|
|
363
|
+
altNames,
|
|
364
|
+
validationMethod: validationMethod === 'dns' ? 'dns' : 'http',
|
|
365
|
+
staging,
|
|
366
|
+
issuedAt: new Date().toISOString(),
|
|
367
|
+
}, null, 2),
|
|
368
|
+
);
|
|
369
|
+
} catch { /* non-fatal */ }
|
|
370
|
+
|
|
371
|
+
return { certPath, keyPath };
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// ─── DNS-01 (wildcards) — unchanged, nginx is never touched ────────────────
|
|
375
|
+
if (validationMethod === 'dns') {
|
|
376
|
+
try {
|
|
377
|
+
const cert = await client.auto({
|
|
378
|
+
csr,
|
|
379
|
+
email: acmeEmail,
|
|
380
|
+
termsOfServiceAgreed: true,
|
|
381
|
+
challengePriority: ['dns-01'],
|
|
382
|
+
challengeCreateFn: async (authz, challenge, keyAuthorization) => {
|
|
383
|
+
if (onDnsChallenge) await onDnsChallenge(`_acme-challenge.${domainName}`, keyAuthorization);
|
|
384
|
+
},
|
|
385
|
+
challengeRemoveFn: async () => { /* TXT cleanup is the user's */ },
|
|
386
|
+
});
|
|
387
|
+
const paths = await writeCert(cert);
|
|
388
|
+
return { ...base, success: true, mode: 'dns-01', ...paths };
|
|
389
|
+
} catch (err) {
|
|
390
|
+
return {
|
|
391
|
+
...base,
|
|
392
|
+
mode: 'dns-01',
|
|
393
|
+
error: buildError(
|
|
394
|
+
'ACME validation',
|
|
395
|
+
err.message || 'DNS-01 challenge failed.',
|
|
396
|
+
'No certificate was issued. nginx was not touched.',
|
|
397
|
+
{ nginxRunning: true, configSaved: false }
|
|
398
|
+
),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// ─── HTTP-01 ───────────────────────────────────────────────────────────────
|
|
404
|
+
const requestedMode = modeOverride || config.sslChallengeMode || CHALLENGE_MODES.AUTO;
|
|
405
|
+
const nginxUp = await nginxRunning(run);
|
|
406
|
+
|
|
407
|
+
let result = { ...base };
|
|
408
|
+
|
|
409
|
+
// Proxy mode is viable only when nginx is actually serving port 80.
|
|
410
|
+
const wantProxy =
|
|
411
|
+
requestedMode === CHALLENGE_MODES.PROXY ||
|
|
412
|
+
(requestedMode === CHALLENGE_MODES.AUTO && nginxUp);
|
|
413
|
+
|
|
414
|
+
if (wantProxy) {
|
|
415
|
+
result = await issueViaProxy({
|
|
416
|
+
client, csr, acmeEmail, domainName, nginxDir, challengePort, run, writeCert, base,
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
if (result.success) return result;
|
|
420
|
+
|
|
421
|
+
// Only a FAILED LOCAL PREFLIGHT may trigger the fallback. A Let's Encrypt
|
|
422
|
+
// failure must not, or a broken DNS record would cost two attempts.
|
|
423
|
+
const preflightFailed = result.preflight && result.preflight.ok === false;
|
|
424
|
+
const mayFallBack = requestedMode === CHALLENGE_MODES.AUTO && preflightFailed;
|
|
425
|
+
|
|
426
|
+
if (!mayFallBack) return result;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// ─── Standalone fallback ───────────────────────────────────────────────────
|
|
430
|
+
if (requestedMode === CHALLENGE_MODES.PROXY) return result;
|
|
431
|
+
|
|
432
|
+
const standalone = await issueViaStandalone({
|
|
433
|
+
client, csr, acmeEmail, nginxDir, run, writeCert, base, nginxWasRunning: nginxUp,
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
standalone.fellBack = wantProxy;
|
|
437
|
+
standalone.preflight = result.preflight ?? null;
|
|
438
|
+
|
|
439
|
+
if (!standalone.success && wantProxy) {
|
|
440
|
+
const seen = result.preflight?.detail ? `\n\nPreflight saw: ${result.preflight.detail}` : '';
|
|
441
|
+
standalone.error = buildError(
|
|
442
|
+
standalone.error?.step ?? 'ACME validation',
|
|
443
|
+
`${standalone.error?.cause ?? 'Challenge failed.'}\n\n${EXTERNAL_CAUSE_HINT}${seen}`,
|
|
444
|
+
standalone.error?.consequence ?? 'No certificate was issued.',
|
|
445
|
+
{ nginxRunning: standalone.error?.nginxRunning ?? true, configSaved: false }
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return standalone;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Proxy mode: nginx keeps serving; we answer the challenge behind it.
|
|
454
|
+
*/
|
|
455
|
+
async function issueViaProxy({
|
|
456
|
+
client, csr, acmeEmail, domainName, nginxDir, challengePort, run, writeCert, base,
|
|
457
|
+
}) {
|
|
458
|
+
// Flow 1: a config generated before this feature has no challenge location.
|
|
459
|
+
const { confPath, text } = await readDomainConf(nginxDir, domainName);
|
|
460
|
+
let configRegenerated = false;
|
|
461
|
+
|
|
462
|
+
if (text !== null && !hasChallengeLocation(text)) {
|
|
463
|
+
return {
|
|
464
|
+
...base,
|
|
465
|
+
mode: CHALLENGE_MODES.PROXY,
|
|
466
|
+
configRegenerated: false,
|
|
467
|
+
preflight: {
|
|
468
|
+
ok: false,
|
|
469
|
+
detail:
|
|
470
|
+
`${confPath} has no ACME challenge location (it predates this feature). ` +
|
|
471
|
+
'Regenerate the domain config first — saving the domain does this.',
|
|
472
|
+
},
|
|
473
|
+
error: buildError(
|
|
474
|
+
'challenge configuration',
|
|
475
|
+
`The nginx config for ${domainName} does not forward ${'/.well-known/acme-challenge/'} to Easy DevOps.`,
|
|
476
|
+
'No certificate was issued. Save the domain once to regenerate its config, then retry.',
|
|
477
|
+
{ nginxRunning: true, configSaved: false }
|
|
478
|
+
),
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Adjustment 3: probe the address nginx actually listens on.
|
|
483
|
+
const listens = text !== null ? parseListenAddresses(text, 80) : [];
|
|
484
|
+
const probeHosts = resolveProbeHosts(listens);
|
|
485
|
+
|
|
486
|
+
const pre = await preflight({ domain: domainName, challengePort, probeHosts });
|
|
487
|
+
|
|
488
|
+
if (!pre.ok) {
|
|
489
|
+
const bound = listens.length > 0
|
|
490
|
+
? ` The config binds: ${listens.map((l) => `${l.host}:${l.port}`).join(', ')}.`
|
|
491
|
+
: '';
|
|
492
|
+
return {
|
|
493
|
+
...base,
|
|
494
|
+
mode: CHALLENGE_MODES.PROXY,
|
|
495
|
+
configRegenerated,
|
|
496
|
+
preflight: pre,
|
|
497
|
+
error: buildError(
|
|
498
|
+
'local preflight',
|
|
499
|
+
`nginx did not return the challenge token on ${pre.triedHosts.join(', ')}.${bound} ${pre.detail}`,
|
|
500
|
+
"No certificate was requested, so Let's Encrypt rate limits are untouched.",
|
|
501
|
+
{ nginxRunning: true, configSaved: false }
|
|
502
|
+
),
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
try {
|
|
507
|
+
const cert = await withChallengeServer(challengePort, async ({ setToken, removeToken }) => {
|
|
508
|
+
return client.auto({
|
|
509
|
+
csr,
|
|
510
|
+
email: acmeEmail,
|
|
511
|
+
termsOfServiceAgreed: true,
|
|
512
|
+
challengePriority: ['http-01'],
|
|
513
|
+
challengeCreateFn: async (authz, challenge, keyAuthorization) => {
|
|
514
|
+
setToken(challenge.token, keyAuthorization);
|
|
515
|
+
},
|
|
516
|
+
challengeRemoveFn: async (authz, challenge) => {
|
|
517
|
+
removeToken(challenge.token);
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
const paths = await writeCert(cert);
|
|
523
|
+
return {
|
|
524
|
+
...base,
|
|
525
|
+
success: true,
|
|
526
|
+
mode: CHALLENGE_MODES.PROXY,
|
|
527
|
+
configRegenerated,
|
|
528
|
+
preflight: pre,
|
|
529
|
+
...paths,
|
|
530
|
+
};
|
|
531
|
+
} catch (err) {
|
|
532
|
+
return {
|
|
533
|
+
...base,
|
|
534
|
+
mode: CHALLENGE_MODES.PROXY,
|
|
535
|
+
configRegenerated,
|
|
536
|
+
preflight: pre,
|
|
537
|
+
error: buildError(
|
|
538
|
+
'ACME validation',
|
|
539
|
+
err.message || 'HTTP-01 challenge failed.',
|
|
540
|
+
'No certificate was issued. nginx kept running throughout.',
|
|
541
|
+
{ nginxRunning: true, configSaved: false }
|
|
542
|
+
),
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Standalone mode: stop nginx, bind port 80 directly, then ALWAYS put nginx
|
|
549
|
+
* back the way it was found.
|
|
550
|
+
*/
|
|
551
|
+
async function issueViaStandalone({
|
|
552
|
+
client, csr, acmeEmail, nginxDir, run, writeCert, base, nginxWasRunning,
|
|
553
|
+
}) {
|
|
554
|
+
let stoppedAt = null;
|
|
555
|
+
|
|
556
|
+
// The result is built up in place so the finally block can record how long
|
|
557
|
+
// nginx was down — a `return { ...base }` would have been evaluated before
|
|
558
|
+
// finally ran, and the timing would never reach the caller.
|
|
559
|
+
const outcome = { ...base, mode: CHALLENGE_MODES.STANDALONE, nginxRestored: !nginxWasRunning };
|
|
560
|
+
|
|
561
|
+
try {
|
|
562
|
+
if (nginxWasRunning) {
|
|
563
|
+
stoppedAt = Date.now();
|
|
564
|
+
await run(nginxServiceStopCmd(nginxDir), { cwd: nginxDir });
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const cert = await withChallengeServer(80, async ({ setToken, removeToken }) => {
|
|
568
|
+
return client.auto({
|
|
569
|
+
csr,
|
|
570
|
+
email: acmeEmail,
|
|
571
|
+
termsOfServiceAgreed: true,
|
|
572
|
+
challengePriority: ['http-01'],
|
|
573
|
+
challengeCreateFn: async (authz, challenge, keyAuthorization) => {
|
|
574
|
+
setToken(challenge.token, keyAuthorization);
|
|
575
|
+
},
|
|
576
|
+
challengeRemoveFn: async (authz, challenge) => {
|
|
577
|
+
removeToken(challenge.token);
|
|
578
|
+
},
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
const paths = await writeCert(cert);
|
|
583
|
+
outcome.success = true;
|
|
584
|
+
outcome.certPath = paths.certPath;
|
|
585
|
+
outcome.keyPath = paths.keyPath;
|
|
586
|
+
return outcome;
|
|
587
|
+
} catch (err) {
|
|
588
|
+
outcome.error = buildError(
|
|
589
|
+
'ACME validation',
|
|
590
|
+
err.message || 'HTTP-01 challenge failed in standalone mode.',
|
|
591
|
+
'No certificate was issued.',
|
|
592
|
+
{ nginxRunning: true, configSaved: false }
|
|
593
|
+
);
|
|
594
|
+
return outcome;
|
|
595
|
+
} finally {
|
|
596
|
+
// Restore nginx no matter how we leave this block — success, failure, or
|
|
597
|
+
// an exception thrown past the catch.
|
|
598
|
+
if (nginxWasRunning) {
|
|
599
|
+
let restored = false;
|
|
600
|
+
try {
|
|
601
|
+
await run(nginxServiceStartCmd(nginxDir), { cwd: nginxDir, timeout: 15000 });
|
|
602
|
+
// Confirm rather than assume; retry once before giving up.
|
|
603
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
604
|
+
restored = await nginxRunning(run);
|
|
605
|
+
if (!restored) {
|
|
606
|
+
await run(nginxServiceStartCmd(nginxDir), { cwd: nginxDir, timeout: 15000 });
|
|
607
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
608
|
+
restored = await nginxRunning(run);
|
|
609
|
+
}
|
|
610
|
+
} catch {
|
|
611
|
+
restored = false;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
outcome.nginxDownMs = stoppedAt ? Date.now() - stoppedAt : 0;
|
|
615
|
+
outcome.nginxRestored = restored;
|
|
616
|
+
|
|
617
|
+
if (!restored) {
|
|
618
|
+
outcome.error = buildError(
|
|
619
|
+
'nginx restore',
|
|
620
|
+
'nginx was stopped for the standalone challenge and did not come back up.',
|
|
621
|
+
`Start it manually: ${nginxServiceStartCmd(nginxDir)}`,
|
|
622
|
+
{ nginxRunning: false, configSaved: false }
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|