@infosupport/huddle-cli 0.0.0-experiment-160.117.1 → 0.0.0-experiment-160.119.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ // ── Host pipe op `sbx.kit`: the Huddle CA for a Docker Sandbox ──
3
+ // Huddle inspects TLS (it terminates it with its own CA) for a devcontainer, and
4
+ // the devcontainer is set up to trust that CA. A Docker Sandbox gets the same
5
+ // through an sbx kit (a "mixin": `sbx create --kit <dir>`), which sbx applies to
6
+ // the sandbox at create:
7
+ // - the CA in the system trust store (install, as root);
8
+ // - NODE_EXTRA_CA_CERTS / SSL_CERT_FILE / REQUESTS_CA_BUNDLE, like a devcontainer,
9
+ // but all on the system bundle: that keeps sbx's own proxy CA, which sbx's
10
+ // credential proxy needs (a tool trusting only the Huddle CA breaks there);
11
+ // - the CA in the keystore of a JetBrains backend (the JBR does not read the
12
+ // system store), also for a backend Toolbox installs later.
13
+ // The op writes the kit to ~/.huddle/sbx/kits/huddle-ca and returns its host path.
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.KIT_NAME = exports.SBX_KITS_DIR = void 0;
19
+ exports.sbxKit = sbxKit;
20
+ const fs_1 = __importDefault(require("fs"));
21
+ const path_1 = __importDefault(require("path"));
22
+ const config_1 = require("../config");
23
+ const protocol_1 = require("./protocol");
24
+ const fs_list_1 = require("./fs-list");
25
+ exports.SBX_KITS_DIR = path_1.default.join(config_1.CONFIG_DIR, 'sbx', 'kits');
26
+ exports.KIT_NAME = 'huddle-ca';
27
+ const PEM_RE = /^-----BEGIN CERTIFICATE-----\r?\n[A-Za-z0-9+/=\r\n]+-----END CERTIFICATE-----\r?\n?$/;
28
+ const MAX_PEM = 16 * 1024;
29
+ // In the sandbox, files/home/ is /home/agent/.
30
+ const IN_BOX = '/home/agent/.huddle-ca';
31
+ const BUNDLE = '/etc/ssl/certs/ca-certificates.crt';
32
+ const SPEC = `schemaVersion: "2"
33
+ kind: mixin
34
+ name: ${exports.KIT_NAME}
35
+ description: Trust the Huddle CA, so Huddle can inspect this sandbox's TLS
36
+ environment:
37
+ variables:
38
+ NODE_EXTRA_CA_CERTS: ${BUNDLE}
39
+ SSL_CERT_FILE: ${BUNDLE}
40
+ REQUESTS_CA_BUNDLE: ${BUNDLE}
41
+ setup:
42
+ install:
43
+ - command: "sh ${IN_BOX}/install.sh"
44
+ user: "0"
45
+ startup:
46
+ - command: "sh ${IN_BOX}/jbr-watch.sh"
47
+ `;
48
+ const INSTALL = `#!/bin/sh
49
+ # Generated by Huddle. Runs as root when the sandbox is created.
50
+ set -e
51
+ mkdir -p /usr/local/share/ca-certificates
52
+ install -m 0644 ${IN_BOX}/huddle-ca.crt /usr/local/share/ca-certificates/huddle-ca.crt
53
+ # sbx's own proxy CA into the same store, so the bundle the variables point at
54
+ # trusts both (sbx's credential proxy signs with that one).
55
+ if [ -n "\${PROXY_CA_CERT_B64:-}" ]; then
56
+ echo "$PROXY_CA_CERT_B64" | base64 -d > /usr/local/share/ca-certificates/sbx-proxy-ca.crt 2>/dev/null \\
57
+ && chmod 644 /usr/local/share/ca-certificates/sbx-proxy-ca.crt \\
58
+ || rm -f /usr/local/share/ca-certificates/sbx-proxy-ca.crt
59
+ fi
60
+ update-ca-certificates >/dev/null 2>&1 || true
61
+ echo "[huddle] CA installed"
62
+ `;
63
+ // A JetBrains backend runs on its own JVM (the JBR), which only trusts its own
64
+ // keystore. Toolbox installs it under ~/.cache/JetBrains/RemoteDev/dist on the
65
+ // first connect, so this keeps watching (in the background) and imports the CA
66
+ // into every backend it finds, once.
67
+ const JBR_WATCH = `#!/bin/sh
68
+ # Generated by Huddle. Runs as the agent user on every sandbox start.
69
+ CA=/usr/local/share/ca-certificates/huddle-ca.crt
70
+ [ -f "$CA" ] || CA=${IN_BOX}/huddle-ca.crt
71
+ import_all() {
72
+ for d in "$HOME"/.cache/JetBrains/RemoteDev/dist/*/; do
73
+ kt="$d/jbr/bin/keytool"; ks="$d/jbr/lib/security/cacerts"
74
+ [ -x "$kt" ] && [ -f "$ks" ] && [ ! -f "$d/.huddle-ca" ] || continue
75
+ "$kt" -delete -alias huddle-ca -keystore "$ks" -storepass changeit >/dev/null 2>&1 || true
76
+ "$kt" -importcert -noprompt -trustcacerts -alias huddle-ca -file "$CA" -keystore "$ks" -storepass changeit >/dev/null 2>&1 \\
77
+ && touch "$d/.huddle-ca"
78
+ done
79
+ }
80
+ import_all
81
+ ( while sleep 30; do import_all; done ) >/dev/null 2>&1 </dev/null &
82
+ exit 0
83
+ `;
84
+ /** The `sbx.kit` op: `{ caPem }` → `{ dir }`, the kit's folder as a host path. */
85
+ function sbxKit(args, root = exports.SBX_KITS_DIR, platform = process.platform) {
86
+ const { caPem } = args;
87
+ if (typeof caPem !== 'string' || caPem.length > MAX_PEM || !PEM_RE.test(caPem)) {
88
+ throw new protocol_1.PipeError('bad_request', 'caPem must be one PEM certificate');
89
+ }
90
+ const dir = path_1.default.join(root, exports.KIT_NAME);
91
+ try {
92
+ (0, protocol_1.ensurePrivateDir)(root);
93
+ (0, protocol_1.ensurePrivateDir)(dir);
94
+ const home = path_1.default.join(dir, 'files', 'home', '.huddle-ca');
95
+ fs_1.default.mkdirSync(home, { recursive: true, mode: 0o700 });
96
+ // \n only: these are read inside the Linux sandbox, also when written on Windows.
97
+ const write = (file, text) => fs_1.default.writeFileSync(file, text.replace(/\r\n/g, '\n'), { mode: 0o644 });
98
+ write(path_1.default.join(dir, 'spec.yaml'), SPEC);
99
+ write(path_1.default.join(home, 'huddle-ca.crt'), caPem.endsWith('\n') ? caPem : `${caPem}\n`);
100
+ write(path_1.default.join(home, 'install.sh'), INSTALL);
101
+ write(path_1.default.join(home, 'jbr-watch.sh'), JBR_WATCH);
102
+ }
103
+ catch (err) {
104
+ throw new protocol_1.PipeError('internal', `could not write the sbx kit: ${err.message ?? err}`);
105
+ }
106
+ return { dir: (0, fs_list_1.toHuddlePath)(dir, platform) };
107
+ }
@@ -19,6 +19,7 @@ const self_update_1 = require("../self-update");
19
19
  const protocol_1 = require("./protocol");
20
20
  const fs_list_1 = require("./fs-list");
21
21
  const tools_1 = require("./tools");
22
+ const sbx_kit_1 = require("./sbx-kit");
22
23
  const sbx_links_1 = require("./sbx-links");
23
24
  const ssh_hosts_1 = require("./ssh-hosts");
24
25
  // Ids are generated by the gateway (32 hex); anything that does not look like
@@ -41,6 +42,8 @@ async function dispatch(req, maxEntries) {
41
42
  return (0, tools_1.toolRun)(args);
42
43
  case 'sbx.links':
43
44
  return (0, sbx_links_1.sbxLinks)(args);
45
+ case 'sbx.kit':
46
+ return (0, sbx_kit_1.sbxKit)(args);
44
47
  case 'ssh.key':
45
48
  return (0, ssh_hosts_1.sshKey)(args);
46
49
  case 'ssh.host':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infosupport/huddle-cli",
3
- "version": "0.0.0-experiment-160.117.1",
3
+ "version": "0.0.0-experiment-160.119.1",
4
4
  "description": "Huddle CLI — start devcontainers and manage firewall rules",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "author": "Info Support B.V.",