Java Code Examples for org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder#trustManager()

The following examples show how to use org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder#trustManager() . 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: GrpcServerProtocolClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public GrpcServerProtocolClient(RaftPeer target, int flowControlWindow,
    TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  raftPeerId = target.getId();
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConfig!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConfig.isFileBasedConfig()) {
      sslContextBuilder.trustManager(tlsConfig.getTrustStoreFile());
    } else {
      sslContextBuilder.trustManager(tlsConfig.getTrustStore());
    }
    if (tlsConfig.getMtlsEnabled()) {
      if (tlsConfig.isFileBasedConfig()) {
        sslContextBuilder.keyManager(tlsConfig.getCertChainFile(),
            tlsConfig.getPrivateKeyFile());
      } else {
        sslContextBuilder.keyManager(tlsConfig.getPrivateKey(),
            tlsConfig.getCertChain());
      }
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, peerId=" + raftPeerId
          + ", tlsConfig=" + tlsConfig, ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow).build();
  blockingStub = RaftServerProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftServerProtocolServiceGrpc.newStub(channel);
  this.requestTimeoutDuration = requestTimeoutDuration;
}
 
Example 2
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GrpcClientProtocolClient(ClientId id, RaftPeer target,
                                RaftProperties properties,
                                GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.getTrustStore() != null) {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      sslContextBuilder.keyManager(tlsConf.getCertChain(),
          tlsConf.getPrivateKey());
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
}
 
Example 3
Source File: GrpcService.java    From ratis with Apache License 2.0 5 votes vote down vote up
private GrpcService(RaftServer raftServer, Supplier<RaftPeerId> idSupplier, int port,
    SizeInBytes grpcMessageSizeMax, SizeInBytes appenderBufferSize,
    SizeInBytes flowControlWindow,TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  super(idSupplier, id -> new PeerProxyMap<>(id.toString(),
      p -> new GrpcServerProtocolClient(p, flowControlWindow.getSizeInt(),
          requestTimeoutDuration, tlsConfig)));
  if (appenderBufferSize.getSize() > grpcMessageSizeMax.getSize()) {
    throw new IllegalArgumentException("Illegal configuration: "
        + RaftServerConfigKeys.Log.Appender.BUFFER_BYTE_LIMIT_KEY + " = " + appenderBufferSize
        + " > " + GrpcConfigKeys.MESSAGE_SIZE_MAX_KEY + " = " + grpcMessageSizeMax);
  }

  NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forPort(port)
      .maxInboundMessageSize(grpcMessageSizeMax.getSizeInt())
      .flowControlWindow(flowControlWindow.getSizeInt())
      .addService(new GrpcServerProtocolService(idSupplier, raftServer))
      .addService(new GrpcClientProtocolService(idSupplier, raftServer))
      .addService(new GrpcAdminProtocolService(raftServer));

  if (tlsConfig != null) {
    SslContextBuilder sslContextBuilder =
        SslContextBuilder.forServer(tlsConfig.getCertChain(),
            tlsConfig.getPrivateKey());
    if (tlsConfig.getMtlsEnabled()) {
      sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
      sslContextBuilder.trustManager(tlsConfig.getCertChain());
    }
    sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, OPENSSL);
    try {
      nettyServerBuilder.sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, tlsConfig=" + tlsConfig, ex);
    }
  }
  server = nettyServerBuilder.build();
  addressSupplier = JavaUtils.memoize(() -> new InetSocketAddress(port != 0? port: server.getPort()));
}
 
Example 4
Source File: GrpcServerProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GrpcServerProtocolClient(RaftPeer target, int flowControlWindow,
    TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConfig!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConfig.getTrustStore() != null) {
      sslContextBuilder.trustManager(tlsConfig.getTrustStore());
    }
    if (tlsConfig.getMtlsEnabled()) {
      sslContextBuilder.keyManager(tlsConfig.getCertChain(),
          tlsConfig.getPrivateKey());
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, tlsConfig=" + tlsConfig, ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow).build();
  blockingStub = RaftServerProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftServerProtocolServiceGrpc.newStub(channel);
  this.requestTimeoutDuration = requestTimeoutDuration;
}
 
Example 5
Source File: XceiverClientGrpc.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private synchronized void connectToDatanode(DatanodeDetails dn)
    throws IOException {
  if (isConnected(dn)){
    return;
  }
  // read port from the data node, on failure use default configured
  // port.
  int port = dn.getPort(DatanodeDetails.Port.Name.STANDALONE).getValue();
  if (port == 0) {
    port = config.getInt(OzoneConfigKeys.DFS_CONTAINER_IPC_PORT,
        OzoneConfigKeys.DFS_CONTAINER_IPC_PORT_DEFAULT);
  }

  // Add credential context to the client call
  if (LOG.isDebugEnabled()) {
    LOG.debug("Nodes in pipeline : {}", pipeline.getNodes());
    LOG.debug("Connecting to server : {}", dn.getIpAddress());
  }
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forAddress(dn.getIpAddress(), port).usePlaintext()
          .maxInboundMessageSize(OzoneConsts.OZONE_SCM_CHUNK_MAX_SIZE)
          .intercept(new GrpcClientInterceptor());
  if (secConfig.isGrpcTlsEnabled()) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (caCert != null) {
      sslContextBuilder.trustManager(caCert);
    }
    if (secConfig.useTestCert()) {
      channelBuilder.overrideAuthority("localhost");
    }
    channelBuilder.useTransportSecurity().
        sslContext(sslContextBuilder.build());
  } else {
    channelBuilder.usePlaintext();
  }
  ManagedChannel channel = channelBuilder.build();
  XceiverClientProtocolServiceStub asyncStub =
      XceiverClientProtocolServiceGrpc.newStub(channel);
  asyncStubs.put(dn.getUuid(), asyncStub);
  channels.put(dn.getUuid(), channel);
}
 
Example 6
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
GrpcClientProtocolClient(ClientId id, RaftPeer target, RaftProperties properties, GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.isFileBasedConfig()) {
      sslContextBuilder.trustManager(tlsConf.getTrustStoreFile());
    } else {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      if (tlsConf.isFileBasedConfig()) {
        sslContextBuilder.keyManager(tlsConf.getCertChainFile(),
            tlsConf.getPrivateKeyFile());
      } else {
        sslContextBuilder.keyManager(tlsConf.getPrivateKey(),
            tlsConf.getCertChain());
      }
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(
          sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
  this.watchRequestTimeoutDuration =
      RaftClientConfigKeys.Rpc.watchRequestTimeout(properties);
}
 
Example 7
Source File: GrpcService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:ParameterNumber") // private constructor
private GrpcService(RaftServer raftServer, Supplier<RaftPeerId> idSupplier, int port,
    SizeInBytes grpcMessageSizeMax, SizeInBytes appenderBufferSize,
    SizeInBytes flowControlWindow,TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  super(idSupplier, id -> new PeerProxyMap<>(id.toString(),
      p -> new GrpcServerProtocolClient(p, flowControlWindow.getSizeInt(),
          requestTimeoutDuration, tlsConfig)));
  if (appenderBufferSize.getSize() > grpcMessageSizeMax.getSize()) {
    throw new IllegalArgumentException("Illegal configuration: "
        + RaftServerConfigKeys.Log.Appender.BUFFER_BYTE_LIMIT_KEY + " = " + appenderBufferSize
        + " > " + GrpcConfigKeys.MESSAGE_SIZE_MAX_KEY + " = " + grpcMessageSizeMax);
  }

  this.clientProtocolService = new GrpcClientProtocolService(idSupplier, raftServer);

  NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forPort(port)
      .withChildOption(ChannelOption.SO_REUSEADDR, true)
      .maxInboundMessageSize(grpcMessageSizeMax.getSizeInt())
      .flowControlWindow(flowControlWindow.getSizeInt())
      .addService(new GrpcServerProtocolService(idSupplier, raftServer))
      .addService(clientProtocolService)
      .addService(new GrpcAdminProtocolService(raftServer));

  if (tlsConfig != null) {
    SslContextBuilder sslContextBuilder =
        tlsConfig.isFileBasedConfig()?
            SslContextBuilder.forServer(tlsConfig.getCertChainFile(),
                tlsConfig.getPrivateKeyFile()):
            SslContextBuilder.forServer(tlsConfig.getPrivateKey(),
                tlsConfig.getCertChain());
    if (tlsConfig.getMtlsEnabled()) {
      sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
      if (tlsConfig.isFileBasedConfig()) {
        sslContextBuilder.trustManager(tlsConfig.getTrustStoreFile());
      } else {
          sslContextBuilder.trustManager(tlsConfig.getTrustStore());
      }
    }
    sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, OPENSSL);
    try {
      nettyServerBuilder.sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, tlsConfig=" + tlsConfig, ex);
    }
  }
  server = nettyServerBuilder.build();
  addressSupplier = JavaUtils.memoize(() -> new InetSocketAddress(port != 0? port: server.getPort()));
}