badmfck-api-server 6.0.6 → 6.0.7
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 +4 -1
- package/dist/apiServer/deployment/AccessConfigWizard.d.ts +1 -0
- package/dist/apiServer/deployment/AccessConfigWizard.js +183 -0
- package/dist/apiServer/deployment/ProjectConfigWizard.js +34 -59
- package/dist/apiServer/deployment/cli.js +10 -0
- package/dist/apiServer/deployment/cliPrompt.d.ts +5 -0
- package/dist/apiServer/deployment/cliPrompt.js +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -294,7 +294,9 @@ monitorAuthDir: "/var/lib/my-service", // where 2FA enrollments are kept
|
|
|
294
294
|
- **Sessions** expire after 15 minutes idle and 8 hours at most. 10 failed logins lock the account
|
|
295
295
|
for 30 minutes.
|
|
296
296
|
- **Change access without a restart.** Instead of an inline object, `access` can be a path to a JSON
|
|
297
|
-
file with the same shape: `access: "/etc/my-service/access.json"`.
|
|
297
|
+
file with the same shape: `access: "/etc/my-service/access.json"`. `npx badmfck access-config` creates
|
|
298
|
+
and edits this file from a menu. Passwords are typed hidden and stored as hashes, and the result is
|
|
299
|
+
checked with the service's own validator. The file is watched, and changes apply
|
|
298
300
|
within a second, without a restart or a redeploy:
|
|
299
301
|
|
|
300
302
|
```jsonc
|
|
@@ -709,6 +711,7 @@ git: it holds credentials.
|
|
|
709
711
|
| `badmfck hash-password [--deployer]` | hash a password for the monitor or `setup.json` |
|
|
710
712
|
| `badmfck project-token [--hash]` | generate a project token and its hash, or hash an existing token |
|
|
711
713
|
| `badmfck project-config` | build a project config for the deployer step by step (plus a `deploy.json` skeleton) |
|
|
714
|
+
| `badmfck access-config` | create or edit `access.json`: monitor and documentation users, roles, passwords, allowed addresses |
|
|
712
715
|
|
|
713
716
|
To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json`.
|
|
714
717
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function AccessConfigWizard(): Promise<void>;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AccessConfigWizard = AccessConfigWizard;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
const process_1 = require("process");
|
|
11
|
+
const PasswordHash_1 = require("../helper/PasswordHash");
|
|
12
|
+
const IpAllowList_1 = require("../helper/IpAllowList");
|
|
13
|
+
const MonitorAuth_1 = require("../monitor/MonitorAuth");
|
|
14
|
+
const AccessStore_1 = require("../monitor/AccessStore");
|
|
15
|
+
const HashPassword_1 = require("./HashPassword");
|
|
16
|
+
const cliCommon_1 = require("./cliCommon");
|
|
17
|
+
const cliPrompt_1 = require("./cliPrompt");
|
|
18
|
+
const ROLES = {
|
|
19
|
+
admin: "everything, incl. restart, log level and IP blocking",
|
|
20
|
+
it: "dashboard and logs",
|
|
21
|
+
analytic: "dashboard only",
|
|
22
|
+
};
|
|
23
|
+
async function AccessConfigWizard() {
|
|
24
|
+
const { red, green, yellow, dim, bold } = (0, cliCommon_1.palette)();
|
|
25
|
+
if (!process_1.stdin.isTTY) {
|
|
26
|
+
console.error(red("✗ access-config needs an interactive terminal — it asks for passwords."));
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
console.log(bold("\nAccess file for the monitor and the documentation") + dim(" (the service re-reads it on change)"));
|
|
30
|
+
const file = path_1.default.resolve(await (0, cliPrompt_1.ask)("File", "access.json"));
|
|
31
|
+
let doc = { monitor: [], documentation: [] };
|
|
32
|
+
if (fs_1.default.existsSync(file)) {
|
|
33
|
+
try {
|
|
34
|
+
doc = JSON.parse(fs_1.default.readFileSync(file, "utf-8") || "{}");
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
console.error(red(`✗ ${file} is not valid JSON (${e?.message ?? e}) — fix it first or choose another file.`));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
const problem = AccessStore_1.AccessStore.validate(doc);
|
|
41
|
+
if (problem)
|
|
42
|
+
console.log(yellow(` ⚠ The file has a problem the service would refuse: ${problem}. Fix it here before saving.`));
|
|
43
|
+
console.log(dim(` Loaded ${file}`));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
console.log(dim(` New file: ${file}`));
|
|
47
|
+
}
|
|
48
|
+
doc.monitor = Array.isArray(doc.monitor) ? doc.monitor : [];
|
|
49
|
+
doc.documentation = Array.isArray(doc.documentation) ? doc.documentation : [];
|
|
50
|
+
let changed = false;
|
|
51
|
+
const show = () => {
|
|
52
|
+
console.log("\n" + bold("Monitor users"));
|
|
53
|
+
if (!doc.monitor.length)
|
|
54
|
+
console.log(dim(" none — nobody can open the monitor"));
|
|
55
|
+
for (const u of doc.monitor) {
|
|
56
|
+
const state = PasswordHash_1.PasswordHash.isHash(u.password) ? dim("hashed password") : red("PLAIN-TEXT password — refused at sign-in, change it");
|
|
57
|
+
console.log(` ${u.login.padEnd(20)} ${String(u.role ?? "admin (default)").padEnd(16)} ${state}`);
|
|
58
|
+
}
|
|
59
|
+
console.log(bold("Documentation users"));
|
|
60
|
+
if (!doc.documentation.length)
|
|
61
|
+
console.log(dim(" none"));
|
|
62
|
+
for (const u of doc.documentation)
|
|
63
|
+
console.log(` ${u.login.padEnd(20)} ${u.endpoints?.length ? "endpoints: " + u.endpoints.join(", ") : dim("all endpoints")}`);
|
|
64
|
+
console.log(bold("Monitor allowed addresses"));
|
|
65
|
+
console.log(" " + (doc.monitorAllowedIps?.length ? doc.monitorAllowedIps.join(", ") : dim("any address")));
|
|
66
|
+
};
|
|
67
|
+
const monitorLogin = (login) => doc.monitor.find((u) => u.login === login);
|
|
68
|
+
const docLogin = (login) => doc.documentation.find((u) => u.login === login);
|
|
69
|
+
async function newMonitorPassword(login) {
|
|
70
|
+
for (;;) {
|
|
71
|
+
const p1 = await (0, HashPassword_1.askHidden)(` password for ${login} (hidden, 12+ characters): `);
|
|
72
|
+
const policy = MonitorAuth_1.MonitorAuth.passwordPolicyError(p1);
|
|
73
|
+
if (policy) {
|
|
74
|
+
console.log(red(` ✗ ${policy}`));
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const p2 = await (0, HashPassword_1.askHidden)(" repeat: ");
|
|
78
|
+
if (p1 !== p2) {
|
|
79
|
+
console.log(red(" ✗ passwords do not match"));
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
return PasswordHash_1.PasswordHash.hash(p1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const askRole = (def) => (0, cliPrompt_1.askValid)(` role: ${Object.keys(ROLES).map(r => `${r} (${ROLES[r]})`).join(" / ")}\n role`, def, v => v in ROLES ? null : "admin, it or analytic");
|
|
86
|
+
const actions = {
|
|
87
|
+
"1": { title: "add a monitor user", run: async () => {
|
|
88
|
+
const login = await (0, cliPrompt_1.askValid)(" login", "", v => !v ? "required" : monitorLogin(v) ? "already exists — use 'change password' or 'change role'" : null);
|
|
89
|
+
const role = await askRole("it");
|
|
90
|
+
doc.monitor.push({ login, password: await newMonitorPassword(login), role });
|
|
91
|
+
console.log(green(` ✓ ${login} added`) + dim(" — 2FA is set up at the first sign-in"));
|
|
92
|
+
changed = true;
|
|
93
|
+
} },
|
|
94
|
+
"2": { title: "change a monitor password", run: async () => {
|
|
95
|
+
const u = monitorLogin(await (0, cliPrompt_1.askValid)(" login", "", v => monitorLogin(v) ? null : "no such monitor user"));
|
|
96
|
+
u.password = await newMonitorPassword(u.login);
|
|
97
|
+
console.log(green(` ✓ password of ${u.login} changed`) + dim(" — their open sessions end when the service picks up the file"));
|
|
98
|
+
changed = true;
|
|
99
|
+
} },
|
|
100
|
+
"3": { title: "change a monitor role", run: async () => {
|
|
101
|
+
const u = monitorLogin(await (0, cliPrompt_1.askValid)(" login", "", v => monitorLogin(v) ? null : "no such monitor user"));
|
|
102
|
+
u.role = await askRole(u.role ?? "admin");
|
|
103
|
+
console.log(green(` ✓ ${u.login} is now ${u.role}`));
|
|
104
|
+
changed = true;
|
|
105
|
+
} },
|
|
106
|
+
"4": { title: "remove a monitor user", run: async () => {
|
|
107
|
+
const login = await (0, cliPrompt_1.askValid)(" login", "", v => monitorLogin(v) ? null : "no such monitor user");
|
|
108
|
+
if (!(await (0, cliPrompt_1.yes)(` remove ${login}?`, false)))
|
|
109
|
+
return;
|
|
110
|
+
doc.monitor = doc.monitor.filter((u) => u.login !== login);
|
|
111
|
+
console.log(green(` ✓ ${login} removed`) + dim(" — their sessions end when the service picks up the file"));
|
|
112
|
+
changed = true;
|
|
113
|
+
} },
|
|
114
|
+
"5": { title: "add a documentation user", run: async () => {
|
|
115
|
+
const login = await (0, cliPrompt_1.askValid)(" login", "", v => !v ? "required" : docLogin(v) ? "already exists" : null);
|
|
116
|
+
console.log(yellow(" ⚠ Documentation passwords are stored as they are (that's how documentation sign-in works) — never reuse one from elsewhere."));
|
|
117
|
+
let password = "";
|
|
118
|
+
if (await (0, cliPrompt_1.yes)(" generate a random password?", true)) {
|
|
119
|
+
password = crypto_1.default.randomBytes(18).toString("base64url");
|
|
120
|
+
console.log(` password: ${bold(password)} ${dim("— pass it on now")}`);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
password = await (0, cliPrompt_1.askValid)(" password", "", v => v.length >= 12 ? null : "at least 12 characters");
|
|
124
|
+
}
|
|
125
|
+
const endpoints = (0, cliPrompt_1.list)(await (0, cliPrompt_1.ask)(" limit to endpoints, comma-separated (empty = all)", ""));
|
|
126
|
+
doc.documentation.push({ login, password, ...(endpoints.length ? { endpoints } : {}) });
|
|
127
|
+
console.log(green(` ✓ ${login} added`));
|
|
128
|
+
changed = true;
|
|
129
|
+
} },
|
|
130
|
+
"6": { title: "remove a documentation user", run: async () => {
|
|
131
|
+
const login = await (0, cliPrompt_1.askValid)(" login", "", v => docLogin(v) ? null : "no such documentation user");
|
|
132
|
+
if (!(await (0, cliPrompt_1.yes)(` remove ${login}?`, false)))
|
|
133
|
+
return;
|
|
134
|
+
doc.documentation = doc.documentation.filter((u) => u.login !== login);
|
|
135
|
+
console.log(green(` ✓ ${login} removed`));
|
|
136
|
+
changed = true;
|
|
137
|
+
} },
|
|
138
|
+
"7": { title: "set the monitor's allowed addresses", run: async () => {
|
|
139
|
+
const v = await (0, cliPrompt_1.askValid)(" IPs and subnets, comma-separated (empty = any address)", (doc.monitorAllowedIps ?? []).join(", "), x => IpAllowList_1.IpAllowList.build((0, cliPrompt_1.list)(x)).error ?? null);
|
|
140
|
+
const ips = (0, cliPrompt_1.list)(v);
|
|
141
|
+
if (ips.length)
|
|
142
|
+
doc.monitorAllowedIps = ips;
|
|
143
|
+
else
|
|
144
|
+
delete doc.monitorAllowedIps;
|
|
145
|
+
if (ips.length)
|
|
146
|
+
console.log(dim(" Behind nginx the service must see the real address (X-Real-IP), or every request looks like 127.0.0.1."));
|
|
147
|
+
changed = true;
|
|
148
|
+
} },
|
|
149
|
+
};
|
|
150
|
+
for (;;) {
|
|
151
|
+
show();
|
|
152
|
+
console.log("\n" + Object.entries(actions).map(([k, a]) => ` ${k}) ${a.title}`).join("\n") + `\n s) save and exit\n q) quit without saving`);
|
|
153
|
+
const choice = (await (0, cliPrompt_1.ask)("Choose", "")).toLowerCase();
|
|
154
|
+
if (choice === "q") {
|
|
155
|
+
if (changed && !(await (0, cliPrompt_1.yes)("Discard the changes?", false)))
|
|
156
|
+
continue;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (choice === "s") {
|
|
160
|
+
const problem = AccessStore_1.AccessStore.validate(doc);
|
|
161
|
+
if (problem) {
|
|
162
|
+
console.log(red(` ✗ Not saved — the service would refuse it: ${problem}`));
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (!doc.monitor.some((u) => PasswordHash_1.PasswordHash.isHash(u.password)))
|
|
166
|
+
console.log(yellow(" ⚠ No monitor user can sign in — the monitor stays closed."));
|
|
167
|
+
fs_1.default.mkdirSync(path_1.default.dirname(file), { recursive: true });
|
|
168
|
+
const tmp = file + ".tmp";
|
|
169
|
+
fs_1.default.writeFileSync(tmp, JSON.stringify(doc, null, 2) + "\n", { encoding: "utf-8", mode: 0o600 });
|
|
170
|
+
fs_1.default.chmodSync(tmp, 0o600);
|
|
171
|
+
fs_1.default.renameSync(tmp, file);
|
|
172
|
+
console.log(green(`✓ Saved ${file}`) + dim(" (0600) — a running service picks it up within a second"));
|
|
173
|
+
console.log(dim(` Point the service at it: access: ${JSON.stringify(file)}`));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const action = actions[choice];
|
|
177
|
+
if (!action) {
|
|
178
|
+
console.log(red(" ✗ choose a number, s or q"));
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
await action.run();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -7,54 +7,29 @@ exports.ProjectConfigWizard = ProjectConfigWizard;
|
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
-
const promises_1 = require("readline/promises");
|
|
11
10
|
const process_1 = require("process");
|
|
12
11
|
const PasswordHash_1 = require("../helper/PasswordHash");
|
|
13
12
|
const Validator_1 = require("../helper/Validator");
|
|
14
13
|
const ConfigService_1 = require("./ConfigService");
|
|
15
14
|
const HashPassword_1 = require("./HashPassword");
|
|
16
15
|
const cliCommon_1 = require("./cliCommon");
|
|
16
|
+
const cliPrompt_1 = require("./cliPrompt");
|
|
17
17
|
async function ProjectConfigWizard() {
|
|
18
18
|
const { red, green, yellow, dim, bold } = (0, cliCommon_1.palette)();
|
|
19
19
|
if (!process_1.stdin.isTTY) {
|
|
20
20
|
console.error(red("✗ project-config needs an interactive terminal — it asks for secrets."));
|
|
21
21
|
process.exit(1);
|
|
22
22
|
}
|
|
23
|
-
async function ask(question, def = "") {
|
|
24
|
-
const rl = (0, promises_1.createInterface)({ input: process_1.stdin, output: process_1.stdout });
|
|
25
|
-
try {
|
|
26
|
-
const a = (await rl.question(question + (def ? dim(` [${def}]`) : "") + ": ")).trim();
|
|
27
|
-
return a || def;
|
|
28
|
-
}
|
|
29
|
-
finally {
|
|
30
|
-
rl.close();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async function askValid(question, def, check) {
|
|
34
|
-
for (;;) {
|
|
35
|
-
const v = await ask(question, def);
|
|
36
|
-
const problem = check(v);
|
|
37
|
-
if (!problem)
|
|
38
|
-
return v;
|
|
39
|
-
console.log(red(` ✗ ${problem}`));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async function yes(question, def) {
|
|
43
|
-
const a = (await ask(question + " (y/n)", def ? "y" : "n")).toLowerCase();
|
|
44
|
-
return a.startsWith("y");
|
|
45
|
-
}
|
|
46
|
-
const list = (v, sep = ",") => v.split(sep).map(s => s.trim()).filter(Boolean);
|
|
47
|
-
const tokens = (v) => Array.from(v.matchAll(/"([^"]*)"|'([^']*)'|(\S+)/g), m => m[1] ?? m[2] ?? m[3]);
|
|
48
23
|
const section = (title) => console.log("\n" + bold(title));
|
|
49
24
|
console.log(bold("\nNew project config for the deployer") + dim(" (Enter keeps the value in brackets)"));
|
|
50
25
|
section("Project");
|
|
51
|
-
const name = await askValid("Name", "", v => /^[A-Za-z0-9._-]+$/.test(v) ? null : "letters, digits, '.', '_' and '-' only");
|
|
52
|
-
const description = await ask("Description", "");
|
|
53
|
-
const destination = await askValid("Destination folder on the server", `/var/node/${name}`, v => !path_1.default.isAbsolute(v) ? "must be an absolute path" : /^\/(var|etc|home|usr|opt|srv|tmp|root)?\/?$/.test(v) ? "pick a folder of its own, not a system folder" : null);
|
|
26
|
+
const name = await (0, cliPrompt_1.askValid)("Name", "", v => /^[A-Za-z0-9._-]+$/.test(v) ? null : "letters, digits, '.', '_' and '-' only");
|
|
27
|
+
const description = await (0, cliPrompt_1.ask)("Description", "");
|
|
28
|
+
const destination = await (0, cliPrompt_1.askValid)("Destination folder on the server", `/var/node/${name}`, v => !path_1.default.isAbsolute(v) ? "must be an absolute path" : /^\/(var|etc|home|usr|opt|srv|tmp|root)?\/?$/.test(v) ? "pick a folder of its own, not a system folder" : null);
|
|
54
29
|
section("Project token");
|
|
55
30
|
let plainToken = null;
|
|
56
31
|
let tokenHash;
|
|
57
|
-
if (await yes("Generate a new random token?", true)) {
|
|
32
|
+
if (await (0, cliPrompt_1.yes)("Generate a new random token?", true)) {
|
|
58
33
|
plainToken = crypto_1.default.randomBytes(32).toString("hex");
|
|
59
34
|
tokenHash = await PasswordHash_1.PasswordHash.hash(plainToken);
|
|
60
35
|
}
|
|
@@ -69,26 +44,26 @@ async function ProjectConfigWizard() {
|
|
|
69
44
|
tokenHash = await PasswordHash_1.PasswordHash.hash(existing);
|
|
70
45
|
}
|
|
71
46
|
section("Deploy steps");
|
|
72
|
-
const unpack = await yes("Unpack the uploaded archive", true);
|
|
73
|
-
const install = unpack ? await yes("Install dependencies (npm ci)", true) : false;
|
|
74
|
-
const ignoreDirectoryClean = unpack ? !(await yes("Clean the destination before unpacking", true)) : false;
|
|
75
|
-
const backup = await yes("Back up the current version before each deploy", true);
|
|
76
|
-
const migrate = await yes("Run `npm run migrate` before starting", false);
|
|
77
|
-
const pm2 = await yes("Run the app with pm2", true);
|
|
78
|
-
const pm2Attributes = pm2 ? tokens(await ask(`pm2 flags (quote values with spaces: --cron-restart "10 0 * * *")`, "")) : [];
|
|
79
|
-
const chmodRaw = await askValid("chmod for deployed files (empty = leave as unpacked)", "", v => !v || /^[0-7]{3,4}$/.test(v) ? null : "octal like 750");
|
|
47
|
+
const unpack = await (0, cliPrompt_1.yes)("Unpack the uploaded archive", true);
|
|
48
|
+
const install = unpack ? await (0, cliPrompt_1.yes)("Install dependencies (npm ci)", true) : false;
|
|
49
|
+
const ignoreDirectoryClean = unpack ? !(await (0, cliPrompt_1.yes)("Clean the destination before unpacking", true)) : false;
|
|
50
|
+
const backup = await (0, cliPrompt_1.yes)("Back up the current version before each deploy", true);
|
|
51
|
+
const migrate = await (0, cliPrompt_1.yes)("Run `npm run migrate` before starting", false);
|
|
52
|
+
const pm2 = await (0, cliPrompt_1.yes)("Run the app with pm2", true);
|
|
53
|
+
const pm2Attributes = pm2 ? (0, cliPrompt_1.tokens)(await (0, cliPrompt_1.ask)(`pm2 flags (quote values with spaces: --cron-restart "10 0 * * *")`, "")) : [];
|
|
54
|
+
const chmodRaw = await (0, cliPrompt_1.askValid)("chmod for deployed files (empty = leave as unpacked)", "", v => !v || /^[0-7]{3,4}$/.test(v) ? null : "octal like 750");
|
|
80
55
|
const chmod = chmodRaw ? Number(chmodRaw) : 0;
|
|
81
56
|
if (chmodRaw && Number(chmodRaw.slice(-1)) & 4)
|
|
82
57
|
console.log(yellow(` ⚠ ${chmodRaw} lets every user on the server read the files.`));
|
|
83
58
|
section("Notifications");
|
|
84
|
-
const email = list(await ask("Emails, comma-separated", ""));
|
|
85
|
-
const phone = list(await ask("Phones for SMS, comma-separated", ""));
|
|
86
|
-
const liveness = await askValid("Liveness URL checked after deploy (empty = none)", "", v => !v || /^https?:\/\/\S+$/.test(v) ? null : "must be an http(s) URL");
|
|
59
|
+
const email = (0, cliPrompt_1.list)(await (0, cliPrompt_1.ask)("Emails, comma-separated", ""));
|
|
60
|
+
const phone = (0, cliPrompt_1.list)(await (0, cliPrompt_1.ask)("Phones for SMS, comma-separated", ""));
|
|
61
|
+
const liveness = await (0, cliPrompt_1.askValid)("Liveness URL checked after deploy (empty = none)", "", v => !v || /^https?:\/\/\S+$/.test(v) ? null : "must be an http(s) URL");
|
|
87
62
|
section("Config values — replace _{{key}}_ in the shipped files");
|
|
88
63
|
console.log(dim(" Values are typed hidden. Empty key finishes."));
|
|
89
64
|
const config = {};
|
|
90
65
|
for (;;) {
|
|
91
|
-
const key = await askValid(" key", "", v => !v || /^[A-Za-z0-9_]+$/.test(v) ? (v && v in config ? `"${v}" is already set` : null) : "letters, digits and '_' only");
|
|
66
|
+
const key = await (0, cliPrompt_1.askValid)(" key", "", v => !v || /^[A-Za-z0-9_]+$/.test(v) ? (v && v in config ? `"${v}" is already set` : null) : "letters, digits and '_' only");
|
|
92
67
|
if (!key)
|
|
93
68
|
break;
|
|
94
69
|
config[key] = await (0, HashPassword_1.askHidden)(` value of ${key} (hidden): `);
|
|
@@ -104,23 +79,23 @@ async function ProjectConfigWizard() {
|
|
|
104
79
|
config,
|
|
105
80
|
};
|
|
106
81
|
section("Blue-green");
|
|
107
|
-
if (await yes("Deploy as two slots (blue/green) with zero-downtime switching?", false)) {
|
|
108
|
-
const bluePort = await askValid(" blue slot port", "", v => /^\d{2,5}$/.test(v) ? null : "a port number");
|
|
109
|
-
const greenPort = await askValid(" green slot port", "", v => !/^\d{2,5}$/.test(v) ? "a port number" : v === bluePort ? "must differ from the blue port" : null);
|
|
110
|
-
const onSwitch = tokens(await ask(" command after each switch", "/usr/sbin/nginx -s reload"));
|
|
82
|
+
if (await (0, cliPrompt_1.yes)("Deploy as two slots (blue/green) with zero-downtime switching?", false)) {
|
|
83
|
+
const bluePort = await (0, cliPrompt_1.askValid)(" blue slot port", "", v => /^\d{2,5}$/.test(v) ? null : "a port number");
|
|
84
|
+
const greenPort = await (0, cliPrompt_1.askValid)(" green slot port", "", v => !/^\d{2,5}$/.test(v) ? "a port number" : v === bluePort ? "must differ from the blue port" : null);
|
|
85
|
+
const onSwitch = (0, cliPrompt_1.tokens)(await (0, cliPrompt_1.ask)(" command after each switch", "/usr/sbin/nginx -s reload"));
|
|
111
86
|
if (onSwitch.length && !path_1.default.isAbsolute(onSwitch[0]))
|
|
112
87
|
console.log(yellow(" ⚠ Use an absolute path to the binary — the deployer runs it without a shell."));
|
|
113
88
|
cfg.bluegreen = { blue_config: { port: bluePort }, green_config: { port: greenPort }, ...(onSwitch.length ? { on_switch: onSwitch } : {}) };
|
|
114
89
|
}
|
|
115
90
|
section("nginx");
|
|
116
|
-
if (await yes("Let the deployer manage the nginx config of this project?", !!cfg.bluegreen)) {
|
|
117
|
-
const config_dir = await askValid(" nginx config folder (nginx includes <folder>/*.conf)", "/etc/nginx/node-projects", v => path_1.default.isAbsolute(v) ? null : "must be an absolute path");
|
|
118
|
-
const domain = await ask(" domain", "");
|
|
91
|
+
if (await (0, cliPrompt_1.yes)("Let the deployer manage the nginx config of this project?", !!cfg.bluegreen)) {
|
|
92
|
+
const config_dir = await (0, cliPrompt_1.askValid)(" nginx config folder (nginx includes <folder>/*.conf)", "/etc/nginx/node-projects", v => path_1.default.isAbsolute(v) ? null : "must be an absolute path");
|
|
93
|
+
const domain = await (0, cliPrompt_1.ask)(" domain", "");
|
|
119
94
|
cfg.nginx = { config_dir, ...(domain ? { domain } : {}) };
|
|
120
95
|
}
|
|
121
96
|
section("Secrets file — keeps config values out of the deployed code");
|
|
122
|
-
if (Object.keys(config).length && await yes("Store config values in a secrets file outside the deploy folder?", true)) {
|
|
123
|
-
const where = await ask(" folder (empty = secrets_dir from the deployer's setup.json)", "");
|
|
97
|
+
if (Object.keys(config).length && await (0, cliPrompt_1.yes)("Store config values in a secrets file outside the deploy folder?", true)) {
|
|
98
|
+
const where = await (0, cliPrompt_1.ask)(" folder (empty = secrets_dir from the deployer's setup.json)", "");
|
|
124
99
|
if (where && (!path_1.default.isAbsolute(where) || path_1.default.resolve(where).startsWith(path_1.default.resolve(destination) + path_1.default.sep)))
|
|
125
100
|
console.log(yellow(" ⚠ The folder must be absolute and outside the deploy folder — the deployer will refuse this project as is."));
|
|
126
101
|
cfg.secrets = where ? { dir: where } : {};
|
|
@@ -128,10 +103,10 @@ async function ProjectConfigWizard() {
|
|
|
128
103
|
console.log(yellow(" ⚠ The pm2 flags switch the user — the app couldn't read the secrets file; the deployer will refuse this project."));
|
|
129
104
|
}
|
|
130
105
|
section("Review — deploys wait for approval");
|
|
131
|
-
if (await yes("Require reviewers to approve each deploy?", false)) {
|
|
132
|
-
const reviewers = list(await askValid(" reviewer logins from the deployer's setup.json, comma-separated", "", v => list(v).length ? null : "at least one login"));
|
|
133
|
-
const mode = (await askValid(" approval: any (one accept) / all (everyone)", "any", v => v === "any" || v === "all" ? null : "any or all"));
|
|
134
|
-
const ttlHours = Number(await askValid(" hours to decide", "24", v => /^\d+$/.test(v) && +v >= 1 && +v <= 168 ? null : "1–168"));
|
|
106
|
+
if (await (0, cliPrompt_1.yes)("Require reviewers to approve each deploy?", false)) {
|
|
107
|
+
const reviewers = (0, cliPrompt_1.list)(await (0, cliPrompt_1.askValid)(" reviewer logins from the deployer's setup.json, comma-separated", "", v => (0, cliPrompt_1.list)(v).length ? null : "at least one login"));
|
|
108
|
+
const mode = (await (0, cliPrompt_1.askValid)(" approval: any (one accept) / all (everyone)", "any", v => v === "any" || v === "all" ? null : "any or all"));
|
|
109
|
+
const ttlHours = Number(await (0, cliPrompt_1.askValid)(" hours to decide", "24", v => /^\d+$/.test(v) && +v >= 1 && +v <= 168 ? null : "1–168"));
|
|
135
110
|
cfg.review = { reviewers, mode, ttlHours };
|
|
136
111
|
}
|
|
137
112
|
const errors = await Validator_1.Validator.validateStructure(ConfigService_1.PROJECT_CONFIG_MODEL, cfg);
|
|
@@ -142,18 +117,18 @@ async function ProjectConfigWizard() {
|
|
|
142
117
|
process.exit(1);
|
|
143
118
|
}
|
|
144
119
|
section("Deploy client (deploy.json for the project)");
|
|
145
|
-
const host = await ask(" deployer upload URL", "https://deployer.example.com/api/pckg/add");
|
|
146
|
-
const username = await ask(" deployer user login", "");
|
|
120
|
+
const host = await (0, cliPrompt_1.ask)(" deployer upload URL", "https://deployer.example.com/api/pckg/add");
|
|
121
|
+
const username = await (0, cliPrompt_1.ask)(" deployer user login", "");
|
|
147
122
|
const projectJson = JSON.stringify(cfg, null, 4);
|
|
148
123
|
const deployJson = JSON.stringify({ name, host, token: plainToken ?? "<the existing token>", username, password: "<your deployer password>" }, null, 4);
|
|
149
124
|
console.log("");
|
|
150
125
|
const save = async (what, content, def) => {
|
|
151
|
-
const target = await ask(`Save ${what} to (empty = print here)`, def);
|
|
126
|
+
const target = await (0, cliPrompt_1.ask)(`Save ${what} to (empty = print here)`, def);
|
|
152
127
|
if (!target) {
|
|
153
128
|
console.log("\n" + content + "\n");
|
|
154
129
|
return;
|
|
155
130
|
}
|
|
156
|
-
if (fs_1.default.existsSync(target) && !(await yes(` ${target} exists — overwrite?`, false))) {
|
|
131
|
+
if (fs_1.default.existsSync(target) && !(await (0, cliPrompt_1.yes)(` ${target} exists — overwrite?`, false))) {
|
|
157
132
|
console.log("\n" + content + "\n");
|
|
158
133
|
return;
|
|
159
134
|
}
|
|
@@ -9,6 +9,7 @@ const Restore_1 = require("./Restore");
|
|
|
9
9
|
const BackupManager_1 = require("./BackupManager");
|
|
10
10
|
const HashPassword_1 = require("./HashPassword");
|
|
11
11
|
const ProjectConfigWizard_1 = require("./ProjectConfigWizard");
|
|
12
|
+
const AccessConfigWizard_1 = require("./AccessConfigWizard");
|
|
12
13
|
function usage() {
|
|
13
14
|
console.error("Usage:");
|
|
14
15
|
console.error(" badmfck deploy [config.json] — build + upload the current project");
|
|
@@ -19,6 +20,7 @@ function usage() {
|
|
|
19
20
|
console.error(" badmfck hash-password [--deployer] — hash a password for the monitor (or deployer setup.json)");
|
|
20
21
|
console.error(" badmfck project-token [--hash] — new project token + its hash (or hash an existing one)");
|
|
21
22
|
console.error(" badmfck project-config — build a project config for the deployer, step by step");
|
|
23
|
+
console.error(" badmfck access-config — create or edit access.json (monitor & documentation users)");
|
|
22
24
|
console.error("");
|
|
23
25
|
console.error("Config path is optional (auto-discovers deploy/ folder → deploy.json when omitted).");
|
|
24
26
|
console.error("Via npm, prefix args with -- e.g. npm run deploy -- deploy/custom.json");
|
|
@@ -105,6 +107,14 @@ async function main() {
|
|
|
105
107
|
await (0, HashPassword_1.HashPassword)(rest.includes("--deployer") ? "deployer" : "monitor");
|
|
106
108
|
break;
|
|
107
109
|
}
|
|
110
|
+
case "access-config": {
|
|
111
|
+
if (rest.length) {
|
|
112
|
+
console.error("access-config takes no arguments — the file is asked interactively.");
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
await (0, AccessConfigWizard_1.AccessConfigWizard)();
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
108
118
|
case "project-config": {
|
|
109
119
|
if (rest.length) {
|
|
110
120
|
console.error("project-config takes no arguments — everything is asked interactively.");
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function ask(question: string, def?: string): Promise<string>;
|
|
2
|
+
export declare function askValid(question: string, def: string, check: (v: string) => string | null): Promise<string>;
|
|
3
|
+
export declare function yes(question: string, def: boolean): Promise<boolean>;
|
|
4
|
+
export declare const list: (v: string, sep?: string) => string[];
|
|
5
|
+
export declare const tokens: (v: string) => string[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokens = exports.list = void 0;
|
|
4
|
+
exports.ask = ask;
|
|
5
|
+
exports.askValid = askValid;
|
|
6
|
+
exports.yes = yes;
|
|
7
|
+
const promises_1 = require("readline/promises");
|
|
8
|
+
const process_1 = require("process");
|
|
9
|
+
const cliCommon_1 = require("./cliCommon");
|
|
10
|
+
async function ask(question, def = "") {
|
|
11
|
+
const { dim } = (0, cliCommon_1.palette)();
|
|
12
|
+
const rl = (0, promises_1.createInterface)({ input: process_1.stdin, output: process_1.stdout });
|
|
13
|
+
try {
|
|
14
|
+
const a = (await rl.question(question + (def ? dim(` [${def}]`) : "") + ": ")).trim();
|
|
15
|
+
return a || def;
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
rl.close();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function askValid(question, def, check) {
|
|
22
|
+
const { red } = (0, cliCommon_1.palette)();
|
|
23
|
+
for (;;) {
|
|
24
|
+
const v = await ask(question, def);
|
|
25
|
+
const problem = check(v);
|
|
26
|
+
if (!problem)
|
|
27
|
+
return v;
|
|
28
|
+
console.log(red(` ✗ ${problem}`));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function yes(question, def) {
|
|
32
|
+
const a = (await ask(question + " (y/n)", def ? "y" : "n")).toLowerCase();
|
|
33
|
+
return a.startsWith("y");
|
|
34
|
+
}
|
|
35
|
+
const list = (v, sep = ",") => v.split(sep).map(s => s.trim()).filter(Boolean);
|
|
36
|
+
exports.list = list;
|
|
37
|
+
const tokens = (v) => Array.from(v.matchAll(/"([^"]*)"|'([^']*)'|(\S+)/g), m => m[1] ?? m[2] ?? m[3]);
|
|
38
|
+
exports.tokens = tokens;
|