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

config.test.ts

138 lines3.9 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 {
8 discoverSshHosts,
9 parseKnownHostsHostnames,
10 resolveSshConfigIncludePattern,
11} from "./config.ts";
12​
13function makeTempHomeDir() {
14 return Effect.gen(function* () {
15 const fs = yield* FileSystem.FileSystem;
16 return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-test-" });
17 });
19​
20describe("ssh config", () => {
21 it.effect("discovers ssh config hosts across included files", () =>
22 Effect.gen(function* () {
23 const fs = yield* FileSystem.FileSystem;
24 const path = yield* Path.Path;
25 const homeDir = yield* makeTempHomeDir();
26 const sshDir = path.join(homeDir, ".ssh");
27 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
28 yield* fs.writeFileString(
29 path.join(sshDir, "config"),
30 [
31 "Host devbox",
32 " HostName devbox.example.com",
33 "Host=equalsbox",
34 "Include=config.d/*.conf",
35 "",
36 ].join("\n"),
37 );
38 yield* fs.writeFileString(
39 path.join(sshDir, "config.d", "team.conf"),
40 [
41 "Host staging",
42 " HostName staging.example.com",
43 "Host *",
44 " ServerAliveInterval 30",
45 "",
46 ].join("\n"),
47 );
48 yield* fs.writeFileString(
49 path.join(sshDir, "known_hosts"),
50 [
51 "known.example.com ssh-ed25519 AAAA",
52 "|1|hashed|entry ssh-ed25519 AAAA",
53 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
54 "",
55 ].join("\n"),
56 );
57​
58 const hosts = yield* discoverSshHosts({ homeDir });
59 assert.deepEqual(hosts, [
60 {
61 alias: "bastion.example.com",
62 hostname: "bastion.example.com",
63 username: null,
64 port: null,
65 source: "known-hosts",
66 },
67 {
68 alias: "devbox",
69 hostname: "devbox",
70 username: null,
71 port: null,
72 source: "ssh-config",
73 },
74 {
75 alias: "equalsbox",
76 hostname: "equalsbox",
77 username: null,
78 port: null,
79 source: "ssh-config",
80 },
81 {
82 alias: "known.example.com",
83 hostname: "known.example.com",
84 username: null,
85 port: null,
86 source: "known-hosts",
87 },
88 {
89 alias: "staging",
90 hostname: "staging",
91 username: null,
92 port: null,
93 source: "ssh-config",
94 },
95 ]);
96 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
97 );
98​
99 it.effect("parses known_hosts entries without returning hashed hosts", () =>
100 Effect.sync(() => {
101 assert.deepEqual(
102 parseKnownHostsHostnames(
103 [
104 "github.com ssh-ed25519 AAAA",
105 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
106 "|1|hashed|entry ssh-ed25519 CCCC",
107 "@cert-authority *.example.com ssh-ed25519 DDDD",
108 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
109 "port.example.com:22 ssh-ed25519 HHHH",
110 "::1 ssh-ed25519 FFFF",
111 "2001:db8::1 ssh-ed25519 GGGG",
112 "",
113 ].join("\n"),
114 ),
115 [
116 "::1",
117 "2001:db8::1",
118 "github.com",
119 "gitlab-alias",
120 "gitlab.com",
121 "port.example.com",
122 "ssh.example.com",
123 ],
124 );
125 }),
126 );
127​
128 it.effect("expands tilde-prefixed ssh config include patterns", () =>
129 Effect.gen(function* () {
130 const pattern = yield* resolveSshConfigIncludePattern(
131 "~/.ssh/config.d/*.conf",
132 "/tmp/project",
133 "/tmp/home",
134 );
135 assert.equal(pattern, "/tmp/home/.ssh/config.d/*.conf");
136 }).pipe(Effect.provide(NodeServices.layer)),
137 );
138});