Open History to see the last commit
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 { HostProcessPlatform } from "@v12/shared/hostProcess";
9
10import {
11 buildSshAskpassHelperDescriptor,
12 buildSshChildEnvironment,
13 isSshAuthFailure,
14} 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: "v12-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.V12_SSH_AUTH_SECRET, "super-secret");
49 assert.equal(env.DISPLAY, "v12");
50 assert.equal(yield* fs.exists(askpassPath), true);
51 assert.include(
52 yield* fs.readFileString(askpassPath),
53 'printf "%s\\n" "$V12_SSH_AUTH_SECRET"',
54 );
55 }).pipe(
56 Effect.provide(Layer.merge(NodeServices.layer, Layer.succeed(HostProcessPlatform, "linux"))),
57 Effect.scoped,
58 ),
59 );
60
61 it.effect("builds a windows askpass launcher pair", () =>
62 Effect.gen(function* () {
63 const descriptor = yield* buildSshAskpassHelperDescriptor({
64 directory: "C:\\temp\\v12-ssh-askpass",
65 }).pipe(
66 Effect.provide(
67 Layer.merge(NodeServices.layer, Layer.succeed(HostProcessPlatform, "win32")),
68 ),
69 );
70
71 assert.equal(descriptor.launcherPath, "C:\\temp\\v12-ssh-askpass\\ssh-askpass.cmd");
72 assert.deepEqual(
73 descriptor.files.map((file) => file.path.split("\\").at(-1)),
74 ["ssh-askpass.cmd", "ssh-askpass.ps1"],
75 );
76 }),
77 );
78});