Skip to content

Commit

Permalink
fix: Port binding is not respected for SSH port (#1043)
Browse files Browse the repository at this point in the history
* Port Bindings is not respected for SSH port

1- Dashboard > Manage Jenkins > Configure Clouds
2- Add a new Cloud
3- Connect method SSH
4- Container settings
5- Port Bindings
     0.0.0.0:20000-20100:22
Expected Results
All docker-proxy process should use a port between 20000 and 20100

Tested for a year now with this fix and docker-proxy always gets a port in the specified range.

* Update DockerComputerSSHConnector.java

* Update DockerComputerSSHConnectorTest.java

* Update DockerComputerSSHConnectorTest.java

---------

Co-authored-by: Kris Stern <[email protected]>
  • Loading branch information
ericlapier and krisstern authored Jun 5, 2024
1 parent 596a3c1 commit 16e30a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ public void beforeContainerCreated(DockerAPI api, String workdir, CreateContaine
}
final Ports portBindings = hostConfig.getPortBindings();
if (portBindings != null) {
portBindings.add(sshPortBinding);
final Ports.Binding[] portBinding = portBindings.getBindings().get(sshPortBinding.getExposedPort());
// Only add default ssh port Binding when it is not already configured
if (portBinding == null || portBinding.length == 0) {

Check warning on line 283 in src/main/java/io/jenkins/docker/connector/DockerComputerSSHConnector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 283 is only partially covered, one branch is missing
portBindings.add(sshPortBinding);
}
hostConfig.withPortBindings(portBindings);
} else {
hostConfig.withPortBindings(sshPortBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,46 @@ public void testPortBinding() throws IOException, InterruptedException {
final String actualHostPortSpecForPort22 = actualBindingsForPort22[0].getHostPortSpec();
Assert.assertNull(actualHostPortSpecForPort22);
}

@Test
public void testPortBindingPort22() throws IOException, InterruptedException {
// Given
DockerComputerSSHConnector connector =
new DockerComputerSSHConnector(Mockito.mock(DockerComputerSSHConnector.SSHKeyStrategy.class));
CreateContainerCmdImpl cmd = new CreateContainerCmdImpl(
Mockito.mock(CreateContainerCmd.Exec.class), Mockito.mock(AuthConfig.class), "");
HostConfig hostConfig = cmd.getHostConfig();
if (hostConfig == null) {
hostConfig = new HostConfig();
cmd.withHostConfig(hostConfig);
}
final PortBinding exportContainerPort42 = PortBinding.parse("42:42");
final PortBinding exportContainerPort22 = PortBinding.parse("3022:22");
hostConfig.withPortBindings(exportContainerPort42, exportContainerPort22);
final ExposedPort port42 = new ExposedPort(42);
final ExposedPort port22 = new ExposedPort(22);

// When
connector.setPort(22);
connector.beforeContainerCreated(Mockito.mock(DockerAPI.class), "/workdir", cmd);

// Then
final Ports actualPortBindings = cmd.getHostConfig().getPortBindings();
Assert.assertNotNull(actualPortBindings);
final Map<ExposedPort, Ports.Binding[]> actualBindingMap = actualPortBindings.getBindings();
Assert.assertNotNull(actualBindingMap);
Assert.assertEquals(2, actualBindingMap.size());

final Ports.Binding[] actualBindingsForPort42 = actualBindingMap.get(port42);
Assert.assertNotNull(actualBindingsForPort42);
Assert.assertEquals(1, actualBindingsForPort42.length);
final String actualHostPortSpecForPort42 = actualBindingsForPort42[0].getHostPortSpec();
Assert.assertEquals("42", actualHostPortSpecForPort42);

final Ports.Binding[] actualBindingsForPort22 = actualBindingMap.get(port22);
Assert.assertNotNull(actualBindingsForPort22);
Assert.assertEquals(1, actualBindingsForPort22.length);
final String actualHostPortSpecForPort22 = actualBindingsForPort22[0].getHostPortSpec();
Assert.assertEquals("3022", actualHostPortSpecForPort22);
}
}

0 comments on commit 16e30a6

Please sign in to comment.