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

auth.test.ts

222 lines8.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3​
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as Path from "effect/Path";
11import * as HostProcess from "@t3tools/shared/HostProcess";
12import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
13​
14import { buildSshChildEnvironment, isSshAuthFailure } from "./auth.ts";
15​
16describe("ssh auth", () => {
17 it.effect("detects ssh auth failures from common permission denied messages", () =>
18 Effect.sync(() => {
19 assert.equal(
20 isSshAuthFailure(
21 new Error(
22 "julius@100.65.180.100: Permission denied (publickey,password,keyboard-interactive).",
23 ),
24 ),
25 true,
26 );
27 assert.equal(isSshAuthFailure(new Error("Permission denied (publickey).")), true);
28 assert.equal(isSshAuthFailure(new Error("Connection timed out")), false);
29 assert.equal(isSshAuthFailure(new Error("mkdir: Permission denied")), false);
30 }),
31 );
32​
33 it.effect("creates askpass env for cached password prompts", () =>
34 Effect.gen(function* () {
35 const fs = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-askpass-test-" });
38 const env = yield* buildSshChildEnvironment({
39 authSecret: "super-secret",
40 interactiveAuth: true,
41 askpassDirectory: directory,
42 baseEnv: {},
43 });
44​
45 const askpassPath = path.join(directory, "ssh-askpass.sh");
46 assert.equal(env.SSH_ASKPASS, askpassPath);
47 assert.equal(env.SSH_ASKPASS_REQUIRE, "force");
48 assert.equal(env.T3_SSH_AUTH_SECRET, "super-secret");
49 assert.equal(env.DISPLAY, "t3code");
50 assert.equal(yield* fs.exists(askpassPath), true);
51 assert.include(yield* fs.readFileString(askpassPath), 'printf "%s\\n" "$T3_SSH_AUTH_SECRET"');
52 }).pipe(
53 Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux"))),
54 Effect.scoped,
55 ),
56 );
57​
58 it.effect("reuses the default askpass directory across interactive commands", () =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const askpassPath = () =>
63 buildSshChildEnvironment({ interactiveAuth: true, authSecret: "pw" }).pipe(
64 Effect.map((env) => env.SSH_ASKPASS ?? ""),
65 );
66 const runtimeDirectoryOf = (askpass: string) => path.dirname(path.dirname(askpass));
67​
68 const first = yield* askpassPath();
69 assert.equal(yield* askpassPath(), first);
70​
71 yield* fs.remove(runtimeDirectoryOf(first), { recursive: true });
72 const recreated = yield* askpassPath();
73 assert.notEqual(recreated, first);
74 assert.equal(yield* fs.exists(recreated), true);
75​
76 const cachedOwner = yield* fs.stat(runtimeDirectoryOf(recreated));
77 const otherUser = yield* askpassPath().pipe(
78 Effect.provideService(
79 HostProcess.UserId,
80 (Option.getOrUndefined(cachedOwner.uid) ?? 0) + 1,
81 ),
82 );
83 assert.notEqual(otherUser, recreated);
84​
85 yield* fs.remove(runtimeDirectoryOf(recreated), { recursive: true });
86 yield* fs.remove(runtimeDirectoryOf(otherUser), { recursive: true });
87 }).pipe(
88 Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux"))),
89 ),
90 );
91​
92 it.effect.skipIf(!symlinksSupported)(
93 "does not reuse a symlink left at the default askpass directory path",
94 () =>
95 Effect.gen(function* () {
96 const fs = yield* FileSystem.FileSystem;
97 const path = yield* Path.Path;
98 const askpassPath = () =>
99 buildSshChildEnvironment({ interactiveAuth: true, authSecret: "pw" }).pipe(
100 Effect.map((env) => env.SSH_ASKPASS ?? ""),
101 );
102 const runtimeDirectoryOf = (askpass: string) => path.dirname(path.dirname(askpass));
103​
104 const first = yield* askpassPath();
105 const ownedTarget = yield* fs.makeTempDirectory({ prefix: "t3code-ssh-symlink-target-" });
106 yield* fs.remove(runtimeDirectoryOf(first), { recursive: true });
107 yield* fs.symlink(ownedTarget, runtimeDirectoryOf(first));
108​
109 const next = yield* askpassPath();
110 assert.notEqual(next, first);
111 assert.equal(yield* fs.exists(path.join(ownedTarget, "t3code-ssh-askpass")), false);
112​
113 yield* fs.remove(runtimeDirectoryOf(first));
114 yield* fs.remove(ownedTarget, { recursive: true });
115 yield* fs.remove(runtimeDirectoryOf(next), { recursive: true });
116 }).pipe(
117 Effect.provide(
118 Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux")),
119 ),
120 ),
121 );
122​
123 it.effect("writes the default askpass helper once when interactive commands overlap", () =>
124 Effect.gen(function* () {
125 const fs = yield* FileSystem.FileSystem;
126 const runtimeDirectories: Array<string> = [];
127 yield* Effect.addFinalizer(() =>
128 Effect.forEach(runtimeDirectories, (directory) =>
129 fs.remove(directory, { recursive: true }).pipe(Effect.ignore),
130 ),
131 );
132​
133 // The first helper write of each round waits for the test, so the second command starts
134 // while the first is still setting up and must not touch the shared files until it is done.
135 let gate: { arrived: Deferred.Deferred<void>; release: Deferred.Deferred<void> } | undefined;
136 let fileCalls = 0;
137 let helperWrites = 0;
138 const counted = <A, E>(effect: Effect.Effect<A, E>) =>
139 Effect.suspend(() => {
140 fileCalls += 1;
141 return effect;
142 });
143 const trackedFs: FileSystem.FileSystem = {
144 ...fs,
145 makeTempDirectory: (options) =>
146 counted(fs.makeTempDirectory(options)).pipe(
147 Effect.tap((directory) => Effect.sync(() => runtimeDirectories.push(directory))),
148 ),
149 stat: (path) => counted(fs.stat(path)),
150 exists: (path) => counted(fs.exists(path)),
151 writeFileString: (path, data, options) =>
152 counted(
153 Effect.gen(function* () {
154 helperWrites += 1;
155 const held = gate;
156 gate = undefined;
157 if (held) {
158 yield* Deferred.succeed(held.arrived, undefined);
159 yield* Deferred.await(held.release);
160 }
161 yield* fs.writeFileString(path, data, options);
162 }),
163 ),
164 };
165 const askpassPath = buildSshChildEnvironment({
166 interactiveAuth: true,
167 authSecret: "pw",
168 }).pipe(
169 Effect.map((env) => env.SSH_ASKPASS ?? ""),
170 Effect.provideService(FileSystem.FileSystem, trackedFs),
171 );
172 const twoOverlappingCommands = Effect.gen(function* () {
173 const held = {
174 arrived: yield* Deferred.make<void>(),
175 release: yield* Deferred.make<void>(),
176 };
177 gate = held;
178 const first = yield* Effect.forkChild(askpassPath);
179 yield* Deferred.await(held.arrived);
180 const callsBefore = fileCalls;
181 const second = yield* askpassPath.pipe(Effect.forkChild({ startImmediately: true }));
182 assert.equal(fileCalls, callsBefore, "second command touched the files mid-setup");
183 yield* Deferred.succeed(held.release, undefined);
184 return [yield* Fiber.join(first), yield* Fiber.join(second)] as const;
185 });
186​
187 const [first, second] = yield* twoOverlappingCommands;
188 assert.equal(second, first);
189 assert.equal(runtimeDirectories.length, 1);
190 assert.equal(helperWrites, 1);
191​
192 yield* fs.remove(first);
193 assert.deepStrictEqual(yield* twoOverlappingCommands, [first, first]);
194 assert.equal(runtimeDirectories.length, 1);
195 assert.equal(helperWrites, 2);
196​
197 yield* fs.remove(first);
198 const held = {
199 arrived: yield* Deferred.make<void>(),
200 release: yield* Deferred.make<void>(),
201 };
202 gate = held;
203 const cancelled = yield* Effect.forkChild(askpassPath);
204 yield* Deferred.await(held.arrived);
205 const callsBeforeCancellation = fileCalls;
206 const interruption = yield* Fiber.interrupt(cancelled).pipe(
207 Effect.forkChild({ startImmediately: true }),
208 );
209 const next = yield* askpassPath.pipe(Effect.forkChild({ startImmediately: true }));
210 const callsDuringCancellation = fileCalls;
211 yield* Deferred.succeed(held.release, undefined);
212 yield* Fiber.join(interruption);
213 assert.equal(yield* Fiber.join(next), first);
214 assert.equal(callsDuringCancellation, callsBeforeCancellation);
215 assert.equal(helperWrites, 3);
216 assert.include(yield* fs.readFileString(first), 'printf "%s\\n" "$T3_SSH_AUTH_SECRET"');
217 }).pipe(
218 Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux"))),
219 Effect.scoped,
220 ),
221 );
222});