Skip to content

Commit

Permalink
Merge pull request #18 from jglick/ExecRemoteAgent-JENKINS-36997
Browse files Browse the repository at this point in the history
[JENKINS-36997] CLI implementation of RemoteAgent
  • Loading branch information
jglick committed Feb 10, 2017
2 parents f38bb5f + ec3aac2 commit 8d02c6c
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 39 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Expand Up @@ -29,7 +29,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.3</version>
<version>2.21</version>
<relativePath/>
</parent>

<artifactId>ssh-agent</artifactId>
Expand Down Expand Up @@ -67,7 +68,7 @@
<properties>
<jenkins.version>1.609.3</jenkins.version>
<java.level>7</java.level> <!-- sshd-core is 7+ -->
<workflow-jenkins-plugin.version>1.9</workflow-jenkins-plugin.version>
<workflow-jenkins-plugin.version>1.14.2</workflow-jenkins-plugin.version>
</properties>

<repositories>
Expand Down
Expand Up @@ -45,10 +45,10 @@ public interface RemoteAgent {
* @param comment the comment to give to the key.
* @throws java.io.IOException if something went wrong.
*/
void addIdentity(String privateKey, String passphrase, String comment) throws IOException;
void addIdentity(String privateKey, String passphrase, String comment) throws IOException, InterruptedException;

/**
* Stops the agent.
*/
void stop();
void stop() throws IOException, InterruptedException;
}
Expand Up @@ -391,7 +391,7 @@ public SSHAgentEnvironment(Launcher launcher, BuildListener listener, @CheckForN
* @throws IOException if the key cannot be added.
* @since 1.9
*/
public void add(SSHUserPrivateKey key) throws IOException {
public void add(SSHUserPrivateKey key) throws IOException, InterruptedException {
final Secret passphrase = key.getPassphrase();
final String effectivePassphrase = passphrase == null ? null : passphrase.getPlainText();
for (String privateKey : key.getPrivateKeys()) {
Expand Down
Expand Up @@ -82,8 +82,9 @@ public void onResume() {
try {
purgeSockets();
initRemoteAgent();
} catch (IOException io) {
} catch (IOException | InterruptedException x) {
listener.getLogger().println(Messages.SSHAgentBuildWrapper_CouldNotStartAgent());
x.printStackTrace(listener.getLogger());
}
}

Expand All @@ -92,7 +93,7 @@ static FilePath tempDir(FilePath ws) {
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
}

private static class Callback extends BodyExecutionCallback {
private static class Callback extends BodyExecutionCallback.TailCall {

private static final long serialVersionUID = 1L;

Expand All @@ -103,15 +104,8 @@ private static class Callback extends BodyExecutionCallback {
}

@Override
public void onSuccess(StepContext context, Object result) {
protected void finished(StepContext context) throws Exception {
execution.cleanUp();
context.onSuccess(result);
}

@Override
public void onFailure(StepContext context, Throwable t) {
execution.cleanUp();
context.onFailure(t);
}

}
Expand All @@ -137,7 +131,7 @@ public void expand(EnvVars env) throws IOException, InterruptedException {
*
* @throws IOException
*/
private void initRemoteAgent() throws IOException {
private void initRemoteAgent() throws IOException, InterruptedException {

List<SSHUserPrivateKey> userPrivateKeys = new ArrayList<SSHUserPrivateKey>();
for (String id : new LinkedHashSet<String>(step.getCredentials())) {
Expand Down Expand Up @@ -197,17 +191,16 @@ private void initRemoteAgent() throws IOException {
/**
* Shuts down the current SSH Agent and purges socket files.
*/
private void cleanUp() {
private void cleanUp() throws Exception {
try {
TaskListener listener = getContext().get(TaskListener.class);
if (agent != null) {
agent.stop();
listener.getLogger().println(Messages.SSHAgentBuildWrapper_Stopped());
}
} catch (Throwable th) {
getContext().onFailure(th);
} finally {
purgeSockets();
}
purgeSockets();
}

/**
Expand Down
@@ -0,0 +1,172 @@
/*
* The MIT License
*
* Copyright (c) 2014, Eccam s.r.o., Milan Kriz, CloudBees Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.cloudbees.jenkins.plugins.sshagent.exec;

import com.cloudbees.jenkins.plugins.sshagent.RemoteAgent;
import hudson.AbortException;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
* An implementation that uses native SSH agent installed on a system.
*/
public class ExecRemoteAgent implements RemoteAgent {
private static final String AuthSocketVar = "SSH_AUTH_SOCK";
private static final String AgentPidVar = "SSH_AGENT_PID";

private final Launcher launcher;

/**
* The listener in case we need to report exceptions
*/
private final TaskListener listener;

private final FilePath temp;

/**
* The socket bound by the agent.
*/
private final String socket;

/** Agent environment used for {@code ssh-add} and {@code ssh-agent -k}. */
private final Map<String, String> agentEnv;

ExecRemoteAgent(Launcher launcher, TaskListener listener, FilePath temp) throws Exception {
this.launcher = launcher;
this.listener = listener;
this.temp = temp;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (launcher.launch().cmds("ssh-agent").stdout(baos).start().joinWithTimeout(1, TimeUnit.MINUTES, listener) != 0) {
throw new AbortException("Failed to run ssh-agent");
}
agentEnv = parseAgentEnv(new String(baos.toByteArray(), StandardCharsets.US_ASCII)); // TODO could include local filenames, better to look up remote charset

if (agentEnv.containsKey(AuthSocketVar)) {
socket = agentEnv.get(AuthSocketVar);
} else {
throw new AbortException(AuthSocketVar + " was not included");
}
}

/**
* {@inheritDoc}
*/
@Override
public String getSocket() {
return socket;
}

/**
* {@inheritDoc}
*/
@Override
public void addIdentity(String privateKey, final String passphrase, String comment) throws IOException, InterruptedException {
FilePath keyFile = temp.createTextTempFile("private_key_", ".key", privateKey);
try {
keyFile.chmod(0600);

FilePath askpass = passphrase != null ? createAskpassScript() : null;
try {

Map<String,String> env = new HashMap<>(agentEnv);
if (passphrase != null) {
env.put("SSH_PASSPHRASE", passphrase);
env.put("DISPLAY", ":0"); // just to force using SSH_ASKPASS
env.put("SSH_ASKPASS", askpass.getRemote());
}
if (launcher.launch().cmds("ssh-add", keyFile.getRemote()).envs(env).stdout(listener).start().joinWithTimeout(1, TimeUnit.MINUTES, listener) != 0) {
throw new AbortException("Failed to run ssh-add");
}
} finally {
if (askpass != null && askpass.exists()) { // the ASKPASS script is self-deleting, anyway rather try to delete it in case of some error
askpass.delete();
}
}
} finally {
keyFile.delete();
}
}

/**
* {@inheritDoc}
*/
@Override
public void stop() throws IOException, InterruptedException {
if (launcher.launch().cmds("ssh-agent", "-k").envs(agentEnv).stdout(listener).start().joinWithTimeout(1, TimeUnit.MINUTES, listener) != 0) {
throw new AbortException("Failed to run ssh-agent -k");
}
}

/**
* Parses ssh-agent output.
*/
private Map<String,String> parseAgentEnv(String agentOutput) throws Exception{
Map<String, String> env = new HashMap<>();

// get SSH_AUTH_SOCK
env.put(AuthSocketVar, getAgentValue(agentOutput, AuthSocketVar));
listener.getLogger().println(AuthSocketVar + "=" + env.get(AuthSocketVar));

// get SSH_AGENT_PID
env.put(AgentPidVar, getAgentValue(agentOutput, AgentPidVar));
listener.getLogger().println(AgentPidVar + "=" + env.get(AgentPidVar));

return env;
}

/**
* Parses a value from ssh-agent output.
*/
private String getAgentValue(String agentOutput, String envVar) {
int pos = agentOutput.indexOf(envVar) + envVar.length() + 1; // +1 for '='
int end = agentOutput.indexOf(';', pos);
return agentOutput.substring(pos, end);
}

/**
* Creates a self-deleting script for SSH_ASKPASS. Self-deleting to be able to detect a wrong passphrase.
*/
private FilePath createAskpassScript() throws IOException, InterruptedException {
// TODO: assuming that ssh-add runs the script in shell even on Windows, not cmd
// for cmd following could work
// suffix = ".bat";
// script = "@ECHO %SSH_PASSPHRASE%\nDEL \"" + askpass.getAbsolutePath() + "\"\n";

FilePath askpass = temp.createTextTempFile("askpass_", ".sh", "#!/bin/sh\necho $SSH_PASSPHRASE\nrm $0\n");

// executable only for a current user
askpass.chmod(0700);
return askpass;
}
}
@@ -0,0 +1,84 @@
/*
* The MIT License
*
* Copyright (c) 2014, Eccam s.r.o., Milan Kriz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.cloudbees.jenkins.plugins.sshagent.exec;

import com.cloudbees.jenkins.plugins.sshagent.RemoteAgent;
import com.cloudbees.jenkins.plugins.sshagent.RemoteAgentFactory;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;

import java.io.IOException;
import java.util.concurrent.TimeUnit;



/**
* A factory that uses the native SSH agent installed on a remote system. SSH agent has to be in PATH environment variable.
*/
@Extension
public class ExecRemoteAgentFactory extends RemoteAgentFactory {

/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "Exec ssh-agent (binary ssh-agent on a remote machine)";
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSupported(Launcher launcher, final TaskListener listener) {
try {
int status = launcher.launch().cmds("ssh-agent", "-k").quiet(true).start().joinWithTimeout(1, TimeUnit.MINUTES, listener);
/*
* `ssh-agent -k` returns 0 if terminates running agent or 1 if
* it fails to terminate it. On Linux,
*/
return (status == 0) || (status == 1);
} catch (IOException e) {
e.printStackTrace();
listener.getLogger().println("Could not find ssh-agent: IOException: " + e.getMessage());
listener.getLogger().println("Check if ssh-agent is installed and in PATH");
return false;
} catch (InterruptedException e) {
e.printStackTrace();
listener.getLogger().println("Could not find ssh-agent: InterruptedException: " + e.getMessage());
return false;
}
}

/**
* {@inheritDoc}
*/
@Override
public RemoteAgent start(Launcher launcher, final TaskListener listener, FilePath temp) throws Throwable {
return new ExecRemoteAgent(launcher, listener, temp);
}
}

0 comments on commit 8d02c6c

Please sign in to comment.