Skip to content

Commit 91f8ca1

Browse files
authored
Merge pull request #63 from tls-attacker/imageProperties
Start container with specific name
2 parents f90e29b + 9edbd35 commit 91f8ca1

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/main/java/de/rub/nds/tls/subject/docker/DockerTlsClientInstance.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public class DockerTlsClientInstance extends DockerTlsInstance {
3333
private final boolean connectOnStartup;
3434

3535
// TODO move away from HostInfo for client...
36-
public DockerTlsClientInstance(ParameterProfile profile, ImageProperties imageProperties, String version,
36+
public DockerTlsClientInstance(String containerName, ParameterProfile profile, ImageProperties imageProperties, String version,
3737
boolean autoRemove, HostInfo hostInfo, String additionalParameters, boolean parallelize,
3838
boolean insecureConnection, boolean connectOnStartup,
3939
UnaryOperator<HostConfig> hostConfigHook) {
40-
super(profile, imageProperties, version, ConnectionRole.CLIENT, autoRemove, hostConfigHook);
40+
super(containerName, profile, imageProperties, version, ConnectionRole.CLIENT, autoRemove, hostConfigHook);
4141
this.hostInfo = hostInfo;
4242
this.additionalParameters = additionalParameters;
4343
this.parallelize = parallelize;

src/main/java/de/rub/nds/tls/subject/docker/DockerTlsInstance.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public abstract class DockerTlsInstance {
3535
protected static final DockerClient DOCKER = DockerClientManager.getDockerClient();
3636
private static final Logger LOGGER = LogManager.getLogger();
3737

38+
private final String containerName;
3839
private String containerId;
3940
protected final Image image;
4041
private Optional<Long> exitCode = Optional.empty();
@@ -45,7 +46,7 @@ public abstract class DockerTlsInstance {
4546
protected List<DockerExecInstance> childExecs = new LinkedList<>();
4647
private final UnaryOperator<HostConfig> hostConfigHook;
4748

48-
public DockerTlsInstance(ParameterProfile profile, ImageProperties imageProperties,
49+
public DockerTlsInstance(String containerName, ParameterProfile profile, ImageProperties imageProperties,
4950
String version, ConnectionRole role, boolean autoRemove,
5051
UnaryOperator<HostConfig> hostConfigHook) {
5152
if (profile == null) {
@@ -57,7 +58,8 @@ public DockerTlsInstance(ParameterProfile profile, ImageProperties imageProperti
5758
this.autoRemove = autoRemove;
5859
this.parameterProfile = profile;
5960
this.imageProperties = imageProperties;
60-
this.hostConfigHook = hostConfigHook;
61+
this.hostConfigHook = hostConfigHook;
62+
this.containerName = containerName;
6163
Map<String, String> labels = new HashMap<>();
6264
labels.put(TlsImageLabels.IMPLEMENTATION.getLabelName(), profile.getType().name().toLowerCase());
6365
labels.put(TlsImageLabels.VERSION.getLabelName(), version);
@@ -106,6 +108,9 @@ protected String createContainer() {
106108
@SuppressWarnings("squid:S2095") // sonarlint: Resources should be closed
107109
// Create container does not need to be closed
108110
CreateContainerCmd containerCmd = DOCKER.createContainerCmd(image.getId());
111+
if(containerName != null) {
112+
containerCmd.withName(containerName);
113+
}
109114
containerCmd = prepareCreateContainerCmd(containerCmd);
110115
CreateContainerResponse container = containerCmd.exec();
111116
String[] warnings = container.getWarnings();
@@ -248,4 +253,8 @@ public long getExitCode() {
248253
}
249254
return exitCode.get();
250255
}
256+
257+
public String getContainerName() {
258+
return containerName;
259+
}
251260
}

src/main/java/de/rub/nds/tls/subject/docker/DockerTlsManagerFactory.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public abstract static class TlsInstanceBuilder<T extends TlsInstanceBuilder<T>>
5353
protected String additionalParameters = null;
5454
protected boolean parallelize = false;
5555
protected boolean insecureConnection = false;
56+
protected String containerName;
5657

5758
public TlsInstanceBuilder(TlsImplementationType type, String version, ConnectionRole role, TransportType transportType) {
5859
this.profile = retrieveParameterProfile(type, version, role);
@@ -75,6 +76,12 @@ public T hostname(String value) {
7576
hostname = value;
7677
return (T) this;
7778
}
79+
80+
public T containerName(String value){
81+
containerName = value;
82+
return (T) this;
83+
}
84+
7885

7986
public T port(int value) {
8087
port = value;
@@ -114,7 +121,7 @@ public TlsClientInstanceBuilder(TlsImplementationType type, String version, Tran
114121

115122
@Override
116123
public DockerTlsClientInstance build() throws DockerException, InterruptedException {
117-
return new DockerTlsClientInstance(profile, imageProperties, version, autoRemove, new HostInfo(ip, hostname, port, transportType), additionalParameters, parallelize, insecureConnection,
124+
return new DockerTlsClientInstance(containerName, profile, imageProperties, version, autoRemove, new HostInfo(ip, hostname, port, transportType), additionalParameters, parallelize, insecureConnection,
118125
connectOnStartup, hostConfigHook);
119126
}
120127

@@ -133,7 +140,7 @@ public TlsServerInstanceBuilder(TlsImplementationType type, String version, Tran
133140

134141
@Override
135142
public DockerTlsServerInstance build() throws DockerException, InterruptedException {
136-
return new DockerTlsServerInstance(profile, imageProperties, version, autoRemove, new HostInfo(ip, hostname, port, transportType), additionalParameters, parallelize, insecureConnection,
143+
return new DockerTlsServerInstance(containerName, profile, imageProperties, version, autoRemove, new HostInfo(ip, hostname, port, transportType), additionalParameters, parallelize, insecureConnection,
137144
hostConfigHook);
138145
}
139146

src/main/java/de/rub/nds/tls/subject/docker/DockerTlsServerInstance.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import de.rub.nds.tls.subject.HostInfo;
1515
import de.rub.nds.tls.subject.params.ParameterProfile;
1616
import de.rub.nds.tls.subject.properties.ImageProperties;
17+
import static java.lang.Thread.sleep;
18+
import java.util.logging.Level;
19+
import java.util.logging.Logger;
1720

1821
public class DockerTlsServerInstance extends DockerTlsInstance {
1922

@@ -23,10 +26,10 @@ public class DockerTlsServerInstance extends DockerTlsInstance {
2326
private final boolean parallelize;
2427
private final boolean insecureConnection;
2528

26-
public DockerTlsServerInstance(ParameterProfile profile, ImageProperties imageProperties, String version, boolean autoRemove, HostInfo hostInfo, String additionalParameters, boolean parallelize,
29+
public DockerTlsServerInstance(String containerName, ParameterProfile profile, ImageProperties imageProperties, String version, boolean autoRemove, HostInfo hostInfo, String additionalParameters, boolean parallelize,
2730
boolean insecureConnection,
2831
UnaryOperator<HostConfig> hostConfigHook) {
29-
super(profile, imageProperties, version, ConnectionRole.SERVER, autoRemove, hostConfigHook);
32+
super(containerName, profile, imageProperties, version, ConnectionRole.SERVER, autoRemove, hostConfigHook);
3033
this.port = hostInfo.getPort(); // fill with default port
3134
this.hostInfo = hostInfo;
3235
this.additionalParameters = additionalParameters;
@@ -64,16 +67,7 @@ public void start() {
6467
* Update port to match actually exposed port.
6568
*/
6669
public void updateInstancePort() {
67-
InspectContainerResponse containerInfo = DOCKER.inspectContainerCmd(getId()).exec();
68-
if (containerInfo == null) {
69-
throw new IllegalStateException("Could not find container with ID:" + getId());
70-
}
71-
NetworkSettings networkSettings = containerInfo.getNetworkSettings();
72-
if (networkSettings == null) {
73-
throw new IllegalStateException("Cannot retrieve InstacePort, Network not properly configured for container with ID:" + getId());
74-
}
75-
// TODO: ignore other exposed ports
76-
port = new Integer(networkSettings.getPorts().getBindings().values().iterator().next()[0].getHostPortSpec());
70+
port = imageProperties.getInternalPort();
7771
}
7872

7973
public int getPort() {

0 commit comments

Comments
 (0)