Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

config.test.ts

337 lines11.8 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6​
7import { discoverSshHosts, parseKnownHosts } from "./config.ts";
8​
9function makeTempHomeDir() {
10 return Effect.gen(function* () {
11 const fs = yield* FileSystem.FileSystem;
12 return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-test-" });
13 });
15​
16describe("ssh config", () => {
17 it.effect("deduplicates hosts through quoted, escaped, and Unicode Includes", () =>
18 Effect.gen(function* () {
19 const fs = yield* FileSystem.FileSystem;
20 const path = yield* Path.Path;
21 const homeDir = yield* makeTempHomeDir();
22 const sshDir = path.join(homeDir, ".ssh");
23 const quotedDir = path.join(homeDir, "quoted dir");
24 yield* fs.makeDirectory(sshDir);
25 yield* fs.makeDirectory(quotedDir);
26 yield* fs.writeFileString(
27 path.join(quotedDir, "team#1=a.conf"),
28 'Host quoted\n HostName quoted".example.com"\nHost *\n',
29 );
30 yield* fs.writeFileString(
31 path.join(homeDir, "escaped space.conf"),
32 "Host escaped\n HostName escaped.example.com\nHost *\n",
33 );
34 const unicodeFiles = [
35 "\u00a0team\u00a0one.conf",
36 "team\u2003one.conf",
37 "team\u2028one.conf",
38 "team\u2029one.conf",
39 ].flatMap((file) => [file, `quoted-${file}`]);
40 for (const [index, file] of unicodeFiles.entries()) {
41 yield* fs.writeFileString(
42 path.join(sshDir, file),
43 `Host unicode-${index}\n HostName unicode-${index}.example.com\nHost *\n`,
44 );
45 }
46 yield* fs.writeFileString(
47 path.join(sshDir, "config"),
48 [
49 `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
50 `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
51 `Include ${unicodeFiles.map((file, index) => (index % 2 === 0 ? file : `"${file}"`)).join("\t")}`,
52 ].join("\n") + "\r",
53 );
54​
55 yield* fs.writeFileString(
56 path.join(sshDir, "known_hosts"),
57 "escaped.example.com ssh-ed25519 AAAA\nquoted.example.com ssh-ed25519 AAAA\n",
58 );
59​
60 const hosts = yield* discoverSshHosts({ homeDir });
61 assert.deepEqual(
62 hosts.map((host) => [host.alias, host.hostname, host.source]),
63 [
64 ["escaped", "escaped.example.com", "ssh-config"],
65 ["quoted", "quoted.example.com", "ssh-config"],
66 ...unicodeFiles.map((_, index) => [
67 `unicode-${index}`,
68 `unicode-${index}.example.com`,
69 "ssh-config",
70 ]),
71 ],
72 );
73 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
74 );
75​
76 it.effect("skips directories and implicit dotfile Include matches but keeps explicit ones", () =>
77 Effect.gen(function* () {
78 const fs = yield* FileSystem.FileSystem;
79 const path = yield* Path.Path;
80 const homeDir = yield* makeTempHomeDir();
81 const sshDir = path.join(homeDir, ".ssh");
82 yield* fs.makeDirectory(path.join(sshDir, "config.d", "ignored"), { recursive: true });
83 yield* fs.makeDirectory(path.join(sshDir, "explicit-directory"));
84 yield* fs.writeFileString(
85 path.join(sshDir, "config"),
86 "Include config.d/*\nInclude config.d/.literal.conf\nInclude config.d/.enabled-*.conf\nInclude explicit-directory\nHost real\n HostName real.example.com\n",
87 );
88 yield* fs.writeFileString(
89 path.join(sshDir, "config.d", "team.conf"),
90 "Host team\n HostName team.example.com\n",
91 );
92 yield* fs.writeFileString(
93 path.join(sshDir, "config.d", ".backup.conf"),
94 "Host *\n HostName obsolete.example.com\n",
95 );
96 yield* fs.writeFileString(
97 path.join(sshDir, "config.d", ".literal.conf"),
98 "Host literal\n HostName literal.example.com\n",
99 );
100 yield* fs.writeFileString(
101 path.join(sshDir, "config.d", ".enabled-one.conf"),
102 "Host enabled\n HostName enabled.example.com\n",
103 );
104 yield* fs.writeFileString(
105 path.join(sshDir, "known_hosts"),
106 "known.example.com ssh-ed25519 AAAA\n",
107 );
108​
109 const hosts = yield* discoverSshHosts({ homeDir });
110 assert.deepEqual(
111 hosts.map((host) => [host.alias, host.hostname, host.source]),
112 [
113 ["enabled", "enabled.example.com", "ssh-config"],
114 ["known.example.com", "known.example.com", "known-hosts"],
115 ["literal", "literal.example.com", "ssh-config"],
116 ["real", "real.example.com", "ssh-config"],
117 ["team", "team.example.com", "ssh-config"],
118 ],
119 );
120 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
121 );
122​
123 it.effect("discovers ssh config hosts across included files", () =>
124 Effect.gen(function* () {
125 const fs = yield* FileSystem.FileSystem;
126 const path = yield* Path.Path;
127 const homeDir = yield* makeTempHomeDir();
128 const sshDir = path.join(homeDir, ".ssh");
129 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
130 yield* fs.writeFileString(
131 path.join(sshDir, "config"),
132 [
133 "Host devbox devbox-admin",
134 " HostName 'devbox.example.com'",
135 "Host=equalsbox",
136 "Host *",
137 "Include=config.d/*.conf",
138 "",
139 ].join("\n"),
140 );
141 yield* fs.writeFileString(
142 path.join(sshDir, "config.d", "team.conf"),
143 [
144 "Host staging",
145 " HostName staging.example.com",
146 "Host *",
147 " ServerAliveInterval 30",
148 "",
149 ].join("\n"),
150 );
151 yield* fs.writeFileString(
152 path.join(sshDir, "known_hosts"),
153 [
154 "devbox.example.com ssh-ed25519 AAAA",
155 "staging.example.com ssh-ed25519 AAAA",
156 "known.example.com ssh-ed25519 AAAA",
157 "|1|hashed|entry ssh-ed25519 AAAA",
158 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
159 "",
160 ].join("\n"),
161 );
162​
163 const hosts = yield* discoverSshHosts({ homeDir });
164 assert.deepEqual(hosts, [
165 {
166 alias: "bastion.example.com",
167 hostname: "bastion.example.com",
168 username: null,
169 port: 2222,
170 source: "known-hosts",
171 },
172 {
173 alias: "devbox",
174 hostname: "devbox.example.com",
175 username: null,
176 port: null,
177 source: "ssh-config",
178 },
179 {
180 alias: "devbox-admin",
181 hostname: "devbox.example.com",
182 username: null,
183 port: null,
184 source: "ssh-config",
185 },
186 {
187 alias: "equalsbox",
188 hostname: "equalsbox",
189 username: null,
190 port: null,
191 source: "ssh-config",
192 },
193 {
194 alias: "known.example.com",
195 hostname: "known.example.com",
196 username: null,
197 port: null,
198 source: "known-hosts",
199 },
200 {
201 alias: "staging",
202 hostname: "staging.example.com",
203 username: null,
204 port: null,
205 source: "ssh-config",
206 },
207 ]);
208 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
209 );
210​
211 it.effect("retains raw targets when included hostnames are inactive or uncertain", () =>
212 Effect.gen(function* () {
213 const fs = yield* FileSystem.FileSystem;
214 const path = yield* Path.Path;
215 const homeDir = yield* makeTempHomeDir();
216 const sshDir = path.join(homeDir, ".ssh");
217 yield* fs.makeDirectory(sshDir, { recursive: true });
218 yield* fs.writeFileString(
219 path.join(sshDir, "config"),
220 [
221 "Host WORK*",
222 " HostName case-sensitive.example.com",
223 "Host work",
224 "Host dev",
225 " Include conditional.conf",
226 "Host prod",
227 "Host matched",
228 "Match originalhost matched",
229 " Include match.conf",
230 "Host matched",
231 " HostName fallback.example.com",
232 "Host bracketed",
233 " Include bracket-[ab].conf",
234 " HostName bracket-fallback.example.com",
235 "",
236 ].join("\n"),
237 );
238 yield* fs.writeFileString(
239 path.join(sshDir, "conditional.conf"),
240 "Host prod\n HostName blocked.example.com\n",
241 );
242 yield* fs.writeFileString(
243 path.join(sshDir, "match.conf"),
244 "Host matched\n HostName match.example.com\n",
245 );
246 yield* fs.writeFileString(
247 path.join(sshDir, "bracket-a.conf"),
248 "HostName bracket-real.example.com\n",
249 );
250 yield* fs.writeFileString(
251 path.join(sshDir, "known_hosts"),
252 [
253 "bracket-real.example.com ssh-ed25519 AAAA",
254 "bracket-fallback.example.com ssh-ed25519 AAAA",
255 "blocked.example.com ssh-ed25519 AAAA",
256 "case-sensitive.example.com ssh-ed25519 AAAA",
257 "match.example.com ssh-ed25519 AAAA",
258 "fallback.example.com ssh-ed25519 AAAA",
259 "",
260 ].join("\n"),
261 );
262​
263 const hosts = yield* discoverSshHosts({ homeDir });
264 assert.deepEqual(
265 hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname),
266 [
267 "blocked.example.com",
268 "bracket-fallback.example.com",
269 "bracket-real.example.com",
270 "case-sensitive.example.com",
271 "fallback.example.com",
272 "match.example.com",
273 ],
274 );
275 assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed");
276 assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod");
277 assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work");
278 assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched");
279 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
280 );
281​
282 it.effect("keeps each explicit known_hosts port as its own destination", () =>
283 Effect.gen(function* () {
284 const fs = yield* FileSystem.FileSystem;
285 const path = yield* Path.Path;
286 const homeDir = yield* makeTempHomeDir();
287 const sshDir = path.join(homeDir, ".ssh");
288 yield* fs.makeDirectory(sshDir);
289 yield* fs.writeFileString(
290 path.join(sshDir, "known_hosts"),
291 [
292 "[100.64.0.1]:2222 ssh-ed25519 AAAA",
293 "[100.64.0.1]:2223 ssh-ed25519 AAAA",
294 "[fd7a:115c:a1e0::1]:2222 ssh-ed25519 AAAA",
295 "",
296 ].join("\n"),
297 );
298​
299 const hosts = yield* discoverSshHosts({ homeDir });
300 assert.deepEqual(
301 hosts
302 .map((host) => `${host.hostname} ${host.port}`)
303 .toSorted((left, right) => left.localeCompare(right)),
304 ["100.64.0.1 2222", "100.64.0.1 2223", "fd7a:115c:a1e0::1 2222"],
305 );
306 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
307 );
308​
309 it.effect("parses known_hosts entries without returning hashed hosts", () =>
310 Effect.sync(() => {
311 assert.deepEqual(
312 parseKnownHosts(
313 [
314 "github.com ssh-ed25519 AAAA",
315 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
316 "|1|hashed|entry ssh-ed25519 CCCC",
317 "@cert-authority *.example.com ssh-ed25519 DDDD",
318 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
319 "port.example.com:22 ssh-ed25519 HHHH",
320 "::1 ssh-ed25519 FFFF",
321 "2001:db8::1 ssh-ed25519 GGGG",
322 "",
323 ].join("\n"),
324 ),
325 [
326 { hostname: "::1", port: null },
327 { hostname: "2001:db8::1", port: null },
328 { hostname: "github.com", port: null },
329 { hostname: "gitlab-alias", port: null },
330 { hostname: "gitlab.com", port: null },
331 { hostname: "port.example.com", port: null },
332 { hostname: "ssh.example.com", port: 2200 },
333 ],
334 );
335 }),
336 );
337});