Skip to content

Commit 7213897

Browse files
authored
Merge pull request #359 from jglick/OutboundAgent
Demonstrating use of `OutboundAgent`
2 parents 10cf4b1 + 41331a0 commit 7213897

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@
5757
<scope>import</scope>
5858
<type>pom</type>
5959
</dependency>
60+
<!-- TODO until in BOM -->
61+
<dependency>
62+
<groupId>org.jenkins-ci.plugins</groupId>
63+
<artifactId>ssh-slaves</artifactId>
64+
<version>3.1071.v0d059c7b_c555</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.jenkins-ci.plugins</groupId>
68+
<artifactId>ssh-slaves</artifactId>
69+
<classifier>tests</classifier>
70+
<version>3.1071.v0d059c7b_c555</version>
71+
</dependency>
6072
</dependencies>
6173
</dependencyManagement>
6274
<dependencies>
@@ -155,6 +167,12 @@
155167
<artifactId>ssh-slaves</artifactId>
156168
<scope>test</scope>
157169
</dependency>
170+
<dependency>
171+
<groupId>org.jenkins-ci.plugins</groupId>
172+
<artifactId>ssh-slaves</artifactId>
173+
<classifier>tests</classifier>
174+
<scope>test</scope>
175+
</dependency>
158176
<dependency>
159177
<groupId>org.testcontainers</groupId>
160178
<artifactId>testcontainers</artifactId>

src/test/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStepTest.java

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package org.jenkinsci.plugins.docker.workflow;
2525

26-
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
2726
import com.cloudbees.plugins.credentials.CredentialsProvider;
2827
import com.cloudbees.plugins.credentials.CredentialsScope;
2928
import com.cloudbees.plugins.credentials.common.IdCredentials;
@@ -38,9 +37,6 @@
3837
import hudson.model.Node;
3938
import hudson.model.Result;
4039
import hudson.model.User;
41-
import hudson.plugins.sshslaves.SSHLauncher;
42-
import hudson.slaves.DumbSlave;
43-
import java.io.ByteArrayOutputStream;
4440
import jenkins.model.Jenkins;
4541
import jenkins.security.QueueItemAuthenticatorConfiguration;
4642
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
@@ -74,21 +70,17 @@
7470
import java.util.Set;
7571
import java.util.logging.Level;
7672
import org.apache.commons.text.StringEscapeUtils;
77-
import org.apache.sshd.common.config.keys.KeyUtils;
78-
import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter;
79-
import org.apache.sshd.common.keyprovider.KeyPairProvider;
8073
import static org.hamcrest.MatcherAssert.assertThat;
8174
import static org.hamcrest.Matchers.containsString;
8275
import static org.hamcrest.Matchers.not;
83-
import static org.jenkinsci.plugins.docker.workflow.DockerTestUtil.assumeDocker;
8476
import org.jenkinsci.plugins.structs.describable.DescribableModel;
8577
import org.jenkinsci.plugins.workflow.support.pickles.FilePathPickle;
8678
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
8779
import org.junit.ClassRule;
8880
import org.jvnet.hudson.test.BuildWatcher;
8981
import org.jvnet.hudson.test.JenkinsSessionRule;
9082
import org.jvnet.hudson.test.LoggerRule;
91-
import org.testcontainers.containers.GenericContainer;
83+
import test.ssh_agent.OutboundAgent;
9284

9385
public class RegistryEndpointStepTest {
9486

@@ -209,11 +201,10 @@ public void stepExecutionWithCredentialsAndQueueItemAuthenticator() throws Throw
209201

210202
@Issue("JENKINS-75679")
211203
@Test public void noFilePathPickle() throws Throwable {
212-
assumeDocker();
213-
try (var agent = new SSHAgentContainer()) {
214-
agent.start();
204+
try (var agent = new OutboundAgent()) {
205+
var connectionDetails = agent.start();
215206
rr.then(r -> {
216-
agent.register("remote");
207+
OutboundAgent.createAgent(r, "remote", connectionDetails);
217208
var registryCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "registryCreds", null, "me", "s3cr3t");
218209
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), registryCredentials);
219210
var p = r.createProject(WorkflowJob.class, "p");
@@ -243,33 +234,6 @@ public void stepExecutionWithCredentialsAndQueueItemAuthenticator() throws Throw
243234
}
244235
}
245236

246-
private static final class SSHAgentContainer extends GenericContainer<SSHAgentContainer> {
247-
private final String priv;
248-
SSHAgentContainer() {
249-
super("jenkins/ssh-agent:6.17.0");
250-
try {
251-
var kp = KeyUtils.generateKeyPair(KeyPairProvider.SSH_RSA, 2048);
252-
var kprw = new OpenSSHKeyPairResourceWriter();
253-
var baos = new ByteArrayOutputStream();
254-
kprw.writePublicKey(kp, null, baos);
255-
var pub = baos.toString(StandardCharsets.US_ASCII);
256-
baos.reset();
257-
kprw.writePrivateKey(kp, null, null, baos);
258-
priv = baos.toString(StandardCharsets.US_ASCII);
259-
withEnv("JENKINS_AGENT_SSH_PUBKEY", pub);
260-
withExposedPorts(22);
261-
} catch (Exception x) {
262-
throw new AssertionError(x);
263-
}
264-
}
265-
void register(String name) throws Exception{
266-
var creds = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, null, "jenkins", new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(priv), null, null);
267-
CredentialsProvider.lookupStores(Jenkins.get()).iterator().next().addCredentials(Domain.global(), creds);
268-
var port = getMappedPort(22);
269-
Jenkins.get().addNode(new DumbSlave(name, "/home/jenkins/agent", new SSHLauncher(getHost(), port, creds.getId())));
270-
}
271-
}
272-
273237
public static class MockLauncherStep extends Step {
274238

275239
@DataBoundConstructor

0 commit comments

Comments
 (0)