Skip to content

Commit 4228d5a

Browse files
authored
Merge pull request #350 from rundeck-plugins/ssh-agent-passphrase
RUN-2224: using ssh-agent with passphrase is not working in Ansible Plugin
2 parents fab01ea + 75a8750 commit 4228d5a

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java

+37-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
import java.nio.file.Paths;
1515
import java.nio.file.SimpleFileVisitor;
1616
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.*;
2418

2519
public class AnsibleRunner {
2620

@@ -636,12 +630,29 @@ public boolean registerKeySshAgent(String keyPath) throws AnsibleException, Exce
636630
// execute the ssh-agent add process
637631
ProcessBuilder processBuilder = new ProcessBuilder()
638632
.command(procArgs)
633+
.redirectErrorStream(true)
639634
.directory(baseDirectory.toFile());
635+
640636
Process proc = null;
641637

642638
Map<String, String> env = processBuilder.environment();
643639
env.put("SSH_AUTH_SOCK", this.sshAgent.getSocketPath());
644640

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+
645656
try {
646657
proc = processBuilder.start();
647658

@@ -659,26 +670,44 @@ public boolean registerKeySshAgent(String keyPath) throws AnsibleException, Exce
659670
}
660671
}
661672

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+
662681
int exitCode = proc.waitFor();
663682

683+
outthread.join();
684+
errthread.join();
685+
System.err.flush();
686+
System.out.flush();
687+
664688
if (exitCode != 0) {
665689
throw new AnsibleException("ERROR: ssh-add returns with non zero code:" + procArgs.toString(),
666690
AnsibleException.AnsibleFailureReason.AnsibleNonZero);
667691
}
668692

693+
669694
} catch (IOException e) {
670695
throw new AnsibleException("ERROR: error adding private key to ssh-agent." + procArgs.toString(), e, AnsibleException.AnsibleFailureReason.Unknown);
671696
} catch (InterruptedException e) {
672697
if(proc!=null) {
673698
proc.destroy();
674699
}
675700
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);
677702
}finally {
678703
// Make sure to always cleanup on failure and success
679704
if(proc!=null) {
680705
proc.destroy();
681706
}
707+
708+
if(tempPassVarsFile!=null && !tempPassVarsFile.delete()){
709+
tempPassVarsFile.deleteOnExit();
710+
}
682711
}
683712

684713
return true;

0 commit comments

Comments
 (0)