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

config.test.ts

167 lines5.1 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("follows 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(path.join(quotedDir, "team#1=a.conf"), "Host quoted\n");
32 yield* fs.writeFileString(path.join(homeDir, "escaped space.conf"), "Host escaped\n");
33 yield* fs.writeFileString(
34 path.join(sshDir, "config"),
35 [
36 `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
37 `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
38 "",
39 ].join("\n"),
40 );
41​
42 const hosts = yield* discoverSshHosts({ homeDir });
43 assert.deepEqual(
44 hosts.map((host) => host.alias),
45 ["escaped", "quoted"],
46 );
47 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
48 );
49​
50 it.effect("discovers ssh config hosts across included files", () =>
51 Effect.gen(function* () {
52 const fs = yield* FileSystem.FileSystem;
53 const path = yield* Path.Path;
54 const homeDir = yield* makeTempHomeDir();
55 const sshDir = path.join(homeDir, ".ssh");
56 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
57 yield* fs.writeFileString(
58 path.join(sshDir, "config"),
59 [
60 "Host devbox",
61 " HostName devbox.example.com",
62 "Host=equalsbox",
63 "Include=config.d/*.conf",
64 "",
65 ].join("\n"),
66 );
67 yield* fs.writeFileString(
68 path.join(sshDir, "config.d", "team.conf"),
69 [
70 "Host staging",
71 " HostName staging.example.com",
72 "Host *",
73 " ServerAliveInterval 30",
74 "",
75 ].join("\n"),
76 );
77 yield* fs.writeFileString(
78 path.join(sshDir, "known_hosts"),
79 [
80 "known.example.com ssh-ed25519 AAAA",
81 "|1|hashed|entry ssh-ed25519 AAAA",
82 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
83 "",
84 ].join("\n"),
85 );
86​
87 const hosts = yield* discoverSshHosts({ homeDir });
88 assert.deepEqual(hosts, [
89 {
90 alias: "bastion.example.com",
91 hostname: "bastion.example.com",
92 username: null,
93 port: null,
94 source: "known-hosts",
95 },
96 {
97 alias: "devbox",
98 hostname: "devbox",
99 username: null,
100 port: null,
101 source: "ssh-config",
102 },
103 {
104 alias: "equalsbox",
105 hostname: "equalsbox",
106 username: null,
107 port: null,
108 source: "ssh-config",
109 },
110 {
111 alias: "known.example.com",
112 hostname: "known.example.com",
113 username: null,
114 port: null,
115 source: "known-hosts",
116 },
117 {
118 alias: "staging",
119 hostname: "staging",
120 username: null,
121 port: null,
122 source: "ssh-config",
123 },
124 ]);
125 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
126 );
127​
128 it.effect("parses known_hosts entries without returning hashed hosts", () =>
129 Effect.sync(() => {
130 assert.deepEqual(
131 parseKnownHostsHostnames(
132 [
133 "github.com ssh-ed25519 AAAA",
134 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
135 "|1|hashed|entry ssh-ed25519 CCCC",
136 "@cert-authority *.example.com ssh-ed25519 DDDD",
137 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
138 "port.example.com:22 ssh-ed25519 HHHH",
139 "::1 ssh-ed25519 FFFF",
140 "2001:db8::1 ssh-ed25519 GGGG",
141 "",
142 ].join("\n"),
143 ),
144 [
145 "::1",
146 "2001:db8::1",
147 "github.com",
148 "gitlab-alias",
149 "gitlab.com",
150 "port.example.com",
151 "ssh.example.com",
152 ],
153 );
154 }),
155 );
156​
157 it.effect("expands tilde-prefixed ssh config include patterns", () =>
158 Effect.gen(function* () {
159 const pattern = yield* resolveSshConfigIncludePattern(
160 "~/.ssh/config.d/*.conf",
161 "/tmp/project",
162 "/tmp/home",
163 );
164 assert.equal(pattern, "/tmp/home/.ssh/config.d/*.conf");
165 }).pipe(Effect.provide(NodePath.layerPosix)),
166 );
167});