Gabriel39 commented on code in PR #70693: URL: https://github.com/apache/doris/pull/70693#discussion_r4600924590
########## be/src/util/private_rpc_tls.cpp: ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "util/private_rpc_tls.h" + +#include <brpc/channel.h> +#include <brpc/server.h> +#include <brpc/ssl_options.h> +#include <thrift/transport/TSSLServerSocket.h> +#include <thrift/transport/TSSLSocket.h> +#include <thrift/transport/TTransportException.h> + +#include <algorithm> +#include <exception> +#include <filesystem> +#include <limits> +#include <memory> +#include <system_error> +#include <utility> + +#include "common/config.h" +#include "util/string_util.h" +#include "util/thrift_server.h" + +namespace doris::rpc_tls { +namespace { + +Status validate_configuration() { + // Optional certificates cannot authenticate peers carrying private file-key RPC payloads. + if (config::tls_verify_mode != "verify_fail_if_no_peer_cert" || + config::tls_certificate_path.empty() || config::tls_private_key_path.empty() || + config::tls_ca_certificate_path.empty()) { + return Status::InvalidArgument( + "Private RPC TLS requires a certificate, private key, CA and mutual auth"); + } + if (config::tls_cert_refresh_interval_seconds != 0 || + !config::tls_peer_cert_required_san_dns.empty() || + !config::tls_private_key_password.empty()) { + return Status::NotSupported( + "Private RPC TLS requires static PEM identities and CA-scoped peer trust"); + } + // BRPC accepts either paths or inline PEM. Only operator-owned local files may reach that API, + // so malformed key input cannot be treated as an inline certificate and echoed by its parser. + for (const auto* path : {&config::tls_certificate_path, &config::tls_private_key_path, + &config::tls_ca_certificate_path}) { + std::error_code error; + if (!std::filesystem::is_regular_file(*path, error)) { + return Status::InvalidArgument( + "Private RPC TLS requires readable local identity files"); + } + } + return Status::OK(); +} + +class HandshakeSocket final : public apache::thrift::transport::TSSLSocket { +public: + HandshakeSocket(std::shared_ptr<apache::thrift::transport::SSLContext> context, + const std::string& host, int port, int timeout) + : TSSLSocket(std::move(context), host, port), _handshake_timeout(timeout) {} + + HandshakeSocket(std::shared_ptr<apache::thrift::transport::SSLContext> context, + THRIFT_SOCKET socket, std::shared_ptr<THRIFT_SOCKET> interrupt, int timeout) + : TSSLSocket(std::move(context), socket, std::move(interrupt)), + _handshake_timeout(timeout) {} + + void open() override { + const int connect_timeout = connTimeout_; + setConnTimeout(bounded_timeout(connect_timeout)); + try { + TSSLSocket::open(); + setConnTimeout(connect_timeout); + authenticate_peer(); + } catch (...) { + setConnTimeout(connect_timeout); + TSSLSocket::close(); + throw; + } + } + + bool peek() override { + // Pool probes after close() must report a closed transport without starting another handshake. + if (!TSocket::isOpen()) { + return false; + } + authenticate_peer(); + return TSSLSocket::peek(); + } + + uint32_t read(uint8_t* buffer, uint32_t size) override { + authenticate_peer(); + return TSSLSocket::read(buffer, size); + } + + void write(const uint8_t* buffer, uint32_t size) override { + authenticate_peer(); + TSSLSocket::write(buffer, size); + } + + uint32_t write_partial(const uint8_t* buffer, uint32_t size) override { + authenticate_peer(); + return TSSLSocket::write_partial(buffer, size); + } + + void flush() override { + // Thrift closes input and output transports separately, so the second flush must stay harmless. + if (!TSocket::isOpen()) { + TSSLSocket::flush(); + return; + } + authenticate_peer(); + TSSLSocket::flush(); + } + +private: + int bounded_timeout(int timeout) const { + return timeout > 0 ? std::min(timeout, _handshake_timeout) : _handshake_timeout; + } + + void authenticate_peer() { + if (checkHandshake()) { + return; + } + // Unlimited RPC reads must not let a TCP-only peer occupy a worker during TLS authentication. + // Restore the RPC timeouts before reading the first application byte after authentication. + const int read_timeout = recvTimeout_; + const int write_timeout = sendTimeout_; + setRecvTimeout(bounded_timeout(read_timeout)); + setSendTimeout(bounded_timeout(write_timeout)); + try { + initializeHandshake(); + } catch (...) { + TSSLSocket::close(); + setRecvTimeout(read_timeout); + setSendTimeout(write_timeout); + throw; + } + setRecvTimeout(read_timeout); + setSendTimeout(write_timeout); + } + + const int _handshake_timeout; +}; + +class HandshakeSocketFactory final : public apache::thrift::transport::TSSLSocketFactory { +public: + explicit HandshakeSocketFactory(int timeout) + : TSSLSocketFactory(apache::thrift::transport::TLSv1_2), _handshake_timeout(timeout) {} + + using TSSLSocketFactory::createSocket; + + std::shared_ptr<apache::thrift::transport::TSSLSocket> createSocket(const std::string& host, + int port) override { + auto socket = std::make_shared<HandshakeSocket>(ctx_, host, port, _handshake_timeout); + socket->server(false); + socket->access(std::make_shared<apache::thrift::transport::DefaultClientAccessManager>()); Review Comment: Fixed in 42f37562dada. The native cache still connects to its resolved IP, but restores the original DNS name before the lazy TLS handshake and certificate/SNI checks. A real FrontendService cache test uses a DNS-only certificate, exercises split fetches, reopen and cached reuse, and rejects a different hostname resolving to the same IP. The regression failed before the fix and now passes. ########## be/src/util/oss/client_connection_provider.cpp: ########## @@ -17,15 +17,19 @@ #include "util/client_connection_provider.h" +#include "util/private_rpc_tls.h" + namespace doris::client { Status configure_brpc_channel_options(brpc::ChannelOptions* options) { - (void)options; - return Status::OK(); + return rpc_tls::configure_channel(options); Review Comment: Fixed in 42f37562dada. OSS PFunctionService clients now retain the existing external-service plaintext policy for both cached and uncached channels, matching FE validation. Internal RPC clients still require private TLS. A real plaintext function service test covers check_fn and fn_call with private TLS enabled; it failed before the fix and now passes. Existing custom provider entry points remain unchanged. ########## fe/fe-core/src/main/java/org/apache/doris/common/OssThriftClientTransportProvider.java: ########## @@ -26,8 +29,9 @@ public class OssThriftClientTransportProvider implements ThriftClientTransportProvider { @Override public TTransport createTransport(TNetworkAddress address, int timeoutMs, boolean nonBlocking) throws Exception { - return nonBlocking - ? new TFramedTransport(new TSocket(address.hostname, address.port, timeoutMs)) + TSocket socket = PrivateRpcTls.enabled(Protocol.THRIFT) Review Comment: Fixed in 42f37562dada. Broker heartbeat and filesystem clients now share the existing OSS external-service policy on both FE and BE. Tests exercise the actual FE broker pool, filesystem factory, native broker cache and reconnects against real plaintext servers with private TLS enabled. The reported mismatch reproduced before the fix. A separate compatibility test preserves legacy custom Java provider overrides. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
