io.grpc.CompressorRegistry Java Examples

The following examples show how to use io.grpc.CompressorRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void serverSendsOne_okFailsOnMissingResponse(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.close(Status.OK, new Metadata());
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.MISSING_RESPONSE, statusCaptor.getValue().getDescription());
}
 
Example #2
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, never()).close(any(Status.class), any(Metadata.class));

  // trying to send a second message causes gRPC to close the underlying stream
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.TOO_MANY_RESPONSES, statusCaptor.getValue().getDescription());
}
 
Example #3
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, never()).close(any(Status.class), any(Metadata.class));

  // trying to send a second message causes gRPC to close the underlying stream
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.TOO_MANY_RESPONSES, statusCaptor.getValue().getDescription());
}
 
Example #4
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall_appRunToCompletion(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, times(1)).cancel(any(Status.class));

  // App runs to completion but everything is ignored
  serverCall.sendMessage(1L);
  serverCall.close(Status.OK, new Metadata());
  try {
    serverCall.close(Status.OK, new Metadata());
    fail("calling a second time should still cause an error");
  } catch (IllegalStateException expected) {
    // noop
  }
}
 
Example #5
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void serverSendsOne_okFailsOnMissingResponse(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.close(Status.OK, new Metadata());
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.MISSING_RESPONSE, statusCaptor.getValue().getDescription());
}
 
Example #6
Source File: ArmeriaServerCallTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void messageRead_wrappedByteBuf() {
    tearDown();

    call = new ArmeriaServerCall<>(
            HttpHeaders.of(),
            TestServiceGrpc.getUnaryCallMethod(),
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            res,
            MAX_MESSAGE_BYTES,
            MAX_MESSAGE_BYTES,
            ctx,
            GrpcSerializationFormats.PROTO,
            new DefaultJsonMarshaller(MessageMarshaller.builder().build()),
            true,
            false,
            ResponseHeaders.builder(HttpStatus.OK)
                           .contentType(GrpcSerializationFormats.PROTO.mediaType())
                           .build());

    final ByteBuf buf = GrpcTestUtil.requestByteBuf();
    call.messageRead(new DeframedMessage(buf, 0));

    verify(buffersAttr).put(any(), same(buf));
}
 
Example #7
Source File: GrpcServiceBuilder.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new {@link GrpcService} that can be bound to
 * {@link ServerBuilder}. It is recommended to bind the service to a server using
 * {@linkplain ServerBuilder#service(HttpServiceWithRoutes, Function[])
 * ServerBuilder.service(HttpServiceWithRoutes)} to mount all service paths
 * without interfering with other services.
 */
public GrpcService build() {
    final HandlerRegistry handlerRegistry = registryBuilder.build();

    final GrpcService grpcService = new FramedGrpcService(
            handlerRegistry,
            handlerRegistry
                    .methods()
                    .keySet()
                    .stream()
                    .map(path -> Route.builder().exact('/' + path).build())
                    .collect(ImmutableSet.toImmutableSet()),
            firstNonNull(decompressorRegistry, DecompressorRegistry.getDefaultInstance()),
            firstNonNull(compressorRegistry, CompressorRegistry.getDefaultInstance()),
            supportedSerializationFormats,
            jsonMarshallerFactory,
            protoReflectionServiceInterceptor,
            maxOutboundMessageSizeBytes,
            useBlockingTaskExecutor,
            unsafeWrapRequestBuffers,
            useClientTimeoutHeader,
            maxInboundMessageSizeBytes);
    return enableUnframedRequests ? new UnframedGrpcService(grpcService) : grpcService;
}
 
Example #8
Source File: AbstractManagedChannelImplBuilder.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public final T compressorRegistry(CompressorRegistry registry) {
  if (registry != null) {
    this.compressorRegistry = registry;
  } else {
    this.compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
  }
  return thisT();
}
 
Example #9
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void compressorRegistry_null() {
  CompressorRegistry defaultValue = builder.compressorRegistry;
  builder.compressorRegistry(CompressorRegistry.newEmptyInstance());
  assertNotEquals(defaultValue, builder.compressorRegistry);
  assertEquals(builder, builder.compressorRegistry(null));
  assertEquals(defaultValue, builder.compressorRegistry);
}
 
Example #10
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void compressorRegistry_normal() {
  CompressorRegistry compressorRegistry = CompressorRegistry.newEmptyInstance();
  assertNotEquals(compressorRegistry, builder.compressorRegistry);
  assertEquals(builder, builder.compressorRegistry(compressorRegistry));
  assertEquals(compressorRegistry, builder.compressorRegistry);
}
 
Example #11
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall_appRunToCompletion(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, times(1)).cancel(any(Status.class));

  // App runs to completion but everything is ignored
  serverCall.sendMessage(1L);
  serverCall.close(Status.OK, new Metadata());
  try {
    serverCall.close(Status.OK, new Metadata());
    fail("calling a second time should still cause an error");
  } catch (IllegalStateException expected) {
    // noop
  }
}
 
Example #12
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void callTracer0(Status status) {
  CallTracer tracer = CallTracer.getDefaultFactory().create();
  ServerStats.Builder beforeBuilder = new ServerStats.Builder();
  tracer.updateBuilder(beforeBuilder);
  ServerStats before = beforeBuilder.build();
  assertEquals(0, before.callsStarted);
  assertEquals(0, before.lastCallStartedNanos);

  call = new ServerCallImpl<>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      tracer, PerfMark.createTag());

  // required boilerplate
  call.sendHeaders(new Metadata());
  call.sendMessage(123L);
  // end: required boilerplate

  call.close(status, new Metadata());
  ServerStats.Builder afterBuilder = new ServerStats.Builder();
  tracer.updateBuilder(afterBuilder);
  ServerStats after = afterBuilder.build();
  assertEquals(1, after.callsStarted);
  if (status.isOk()) {
    assertEquals(1, after.callsSucceeded);
  } else {
    assertEquals(1, after.callsFailed);
  }
}
 
Example #13
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  context = Context.ROOT.withCancellation();
  call = new ServerCallImpl<>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      serverCallTracer, PerfMark.createTag());
}
 
Example #14
Source File: ServerCallImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
ServerCallImpl(ServerStream stream, MethodDescriptor<ReqT, RespT> method,
    Metadata inboundHeaders, Context.CancellableContext context,
    DecompressorRegistry decompressorRegistry, CompressorRegistry compressorRegistry,
    CallTracer serverCallTracer, Tag tag) {
  this.stream = stream;
  this.method = method;
  this.context = context;
  this.messageAcceptEncoding = inboundHeaders.get(MESSAGE_ACCEPT_ENCODING_KEY);
  this.decompressorRegistry = decompressorRegistry;
  this.compressorRegistry = compressorRegistry;
  this.serverCallTracer = serverCallTracer;
  this.serverCallTracer.reportCallStarted();
  this.tag = tag;
}
 
Example #15
Source File: AbstractManagedChannelImplBuilder.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public final T compressorRegistry(CompressorRegistry registry) {
  if (registry != null) {
    this.compressorRegistry = registry;
  } else {
    this.compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
  }
  return thisT();
}
 
Example #16
Source File: ArmeriaServerCallTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    completionFuture = new CompletableFuture<>();
    when(res.whenComplete()).thenReturn(completionFuture);

    ctx = ServiceRequestContext.builder(HttpRequest.of(HttpMethod.POST, "/"))
                               .eventLoop(eventLoop.get())
                               .build();

    call = new ArmeriaServerCall<>(
            HttpHeaders.of(),
            TestServiceGrpc.getUnaryCallMethod(),
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            res,
            MAX_MESSAGE_BYTES,
            MAX_MESSAGE_BYTES,
            ctx,
            GrpcSerializationFormats.PROTO,
            new DefaultJsonMarshaller(MessageMarshaller.builder().build()),
            false,
            false,
            ResponseHeaders.builder(HttpStatus.OK)
                           .contentType(GrpcSerializationFormats.PROTO.mediaType())
                           .build());
    call.setListener(listener);
    call.messageReader().onSubscribe(subscription);

    ctx.setAttr(GrpcUnsafeBufferUtil.BUFFERS, buffersAttr);
}
 
Example #17
Source File: GrpcCommonCodecAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@ConditionalOnBean(GrpcCodecDiscoverer.class)
@ConditionalOnMissingBean
@Bean
public CompressorRegistry defaultCompressorRegistry(final GrpcCodecDiscoverer codecDiscoverer) {
    log.debug("Found GrpcCodecDiscoverer -> Creating custom CompressorRegistry");
    final CompressorRegistry registry = CompressorRegistry.getDefaultInstance();
    for (final GrpcCodecDefinition definition : codecDiscoverer.findGrpcCodecs()) {
        if (definition.getCodecType().isForCompression()) {
            final Codec codec = definition.getCodec();
            log.debug("Registering compressor: '{}' ({})", codec.getMessageEncoding(), codec.getClass().getName());
            registry.register(codec);
        }
    }
    return registry;
}
 
Example #18
Source File: ServerCallImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
ServerCallImpl(ServerStream stream, MethodDescriptor<ReqT, RespT> method,
    Metadata inboundHeaders, Context.CancellableContext context,
    DecompressorRegistry decompressorRegistry, CompressorRegistry compressorRegistry,
    CallTracer serverCallTracer) {
  this.stream = stream;
  this.method = method;
  this.context = context;
  this.messageAcceptEncoding = inboundHeaders.get(MESSAGE_ACCEPT_ENCODING_KEY);
  this.decompressorRegistry = decompressorRegistry;
  this.compressorRegistry = compressorRegistry;
  this.serverCallTracer = serverCallTracer;
  this.serverCallTracer.reportCallStarted();
}
 
Example #19
Source File: AbstractServerImplBuilder.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public final T compressorRegistry(CompressorRegistry registry) {
  if (registry != null) {
    compressorRegistry = registry;
  } else {
    compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
  }
  return thisT();
}
 
Example #20
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  context = Context.ROOT.withCancellation();
  call = new ServerCallImpl<Long, Long>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
}
 
Example #21
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void callTracer0(Status status) {
  CallTracer tracer = CallTracer.getDefaultFactory().create();
  ServerStats.Builder beforeBuilder = new ServerStats.Builder();
  tracer.updateBuilder(beforeBuilder);
  ServerStats before = beforeBuilder.build();
  assertEquals(0, before.callsStarted);
  assertEquals(0, before.lastCallStartedNanos);

  call = new ServerCallImpl<Long, Long>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      tracer);

  // required boilerplate
  call.sendHeaders(new Metadata());
  call.sendMessage(123L);
  // end: required boilerplate

  call.close(status, new Metadata());
  ServerStats.Builder afterBuilder = new ServerStats.Builder();
  tracer.updateBuilder(afterBuilder);
  ServerStats after = afterBuilder.build();
  assertEquals(1, after.callsStarted);
  if (status.isOk()) {
    assertEquals(1, after.callsSucceeded);
  } else {
    assertEquals(1, after.callsFailed);
  }
}
 
Example #22
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void compressorRegistry_normal() {
  CompressorRegistry compressorRegistry = CompressorRegistry.newEmptyInstance();
  assertNotEquals(compressorRegistry, builder.compressorRegistry);
  assertEquals(builder, builder.compressorRegistry(compressorRegistry));
  assertEquals(compressorRegistry, builder.compressorRegistry);
}
 
Example #23
Source File: GrpcCommonCodecAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@ConditionalOnBean(GrpcCodecDiscoverer.class)
@ConditionalOnMissingBean
@Bean
public CompressorRegistry defaultCompressorRegistry(final GrpcCodecDiscoverer codecDiscoverer) {
    log.debug("Found GrpcCodecDiscoverer -> Creating custom CompressorRegistry");
    final CompressorRegistry registry = CompressorRegistry.getDefaultInstance();
    for (final GrpcCodecDefinition definition : codecDiscoverer.findGrpcCodecs()) {
        if (definition.getCodecType().isForCompression()) {
            final Codec codec = definition.getCodec();
            log.debug("Registering compressor: '{}' ({})", codec.getMessageEncoding(), codec.getClass().getName());
            registry.register(codec);
        }
    }
    return registry;
}
 
Example #24
Source File: AbstractManagedChannelImplBuilderTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void compressorRegistry_null() {
  CompressorRegistry defaultValue = builder.compressorRegistry;
  builder.compressorRegistry(CompressorRegistry.newEmptyInstance());
  assertNotEquals(defaultValue, builder.compressorRegistry);
  assertEquals(builder, builder.compressorRegistry(null));
  assertEquals(defaultValue, builder.compressorRegistry);
}
 
Example #25
Source File: TestGRPCClient.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Build a channel with the given host and port and optional ssl properties.
 *
 * @param host          the host to establish a connection with
 * @param port          the port on which to communicate with the host
 * @param sslProperties the properties by which to establish an ssl connection
 * @return a constructed channel
 */
public static ManagedChannel buildChannel(final String host, final int port, final Map<String, String> sslProperties)
        throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
    NettyChannelBuilder channelBuilder = NettyChannelBuilder.forAddress(host, port)
            .directExecutor()
            .compressorRegistry(CompressorRegistry.getDefaultInstance())
            .decompressorRegistry(DecompressorRegistry.getDefaultInstance())
            .userAgent("testAgent");

    if (sslProperties != null) {
        SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();

        if(sslProperties.get(StandardSSLContextService.KEYSTORE.getName()) != null) {
            final KeyManagerFactory keyManager = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            final KeyStore keyStore = KeyStore.getInstance(sslProperties.get(StandardSSLContextService.KEYSTORE_TYPE.getName()));
            final String keyStoreFile = sslProperties.get(StandardSSLContextService.KEYSTORE.getName());
            final String keyStorePassword = sslProperties.get(StandardSSLContextService.KEYSTORE_PASSWORD.getName());
            try (final InputStream is = new FileInputStream(keyStoreFile)) {
                keyStore.load(is, keyStorePassword.toCharArray());
            }
            keyManager.init(keyStore, keyStorePassword.toCharArray());
            sslContextBuilder = sslContextBuilder.keyManager(keyManager);
        }

        if (sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName()) != null) {
            final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            final KeyStore trustStore = KeyStore.getInstance(sslProperties.get(StandardSSLContextService.TRUSTSTORE_TYPE.getName()));
            final String trustStoreFile = sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName());
            final String trustStorePassword = sslProperties.get(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName());
            try (final InputStream is = new FileInputStream(trustStoreFile)) {
                trustStore.load(is, trustStorePassword.toCharArray());
            }
            trustManagerFactory.init(trustStore);
            sslContextBuilder = sslContextBuilder.trustManager(trustManagerFactory);
        }

        final String clientAuth = sslProperties.get(NEED_CLIENT_AUTH);
        if (clientAuth == null) {
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
        } else {
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.valueOf(clientAuth));
        }
        sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder);
        channelBuilder = channelBuilder.sslContext(sslContextBuilder.build());
    } else {
        channelBuilder.usePlaintext(true);
    }
    return channelBuilder.build();
}
 
Example #26
Source File: GrpcServerAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
@ConditionalOnBean(CompressorRegistry.class)
@Bean
public GrpcServerConfigurer compressionServerConfigurer(final CompressorRegistry registry) {
    return builder -> builder.compressorRegistry(registry);
}
 
Example #27
Source File: TestGRPCServer.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Starts the gRPC server @localhost:port.
 */
public int start(final int port) throws Exception {
    final NettyServerBuilder nettyServerBuilder = NettyServerBuilder
            .forPort(port)
            .directExecutor()
            .addService(clazz.newInstance())
            .compressorRegistry(CompressorRegistry.getDefaultInstance())
            .decompressorRegistry(DecompressorRegistry.getDefaultInstance());

    if (this.sslProperties != null) {
        if (sslProperties.get(StandardSSLContextService.KEYSTORE.getName()) == null) {
            throw new RuntimeException("You must configure a keystore in order to use SSL with gRPC.");
        }

        final KeyManagerFactory keyManager = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        final KeyStore keyStore = KeyStore.getInstance(sslProperties.get(StandardSSLContextService.KEYSTORE_TYPE.getName()));
        final String keyStoreFile = sslProperties.get(StandardSSLContextService.KEYSTORE.getName());
        final String keyStorePassword = sslProperties.get(StandardSSLContextService.KEYSTORE_PASSWORD.getName());
        try (final InputStream is = new FileInputStream(keyStoreFile)) {
            keyStore.load(is, keyStorePassword.toCharArray());
        }
        keyManager.init(keyStore, keyStorePassword.toCharArray());
        SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(keyManager);

        if (sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName()) != null) {
            final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            final KeyStore trustStore = KeyStore.getInstance(sslProperties.get(StandardSSLContextService.TRUSTSTORE_TYPE.getName()));
            final String trustStoreFile = sslProperties.get(StandardSSLContextService.TRUSTSTORE.getName());
            final String trustStorePassword = sslProperties.get(StandardSSLContextService.TRUSTSTORE_PASSWORD.getName());
            try (final InputStream is = new FileInputStream(trustStoreFile)) {
                trustStore.load(is, trustStorePassword.toCharArray());
            }
            trustManagerFactory.init(trustStore);
            sslContextBuilder = sslContextBuilder.trustManager(trustManagerFactory);
        }

        final String clientAuth = sslProperties.get(NEED_CLIENT_AUTH);
        if (clientAuth == null) {
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
        } else {
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.valueOf(clientAuth));
        }
        sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder);
        nettyServerBuilder.sslContext(sslContextBuilder.build());
    }

    server = nettyServerBuilder.build().start();
    final int actualPort = server.getPort();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            // Use stderr here since the logger may have been reset by its JVM shutdown hook.
            System.err.println("*** shutting down gRPC server since JVM is shutting down");
            TestGRPCServer.this.stop();
            System.err.println("*** server shut down");
        }
    });
    return actualPort;
}
 
Example #28
Source File: InvokeGRPC.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Whenever this processor is triggered, we need to construct a client in order to communicate
 * with the configured gRPC service.
 *
 * @param context the processor context
 */
@OnScheduled
public void initializeClient(final ProcessContext context) throws Exception {

    channelReference.set(null);
    blockingStubReference.set(null);
    final ComponentLog logger = getLogger();

    final String host = context.getProperty(PROP_SERVICE_HOST).getValue();
    final int port = context.getProperty(PROP_SERVICE_PORT).asInteger();
    final Integer maxMessageSize = context.getProperty(PROP_MAX_MESSAGE_SIZE).asDataSize(DataUnit.B).intValue();
    String userAgent = USER_AGENT_PREFIX;
    try {
        userAgent += "_" + InetAddress.getLocalHost().getHostName();
    } catch (final UnknownHostException e) {
        logger.warn("Unable to determine local hostname. Defaulting gRPC user agent to {}.", new Object[]{USER_AGENT_PREFIX}, e);
    }

    final NettyChannelBuilder nettyChannelBuilder = NettyChannelBuilder.forAddress(host, port)
            // supports both gzip and plaintext, but will compress by default.
            .compressorRegistry(CompressorRegistry.getDefaultInstance())
            .decompressorRegistry(DecompressorRegistry.getDefaultInstance())
            .maxInboundMessageSize(maxMessageSize)
            .userAgent(userAgent);

    // configure whether or not we're using secure comms
    final boolean useSecure = context.getProperty(PROP_USE_SECURE).asBoolean();
    final SSLContextService sslContextService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslContextService == null ? null : sslContextService.createSSLContext(SslContextFactory.ClientAuth.NONE);

    if (useSecure && sslContext != null) {
        SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
        if(StringUtils.isNotBlank(sslContextService.getKeyStoreFile())) {
            final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm(),
                    sslContext.getProvider());
            final KeyStore keyStore = KeyStore.getInstance(sslContextService.getKeyStoreType());
            try (final InputStream is = new FileInputStream(sslContextService.getKeyStoreFile())) {
                keyStore.load(is, sslContextService.getKeyStorePassword().toCharArray());
            }
            keyManagerFactory.init(keyStore, sslContextService.getKeyStorePassword().toCharArray());
            sslContextBuilder.keyManager(keyManagerFactory);
        }

        if(StringUtils.isNotBlank(sslContextService.getTrustStoreFile())) {
            final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm(),
                    sslContext.getProvider());
            final KeyStore trustStore = KeyStore.getInstance(sslContextService.getTrustStoreType());
            try (final InputStream is = new FileInputStream(sslContextService.getTrustStoreFile())) {
                trustStore.load(is, sslContextService.getTrustStorePassword().toCharArray());
            }
            trustManagerFactory.init(trustStore);
            sslContextBuilder.trustManager(trustManagerFactory);
        }
        nettyChannelBuilder.sslContext(sslContextBuilder.build());

    } else {
        nettyChannelBuilder.usePlaintext(true);
    }

    final ManagedChannel channel = nettyChannelBuilder.build();
    final FlowFileServiceGrpc.FlowFileServiceBlockingStub blockingStub = FlowFileServiceGrpc.newBlockingStub(channel);
    channelReference.set(channel);
    blockingStubReference.set(blockingStub);
}
 
Example #29
Source File: ListenGRPC.java    From nifi with Apache License 2.0 4 votes vote down vote up
@OnScheduled
public void startServer(final ProcessContext context) throws NoSuchAlgorithmException, IOException, KeyStoreException, CertificateException, UnrecoverableKeyException {
    final ComponentLog logger = getLogger();
    // gather configured properties
    final Integer port = context.getProperty(PROP_SERVICE_PORT).asInteger();
    final Boolean useSecure = context.getProperty(PROP_USE_SECURE).asBoolean();
    final Integer flowControlWindow = context.getProperty(PROP_FLOW_CONTROL_WINDOW).asDataSize(DataUnit.B).intValue();
    final Integer maxMessageSize = context.getProperty(PROP_MAX_MESSAGE_SIZE).asDataSize(DataUnit.B).intValue();
    final SSLContextService sslContextService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslContextService == null ? null : sslContextService.createSSLContext(SslContextFactory.ClientAuth.NONE);
    final Pattern authorizedDnPattern = Pattern.compile(context.getProperty(PROP_AUTHORIZED_DN_PATTERN).getValue());
    final FlowFileIngestServiceInterceptor callInterceptor = new FlowFileIngestServiceInterceptor(getLogger());
    callInterceptor.enforceDNPattern(authorizedDnPattern);

    final FlowFileIngestService flowFileIngestService = new FlowFileIngestService(getLogger(),
            sessionFactoryReference,
            context);
    NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port)
            .addService(ServerInterceptors.intercept(flowFileIngestService, callInterceptor))
            // default (de)compressor registries handle both plaintext and gzip compressed messages
            .compressorRegistry(CompressorRegistry.getDefaultInstance())
            .decompressorRegistry(DecompressorRegistry.getDefaultInstance())
            .flowControlWindow(flowControlWindow)
            .maxMessageSize(maxMessageSize);

    if (useSecure && sslContext != null) {
        // construct key manager
        if (StringUtils.isBlank(sslContextService.getKeyStoreFile())) {
            throw new IllegalStateException("SSL is enabled, but no keystore has been configured. You must configure a keystore.");
        }

        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm(),
                sslContext.getProvider());
        final KeyStore keyStore = KeyStore.getInstance(sslContextService.getKeyStoreType());
        try (final InputStream is = new FileInputStream(sslContextService.getKeyStoreFile())) {
            keyStore.load(is, sslContextService.getKeyStorePassword().toCharArray());
        }
        keyManagerFactory.init(keyStore, sslContextService.getKeyStorePassword().toCharArray());

        SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(keyManagerFactory);

        // if the trust store is configured, then client auth is required.
        if (StringUtils.isNotBlank(sslContextService.getTrustStoreFile())) {
            final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm(),
                    sslContext.getProvider());
            final KeyStore trustStore = KeyStore.getInstance(sslContextService.getTrustStoreType());
            try (final InputStream is = new FileInputStream(sslContextService.getTrustStoreFile())) {
                trustStore.load(is, sslContextService.getTrustStorePassword().toCharArray());
            }
            trustManagerFactory.init(trustStore);
            sslContextBuilder = sslContextBuilder.trustManager(trustManagerFactory);
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
        } else {
            sslContextBuilder = sslContextBuilder.clientAuth(ClientAuth.NONE);
        }
        sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder);
        serverBuilder = serverBuilder.sslContext(sslContextBuilder.build());
    }
    logger.info("Starting gRPC server on port: {}", new Object[]{port.toString()});
    this.server = serverBuilder.build().start();
}
 
Example #30
Source File: AltsServerBuilder.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public AltsServerBuilder compressorRegistry(CompressorRegistry registry) {
  delegate.compressorRegistry(registry);
  return this;
}