Skip to content

Commit f25064c

Browse files
feat:support partial configuration encrypt. (#566)
1 parent 2d65ad3 commit f25064c

File tree

36 files changed

+790
-202
lines changed

36 files changed

+790
-202
lines changed

polaris-assembly/polaris-assembly-client/src/main/java/com/tencent/polaris/assembly/client/flow/DefaultAssemblyFlow.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,13 @@
1717

1818
package com.tencent.polaris.assembly.client.flow;
1919

20-
import java.util.Collections;
21-
import java.util.List;
22-
2320
import com.tencent.polaris.api.config.Configuration;
2421
import com.tencent.polaris.api.config.global.FlowConfig;
2522
import com.tencent.polaris.api.plugin.compose.Extensions;
2623
import com.tencent.polaris.api.plugin.loadbalance.LoadBalancer;
2724
import com.tencent.polaris.api.plugin.route.RouteInfo;
2825
import com.tencent.polaris.api.plugin.stat.TraceReporter;
29-
import com.tencent.polaris.api.pojo.Instance;
30-
import com.tencent.polaris.api.pojo.ServiceEventKey;
31-
import com.tencent.polaris.api.pojo.ServiceInfo;
32-
import com.tencent.polaris.api.pojo.ServiceInstances;
33-
import com.tencent.polaris.api.pojo.ServiceKey;
34-
import com.tencent.polaris.api.pojo.SourceService;
26+
import com.tencent.polaris.api.pojo.*;
3527
import com.tencent.polaris.api.rpc.RequestBaseEntity;
3628
import com.tencent.polaris.api.rpc.ServiceCallResult;
3729
import com.tencent.polaris.api.utils.CollectionUtils;
@@ -46,6 +38,9 @@
4638
import com.tencent.polaris.client.flow.ResourcesResponse;
4739
import com.tencent.polaris.discovery.client.flow.CommonInstancesRequest;
4840

41+
import java.util.Collections;
42+
import java.util.List;
43+
4944
public class DefaultAssemblyFlow implements AssemblyFlow {
5045

5146
private SDKContext sdkContext;

polaris-assembly/polaris-assembly-factory/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
<artifactId>event-logger</artifactId>
8585
<version>${project.version}</version>
8686
</dependency>
87+
<dependency>
88+
<groupId>com.tencent.polaris</groupId>
89+
<artifactId>event-tsf</artifactId>
90+
<version>${project.version}</version>
91+
</dependency>
8792

8893
<!--测试依赖插件-->
8994
<dependency>

polaris-auth/polaris-auth-factory/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<artifactId>polaris-auth-client</artifactId>
2121
<version>${project.version}</version>
2222
</dependency>
23+
<dependency>
24+
<groupId>com.tencent.polaris</groupId>
25+
<artifactId>auth-block-allow-list</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
2328
<dependency>
2429
<groupId>com.tencent.polaris</groupId>
2530
<artifactId>polaris-client</artifactId>

polaris-circuitbreaker/polaris-circuitbreaker-factory/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
<artifactId>event-logger</artifactId>
107107
<version>${project.version}</version>
108108
</dependency>
109+
<dependency>
110+
<groupId>com.tencent.polaris</groupId>
111+
<artifactId>event-tsf</artifactId>
112+
<version>${project.version}</version>
113+
</dependency>
109114

110115
<!--健康检查插件-->
111116
<dependency>

polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/flow/BaseFlow.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private static boolean loadLocalResources(ServiceEventKey svcEventKey, Resources
297297
}
298298

299299
public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria criteria,
300-
ServiceInstances dstInstances, List<WeightAdjuster> weightAdjusters) throws PolarisException {
300+
ServiceInstances dstInstances, List<WeightAdjuster> weightAdjusters) throws PolarisException {
301301
if (criteria == null) {
302302
criteria = new Criteria();
303303
}
@@ -357,6 +357,9 @@ public static void reportFlowEvent(Extensions extensions, FlowEvent flowEvent) {
357357
}
358358
for (EventReporter eventReporter : eventReporterList) {
359359
try {
360+
if (!eventReporter.isEnabled()) {
361+
continue;
362+
}
360363
if (!eventReporter.reportEvent(flowEvent)) {
361364
LOG.warn("Report event by {} failed. Flow event detail: {}", eventReporter.getName(), flowEvent);
362365
}

polaris-common/polaris-config-default/src/main/resources/conf/default-config.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ global:
8686
#描述: 事件上报插件名列表
8787
reporters:
8888
- logger
89+
# 描述:事件上报插件配置
90+
plugin:
91+
# 描述:TSF 事件上报插件配置
92+
tsf:
93+
# 描述:TSF 事件上报开关
94+
enable: false
8995
# 描述:Admin相关的配置
9096
admin:
9197
# 描述:Admin的监听的IP
@@ -286,7 +292,7 @@ consumer:
286292
weightAdjust:
287293
enable: true
288294
chain:
289-
# 开启了服务预热插件,可以支持多个动态权重调整插件同时生效
295+
# 开启了服务预热插件,可以支持多个动态权重调整插件同时生效
290296
# - warmup
291297
# 被调方配置
292298
provider:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>polaris-common</artifactId>
7+
<groupId>com.tencent.polaris</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>polaris-encrypt</artifactId>
14+
<name>Polaris Common Encrypt</name>
15+
<description>Polaris Common Encrypt JAR</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.tencent.polaris</groupId>
20+
<artifactId>polaris-model</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>${slf4j.version}</version>
27+
<scope>provided</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.bouncycastle</groupId>
31+
<artifactId>bcpkix-jdk15to18</artifactId>
32+
<version>${bouncycastle.version}</version>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
/**
21+
* TSF 配置加密提供器接口
22+
*
23+
* @author hongweizhu
24+
*/
25+
public abstract class ConfigEncryptProvider {
26+
27+
/**
28+
* 加密
29+
*
30+
* @param content 明文
31+
* @param password 密码
32+
* @return 密文
33+
*/
34+
public abstract String encrypt(String content, String password);
35+
36+
/**
37+
* 解密
38+
*
39+
* @param encryptedContent 密文
40+
* @param password 密码
41+
* @return 明文
42+
*/
43+
public abstract String decrypt(String encryptedContent, String password);
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
public class ConfigEncryptProviderFactory {
24+
25+
private static final Logger log = LoggerFactory.getLogger(ConfigEncryptProviderFactory.class);
26+
27+
private static ConfigEncryptProvider configEncryptProvider = null;
28+
29+
public static ConfigEncryptProvider getInstance() {
30+
if (null == configEncryptProvider) {
31+
try {
32+
Class<?> providerClass = Class.forName(EncryptConfig.getProviderClass());
33+
configEncryptProvider = (ConfigEncryptProvider) providerClass.newInstance();
34+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
35+
log.error("get config encrypt provider error", e);
36+
}
37+
}
38+
return configEncryptProvider;
39+
}
40+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
import com.tencent.polaris.api.utils.ClassUtils;
21+
import com.tencent.polaris.api.utils.StringUtils;
22+
23+
public class EncryptConfig {
24+
25+
private static final String TSF_PASSWORD_KEY = "tsf_config_encrypt_password";
26+
27+
private static final String PASSWORD_KEY = "config_encrypt_password";
28+
29+
static {
30+
// TSF 环境变量
31+
if (null != System.getenv(TSF_PASSWORD_KEY)) {
32+
password = System.getenv(TSF_PASSWORD_KEY);
33+
}
34+
// TSF JVM参数
35+
if (null != System.getProperty(TSF_PASSWORD_KEY)) {
36+
password = System.getProperty(TSF_PASSWORD_KEY);
37+
}
38+
// 环境变量
39+
if (null != System.getenv(PASSWORD_KEY)) {
40+
password = System.getenv(PASSWORD_KEY);
41+
}
42+
// JVM参数
43+
if (null != System.getProperty(PASSWORD_KEY)) {
44+
password = System.getProperty(PASSWORD_KEY);
45+
}
46+
}
47+
48+
/**
49+
* 加密前缀
50+
*/
51+
public static String ENCRYPT_PREFIX = "ENC(";
52+
/**
53+
* 加密后缀
54+
*/
55+
public static String ENCRYPT_SUFFIX = ")";
56+
57+
/**
58+
* 密码
59+
*/
60+
private static String password;
61+
62+
/**
63+
* 加解密提供器类名
64+
*/
65+
private static String providerClass = "com.tencent.polaris.encrypt.impl.ConfigEncryptAESProvider";
66+
67+
/**
68+
* 是否开启配置,判断 password 是否为空
69+
*/
70+
public static Boolean getEnabled() {
71+
return StringUtils.isNotBlank(password);
72+
}
73+
74+
public static String getPassword() {
75+
return EncryptConfig.password;
76+
}
77+
78+
public static void setPassword(String password) {
79+
EncryptConfig.password = password;
80+
}
81+
82+
public static ConfigEncryptProvider getProvider() {
83+
return ConfigEncryptProviderFactory.getInstance();
84+
}
85+
86+
public static String getProviderClass() {
87+
return providerClass;
88+
}
89+
90+
public static void setProviderClass(String providerClass) {
91+
EncryptConfig.providerClass = providerClass;
92+
}
93+
94+
/**
95+
* 是否需要进行解密
96+
*
97+
* @param content 判断对象
98+
* @return true:需要解密;false:不需要解密
99+
*/
100+
public static Boolean needDecrypt(Object content) {
101+
if (null == content || !ClassUtils.isClassPresent("org.bouncycastle.jce.provider.BouncyCastleProvider")) {
102+
return false;
103+
} else {
104+
String stringValue = String.valueOf(content);
105+
return stringValue.startsWith(ENCRYPT_PREFIX) && stringValue.endsWith(ENCRYPT_SUFFIX);
106+
}
107+
}
108+
109+
/**
110+
* 获取真实密文
111+
*
112+
* @param content 原始配置值
113+
* @return 真实密文
114+
*/
115+
public static String realContent(Object content) {
116+
if (null != content) {
117+
String stringValue = String.valueOf(content);
118+
return stringValue.substring(ENCRYPT_PREFIX.length(), stringValue.length() - ENCRYPT_SUFFIX.length());
119+
}
120+
return null;
121+
}
122+
}

0 commit comments

Comments
 (0)