badmfck-api-server 6.0.1 → 6.0.3
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 +162 -10
- package/dist/apiServer/APIService.js +2 -0
- package/dist/apiServer/deployment/ConfigService.d.ts +23 -66
- package/dist/apiServer/deployment/ConfigService.js +20 -77
- package/dist/apiServer/deployment/Deploy.js +18 -0
- package/dist/apiServer/deployment/Deployer.js +5 -5
- package/dist/apiServer/deployment/DeployerService.d.ts +66 -4
- package/dist/apiServer/deployment/DeployerService.js +408 -43
- package/dist/apiServer/deployment/HashPassword.d.ts +1 -0
- package/dist/apiServer/deployment/HashPassword.js +34 -0
- package/dist/apiServer/deployment/Notifier.d.ts +14 -0
- package/dist/apiServer/deployment/Notifier.js +22 -0
- package/dist/apiServer/deployment/ReviewStore.d.ts +62 -0
- package/dist/apiServer/deployment/ReviewStore.js +186 -0
- package/dist/apiServer/deployment/SecretsInjector.d.ts +21 -0
- package/dist/apiServer/deployment/SecretsInjector.js +188 -0
- package/dist/apiServer/deployment/cli.js +10 -0
- package/dist/apiServer/review/Review.d.ts +23 -0
- package/dist/apiServer/review/Review.js +149 -0
- package/dist/apiServer/review/ReviewAuth.d.ts +56 -0
- package/dist/apiServer/review/ReviewAuth.js +147 -0
- package/dist/apiServer/review/ReviewDiff.d.ts +43 -0
- package/dist/apiServer/review/ReviewDiff.js +175 -0
- package/dist/apiServer/review/index.html +501 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,8 +28,9 @@ Node.js 21+ is required.
|
|
|
28
28
|
11. [Deployer and CLI](#deployer-and-cli)
|
|
29
29
|
12. [Blue-green deployments](#blue-green-deployments)
|
|
30
30
|
13. [Backups and restore](#backups-and-restore)
|
|
31
|
-
14. [
|
|
32
|
-
15. [
|
|
31
|
+
14. [Deploy review](#deploy-review)
|
|
32
|
+
15. [Options reference](#options-reference)
|
|
33
|
+
16. [Security](#security)
|
|
33
34
|
|
|
34
35
|
---
|
|
35
36
|
|
|
@@ -321,6 +322,7 @@ The library protects four things, each in its own way:
|
|
|
321
322
|
| Monitor (`sm/*`) | your operations team | password + mandatory TOTP 2FA, then a session token |
|
|
322
323
|
| Documentation (`doc/*`) | developers and partners | login and password (a static token) |
|
|
323
324
|
| Deployer (`pckg/*`) | CI and developers | deployer user + project token |
|
|
325
|
+
| Deploy review (`review/*`) | reviewers | personal emailed link + login, password and TOTP 2FA |
|
|
324
326
|
|
|
325
327
|
The library doesn't manage the users of your application. That's the job of your interceptor and
|
|
326
328
|
database. The rest of this section is about the built-in consoles.
|
|
@@ -458,11 +460,16 @@ documentation users their own passwords, never ones they use elsewhere.
|
|
|
458
460
|
### 5. Deployer
|
|
459
461
|
|
|
460
462
|
- **Server.** The deployer server keeps `setup.json` users as hashes (`hash-password --deployer`).
|
|
463
|
+
- **Addresses.** `allowed_ips` in `setup.json` limits which addresses can use the deployer at all.
|
|
461
464
|
- **Client.** The deploying machine keeps the login and the plain password in `deploy.json`, which
|
|
462
465
|
never leaves that machine and must stay out of git.
|
|
463
466
|
- **What goes over the wire.** The CLI sends `Authorization: Bearer sha256(login:password)` and
|
|
464
467
|
`X-Deployer-Login`.
|
|
465
|
-
- **Project token.** Deploying, switching and restoring also require the project's own `token`.
|
|
468
|
+
- **Project token.** Deploying, switching and restoring also require the project's own `token`. The
|
|
469
|
+
project config on the server holds only its hash. Run `npx badmfck project-token`: it prints a new
|
|
470
|
+
random token for `deploy.json` and its hash for the project config. To keep an existing token, run
|
|
471
|
+
`npx badmfck project-token --hash`. A token stored in plain text is refused with "project token is
|
|
472
|
+
built incorrectly", and a startup error names the affected projects.
|
|
466
473
|
- **CLI version.** Users stored as a hash need a CLI from version 6 or later.
|
|
467
474
|
|
|
468
475
|
---
|
|
@@ -541,16 +548,28 @@ getDefaultOptions({
|
|
|
541
548
|
`npx badmfck hash-password --deployer`.
|
|
542
549
|
|
|
543
550
|
```jsonc
|
|
544
|
-
{
|
|
551
|
+
{
|
|
552
|
+
"users": [ { "login": "ci", "password": "scrypt$16384$8$1$…" } ],
|
|
553
|
+
"allowed_ips": ["203.0.113.7", "10.8.0.0/24"] // who may deploy; IPs and CIDR subnets, IPv4/IPv6
|
|
554
|
+
}
|
|
545
555
|
```
|
|
546
556
|
|
|
557
|
+
`allowed_ips` is checked before the credentials, for deploy, activate, status, backups and restore.
|
|
558
|
+
Other addresses get `403`. Without the list the deployer accepts any address, and a warning is logged.
|
|
559
|
+
`setup.json` is reloaded on change, so the list can be edited without a restart. An invalid entry
|
|
560
|
+
rejects the whole change, and the previous setup stays in force.
|
|
561
|
+
|
|
562
|
+
> **Behind nginx,** pass the real client address: `proxy_set_header X-Real-IP $remote_addr;`. Without
|
|
563
|
+
> it, every request appears to come from `127.0.0.1`. Never add `127.0.0.1` to `allowed_ips` on a
|
|
564
|
+
> deployer behind a proxy on the same host: that would let every request through.
|
|
565
|
+
|
|
547
566
|
A project config (`projects_path/<name>.json`):
|
|
548
567
|
|
|
549
568
|
```jsonc
|
|
550
569
|
{
|
|
551
570
|
"name": "my-service",
|
|
552
571
|
"destination": "/var/node/my-service",
|
|
553
|
-
"token": "
|
|
572
|
+
"token": "scrypt$16384$8$1$…", // hash of the project token: npx badmfck project-token
|
|
554
573
|
"unpack": true, "install": true, "pm2": true,
|
|
555
574
|
"backup": true, // copy the current version aside before each deploy (see Backups)
|
|
556
575
|
"migrate": false, // run `npm run migrate` before the process starts
|
|
@@ -558,6 +577,43 @@ A project config (`projects_path/<name>.json`):
|
|
|
558
577
|
}
|
|
559
578
|
```
|
|
560
579
|
|
|
580
|
+
#### Keeping secrets out of the deployed code
|
|
581
|
+
|
|
582
|
+
By default, `config` values are written into the shipped files in place of `_{{KEY}}_`. That puts
|
|
583
|
+
passwords into the code, into both blue-green slots and into every backup. Add `secrets` to the project
|
|
584
|
+
config to avoid this:
|
|
585
|
+
|
|
586
|
+
```jsonc
|
|
587
|
+
"secrets": {} // uses "secrets_dir" from setup.json
|
|
588
|
+
"secrets": { "dir": "/etc/node-secrets" } // → /etc/node-secrets/<project>[-<slot>].json
|
|
589
|
+
"secrets": { "file": "/etc/node-secrets/my.json" }
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
The deployer writes the values (merged for the slot) into that file, with permissions `600` and
|
|
593
|
+
outside the deploy folder, and rewrites each quoted placeholder in `.js` files as a read of the file:
|
|
594
|
+
|
|
595
|
+
```js
|
|
596
|
+
DB_PASSWORD: "_{{db_password}}_" // in the archive
|
|
597
|
+
DB_PASSWORD: (function(k){ … reads /etc/node-secrets/my-blue.json … })("db_password") // on the server
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
- **No code changes.** The application gets exactly the strings it got before, so `parseInt("_{{port}}_")`
|
|
601
|
+
still works. The file is read once per process.
|
|
602
|
+
- **Restarts.** pm2 restarts, `migrate` and cluster workers keep working, because the file stays in place.
|
|
603
|
+
- **Backups and restore.** Backups and archives contain only the file path. A restored version reads
|
|
604
|
+
the current values instead of the ones from its deploy.
|
|
605
|
+
- **Missing key.** If a key is missing from the file, the start fails with a clear error instead of
|
|
606
|
+
running with a placeholder.
|
|
607
|
+
- **Same user.** The secrets file belongs to the deployer's user and is readable only by it. pm2,
|
|
608
|
+
started by the deployer, runs the app as that same user. A project whose pm2 attributes switch the
|
|
609
|
+
user (`--uid`, `--gid`) is refused before anything changes, since the app couldn't read the file.
|
|
610
|
+
The file is also read back before the app is started.
|
|
611
|
+
- **Start with a blue-green project.** If the new version fails to start, the inactive slot fails and
|
|
612
|
+
traffic stays on the old one until you `activate`.
|
|
613
|
+
- **Fallback to plain substitution.** Values are still written into the file for `.json`, `.env`,
|
|
614
|
+
unquoted placeholders, templates with `${…}`, ES-module projects, and any file that wouldn't compile
|
|
615
|
+
after the rewrite. Each deploy logs which files still hold values.
|
|
616
|
+
|
|
561
617
|
**In your project**, create `deploy.json` or a `deploy/` folder with one or more configs. Keep it out of
|
|
562
618
|
git: it holds credentials.
|
|
563
619
|
|
|
@@ -565,7 +621,7 @@ git: it holds credentials.
|
|
|
565
621
|
{
|
|
566
622
|
"name": "my-service",
|
|
567
623
|
"host": "https://deployer.example.com/api/pckg/add",
|
|
568
|
-
"token": "<
|
|
624
|
+
"token": "<project token, printed by npx badmfck project-token>",
|
|
569
625
|
"username": "ci",
|
|
570
626
|
"password": "<your password>",
|
|
571
627
|
"includes": ["certs", { "src": "assets/logo.png", "dest": "bin/logo.png" }],
|
|
@@ -591,6 +647,7 @@ git: it holds credentials.
|
|
|
591
647
|
| `badmfck backups [config]` | list backups |
|
|
592
648
|
| `badmfck restore [config] [backup] [--yes]` | restore a backup |
|
|
593
649
|
| `badmfck hash-password [--deployer]` | hash a password for the monitor or `setup.json` |
|
|
650
|
+
| `badmfck project-token [--hash]` | generate a project token and its hash, or hash an existing token |
|
|
594
651
|
|
|
595
652
|
To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json`.
|
|
596
653
|
|
|
@@ -602,7 +659,11 @@ To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json
|
|
|
602
659
|
(`/var`, `/etc`…), the deployer's own folders, and a folder whose wipe would delete another project.
|
|
603
660
|
- Dependencies are installed with `npm ci --ignore-scripts`. `migrate` is the only script the deployer
|
|
604
661
|
runs.
|
|
605
|
-
- Every deploy, switch and restore needs a deployer user and the project token.
|
|
662
|
+
- Every deploy, switch and restore needs a deployer user and the project token. Both are stored on
|
|
663
|
+
the server only as hashes.
|
|
664
|
+
- `allowed_ips` limits which addresses can reach the deployer at all.
|
|
665
|
+
- With [deploy review](#deploy-review), uploads wait for approval by reviewers who sign in with a
|
|
666
|
+
password and 2FA.
|
|
606
667
|
|
|
607
668
|
---
|
|
608
669
|
|
|
@@ -640,7 +701,7 @@ Add `bluegreen` and `nginx` blocks to the project config on the deployer:
|
|
|
640
701
|
{
|
|
641
702
|
"name": "my-service",
|
|
642
703
|
"destination": "/var/node/my-service", // slots go to …/blue and …/green
|
|
643
|
-
"token": "
|
|
704
|
+
"token": "scrypt$…", "unpack": true, "install": true, "pm2": true, "backup": true,
|
|
644
705
|
|
|
645
706
|
"config": { "DB_HOST": "…" }, // shared by both slots
|
|
646
707
|
|
|
@@ -743,7 +804,98 @@ How a restore works:
|
|
|
743
804
|
Restoring a standby slot changes nothing until you `activate` it.
|
|
744
805
|
|
|
745
806
|
Configuration values are baked in at deploy time. A restored version comes back with the secrets it was
|
|
746
|
-
deployed with.
|
|
807
|
+
deployed with, unless the project uses a [secrets file](#keeping-secrets-out-of-the-deployed-code).
|
|
808
|
+
|
|
809
|
+
---
|
|
810
|
+
|
|
811
|
+
## Deploy review
|
|
812
|
+
|
|
813
|
+
A project can require approval before anything reaches the server. The upload is held, each reviewer
|
|
814
|
+
gets a personal link by email, and the deploy runs only after approval. That way stolen deploy
|
|
815
|
+
credentials alone can't change production.
|
|
816
|
+
|
|
817
|
+
### Setup
|
|
818
|
+
|
|
819
|
+
Reviewers are defined in the deployer's `setup.json`. Their passwords are hashes made with
|
|
820
|
+
`npx badmfck hash-password`, and they set up 2FA at the first sign-in. `review_url` is the address of
|
|
821
|
+
the review page, as reviewers reach it:
|
|
822
|
+
|
|
823
|
+
```jsonc
|
|
824
|
+
{
|
|
825
|
+
"reviewers": [
|
|
826
|
+
{ "login": "lead", "password": "scrypt$16384$8$1$…", "email": "lead@example.com" },
|
|
827
|
+
{ "login": "cto", "password": "scrypt$16384$8$1$…", "email": "cto@example.com" }
|
|
828
|
+
],
|
|
829
|
+
"review_url": "https://deployer.example.com/api/review/html",
|
|
830
|
+
"notifier": { "URL": "…", "KEY": "…" }, // sends the emails
|
|
831
|
+
"allowed_ips": ["10.8.0.0/24"] // the review page is behind it too, e.g. the VPN range
|
|
832
|
+
}
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
A project opts in from its config:
|
|
836
|
+
|
|
837
|
+
```jsonc
|
|
838
|
+
"review": { "reviewers": ["lead", "cto"], "mode": "all", "ttlHours": 24 }
|
|
839
|
+
```
|
|
840
|
+
|
|
841
|
+
- **`mode`.** With `"any"`, one accept is enough; with `"all"`, every reviewer must accept. Either
|
|
842
|
+
way, a single decline rejects the upload.
|
|
843
|
+
- **`ttlHours`.** How long the links stay valid: 1 to 168 hours, 24 by default.
|
|
844
|
+
|
|
845
|
+
If a project's review can't work (an unknown reviewer, no `review_url`, no notifier), the deployer says
|
|
846
|
+
so at startup and refuses that project's uploads until it is fixed.
|
|
847
|
+
|
|
848
|
+
### How it works
|
|
849
|
+
|
|
850
|
+
1. **`badmfck deploy`** uploads as usual. The archive goes through all the usual checks and is then
|
|
851
|
+
held. The CLI prints `WAITING FOR REVIEW`, and production is untouched.
|
|
852
|
+
2. **Each reviewer gets an email** with a personal link. The link alone is not enough: it opens a
|
|
853
|
+
sign-in with login, password and a 2FA code.
|
|
854
|
+
3. **The page shows the changes** compared with the last deployed version: version before and
|
|
855
|
+
after, dependency changes in `package.json`, and added, removed and changed files with a
|
|
856
|
+
line-by-line diff.
|
|
857
|
+
4. **Accept** runs the normal deploy (backup, migrations, pm2, nginx) from the stored archive, after
|
|
858
|
+
checking that its SHA-256 still matches what the reviewers saw. For blue-green, the new version
|
|
859
|
+
lands in the inactive slot, and `badmfck activate` still switches traffic. **Decline** deletes the
|
|
860
|
+
archive.
|
|
861
|
+
5. **A new upload** replaces the pending one. Every old link and every open review session stop
|
|
862
|
+
working, and reviewers get new links.
|
|
863
|
+
|
|
864
|
+
Undecided uploads expire after `ttlHours`, and their archive is removed.
|
|
865
|
+
|
|
866
|
+
### What the diff shows and hides
|
|
867
|
+
|
|
868
|
+
The comparison uses the **raw archives**: the stored archive of the last successful deploy and the
|
|
869
|
+
new one. Deployed folders are never read, because config values (passwords) are substituted there.
|
|
870
|
+
Files such as `.env*`, `*.pem` and `*.key` are listed, but their content is never shown. Binary and
|
|
871
|
+
very large files are listed with their sizes. The first reviewed deploy of a project has nothing to
|
|
872
|
+
compare with, so every file is shown as added.
|
|
873
|
+
|
|
874
|
+
### Security notes
|
|
875
|
+
|
|
876
|
+
- **Personal links.** A link works only for the reviewer it was sent to. The token is in the URL
|
|
877
|
+
fragment (`#…`), which never reaches the server logs, and the page removes it from the address bar.
|
|
878
|
+
- **Sessions.** A session lasts 15 minutes without activity and 1 hour at most. It is bound to one
|
|
879
|
+
upload: a new upload ends it.
|
|
880
|
+
- **Lockout.** 10 failed sign-ins lock the reviewer for 30 minutes.
|
|
881
|
+
- **Mail scanners.** Opening the link changes nothing. Accept and decline need a signed-in click, so
|
|
882
|
+
a mail scanner that "visits" links can't approve anything.
|
|
883
|
+
- **Separation of duties.** Give the review to someone other than the person who deploys. That's
|
|
884
|
+
what change control (for example PCI DSS 6.5) expects.
|
|
885
|
+
|
|
886
|
+
### Audit trail and maintenance
|
|
887
|
+
|
|
888
|
+
All review data lives in `<projects_path>/.review/`, with permissions `600`:
|
|
889
|
+
|
|
890
|
+
| File | Content |
|
|
891
|
+
|---|---|
|
|
892
|
+
| `<project>.history.jsonl` | every upload, decision and result, with IP addresses and the archive SHA-256 |
|
|
893
|
+
| `<project>.pending.*` | the archive waiting for review and its state |
|
|
894
|
+
| `<project>.last.tgz` | the raw archive of the last successful deploy, the "before" of the next diff |
|
|
895
|
+
| `reviewers-2fa.json` | reviewers' 2FA enrollments |
|
|
896
|
+
|
|
897
|
+
To reset a reviewer's 2FA, delete their entry from `reviewers-2fa.json`. It is read at every sign-in,
|
|
898
|
+
so no restart is needed.
|
|
747
899
|
|
|
748
900
|
---
|
|
749
901
|
|
|
@@ -789,7 +941,7 @@ deployed with.
|
|
|
789
941
|
- **Client IP.** Proxy headers are trusted only from `trustedProxies`, so clients can't spoof their
|
|
790
942
|
address.
|
|
791
943
|
- **Deploys.** Archives are checked for path traversal and escaping symlinks, package install scripts
|
|
792
|
-
are disabled, and every deploy needs a user and the project token.
|
|
944
|
+
are disabled, and every deploy needs a user and the project token, both stored only as hashes.
|
|
793
945
|
- **Blue-green.** Applications get a read-only status token; deploy credentials never reach them.
|
|
794
946
|
- **WebSocket.** Origin checks, authorization within a time limit, and limits on message size, message
|
|
795
947
|
rate and connections. A malformed frame can't crash the process.
|
|
@@ -66,6 +66,7 @@ const AccessStore_1 = require("./monitor/AccessStore");
|
|
|
66
66
|
const MonitorAuth_1 = require("./monitor/MonitorAuth");
|
|
67
67
|
const ClientIp_1 = require("./helper/ClientIp");
|
|
68
68
|
const Deployer_1 = require("./deployment/Deployer");
|
|
69
|
+
const Review_1 = require("./review/Review");
|
|
69
70
|
const ws_1 = require("ws");
|
|
70
71
|
const WebSocketServer_1 = require("./WebSocketServer");
|
|
71
72
|
let nextLogID = 0;
|
|
@@ -198,6 +199,7 @@ class APIService extends BaseService_1.BaseService {
|
|
|
198
199
|
this.deployer.init();
|
|
199
200
|
this.options.endpoints = [
|
|
200
201
|
new Deployer_1.Deployer(),
|
|
202
|
+
new Review_1.Review(this.deployer, this.options),
|
|
201
203
|
new Documentation_1.Documentation(this.options),
|
|
202
204
|
new Monitor_1.Monitor(this.options),
|
|
203
205
|
];
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import Signal, { Req } from "badmfck-signal";
|
|
2
2
|
import { BaseService } from "../BaseService";
|
|
3
3
|
import { ValidationModel } from "../helper/Validator";
|
|
4
|
-
import { IError } from "../structures/Interfaces";
|
|
5
4
|
declare const _ProjectState: {
|
|
6
5
|
readonly active: "";
|
|
7
6
|
readonly $__active_values: readonly ["blue", "green"];
|
|
@@ -61,6 +60,21 @@ declare const _TConfig: {
|
|
|
61
60
|
readonly $__upstream_name_optional: true;
|
|
62
61
|
};
|
|
63
62
|
readonly $__nginx_optional: true;
|
|
63
|
+
readonly review: {
|
|
64
|
+
readonly reviewers: readonly [""];
|
|
65
|
+
readonly mode: "";
|
|
66
|
+
readonly $__mode_values: readonly ["any", "all"];
|
|
67
|
+
readonly ttlHours: 24;
|
|
68
|
+
readonly $__ttlHours_optional: true;
|
|
69
|
+
};
|
|
70
|
+
readonly $__review_optional: true;
|
|
71
|
+
readonly secrets: {
|
|
72
|
+
readonly dir: "";
|
|
73
|
+
readonly $__dir_optional: true;
|
|
74
|
+
readonly file: "";
|
|
75
|
+
readonly $__file_optional: true;
|
|
76
|
+
};
|
|
77
|
+
readonly $__secrets_optional: true;
|
|
64
78
|
};
|
|
65
79
|
export type IConfig = ValidationModel<typeof _TConfig>;
|
|
66
80
|
export declare const REQ_DEPLOYMENT_CONFIG: Req<void, Map<string, {
|
|
@@ -93,69 +107,16 @@ export declare const REQ_DEPLOYMENT_CONFIG: Req<void, Map<string, {
|
|
|
93
107
|
domain?: string | undefined;
|
|
94
108
|
upstream_name?: string | undefined;
|
|
95
109
|
} | undefined;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
description: string;
|
|
101
|
-
token: string;
|
|
102
|
-
unpack: boolean;
|
|
103
|
-
ignoreDirectoryClean: boolean;
|
|
104
|
-
backup: boolean;
|
|
105
|
-
install: boolean;
|
|
106
|
-
migrate?: boolean | undefined;
|
|
107
|
-
pm2: boolean;
|
|
108
|
-
pm2_attributes?: string[] | undefined;
|
|
109
|
-
chmod: number;
|
|
110
|
-
email: string[];
|
|
111
|
-
phone: string[];
|
|
112
|
-
liveness?: string | undefined;
|
|
113
|
-
readiness?: string | undefined;
|
|
114
|
-
config: {};
|
|
115
|
-
bluegreen?: {
|
|
116
|
-
blue_config: {};
|
|
117
|
-
blue_pm2_attributes?: string[] | undefined;
|
|
118
|
-
green_config: {};
|
|
119
|
-
green_pm2_attributes?: string[] | undefined;
|
|
120
|
-
on_switch?: string[] | undefined;
|
|
121
|
-
} | undefined;
|
|
122
|
-
nginx?: {
|
|
123
|
-
config_dir?: string | undefined;
|
|
124
|
-
domain?: string | undefined;
|
|
125
|
-
upstream_name?: string | undefined;
|
|
126
|
-
} | undefined;
|
|
127
|
-
}, IError | {
|
|
128
|
-
name: string;
|
|
129
|
-
destination: string;
|
|
130
|
-
description: string;
|
|
131
|
-
token: string;
|
|
132
|
-
unpack: boolean;
|
|
133
|
-
ignoreDirectoryClean: boolean;
|
|
134
|
-
backup: boolean;
|
|
135
|
-
install: boolean;
|
|
136
|
-
migrate?: boolean | undefined;
|
|
137
|
-
pm2: boolean;
|
|
138
|
-
pm2_attributes?: string[] | undefined;
|
|
139
|
-
chmod: number;
|
|
140
|
-
email: string[];
|
|
141
|
-
phone: string[];
|
|
142
|
-
liveness?: string | undefined;
|
|
143
|
-
readiness?: string | undefined;
|
|
144
|
-
config: {};
|
|
145
|
-
bluegreen?: {
|
|
146
|
-
blue_config: {};
|
|
147
|
-
blue_pm2_attributes?: string[] | undefined;
|
|
148
|
-
green_config: {};
|
|
149
|
-
green_pm2_attributes?: string[] | undefined;
|
|
150
|
-
on_switch?: string[] | undefined;
|
|
110
|
+
review?: {
|
|
111
|
+
reviewers: string[];
|
|
112
|
+
mode: "all" | "any";
|
|
113
|
+
ttlHours?: number | undefined;
|
|
151
114
|
} | undefined;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
upstream_name?: string | undefined;
|
|
115
|
+
secrets?: {
|
|
116
|
+
dir?: string | undefined;
|
|
117
|
+
file?: string | undefined;
|
|
156
118
|
} | undefined;
|
|
157
|
-
}
|
|
158
|
-
export declare const REQ_DEPLOYMENT_CONFIG_DELETE: Req<string, boolean | IError>;
|
|
119
|
+
}>>;
|
|
159
120
|
export declare const S_CONFIG_UPDATED: Signal<void>;
|
|
160
121
|
export declare class ConfigService extends BaseService {
|
|
161
122
|
private configurations;
|
|
@@ -166,10 +127,6 @@ export declare class ConfigService extends BaseService {
|
|
|
166
127
|
private _lastConfigHash;
|
|
167
128
|
constructor(dir: string);
|
|
168
129
|
init(): Promise<void>;
|
|
169
|
-
configurationDelete(name: string): Promise<IError | boolean>;
|
|
170
|
-
configurationAdd(req: IConfig): Promise<IError | IConfig>;
|
|
171
130
|
getConfig(): void;
|
|
172
|
-
validateRequest(data: IConfig): Promise<IError | undefined>;
|
|
173
|
-
checkName(name: string): IError | undefined;
|
|
174
131
|
}
|
|
175
132
|
export {};
|
|
@@ -36,15 +36,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ConfigService = exports.S_CONFIG_UPDATED = exports.
|
|
39
|
+
exports.ConfigService = exports.S_CONFIG_UPDATED = exports.REQ_DEPLOYMENT_CONFIG = void 0;
|
|
40
40
|
const fs_1 = __importDefault(require("fs"));
|
|
41
41
|
const path_1 = __importDefault(require("path"));
|
|
42
42
|
const crypto_1 = __importDefault(require("crypto"));
|
|
43
43
|
const badmfck_signal_1 = __importStar(require("badmfck-signal"));
|
|
44
44
|
const BaseService_1 = require("../BaseService");
|
|
45
|
-
const Validator_1 = require("../helper/Validator");
|
|
46
45
|
const LogService_1 = require("../LogService");
|
|
47
|
-
const
|
|
46
|
+
const PasswordHash_1 = require("../helper/PasswordHash");
|
|
48
47
|
const _ProjectState = {
|
|
49
48
|
active: "",
|
|
50
49
|
$__active_values: ["blue", "green"],
|
|
@@ -103,10 +102,23 @@ const _TConfig = {
|
|
|
103
102
|
$__upstream_name_optional: true,
|
|
104
103
|
},
|
|
105
104
|
$__nginx_optional: true,
|
|
105
|
+
review: {
|
|
106
|
+
reviewers: [""],
|
|
107
|
+
mode: "",
|
|
108
|
+
$__mode_values: ["any", "all"],
|
|
109
|
+
ttlHours: 24,
|
|
110
|
+
$__ttlHours_optional: true,
|
|
111
|
+
},
|
|
112
|
+
$__review_optional: true,
|
|
113
|
+
secrets: {
|
|
114
|
+
dir: "",
|
|
115
|
+
$__dir_optional: true,
|
|
116
|
+
file: "",
|
|
117
|
+
$__file_optional: true,
|
|
118
|
+
},
|
|
119
|
+
$__secrets_optional: true,
|
|
106
120
|
};
|
|
107
121
|
exports.REQ_DEPLOYMENT_CONFIG = new badmfck_signal_1.Req();
|
|
108
|
-
exports.REQ_DEPLOYMENT_CONFIG_ADD = new badmfck_signal_1.Req();
|
|
109
|
-
exports.REQ_DEPLOYMENT_CONFIG_DELETE = new badmfck_signal_1.Req();
|
|
110
122
|
exports.S_CONFIG_UPDATED = new badmfck_signal_1.default();
|
|
111
123
|
class ConfigService extends BaseService_1.BaseService {
|
|
112
124
|
configurations = new Map();
|
|
@@ -122,8 +134,6 @@ class ConfigService extends BaseService_1.BaseService {
|
|
|
122
134
|
async init() {
|
|
123
135
|
super.init();
|
|
124
136
|
exports.REQ_DEPLOYMENT_CONFIG.listener = async (req) => this.configurations;
|
|
125
|
-
exports.REQ_DEPLOYMENT_CONFIG_ADD.listener = async (req) => this.configurationAdd(req);
|
|
126
|
-
exports.REQ_DEPLOYMENT_CONFIG_DELETE.listener = async (name) => this.configurationDelete(name);
|
|
127
137
|
if (!fs_1.default.existsSync(this.dir))
|
|
128
138
|
fs_1.default.mkdirSync(this.dir, { recursive: true });
|
|
129
139
|
this.getConfig();
|
|
@@ -139,32 +149,6 @@ class ConfigService extends BaseService_1.BaseService {
|
|
|
139
149
|
});
|
|
140
150
|
setInterval(() => this.getConfig(), 60_000).unref();
|
|
141
151
|
}
|
|
142
|
-
async configurationDelete(name) {
|
|
143
|
-
try {
|
|
144
|
-
fs_1.default.unlinkSync(path_1.default.resolve(this.dir, name + ".json"));
|
|
145
|
-
}
|
|
146
|
-
catch (e) {
|
|
147
|
-
return { code: 33, message: "Can't delete configuration file." };
|
|
148
|
-
}
|
|
149
|
-
this.getConfig();
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
async configurationAdd(req) {
|
|
153
|
-
const err = await this.validateRequest(req);
|
|
154
|
-
if (DefaultErrors_1.ErrorUtils.isError(err))
|
|
155
|
-
return err;
|
|
156
|
-
try {
|
|
157
|
-
fs_1.default.writeFileSync(path_1.default.resolve(this.dir, req.name + ".json"), JSON.stringify(req));
|
|
158
|
-
}
|
|
159
|
-
catch (e) {
|
|
160
|
-
return { code: 31, message: "Can't write configuration file." };
|
|
161
|
-
}
|
|
162
|
-
this.getConfig();
|
|
163
|
-
const conf = this.configurations.get(req.name);
|
|
164
|
-
if (!conf)
|
|
165
|
-
return { code: 32, message: "Can't read configuration file." };
|
|
166
|
-
return conf;
|
|
167
|
-
}
|
|
168
152
|
getConfig() {
|
|
169
153
|
if (this.busy) {
|
|
170
154
|
this.retake = true;
|
|
@@ -198,6 +182,9 @@ class ConfigService extends BaseService_1.BaseService {
|
|
|
198
182
|
this.configurations = temp;
|
|
199
183
|
exports.S_CONFIG_UPDATED.invoke();
|
|
200
184
|
(0, LogService_1.logWarn)("Configurations updated");
|
|
185
|
+
const plain = Array.from(temp.values()).filter((c) => c?.name && !PasswordHash_1.PasswordHash.isHash(c.token)).map((c) => c.name);
|
|
186
|
+
if (plain.length)
|
|
187
|
+
(0, LogService_1.logError)(`Deployer: project token stored as plain text for ${plain.map(n => '"' + n + '"').join(", ")} — these projects can't be deployed; store a hash instead: npx badmfck project-token --hash`);
|
|
201
188
|
}
|
|
202
189
|
}
|
|
203
190
|
}
|
|
@@ -210,49 +197,5 @@ class ConfigService extends BaseService_1.BaseService {
|
|
|
210
197
|
this.getConfig();
|
|
211
198
|
}
|
|
212
199
|
}
|
|
213
|
-
async validateRequest(data) {
|
|
214
|
-
const validationErrors = await Validator_1.Validator.validateStructure(_TConfig, data);
|
|
215
|
-
if (validationErrors && validationErrors.length > 0)
|
|
216
|
-
return { ...DefaultErrors_1.default.INVALID_DATA, stack: validationErrors, details: "Invalid configuration data." };
|
|
217
|
-
const errors = [];
|
|
218
|
-
const cname = this.checkName(data.name);
|
|
219
|
-
if (DefaultErrors_1.ErrorUtils.isError(cname))
|
|
220
|
-
errors.push(cname);
|
|
221
|
-
let dest = data.destination.replace(/[^a-zA-Z0-9.\/_\-`~!]/g, "");
|
|
222
|
-
dest = dest.replace("./", "").replace("../", "").replace("..\\", "").replace(".\\", "");
|
|
223
|
-
if (dest !== data.destination)
|
|
224
|
-
errors.push({ code: 22, message: "Incorrect destination." });
|
|
225
|
-
if (data.email) {
|
|
226
|
-
if (typeof data.email === "string") {
|
|
227
|
-
if (Validator_1.Validator.validateValue(data.email, { type: "string", min: 5, max: 128 }) !== Validator_1.ValidationReport.OK)
|
|
228
|
-
errors.push({ code: 29, message: "Invalid email." });
|
|
229
|
-
}
|
|
230
|
-
else if (Array.isArray(data.email)) {
|
|
231
|
-
for (let i of data.email) {
|
|
232
|
-
if (Validator_1.Validator.validateValue(i, { type: "string", min: 5, max: 128 }) !== Validator_1.ValidationReport.OK)
|
|
233
|
-
errors.push({ code: 29, message: "Invalid email." });
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
if (data.phone) {
|
|
238
|
-
if (typeof data.phone === "string") {
|
|
239
|
-
if (Validator_1.Validator.validateValue(data.phone, { type: "string", min: 9, max: 16 }) !== Validator_1.ValidationReport.OK)
|
|
240
|
-
errors.push({ code: 30, message: "Invalid phone." });
|
|
241
|
-
}
|
|
242
|
-
else if (Array.isArray(data.phone)) {
|
|
243
|
-
for (let i of data.phone) {
|
|
244
|
-
if (Validator_1.Validator.validateValue(i, { type: "string", min: 9, max: 16 }) !== Validator_1.ValidationReport.OK)
|
|
245
|
-
errors.push({ code: 30, message: "Invalid phone." });
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (errors.length > 0)
|
|
250
|
-
return { ...DefaultErrors_1.default.INVALID_DATA, stack: errors, details: "Invalid configuration data." };
|
|
251
|
-
}
|
|
252
|
-
checkName(name) {
|
|
253
|
-
const n = name.replace(/[^a-zA-Z0-9\.\-]/g, "");
|
|
254
|
-
if (n !== name)
|
|
255
|
-
return { code: 21, message: "Incorrect name." };
|
|
256
|
-
}
|
|
257
200
|
}
|
|
258
201
|
exports.ConfigService = ConfigService;
|
|
@@ -274,6 +274,24 @@ async function Deploy(opt) {
|
|
|
274
274
|
}
|
|
275
275
|
const emptyResponse = body === null || body === undefined
|
|
276
276
|
|| (typeof body === "object" && Object.keys(body).length === 0);
|
|
277
|
+
const pending = body?.data?.data;
|
|
278
|
+
if (pending && typeof pending === "object" && pending.pendingReview === true) {
|
|
279
|
+
console.log(yellow(bold("⏳ UPLOADED — WAITING FOR REVIEW (not deployed yet)")));
|
|
280
|
+
console.log(line);
|
|
281
|
+
console.log(` ${dim("project: ")} ${opt.name}`);
|
|
282
|
+
console.log(` ${dim("version: ")} ${oldVersion} ${dim("→")} ${bold(newVersion)}`);
|
|
283
|
+
console.log(` ${dim("reviewers:")} ${(pending.reviewers ?? []).join(", ")} ${dim(pending.mode === "all" ? "(all must accept)" : "(any one can accept)")}`);
|
|
284
|
+
if (Array.isArray(pending.notNotified) && pending.notNotified.length)
|
|
285
|
+
console.log(` ${yellow("⚠ email not delivered to:")} ${pending.notNotified.join(", ")}`);
|
|
286
|
+
if (pending.expiresAt)
|
|
287
|
+
console.log(` ${dim("link valid until:")} ${new Date(pending.expiresAt).toISOString()}`);
|
|
288
|
+
if (pending.replacedPrevious)
|
|
289
|
+
console.log(` ${dim("replaced the previous pending upload — its links no longer work")}`);
|
|
290
|
+
console.log(` ${dim("elapsed: ")} ${elapsedMs}ms`);
|
|
291
|
+
console.log(line);
|
|
292
|
+
console.log("");
|
|
293
|
+
return archiveName;
|
|
294
|
+
}
|
|
277
295
|
if (emptyResponse) {
|
|
278
296
|
console.log(yellow(bold("⚠ DEPLOY LIKELY OK (server returned empty response)")));
|
|
279
297
|
}
|
|
@@ -72,20 +72,20 @@ class Deployer extends BaseEndpoint_1.BaseEndpoint {
|
|
|
72
72
|
async file(req) {
|
|
73
73
|
if (!req.files)
|
|
74
74
|
throw { ...DefaultErrors_1.default.FILE_NOT_EXISTS, details: "No files were uploaded" };
|
|
75
|
-
const result = await DeployerService_1.REQ_DEPLOYMENT_PROCEED.request({ ...req.data, files: req.files, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"] });
|
|
75
|
+
const result = await DeployerService_1.REQ_DEPLOYMENT_PROCEED.request({ ...req.data, files: req.files, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"], ip: req.ip });
|
|
76
76
|
return { data: result };
|
|
77
77
|
}
|
|
78
78
|
async switchSlot(req) {
|
|
79
|
-
return await DeployerService_1.REQ_DEPLOYMENT_SWITCH.request({ ...req.data, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"] });
|
|
79
|
+
return await DeployerService_1.REQ_DEPLOYMENT_SWITCH.request({ ...req.data, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"], ip: req.ip });
|
|
80
80
|
}
|
|
81
81
|
async status(req) {
|
|
82
|
-
return await DeployerService_1.REQ_DEPLOYMENT_STATUS.request({ ...req.data, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"] });
|
|
82
|
+
return await DeployerService_1.REQ_DEPLOYMENT_STATUS.request({ ...req.data, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"], ip: req.ip });
|
|
83
83
|
}
|
|
84
84
|
async backups(req) {
|
|
85
|
-
return await DeployerService_1.REQ_DEPLOYMENT_BACKUPS.request({ name: req.data.name, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"] });
|
|
85
|
+
return await DeployerService_1.REQ_DEPLOYMENT_BACKUPS.request({ name: req.data.name, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"], ip: req.ip });
|
|
86
86
|
}
|
|
87
87
|
async restore(req) {
|
|
88
|
-
return await DeployerService_1.REQ_DEPLOYMENT_RESTORE.request({ name: req.data.name, token: req.data.token, backup: req.data.backup, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"] });
|
|
88
|
+
return await DeployerService_1.REQ_DEPLOYMENT_RESTORE.request({ name: req.data.name, token: req.data.token, backup: req.data.backup, auth: req.headers.authorization, authLogin: req.headers["x-deployer-login"], ip: req.ip });
|
|
89
89
|
}
|
|
90
90
|
async slot(req) {
|
|
91
91
|
return await DeployerService_1.REQ_DEPLOYMENT_SLOT.request({ name: req.data.name, token: req.data.token, slot: req.data.slot });
|