Skip to content

Commit

Permalink
[ISSUE alibaba#11456]Fix unit test and rpc constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-98 committed Dec 24, 2023
1 parent 2498821 commit 153ad0a
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,55 @@
public class RpcClientTlsConfig extends TlsConfig {

/**
* get tls config from properties.
* @param properties Properties.
* get tls config from properties.
*
* @param properties Properties.
* @return tls of config.
*/
public static RpcClientTlsConfig properties(Properties properties) {
RpcClientTlsConfig tlsConfig = new RpcClientTlsConfig();
if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_ENABLE)) {
tlsConfig.setEnableTls(Boolean.parseBoolean(
properties.getProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE)));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE)) {
tlsConfig.setEnableTls(
Boolean.parseBoolean(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE)));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_MUTUAL_AUTH)) {
tlsConfig.setMutualAuthEnable(Boolean.parseBoolean(
properties.getProperty(RpcConstants.RPC_CLIENT_MUTUAL_AUTH)));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH)) {
tlsConfig.setMutualAuthEnable(
Boolean.parseBoolean(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH)));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_PROTOCOLS)) {
tlsConfig.setProtocols(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_PROTOCOLS));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_PROTOCOLS)) {
tlsConfig.setProtocols(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_PROTOCOLS));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_CIPHERS)) {
tlsConfig.setCiphers(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_CIPHERS));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_CIPHERS)) {
tlsConfig.setCiphers(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CIPHERS));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH)) {
tlsConfig.setTrustCollectionCertFile(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH)) {
tlsConfig.setTrustCollectionCertFile(
properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH)) {
tlsConfig.setCertChainFile(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH)) {
tlsConfig.setCertChainFile(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_CERT_KEY)) {
tlsConfig.setCertPrivateKey(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_CERT_KEY));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY)) {
tlsConfig.setCertPrivateKey(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_TRUST_ALL)) {
tlsConfig.setTrustAll(Boolean.parseBoolean(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_ALL)));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_ALL)) {
tlsConfig.setTrustAll(
Boolean.parseBoolean(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_ALL)));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_TRUST_PWD)) {
tlsConfig.setCertPrivateKeyPassword(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_PWD));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_PWD)) {
tlsConfig.setCertPrivateKeyPassword(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_PWD));
}

if (properties.containsKey(RpcConstants.RPC_CLIENT_TLS_PROVIDER)) {
tlsConfig.setSslProvider(properties.getProperty(RpcConstants.RPC_CLIENT_TLS_PROVIDER));
if (properties.containsKey(RpcConstants.RPC_SDK_CLIENT_TLS_PROVIDER)) {
tlsConfig.setSslProvider(properties.getProperty(RpcConstants.RPC_SDK_CLIENT_TLS_PROVIDER));
}
return tlsConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@
*/
public class RpcConstants {

public static final String NACOS_CLIENT_RPC = "nacos.remote.client.rpc";
public static final String NACOS_SDK_CLIENT_RPC = "nacos.remote.sdk.client.rpc";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_ENABLE = NACOS_CLIENT_RPC + ".tls.enable";
public static final String RPC_SDK_CLIENT_TLS_ENABLE = NACOS_SDK_CLIENT_RPC + ".tls.enable";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_PROVIDER = NACOS_CLIENT_RPC + ".tls.provider";
public static final String RPC_SDK_CLIENT_TLS_PROVIDER = NACOS_SDK_CLIENT_RPC + ".tls.provider";

@RpcConfigLabel
public static final String RPC_CLIENT_MUTUAL_AUTH = NACOS_CLIENT_RPC + ".tls.mutualAuth";
public static final String RPC_SDK_CLIENT_MUTUAL_AUTH = NACOS_SDK_CLIENT_RPC + ".tls.mutualAuth";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_PROTOCOLS = NACOS_CLIENT_RPC + ".tls.protocols";
public static final String RPC_SDK_CLIENT_TLS_PROTOCOLS = NACOS_SDK_CLIENT_RPC + ".tls.protocols";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_CIPHERS = NACOS_CLIENT_RPC + ".tls.ciphers";
public static final String RPC_SDK_CLIENT_TLS_CIPHERS = NACOS_SDK_CLIENT_RPC + ".tls.ciphers";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_CERT_CHAIN_PATH = NACOS_CLIENT_RPC + ".tls.certChainFile";
public static final String RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH = NACOS_SDK_CLIENT_RPC + ".tls.certChainFile";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_CERT_KEY = NACOS_CLIENT_RPC + ".tls.certPrivateKey";
public static final String RPC_SDK_CLIENT_TLS_CERT_KEY = NACOS_SDK_CLIENT_RPC + ".tls.certPrivateKey";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_TRUST_PWD = NACOS_CLIENT_RPC + ".tls.certPrivateKeyPassword";
public static final String RPC_SDK_CLIENT_TLS_TRUST_PWD = NACOS_SDK_CLIENT_RPC + ".tls.certPrivateKeyPassword";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH =
NACOS_CLIENT_RPC + ".tls.trustCollectionChainPath";
public static final String RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH =
NACOS_SDK_CLIENT_RPC + ".tls.trustCollectionChainPath";

@RpcConfigLabel
public static final String RPC_CLIENT_TLS_TRUST_ALL = NACOS_CLIENT_RPC + ".tls.trustAll";
public static final String RPC_SDK_CLIENT_TLS_TRUST_ALL = NACOS_SDK_CLIENT_RPC + ".tls.trustAll";

private static final Set<String> CONFIG_NAMES = new HashSet<>();

public static final String NACOS_CLUSTER_CLIENT_RPC = "nacos.remote.client.rpc";
public static final String NACOS_CLUSTER_CLIENT_RPC = "nacos.remote.cluster.client.rpc";

@RpcClusterConfigLabel
public static final String RPC_CLUSTER_CLIENT_TLS_ENABLE = NACOS_CLUSTER_CLIENT_RPC + ".tls.enable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,83 +24,83 @@
import static org.junit.Assert.assertTrue;

public class RpcClientTlsConfigTest {

@Test
public void testEnableTls() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE, "true");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE, "true");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertTrue(tlsConfig.getEnableTls());
}

@Test
public void testSslProvider() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_PROVIDER, "provider");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_PROVIDER, "provider");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("provider", tlsConfig.getSslProvider());
}

@Test
public void testMutualAuthEnable() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_MUTUAL_AUTH, "true");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH, "true");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertTrue(tlsConfig.getMutualAuthEnable());
}

@Test
public void testProtocols() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_PROTOCOLS, "protocols");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_PROTOCOLS, "protocols");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("protocols", tlsConfig.getProtocols());
}

@Test
public void testCiphers() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_CIPHERS, "ciphers");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CIPHERS, "ciphers");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("ciphers", tlsConfig.getCiphers());
}

@Test
public void testTrustCollectionCertFile() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH, "trustCollectionCertFile");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH, "trustCollectionCertFile");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("trustCollectionCertFile", tlsConfig.getTrustCollectionCertFile());
}

@Test
public void testCertChainFile() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH, "certChainFile");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH, "certChainFile");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("certChainFile", tlsConfig.getCertChainFile());
}

@Test
public void testCertPrivateKey() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_KEY, "certPrivateKey");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY, "certPrivateKey");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("certPrivateKey", tlsConfig.getCertPrivateKey());
}

@Test
public void testTrustAll() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_ALL, "true");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_ALL, "true");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertTrue(tlsConfig.getTrustAll());
}

@Test
public void testCertPrivateKeyPassword() {
Properties properties = new Properties();
properties.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_PWD, "trustPwd");
properties.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_PWD, "trustPwd");
RpcClientTlsConfig tlsConfig = RpcClientTlsConfig.properties(properties);
assertEquals("trustPwd", tlsConfig.getCertPrivateKeyPassword());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector;
import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig;
import com.alibaba.nacos.core.remote.tls.RpcSdkServerTlsConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -45,23 +45,23 @@
import static com.alibaba.nacos.test.naming.NamingBase.randomDomainName;

/**
* NamingCompatibilityServiceTls_ITCase.
* @author githucheng2978.
* @date .
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos",
RpcServerTlsConfig.PREFIX+".enableTls=true",
RpcServerTlsConfig.PREFIX+".compatibility=true",
RpcServerTlsConfig.PREFIX+".certChainFile=test-server-cert.pem",
RpcServerTlsConfig.PREFIX+".certPrivateKey=test-server-key.pem",
},
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos",
RpcSdkServerTlsConfig.PREFIX + ".enableTls=true", RpcSdkServerTlsConfig.PREFIX + ".compatibility=true",
RpcSdkServerTlsConfig.PREFIX + ".certChainFile=test-server-cert.pem", RpcSdkServerTlsConfig.PREFIX
+ ".certPrivateKey=test-server-key.pem"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class NamingCompatibilityServiceTls_ITCase {

private NamingMaintainService namingMaintainService;

private NamingService namingService;

private Instance instance;

private String serviceName;

@LocalServerPort
Expand Down Expand Up @@ -170,7 +170,7 @@ public void deleteService() throws NacosException {

Assert.assertTrue(namingMaintainService.deleteService(serviceName));
}

@After
public void tearDown() throws NacosException {
namingMaintainService.shutDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.remote.client.RpcConstants;
import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig;
import com.alibaba.nacos.core.remote.tls.RpcSdkServerTlsConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.FixMethodOrder;
Expand All @@ -43,22 +43,19 @@
import static com.alibaba.nacos.test.naming.NamingBase.randomDomainName;

/**
* NamingTlsServiceAndMutualAuth_ITCase.
*
* @author githucheng2978.
* @date .
**/
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringBootTest(classes = Nacos.class, properties = {
"server.servlet.context-path=/nacos",
RpcServerTlsConfig.PREFIX+".enableTls=true",
RpcServerTlsConfig.PREFIX+".mutualAuthEnable=true",
RpcServerTlsConfig.PREFIX+".compatibility=false",
RpcServerTlsConfig.PREFIX+".certChainFile=test-server-cert.pem",
RpcServerTlsConfig.PREFIX+".certPrivateKey=test-server-key.pem",
RpcServerTlsConfig.PREFIX+".trustCollectionCertFile=test-ca-cert.pem",

},
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos",
RpcSdkServerTlsConfig.PREFIX + ".enableTls=true", RpcSdkServerTlsConfig.PREFIX + ".mutualAuthEnable=true",
RpcSdkServerTlsConfig.PREFIX + ".compatibility=false",
RpcSdkServerTlsConfig.PREFIX + ".certChainFile=test-server-cert.pem",
RpcSdkServerTlsConfig.PREFIX + ".certPrivateKey=test-server-key.pem", RpcSdkServerTlsConfig.PREFIX
+ ".trustCollectionCertFile=test-ca-cert.pem"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Ignore("TODO, Fix cert expired problem")
public class NamingTlsServiceAndMutualAuth_ITCase {

Expand All @@ -69,12 +66,12 @@ public class NamingTlsServiceAndMutualAuth_ITCase {
@Test
public void test_a_MutualAuth() throws NacosException {
String serviceName = randomDomainName();
System.setProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE,"true");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH,"test-ca-cert.pem");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH,"test-client-cert.pem");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_KEY,"test-client-key.pem");
System.setProperty(RpcConstants.RPC_CLIENT_MUTUAL_AUTH,"true");
Instance instance = new Instance();
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE, "true");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH, "test-ca-cert.pem");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH, "test-client-cert.pem");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY, "test-client-key.pem");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH, "true");
Instance instance = new Instance();
instance.setIp("127.0.0.1");
instance.setPort(8081);
instance.setWeight(2);
Expand All @@ -101,12 +98,12 @@ public void test_a_MutualAuth() throws NacosException {
@Test(expected = NacosException.class)
public void test_b_MutualAuthClientTrustCa() throws NacosException {
String serviceName = randomDomainName();
System.setProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE,"true");
System.setProperty(RpcConstants.RPC_CLIENT_MUTUAL_AUTH,"true");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH,"");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_KEY,"");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH,"test-ca-cert.pem");
Instance instance = new Instance();
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE, "true");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH, "true");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH, "");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY, "");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH, "test-ca-cert.pem");
Instance instance = new Instance();
instance.setIp("127.0.0.1");
instance.setPort(8081);
instance.setWeight(2);
Expand All @@ -124,12 +121,12 @@ public void test_b_MutualAuthClientTrustCa() throws NacosException {
@Test(expected = NacosException.class)
public void test_c_MutualAuthClientTrustALl() throws NacosException {
String serviceName = randomDomainName();
System.setProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE,"true");
System.setProperty(RpcConstants.RPC_CLIENT_MUTUAL_AUTH,"true");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_CHAIN_PATH,"");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_CERT_KEY,"");
System.setProperty(RpcConstants.RPC_CLIENT_TLS_TRUST_ALL,"true");
Instance instance = new Instance();
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE, "true");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_MUTUAL_AUTH, "true");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH, "");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_CERT_KEY, "");
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_TRUST_ALL, "true");
Instance instance = new Instance();
instance.setIp("127.0.0.1");
instance.setPort(8081);
instance.setWeight(2);
Expand All @@ -144,7 +141,7 @@ public void test_c_MutualAuthClientTrustALl() throws NacosException {
}

@After
public void after(){
System.setProperty(RpcConstants.RPC_CLIENT_TLS_ENABLE,"");
public void after() {
System.setProperty(RpcConstants.RPC_SDK_CLIENT_TLS_ENABLE, "");
}
}
Loading

0 comments on commit 153ad0a

Please sign in to comment.