Skip to content

Commit cef6867

Browse files
committed
Fix #33752: do not use protocol of docker.url as target protocol
1 parent bd0ccd6 commit cef6867

3 files changed

Lines changed: 27 additions & 28 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/backend/docker/AbstractDockerBackend.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import org.slf4j.LoggerFactory;
3939

4040
import javax.inject.Inject;
41+
import java.net.MalformedURLException;
42+
import java.net.URI;
43+
import java.net.URISyntaxException;
44+
import java.net.URL;
4145
import java.nio.file.Paths;
4246
import java.util.ArrayList;
4347
import java.util.HashMap;
@@ -52,6 +56,7 @@ public abstract class AbstractDockerBackend extends AbstractContainerBackend {
5256

5357
protected static final String PROPERTY_PORT_RANGE_START = "port-range-start";
5458
protected static final String PROPERTY_PORT_RANGE_MAX = "port-range-max";
59+
protected static final String PROPERTY_TARGET_URL = "target-url";
5560
protected static final String DEFAULT_TARGET_URL = DEFAULT_TARGET_PROTOCOL + "://localhost";
5661
private static final String PROPERTY_PREFIX = "proxy.docker.";
5762
@Inject
@@ -60,6 +65,8 @@ public abstract class AbstractDockerBackend extends AbstractContainerBackend {
6065

6166
protected Integer portRangeFrom;
6267
protected Integer portRangeTo;
68+
protected String nonInternalNetworkTargetProtocol;
69+
protected URL nonInternalNetworkTargetURL;
6370

6471
private final ScheduledExecutorService releasePortExecutor = Executors.newSingleThreadScheduledExecutor();
6572
private final ObjectMapper objectMapper = new ObjectMapper();
@@ -88,14 +95,21 @@ public void initialize() throws ContainerProxyException {
8895
}
8996

9097
String confUrl = getProperty(PROPERTY_URL);
91-
if (confUrl != null) builder.uri(confUrl);
98+
if (confUrl != null) {
99+
builder.uri(confUrl);
100+
}
92101

93102
dockerClient = builder.build();
94103
portRangeFrom = environment.getProperty(getPropertyPrefix() + PROPERTY_PORT_RANGE_START, Integer.class, 20000);
95104
portRangeTo = environment.getProperty(getPropertyPrefix() + PROPERTY_PORT_RANGE_MAX, Integer.class, -1);
96-
}
97-
98105

106+
try {
107+
nonInternalNetworkTargetURL = new URI(getProperty(PROPERTY_TARGET_URL, DEFAULT_TARGET_URL)).toURL();
108+
nonInternalNetworkTargetProtocol = getProperty(PROPERTY_CONTAINER_PROTOCOL, nonInternalNetworkTargetURL.getProtocol());
109+
} catch (MalformedURLException | URISyntaxException e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
99113

100114
@Override
101115
protected String getPropertyPrefix() {

src/main/java/eu/openanalytics/containerproxy/backend/docker/DockerEngineBackend.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@
6060
import javax.annotation.PostConstruct;
6161
import java.io.IOException;
6262
import java.io.OutputStream;
63-
import java.net.MalformedURLException;
6463
import java.net.URI;
65-
import java.net.URL;
6664
import java.nio.channels.ClosedChannelException;
6765
import java.util.ArrayList;
6866
import java.util.Collections;
@@ -81,8 +79,6 @@ public class DockerEngineBackend extends AbstractDockerBackend {
8179
private static final String PROPERTY_LOKI_URL = "loki-url";
8280

8381
private ImagePullPolicy imagePullPolicy;
84-
private String nonInternalNetworkTargetProtocol;
85-
private URL hostURL;
8682
private String containerNetwork;
8783
private String lokiUrl;
8884

@@ -92,13 +88,6 @@ public void initialize() {
9288
imagePullPolicy = environment.getProperty(getPropertyPrefix() + PROPERTY_IMG_PULL_POLICY, ImagePullPolicy.class, ImagePullPolicy.IfNotPresent);
9389
containerNetwork = environment.getProperty(getPropertyPrefix() + PROPERTY_CONTAINER_NETWORK);
9490
lokiUrl = environment.getProperty(getPropertyPrefix() + PROPERTY_LOKI_URL);
95-
96-
try {
97-
hostURL = new URL(getProperty(PROPERTY_URL, DEFAULT_TARGET_URL));
98-
nonInternalNetworkTargetProtocol = getProperty(PROPERTY_CONTAINER_PROTOCOL, hostURL.getProtocol());
99-
} catch (MalformedURLException e) {
100-
throw new RuntimeException(e);
101-
}
10291
}
10392

10493
@Override
@@ -228,7 +217,7 @@ public Proxy startContainer(Authentication user, Container initialContainer, Con
228217
protected URI calculateTarget(Container container, PortMappings.PortMappingEntry portMapping, Integer hostPort) throws Exception {
229218
String targetProtocol;
230219
String targetHostName;
231-
String targetPort;
220+
int targetPort;
232221

233222
if (isUseInternalNetwork()) {
234223
targetProtocol = getDefaultTargetProtocol();
@@ -238,11 +227,11 @@ protected URI calculateTarget(Container container, PortMappings.PortMappingEntry
238227
ContainerInfo info = dockerClient.inspectContainer(container.getId());
239228
targetHostName = info.config().hostname();
240229

241-
targetPort = String.valueOf(portMapping.getPort());
230+
targetPort = portMapping.getPort();
242231
} else {
243232
targetProtocol = nonInternalNetworkTargetProtocol;
244-
targetHostName = hostURL.getHost();
245-
targetPort = String.valueOf(hostPort);
233+
targetHostName = nonInternalNetworkTargetURL.getHost();
234+
targetPort = hostPort;
246235
}
247236
return new URI(String.format("%s://%s:%s%s", targetProtocol, targetHostName, targetPort, portMapping.getTargetPath()));
248237
}

src/main/java/eu/openanalytics/containerproxy/backend/docker/DockerSwarmBackend.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import java.io.IOException;
6969
import java.io.OutputStream;
7070
import java.net.URI;
71-
import java.net.URISyntaxException;
7271
import java.nio.channels.ClosedChannelException;
7372
import java.util.ArrayList;
7473
import java.util.HashMap;
@@ -82,8 +81,6 @@
8281
@ConditionalOnProperty(name = "proxy.container-backend", havingValue = "docker-swarm")
8382
public class DockerSwarmBackend extends AbstractDockerBackend {
8483

85-
private URI hostURL;
86-
8784
private int serviceWaitTime;
8885

8986
private static final List<String> STARTING_STATES = List.of("new", "pending", "assigned", "accepted", "ready", "preparing", "starting", "running");
@@ -97,11 +94,6 @@ public void initialize() {
9794
} catch (Exception e) {
9895
}
9996
if (swarmId == null) throw new ContainerProxyException("Backend is not a Docker Swarm");
100-
try {
101-
hostURL = new URI(getProperty(PROPERTY_URL, DEFAULT_TARGET_URL));
102-
} catch (URISyntaxException e) {
103-
throw new RuntimeException(e);
104-
}
10597
serviceWaitTime = environment.getProperty("proxy.docker.service-wait-time", Integer.class, 60000);
10698
}
10799

@@ -252,20 +244,24 @@ public Proxy startContainer(Authentication user, Container initialContainer, Con
252244

253245
@Override
254246
protected URI calculateTarget(Container container, PortMappings.PortMappingEntry portMapping, Integer servicePort) throws Exception {
247+
String targetProtocol;
255248
String targetHostName;
256249
int targetPort;
257250

258251
if (isUseInternalNetwork()) {
252+
targetProtocol = getDefaultTargetProtocol();
253+
259254
// Access on containerShortId:containerPort
260255
targetHostName = container.getId().substring(0, 12);
261256
targetPort = portMapping.getPort();
262257
} else {
263258
// Access on dockerHostName:servicePort
264-
targetHostName = hostURL.getHost();
259+
targetProtocol = nonInternalNetworkTargetProtocol;
260+
targetHostName = nonInternalNetworkTargetURL.getHost();
265261
targetPort = servicePort;
266262
}
267263

268-
return new URI(String.format("%s://%s:%s%s", getDefaultTargetProtocol(), targetHostName, targetPort, portMapping.getTargetPath()));
264+
return new URI(String.format("%s://%s:%s%s", targetProtocol, targetHostName, targetPort, portMapping.getTargetPath()));
269265
}
270266

271267
private SecretBind convertSecret(DockerSwarmSecret secret) throws DockerException, InterruptedException {

0 commit comments

Comments
 (0)