Skip to content

Commit

Permalink
[JENKINS-30656] Add support for password protected ssh key
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsirot committed Dec 15, 2015
1 parent 67943cf commit 727083b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Expand Up @@ -50,6 +50,7 @@ abstract class AbstractAnsibleInvocation<T extends AbstractAnsibleInvocation<T>>
protected String additionalParameters;

private FilePath key = null;
private FilePath script = null;
private Inventory inventory;
private boolean copyCredentialsInWorkspace = false;
private final FilePath ws;
Expand Down Expand Up @@ -151,6 +152,11 @@ protected ArgumentListBuilder appendCredentials(ArgumentListBuilder args)
key = Utils.createSshKeyFile(key, ws, privateKeyCredentials, copyCredentialsInWorkspace);
args.add("--private-key").add(key);
args.add("-u").add(privateKeyCredentials.getUsername());
if (privateKeyCredentials.getPassphrase() != null) {
script = Utils.createSshAskPassFile(script, ws, privateKeyCredentials, copyCredentialsInWorkspace);
environment.put("SSH_ASKPASS", script.getRemote());
environment.put("DISPLAY", "nodisplay");
}
} else if (credentials instanceof UsernamePasswordCredentials) {
args.add("-u").add(credentials.getUsername());
args.add("-k");
Expand Down Expand Up @@ -190,6 +196,7 @@ public boolean execute(CLIRunner runner) throws IOException, InterruptedExceptio
inventory.tearDown(listener);
}
Utils.deleteTempFile(key, listener);
Utils.deleteTempFile(script, listener);
}
}
}
15 changes: 12 additions & 3 deletions src/main/java/org/jenkinsci/plugins/ansible/Utils.java
Expand Up @@ -15,12 +15,13 @@
*/
package org.jenkinsci.plugins.ansible;

import java.io.IOException;
import java.util.List;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.util.Secret;

import java.io.IOException;
import java.util.List;

class Utils
{
Expand All @@ -44,6 +45,14 @@ static FilePath createSshKeyFile(FilePath key, FilePath workspace, SSHUserPrivat
return key;
}

static FilePath createSshAskPassFile(FilePath script, FilePath workspace, SSHUserPrivateKey credentials, boolean inWorkspace) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
sb.append("#! /bin/sh\n").append("/bin/echo \"" + Secret.toString(credentials.getPassphrase()) + "\"");
script = workspace.createTextTempFile("ssh", ".sh", sb.toString(), inWorkspace);
script.chmod(0700);
return script;
}

/**
* Delete a temporary file. Print a warning in the log when deletion fails.
*
Expand Down

0 comments on commit 727083b

Please sign in to comment.