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

config.test.ts

279 lines9.3 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 directory Include matches without losing file aliases or known hosts", () =>
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 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, "known_hosts"),
77 "known.example.com ssh-ed25519 AAAA\n",
78 );
79​
80 const hosts = yield* discoverSshHosts({ homeDir });
81 assert.deepEqual(
82 hosts.map((host) => [host.alias, host.hostname, host.source]),
83 [
84 ["known.example.com", "known.example.com", "known-hosts"],
85 ["real", "real.example.com", "ssh-config"],
86 ["team", "team.example.com", "ssh-config"],
87 ],
88 );
89 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
90 );
91​
92 it.effect("discovers ssh config hosts across included files", () =>
93 Effect.gen(function* () {
94 const fs = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const homeDir = yield* makeTempHomeDir();
97 const sshDir = path.join(homeDir, ".ssh");
98 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
99 yield* fs.writeFileString(
100 path.join(sshDir, "config"),
101 [
102 "Host devbox devbox-admin",
103 " HostName 'devbox.example.com'",
104 "Host=equalsbox",
105 "Host *",
106 "Include=config.d/*.conf",
107 "",
108 ].join("\n"),
109 );
110 yield* fs.writeFileString(
111 path.join(sshDir, "config.d", "team.conf"),
112 [
113 "Host staging",
114 " HostName staging.example.com",
115 "Host *",
116 " ServerAliveInterval 30",
117 "",
118 ].join("\n"),
119 );
120 yield* fs.writeFileString(
121 path.join(sshDir, "known_hosts"),
122 [
123 "devbox.example.com ssh-ed25519 AAAA",
124 "staging.example.com ssh-ed25519 AAAA",
125 "known.example.com ssh-ed25519 AAAA",
126 "|1|hashed|entry ssh-ed25519 AAAA",
127 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
128 "",
129 ].join("\n"),
130 );
131​
132 const hosts = yield* discoverSshHosts({ homeDir });
133 assert.deepEqual(hosts, [
134 {
135 alias: "bastion.example.com",
136 hostname: "bastion.example.com",
137 username: null,
138 port: null,
139 source: "known-hosts",
140 },
141 {
142 alias: "devbox",
143 hostname: "devbox.example.com",
144 username: null,
145 port: null,
146 source: "ssh-config",
147 },
148 {
149 alias: "devbox-admin",
150 hostname: "devbox.example.com",
151 username: null,
152 port: null,
153 source: "ssh-config",
154 },
155 {
156 alias: "equalsbox",
157 hostname: "equalsbox",
158 username: null,
159 port: null,
160 source: "ssh-config",
161 },
162 {
163 alias: "known.example.com",
164 hostname: "known.example.com",
165 username: null,
166 port: null,
167 source: "known-hosts",
168 },
169 {
170 alias: "staging",
171 hostname: "staging.example.com",
172 username: null,
173 port: null,
174 source: "ssh-config",
175 },
176 ]);
177 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
178 );
179​
180 it.effect("retains raw targets when included hostnames are inactive or uncertain", () =>
181 Effect.gen(function* () {
182 const fs = yield* FileSystem.FileSystem;
183 const path = yield* Path.Path;
184 const homeDir = yield* makeTempHomeDir();
185 const sshDir = path.join(homeDir, ".ssh");
186 yield* fs.makeDirectory(sshDir, { recursive: true });
187 yield* fs.writeFileString(
188 path.join(sshDir, "config"),
189 [
190 "Host WORK*",
191 " HostName case-sensitive.example.com",
192 "Host work",
193 "Host dev",
194 " Include conditional.conf",
195 "Host prod",
196 "Host matched",
197 "Match originalhost matched",
198 " Include match.conf",
199 "Host matched",
200 " HostName fallback.example.com",
201 "Host bracketed",
202 " Include bracket-[ab].conf",
203 " HostName bracket-fallback.example.com",
204 "",
205 ].join("\n"),
206 );
207 yield* fs.writeFileString(
208 path.join(sshDir, "conditional.conf"),
209 "Host prod\n HostName blocked.example.com\n",
210 );
211 yield* fs.writeFileString(
212 path.join(sshDir, "match.conf"),
213 "Host matched\n HostName match.example.com\n",
214 );
215 yield* fs.writeFileString(
216 path.join(sshDir, "bracket-a.conf"),
217 "HostName bracket-real.example.com\n",
218 );
219 yield* fs.writeFileString(
220 path.join(sshDir, "known_hosts"),
221 [
222 "bracket-real.example.com ssh-ed25519 AAAA",
223 "bracket-fallback.example.com ssh-ed25519 AAAA",
224 "blocked.example.com ssh-ed25519 AAAA",
225 "case-sensitive.example.com ssh-ed25519 AAAA",
226 "match.example.com ssh-ed25519 AAAA",
227 "fallback.example.com ssh-ed25519 AAAA",
228 "",
229 ].join("\n"),
230 );
231​
232 const hosts = yield* discoverSshHosts({ homeDir });
233 assert.deepEqual(
234 hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname),
235 [
236 "blocked.example.com",
237 "bracket-fallback.example.com",
238 "bracket-real.example.com",
239 "case-sensitive.example.com",
240 "fallback.example.com",
241 "match.example.com",
242 ],
243 );
244 assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed");
245 assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod");
246 assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work");
247 assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched");
248 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
249 );
250​
251 it.effect("parses known_hosts entries without returning hashed hosts", () =>
252 Effect.sync(() => {
253 assert.deepEqual(
254 parseKnownHostsHostnames(
255 [
256 "github.com ssh-ed25519 AAAA",
257 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
258 "|1|hashed|entry ssh-ed25519 CCCC",
259 "@cert-authority *.example.com ssh-ed25519 DDDD",
260 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
261 "port.example.com:22 ssh-ed25519 HHHH",
262 "::1 ssh-ed25519 FFFF",
263 "2001:db8::1 ssh-ed25519 GGGG",
264 "",
265 ].join("\n"),
266 ),
267 [
268 "::1",
269 "2001:db8::1",
270 "github.com",
271 "gitlab-alias",
272 "gitlab.com",
273 "port.example.com",
274 "ssh.example.com",
275 ],
276 );
277 }),
278 );
279});