@infosupport/huddle-cli 0.0.0-experiment-160.112.1 → 0.0.0-experiment-160.114.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.
- package/dist/pipe/serve.js +5 -0
- package/dist/pipe/ssh-hosts.js +153 -0
- package/dist/start.js +5 -4
- package/package.json +1 -1
package/dist/pipe/serve.js
CHANGED
|
@@ -20,6 +20,7 @@ const protocol_1 = require("./protocol");
|
|
|
20
20
|
const fs_list_1 = require("./fs-list");
|
|
21
21
|
const tools_1 = require("./tools");
|
|
22
22
|
const sbx_links_1 = require("./sbx-links");
|
|
23
|
+
const ssh_hosts_1 = require("./ssh-hosts");
|
|
23
24
|
// Ids are generated by the gateway (32 hex); anything that does not look like
|
|
24
25
|
// an id is not ours and is left alone rather than turned into a reply path.
|
|
25
26
|
const REQUEST_RE = /^([A-Za-z0-9_-]{1,128})\.in$/;
|
|
@@ -40,6 +41,10 @@ async function dispatch(req, maxEntries) {
|
|
|
40
41
|
return (0, tools_1.toolRun)(args);
|
|
41
42
|
case 'sbx.links':
|
|
42
43
|
return (0, sbx_links_1.sbxLinks)(args);
|
|
44
|
+
case 'ssh.key':
|
|
45
|
+
return (0, ssh_hosts_1.sshKey)(args);
|
|
46
|
+
case 'ssh.host':
|
|
47
|
+
return (0, ssh_hosts_1.sshHost)(args);
|
|
43
48
|
default:
|
|
44
49
|
throw new protocol_1.PipeError('unknown_op', `unknown op '${req.op}'`);
|
|
45
50
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ── Host pipe ops `ssh.key` and `ssh.host`: SSH into a devcontainer, no key setup ──
|
|
3
|
+
// Like `sbx setup ssh` does for Docker Sandboxes: Huddle keeps its own key pair in
|
|
4
|
+
// ~/.huddle/ssh (made here, on the host, so the private key never leaves it) and
|
|
5
|
+
// one ssh config entry per devcontainer, `<name>.huddle`, in ~/.huddle/ssh/hosts.
|
|
6
|
+
// ~/.ssh/config includes those at its top, so the IDE (JetBrains Toolbox), and
|
|
7
|
+
// `ssh <name>.huddle` in a terminal, connect without asking for a key.
|
|
8
|
+
//
|
|
9
|
+
// The gateway only ever gets the public key (`ssh.key`), which it puts in the
|
|
10
|
+
// container's authorized_keys. `ssh.host` writes the entry with the container's
|
|
11
|
+
// host key pinned, so the connection is verified without a fingerprint prompt.
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.INCLUDE_LINE = exports.SSH_DIR = void 0;
|
|
17
|
+
exports.generateEd25519 = generateEd25519;
|
|
18
|
+
exports.ensureInclude = ensureInclude;
|
|
19
|
+
exports.sshKey = sshKey;
|
|
20
|
+
exports.sshHost = sshHost;
|
|
21
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
22
|
+
const fs_1 = __importDefault(require("fs"));
|
|
23
|
+
const os_1 = __importDefault(require("os"));
|
|
24
|
+
const path_1 = __importDefault(require("path"));
|
|
25
|
+
const config_1 = require("../config");
|
|
26
|
+
const protocol_1 = require("./protocol");
|
|
27
|
+
exports.SSH_DIR = path_1.default.join(config_1.CONFIG_DIR, 'ssh');
|
|
28
|
+
const KEY_FILE = 'id_ed25519';
|
|
29
|
+
/** The line Huddle puts at the top of ~/.ssh/config. Hosts in an included file stay in it. */
|
|
30
|
+
exports.INCLUDE_LINE = 'Include ~/.huddle/ssh/hosts/*.conf';
|
|
31
|
+
// Docker container names ([a-zA-Z0-9][a-zA-Z0-9_.-]*) plus `.huddle`; also a file name here.
|
|
32
|
+
const ALIAS_RE = /^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,127}\.huddle$/;
|
|
33
|
+
const USER_RE = /^[a-z_][a-z0-9_-]{0,31}$/;
|
|
34
|
+
const HOST_KEY_RE = /^ssh-ed25519 [A-Za-z0-9+/]+={0,3}$/;
|
|
35
|
+
const DEFAULT_DIRS = { huddle: exports.SSH_DIR, user: path_1.default.join(os_1.default.homedir(), '.ssh') };
|
|
36
|
+
function sshString(b) {
|
|
37
|
+
const buf = typeof b === 'string' ? Buffer.from(b) : b;
|
|
38
|
+
const len = Buffer.alloc(4);
|
|
39
|
+
len.writeUInt32BE(buf.length);
|
|
40
|
+
return Buffer.concat([len, buf]);
|
|
41
|
+
}
|
|
42
|
+
/** A new ed25519 key pair in OpenSSH's formats (unencrypted private key, `ssh-ed25519 …` public key). */
|
|
43
|
+
function generateEd25519(comment) {
|
|
44
|
+
const { privateKey } = crypto_1.default.generateKeyPairSync('ed25519');
|
|
45
|
+
const jwk = privateKey.export({ format: 'jwk' });
|
|
46
|
+
const seed = Buffer.from(jwk.d, 'base64url');
|
|
47
|
+
const pub = Buffer.from(jwk.x, 'base64url');
|
|
48
|
+
const pubBlob = Buffer.concat([sshString('ssh-ed25519'), sshString(pub)]);
|
|
49
|
+
const check = crypto_1.default.randomBytes(4);
|
|
50
|
+
let priv = Buffer.concat([check, check, sshString('ssh-ed25519'), sshString(pub), sshString(Buffer.concat([seed, pub])), sshString(comment)]);
|
|
51
|
+
const pad = (8 - (priv.length % 8)) % 8;
|
|
52
|
+
priv = Buffer.concat([priv, Buffer.from(Array.from({ length: pad }, (_, i) => i + 1))]);
|
|
53
|
+
const body = Buffer.concat([
|
|
54
|
+
Buffer.from('openssh-key-v1\0'),
|
|
55
|
+
sshString('none'), sshString('none'), sshString(''),
|
|
56
|
+
Buffer.from([0, 0, 0, 1]),
|
|
57
|
+
sshString(pubBlob),
|
|
58
|
+
sshString(priv),
|
|
59
|
+
]).toString('base64');
|
|
60
|
+
return {
|
|
61
|
+
privateKey: `-----BEGIN OPENSSH PRIVATE KEY-----\n${body.match(/.{1,70}/g).join('\n')}\n-----END OPENSSH PRIVATE KEY-----\n`,
|
|
62
|
+
publicKey: `ssh-ed25519 ${pubBlob.toString('base64')} ${comment}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Huddle's public key, after making the key pair if there is none. */
|
|
66
|
+
function ensureKey(dir) {
|
|
67
|
+
(0, protocol_1.ensurePrivateDir)(dir);
|
|
68
|
+
const keyPath = path_1.default.join(dir, KEY_FILE);
|
|
69
|
+
const pubPath = `${keyPath}.pub`;
|
|
70
|
+
if (fs_1.default.existsSync(keyPath) && fs_1.default.existsSync(pubPath))
|
|
71
|
+
return fs_1.default.readFileSync(pubPath, 'utf8').trim();
|
|
72
|
+
const { privateKey, publicKey } = generateEd25519(`huddle@${os_1.default.hostname()}`);
|
|
73
|
+
fs_1.default.rmSync(keyPath, { force: true });
|
|
74
|
+
fs_1.default.writeFileSync(keyPath, privateKey, { mode: 0o600 });
|
|
75
|
+
fs_1.default.writeFileSync(pubPath, `${publicKey}\n`, { mode: 0o644 });
|
|
76
|
+
return publicKey;
|
|
77
|
+
}
|
|
78
|
+
/** Puts INCLUDE_LINE at the top of ~/.ssh/config (making it when there is none). */
|
|
79
|
+
function ensureInclude(userDir) {
|
|
80
|
+
const file = path_1.default.join(userDir, 'config');
|
|
81
|
+
let text = '';
|
|
82
|
+
try {
|
|
83
|
+
text = fs_1.default.readFileSync(file, 'utf8');
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
fs_1.default.mkdirSync(userDir, { recursive: true, mode: 0o700 });
|
|
87
|
+
}
|
|
88
|
+
if (text.split(/\r?\n/).some((l) => l.trim() === exports.INCLUDE_LINE))
|
|
89
|
+
return;
|
|
90
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n';
|
|
91
|
+
// At the top: an Include after a Host block would only apply inside that block.
|
|
92
|
+
const head = `# Added by Huddle: SSH into devcontainers as <name>.huddle${eol}${exports.INCLUDE_LINE}${eol}`;
|
|
93
|
+
fs_1.default.writeFileSync(file, text ? `${head}${eol}${text}` : head, { mode: 0o600 });
|
|
94
|
+
}
|
|
95
|
+
function prepare(dirs) {
|
|
96
|
+
try {
|
|
97
|
+
const publicKey = ensureKey(dirs.huddle);
|
|
98
|
+
(0, protocol_1.ensurePrivateDir)(path_1.default.join(dirs.huddle, 'hosts'));
|
|
99
|
+
ensureInclude(dirs.user);
|
|
100
|
+
return publicKey;
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
throw new protocol_1.PipeError('internal', `could not prepare SSH: ${err.message ?? err}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** The `ssh.key` op: Huddle's public key. */
|
|
107
|
+
function sshKey(_args, dirs = DEFAULT_DIRS) {
|
|
108
|
+
return { publicKey: prepare(dirs) };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The `ssh.host` op: (re)writes the entry for one devcontainer,
|
|
112
|
+
* `{ alias, port, user, hostKey }`, or removes it with `{ alias, remove: true }`.
|
|
113
|
+
*/
|
|
114
|
+
function sshHost(args, dirs = DEFAULT_DIRS) {
|
|
115
|
+
const { alias, port, user, hostKey, remove } = args;
|
|
116
|
+
if (typeof alias !== 'string' || !ALIAS_RE.test(alias))
|
|
117
|
+
throw new protocol_1.PipeError('bad_request', 'alias must be <container name>.huddle');
|
|
118
|
+
const hosts = path_1.default.join(dirs.huddle, 'hosts');
|
|
119
|
+
const conf = path_1.default.join(hosts, `${alias}.conf`);
|
|
120
|
+
const known = path_1.default.join(hosts, `${alias}.known_hosts`);
|
|
121
|
+
if (remove === true) {
|
|
122
|
+
fs_1.default.rmSync(conf, { force: true });
|
|
123
|
+
fs_1.default.rmSync(known, { force: true });
|
|
124
|
+
return { publicKey: null };
|
|
125
|
+
}
|
|
126
|
+
if (typeof port !== 'number' || !Number.isInteger(port) || port < 1 || port > 65535)
|
|
127
|
+
throw new protocol_1.PipeError('bad_request', 'port must be 1-65535');
|
|
128
|
+
if (typeof user !== 'string' || !USER_RE.test(user))
|
|
129
|
+
throw new protocol_1.PipeError('bad_request', 'invalid user');
|
|
130
|
+
if (typeof hostKey !== 'string' || !HOST_KEY_RE.test(hostKey.trim()))
|
|
131
|
+
throw new protocol_1.PipeError('bad_request', 'hostKey must be an ssh-ed25519 public key');
|
|
132
|
+
const publicKey = prepare(dirs);
|
|
133
|
+
try {
|
|
134
|
+
fs_1.default.writeFileSync(known, `${alias} ${hostKey.trim()}\n`, { mode: 0o600 });
|
|
135
|
+
fs_1.default.writeFileSync(conf, [
|
|
136
|
+
'# Generated by Huddle; rewritten on every Connect IDE.',
|
|
137
|
+
`Host ${alias}`,
|
|
138
|
+
' HostName localhost',
|
|
139
|
+
` Port ${port}`,
|
|
140
|
+
` User ${user}`,
|
|
141
|
+
` IdentityFile ~/.huddle/ssh/${KEY_FILE}`,
|
|
142
|
+
' IdentitiesOnly yes',
|
|
143
|
+
` HostKeyAlias ${alias}`,
|
|
144
|
+
` UserKnownHostsFile ~/.huddle/ssh/hosts/${alias}.known_hosts`,
|
|
145
|
+
' StrictHostKeyChecking yes',
|
|
146
|
+
'',
|
|
147
|
+
].join('\n'), { mode: 0o600 });
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
throw new protocol_1.PipeError('internal', `could not write the SSH entry: ${err.message ?? err}`);
|
|
151
|
+
}
|
|
152
|
+
return { publicKey };
|
|
153
|
+
}
|
package/dist/start.js
CHANGED
|
@@ -82,14 +82,15 @@ async function runStart(opts) {
|
|
|
82
82
|
}
|
|
83
83
|
await tryPrintSshLink(result.containerName);
|
|
84
84
|
}
|
|
85
|
-
// Same link as the portal's Connect IDE button: sets up sshd
|
|
86
|
-
// relay and
|
|
87
|
-
//
|
|
85
|
+
// Same link as the portal's Connect IDE button: sets up sshd, the localhost
|
|
86
|
+
// relay and the `<name>.huddle` ssh alias (key made by the host agent), and
|
|
87
|
+
// returns a JetBrains Toolbox link. Best-effort: without the host agent, or on an
|
|
88
|
+
// older image, the portal button explains what is missing.
|
|
88
89
|
async function tryPrintSshLink(containerName) {
|
|
89
90
|
try {
|
|
90
91
|
const res = await (0, api_1.get)(`/api/docker/containers/${encodeURIComponent(containerName)}/ssh-link`);
|
|
91
92
|
if (res?.link)
|
|
92
|
-
console.log(`Open in JetBrains
|
|
93
|
+
console.log(`Open in JetBrains Toolbox: ${(0, utils_1.cyan)(res.link)} (or: ssh ${containerName}.huddle)`);
|
|
93
94
|
}
|
|
94
95
|
catch (err) {
|
|
95
96
|
console.log((0, utils_1.dim)(`No SSH link yet (${err?.message ?? err}); use Connect IDE in the Huddle portal.`));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infosupport/huddle-cli",
|
|
3
|
-
"version": "0.0.0-experiment-160.
|
|
3
|
+
"version": "0.0.0-experiment-160.114.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.",
|