@@ -15,7 +15,7 @@ import { HttpClient, HttpClientResponse } from "effect/http";
1515import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1616
1717import * as SshAuth from "./auth.ts" ;
18- import { SshCommandError } from "./errors.ts" ;
18+ import { SshCommandError , SshPasswordPromptError } from "./errors.ts" ;
1919import * as SshTunnel from "./tunnel.ts" ;
2020
2121const makeSuccessfulProcess = ( stdout : string ) => {
@@ -260,6 +260,84 @@ describe("ssh tunnel scripts", () => {
260260 } ) ,
261261 ) ;
262262
263+ it . effect . each ( [
264+ {
265+ name : "does not ask for a password when the agent cannot sign an accepted key" ,
266+ stderr :
267+ 'sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n' +
268+ "julius@devbox: Permission denied (publickey,password).\r\n" ,
269+ expectedPromptAttempts : [ ] ,
270+ } ,
271+ {
272+ name : "asks for a password when signing failure has no agent provenance" ,
273+ stderr :
274+ 'sign_and_send_pubkey: signing failed for ED25519-CERT "key-cert.pub": agent refused operation\n' +
275+ "julius@devbox: Permission denied (publickey,password).\n" ,
276+ expectedPromptAttempts : [ 1 ] ,
277+ } ,
278+ {
279+ name : "asks for a password when the server rejects every key" ,
280+ stderr : "julius@devbox: Permission denied (publickey,password).\r\n" ,
281+ expectedPromptAttempts : [ 1 ] ,
282+ } ,
283+ ] ) ( "$name" , ( { stderr, expectedPromptAttempts } ) =>
284+ Effect . gen ( function * ( ) {
285+ const promptAttempts : Array < number > = [ ] ;
286+ const spawner = ChildProcessSpawner . make ( ( command ) =>
287+ Effect . succeed (
288+ commandArgs ( command ) . includes ( "-G" )
289+ ? makeSuccessfulProcess ( "" )
290+ : {
291+ ...makeSuccessfulProcess ( "" ) ,
292+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 255 ) ) ,
293+ stderr : Stream . make ( new TextEncoder ( ) . encode ( stderr ) ) ,
294+ } ,
295+ ) ,
296+ ) ;
297+ const layer = Layer . mergeAll (
298+ NodeServices . layer ,
299+ Layer . succeed ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
300+ Layer . succeed ( HttpClient . HttpClient , testHttpClient ) ,
301+ Layer . succeed ( NetService . NetService , testNetService ) ,
302+ Layer . succeed (
303+ SshAuth . SshPasswordPrompt ,
304+ SshAuth . SshPasswordPrompt . of ( {
305+ isAvailable : true ,
306+ request : ( request ) =>
307+ Effect . sync ( ( ) => {
308+ promptAttempts . push ( request . attempt ) ;
309+ return null ;
310+ } ) ,
311+ } ) ,
312+ ) ,
313+ SshTunnel . SshEnvironmentManager . layer ( { resolveCliRunner : Effect . succeed ( ARCHIVE ) } ) ,
314+ ) ;
315+ yield * Effect . gen ( function * ( ) {
316+ const manager = yield * SshTunnel . SshEnvironmentManager ;
317+ const result = yield * Effect . result (
318+ manager . ensureEnvironment ( {
319+ alias : "devbox" ,
320+ hostname : "devbox" ,
321+ username : null ,
322+ port : null ,
323+ } ) ,
324+ ) ;
325+ assert . isTrue ( Result . isFailure ( result ) ) ;
326+ assert . deepEqual ( promptAttempts , expectedPromptAttempts ) ;
327+ if ( Result . isFailure ( result ) ) {
328+ if ( expectedPromptAttempts . length === 0 ) {
329+ assert . instanceOf ( result . failure , SshCommandError ) ;
330+ if ( result . failure instanceof SshCommandError ) {
331+ assert . equal ( result . failure . stderr , stderr ) ;
332+ }
333+ } else {
334+ assert . instanceOf ( result . failure , SshPasswordPromptError ) ;
335+ }
336+ }
337+ } ) . pipe ( Effect . provide ( layer ) , Effect . scoped ) ;
338+ } ) ,
339+ ) ;
340+
263341 it . effect . each ( [ "local tunnel" , "remote server" ] as const ) (
264342 "waits for %s shutdown before reconnecting the same target" ,
265343 ( stalledStep ) =>
0 commit comments