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