Search in sources :

Example 21 with SslContext

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

the class DiscoveryServiceTest method connectToService.

/**
 * creates ClientHandler channel to connect and communicate with server
 *
 * @param serviceUrl
 * @param latch
 * @return
 * @throws URISyntaxException
 */
public static NioEventLoopGroup connectToService(String serviceUrl, CountDownLatch latch, boolean tls) throws URISyntaxException {
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            if (tls) {
                SslContextBuilder builder = SslContextBuilder.forClient();
                builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
                X509Certificate[] certificates = SecurityUtility.loadCertificatesFromPemFile(TLS_CLIENT_CERT_FILE_PATH);
                PrivateKey privateKey = SecurityUtility.loadPrivateKeyFromPemFile(TLS_CLIENT_KEY_FILE_PATH);
                builder.keyManager(privateKey, (X509Certificate[]) certificates);
                SslContext sslCtx = builder.build();
                ch.pipeline().addLast("tls", sslCtx.newHandler(ch.alloc()));
            }
            ch.pipeline().addLast(new ClientHandler(latch));
        }
    });
    URI uri = new URI(serviceUrl);
    InetSocketAddress serviceAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
    b.connect(serviceAddress).addListener((ChannelFuture future) -> {
        if (!future.isSuccess()) {
            throw new IllegalStateException(future.cause());
        }
    });
    return workerGroup;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) PrivateKey(java.security.PrivateKey) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) ExecutionException(java.util.concurrent.ExecutionException) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 22 with SslContext

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

the class NettyWebSocket method connect.

@Override
public void connect(ITransportHandler transportHandler, TransportOptions options) throws Exception {
    if (options == null) {
        if (mOptions == null) {
            options = new TransportOptions();
        } else {
            options = new TransportOptions();
            options.setAutoPingInterval(mOptions.getAutoPingInterval());
            options.setAutoPingTimeout(mOptions.getAutoPingTimeout());
            options.setMaxFramePayloadSize(mOptions.getMaxFramePayloadSize());
        }
    }
    URI uri;
    uri = new URI(mUri);
    int port = validateURIAndGetPort(uri);
    String scheme = uri.getScheme();
    String host = uri.getHost();
    final SslContext sslContext = getSSLContext(scheme);
    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, mSerializers, true, new DefaultHttpHeaders(), options.getMaxFramePayloadSize());
    mHandler = new NettyWebSocketClientHandler(handshaker, this, transportHandler);
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group);
    bootstrap.channel(NioSocketChannel.class);
    TransportOptions opt = options;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline channelPipeline = ch.pipeline();
            if (sslContext != null) {
                channelPipeline.addLast(sslContext.newHandler(ch.alloc(), host, port));
            }
            channelPipeline.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, new IdleStateHandler(opt.getAutoPingInterval() + opt.getAutoPingTimeout(), opt.getAutoPingInterval(), 0, TimeUnit.SECONDS), mHandler);
        }
    });
    mChannel = bootstrap.connect(uri.getHost(), port).sync().channel();
    mHandler.getHandshakeFuture().sync();
}
Also used : WebSocketClientHandshaker(io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) TransportOptions(io.crossbar.autobahn.wamp.types.TransportOptions) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) URI(java.net.URI) SSLException(javax.net.ssl.SSLException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 23 with SslContext

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

the class GatewayAutoConfigurationTests method nettyHttpClientDefaults.

@Test
public void nettyHttpClientDefaults() {
    new ReactiveWebApplicationContextRunner().withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class, GatewayAutoConfiguration.class)).withPropertyValues("debug=true").run(context -> {
        assertThat(context).hasSingleBean(HttpClient.class);
        HttpClient httpClient = context.getBean(HttpClient.class);
        HttpClientOptions options = httpClient.options();
        PoolResources poolResources = options.getPoolResources();
        assertThat(poolResources).isNotNull();
        // TODO: howto test PoolResources
        ClientProxyOptions proxyOptions = options.getProxyOptions();
        assertThat(proxyOptions).isNull();
        SslContext sslContext = options.sslContext();
        assertThat(sslContext).isNull();
    });
}
Also used : WebFluxAutoConfiguration(org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration) PoolResources(reactor.ipc.netty.resources.PoolResources) ClientProxyOptions(reactor.ipc.netty.options.ClientProxyOptions) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpClientOptions(reactor.ipc.netty.http.client.HttpClientOptions) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 24 with SslContext

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

the class GatewayAutoConfigurationTests method nettyHttpClientConfigured.

@Test
public void nettyHttpClientConfigured() {
    new ReactiveWebApplicationContextRunner().withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class, GatewayAutoConfiguration.class)).withPropertyValues("spring.cloud.gateway.httpclient.ssl.use-insecure-trust-manager=true", "spring.cloud.gateway.httpclient.pool.type=fixed", "spring.cloud.gateway.httpclient.proxy.host=myhost").run(context -> {
        assertThat(context).hasSingleBean(HttpClient.class);
        HttpClient httpClient = context.getBean(HttpClient.class);
        HttpClientOptions options = httpClient.options();
        PoolResources poolResources = options.getPoolResources();
        assertThat(poolResources).isNotNull();
        // TODO: howto test PoolResources
        ClientProxyOptions proxyOptions = options.getProxyOptions();
        assertThat(proxyOptions).isNotNull();
        assertThat(proxyOptions.getAddress().get().getHostName()).isEqualTo("myhost");
        SslContext sslContext = options.sslContext();
        assertThat(sslContext).isNotNull();
    // TODO: howto test SslContext
    });
}
Also used : WebFluxAutoConfiguration(org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration) PoolResources(reactor.ipc.netty.resources.PoolResources) ClientProxyOptions(reactor.ipc.netty.options.ClientProxyOptions) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpClientOptions(reactor.ipc.netty.http.client.HttpClientOptions) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 25 with SslContext

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

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    try {
        Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
        Struct serviceEndpointConfig = serviceEndpoint.getStructField(EndpointConstants.ENDPOINT_CONFIG);
        EndpointConfiguration configuration = EndpointUtils.getEndpointConfiguration(serviceEndpointConfig);
        io.grpc.ServerBuilder serverBuilder;
        if (configuration.getSslConfig() != null) {
            SslContext sslCtx = new SSLHandlerFactory(configuration.getSslConfig()).createHttp2TLSContextForServer();
            serverBuilder = GrpcServicesBuilder.initService(configuration, sslCtx);
        } else {
            serverBuilder = GrpcServicesBuilder.initService(configuration, null);
        }
        serviceEndpoint.addNativeData(SERVICE_BUILDER, serverBuilder);
        context.setReturnValues();
    } catch (Throwable throwable) {
        BStruct err = getConnectorError(context, throwable);
        context.setError(err);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) EndpointConfiguration(org.ballerinalang.net.grpc.config.EndpointConfiguration) SSLHandlerFactory(org.ballerinalang.net.grpc.ssl.SSLHandlerFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

SslContext (io.netty.handler.ssl.SslContext)220 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 ChannelPipeline (io.netty.channel.ChannelPipeline)23 File (java.io.File)23