Java Code Examples for io.grpc.ServerBuilder#useTransportSecurity()

The following examples show how to use io.grpc.ServerBuilder#useTransportSecurity() . 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: PubsubEmulatorServer.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Inject
public PubsubEmulatorServer(
    ConfigurationManager configurationManager,
    PublisherService publisher,
    SubscriberService subscriber,
    AdminService admin,
    HealthStatusManager healthStatusManager) {
  this.publisher = publisher;
  this.subscriber = subscriber;
  this.healthStatusManager = healthStatusManager;

  ServerBuilder builder =
      ServerBuilder.forPort(configurationManager.getServer().getPort())
          .addService(publisher)
          .addService(subscriber)
          .addService(admin)
          .addService(healthStatusManager.getHealthService())
          .maxInboundMessageSize(MAX_MESSAGE_SIZE);
  if (configurationManager.getServer().hasSecurity()) {
    builder.useTransportSecurity(
        new File(configurationManager.getServer().getSecurity().getCertificateChainFile()),
        new File(configurationManager.getServer().getSecurity().getPrivateKeyFile()));
  }
  server = builder.build();
}
 
Example 2
Source File: GRPCServer.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Create gRPC server on the specified port.
 *
 * @param controllerService The controller service implementation.
 * @param serverConfig      The RPC Server config.
 * @param requestTracker    Cache to track and access to client request identifiers.
 */
public GRPCServer(ControllerService controllerService, GRPCServerConfig serverConfig, RequestTracker requestTracker) {
    this.objectId = "gRPCServer";
    this.config = serverConfig;
    GrpcAuthHelper authHelper = new GrpcAuthHelper(serverConfig.isAuthorizationEnabled(),
            serverConfig.getTokenSigningKey(), serverConfig.getAccessTokenTTLInSeconds());
    ServerBuilder<?> builder = ServerBuilder
            .forPort(serverConfig.getPort())
            .addService(ServerInterceptors.intercept(new ControllerServiceImpl(controllerService, authHelper, requestTracker,
                            serverConfig.isReplyWithStackTraceOnError()),
                    RPCTracingHelpers.getServerInterceptor(requestTracker)));
    if (serverConfig.isAuthorizationEnabled()) {
        this.authHandlerManager = new AuthHandlerManager(serverConfig);
        this.authHandlerManager.registerInterceptors(builder);
    } else {
        this.authHandlerManager = null;
    }

    if (serverConfig.isTlsEnabled() && !Strings.isNullOrEmpty(serverConfig.getTlsCertFile())) {
        builder = builder.useTransportSecurity(new File(serverConfig.getTlsCertFile()),
                new File(serverConfig.getTlsKeyFile()));
    }
    this.server = builder.build();
}
 
Example 3
Source File: GrpcServerFactory.java    From dropwizard-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * @param environment to use
 * @return A {@link ServerBuilder}, with port and optional transport security set from the configuration. To use
 *         this, add gRPC services to the server, then call build(). The returned server is lifecycle-managed in the
 *         given {@link Environment}.
 */
public ServerBuilder<?> builder(final Environment environment) {
    final ServerBuilder<?> originBuilder;
    final ServerBuilder<?> dropwizardBuilder;
    originBuilder = ServerBuilder.forPort(port);
    dropwizardBuilder = new DropwizardServerBuilder(environment, originBuilder, shutdownPeriod);
    if (certChainFile != null && privateKeyFile != null) {
        dropwizardBuilder.useTransportSecurity(certChainFile.toFile(), privateKeyFile.toFile());
    }
    return dropwizardBuilder;
}
 
Example 4
Source File: LoadServer.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
LoadServer(Control.ServerConfig config) throws Exception {
  log.log(Level.INFO, "Server Config \n" + config.toString());
  port = config.getPort() ==  0 ? Utils.pickUnusedPort() : config.getPort();
  ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port);
  int asyncThreads = config.getAsyncServerThreads() == 0
      ? Runtime.getRuntime().availableProcessors()
      : config.getAsyncServerThreads();
  // The concepts of sync & async server are quite different in the C impl and the names
  // chosen for the enum are based on that implementation. We use 'sync' to mean
  // the direct executor case in Java even though the service implementations are always
  // fully async.
  switch (config.getServerType()) {
    case ASYNC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      break;
    }
    case SYNC_SERVER: {
      serverBuilder.directExecutor();
      break;
    }
    case ASYNC_GENERIC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      // Create buffers for the generic service
      PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
      genericResponse = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize());
      if (genericResponse.capacity() > 0) {
        genericResponse.writerIndex(genericResponse.capacity() - 1);
      }
      break;
    }
    default: {
      throw new IllegalArgumentException();
    }
  }
  if (config.hasSecurityParams()) {
    File cert = TestUtils.loadCert("server1.pem");
    File key = TestUtils.loadCert("server1.key");
    serverBuilder.useTransportSecurity(cert, key);
  }
  benchmarkService = new AsyncServer.BenchmarkServiceImpl();
  if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) {
    serverBuilder.addService(
        ServerServiceDefinition
            .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME,
                GENERIC_STREAMING_PING_PONG_METHOD))
            .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler())
            .build());
  } else {
    serverBuilder.addService(benchmarkService);
  }
  server = serverBuilder.build();

  List<OperatingSystemMXBean> beans =
      ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class);
  if (!beans.isEmpty()) {
    osBean = beans.get(0);
  } else {
    osBean = null;
  }
}
 
Example 5
Source File: LoadServer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
LoadServer(Control.ServerConfig config) throws Exception {
  log.log(Level.INFO, "Server Config \n" + config.toString());
  port = config.getPort() ==  0 ? Utils.pickUnusedPort() : config.getPort();
  ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port);
  int asyncThreads = config.getAsyncServerThreads() == 0
      ? Runtime.getRuntime().availableProcessors()
      : config.getAsyncServerThreads();
  // The concepts of sync & async server are quite different in the C impl and the names
  // chosen for the enum are based on that implementation. We use 'sync' to mean
  // the direct executor case in Java even though the service implementations are always
  // fully async.
  switch (config.getServerType()) {
    case ASYNC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      break;
    }
    case SYNC_SERVER: {
      serverBuilder.directExecutor();
      break;
    }
    case ASYNC_GENERIC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      // Create buffers for the generic service
      PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
      genericResponse = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize());
      if (genericResponse.capacity() > 0) {
        genericResponse.writerIndex(genericResponse.capacity() - 1);
      }
      break;
    }
    default: {
      throw new IllegalArgumentException();
    }
  }
  if (config.hasSecurityParams()) {
    File cert = TestUtils.loadCert("server1.pem");
    File key = TestUtils.loadCert("server1.key");
    serverBuilder.useTransportSecurity(cert, key);
  }
  benchmarkService = new AsyncServer.BenchmarkServiceImpl();
  if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) {
    serverBuilder.addService(
        ServerServiceDefinition
            .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME,
                GENERIC_STREAMING_PING_PONG_METHOD))
            .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler())
            .build());
  } else {
    serverBuilder.addService(benchmarkService);
  }
  server = serverBuilder.build();

  List<OperatingSystemMXBean> beans =
      ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class);
  if (!beans.isEmpty()) {
    osBean = beans.get(0);
  } else {
    osBean = null;
  }
}