Skip to content

Commit

Permalink
Merge pull request #50 from jglick/UOE-JENKINS-42969
Browse files Browse the repository at this point in the history
[JENKINS-42969] Fix ManuallyTrustedKeyVerificationStrategy on 2.30+
  • Loading branch information
jglick authored Mar 23, 2017
2 parents 583768a + 26a47d9 commit 677cf06
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@
*/
package hudson.plugins.sshslaves.verifiers;

import java.io.IOException;

import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.Computer;
import hudson.model.TaskListener;
import hudson.plugins.sshslaves.Messages;
import hudson.plugins.sshslaves.SSHLauncher;
import hudson.slaves.SlaveComputer;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* A host key verification strategy that works in a similar way to host key verification on
Expand All @@ -45,6 +51,8 @@
* @since 1.13
*/
public class ManuallyTrustedKeyVerificationStrategy extends SshHostKeyVerificationStrategy {

private static final Logger LOGGER = Logger.getLogger(ManuallyTrustedKeyVerificationStrategy.class.getName());

private final boolean requireInitialManualTrust;

Expand All @@ -67,7 +75,7 @@ public boolean verify(final SlaveComputer computer, HostKey hostKey, TaskListene
if (isRequireInitialManualTrust()) {
listener.getLogger().println(Messages.ManualTrustingHostKeyVerifier_KeyNotTrusted(SSHLauncher.getTimestamp()));
if (!hasExistingTrustAction(computer, hostKey)) {
computer.addAction(new TrustHostKeyAction(computer, hostKey));
addAction(computer, new TrustHostKeyAction(computer, hostKey));
}
return false;
}
Expand All @@ -80,7 +88,7 @@ public boolean verify(final SlaveComputer computer, HostKey hostKey, TaskListene
else if (!existingHostKey.equals(hostKey)) {
listener.getLogger().println(Messages.ManualTrustingHostKeyVerifier_KeyNotTrusted(SSHLauncher.getTimestamp()));
if (!hasExistingTrustAction(computer, hostKey)) {
computer.addAction(new TrustHostKeyAction(computer, hostKey));
addAction(computer, new TrustHostKeyAction(computer, hostKey));
}
return false;
}
Expand All @@ -89,6 +97,23 @@ else if (!existingHostKey.equals(hostKey)) {
return true;
}
}

/** TODO replace with {@link Computer#addAction} after core baseline picks up JENKINS-42969 fix */
private static void addAction(@Nonnull Computer c, @Nonnull Action a) {
try {
c.addAction(a);
} catch (UnsupportedOperationException x) {
try {
Field actionsF = Actionable.class.getDeclaredField("actions");
actionsF.setAccessible(true);
@SuppressWarnings("unchecked")
List<Action> actions = (List) actionsF.get(c);
actions.add(a);
} catch (Exception x2) {
LOGGER.log(Level.WARNING, null, x2);
}
}
}

private boolean hasExistingTrustAction(SlaveComputer computer, HostKey hostKey) {
for (TrustHostKeyAction action : computer.getActions(TrustHostKeyAction.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@

public class TrustHostKeyActionTest {





@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

Expand Down Expand Up @@ -131,13 +127,13 @@ public boolean authenticate(String username, String password, ServerSession sess
try {
computer.connect(false).get();
} catch (ExecutionException ex){
if (!ex.getMessage().startsWith("java.io.IOException: Slave failed")) {
if (!ex.getMessage().startsWith("java.io.IOException: Slave failed") && !ex.getMessage().startsWith("java.io.IOException: Agent failed")) {
throw ex;
}
}

List<TrustHostKeyAction> actions = computer.getActions(TrustHostKeyAction.class);
assertEquals(1, actions.size());
assertEquals(computer.getLog(), 1, actions.size());
assertNull(actions.get(0).getExistingHostKey());

HtmlPage p = jenkins.createWebClient().getPage(slave, actions.get(0).getUrlName());
Expand Down

0 comments on commit 677cf06

Please sign in to comment.