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

config.test.ts

293 lines10.0 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, parseKnownHostsHostnames } 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 and escaped Include paths like OpenSSH", () =>
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 yield* fs.writeFileString(
35 path.join(sshDir, "config"),
36 [
37 `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
38 `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
39 "",
40 ].join("\n"),
41 );
42​
43 yield* fs.writeFileString(
44 path.join(sshDir, "known_hosts"),
45 "escaped.example.com ssh-ed25519 AAAA\nquoted.example.com ssh-ed25519 AAAA\n",
46 );
47​
48 const hosts = yield* discoverSshHosts({ homeDir });
49 assert.deepEqual(
50 hosts.map((host) => [host.alias, host.hostname, host.source]),
51 [
52 ["escaped", "escaped.example.com", "ssh-config"],
53 ["quoted", "quoted.example.com", "ssh-config"],
54 ],
55 );
56 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
57 );
58​
59 it.effect("skips directories and implicit dotfile Include matches but keeps explicit ones", () =>
60 Effect.gen(function* () {
61 const fs = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 const homeDir = yield* makeTempHomeDir();
64 const sshDir = path.join(homeDir, ".ssh");
65 yield* fs.makeDirectory(path.join(sshDir, "config.d", "ignored"), { recursive: true });
66 yield* fs.makeDirectory(path.join(sshDir, "explicit-directory"));
67 yield* fs.writeFileString(
68 path.join(sshDir, "config"),
69 "Include config.d/*\nInclude config.d/.literal.conf\nInclude config.d/.enabled-*.conf\nInclude explicit-directory\nHost real\n HostName real.example.com\n",
70 );
71 yield* fs.writeFileString(
72 path.join(sshDir, "config.d", "team.conf"),
73 "Host team\n HostName team.example.com\n",
74 );
75 yield* fs.writeFileString(
76 path.join(sshDir, "config.d", ".backup.conf"),
77 "Host *\n HostName obsolete.example.com\n",
78 );
79 yield* fs.writeFileString(
80 path.join(sshDir, "config.d", ".literal.conf"),
81 "Host literal\n HostName literal.example.com\n",
82 );
83 yield* fs.writeFileString(
84 path.join(sshDir, "config.d", ".enabled-one.conf"),
85 "Host enabled\n HostName enabled.example.com\n",
86 );
87 yield* fs.writeFileString(
88 path.join(sshDir, "known_hosts"),
89 "known.example.com ssh-ed25519 AAAA\n",
90 );
91​
92 const hosts = yield* discoverSshHosts({ homeDir });
93 assert.deepEqual(
94 hosts.map((host) => [host.alias, host.hostname, host.source]),
95 [
96 ["enabled", "enabled.example.com", "ssh-config"],
97 ["known.example.com", "known.example.com", "known-hosts"],
98 ["literal", "literal.example.com", "ssh-config"],
99 ["real", "real.example.com", "ssh-config"],
100 ["team", "team.example.com", "ssh-config"],
101 ],
102 );
103 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
104 );
105​
106 it.effect("discovers ssh config hosts across included files", () =>
107 Effect.gen(function* () {
108 const fs = yield* FileSystem.FileSystem;
109 const path = yield* Path.Path;
110 const homeDir = yield* makeTempHomeDir();
111 const sshDir = path.join(homeDir, ".ssh");
112 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
113 yield* fs.writeFileString(
114 path.join(sshDir, "config"),
115 [
116 "Host devbox devbox-admin",
117 " HostName 'devbox.example.com'",
118 "Host=equalsbox",
119 "Host *",
120 "Include=config.d/*.conf",
121 "",
122 ].join("\n"),
123 );
124 yield* fs.writeFileString(
125 path.join(sshDir, "config.d", "team.conf"),
126 [
127 "Host staging",
128 " HostName staging.example.com",
129 "Host *",
130 " ServerAliveInterval 30",
131 "",
132 ].join("\n"),
133 );
134 yield* fs.writeFileString(
135 path.join(sshDir, "known_hosts"),
136 [
137 "devbox.example.com ssh-ed25519 AAAA",
138 "staging.example.com ssh-ed25519 AAAA",
139 "known.example.com ssh-ed25519 AAAA",
140 "|1|hashed|entry ssh-ed25519 AAAA",
141 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
142 "",
143 ].join("\n"),
144 );
145​
146 const hosts = yield* discoverSshHosts({ homeDir });
147 assert.deepEqual(hosts, [
148 {
149 alias: "bastion.example.com",
150 hostname: "bastion.example.com",
151 username: null,
152 port: null,
153 source: "known-hosts",
154 },
155 {
156 alias: "devbox",
157 hostname: "devbox.example.com",
158 username: null,
159 port: null,
160 source: "ssh-config",
161 },
162 {
163 alias: "devbox-admin",
164 hostname: "devbox.example.com",
165 username: null,
166 port: null,
167 source: "ssh-config",
168 },
169 {
170 alias: "equalsbox",
171 hostname: "equalsbox",
172 username: null,
173 port: null,
174 source: "ssh-config",
175 },
176 {
177 alias: "known.example.com",
178 hostname: "known.example.com",
179 username: null,
180 port: null,
181 source: "known-hosts",
182 },
183 {
184 alias: "staging",
185 hostname: "staging.example.com",
186 username: null,
187 port: null,
188 source: "ssh-config",
189 },
190 ]);
191 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
192 );
193​
194 it.effect("retains raw targets when included hostnames are inactive or uncertain", () =>
195 Effect.gen(function* () {
196 const fs = yield* FileSystem.FileSystem;
197 const path = yield* Path.Path;
198 const homeDir = yield* makeTempHomeDir();
199 const sshDir = path.join(homeDir, ".ssh");
200 yield* fs.makeDirectory(sshDir, { recursive: true });
201 yield* fs.writeFileString(
202 path.join(sshDir, "config"),
203 [
204 "Host WORK*",
205 " HostName case-sensitive.example.com",
206 "Host work",
207 "Host dev",
208 " Include conditional.conf",
209 "Host prod",
210 "Host matched",
211 "Match originalhost matched",
212 " Include match.conf",
213 "Host matched",
214 " HostName fallback.example.com",
215 "Host bracketed",
216 " Include bracket-[ab].conf",
217 " HostName bracket-fallback.example.com",
218 "",
219 ].join("\n"),
220 );
221 yield* fs.writeFileString(
222 path.join(sshDir, "conditional.conf"),
223 "Host prod\n HostName blocked.example.com\n",
224 );
225 yield* fs.writeFileString(
226 path.join(sshDir, "match.conf"),
227 "Host matched\n HostName match.example.com\n",
228 );
229 yield* fs.writeFileString(
230 path.join(sshDir, "bracket-a.conf"),
231 "HostName bracket-real.example.com\n",
232 );
233 yield* fs.writeFileString(
234 path.join(sshDir, "known_hosts"),
235 [
236 "bracket-real.example.com ssh-ed25519 AAAA",
237 "bracket-fallback.example.com ssh-ed25519 AAAA",
238 "blocked.example.com ssh-ed25519 AAAA",
239 "case-sensitive.example.com ssh-ed25519 AAAA",
240 "match.example.com ssh-ed25519 AAAA",
241 "fallback.example.com ssh-ed25519 AAAA",
242 "",
243 ].join("\n"),
244 );
245​
246 const hosts = yield* discoverSshHosts({ homeDir });
247 assert.deepEqual(
248 hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname),
249 [
250 "blocked.example.com",
251 "bracket-fallback.example.com",
252 "bracket-real.example.com",
253 "case-sensitive.example.com",
254 "fallback.example.com",
255 "match.example.com",
256 ],
257 );
258 assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed");
259 assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod");
260 assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work");
261 assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched");
262 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
263 );
264​
265 it.effect("parses known_hosts entries without returning hashed hosts", () =>
266 Effect.sync(() => {
267 assert.deepEqual(
268 parseKnownHostsHostnames(
269 [
270 "github.com ssh-ed25519 AAAA",
271 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
272 "|1|hashed|entry ssh-ed25519 CCCC",
273 "@cert-authority *.example.com ssh-ed25519 DDDD",
274 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
275 "port.example.com:22 ssh-ed25519 HHHH",
276 "::1 ssh-ed25519 FFFF",
277 "2001:db8::1 ssh-ed25519 GGGG",
278 "",
279 ].join("\n"),
280 ),
281 [
282 "::1",
283 "2001:db8::1",
284 "github.com",
285 "gitlab-alias",
286 "gitlab.com",
287 "port.example.com",
288 "ssh.example.com",
289 ],
290 );
291 }),
292 );
293});