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

auth.test.ts

53 lines2.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3​
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9​
10import { buildSshChildEnvironment, isSshAuthFailure } from "./auth.ts";
11​
12describe("ssh auth", () => {
13 it.effect("detects ssh auth failures from common permission denied messages", () =>
14 Effect.sync(() => {
15 assert.equal(
16 isSshAuthFailure(
17 new Error(
18 "julius@100.65.180.100: Permission denied (publickey,password,keyboard-interactive).",
19 ),
20 ),
21 true,
22 );
23 assert.equal(isSshAuthFailure(new Error("Permission denied (publickey).")), true);
24 assert.equal(isSshAuthFailure(new Error("Connection timed out")), false);
25 assert.equal(isSshAuthFailure(new Error("mkdir: Permission denied")), false);
26 }),
27 );
28​
29 it.effect("creates askpass env for cached password prompts", () =>
30 Effect.gen(function* () {
31 const fs = yield* FileSystem.FileSystem;
32 const path = yield* Path.Path;
33 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-askpass-test-" });
34 const env = yield* buildSshChildEnvironment({
35 authSecret: "super-secret",
36 interactiveAuth: true,
37 askpassDirectory: directory,
38 baseEnv: {},
39 });
40​
41 const askpassPath = path.join(directory, "ssh-askpass.sh");
42 assert.equal(env.SSH_ASKPASS, askpassPath);
43 assert.equal(env.SSH_ASKPASS_REQUIRE, "force");
44 assert.equal(env.T3_SSH_AUTH_SECRET, "super-secret");
45 assert.equal(env.DISPLAY, "t3code");
46 assert.equal(yield* fs.exists(askpassPath), true);
47 assert.include(yield* fs.readFileString(askpassPath), 'printf "%s\\n" "$T3_SSH_AUTH_SECRET"');
48 }).pipe(
49 Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcess.Platform, "linux"))),
50 Effect.scoped,
51 ),
52 );
53});