// Generated by delombok at Sun Sep 08 08:37:39 UTC 2019
package org.testcontainers.containers;
import com.github.dockerjava.api.model.
ContainerNetwork;
import com.
trilead.
ssh2.
Connection;
import org.testcontainers.utility.
TestcontainersConfiguration;
import java.time.
Duration;
import java.util.
AbstractMap;
import java.util.
Collections;
import java.util.
Optional;
import java.util.
Set;
import java.util.
UUID;
import java.util.
Map.
Entry;
import java.util.concurrent.
ConcurrentHashMap;
public enum
PortForwardingContainer {
INSTANCE;
private
GenericContainer container;
private final
Set<
Entry<
Integer,
Integer>>
exposedPorts =
Collections.
newSetFromMap(new
ConcurrentHashMap<>());
private final java.util.concurrent.atomic.
AtomicReference<java.lang.
Object>
sshConnection = new java.util.concurrent.atomic.
AtomicReference<java.lang.
Object>();
private
Connection createSSHSession() {
try {
String password =
UUID.
randomUUID().
toString();
container = new
GenericContainer<>(
TestcontainersConfiguration.
getInstance().
getSSHdImage()).
withExposedPorts(22).
withEnv("PASSWORD",
password).
withCommand("sh", "-c",
// Disable ipv6 & Make it listen on all interfaces, not just localhost
"echo \"root:$PASSWORD\" | chpasswd && /usr/sbin/sshd -D -o PermitRootLogin=yes -o AddressFamily=inet -o GatewayPorts=yes");
container.
start();
Connection connection = new
Connection(
container.
getContainerIpAddress(),
container.
getMappedPort(22));
connection.
setTCPNoDelay(true);
connection.
connect((
hostname,
port,
serverHostKeyAlgorithm,
serverHostKey) -> true, (int)
Duration.
ofSeconds(30).
toMillis(), (int)
Duration.
ofSeconds(30).
toMillis());
if (!
connection.
authenticateWithPassword("root",
password)) {
throw new
IllegalStateException("Authentication failed.");
}
return
connection;
} catch (final java.lang.
Throwable $ex) {
throw lombok.
Lombok.
sneakyThrow(
$ex);
}
}
public void
exposeHostPort(int
port) {
try {
exposeHostPort(
port,
port);
} catch (final java.lang.
Throwable $ex) {
throw lombok.
Lombok.
sneakyThrow(
$ex);
}
}
public void
exposeHostPort(int
hostPort, int
containerPort) {
try {
if (
exposedPorts.
add(new
AbstractMap.
SimpleEntry<>(
hostPort,
containerPort))) {
getSshConnection().
requestRemotePortForwarding("",
containerPort, "localhost",
hostPort);
}
} catch (final java.lang.
Throwable $ex) {
throw lombok.
Lombok.
sneakyThrow(
$ex);
}
}
Optional<
ContainerNetwork>
getNetwork() {
return
Optional.
ofNullable(
container).
map(
GenericContainer::getContainerInfo).
flatMap(
it ->
it.
getNetworkSettings().
getNetworks().
values().
stream().
findFirst());
}
@java.lang.
SuppressWarnings("all")
private
Connection getSshConnection() {
java.lang.
Object value = this.
sshConnection.
get();
if (
value == null) {
synchronized (this.
sshConnection) {
value = this.
sshConnection.
get();
if (
value == null) {
final
Connection actualValue =
createSSHSession();
value =
actualValue == null ? this.
sshConnection :
actualValue;
this.
sshConnection.
set(
value);
}
}
}
return (
Connection) (
value == this.
sshConnection ? null :
value);
}
}