Skip to content

Commit

Permalink
Fix race condtion in ssh key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimilian committed Oct 26, 2017
1 parent fe5e265 commit 47c4bea
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class GerritProjectListUpdaterFunctionalTest {
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
// We need to do this so that subsequent calls will be served with another command define later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class GerritServerHudsonTest {
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();

serverOne = new SshdServerMock();
serverTwo = new SshdServerMock();
sshdOne = SshdServerMock.startServer(serverOne);
Expand All @@ -114,7 +115,6 @@ public void setUp() throws Exception {
serverTwo.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
serverTwo.returnCommandFor("gerrit review.*", SshdServerMock.EofCommandMock.class);
serverTwo.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class LockedDownGerritEventTest {
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public class GerritTriggerApiTest {
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
server.returnCommandFor("gerrit review.*", SshdServerMock.EofCommandMock.class);
server.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public class GerritTriggeredBuildListenerTest {
public final JenkinsRule j = new JenkinsRule();

private SshServer sshd;
@SuppressWarnings("unused")
private SshdServerMock.KeyPairFiles sshKey;

private SshdServerMock server;

private static CountDownLatch buildListenerLatch;
Expand All @@ -81,14 +80,14 @@ public class GerritTriggeredBuildListenerTest {
*/
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
SshdServerMock.generateKeyPair();

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
server.returnCommandFor("gerrit review.*", SshdServerMock.EofCommandMock.class);
server.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
GerritServer gserver = PluginImpl.getFirstServer_();
assertNotNull(gserver);
SshdServerMock.configureFor(sshd, gserver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class ManualTriggerActionApprovalTest {

private SshdServerMock server;
private SshServer sshd;
private SshdServerMock.KeyPairFiles sshKey;

/**
* Runs before test method.
Expand All @@ -75,8 +74,8 @@ public class ManualTriggerActionApprovalTest {
*/
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
final SshdServerMock.KeyPairFiles sshKey = SshdServerMock.generateKeyPair();

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class GerritMissedEventsFunctionalTest {
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class BackCompat252HudsonTest {
@Rule
public JenkinsRule j = new JenkinsRule();
private SshServer sshd;
private SshdServerMock.KeyPairFiles sshKey;
private SshdServerMock server;

/**
Expand All @@ -95,7 +94,8 @@ public class BackCompat252HudsonTest {
*/
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
SshdServerMock.generateKeyPair();

server = new SshdServerMock();
sshd = SshdServerMock.startServer(server);
GerritServer gerritServer = PluginImpl.getFirstServer_();
Expand All @@ -107,7 +107,6 @@ public void setUp() throws Exception {
server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
server.returnCommandFor("gerrit review.*", SshdServerMock.EofCommandMock.class);
server.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
SshdServerMock.configureFor(sshd, PluginImpl.getFirstServer_());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public class SpecGerritTriggerHudsonTest {
//TODO Fix the SshdServerMock so that asserts can be done on review commands.

private SshServer sshd;
private SshdServerMock.KeyPairFiles sshKey;
private SshdServerMock serverMock;
private GerritServer gerritServer;

Expand All @@ -100,7 +99,8 @@ public class SpecGerritTriggerHudsonTest {
*/
@Before
public void setUp() throws Exception {
sshKey = SshdServerMock.generateKeyPair();
SshdServerMock.generateKeyPair();

serverMock = new SshdServerMock();
sshd = SshdServerMock.startServer(serverMock);
serverMock.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
Expand All @@ -109,7 +109,6 @@ public void setUp() throws Exception {
serverMock.returnCommandFor("gerrit approve.*", SshdServerMock.EofCommandMock.class);
serverMock.returnCommandFor("gerrit version", SshdServerMock.SendVersionCommand.class);
serverMock.returnCommandFor("gerrit approve.*", SshdServerMock.EofCommandMock.class);
System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
gerritServer = PluginImpl.getFirstServer_();
SshdServerMock.configureFor(sshd, gerritServer, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.jcraft.jsch.JSchException;

import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -447,6 +448,7 @@ public static KeyPairFiles generateKeyPair() throws IOException, InterruptedExce
File tmp = new File(System.getProperty("java.io.tmpdir"));
File priv = new File(tmp, "jenkins-testkey");
File pub = new File(tmp, "jenkins-testkey.pub");
final KeyPairFiles sshKey;
if (!(priv.exists() && pub.exists())) {
if (priv.exists()) {
if (!priv.delete()) {
Expand All @@ -466,11 +468,14 @@ public static KeyPairFiles generateKeyPair() throws IOException, InterruptedExce
kpair.writePublicKey(new FileOutputStream(pub), "Test");
System.out.println("Finger print: " + kpair.getFingerPrint());
kpair.dispose();
return new KeyPairFiles(priv, pub);
sshKey = new KeyPairFiles(priv, pub);
} else {
System.out.println("Test key-pair seems to already exist.");
return new KeyPairFiles(priv, pub);
sshKey = new KeyPairFiles(priv, pub);
}

System.setProperty(PluginImpl.TEST_SSH_KEYFILE_LOCATION_PROPERTY, sshKey.getPrivateKey().getAbsolutePath());
return sshKey;
}

/**
Expand Down

0 comments on commit 47c4bea

Please sign in to comment.