Search in sources :

Example 1 with ProtocolNegotiationEvent

use of io.grpc.netty.ProtocolNegotiationEvent in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method serverSdsHandler_addLast.

@Test
public void serverSdsHandler_addLast() throws InterruptedException, TimeoutException, ExecutionException {
    FakeClock executor = new FakeClock();
    CommonCertProviderTestUtils.register(executor);
    // we need InetSocketAddress instead of EmbeddedSocketAddress as localAddress for this test
    channel = new EmbeddedChannel() {

        @Override
        public SocketAddress localAddress() {
            return new InetSocketAddress("172.168.1.1", 80);
        }

        @Override
        public SocketAddress remoteAddress() {
            return new InetSocketAddress("172.168.2.2", 90);
        }
    };
    pipeline = channel.pipeline();
    Bootstrapper.BootstrapInfo bootstrapInfoForServer = CommonBootstrapperTestUtils.buildBootstrapInfo("google_cloud_private_spiffe-server", SERVER_1_KEY_FILE, SERVER_1_PEM_FILE, CA_PEM_FILE, null, null, null, null);
    DownstreamTlsContext downstreamTlsContext = CommonTlsContextTestsUtil.buildDownstreamTlsContext("google_cloud_private_spiffe-server", true, true);
    TlsContextManagerImpl tlsContextManager = new TlsContextManagerImpl(bootstrapInfoForServer);
    SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, InternalProtocolNegotiators.serverPlaintext());
    pipeline.addLast(handlerPickerHandler);
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    // should find HandlerPickerHandler
    assertThat(channelHandlerCtx).isNotNull();
    // kick off protocol negotiation: should replace HandlerPickerHandler with ServerSdsHandler
    ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
    Attributes attr = InternalProtocolNegotiationEvent.getAttributes(event).toBuilder().set(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER, new SslContextProviderSupplier(downstreamTlsContext, tlsContextManager)).build();
    pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.withAttributes(event, attr));
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    assertThat(channelHandlerCtx).isNull();
    channelHandlerCtx = pipeline.context(SdsProtocolNegotiators.ServerSdsHandler.class);
    assertThat(channelHandlerCtx).isNotNull();
    SslContextProviderSupplier sslContextProviderSupplier = new SslContextProviderSupplier(downstreamTlsContext, tlsContextManager);
    final SettableFuture<Object> future = SettableFuture.create();
    sslContextProviderSupplier.updateSslContext(new SslContextProvider.Callback(MoreExecutors.directExecutor()) {

        @Override
        public void updateSecret(SslContext sslContext) {
            future.set(sslContext);
        }

        @Override
        protected void onException(Throwable throwable) {
            future.set(throwable);
        }
    });
    // need this for tasks to execute on eventLoop
    channel.runPendingTasks();
    assertThat(executor.runDueTasks()).isEqualTo(1);
    Object fromFuture = future.get(2, TimeUnit.SECONDS);
    assertThat(fromFuture).isInstanceOf(SslContext.class);
    channel.runPendingTasks();
    channelHandlerCtx = pipeline.context(SdsProtocolNegotiators.ServerSdsHandler.class);
    assertThat(channelHandlerCtx).isNull();
    // pipeline should only have SslHandler and ServerTlsHandler
    Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
    assertThat(iterator.next().getValue()).isInstanceOf(SslHandler.class);
    // ProtocolNegotiators.ServerTlsHandler.class is not accessible, get canonical name
    assertThat(iterator.next().getValue().getClass().getCanonicalName()).contains("ProtocolNegotiators.ServerTlsHandler");
    CommonCertProviderTestUtils.register0();
}
Also used : ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) FakeClock(io.grpc.internal.FakeClock) InetSocketAddress(java.net.InetSocketAddress) DownstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext) Attributes(io.grpc.Attributes) InternalXdsAttributes(io.grpc.xds.InternalXdsAttributes) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Bootstrapper(io.grpc.xds.Bootstrapper) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 2 with ProtocolNegotiationEvent

use of io.grpc.netty.ProtocolNegotiationEvent in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method serverSdsHandler_defaultDownstreamTlsContext_expectFallbackProtocolNegotiator.

@Test
public void serverSdsHandler_defaultDownstreamTlsContext_expectFallbackProtocolNegotiator() throws IOException {
    ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
    ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
    when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
    // we need InetSocketAddress instead of EmbeddedSocketAddress as localAddress for this test
    channel = new EmbeddedChannel() {

        @Override
        public SocketAddress localAddress() {
            return new InetSocketAddress("172.168.1.1", 80);
        }
    };
    pipeline = channel.pipeline();
    SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, mockProtocolNegotiator);
    pipeline.addLast(handlerPickerHandler);
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    // should find HandlerPickerHandler
    assertThat(channelHandlerCtx).isNotNull();
    // kick off protocol negotiation: should replace HandlerPickerHandler with ServerSdsHandler
    ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
    Attributes attr = InternalProtocolNegotiationEvent.getAttributes(event).toBuilder().set(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER, null).build();
    pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.withAttributes(event, attr));
    channelHandlerCtx = pipeline.context(handlerPickerHandler);
    assertThat(channelHandlerCtx).isNull();
    // need this for tasks to execute on eventLoop
    channel.runPendingTasks();
    Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
    assertThat(iterator.next().getValue()).isSameInstanceAs(mockChannelHandler);
    // no more handlers in the pipeline
    assertThat(iterator.hasNext()).isFalse();
}
Also used : ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) InetSocketAddress(java.net.InetSocketAddress) Attributes(io.grpc.Attributes) InternalXdsAttributes(io.grpc.xds.InternalXdsAttributes) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandler(io.netty.channel.ChannelHandler) ProtocolNegotiator(io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator) ClientSdsProtocolNegotiator(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsProtocolNegotiator) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 3 with ProtocolNegotiationEvent

use of io.grpc.netty.ProtocolNegotiationEvent in project grpc-java by grpc.

the class XdsClientWrapperForServerSdsTestMisc method getSslContextProviderSupplier.

private SslContextProviderSupplier getSslContextProviderSupplier(FilterChainSelector selector) throws Exception {
    final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
    ChannelHandler next = new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
            ProtocolNegotiationEvent e = (ProtocolNegotiationEvent) evt;
            sslSet.set(InternalProtocolNegotiationEvent.getAttributes(e).get(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER));
            ctx.pipeline().remove(this);
        }
    };
    ProtocolNegotiator mockDelegate = mock(ProtocolNegotiator.class);
    GrpcHttp2ConnectionHandler grpcHandler = FakeGrpcHttp2ConnectionHandler.newHandler();
    when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
    FilterChainSelectorManager manager = new FilterChainSelectorManager();
    manager.updateSelector(selector);
    FilterChainMatchingHandler filterChainMatchingHandler = new FilterChainMatchingHandler(grpcHandler, manager, mockDelegate);
    pipeline.addLast(filterChainMatchingHandler);
    ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
    pipeline.fireUserEventTriggered(event);
    channel.runPendingTasks();
    sslSet.set(InternalProtocolNegotiationEvent.getAttributes(event).get(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER));
    return sslSet.get();
}
Also used : ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) ProtocolNegotiator(io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator) FilterChainMatchingHandler(io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) GrpcHttp2ConnectionHandler(io.grpc.netty.GrpcHttp2ConnectionHandler) SslContextProviderSupplier(io.grpc.xds.internal.sds.SslContextProviderSupplier) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 4 with ProtocolNegotiationEvent

use of io.grpc.netty.ProtocolNegotiationEvent in project grpc-java by grpc.

the class TsiHandshakeHandler method fireProtocolNegotiationEvent.

private void fireProtocolNegotiationEvent(ChannelHandlerContext ctx, TsiPeer peer, Object authContext, SecurityDetails details) {
    checkState(pne != null, "negotiation not yet complete");
    negotiationLogger.log(ChannelLogLevel.INFO, "TsiHandshake finished");
    ProtocolNegotiationEvent localPne = pne;
    Attributes.Builder attrs = InternalProtocolNegotiationEvent.getAttributes(localPne).toBuilder().set(TSI_PEER_KEY, peer).set(AUTH_CONTEXT_KEY, authContext).set(GrpcAttributes.ATTR_SECURITY_LEVEL, details.getSecurityLevel());
    localPne = InternalProtocolNegotiationEvent.withAttributes(localPne, attrs.build());
    localPne = InternalProtocolNegotiationEvent.withSecurity(localPne, details.getSecurity());
    ctx.fireUserEventTriggered(localPne);
}
Also used : InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) Attributes(io.grpc.Attributes) GrpcAttributes(io.grpc.internal.GrpcAttributes)

Example 5 with ProtocolNegotiationEvent

use of io.grpc.netty.ProtocolNegotiationEvent in project grpc-java by grpc.

the class TsiHandshakeHandler method userEventTriggered.

@Override
public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof ProtocolNegotiationEvent) {
        checkState(pne == null, "negotiation already started");
        pne = (ProtocolNegotiationEvent) evt;
        negotiationLogger.log(ChannelLogLevel.INFO, "TsiHandshake started");
        ChannelFuture acquire = semaphoreAcquire(ctx);
        if (acquire.isSuccess()) {
            semaphoreAcquired = true;
            sendHandshake(ctx);
        } else {
            acquire.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    if (!future.isSuccess()) {
                        ctx.fireExceptionCaught(future.cause());
                        return;
                    }
                    if (ctx.isRemoved()) {
                        semaphoreRelease();
                        return;
                    }
                    semaphoreAcquired = true;
                    try {
                        sendHandshake(ctx);
                    } catch (Exception ex) {
                        ctx.fireExceptionCaught(ex);
                    }
                    ctx.flush();
                }
            });
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) GeneralSecurityException(java.security.GeneralSecurityException)

Aggregations

InternalProtocolNegotiationEvent (io.grpc.netty.InternalProtocolNegotiationEvent)7 ProtocolNegotiationEvent (io.grpc.netty.ProtocolNegotiationEvent)7 Attributes (io.grpc.Attributes)3 ChannelHandler (io.netty.channel.ChannelHandler)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 Test (org.junit.Test)3 ProtocolNegotiator (io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator)2 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)2 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)2 InternalXdsAttributes (io.grpc.xds.InternalXdsAttributes)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 FakeClock (io.grpc.internal.FakeClock)1 GrpcAttributes (io.grpc.internal.GrpcAttributes)1 GrpcHttp2ConnectionHandler (io.grpc.netty.GrpcHttp2ConnectionHandler)1 Bootstrapper (io.grpc.xds.Bootstrapper)1 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)1 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)1