badmfck-api-server 6.0.8 → 6.0.9
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
CHANGED
|
@@ -713,6 +713,7 @@ git: it holds credentials.
|
|
|
713
713
|
| `badmfck project-token [--hash]` | generate a project token and its hash, or hash an existing token |
|
|
714
714
|
| `badmfck project-config` | build a project config for the deployer step by step (plus a `deploy.json` skeleton) |
|
|
715
715
|
| `badmfck access-config [file]` | create or edit `access.json`: monitor and documentation users, roles, passwords, allowed addresses |
|
|
716
|
+
| `badmfck migrate-configs [folder] [--yes]` | hash the secrets of old `setup.json`, project configs and `access.json` files in place |
|
|
716
717
|
|
|
717
718
|
To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json`.
|
|
718
719
|
|
|
@@ -1037,6 +1038,32 @@ so no restart is needed.
|
|
|
1037
1038
|
|
|
1038
1039
|
---
|
|
1039
1040
|
|
|
1041
|
+
## Upgrading to 6.x
|
|
1042
|
+
|
|
1043
|
+
6.x stores secrets only as hashes and refuses plain text, while 5.x doesn't understand hashes. Upgrade
|
|
1044
|
+
each part and convert its files right after:
|
|
1045
|
+
|
|
1046
|
+
1. **CLI.** Update the CLI on the machines you deploy from. The new CLI also works with old deployers.
|
|
1047
|
+
2. **Deployer.** Upgrade the deployer, then run `npx badmfck migrate-configs <projects folder>`. It
|
|
1048
|
+
picks up the folder's `setup.json` and every project config at once. Add `--yes` to skip the
|
|
1049
|
+
confirmation, for example in a script. It hashes the
|
|
1050
|
+
passwords in `setup.json` and the token of every project config, and renames the old `username`
|
|
1051
|
+
field to `login`. Every hash is computed from the value already in the file, so clients keep their
|
|
1052
|
+
`deploy.json` unchanged.
|
|
1053
|
+
3. **Apps.** Upgrade each app, then convert its `access.json` with the same command. Monitor users
|
|
1054
|
+
keep their passwords; documentation passwords stay as they are. An app that sets `access` as an
|
|
1055
|
+
inline object needs the passwords in that object replaced with hashes (`npx badmfck hash-password`),
|
|
1056
|
+
or moved to a file with `npx badmfck access-config`.
|
|
1057
|
+
|
|
1058
|
+
Before changing anything, `migrate-configs` shows what it will change and flags weak passwords (short,
|
|
1059
|
+
or the same as the login). It copies the originals into a `.pre-hash-backup-*` folder (permissions
|
|
1060
|
+
`700`) and verifies every new hash. Delete that folder once deploys and sign-ins work, because it holds
|
|
1061
|
+
the plain secrets.
|
|
1062
|
+
|
|
1063
|
+
Also removed in 6.x: the old `MysqlService` (use `DBService`).
|
|
1064
|
+
|
|
1065
|
+
---
|
|
1066
|
+
|
|
1040
1067
|
## License
|
|
1041
1068
|
|
|
1042
1069
|
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function MigrateConfigsWizard(folder?: string, assumeYes?: boolean): Promise<void>;
|
|
@@ -0,0 +1,188 @@
|
|
|
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.MigrateConfigsWizard = MigrateConfigsWizard;
|
|
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 MonitorAuth_1 = require("../monitor/MonitorAuth");
|
|
13
|
+
const cliCommon_1 = require("./cliCommon");
|
|
14
|
+
const cliPrompt_1 = require("./cliPrompt");
|
|
15
|
+
const sha256 = (s) => crypto_1.default.createHash("sha256").update(s).digest("hex");
|
|
16
|
+
async function MigrateConfigsWizard(folder, assumeYes = false) {
|
|
17
|
+
const { red, green, yellow, dim, bold } = (0, cliCommon_1.palette)();
|
|
18
|
+
if (!process_1.stdin.isTTY && !assumeYes) {
|
|
19
|
+
console.error(red("✗ migrate-configs asks for confirmation — run it in a terminal, or add --yes."));
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
console.log(bold("\nMove deployer and access files to hashed secrets") + dim(assumeYes ? " (--yes: no confirmation)" : " (nothing is written before you confirm)"));
|
|
23
|
+
console.log(yellow(" Run this right after upgrading to 6.x: older versions don't understand hashes, newer ones refuse plain text.\n"));
|
|
24
|
+
let setupFile, projectsDir, accessFiles;
|
|
25
|
+
if (folder) {
|
|
26
|
+
if (!fs_1.default.existsSync(folder) || !fs_1.default.statSync(folder).isDirectory()) {
|
|
27
|
+
console.error(red(`✗ ${folder} is not a folder.`));
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
projectsDir = folder;
|
|
31
|
+
const inside = path_1.default.join(folder, "setup.json");
|
|
32
|
+
if (fs_1.default.existsSync(inside))
|
|
33
|
+
setupFile = inside;
|
|
34
|
+
else if (assumeYes) {
|
|
35
|
+
setupFile = "";
|
|
36
|
+
console.log(yellow(` ⚠ No setup.json in ${folder} — deployer users are not converted.`));
|
|
37
|
+
}
|
|
38
|
+
else
|
|
39
|
+
setupFile = await (0, cliPrompt_1.ask)(`No setup.json in ${folder}. Deployer setup.json (empty = skip)`, "");
|
|
40
|
+
accessFiles = [];
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const defSetup = ["setup.json", "projects/setup.json"].find(f => fs_1.default.existsSync(f)) ?? "";
|
|
44
|
+
setupFile = await (0, cliPrompt_1.ask)("Deployer setup.json (empty = skip)", defSetup);
|
|
45
|
+
projectsDir = await (0, cliPrompt_1.ask)("Folder with project configs (empty = skip)", setupFile ? path_1.default.dirname(setupFile) : "");
|
|
46
|
+
accessFiles = (0, cliPrompt_1.list)(await (0, cliPrompt_1.ask)("access.json files of apps, comma-separated (empty = skip)", ""));
|
|
47
|
+
}
|
|
48
|
+
const plans = [];
|
|
49
|
+
const readJson = (file) => {
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(fs_1.default.readFileSync(file, "utf-8"));
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
console.log(red(` ✗ ${file}: ${e?.code === "ENOENT" ? "not found" : "not valid JSON"} — skipped`));
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const plain = (v) => typeof v === "string" && v.length > 0 && !PasswordHash_1.PasswordHash.isHash(v);
|
|
59
|
+
if (setupFile) {
|
|
60
|
+
const doc = readJson(setupFile);
|
|
61
|
+
if (doc) {
|
|
62
|
+
const plan = { file: path_1.default.resolve(setupFile), doc, changes: [], notes: [] };
|
|
63
|
+
(doc.users ?? []).forEach((u, i) => {
|
|
64
|
+
if (u && u.login === undefined && typeof u.username === "string") {
|
|
65
|
+
plan.notes.push(`users[${i}]: "username" renamed to "login" (${u.username})`);
|
|
66
|
+
u.login = u.username;
|
|
67
|
+
delete u.username;
|
|
68
|
+
}
|
|
69
|
+
if (u && plain(u.password)) {
|
|
70
|
+
if (u.password === u.login)
|
|
71
|
+
plan.notes.push(`deployer user "${u.login}": the password is the same as the login — change it (npx badmfck hash-password --deployer, and the client's deploy.json)`);
|
|
72
|
+
else if (MonitorAuth_1.MonitorAuth.passwordPolicyError(u.password))
|
|
73
|
+
plan.notes.push(`deployer user "${u.login}": password shorter than 12 characters — change it (npx badmfck hash-password --deployer, and the client's deploy.json)`);
|
|
74
|
+
const secret = sha256(u.login + ":" + u.password);
|
|
75
|
+
plan.changes.push({ file: plan.file, label: `deployer user "${u.login}"`, secret,
|
|
76
|
+
apply: (d, h) => { d.users[i].password = h; }, read: d => d.users[i].password });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
(doc.reviewers ?? []).forEach((r, i) => {
|
|
80
|
+
if (r && plain(r.password)) {
|
|
81
|
+
if (r.password === r.login)
|
|
82
|
+
plan.notes.push(`reviewer "${r.login}": the password is the same as the login — change it later`);
|
|
83
|
+
else if (MonitorAuth_1.MonitorAuth.passwordPolicyError(r.password))
|
|
84
|
+
plan.notes.push(`reviewer "${r.login}": password shorter than 12 characters — change it later`);
|
|
85
|
+
plan.changes.push({ file: plan.file, label: `reviewer "${r.login}"`, secret: r.password,
|
|
86
|
+
apply: (d, h) => { d.reviewers[i].password = h; }, read: d => d.reviewers[i].password });
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
plans.push(plan);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (projectsDir) {
|
|
93
|
+
let names = [];
|
|
94
|
+
try {
|
|
95
|
+
names = fs_1.default.readdirSync(projectsDir).filter(n => n.endsWith(".json")).sort();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
console.log(red(` ✗ ${projectsDir}: not a readable folder — skipped`));
|
|
99
|
+
}
|
|
100
|
+
for (const n of names) {
|
|
101
|
+
const file = path_1.default.resolve(projectsDir, n);
|
|
102
|
+
if (setupFile && file === path_1.default.resolve(setupFile))
|
|
103
|
+
continue;
|
|
104
|
+
if (n.endsWith("_state_.json"))
|
|
105
|
+
continue;
|
|
106
|
+
const doc = readJson(file);
|
|
107
|
+
if (!doc || typeof doc !== "object" || typeof doc.name !== "string" || typeof doc.token !== "string")
|
|
108
|
+
continue;
|
|
109
|
+
const plan = { file, doc, changes: [], notes: [] };
|
|
110
|
+
if (plain(doc.token)) {
|
|
111
|
+
if (doc.token.length < 32)
|
|
112
|
+
plan.notes.push(`project "${doc.name}": short token (${doc.token.length} chars) — consider a new one: npx badmfck project-token`);
|
|
113
|
+
plan.changes.push({ file, label: `project token "${doc.name}"`, secret: doc.token,
|
|
114
|
+
apply: (d, h) => { d.token = h; }, read: d => d.token });
|
|
115
|
+
}
|
|
116
|
+
plans.push(plan);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const f of accessFiles) {
|
|
120
|
+
const doc = readJson(f);
|
|
121
|
+
if (!doc)
|
|
122
|
+
continue;
|
|
123
|
+
const plan = { file: path_1.default.resolve(f), doc, changes: [], notes: [] };
|
|
124
|
+
(doc.monitor ?? []).forEach((u, i) => {
|
|
125
|
+
if (u && plain(u.password)) {
|
|
126
|
+
if (u.password === u.login)
|
|
127
|
+
plan.notes.push(`monitor user "${u.login}": the password is the same as the login — change it later (npx badmfck access-config)`);
|
|
128
|
+
else if (MonitorAuth_1.MonitorAuth.passwordPolicyError(u.password))
|
|
129
|
+
plan.notes.push(`monitor user "${u.login}": password shorter than 12 characters — change it later (npx badmfck access-config)`);
|
|
130
|
+
plan.changes.push({ file: plan.file, label: `monitor user "${u.login}"`, secret: u.password,
|
|
131
|
+
apply: (d, h) => { d.monitor[i].password = h; }, read: d => d.monitor[i].password });
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
plans.push(plan);
|
|
135
|
+
}
|
|
136
|
+
const todo = plans.filter(p => p.changes.length || p.notes.some(n => n.includes("renamed")));
|
|
137
|
+
console.log("\n" + bold("Report"));
|
|
138
|
+
for (const p of plans) {
|
|
139
|
+
const head = p.changes.length ? yellow(`${p.changes.length} to hash`) : green("already hashed");
|
|
140
|
+
console.log(` ${p.file} ${head}`);
|
|
141
|
+
for (const c of p.changes)
|
|
142
|
+
console.log(dim(` • ${c.label}`));
|
|
143
|
+
for (const n of p.notes)
|
|
144
|
+
console.log(yellow(` ⚠ ${n}`));
|
|
145
|
+
}
|
|
146
|
+
if (!todo.length) {
|
|
147
|
+
console.log(green("\n✓ Nothing to change."));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (!assumeYes && !(await (0, cliPrompt_1.yes)(`\nHash ${todo.reduce((n, p) => n + p.changes.length, 0)} value(s) in ${todo.length} file(s)?`, false))) {
|
|
151
|
+
console.log(dim("Nothing was changed."));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
155
|
+
const backupDir = path_1.default.resolve(path_1.default.dirname(todo[0].file), `.pre-hash-backup-${stamp}`);
|
|
156
|
+
fs_1.default.mkdirSync(backupDir, { recursive: true, mode: 0o700 });
|
|
157
|
+
for (const [i, p] of todo.entries()) {
|
|
158
|
+
const copy = path_1.default.join(backupDir, `${i + 1}-${path_1.default.basename(p.file)}`);
|
|
159
|
+
fs_1.default.copyFileSync(p.file, copy);
|
|
160
|
+
fs_1.default.chmodSync(copy, 0o600);
|
|
161
|
+
}
|
|
162
|
+
let failed = 0;
|
|
163
|
+
for (const p of todo) {
|
|
164
|
+
for (const c of p.changes)
|
|
165
|
+
c.apply(p.doc, await PasswordHash_1.PasswordHash.hash(c.secret));
|
|
166
|
+
const tmp = p.file + ".tmp";
|
|
167
|
+
fs_1.default.writeFileSync(tmp, JSON.stringify(p.doc, null, 4) + "\n", { encoding: "utf-8", mode: 0o600 });
|
|
168
|
+
fs_1.default.chmodSync(tmp, 0o600);
|
|
169
|
+
fs_1.default.renameSync(tmp, p.file);
|
|
170
|
+
const back = JSON.parse(fs_1.default.readFileSync(p.file, "utf-8"));
|
|
171
|
+
for (const c of p.changes) {
|
|
172
|
+
if (await PasswordHash_1.PasswordHash.verify(c.secret, c.read(back)))
|
|
173
|
+
continue;
|
|
174
|
+
failed++;
|
|
175
|
+
console.log(red(` ✗ ${p.file}: ${c.label} did not verify — restore it from ${backupDir}`));
|
|
176
|
+
}
|
|
177
|
+
console.log((failed ? yellow : green)(` ✓ ${p.file}`) + dim(" (0600)"));
|
|
178
|
+
}
|
|
179
|
+
console.log("");
|
|
180
|
+
if (failed) {
|
|
181
|
+
console.log(red(`✗ ${failed} value(s) did not verify. The originals are in ${backupDir}.`));
|
|
182
|
+
process.exit(1);
|
|
183
|
+
}
|
|
184
|
+
console.log(green("✓ Done. A running 6.x deployer and apps pick the files up without a restart."));
|
|
185
|
+
console.log(yellow(` The originals (with the plain secrets) are in ${backupDir}.`));
|
|
186
|
+
console.log(yellow(" Check that deploys and monitor sign-in work, then delete that folder:"));
|
|
187
|
+
console.log(` rm -rf ${JSON.stringify(backupDir)}`);
|
|
188
|
+
}
|
|
@@ -10,6 +10,7 @@ const BackupManager_1 = require("./BackupManager");
|
|
|
10
10
|
const HashPassword_1 = require("./HashPassword");
|
|
11
11
|
const ProjectConfigWizard_1 = require("./ProjectConfigWizard");
|
|
12
12
|
const AccessConfigWizard_1 = require("./AccessConfigWizard");
|
|
13
|
+
const MigrateConfigsWizard_1 = require("./MigrateConfigsWizard");
|
|
13
14
|
function usage() {
|
|
14
15
|
console.error("Usage:");
|
|
15
16
|
console.error(" badmfck deploy [config.json] — build + upload the current project");
|
|
@@ -21,6 +22,7 @@ function usage() {
|
|
|
21
22
|
console.error(" badmfck project-token [--hash] — new project token + its hash (or hash an existing one)");
|
|
22
23
|
console.error(" badmfck project-config — build a project config for the deployer, step by step");
|
|
23
24
|
console.error(" badmfck access-config [file] — create or edit access.json (found in the project's code if omitted)");
|
|
25
|
+
console.error(" badmfck migrate-configs [folder] [--yes] — hash the secrets of old setup.json, project configs and access.json files");
|
|
24
26
|
console.error("");
|
|
25
27
|
console.error("Config path is optional (auto-discovers deploy/ folder → deploy.json when omitted).");
|
|
26
28
|
console.error("Via npm, prefix args with -- e.g. npm run deploy -- deploy/custom.json");
|
|
@@ -107,6 +109,16 @@ async function main() {
|
|
|
107
109
|
await (0, HashPassword_1.HashPassword)(rest.includes("--deployer") ? "deployer" : "monitor");
|
|
108
110
|
break;
|
|
109
111
|
}
|
|
112
|
+
case "migrate-configs": {
|
|
113
|
+
const assumeYes = rest.includes("--yes") || rest.includes("-y");
|
|
114
|
+
const folders = rest.filter(a => a !== "--yes" && a !== "-y");
|
|
115
|
+
if (folders.length > 1 || folders.some(a => a.startsWith("-"))) {
|
|
116
|
+
console.error("Usage: badmfck migrate-configs [folder] [--yes]");
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
await (0, MigrateConfigsWizard_1.MigrateConfigsWizard)(folders[0], assumeYes);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
110
122
|
case "access-config": {
|
|
111
123
|
if (rest.length > 1) {
|
|
112
124
|
console.error("access-config takes at most one argument: the path to the access file.");
|