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

config.test.ts

262 lines8.4 KB

1import * as NodePath from "@effect/platform-node/NodePath";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7​
8import {
9 discoverSshHosts,
10 parseKnownHostsHostnames,
11 resolveSshConfigIncludePattern,
12} from "./config.ts";
13​
14function makeTempHomeDir() {
15 return Effect.gen(function* () {
16 const fs = yield* FileSystem.FileSystem;
17 return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-test-" });
18 });
20​
21describe("ssh config", () => {
22 it.effect("deduplicates hosts through quoted and escaped Include paths like OpenSSH", () =>
23 Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const homeDir = yield* makeTempHomeDir();
27 const sshDir = path.join(homeDir, ".ssh");
28 const quotedDir = path.join(homeDir, "quoted dir");
29 yield* fs.makeDirectory(sshDir);
30 yield* fs.makeDirectory(quotedDir);
31 yield* fs.writeFileString(
32 path.join(quotedDir, "team#1=a.conf"),
33 'Host quoted\n HostName quoted".example.com"\nHost *\n',
34 );
35 yield* fs.writeFileString(
36 path.join(homeDir, "escaped space.conf"),
37 "Host escaped\n HostName escaped.example.com\nHost *\n",
38 );
39 yield* fs.writeFileString(
40 path.join(sshDir, "config"),
41 [
42 `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
43 `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
44 "",
45 ].join("\n"),
46 );
47​
48 yield* fs.writeFileString(
49 path.join(sshDir, "known_hosts"),
50 "escaped.example.com ssh-ed25519 AAAA\nquoted.example.com ssh-ed25519 AAAA\n",
51 );
52​
53 const hosts = yield* discoverSshHosts({ homeDir });
54 assert.deepEqual(
55 hosts.map((host) => [host.alias, host.hostname, host.source]),
56 [
57 ["escaped", "escaped.example.com", "ssh-config"],
58 ["quoted", "quoted.example.com", "ssh-config"],
59 ],
60 );
61 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
62 );
63​
64 it.effect("discovers ssh config hosts across included files", () =>
65 Effect.gen(function* () {
66 const fs = yield* FileSystem.FileSystem;
67 const path = yield* Path.Path;
68 const homeDir = yield* makeTempHomeDir();
69 const sshDir = path.join(homeDir, ".ssh");
70 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
71 yield* fs.writeFileString(
72 path.join(sshDir, "config"),
73 [
74 "Host devbox devbox-admin",
75 " HostName 'devbox.example.com'",
76 "Host=equalsbox",
77 "Host *",
78 "Include=config.d/*.conf",
79 "",
80 ].join("\n"),
81 );
82 yield* fs.writeFileString(
83 path.join(sshDir, "config.d", "team.conf"),
84 [
85 "Host staging",
86 " HostName staging.example.com",
87 "Host *",
88 " ServerAliveInterval 30",
89 "",
90 ].join("\n"),
91 );
92 yield* fs.writeFileString(
93 path.join(sshDir, "known_hosts"),
94 [
95 "devbox.example.com ssh-ed25519 AAAA",
96 "staging.example.com ssh-ed25519 AAAA",
97 "known.example.com ssh-ed25519 AAAA",
98 "|1|hashed|entry ssh-ed25519 AAAA",
99 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
100 "",
101 ].join("\n"),
102 );
103​
104 const hosts = yield* discoverSshHosts({ homeDir });
105 assert.deepEqual(hosts, [
106 {
107 alias: "bastion.example.com",
108 hostname: "bastion.example.com",
109 username: null,
110 port: null,
111 source: "known-hosts",
112 },
113 {
114 alias: "devbox",
115 hostname: "devbox.example.com",
116 username: null,
117 port: null,
118 source: "ssh-config",
119 },
120 {
121 alias: "devbox-admin",
122 hostname: "devbox.example.com",
123 username: null,
124 port: null,
125 source: "ssh-config",
126 },
127 {
128 alias: "equalsbox",
129 hostname: "equalsbox",
130 username: null,
131 port: null,
132 source: "ssh-config",
133 },
134 {
135 alias: "known.example.com",
136 hostname: "known.example.com",
137 username: null,
138 port: null,
139 source: "known-hosts",
140 },
141 {
142 alias: "staging",
143 hostname: "staging.example.com",
144 username: null,
145 port: null,
146 source: "ssh-config",
147 },
148 ]);
149 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
150 );
151​
152 it.effect("retains raw targets when included hostnames are inactive or uncertain", () =>
153 Effect.gen(function* () {
154 const fs = yield* FileSystem.FileSystem;
155 const path = yield* Path.Path;
156 const homeDir = yield* makeTempHomeDir();
157 const sshDir = path.join(homeDir, ".ssh");
158 yield* fs.makeDirectory(sshDir, { recursive: true });
159 yield* fs.writeFileString(
160 path.join(sshDir, "config"),
161 [
162 "Host WORK*",
163 " HostName case-sensitive.example.com",
164 "Host work",
165 "Host dev",
166 " Include conditional.conf",
167 "Host prod",
168 "Host matched",
169 "Match originalhost matched",
170 " Include match.conf",
171 "Host matched",
172 " HostName fallback.example.com",
173 "Host bracketed",
174 " Include bracket-[ab].conf",
175 " HostName bracket-fallback.example.com",
176 "",
177 ].join("\n"),
178 );
179 yield* fs.writeFileString(
180 path.join(sshDir, "conditional.conf"),
181 "Host prod\n HostName blocked.example.com\n",
182 );
183 yield* fs.writeFileString(
184 path.join(sshDir, "match.conf"),
185 "Host matched\n HostName match.example.com\n",
186 );
187 yield* fs.writeFileString(
188 path.join(sshDir, "bracket-a.conf"),
189 "HostName bracket-real.example.com\n",
190 );
191 yield* fs.writeFileString(
192 path.join(sshDir, "known_hosts"),
193 [
194 "bracket-real.example.com ssh-ed25519 AAAA",
195 "bracket-fallback.example.com ssh-ed25519 AAAA",
196 "blocked.example.com ssh-ed25519 AAAA",
197 "case-sensitive.example.com ssh-ed25519 AAAA",
198 "match.example.com ssh-ed25519 AAAA",
199 "fallback.example.com ssh-ed25519 AAAA",
200 "",
201 ].join("\n"),
202 );
203​
204 const hosts = yield* discoverSshHosts({ homeDir });
205 assert.deepEqual(
206 hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname),
207 [
208 "blocked.example.com",
209 "bracket-fallback.example.com",
210 "bracket-real.example.com",
211 "case-sensitive.example.com",
212 "fallback.example.com",
213 "match.example.com",
214 ],
215 );
216 assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed");
217 assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod");
218 assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work");
219 assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched");
220 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
221 );
222​
223 it.effect("parses known_hosts entries without returning hashed hosts", () =>
224 Effect.sync(() => {
225 assert.deepEqual(
226 parseKnownHostsHostnames(
227 [
228 "github.com ssh-ed25519 AAAA",
229 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
230 "|1|hashed|entry ssh-ed25519 CCCC",
231 "@cert-authority *.example.com ssh-ed25519 DDDD",
232 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
233 "port.example.com:22 ssh-ed25519 HHHH",
234 "::1 ssh-ed25519 FFFF",
235 "2001:db8::1 ssh-ed25519 GGGG",
236 "",
237 ].join("\n"),
238 ),
239 [
240 "::1",
241 "2001:db8::1",
242 "github.com",
243 "gitlab-alias",
244 "gitlab.com",
245 "port.example.com",
246 "ssh.example.com",
247 ],
248 );
249 }),
250 );
251​
252 it.effect("expands tilde-prefixed ssh config include patterns", () =>
253 Effect.gen(function* () {
254 const pattern = yield* resolveSshConfigIncludePattern(
255 "~/.ssh/config.d/*.conf",
256 "/tmp/project",
257 "/tmp/home",
258 );
259 assert.equal(pattern, "/tmp/home/.ssh/config.d/*.conf");
260 }).pipe(Effect.provide(NodePath.layerPosix)),
261 );
262});