Search in sources :

Example 46 with SslContext

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext in project reactor-netty by reactor.

the class TcpServerTests method sendFileSecure.

@Test
public void sendFileSecure() throws CertificateException, SSLException, InterruptedException, URISyntaxException {
    Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(ssc.cert()).build();
    NettyContext context = TcpServer.create(opt -> opt.sslContext(sslServer)).newHandler((in, out) -> in.receive().asString().flatMap(word -> "GOGOGO".equals(word) ? out.sendFile(largeFile).then() : out.sendString(Mono.just("NOPE")))).block();
    MonoProcessor<String> m1 = MonoProcessor.create();
    MonoProcessor<String> m2 = MonoProcessor.create();
    NettyContext client1 = TcpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).newHandler((in, out) -> {
        in.receive().asString().log("-----------------CLIENT1").subscribe(m1::onNext);
        return out.sendString(Mono.just("gogogo")).neverComplete();
    }).block();
    NettyContext client2 = TcpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).newHandler((in, out) -> {
        in.receive().asString(StandardCharsets.UTF_8).take(2).reduceWith(String::new, String::concat).log("-----------------CLIENT2").subscribe(m2::onNext);
        return out.sendString(Mono.just("GOGOGO")).neverComplete();
    }).block();
    String client1Response = m1.block();
    String client2Response = m2.block();
    client1.dispose();
    client1.onClose().block();
    client2.dispose();
    client2.onClose().block();
    context.dispose();
    context.onClose().block();
    Assertions.assertThat(client1Response).isEqualTo("NOPE");
    Assertions.assertThat(client2Response).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here ->").contains("<- 1024 mark here").endsWith("End of File");
}
Also used : Path(java.nio.file.Path) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Processor(org.reactivestreams.Processor) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) After(org.junit.After) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) SocketUtils(reactor.ipc.netty.SocketUtils) Path(java.nio.file.Path) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Exceptions(reactor.core.Exceptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) WorkQueueProcessor(reactor.core.publisher.WorkQueueProcessor) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) NettyInbound(reactor.ipc.netty.NettyInbound) NettyPipeline(reactor.ipc.netty.NettyPipeline) EmitterProcessor(reactor.core.publisher.EmitterProcessor) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpServer(reactor.ipc.netty.http.server.HttpServer) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MonoProcessor(reactor.core.publisher.MonoProcessor) NetUtil(io.netty.util.NetUtil) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) FileSystems(java.nio.file.FileSystems) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SslContext(io.netty.handler.ssl.SslContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 47 with SslContext

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext in project reactor-netty by reactor.

the class HttpServerTests method sendFileSecure.

@Test
public void sendFileSecure() throws CertificateException, SSLException, URISyntaxException {
    Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)).newHandler((req, resp) -> resp.sendFile(largeFile)).block();
    HttpClientResponse response = HttpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo").block(Duration.ofSeconds(120));
    context.dispose();
    context.onClose().block();
    String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();
    assertThat(body).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here -><- 1024 mark here").endsWith("End of File");
}
Also used : Path(java.nio.file.Path) HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) SslContext(io.netty.handler.ssl.SslContext) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 48 with SslContext

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext in project reactor-netty by reactor.

the class HttpClientTest method sshExchangeAbsoluteGet.

@Test
public void sshExchangeAbsoluteGet() throws CertificateException, SSLException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)).newHandler((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))).block();
    HttpClientResponse response = HttpClient.create(opt -> applyHostAndPortFromContext(opt, context).sslContext(sslClient)).get("/foo").block();
    context.dispose();
    context.onClose().block();
    String responseString = response.receive().aggregate().asString(CharsetUtil.UTF_8).block();
    assertThat(responseString).isEqualTo("hello /foo");
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SslContext(io.netty.handler.ssl.SslContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 49 with SslContext

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext in project reactor-netty by reactor.

the class ByteBufFluxTest method doTestByteBufFluxFromPath.

private void doTestByteBufFluxFromPath(boolean withSecurity) throws Exception {
    Consumer<HttpServerOptions.Builder> serverOptions;
    Consumer<HttpClientOptions.Builder> clientOptions;
    final int serverPort = SocketUtils.findAvailableTcpPort();
    if (withSecurity) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        serverOptions = ops -> ops.port(serverPort).sslContext(sslServer);
        clientOptions = ops -> ops.port(serverPort).sslContext(sslClient);
    } else {
        serverOptions = ops -> ops.port(serverPort);
        clientOptions = ops -> ops.port(serverPort);
    }
    Path path = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    HttpServer.create(serverOptions).newHandler((req, res) -> res.send(ByteBufFlux.fromPath(path)).then()).block(Duration.ofSeconds(30));
    AtomicLong counter = new AtomicLong(0);
    HttpClient.create(clientOptions).get("/download").flatMapMany(NettyInbound::receive).doOnNext(b -> counter.addAndGet(b.readableBytes())).blockLast(Duration.ofSeconds(30));
    assertTrue(counter.get() == 1245);
}
Also used : Path(java.nio.file.Path) HttpClientOptions(reactor.ipc.netty.http.client.HttpClientOptions) AfterClass(org.junit.AfterClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Iterator(java.util.Iterator) SslContext(io.netty.handler.ssl.SslContext) BeforeClass(org.junit.BeforeClass) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) HttpServerOptions(reactor.ipc.netty.http.server.HttpServerOptions) File(java.io.File) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Paths(java.nio.file.Paths) Duration(java.time.Duration) HttpClient(reactor.ipc.netty.http.client.HttpClient) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Assert(org.junit.Assert) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) AtomicLong(java.util.concurrent.atomic.AtomicLong) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) SslContext(io.netty.handler.ssl.SslContext)

Example 50 with SslContext

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext in project drill by apache.

the class SSLConfigServer method initNettySslContext.

@Override
public SslContext initNettySslContext() throws DrillException {
    final SslContext sslCtx;
    if (!userSslEnabled) {
        return null;
    }
    KeyManagerFactory kmf;
    TrustManagerFactory tmf;
    try {
        if (keyStorePath.isEmpty()) {
            throw new DrillException("No Keystore provided.");
        }
        kmf = initializeKeyManagerFactory();
        tmf = initializeTrustManagerFactory();
        sslCtx = SslContextBuilder.forServer(kmf).trustManager(tmf).protocols(protocol).sslProvider(getProvider()).build();
    } catch (Exception e) {
        // Catch any SSL initialization Exceptions here and abort.
        throw new DrillException(new StringBuilder().append("SSL is enabled but cannot be initialized - ").append("[ ").append(e.getMessage()).append("]. ").toString());
    }
    this.nettySslContext = sslCtx;
    return sslCtx;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) DrillException(org.apache.drill.common.exceptions.DrillException) DrillException(org.apache.drill.common.exceptions.DrillException) SslContext(io.netty.handler.ssl.SslContext) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

SslContext (io.netty.handler.ssl.SslContext)221 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)67 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)59 EventLoopGroup (io.netty.channel.EventLoopGroup)52 Channel (io.netty.channel.Channel)48 Test (org.junit.Test)48 SSLException (javax.net.ssl.SSLException)46 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)41 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)37 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 Bootstrap (io.netty.bootstrap.Bootstrap)35 LoggingHandler (io.netty.handler.logging.LoggingHandler)35 SocketChannel (io.netty.channel.socket.SocketChannel)34 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)33 InetSocketAddress (java.net.InetSocketAddress)31 SslHandler (io.netty.handler.ssl.SslHandler)30 CertificateException (java.security.cert.CertificateException)29 IOException (java.io.IOException)26 File (java.io.File)24 ChannelPipeline (io.netty.channel.ChannelPipeline)23