All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.dl.client.rpc.RpcUtil Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
package com.scalar.dl.client.rpc;

import com.scalar.dl.client.exception.ClientException;
import com.scalar.dl.ledger.service.StatusCode;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import javax.net.ssl.SSLException;

public class RpcUtil {

  public static void configureTls(
      NettyChannelBuilder builder, boolean tlsEnabled, Optional tlsCaRootCert) {
    if (tlsEnabled) {
      if (tlsCaRootCert.isPresent()) {
        // With a custom root CA certificate
        try {
          builder.sslContext(
              GrpcSslContexts.forClient()
                  .trustManager(
                      new ByteArrayInputStream(
                          tlsCaRootCert.get().getBytes(StandardCharsets.UTF_8)))
                  .build());
        } catch (SSLException e) {
          throw new ClientException("couldn't configure SSL.", e, StatusCode.RUNTIME_ERROR);
        }
      } else {
        // Use a certificate from the trusted CAs if it's not given
      }
    } else {
      builder.usePlaintext();
    }
  }

  public static void configureHeader(
      NettyChannelBuilder builder, Optional authorizationCredential) {
    authorizationCredential.ifPresent(
        credential -> {
          // add an authorization header
          builder.intercept(new AuthorizationInterceptor(credential));
        });
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy