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();
}
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();
}
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();
}
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);
}
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);
}
}
Aggregations