Java Code Examples for io.grpc.ManagedChannelBuilder#usePlaintext()

The following examples show how to use io.grpc.ManagedChannelBuilder#usePlaintext() . 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: OtlpGrpcSpanExporter.java    From opentelemetry-java with Apache License 2.0 7 votes vote down vote up
/**
 * Constructs a new instance of the exporter based on the builder's values.
 *
 * @return a new exporter's instance
 */
public OtlpGrpcSpanExporter build() {
  if (endpoint != null) {
    final ManagedChannelBuilder<?> managedChannelBuilder =
        ManagedChannelBuilder.forTarget(endpoint);

    if (useTls) {
      managedChannelBuilder.useTransportSecurity();
    } else {
      managedChannelBuilder.usePlaintext();
    }

    if (metadata != null) {
      managedChannelBuilder.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
    }

    channel = managedChannelBuilder.build();
  }
  return new OtlpGrpcSpanExporter(channel, deadlineMs);
}
 
Example 2
Source File: TesterOkHttpChannelBuilder.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
public static ManagedChannel build(
    String host,
    int port,
    @Nullable String serverHostOverride,
    boolean useTls,
    @Nullable InputStream testCa) {
  ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
      .maxInboundMessageSize(16 * 1024 * 1024);
  if (serverHostOverride != null) {
    // Force the hostname to match the cert the server uses.
    channelBuilder.overrideAuthority(serverHostOverride);
  }
  if (useTls) {
    try {
      ((OkHttpChannelBuilder) channelBuilder).useTransportSecurity();
      ((OkHttpChannelBuilder) channelBuilder).sslSocketFactory(getSslSocketFactory(testCa));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  } else {
    channelBuilder.usePlaintext();
  }
  return channelBuilder.build();
}
 
Example 3
Source File: GrpcServerChannel.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a managed server channel.
 * @param server The server
 * @param executorService The executor service
 * @param clientInterceptors The client interceptors
 * @return The channel
 */
@Singleton
@Named(NAME)
@Requires(beans = GrpcEmbeddedServer.class)
@Bean(preDestroy = "shutdown")
protected ManagedChannel serverChannel(
        GrpcEmbeddedServer server,
        @javax.inject.Named(TaskExecutors.IO) ExecutorService executorService,
        List<ClientInterceptor> clientInterceptors) {
    final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(
            server.getHost(),
            server.getPort()
    ).executor(executorService);
    if (!server.getServerConfiguration().isSecure()) {
        builder.usePlaintext();
    }
    if (CollectionUtils.isNotEmpty(clientInterceptors)) {
        builder.intercept(clientInterceptors);
    }
    return builder.build();
}
 
Example 4
Source File: GrpcServerChannel.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a managed server channel.
 * @param server The server
 * @param executorService The executor service
 * @param clientInterceptors The client interceptors
 * @return The channel
 */
@Singleton
@Named(NAME)
@Requires(beans = GrpcEmbeddedServer.class)
@Bean(preDestroy = "shutdown")
protected ManagedChannel serverChannel(
        GrpcEmbeddedServer server,
        @javax.inject.Named(TaskExecutors.IO) ExecutorService executorService,
        List<ClientInterceptor> clientInterceptors) {
    final ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(
            server.getHost(),
            server.getPort()
    ).executor(executorService);
    if (!server.getServerConfiguration().isSecure()) {
        builder.usePlaintext();
    }
    if (CollectionUtils.isNotEmpty(clientInterceptors)) {
        builder.intercept(clientInterceptors);
    }
    return builder.build();
}
 
Example 5
Source File: GrpcServerTestBase.java    From grpc-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Before
public final void setupChannels() throws IOException {
    if(gRpcServerProperties.isEnabled()) {
        ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress("localhost", getPort());
        Resource certChain = Optional.ofNullable(gRpcServerProperties.getSecurity())
                .map(GRpcServerProperties.SecurityProperties::getCertChain)
                .orElse(null);
        if(null!= certChain){
            ((NettyChannelBuilder)channelBuilder)
                    .useTransportSecurity()
                    .sslContext(GrpcSslContexts.forClient().trustManager(certChain.getInputStream()).build());
        }else{
            channelBuilder.usePlaintext();
        }


        channel = onChannelBuild(channelBuilder).build();
    }
    if(StringUtils.hasText(gRpcServerProperties.getInProcessServerName())){
        inProcChannel = onChannelBuild(
                            InProcessChannelBuilder.forName(gRpcServerProperties.getInProcessServerName())
                            .usePlaintext()
                        ).build();

    }
}
 
Example 6
Source File: DiscoveryClientChannelFactory.java    From spring-boot-starter-grpc with Apache License 2.0 6 votes vote down vote up
@Override
public Channel createChannel(String name) {
  RoundRobinLoadBalancerFactory instance = RoundRobinLoadBalancerFactory.getInstance();
  ManagedChannelBuilder builder = ManagedChannelBuilder.forTarget("spring://" + name)
          .nameResolverFactory(new DiscoveryClientResolverFactory(client))
	.loadBalancerFactory(instance);

  if (channels.getChannels().containsKey(name)) {
    if (channels.getChannels().get(name).isPlaintext()) {
       builder.usePlaintext();
    }
  } else {
    builder.usePlaintext();
  }

  return builder.build();
}
 
Example 7
Source File: TesterOkHttpChannelBuilder.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public static ManagedChannel build(
    String host,
    int port,
    @Nullable String serverHostOverride,
    boolean useTls,
    @Nullable InputStream testCa) {
  ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
      .maxInboundMessageSize(16 * 1024 * 1024);
  if (serverHostOverride != null) {
    // Force the hostname to match the cert the server uses.
    channelBuilder.overrideAuthority(serverHostOverride);
  }
  if (useTls) {
    try {
      ((OkHttpChannelBuilder) channelBuilder).useTransportSecurity();
      ((OkHttpChannelBuilder) channelBuilder).sslSocketFactory(getSslSocketFactory(testCa));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  } else {
    channelBuilder.usePlaintext();
  }
  return channelBuilder.build();
}
 
Example 8
Source File: GrpcCallProvider.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Sets up the SSL provider and configures the gRPC channel. */
private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) {
  try {
    // We need to upgrade the Security Provider before any network channels are initialized.
    // `OkHttp` maintains a list of supported providers that is initialized when the JVM first
    // resolves the static dependencies of ManagedChannel.
    ProviderInstaller.installIfNeeded(context);
  } catch (GooglePlayServicesNotAvailableException /* Thrown by ProviderInstaller */
      | GooglePlayServicesRepairableException /* Thrown by ProviderInstaller */
      | IllegalStateException e /* Thrown by Robolectric */) {
    // Mark the SSL initialization as done, even though we may be using outdated SSL
    // ciphers. gRPC-Java recommends obtaining updated ciphers from GMSCore, but we allow
    // the device to fall back to other SSL ciphers if GMSCore is not available.
    Logger.warn(LOG_TAG, "Failed to update ssl context: %s", e);
  }

  ManagedChannelBuilder<?> channelBuilder;
  if (overrideChannelBuilderSupplier != null) {
    channelBuilder = overrideChannelBuilderSupplier.get();
  } else {
    channelBuilder = ManagedChannelBuilder.forTarget(databaseInfo.getHost());
    if (!databaseInfo.isSslEnabled()) {
      // Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
      // It merely turns off SSL encryption.
      channelBuilder.usePlaintext();
    }
  }

  // Ensure gRPC recovers from a dead connection. (Not typically necessary, as the OS will
  // usually notify gRPC when a connection dies. But not always. This acts as a failsafe.)
  channelBuilder.keepAliveTime(30, TimeUnit.SECONDS);

  // Wrap the ManagedChannelBuilder in an AndroidChannelBuilder. This allows the channel to
  // respond more gracefully to network change events (such as switching from cell to wifi).
  AndroidChannelBuilder androidChannelBuilder =
      AndroidChannelBuilder.usingBuilder(channelBuilder).context(context);

  return androidChannelBuilder.build();
}
 
Example 9
Source File: GRPCChannelProvider.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
public ManagedChannel channel() {
    ManagedChannelBuilder channelBuilder = ManagedChannelBuilder
            .forAddress(host, port);
    if (!tls)
        channelBuilder.usePlaintext();
    return channelBuilder.build();
}
 
Example 10
Source File: ITTracingClientInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected ManagedChannelBuilder<?> usePlainText(ManagedChannelBuilder<?> builder) {
  return builder.usePlaintext(true);
}
 
Example 11
Source File: ITTracingServerInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected ManagedChannelBuilder<?> usePlainText(ManagedChannelBuilder<?> builder) {
  return builder.usePlaintext(true);
}
 
Example 12
Source File: ITTracingClientInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected ManagedChannelBuilder<?> usePlainText(ManagedChannelBuilder<?> builder) {
  return builder.usePlaintext();
}
 
Example 13
Source File: ITTracingServerInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected ManagedChannelBuilder<?> usePlainText(ManagedChannelBuilder<?> builder) {
  return builder.usePlaintext();
}