redkite 0.1.9 → 0.1.10
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 +131 -11
- package/dist/build.d.ts +1 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +7 -1
- package/dist/build.js.map +1 -1
- package/dist/cli/agent.js +2 -2
- package/dist/cli/agent.js.map +1 -1
- package/dist/cli/config.d.ts +1 -0
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +67 -14
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/dotenv.d.ts +2 -0
- package/dist/cli/dotenv.d.ts.map +1 -0
- package/dist/cli/dotenv.js +30 -0
- package/dist/cli/dotenv.js.map +1 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +11 -5
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/screen.d.ts +10 -3
- package/dist/cli/screen.d.ts.map +1 -1
- package/dist/cli/screen.js +153 -36
- package/dist/cli/screen.js.map +1 -1
- package/dist/cli/viewer.d.ts.map +1 -1
- package/dist/cli/viewer.js +36 -4
- package/dist/cli/viewer.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +8 -0
- package/dist/config.js.map +1 -1
- package/dist/deploy.d.ts.map +1 -1
- package/dist/deploy.js +12 -2
- package/dist/deploy.js.map +1 -1
- package/dist/docker.d.ts +1 -0
- package/dist/docker.d.ts.map +1 -1
- package/dist/docker.js +3 -0
- package/dist/docker.js.map +1 -1
- package/dist/dockerfile.d.ts +1 -0
- package/dist/dockerfile.d.ts.map +1 -1
- package/dist/dockerfile.js +16 -3
- package/dist/dockerfile.js.map +1 -1
- package/dist/environment.d.ts +2 -0
- package/dist/environment.d.ts.map +1 -1
- package/dist/environment.js +56 -1
- package/dist/environment.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +1 -0
- package/dist/log.js.map +1 -1
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +3 -0
- package/dist/pipeline.js.map +1 -1
- package/dist/plugins/bitwarden.d.ts.map +1 -1
- package/dist/plugins/bitwarden.js +25 -6
- package/dist/plugins/bitwarden.js.map +1 -1
- package/dist/presets/nextApp.d.ts.map +1 -1
- package/dist/presets/nextApp.js +5 -4
- package/dist/presets/nextApp.js.map +1 -1
- package/dist/presets/nodeApp.d.ts.map +1 -1
- package/dist/presets/nodeApp.js +15 -6
- package/dist/presets/nodeApp.js.map +1 -1
- package/dist/secrets/manager.d.ts +9 -0
- package/dist/secrets/manager.d.ts.map +1 -0
- package/dist/secrets/manager.js +130 -0
- package/dist/secrets/manager.js.map +1 -0
- package/dist/secrets/store.d.ts.map +1 -1
- package/dist/secrets/store.js +46 -12
- package/dist/secrets/store.js.map +1 -1
- package/dist/services/ensure.js +1 -1
- package/dist/services/ensure.js.map +1 -1
- package/dist/services/index.d.ts +2 -1
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +11 -0
- package/dist/services/index.js.map +1 -1
- package/dist/services/planned.d.ts +0 -1
- package/dist/services/planned.d.ts.map +1 -1
- package/dist/services/planned.js +3 -12
- package/dist/services/planned.js.map +1 -1
- package/dist/services/proxy.d.ts +7 -0
- package/dist/services/proxy.d.ts.map +1 -0
- package/dist/services/proxy.js +103 -0
- package/dist/services/proxy.js.map +1 -0
- package/dist/source.d.ts +1 -0
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +5 -5
- package/dist/source.js.map +1 -1
- package/dist/steps.d.ts +1 -6
- package/dist/steps.d.ts.map +1 -1
- package/dist/steps.js +3 -3
- package/dist/steps.js.map +1 -1
- package/dist/types.d.ts +7 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/build.ts +10 -1
- package/src/cli/agent.ts +2 -2
- package/src/cli/config.ts +77 -13
- package/src/cli/dotenv.ts +35 -0
- package/src/cli/index.ts +11 -5
- package/src/cli/screen.ts +193 -43
- package/src/cli/viewer.ts +41 -4
- package/src/config.ts +11 -0
- package/src/deploy.ts +14 -2
- package/src/docker.ts +5 -0
- package/src/dockerfile.ts +23 -3
- package/src/environment.ts +71 -1
- package/src/index.ts +2 -1
- package/src/log.ts +4 -0
- package/src/pipeline.ts +4 -0
- package/src/plugins/bitwarden.ts +38 -11
- package/src/presets/nextApp.ts +5 -4
- package/src/presets/nodeApp.ts +15 -6
- package/src/secrets/manager.ts +172 -0
- package/src/secrets/store.ts +51 -16
- package/src/services/ensure.ts +1 -1
- package/src/services/index.ts +13 -1
- package/src/services/planned.ts +3 -13
- package/src/services/proxy.ts +126 -0
- package/src/source.ts +13 -4
- package/src/steps.ts +7 -7
- package/src/types.ts +19 -4
- package/dist/nginx.d.ts +0 -4
- package/dist/nginx.d.ts.map +0 -1
- package/dist/nginx.js +0 -53
- package/dist/nginx.js.map +0 -1
- package/src/nginx.ts +0 -63
package/src/index.ts
CHANGED
|
@@ -9,18 +9,19 @@ export * from "./host.js";
|
|
|
9
9
|
export * from "./layout.js";
|
|
10
10
|
export * from "./localHost.js";
|
|
11
11
|
export * from "./log.js";
|
|
12
|
-
export * from "./nginx.js";
|
|
13
12
|
export * from "./pipeline.js";
|
|
14
13
|
export * from "./plugin.js";
|
|
15
14
|
export * from "./plugins/index.js";
|
|
16
15
|
export * from "./recover.js";
|
|
17
16
|
export * from "./presets/index.js";
|
|
17
|
+
export * from "./secrets/manager.js";
|
|
18
18
|
export * from "./secrets/refs.js";
|
|
19
19
|
export * from "./secrets/store.js";
|
|
20
20
|
export * from "./shell.js";
|
|
21
21
|
export * from "./services/ensure.js";
|
|
22
22
|
export * from "./services/index.js";
|
|
23
23
|
export * from "./services/planned.js";
|
|
24
|
+
export * from "./services/proxy.js";
|
|
24
25
|
export * from "./source.js";
|
|
25
26
|
export * from "./sshHost.js";
|
|
26
27
|
export * from "./steps.js";
|
package/src/log.ts
CHANGED
|
@@ -14,6 +14,9 @@ export type Task = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export type Log = ((message: string) => void) & {
|
|
17
|
+
// Every point the run will walk, said once before the first of them starts.
|
|
18
|
+
// A view that knows the whole list can show what has not begun
|
|
19
|
+
plan?(points: string[]): void;
|
|
17
20
|
// Something the run survived, but that the person should know happened
|
|
18
21
|
warn(message: string): void;
|
|
19
22
|
// The reason a deploy is about to stop, or did
|
|
@@ -31,6 +34,7 @@ const NOTHING: Task = {
|
|
|
31
34
|
};
|
|
32
35
|
|
|
33
36
|
export const silent: Log = Object.assign(() => {}, {
|
|
37
|
+
plan: () => {},
|
|
34
38
|
warn: () => {},
|
|
35
39
|
fail: () => {},
|
|
36
40
|
done: () => {},
|
package/src/pipeline.ts
CHANGED
|
@@ -207,6 +207,10 @@ export async function runPipeline(
|
|
|
207
207
|
const ordered = sequence(steps, RUNS[run]);
|
|
208
208
|
const plan: Plan = { config: setting.config, environment: setting.environment };
|
|
209
209
|
|
|
210
|
+
// What the run will walk, said before any of it does. Nothing depends on it
|
|
211
|
+
// being heard: a log without a view has nothing to do with the list
|
|
212
|
+
setting.log.plan?.(ordered.map((step) => step.point));
|
|
213
|
+
|
|
210
214
|
// Every check before any step, so a run that is going to fail on a config
|
|
211
215
|
// mistake fails before it has created a network or built an image. Nothing
|
|
212
216
|
// wraps what a check throws: it is about the config, and the config is what
|
package/src/plugins/bitwarden.ts
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
import { definePlugin, type OpenContext, type Plugin } from "../plugin.js";
|
|
2
|
+
import { secretsManagerStore } from "../secrets/manager.js";
|
|
2
3
|
import { bitwardenStore } from "../secrets/store.js";
|
|
3
4
|
import type { SecretRef } from "../types.js";
|
|
4
5
|
|
|
5
6
|
// Redkite's own vault, and a plugin like any other: a deployment that does not
|
|
6
7
|
// register it cannot resolve a bitwarden ref, and says so before it builds.
|
|
8
|
+
//
|
|
9
|
+
// Bitwarden is two services rather than one. Secrets Manager holds secrets for
|
|
10
|
+
// machines and opens with an access token, which is what a deploy wants. The
|
|
11
|
+
// password manager holds a person's vault and wants a session obtained by
|
|
12
|
+
// unlocking it. The ids are different, the CLIs are different, and giving one
|
|
13
|
+
// service's credential to the other is how a deploy ends up waiting on a
|
|
14
|
+
// password prompt nobody can see.
|
|
7
15
|
|
|
8
|
-
// The one variable
|
|
9
|
-
// job that unlocked once and reuses it across several deploys is the case
|
|
16
|
+
// The one variable either side reads, so a job sets one thing whichever it is
|
|
10
17
|
const KEY = "BW_KEY";
|
|
11
18
|
|
|
12
19
|
export type BitwardenOptions = {
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
20
|
+
// Which Bitwarden this deployment reads, and what opens it.
|
|
21
|
+
//
|
|
22
|
+
// true, the default, is Secrets Manager: BW_KEY is the access token. A
|
|
23
|
+
// string is that token given directly, for a config naming its own variable.
|
|
24
|
+
//
|
|
25
|
+
// false is the password manager: BW_KEY is a session from bw unlock --raw,
|
|
26
|
+
// and without one the api credentials obtain one.
|
|
18
27
|
secrets?: boolean | string;
|
|
19
28
|
};
|
|
20
29
|
|
|
21
30
|
function vault(options: BitwardenOptions = {}): Plugin {
|
|
22
31
|
const secrets = options.secrets ?? true;
|
|
23
|
-
if (secrets === false) return definePlugin({ name: "bitwarden" });
|
|
24
32
|
|
|
25
33
|
return definePlugin({
|
|
26
34
|
name: "bitwarden",
|
|
@@ -28,8 +36,26 @@ function vault(options: BitwardenOptions = {}): Plugin {
|
|
|
28
36
|
});
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
async function open(secrets:
|
|
32
|
-
|
|
39
|
+
async function open(secrets: boolean | string, context: OpenContext) {
|
|
40
|
+
if (secrets === false) return await passwords(context);
|
|
41
|
+
|
|
42
|
+
const token = typeof secrets === "string" ? secrets : process.env[KEY];
|
|
43
|
+
|
|
44
|
+
if (!token) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`${KEY} is not set, and this deployment reads from Bitwarden Secrets ` +
|
|
47
|
+
"Manager. It is an access token, not a password. A deployment reading " +
|
|
48
|
+
"the password manager instead says bitwarden({ secrets: false })",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return await secretsManagerStore({ token, detail: context.detail });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The personal vault. A session skips the login and the unlock, which is two
|
|
56
|
+
// fewer round trips and the only way a job without a master password gets in
|
|
57
|
+
async function passwords(context: OpenContext) {
|
|
58
|
+
const session = process.env[KEY];
|
|
33
59
|
if (session) return await bitwardenStore({ session, detail: context.detail });
|
|
34
60
|
|
|
35
61
|
return await bitwardenStore({
|
|
@@ -45,7 +71,8 @@ function required(name: string) {
|
|
|
45
71
|
if (value) return value;
|
|
46
72
|
|
|
47
73
|
throw new Error(
|
|
48
|
-
`Neither ${KEY} nor ${name} is set, and this deployment reads its
|
|
74
|
+
`Neither ${KEY} nor ${name} is set, and this deployment reads its ` +
|
|
75
|
+
"environment from the Bitwarden password manager",
|
|
49
76
|
);
|
|
50
77
|
}
|
|
51
78
|
|
package/src/presets/nextApp.ts
CHANGED
|
@@ -57,10 +57,11 @@ export function nextApp(options: NextAppOptions = {}): BuildSpec {
|
|
|
57
57
|
// whatever the repository put there, and plenty of apps have none
|
|
58
58
|
carry: ["/app/.next/static", { path: "/app/public", optional: true }],
|
|
59
59
|
entrypoint: start("node server.js"),
|
|
60
|
-
// app
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
60
|
+
// next-app is Next's own build cache, which only ever makes a build
|
|
61
|
+
// slower when it is dropped. node_modules is not here for the same
|
|
62
|
+
// reason it is not in nodeApp: a dropped mount and a kept layer is a
|
|
63
|
+
// build that cannot find what the manifest lists
|
|
64
|
+
caches: ["yarn", "npm", "next-app"],
|
|
64
65
|
}),
|
|
65
66
|
preset: "nextApp",
|
|
66
67
|
// The standalone tree traces from the workspace root, so an app in a
|
package/src/presets/nodeApp.ts
CHANGED
|
@@ -31,13 +31,22 @@ export function nodeApp(options: NodeAppOptions): BuildSpec {
|
|
|
31
31
|
output: options.output,
|
|
32
32
|
carry: options.carry ?? [],
|
|
33
33
|
entrypoint: options.entrypoint,
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
|
|
34
|
+
// The package manager's own cache, and nothing else. node_modules used to
|
|
35
|
+
// be a mount too, and BuildKit drops a cache mount whenever it likes while
|
|
36
|
+
// keeping the layer that filled it: the install then does not re-run and
|
|
37
|
+
// the modules are simply gone. What that looks like is a build saying it
|
|
38
|
+
// cannot find a dependency the manifest plainly lists.
|
|
39
|
+
//
|
|
40
|
+
// Installed into the layer instead, they are there for every step below,
|
|
41
|
+
// for a migration run in this image afterwards, and for a check. The
|
|
42
|
+
// manager's cache still makes the install itself fast
|
|
43
|
+
caches: options.caches ?? ["yarn", "npm"],
|
|
38
44
|
submodules: options.submodules ?? false,
|
|
39
|
-
// The checkout arrives complete, so this is what a build step itself needs
|
|
40
|
-
|
|
45
|
+
// The checkout arrives complete, so this is what a build step itself needs.
|
|
46
|
+
// openssh-client is for the dependencies it resolves rather than the
|
|
47
|
+
// repository: a git+ssh entry in a manifest is fetched during the install,
|
|
48
|
+
// and git cannot do that without an ssh to run
|
|
49
|
+
aptPackages: ["git", "openssh-client"],
|
|
41
50
|
runtimePackages: ["curl"],
|
|
42
51
|
runtimeSteps: [],
|
|
43
52
|
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { chmodSync, existsSync } from "node:fs";
|
|
3
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
|
|
7
|
+
import type { SecretStore } from "./refs.js";
|
|
8
|
+
|
|
9
|
+
// Bitwarden Secrets Manager, which is not the password manager: different
|
|
10
|
+
// service, different CLI, different credential. An access token opens a set of
|
|
11
|
+
// projects rather than a personal vault, which is what a deploy wants, and it
|
|
12
|
+
// is the whole of the authentication: nothing to log in to and nothing to
|
|
13
|
+
// unlock, so there is no session to obtain or lose.
|
|
14
|
+
|
|
15
|
+
// Not an npm package. The password manager's CLI is one and redkite installs
|
|
16
|
+
// it with npm; this one is a released binary, and the only thing on npm under
|
|
17
|
+
// its name belongs to somebody else entirely. Pinned for the same reason the
|
|
18
|
+
// other is: an unpinned CLI is a different program on a machine that has never
|
|
19
|
+
// run a deploy before
|
|
20
|
+
const VERSION = "2.1.0";
|
|
21
|
+
const RELEASES = "https://github.com/bitwarden/sdk-sm/releases/download";
|
|
22
|
+
const CLIS = join(homedir(), ".cache", "redkite", "cli");
|
|
23
|
+
|
|
24
|
+
// What the release calls each platform. Anything not here has to bring its own
|
|
25
|
+
const TARGETS: Record<string, string> = {
|
|
26
|
+
"linux-x64": "x86_64-unknown-linux-gnu",
|
|
27
|
+
"linux-arm64": "aarch64-unknown-linux-gnu",
|
|
28
|
+
"darwin-x64": "x86_64-apple-darwin",
|
|
29
|
+
"darwin-arm64": "aarch64-apple-darwin",
|
|
30
|
+
"win32-x64": "x86_64-pc-windows-msvc",
|
|
31
|
+
"win32-arm64": "aarch64-pc-windows-msvc",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ManagerCredentials = {
|
|
35
|
+
// The access token, which is the only credential this service has
|
|
36
|
+
token: string;
|
|
37
|
+
detail?: (message: string) => void;
|
|
38
|
+
// Injected so the argv this builds is asserted on rather than trusted
|
|
39
|
+
bws?: Bws;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type Bws = (args: string[], env: Record<string, string>) => Promise<string>;
|
|
43
|
+
|
|
44
|
+
export async function secretsManagerStore(
|
|
45
|
+
credentials: ManagerCredentials,
|
|
46
|
+
): Promise<SecretStore> {
|
|
47
|
+
const detail = credentials.detail ?? (() => {});
|
|
48
|
+
const bws = credentials.bws ?? (await resolve(detail));
|
|
49
|
+
|
|
50
|
+
// Fetched once each. A second read of the same item during a deploy is the
|
|
51
|
+
// same answer, and this avoids a process per call site
|
|
52
|
+
const cache = new Map<string, Promise<string>>();
|
|
53
|
+
|
|
54
|
+
const fetch = async (id: string) => {
|
|
55
|
+
detail(`reading ${id.slice(0, 8)}`);
|
|
56
|
+
|
|
57
|
+
const answer = await bws(["secret", "get", id, "--output", "json"], {
|
|
58
|
+
BWS_ACCESS_TOKEN: credentials.token,
|
|
59
|
+
}).catch((error: unknown) => {
|
|
60
|
+
throw new Error(`Could not read ${id} from Bitwarden Secrets Manager: ${messageOf(error)}`);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return valueOf(answer, id);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
read: async (id) => {
|
|
68
|
+
const pending = cache.get(id) ?? fetch(id);
|
|
69
|
+
cache.set(id, pending);
|
|
70
|
+
return await pending;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The secret's value, which is the whole of what a ref points at. Everything
|
|
76
|
+
// else the call answers with is about the item rather than in it
|
|
77
|
+
function valueOf(answer: string, id: string) {
|
|
78
|
+
try {
|
|
79
|
+
const parsed = JSON.parse(answer) as { value?: unknown };
|
|
80
|
+
if (typeof parsed.value === "string") return parsed.value;
|
|
81
|
+
} catch {
|
|
82
|
+
// Falls through to the same refusal as a shape without a value
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
throw new Error(`Bitwarden Secrets Manager answered for ${id} without a value`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function resolve(detail: (message: string) => void): Promise<Bws> {
|
|
89
|
+
const file = await binary(detail);
|
|
90
|
+
return async (args, env) => await run(file, args, env);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function binary(detail: (message: string) => void) {
|
|
94
|
+
const override = process.env["REDKITE_BWS_BIN"];
|
|
95
|
+
if (override) return override;
|
|
96
|
+
|
|
97
|
+
const onPath = await run("bws", ["--version"], {}).then(
|
|
98
|
+
() => true,
|
|
99
|
+
() => false,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (onPath) return "bws";
|
|
103
|
+
|
|
104
|
+
const directory = join(CLIS, `bws-${VERSION}`);
|
|
105
|
+
const installed = join(directory, process.platform === "win32" ? "bws.exe" : "bws");
|
|
106
|
+
if (existsSync(installed)) return installed;
|
|
107
|
+
|
|
108
|
+
return await install(directory, installed, detail);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Downloaded once per machine per pinned version, the way the other CLI is
|
|
112
|
+
// installed once. The archive is a zip with one file in it, and unzip is the
|
|
113
|
+
// one thing on every platform that reads one
|
|
114
|
+
async function install(directory: string, installed: string, detail: (m: string) => void) {
|
|
115
|
+
const target = TARGETS[`${process.platform}-${process.arch}`];
|
|
116
|
+
|
|
117
|
+
if (!target) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Bitwarden publishes no bws for ${process.platform}-${process.arch}. ` +
|
|
120
|
+
"Install it yourself and point REDKITE_BWS_BIN at it",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const url = `${RELEASES}/bws-v${VERSION}/bws-${target}-${VERSION}.zip`;
|
|
125
|
+
detail(`installing bws ${VERSION}, once for this machine`);
|
|
126
|
+
|
|
127
|
+
await mkdir(directory, { recursive: true });
|
|
128
|
+
const archive = join(directory, "bws.zip");
|
|
129
|
+
|
|
130
|
+
const answer = await globalThis.fetch(url);
|
|
131
|
+
if (!answer.ok) throw new Error(`Could not download bws from ${url}: ${answer.status}`);
|
|
132
|
+
|
|
133
|
+
await writeFile(archive, Buffer.from(await answer.arrayBuffer()));
|
|
134
|
+
await run("unzip", ["-o", "-q", archive, "-d", directory], {}).catch((error: unknown) => {
|
|
135
|
+
throw new Error(`Could not unpack bws: ${messageOf(error)}`);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await rm(archive, { force: true });
|
|
139
|
+
|
|
140
|
+
// The zip carries no mode anything trusts, so it arrives unrunnable
|
|
141
|
+
chmodSync(installed, 0o755);
|
|
142
|
+
|
|
143
|
+
return installed;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Nothing on stdin, and the output kept exactly as it came. The same reasoning
|
|
147
|
+
// as the password manager's runner: a question nobody can answer is a deploy
|
|
148
|
+
// that waits for ever
|
|
149
|
+
function run(file: string, args: string[], env: Record<string, string>) {
|
|
150
|
+
return new Promise<string>((resolve_, reject) => {
|
|
151
|
+
const child = spawn(file, args, {
|
|
152
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
153
|
+
env: { ...process.env, ...env },
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
let stdout = "";
|
|
157
|
+
let stderr = "";
|
|
158
|
+
|
|
159
|
+
child.stdout.on("data", (chunk: Buffer) => (stdout += chunk.toString()));
|
|
160
|
+
child.stderr.on("data", (chunk: Buffer) => (stderr += chunk.toString()));
|
|
161
|
+
child.on("error", reject);
|
|
162
|
+
|
|
163
|
+
child.on("close", (code) => {
|
|
164
|
+
if (code === 0) return resolve_(stdout);
|
|
165
|
+
reject(new Error(stderr.trim() || stdout.trim() || `${file} exited ${code}`));
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function messageOf(error: unknown) {
|
|
171
|
+
return error instanceof Error ? error.message : String(error);
|
|
172
|
+
}
|
package/src/secrets/store.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { mkdir } from "node:fs/promises";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
|
-
import { promisify } from "node:util";
|
|
7
6
|
|
|
8
7
|
import type { SecretStore } from "./refs.js";
|
|
9
8
|
|
|
@@ -14,7 +13,32 @@ import type { SecretStore } from "./refs.js";
|
|
|
14
13
|
// purely as a sandbox, which meant installing several thousand packages on
|
|
15
14
|
// every deploy to read three items.
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
// Nothing on stdin, ever. Both CLIs ask for a password when they decide the
|
|
17
|
+
// vault is locked, and a question put down a pipe nobody writes to is a deploy
|
|
18
|
+
// that waits for ever rather than one that fails.
|
|
19
|
+
//
|
|
20
|
+
// The output is kept exactly as it came: a secret is a file's contents, and a
|
|
21
|
+
// key that lost its last newline is a key some parsers refuse
|
|
22
|
+
function run(file: string, args: string[], env: Record<string, string> = {}) {
|
|
23
|
+
return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
|
|
24
|
+
const child = spawn(file, args, {
|
|
25
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
26
|
+
env: { ...process.env, ...env },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
let stdout = "";
|
|
30
|
+
let stderr = "";
|
|
31
|
+
|
|
32
|
+
child.stdout.on("data", (chunk: Buffer) => (stdout += chunk.toString()));
|
|
33
|
+
child.stderr.on("data", (chunk: Buffer) => (stderr += chunk.toString()));
|
|
34
|
+
child.on("error", reject);
|
|
35
|
+
|
|
36
|
+
child.on("close", (code) => {
|
|
37
|
+
if (code === 0) return resolve({ stdout, stderr });
|
|
38
|
+
reject(new Error(stderr.trim() || stdout.trim() || `${file} exited ${code}`));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
18
42
|
|
|
19
43
|
// A session the caller already holds, or what it takes to obtain one. Both
|
|
20
44
|
// spellings in one object would be a pair of credentials nobody can tell apart
|
|
@@ -44,25 +68,32 @@ export async function bitwardenStore(
|
|
|
44
68
|
|
|
45
69
|
const bw = async (args: string[], env: Record<string, string> = {}) =>
|
|
46
70
|
await run(command.file, [...command.prefix, ...args], {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
BITWARDENCLI_APPDATA_DIR: appdata,
|
|
50
|
-
...env,
|
|
51
|
-
},
|
|
52
|
-
maxBuffer: 32 * 1024 * 1024,
|
|
71
|
+
BITWARDENCLI_APPDATA_DIR: appdata,
|
|
72
|
+
...env,
|
|
53
73
|
});
|
|
54
74
|
|
|
55
75
|
const session = await unlock(bw, credentials, detail);
|
|
56
76
|
|
|
57
77
|
// An item added since the last deploy is not in the local vault otherwise,
|
|
58
|
-
// and bw get answers "not found" rather than fetching it
|
|
59
|
-
|
|
78
|
+
// and bw get answers "not found" rather than fetching it. This is also what
|
|
79
|
+
// proves a session handed in still works: without it the first read is what
|
|
80
|
+
// finds out, and by then a build is waiting on the answer
|
|
81
|
+
await bw(["sync"], { BW_SESSION: session }).catch((error: unknown) => {
|
|
82
|
+
if (!("session" in credentials)) return undefined;
|
|
83
|
+
|
|
84
|
+
throw new Error(
|
|
85
|
+
`The session it was given does not open this vault: ${messageOf(error)}. ` +
|
|
86
|
+
"Obtain one with bw unlock --raw, or unset it and let the api credentials do it",
|
|
87
|
+
);
|
|
88
|
+
});
|
|
60
89
|
|
|
61
90
|
// Fetched once each. A second read of the same item during a deploy is the
|
|
62
91
|
// same answer, and this avoids a process per call site
|
|
63
92
|
const cache = new Map<string, Promise<string>>();
|
|
64
93
|
|
|
65
94
|
const fetch = async (id: string) => {
|
|
95
|
+
detail(`reading ${id.slice(0, 8)}`);
|
|
96
|
+
|
|
66
97
|
const item = await bw(["get", "notes", id, "--raw"], {
|
|
67
98
|
BW_SESSION: session,
|
|
68
99
|
}).catch((error: unknown) => {
|
|
@@ -145,11 +176,15 @@ async function install(detail: (message: string) => void) {
|
|
|
145
176
|
detail(`installing ${CLI}, once for this machine`);
|
|
146
177
|
await mkdir(directory, { recursive: true });
|
|
147
178
|
|
|
148
|
-
await run(
|
|
149
|
-
"
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
179
|
+
await run("npm", [
|
|
180
|
+
"install",
|
|
181
|
+
"--prefix",
|
|
182
|
+
directory,
|
|
183
|
+
"--no-save",
|
|
184
|
+
"--no-audit",
|
|
185
|
+
"--no-fund",
|
|
186
|
+
CLI,
|
|
187
|
+
]).catch((error: unknown) => {
|
|
153
188
|
throw new Error(`Could not install ${CLI}: ${messageOf(error)}`);
|
|
154
189
|
});
|
|
155
190
|
|
package/src/services/ensure.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SPEC_LABEL, type Docker } from "../docker.js";
|
|
2
2
|
import type { Host } from "../host.js";
|
|
3
3
|
import type { Log } from "../log.js";
|
|
4
|
-
import { LISTEN_PORT } from "
|
|
4
|
+
import { LISTEN_PORT } from "./proxy.js";
|
|
5
5
|
import { readEnv, type SecretStores } from "../secrets/refs.js";
|
|
6
6
|
import type { ServiceTopology, Topology } from "../topology.js";
|
|
7
7
|
import type { ServiceSpec } from "../types.js";
|
package/src/services/index.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import type { SecretRefs, ServiceSpec } from "../types.js";
|
|
1
|
+
import type { ProxySpec, SecretRefs, ServiceSpec } from "../types.js";
|
|
2
|
+
|
|
3
|
+
// Not something a deployment lists. Apps carry routes, routes need something
|
|
4
|
+
// to resolve them, and this says what that something is rather than that it
|
|
5
|
+
// exists. A service by this name is refused: there is already one
|
|
6
|
+
export function nginx(options: ProxySpec = {}): ProxySpec {
|
|
7
|
+
return {
|
|
8
|
+
image: options.image ?? "nginx:stable",
|
|
9
|
+
maxBodySize: options.maxBodySize ?? "1M",
|
|
10
|
+
server: options.server ?? [],
|
|
11
|
+
location: options.location ?? [],
|
|
12
|
+
};
|
|
13
|
+
}
|
|
2
14
|
|
|
3
15
|
type RedisOptions = {
|
|
4
16
|
image?: string;
|
package/src/services/planned.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
import type { Docker } from "../docker.js";
|
|
4
|
-
import { LISTEN_PORT,
|
|
4
|
+
import { LISTEN_PORT, proxyService, renderProxy } from "./proxy.js";
|
|
5
5
|
import type { Run } from "../pipeline.js";
|
|
6
6
|
import type { ServiceTopology, Topology } from "../topology.js";
|
|
7
7
|
import type { Deployment, ServiceSpec } from "../types.js";
|
|
@@ -36,10 +36,10 @@ export function plannedServices(
|
|
|
36
36
|
if (run === "verify") return rest;
|
|
37
37
|
|
|
38
38
|
const proxy: PlannedService = {
|
|
39
|
-
spec:
|
|
39
|
+
spec: proxyService(config),
|
|
40
40
|
service: topology.router,
|
|
41
41
|
files: {
|
|
42
|
-
"/etc/nginx/conf.d/default.conf":
|
|
42
|
+
"/etc/nginx/conf.d/default.conf": renderProxy(topology, config.proxy),
|
|
43
43
|
},
|
|
44
44
|
publish: topology.publicPort,
|
|
45
45
|
};
|
|
@@ -47,16 +47,6 @@ export function plannedServices(
|
|
|
47
47
|
return [proxy, ...rest];
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
// Not something a deployment lists. Apps carry routes, routes need a proxy to
|
|
51
|
-
// resolve them, and the one that renders them is this
|
|
52
|
-
export function router(config: Deployment): ServiceSpec {
|
|
53
|
-
return {
|
|
54
|
-
name: "nginx",
|
|
55
|
-
image: config.proxyImage ?? "nginx:stable",
|
|
56
|
-
restart: "always",
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
50
|
// Everything a recreate would change, and nothing a deploy resolves. Secrets
|
|
61
51
|
// are named by their refs rather than their values: whether the config is the
|
|
62
52
|
// one running is a question a plan should answer without unlocking a vault
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { AppTopology, Topology } from "../topology.js";
|
|
2
|
+
import type { Deployment, ProxySpec, ServiceSpec } from "../types.js";
|
|
3
|
+
|
|
4
|
+
// The proxy every route is resolved by: what it is, and the configuration it
|
|
5
|
+
// runs. Both halves here, because a deployment does not list this service and
|
|
6
|
+
// the only thing that decides either is the app list.
|
|
7
|
+
//
|
|
8
|
+
// Header policy is set once rather than per location, which is what let the two
|
|
9
|
+
// blocks drift apart and lose X-Forwarded-For on one of them.
|
|
10
|
+
|
|
11
|
+
const FORWARDING = [
|
|
12
|
+
"proxy_set_header Host $http_host;",
|
|
13
|
+
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;",
|
|
14
|
+
"proxy_set_header X-Forwarded-Host $http_host;",
|
|
15
|
+
"proxy_set_header X-Forwarded-Proto $scheme;",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const FAILOVER = [
|
|
19
|
+
"proxy_connect_timeout 2s;",
|
|
20
|
+
"proxy_read_timeout 30s;",
|
|
21
|
+
"proxy_next_upstream error timeout http_502 http_503;",
|
|
22
|
+
"proxy_next_upstream_tries 2;",
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// The port the proxy listens on inside its container. The published port maps
|
|
26
|
+
// onto this one, so both sides have to agree and only one of them may say it
|
|
27
|
+
export const LISTEN_PORT = 3000;
|
|
28
|
+
|
|
29
|
+
// What the derived proxy is called. A service by this name would be a second
|
|
30
|
+
// container claiming the first one's, so the config surface refuses it
|
|
31
|
+
export const PROXY = "nginx";
|
|
32
|
+
|
|
33
|
+
// Not something a deployment lists. Apps carry routes, routes need something
|
|
34
|
+
// to resolve them, and this says what that something is
|
|
35
|
+
export function proxyService(config: Deployment): ServiceSpec {
|
|
36
|
+
return {
|
|
37
|
+
name: PROXY,
|
|
38
|
+
image: config.proxy?.image ?? "nginx:stable",
|
|
39
|
+
restart: "always",
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function renderProxy(topology: Topology, proxy: ProxySpec = {}) {
|
|
44
|
+
const upstreams = topology.apps.map((app) => upstream(app)).join("\n\n");
|
|
45
|
+
// Longest route first, so "/api/" is not shadowed by the "/" catch-all
|
|
46
|
+
const ordered = [...topology.apps].sort((a, b) => b.route.length - a.route.length);
|
|
47
|
+
const locations = ordered.map((app) => location(app, proxy.location ?? [])).join("\n\n");
|
|
48
|
+
|
|
49
|
+
assertNotDerived(proxy.server ?? []);
|
|
50
|
+
|
|
51
|
+
const server = settled([
|
|
52
|
+
`merge_slashes off;`,
|
|
53
|
+
`client_max_body_size ${proxy.maxBodySize ?? "1M"};`,
|
|
54
|
+
...(proxy.server ?? []),
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
return `resolver 127.0.0.1 valid=5s;
|
|
58
|
+
|
|
59
|
+
${upstreams}
|
|
60
|
+
|
|
61
|
+
server {
|
|
62
|
+
listen ${LISTEN_PORT};
|
|
63
|
+
${indented(server, 4)}
|
|
64
|
+
${locations}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// nginx refuses a second proxy_read_timeout but expects several
|
|
70
|
+
// proxy_set_header, so what identifies a line is the directive and, for the
|
|
71
|
+
// ones that carry a name, the name after it
|
|
72
|
+
const NAMED = new Set(["add_header", "proxy_set_header", "proxy_hide_header", "set"]);
|
|
73
|
+
|
|
74
|
+
function directive(line: string) {
|
|
75
|
+
const [name = "", argument = ""] = line.trim().split(/\s+/);
|
|
76
|
+
return NAMED.has(name) ? `${name} ${argument}` : name;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The last of each wins. A line written by hand replaces the one redkite would
|
|
80
|
+
// have put there rather than repeating it, which nginx would refuse outright
|
|
81
|
+
function settled(lines: string[]) {
|
|
82
|
+
const kept = new Map<string, string>();
|
|
83
|
+
|
|
84
|
+
for (const line of lines) {
|
|
85
|
+
if (!line.trim()) continue;
|
|
86
|
+
kept.set(directive(line), line.trim());
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return [...kept.values()];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The published port maps onto this one, so both sides have to agree and only
|
|
93
|
+
// one of them may say it
|
|
94
|
+
function assertNotDerived(lines: string[]) {
|
|
95
|
+
const listening = lines.find((line) => directive(line) === "listen");
|
|
96
|
+
if (!listening) return;
|
|
97
|
+
|
|
98
|
+
throw new Error(
|
|
99
|
+
`The proxy's server block says ${listening.trim()}, and the port it listens ` +
|
|
100
|
+
"on inside its container is what publicPort is published onto",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function indented(lines: string[], by: number) {
|
|
105
|
+
return lines.map((line) => " ".repeat(by) + line).join("\n");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function upstream(app: AppTopology) {
|
|
109
|
+
return `upstream ${app.name} {
|
|
110
|
+
server ${app.container}:${app.port} max_fails=1 fail_timeout=2s;
|
|
111
|
+
server ${app.retired}:${app.port} backup;
|
|
112
|
+
}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function location(app: AppTopology, added: string[]) {
|
|
116
|
+
// A trailing slash on proxy_pass strips the location prefix. The catch-all
|
|
117
|
+
// must not carry one, a mounted route must
|
|
118
|
+
const target = app.route === "/" ? `http://${app.name}` : `http://${app.name}/`;
|
|
119
|
+
|
|
120
|
+
// Last, so one of these replaces what redkite set rather than repeating it
|
|
121
|
+
const body = settled([`proxy_pass ${target};`, ...FAILOVER, ...FORWARDING, ...added]);
|
|
122
|
+
|
|
123
|
+
return ` location ${app.route} {
|
|
124
|
+
${indented(body, 8)}
|
|
125
|
+
}`;
|
|
126
|
+
}
|