Skip to content

Commit

Permalink
[ISSUE alibaba#11456]Support TLS GRPC communication between clusters.
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-98 committed Jan 22, 2024
1 parent 5880368 commit 664f9e0
Show file tree
Hide file tree
Showing 52 changed files with 1,971 additions and 778 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import com.alibaba.nacos.common.remote.client.ConnectionEventListener;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfig;
import com.alibaba.nacos.common.remote.client.RpcSdkClientTlsConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
Expand Down Expand Up @@ -1032,7 +1032,7 @@ private RpcClient ensureRpcClient(String taskId) throws NacosException {
Map<String, String> newLabels = new HashMap<>(labels);
newLabels.put("taskId", taskId);
RpcClient rpcClient = RpcClientFactory.createClient(uuid + "_config-" + taskId, getConnectionType(),
newLabels, RpcClientTlsConfig.properties(this.properties));
newLabels, RpcSdkClientTlsConfig.properties(this.properties));
if (rpcClient.isWaitInitiated()) {
initRpcClientHandler(rpcClient);
rpcClient.setTenant(getTenant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfig;
import com.alibaba.nacos.common.remote.client.RpcSdkClientTlsConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
Expand Down Expand Up @@ -104,7 +104,7 @@ public NamingGrpcClientProxy(String namespaceId, SecurityProxy securityProxy, Se
labels.put(RemoteConstants.LABEL_MODULE, RemoteConstants.LABEL_MODULE_NAMING);
labels.put(Constants.APPNAME, AppNameUtils.getAppName());
this.rpcClient = RpcClientFactory.createClient(uuid, ConnectionType.GRPC, labels,
RpcClientTlsConfig.properties(properties.asProperties()));
RpcSdkClientTlsConfig.properties(properties.asProperties()));
this.redoService = new NamingGrpcRedoService(this, properties);
NAMING_LOGGER.info("Create naming rpc client for uuid->{}", uuid);
start(serverListFactory, serviceInfoHolder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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.
*/

package com.alibaba.nacos.common.remote;

/**
* Enum representing different types of communication.
*
* <p>CommunicationType includes:</p>
* <ul>
* <li>SDK: Communication between SDK and servers.</li>
* <li>CLUSTER: Communication between servers in a cluster.</li>
* </ul>
*
* @author stone-98
* @date 2023/12/23
*/
public enum CommunicationType {
/**
* Communication between SDK and servers.
*/
SDK("sdk"),
/**
* Communication between servers in a cluster.
*/
CLUSTER("cluster");

private final String type;

CommunicationType(String type) {
this.type = type;
}

public String getType() {
return type;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static RpcClient createClient(String clientName, ConnectionType connectio
}

public static RpcClient createClient(String clientName, ConnectionType connectionType, Map<String, String> labels,
RpcClientTlsConfig tlsConfig) {
RpcSdkClientTlsConfig tlsConfig) {
return createClient(clientName, connectionType, null, null, labels, tlsConfig);

}
Expand All @@ -97,7 +97,7 @@ public static RpcClient createClient(String clientName, ConnectionType connectio
* @return rpc client.
*/
public static RpcClient createClient(String clientName, ConnectionType connectionType, Integer threadPoolCoreSize,
Integer threadPoolMaxSize, Map<String, String> labels, RpcClientTlsConfig tlsConfig) {
Integer threadPoolMaxSize, Map<String, String> labels, RpcSdkClientTlsConfig tlsConfig) {

if (!ConnectionType.GRPC.equals(connectionType)) {
throw new UnsupportedOperationException("unsupported connection type :" + connectionType.getType());
Expand All @@ -110,30 +110,41 @@ public static RpcClient createClient(String clientName, ConnectionType connectio
}

/**
* create a rpc client.
* Creates an RPC client for cluster communication with default thread pool settings.
*
* @param clientName client name.
* @param connectionType client type.
* @return rpc client.
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param labels Additional labels for RPC-related attributes.
* @return An RPC client for cluster communication.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Map<String, String> labels) {
return createClusterClient(clientName, connectionType, null, null, labels);
}

/**
* Creates an RPC client for cluster communication with TLS configuration.
*
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param labels Additional labels for RPC-related attributes.
* @param tlsConfig TLS configuration for secure communication.
* @return An RPC client for cluster communication with TLS configuration.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Map<String, String> labels, RpcClientTlsConfig tlsConfig) {
Map<String, String> labels, RpcClusterClientTlsConfig tlsConfig) {
return createClusterClient(clientName, connectionType, null, null, labels, tlsConfig);
}

/**
* create a rpc client.
* Creates an RPC client for cluster communication with custom thread pool settings.
*
* @param clientName client name.
* @param connectionType client type.
* @param threadPoolCoreSize grpc thread pool core size
* @param threadPoolMaxSize grpc thread pool max size
* @return rpc client.
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param threadPoolCoreSize The core size of the gRPC thread pool.
* @param threadPoolMaxSize The maximum size of the gRPC thread pool.
* @param labels Additional labels for RPC-related attributes.
* @return An RPC client for cluster communication with custom thread pool settings.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Integer threadPoolCoreSize, Integer threadPoolMaxSize, Map<String, String> labels) {
Expand All @@ -151,10 +162,9 @@ public static RpcClient createClusterClient(String clientName, ConnectionType co
* @param tlsConfig tlsConfig.
* @return
*/

public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Integer threadPoolCoreSize, Integer threadPoolMaxSize, Map<String, String> labels,
RpcClientTlsConfig tlsConfig) {
RpcClusterClientTlsConfig tlsConfig) {
if (!ConnectionType.GRPC.equals(connectionType)) {
throw new UnsupportedOperationException("unsupported connection type :" + connectionType.getType());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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.
*/

package com.alibaba.nacos.common.remote.client;

import com.alibaba.nacos.common.remote.TlsConfig;
import com.alibaba.nacos.common.utils.StringUtils;

/**
* RPC Cluster Client TLS Configuration for Nacos.
* <p>
* This class extends the {@link TlsConfig} class and provides a convenient way to create a configuration instance
* specifically for the RPC (Remote Procedure Call) cluster client in Nacos.
* </p>
* <p>
* To configure RPC cluster client TLS settings, you can use the following system properties:
* </p>
* <ul>
* <li>{@code nacos.remote.cluster.client.rpc.tls.enable}: Enable or disable TLS. Default is {@code false}.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.Provider}: Specify the SSL provider.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.mutualAuth}: Enable or disable mutual authentication. Default is {@code false}.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.protocols}: Specify the TLS protocols.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.ciphers}: Specify the TLS ciphers.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.certChainFile}: Specify the path to the certificate chain file.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.certPrivateKey}: Specify the path to the certificate private key file.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.certPrivateKeyPassword}: Specify the password for the certificate private key.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.trustCollectionCertFile}: Specify the path to the trust collection chain file.</li>
* <li>{@code nacos.remote.cluster.client.rpc.tls.trustAll}: Enable or disable trusting all certificates. Default is {@code false}.</li>
* </ul>
*
* @author stone-98
* @date 2023/12/20
*/
public class RpcClusterClientTlsConfig extends TlsConfig {

/**
* Creates a new instance of {@link RpcClusterClientTlsConfig} by loading TLS configuration from system properties.
*
* @return A new instance of {@link RpcClusterClientTlsConfig} with loaded TLS configuration.
*/
public static RpcClusterClientTlsConfig createConfig() {
RpcClusterClientTlsConfig tlsConfig = new RpcClusterClientTlsConfig();
tlsConfig.setEnableTls(Boolean.getBoolean(RpcConstants.RPC_CLUSTER_CLIENT_TLS_ENABLE));
if (!Boolean.getBoolean(RpcConstants.RPC_CLUSTER_CLIENT_TLS_ENABLE)) {
return tlsConfig;
}

String sslProvider = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_PROVIDER);
if (StringUtils.isNotBlank(sslProvider)) {
tlsConfig.setSslProvider(sslProvider);
}

boolean mutualAuth = Boolean.getBoolean(RpcConstants.RPC_CLUSTER_CLIENT_MUTUAL_AUTH);
tlsConfig.setMutualAuthEnable(mutualAuth);

String protocols = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_PROTOCOLS);
if (StringUtils.isNotBlank(protocols)) {
tlsConfig.setProtocols(protocols);
}

String ciphers = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_CIPHERS);
if (StringUtils.isNotBlank(ciphers)) {
tlsConfig.setCiphers(ciphers);
}

String certChain = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_CERT_CHAIN_PATH);
if (StringUtils.isNotBlank(ciphers)) {
tlsConfig.setCertChainFile(certChain);
}

String certPrivateKey = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_CERT_KEY);
if (StringUtils.isNotBlank(certPrivateKey)) {
tlsConfig.setCertPrivateKey(certPrivateKey);
}

String certPrivateKeyPassword = System.getProperty(RpcConstants.RPC_CLUSTER_CLIENT_TLS_TRUST_PWD);
if (StringUtils.isNotBlank(certPrivateKeyPassword)) {
tlsConfig.setCertPrivateKeyPassword(certPrivateKeyPassword);
}

String trustCollectionCertFile = System.getProperty(
RpcConstants.RPC_CLUSTER_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH);
if (StringUtils.isNotBlank(trustCollectionCertFile)) {
tlsConfig.setTrustCollectionCertFile(trustCollectionCertFile);
}

boolean trustAll = Boolean.getBoolean(RpcConstants.RPC_CLUSTER_CLIENT_TLS_TRUST_ALL);
tlsConfig.setTrustAll(trustAll);

return tlsConfig;
}
}
Loading

0 comments on commit 664f9e0

Please sign in to comment.