14
14
import java .nio .file .Paths ;
15
15
import java .nio .file .SimpleFileVisitor ;
16
16
import java .nio .file .attribute .BasicFileAttributes ;
17
- import java .util .ArrayList ;
18
- import java .util .HashSet ;
19
- import java .util .List ;
20
- import java .util .Set ;
21
- import java .util .Collection ;
22
- import java .util .Map ;
23
- import java .util .HashMap ;
17
+ import java .util .*;
24
18
25
19
public class AnsibleRunner {
26
20
@@ -636,12 +630,29 @@ public boolean registerKeySshAgent(String keyPath) throws AnsibleException, Exce
636
630
// execute the ssh-agent add process
637
631
ProcessBuilder processBuilder = new ProcessBuilder ()
638
632
.command (procArgs )
633
+ .redirectErrorStream (true )
639
634
.directory (baseDirectory .toFile ());
635
+
640
636
Process proc = null ;
641
637
642
638
Map <String , String > env = processBuilder .environment ();
643
639
env .put ("SSH_AUTH_SOCK" , this .sshAgent .getSocketPath ());
644
640
641
+ File tempPassVarsFile = null ;
642
+ if (sshPassphrase != null && sshPassphrase .length () > 0 ) {
643
+ tempPassVarsFile = File .createTempFile ("ansible-runner" , "ssh-add-check" );
644
+ tempPassVarsFile .setExecutable (true );
645
+
646
+ List <String > passScript = new ArrayList <>();
647
+ passScript .add ("read SECRET" );
648
+ passScript .add ("echo $SECRET" );
649
+
650
+ Files .write (tempPassVarsFile .toPath (),passScript );
651
+
652
+ env .put ("DISPLAY" , "0" );
653
+ env .put ("SSH_ASKPASS" , tempPassVarsFile .getAbsolutePath ());
654
+ }
655
+
645
656
try {
646
657
proc = processBuilder .start ();
647
658
@@ -659,26 +670,44 @@ public boolean registerKeySshAgent(String keyPath) throws AnsibleException, Exce
659
670
}
660
671
}
661
672
673
+ stdinw .close ();
674
+ stdin .close ();
675
+
676
+ Thread errthread = Logging .copyStreamThread (proc .getErrorStream (), ListenerFactory .getListener (System .err ));
677
+ Thread outthread = Logging .copyStreamThread (proc .getInputStream (), ListenerFactory .getListener (System .out ));
678
+ errthread .start ();
679
+ outthread .start ();
680
+
662
681
int exitCode = proc .waitFor ();
663
682
683
+ outthread .join ();
684
+ errthread .join ();
685
+ System .err .flush ();
686
+ System .out .flush ();
687
+
664
688
if (exitCode != 0 ) {
665
689
throw new AnsibleException ("ERROR: ssh-add returns with non zero code:" + procArgs .toString (),
666
690
AnsibleException .AnsibleFailureReason .AnsibleNonZero );
667
691
}
668
692
693
+
669
694
} catch (IOException e ) {
670
695
throw new AnsibleException ("ERROR: error adding private key to ssh-agent." + procArgs .toString (), e , AnsibleException .AnsibleFailureReason .Unknown );
671
696
} catch (InterruptedException e ) {
672
697
if (proc !=null ) {
673
698
proc .destroy ();
674
699
}
675
700
Thread .currentThread ().interrupt ();
676
- throw new AnsibleException ("ERROR: error adding private key to ssh-agen Interrupted." , e , AnsibleException .AnsibleFailureReason .Interrupted );
701
+ throw new AnsibleException ("ERROR: error adding private key to ssh-agent Interrupted." , e , AnsibleException .AnsibleFailureReason .Interrupted );
677
702
}finally {
678
703
// Make sure to always cleanup on failure and success
679
704
if (proc !=null ) {
680
705
proc .destroy ();
681
706
}
707
+
708
+ if (tempPassVarsFile !=null && !tempPassVarsFile .delete ()){
709
+ tempPassVarsFile .deleteOnExit ();
710
+ }
682
711
}
683
712
684
713
return true ;
0 commit comments