|
| 1 | +package org.testcontainers.dockerclient.transport; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.DockerCmdExecFactory; |
| 4 | +import com.github.dockerjava.core.AbstractDockerCmdExecFactory; |
| 5 | +import com.github.dockerjava.core.DockerClientConfig; |
| 6 | +import com.github.dockerjava.core.SSLConfig; |
| 7 | +import com.github.dockerjava.core.WebTarget; |
| 8 | +import com.github.dockerjava.netty.NettyWebTarget; |
| 9 | +import io.netty.bootstrap.Bootstrap; |
| 10 | +import io.netty.channel.*; |
| 11 | +import io.netty.channel.epoll.EpollDomainSocketChannel; |
| 12 | +import io.netty.channel.epoll.EpollEventLoopGroup; |
| 13 | +import io.netty.channel.kqueue.KQueueDomainSocketChannel; |
| 14 | +import io.netty.channel.kqueue.KQueueEventLoopGroup; |
| 15 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 16 | +import io.netty.channel.socket.DuplexChannel; |
| 17 | +import io.netty.channel.socket.SocketChannel; |
| 18 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 19 | +import io.netty.channel.unix.DomainSocketAddress; |
| 20 | +import io.netty.channel.unix.UnixChannel; |
| 21 | +import io.netty.handler.codec.http.HttpClientCodec; |
| 22 | +import io.netty.handler.ssl.SslHandler; |
| 23 | +import io.netty.handler.timeout.IdleState; |
| 24 | +import io.netty.handler.timeout.IdleStateEvent; |
| 25 | +import io.netty.handler.timeout.IdleStateHandler; |
| 26 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 27 | +import org.apache.commons.lang.SystemUtils; |
| 28 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 29 | +import org.testcontainers.DockerClientFactory; |
| 30 | + |
| 31 | +import javax.net.ssl.SSLEngine; |
| 32 | +import javax.net.ssl.SSLParameters; |
| 33 | +import java.io.IOException; |
| 34 | +import java.net.SocketTimeoutException; |
| 35 | +import java.security.Security; |
| 36 | +import java.util.concurrent.ThreadFactory; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | + |
| 41 | +/** |
| 42 | + * This class is a modified version of docker-java's NettyDockerCmdExecFactory v3.1.0-rc-2 |
| 43 | + * Changes: |
| 44 | + * - daemonized thread factory |
| 45 | + * - thread group |
| 46 | + * - the logging handler removed |
| 47 | + * - |
| 48 | + */ |
| 49 | +public class TestcontainersDockerCmdExecFactory extends AbstractDockerCmdExecFactory implements DockerCmdExecFactory { |
| 50 | + |
| 51 | + private static final String THREAD_PREFIX = "testcontainers-netty"; |
| 52 | + |
| 53 | + /* |
| 54 | + * useful links: |
| 55 | + * |
| 56 | + * http://stackoverflow.com/questions/33296749/netty-connect-to-unix-domain-socket-failed |
| 57 | + * http://netty.io/wiki/native-transports.html |
| 58 | + * https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/http/snoop/HttpSnoopClient.java |
| 59 | + * https://github.com/slandelle/netty-request-chunking/blob/master/src/test/java/slandelle/ChunkingTest.java |
| 60 | + */ |
| 61 | + |
| 62 | + private Bootstrap bootstrap; |
| 63 | + |
| 64 | + private EventLoopGroup eventLoopGroup; |
| 65 | + |
| 66 | + private NettyInitializer nettyInitializer; |
| 67 | + |
| 68 | + private WebTarget baseResource; |
| 69 | + |
| 70 | + private Integer connectTimeout = null; |
| 71 | + |
| 72 | + private Integer readTimeout = null; |
| 73 | + |
| 74 | + @Override |
| 75 | + public void init(DockerClientConfig dockerClientConfig) { |
| 76 | + super.init(dockerClientConfig); |
| 77 | + |
| 78 | + bootstrap = new Bootstrap(); |
| 79 | + |
| 80 | + String scheme = dockerClientConfig.getDockerHost().getScheme(); |
| 81 | + |
| 82 | + if ("unix".equals(scheme)) { |
| 83 | + nettyInitializer = new UnixDomainSocketInitializer(); |
| 84 | + } else if ("tcp".equals(scheme)) { |
| 85 | + nettyInitializer = new InetSocketInitializer(); |
| 86 | + } |
| 87 | + |
| 88 | + eventLoopGroup = nettyInitializer.init(bootstrap, dockerClientConfig); |
| 89 | + |
| 90 | + baseResource = new NettyWebTarget(this::connect).path(dockerClientConfig.getApiVersion().asWebPathPart()); |
| 91 | + } |
| 92 | + |
| 93 | + private DuplexChannel connect() { |
| 94 | + try { |
| 95 | + return connect(bootstrap); |
| 96 | + } catch (InterruptedException e) { |
| 97 | + throw new RuntimeException(e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private DuplexChannel connect(final Bootstrap bootstrap) throws InterruptedException { |
| 102 | + return nettyInitializer.connect(bootstrap); |
| 103 | + } |
| 104 | + |
| 105 | + private interface NettyInitializer { |
| 106 | + EventLoopGroup init(final Bootstrap bootstrap, DockerClientConfig dockerClientConfig); |
| 107 | + |
| 108 | + DuplexChannel connect(final Bootstrap bootstrap) throws InterruptedException; |
| 109 | + } |
| 110 | + |
| 111 | + private class UnixDomainSocketInitializer implements NettyInitializer { |
| 112 | + @Override |
| 113 | + public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) { |
| 114 | + if (SystemUtils.IS_OS_LINUX) { |
| 115 | + return epollGroup(); |
| 116 | + } else if (SystemUtils.IS_OS_MAC_OSX) { |
| 117 | + return kqueueGroup(); |
| 118 | + } |
| 119 | + throw new RuntimeException("Unspported OS"); |
| 120 | + } |
| 121 | + |
| 122 | + private ThreadFactory createThreadFactory() { |
| 123 | + return new DefaultThreadFactory(THREAD_PREFIX, true, Thread.NORM_PRIORITY, DockerClientFactory.TESTCONTAINERS_THREAD_GROUP); |
| 124 | + } |
| 125 | + |
| 126 | + public EventLoopGroup epollGroup() { |
| 127 | + EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, createThreadFactory()); |
| 128 | + |
| 129 | + ChannelFactory<EpollDomainSocketChannel> factory = () -> configure(new EpollDomainSocketChannel()); |
| 130 | + |
| 131 | + bootstrap.group(epollEventLoopGroup).channelFactory(factory).handler(new ChannelInitializer<UnixChannel>() { |
| 132 | + @Override |
| 133 | + protected void initChannel(final UnixChannel channel) throws Exception { |
| 134 | + channel.pipeline().addLast(new HttpClientCodec()); |
| 135 | + } |
| 136 | + }); |
| 137 | + return epollEventLoopGroup; |
| 138 | + } |
| 139 | + |
| 140 | + public EventLoopGroup kqueueGroup() { |
| 141 | + EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, createThreadFactory()); |
| 142 | + |
| 143 | + bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class) |
| 144 | + .handler(new ChannelInitializer<KQueueDomainSocketChannel>() { |
| 145 | + @Override |
| 146 | + protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception { |
| 147 | + channel.pipeline().addLast(new HttpClientCodec()); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + return nioEventLoopGroup; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException { |
| 156 | + return (DuplexChannel) bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")).sync().channel(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private class InetSocketInitializer implements NettyInitializer { |
| 161 | + @Override |
| 162 | + public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) { |
| 163 | + EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, new DefaultThreadFactory(THREAD_PREFIX)); |
| 164 | + |
| 165 | + // TODO do we really need BouncyCastle? |
| 166 | + Security.addProvider(new BouncyCastleProvider()); |
| 167 | + |
| 168 | + ChannelFactory<NioSocketChannel> factory = () -> configure(new NioSocketChannel()); |
| 169 | + |
| 170 | + bootstrap.group(nioEventLoopGroup).channelFactory(factory) |
| 171 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 172 | + @Override |
| 173 | + protected void initChannel(final SocketChannel channel) throws Exception { |
| 174 | + channel.pipeline().addLast(new HttpClientCodec()); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + return nioEventLoopGroup; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException { |
| 183 | + DockerClientConfig dockerClientConfig = getDockerClientConfig(); |
| 184 | + String host = dockerClientConfig.getDockerHost().getHost(); |
| 185 | + int port = dockerClientConfig.getDockerHost().getPort(); |
| 186 | + |
| 187 | + if (port == -1) { |
| 188 | + throw new RuntimeException("no port configured for " + host); |
| 189 | + } |
| 190 | + |
| 191 | + DuplexChannel channel = (DuplexChannel) bootstrap.connect(host, port).sync().channel(); |
| 192 | + |
| 193 | + final SslHandler ssl = initSsl(dockerClientConfig); |
| 194 | + |
| 195 | + if (ssl != null) { |
| 196 | + channel.pipeline().addFirst(ssl); |
| 197 | + } |
| 198 | + |
| 199 | + return channel; |
| 200 | + } |
| 201 | + |
| 202 | + private SslHandler initSsl(DockerClientConfig dockerClientConfig) { |
| 203 | + SslHandler ssl = null; |
| 204 | + |
| 205 | + try { |
| 206 | + String host = dockerClientConfig.getDockerHost().getHost(); |
| 207 | + int port = dockerClientConfig.getDockerHost().getPort(); |
| 208 | + |
| 209 | + final SSLConfig sslConfig = dockerClientConfig.getSSLConfig(); |
| 210 | + |
| 211 | + if (sslConfig != null && sslConfig.getSSLContext() != null) { |
| 212 | + |
| 213 | + SSLEngine engine = sslConfig.getSSLContext().createSSLEngine(host, port); |
| 214 | + engine.setUseClientMode(true); |
| 215 | + engine.setSSLParameters(enableHostNameVerification(engine.getSSLParameters())); |
| 216 | + |
| 217 | + // in the future we may use HostnameVerifier like here: |
| 218 | + // https://github.com/AsyncHttpClient/async-http-client/blob/1.8.x/src/main/java/com/ning/http/client/providers/netty/NettyConnectListener.java#L76 |
| 219 | + |
| 220 | + ssl = new SslHandler(engine); |
| 221 | + } |
| 222 | + |
| 223 | + } catch (Exception e) { |
| 224 | + throw new RuntimeException(e); |
| 225 | + } |
| 226 | + |
| 227 | + return ssl; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + public SSLParameters enableHostNameVerification(SSLParameters sslParameters) { |
| 232 | + sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); |
| 233 | + return sslParameters; |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + public void close() throws IOException { |
| 238 | + checkNotNull(eventLoopGroup, "Factory not initialized. You probably forgot to call init()!"); |
| 239 | + |
| 240 | + eventLoopGroup.shutdownGracefully(); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Configure connection timeout in milliseconds |
| 245 | + */ |
| 246 | + public TestcontainersDockerCmdExecFactory withConnectTimeout(Integer connectTimeout) { |
| 247 | + this.connectTimeout = connectTimeout; |
| 248 | + return this; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Configure read timeout in milliseconds |
| 253 | + */ |
| 254 | + public TestcontainersDockerCmdExecFactory withReadTimeout(Integer readTimeout) { |
| 255 | + this.readTimeout = readTimeout; |
| 256 | + return this; |
| 257 | + } |
| 258 | + |
| 259 | + private <T extends Channel> T configure(T channel) { |
| 260 | + ChannelConfig channelConfig = channel.config(); |
| 261 | + |
| 262 | + if (connectTimeout != null) { |
| 263 | + channelConfig.setConnectTimeoutMillis(connectTimeout); |
| 264 | + } |
| 265 | + if (readTimeout != null) { |
| 266 | + channel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler()); |
| 267 | + } |
| 268 | + |
| 269 | + return channel; |
| 270 | + } |
| 271 | + |
| 272 | + private final class ReadTimeoutHandler extends IdleStateHandler { |
| 273 | + private boolean alreadyTimedOut; |
| 274 | + |
| 275 | + ReadTimeoutHandler() { |
| 276 | + super(readTimeout, 0, 0, TimeUnit.MILLISECONDS); |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Called when a read timeout was detected. |
| 281 | + */ |
| 282 | + @Override |
| 283 | + protected synchronized void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { |
| 284 | + assert evt.state() == IdleState.READER_IDLE; |
| 285 | + final Channel channel = ctx.channel(); |
| 286 | + if (channel == null || !channel.isActive() || alreadyTimedOut) { |
| 287 | + return; |
| 288 | + } |
| 289 | + DockerClientConfig dockerClientConfig = getDockerClientConfig(); |
| 290 | + final Object dockerAPIEndpoint = dockerClientConfig.getDockerHost(); |
| 291 | + final String msg = "Read timed out: No data received within " + readTimeout |
| 292 | + + "ms. Perhaps the docker API (" + dockerAPIEndpoint |
| 293 | + + ") is not responding normally, or perhaps you need to increase the readTimeout value."; |
| 294 | + final Exception ex = new SocketTimeoutException(msg); |
| 295 | + ctx.fireExceptionCaught(ex); |
| 296 | + alreadyTimedOut = true; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + protected WebTarget getBaseResource() { |
| 301 | + checkNotNull(baseResource, "Factory not initialized, baseResource not set. You probably forgot to call init()!"); |
| 302 | + return baseResource; |
| 303 | + } |
| 304 | +} |
0 commit comments